From 15694b5c40883c66e8645823efd7655567cceefd Mon Sep 17 00:00:00 2001 From: antarcticrainforest Date: Fri, 15 Mar 2024 16:59:06 +0100 Subject: [PATCH] Smoke test for build container. --- .github/workflows/ci_job.yml | 33 ++++++++++++++++++++ dev-env/check-container.py | 58 ++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 dev-env/check-container.py diff --git a/.github/workflows/ci_job.yml b/.github/workflows/ci_job.yml index 709720dd..c180a6ef 100644 --- a/.github/workflows/ci_job.yml +++ b/.github/workflows/ci_job.yml @@ -68,6 +68,39 @@ jobs: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./docs/build/html + build-docker-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up services + run: | + docker-compose -f dev-env/docker-compose.yaml up -d --remove-orphans + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Build freva-storage-service image + uses: docker/build-push-action@v4 + with: + platforms: linux/amd64 + push: false + load: true + tags: freva-storage-service + + - name: Check freva-storage-service image + run: python3 dev-env/check-container.py + + dependabot: name: Merge PR by dependabot runs-on: ubuntu-latest diff --git a/dev-env/check-container.py b/dev-env/check-container.py new file mode 100644 index 00000000..d8bef6b6 --- /dev/null +++ b/dev-env/check-container.py @@ -0,0 +1,58 @@ +"""Simple script to start and stop the storage service.""" + +import logging +import subprocess +import time +import urllib.request +from pathlib import Path + +# Set up logging +logging.basicConfig( + format="%(name)s - %(levelname)s - %(message)s", + datefmt="[%X]", + level=logging.INFO, +) +logger = logging.getLogger("container-check") + + +def check_container(container_name: str = "databrowser") -> None: + """Check if the contianer starts up.""" + try: + process = subprocess.Popen( + [ + "docker", + "run", + "--net=host", + "-e", + "MONGO_USER=mongo", + "-e", + "MONGO_PASSWORD=secret", + "-e", + "MONGO_HOST=localhost:27017", + "-e", + "API_PORT=8080", + "-e", + "API_WORKER=8", + "-e", + "MONGO_DB=search_stats", + container_name, + ], + ) + time.sleep(5) + if process.poll() is not None: + raise RuntimeError("Container died.") + res = urllib.request.Request( + "http://localhost:8080/api/databrowser/overview", + ) + with urllib.request.urlopen(res) as response: + if response.getcode() != 200: + raise RuntimeError("Container not properly set up.") + except Exception as error: + logger.critical("Strting the container failed: %s", error) + raise + process.terminate() + logger.info("Container seems to work!") + + +if __name__ == "__main__": + check_container()