diff --git a/.github/workflows/python-ci-docker.yml b/.github/workflows/python-ci-docker.yml new file mode 100644 index 0000000..a38c28f --- /dev/null +++ b/.github/workflows/python-ci-docker.yml @@ -0,0 +1,44 @@ +# python-ci.yml +name: Test API on Push with Docker + +on: + push: + branches: [master, main] + pull_request: + branches: [master, main] + +jobs: + build-and-run-pytest: + runs-on: ubuntu-latest + + steps: + # 1. Checkout to the branch that triggered the event + - uses: actions/checkout@v3 + + # 2. Install python 3.10 + - name: Set up Python 3.10 + uses: actions/setup-python@v2 + with: + python-version: "3.10" + + # 3. Install python packages using a requirements file + - name: Install dependencies + run: | + python -m pip install --upgrade pip cython wheel + pip install -r tests/requirements.txt + + # 4. Build test image + - name: Build test Docker image + run: docker build -f tests/Dockerfile.test -t api-test . + + # 5. Run test container + - name: Run testing Container + run: docker run -d -e PORT=8000 --name api-test-container -p 8080:8000 api-test + + # 6. Run tests + - name: Run tests on API + run: make + + # Last step: Stop and remove container + - name: Stop API Container + run: docker stop api-test-container && docker rm api-test-container diff --git a/.github/workflows/python-ci.yml b/.github/workflows/python-ci.yml index 8142826..b603aba 100644 --- a/.github/workflows/python-ci.yml +++ b/.github/workflows/python-ci.yml @@ -3,43 +3,30 @@ name: Test API on Push on: push: - branches: [ master, main ] + branches: [master, main] pull_request: - branches: [ master, main ] + branches: [master, main] jobs: build-and-run-pytest: - runs-on: ubuntu-latest steps: - # 1. Checkout to the branch that triggered the event - - uses: actions/checkout@v3 - - # 2. Install python 3.10 - - name: Set up Python 3.10 - uses: actions/setup-python@v2 - with: - python-version: "3.10" - - # 3. Install python packages using a requirements file - - name: Install dependencies - run: | - python -m pip install --upgrade pip cython wheel - pip install -r tests/requirements.txt - - # 4. Build test image - - name: Build test Docker image - run: docker build -f tests/Dockerfile.test -t api-test . - - # 5. Run test container - - name: Run testing Container - run: docker run -d -e PORT=8000 --name api-test-container -p 8080:8000 api-test - - # 6. Run tests - - name: Run tests on API - run: make - - # Last step: Stop and remove container - - name: Stop API Container - run: docker stop api-test-container && docker rm api-test-container + # 1. Checkout to the branch that triggered the event + - uses: actions/checkout@v3 + + # 2. Install python 3.10 + - name: Set up Python 3.10 + uses: actions/setup-python@v2 + with: + python-version: "3.10" + + # 3. Install python packages using a requirements file + - name: Install dependencies + run: | + python -m pip install --upgrade pip cython wheel + pip install -r tests/requirements.txt + + # 4. Run tests + - name: Run tests on API + run: make diff --git a/Makefile b/Makefile index 1a00479..f2777b3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,39 @@ +PROJECT := hip-voyager-421200 +IMAGE := test-app +REGION := europe-west1 +DOCKER_REPO_NAME := my-docker-repo +TAG := 0.1 + +IMAGE_URI := $(REGION)-docker.pkg.dev/$(PROJECT)/$(DOCKER_REPO_NAME)/$(IMAGE):$(TAG) + default: pylint pytest +# ------------------------- +# Python checks +# ------------------------- + pylint: - find . -iname "*.py" -not -path "./tests/*" | xargs -n1 -I {} pylint --output-format=colorized {}; true + find . -iname "*.py" -not -path "./tests/*" | xargs -n1 pylint --output-format=colorized; true pytest: PYTHONDONTWRITEBYTECODE=1 pytest -v --color=yes + +# ------------------------- +# Docker +# ------------------------- + +build: + docker build --platform=linux/amd64 -t $(IMAGE_URI) . + +push: build + docker push $(IMAGE_URI) + +run: + docker run --rm -p 8080:8080 $(IMAGE_URI) + +# ------------------------- +# Debug helpers +# ------------------------- + +print-image: + @echo $(IMAGE_URI) diff --git a/api/fast.py b/api/fast.py index dc3b65e..5f49107 100644 --- a/api/fast.py +++ b/api/fast.py @@ -17,7 +17,8 @@ def root(): response = { 'greeting': 'Servus, griaß di!', # This is a typical Bavarian greeting ;) - 'timestamp': datetime.now() + 'timestamp': datetime.now(), + 'greeting_german': "Servus, grüß Dich" } return response diff --git a/tests/test_api_root.py b/tests/test_api_root.py deleted file mode 100644 index 87bc7b3..0000000 --- a/tests/test_api_root.py +++ /dev/null @@ -1,17 +0,0 @@ -# pylint: disable-all - -import unittest -import requests -import time - -class TestApiRoot(unittest.TestCase): - def test_api_root(self): - # Adding a 15s sleep timer to ensure - # that the container has time to start up - time.sleep(15) - - # Call API and assign to a variable - url = 'http://localhost:8080' - result = requests.get(url).json() - - self.assertEqual(result['greeting'], 'Servus, griaß di!')