diff --git a/.github/workflows/ci-build.yaml b/.github/workflows/ci-build.yaml index 3c12436..96dcb12 100644 --- a/.github/workflows/ci-build.yaml +++ b/.github/workflows/ci-build.yaml @@ -4,7 +4,7 @@ on: push: branches: [master] paths: - - "src/**" + - 'src/**' pull_request: env: @@ -13,31 +13,45 @@ env: jobs: test: - name: "Tests & Linting" + name: 'Tests & Linting' runs-on: ubuntu-latest steps: - - name: "Checkout" + - name: 'Checkout' uses: actions/checkout@v2 - - name: "Run linting" + - name: 'Run linting' run: make lint - - name: "Run tests" + - name: 'Run tests' run: make test-report + - name: 'Upload test results' + uses: actions/upload-artifact@v2 + # Disabled when running locally with the nektos/act tool + if: ${{ always() && !env.ACT }} + with: + name: test-results + path: ./test-results.xml + + - name: Publish Unit Test Results + uses: EnricoMi/publish-unit-test-result-action@v1 + if: ${{ always() && !env.ACT }} + with: + files: test-results.xml + build: - name: "Build & Push Image" + name: 'Build & Push Image' needs: test runs-on: ubuntu-latest steps: - - name: "Checkout" + - name: 'Checkout' uses: actions/checkout@v2 # Nicer than using github runid, I think, will be picked up automatically by make - - name: "Create datestamp image tag" + - name: 'Create datestamp image tag' run: echo "IMAGE_TAG=$(date +%d-%m-%Y.%H%M)" >> $GITHUB_ENV - - name: "Docker build image" + - name: 'Docker build image' run: make image # Only when pushing to default branch (e.g. master or main), then push image to registry @@ -47,18 +61,18 @@ jobs: echo ${{ secrets.GITHUB_TOKEN }} | docker login $IMAGE_REG -u $GITHUB_ACTOR --password-stdin make push - - name: "Trigger AKS release pipeline" + - name: 'Trigger AKS release pipeline' if: github.ref == 'refs/heads/master' uses: benc-uk/workflow-dispatch@v1 with: - workflow: "CD Release - AKS" + workflow: 'CD Release - AKS' token: ${{ secrets.GH_PAT }} inputs: '{ "IMAGE_TAG": "${{ env.IMAGE_TAG }}" }' - - name: "Trigger Azure web app release pipeline" + - name: 'Trigger Azure web app release pipeline' if: github.ref == 'refs/heads/master' uses: benc-uk/workflow-dispatch@v1 with: - workflow: "CD Release - Webapp" + workflow: 'CD Release - Webapp' token: ${{ secrets.GH_PAT }} inputs: '{ "IMAGE_TAG": "${{ env.IMAGE_TAG }}" }' diff --git a/.gitignore b/.gitignore index 5e128b2..b2a3b6d 100644 --- a/.gitignore +++ b/.gitignore @@ -100,4 +100,8 @@ venv.bak/ # mypy .mypy_cache/ .secrets -.env \ No newline at end of file +.env +test-*.xml +tests/node_modules +tests/package*.json +.pytest_cache \ No newline at end of file diff --git a/README.md b/README.md index bbec83d..7f34e9d 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,8 @@ push ๐ค Push container image to registry run ๐ Run the server locally using Python & Flask deploy ๐ Deploy to Azure Web App undeploy ๐ Remove from Azure -test ๐ฏ Unit tests for server and frontend -test-report ๐ฏ Unit tests for server and frontend (with report output) +test ๐ฏ Unit tests for Flask app +test-report ๐ฏ Unit tests for Flask app (with report output) test-api ๐ฆ Run integration API tests, server must be running clean ๐งน Clean up project ``` diff --git a/build/Dockerfile b/build/Dockerfile index d72049e..ae0775a 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -1,6 +1,6 @@ FROM python:3.9-slim-buster -LABEL Name="Python Flask Demo App" Version=1.4.1 +LABEL Name="Python Flask Demo App" Version=1.4.2 LABEL org.opencontainers.image.source = "https://github.com/benc-uk/python-demoapp" ARG srcDir=src diff --git a/makefile b/makefile index 7da35f5..cebf7c5 100644 --- a/makefile +++ b/makefile @@ -52,10 +52,13 @@ undeploy: ## ๐ Remove from Azure @echo "### WARNING! Going to delete $(AZURE_RES_GROUP) ๐ฒ" az group delete -n $(AZURE_RES_GROUP) -o table --no-wait -test: ## ๐ฏ Unit tests for server and frontend - @echo "Not implemented!" +test: venv ## ๐ฏ Unit tests for Flask app + . $(SRC_DIR)/.venv/bin/activate \ + && pytest -v -test-report: test ## ๐ฏ Unit tests for server and frontend (with report output) +test-report: venv ## ๐ฏ Unit tests for Flask app (with report output) + . $(SRC_DIR)/.venv/bin/activate \ + && pytest -v --junitxml=test-results.xml test-api: .EXPORT_ALL_VARIABLES ## ๐ฆ Run integration API tests, server must be running cd tests \ @@ -66,6 +69,11 @@ clean: ## ๐งน Clean up project rm -rf $(SRC_DIR)/.venv rm -rf tests/node_modules rm -rf tests/package* + rm -rf test-results.xml + rm -rf $(SRC_DIR)/app/__pycache__ + rm -rf $(SRC_DIR)/app/tests/__pycache__ + rm -rf .pytest_cache + rm -rf $(SRC_DIR)/.pytest_cache # ============================================================================ diff --git a/src/app/__init__.py b/src/app/__init__.py index 94bc434..3bab841 100644 --- a/src/app/__init__.py +++ b/src/app/__init__.py @@ -1,6 +1,9 @@ from flask import Flask -app = Flask(__name__) -from app import views # noqa: E402,F401 -from app import apis # noqa: E402,F401 +def create_app(): + app = Flask(__name__) + with app.app_context(): + from . import views # noqa: E402,F401 + from . import apis # noqa: E402,F401 + return app diff --git a/src/app/apis.py b/src/app/apis.py index 818b2c7..8543c57 100644 --- a/src/app/apis.py +++ b/src/app/apis.py @@ -1,5 +1,4 @@ -from flask import jsonify -from app import app +from flask import jsonify, current_app as app import psutil olddata = {} diff --git a/src/app/conftest.py b/src/app/conftest.py new file mode 100644 index 0000000..6269e6e --- /dev/null +++ b/src/app/conftest.py @@ -0,0 +1,10 @@ +from . import create_app +import pytest + +app = create_app() + + +@pytest.fixture +def client(): + with app.test_client() as client: + yield client \ No newline at end of file diff --git a/src/app/templates/base.html b/src/app/templates/base.html index 720f61c..a1a6fe7 100644 --- a/src/app/templates/base.html +++ b/src/app/templates/base.html @@ -63,6 +63,6 @@ >