Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Add smoke tests #276

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion templates/conftest_flask_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def app_with_db():
}
app = create_app(config_override)
db = engine.connect(host=app.config.get("DATABASE_URI")) # noqa: F841
seeder.seed_data(pathlib.Path(__file__).parent.parent / "seed_data.json", drop=True)
seeder.seed_data(pathlib.Path(__file__).parent.parent.parent / "seed_data.json", drop=True)

# establish an application context before running the tests
yield app
Expand Down
2 changes: 1 addition & 1 deletion templates/conftest_flask_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def app_with_db():
with app.app_context():
engines = db.engines
db.create_all()
seeder.seed_data(db, pathlib.Path(__file__).parent.parent / "seed_data.json")
seeder.seed_data(db, pathlib.Path(__file__).parent.parent.parent / "seed_data.json")

engine_cleanup = []

Expand Down
Empty file added tests/__init__.py
Empty file.
54 changes: 46 additions & 8 deletions {{cookiecutter.__src_folder_name}}/.github/workflows/azure-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ on:

# GitHub Actions workflow to deploy to Azure using azd
# To configure required secrets for connecting to Azure, simply run `azd pipeline config`

# Set up permissions for deploying with secretless Azure federated credentials
# https://learn.microsoft.com/en-us/azure/developer/github/connect-from-azure?tabs=azure-portal%2Clinux#set-up-azure-login-with-openid-connect-authentication
permissions:
id-token: write
contents: read

jobs:
build:
runs-on: ubuntu-latest
outputs:
uri: ${{ steps.output.outputs.uri }}
env:
AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }}
AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
Expand All @@ -27,10 +29,10 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install azd
uses: Azure/[email protected]

- name: Log in with Azure (Federated Credentials)
if: ${{ env.AZURE_CLIENT_ID != '' }}
run: |
Expand All @@ -39,31 +41,67 @@ jobs:
--federated-credential-provider "github" `
--tenant-id "$Env:AZURE_TENANT_ID"
shell: pwsh

- name: Log in with Azure (Client Credentials)
if: ${{ env.AZURE_CREDENTIALS != '' }}
run: |
$info = $Env:AZURE_CREDENTIALS | ConvertFrom-Json -AsHashtable;
Write-Host "::add-mask::$($info.clientSecret)"

azd auth login `
--client-id "$($info.clientId)" `
--client-secret "$($info.clientSecret)" `
--tenant-id "$($info.tenantId)"
shell: pwsh
env:
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}

- name: Provision Infrastructure
run: azd provision --no-prompt
env:
AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}
AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}
AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}

- name: Deploy Application
run: azd deploy --no-prompt
env:
AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}
AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}
AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}

- name: Output Deployment URI
id: output
run: |
azd env get-values > .env
source .env
echo "uri=$BACKEND_URI" >> "$GITHUB_OUTPUT"

smoketests:
runs-on: ubuntu-latest
needs: build
steps:

- name: Basic smoke test (curl)
env:
URI: ${{needs.build.outputs.uri}}
run: |
echo "Sleeping 1 minute due to https://github.com/Azure/azure-dev/issues/2669"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder if we want to do this a fancier way. We could even write a Python script using tenacity to check for 200 status with backoff and 10 attempts, that sort of thing. Or we could do that in bash (barf).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can make it cooler later... this works for now (unless it doesn't)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does seem to work so far! We'll see what happens when we cruft update on all 17 repos.

sleep 60
curl -sSf $URI
- name: Checkout
uses: actions/checkout@v3

- name: Setup python
uses: actions/setup-python@v4
with:
python-version: 3.12

- name: End-to-end smoke tests (playwright)
env:
URI: ${{needs.build.outputs.uri}}
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements-dev.in
python3 -m playwright install --with-deps
python3 -m pytest --exitfirst src/tests/smoke/smoketests.py --live-server-url $URI
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
{% if cookiecutter.project_backend == "fastapi" %}
python3 src/fastapi_app/seed_data.py
{% endif %}
python3 -m pytest
python3 -m pytest
env:
{% if "postgres" in cookiecutter.db_resource %}
POSTGRES_HOST: localhost
Expand Down
2 changes: 2 additions & 0 deletions {{cookiecutter.__src_folder_name}}/infra/aca.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,5 @@ output SERVICE_WEB_IDENTITY_PRINCIPAL_ID string = webIdentity.properties.princip
output SERVICE_WEB_NAME string = app.outputs.name
output SERVICE_WEB_URI string = app.outputs.uri
output SERVICE_WEB_IMAGE_NAME string = app.outputs.imageName

output uri string = app.outputs.uri
2 changes: 2 additions & 0 deletions {{cookiecutter.__src_folder_name}}/infra/appservice.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ module webKeyVaultAccess './core/security/keyvault-access.bicep' = {
}

output SERVICE_WEB_IDENTITY_PRINCIPAL_ID string = web.outputs.identityPrincipalId

output uri string = web.outputs.uri
2 changes: 2 additions & 0 deletions {{cookiecutter.__src_folder_name}}/infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,5 @@ output SERVICE_WEB_IMAGE_NAME string = web.outputs.SERVICE_WEB_IMAGE_NAME
output AZURE_KEY_VAULT_ENDPOINT string = keyVault.outputs.endpoint
output AZURE_KEY_VAULT_NAME string = keyVault.outputs.name
output APPLICATIONINSIGHTS_NAME string = monitoring.outputs.applicationInsightsName

output BACKEND_URI string = web.outputs.uri
11 changes: 11 additions & 0 deletions {{cookiecutter.__src_folder_name}}/src/tests/smoke/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Smoke tests

This directory contains smoke tests for the project.
These tests are meant to be run against a running instance of the project,
not against a local development server.

See azure-dev.yaml for an example of how to run these tests in GitHub actions.

```
python3 -m pytest --exitfirst src/tests/smoke/smoketests.py --live-server-url $URI
```
15 changes: 15 additions & 0 deletions {{cookiecutter.__src_folder_name}}/src/tests/smoke/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest


def pytest_addoption(parser):
parser.addoption(
"--live-server-url",
action="store",
default="http://localhost:8000",
help="URL for the live server to test against",
)


@pytest.fixture(scope="function")
def live_server_url(request):
return request.config.getoption("--live-server-url")
10 changes: 10 additions & 0 deletions {{cookiecutter.__src_folder_name}}/src/tests/smoke/smoketests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ruff: noqa: F401

# Import only the tests that we want to run for smoke testing
from ..local.test_playwright import (
test_about,
test_destination_options_have_cruises,
test_destinations,
test_home,
test_request_information,
)
Loading