Skip to content

Commit a74bfff

Browse files
committed
feat: snaphost testing implementation
1 parent 64bb1e6 commit a74bfff

31 files changed

+68227
-7
lines changed

.github/workflows/cicd-1-pull-request.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ permissions:
1313
contents: read
1414
id-token: write
1515
pull-requests: write
16+
packages: write
1617

1718
jobs:
1819
metadata:
@@ -127,11 +128,14 @@ jobs:
127128
if: needs.metadata.outputs.does_pull_request_exist == 'true' || github.ref == 'refs/heads/main' || (github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'reopened'))
128129
with:
129130
docker_compose_file: application/CohortManager/compose.yaml
131+
snapshot_test_dir: tests/snapshot-tests
132+
app_dir: application/CohortManager
130133
excluded_containers_csv_list: azurite,azurite-setup,sql-server
131134
environment_tag: ${{ needs.metadata.outputs.environment_tag }}
132135
function_app_source_code_path: application/CohortManager/src
133136
project_name: cohort-manager
134137
build_all_images: true
138+
gh_registry: ghcr.io
135139
secrets: inherit
136140
acceptance-stage: # Recommended maximum execution time is 10 minutes
137141
name: "Acceptance stage"

.github/workflows/stage-3-build-images.yaml

Lines changed: 147 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ on:
2929
required: false
3030
type: boolean
3131
default: false
32+
app_dir:
33+
description: Directory containing application code and compose files
34+
required: true
35+
type: string
36+
snapshot_test_dir:
37+
description: Directory containing the snapshot tests
38+
required: true
39+
type: string
40+
gh_registry:
41+
description: GitHub registry to push images for Snapshot Testing
42+
required: true
43+
type: string
44+
default: ghcr.io
3245

3346
jobs:
3447
get-functions:
@@ -69,6 +82,7 @@ jobs:
6982
id-token: write
7083
contents: read
7184
pull-requests: read
85+
packages: write
7286
needs: get-functions
7387
strategy:
7488
matrix:
@@ -91,6 +105,13 @@ jobs:
91105
path: templates
92106
ref: main
93107

108+
- name: Log in to the GitHub Container registry
109+
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1
110+
with:
111+
registry: ${{ inputs.gh_registry }}
112+
username: ${{ github.actor }}
113+
password: ${{ secrets.GITHUB_TOKEN }}
114+
94115
- name: Az CLI login
95116
if: github.ref == 'refs/heads/main'
96117
uses: azure/login@v2
@@ -135,6 +156,7 @@ jobs:
135156
env:
136157
COMPOSE_FILE: ${{ inputs.docker_compose_file }}
137158
PROJECT_NAME: ${{ inputs.project_name }}
159+
GH_REGISTRY: ${{ inputs.gh_registry }}
138160
run: |
139161
function=${{ matrix.function }}
140162
@@ -173,20 +195,29 @@ jobs:
173195
docker push "${repo_name}:${ENVIRONMENT_TAG}"
174196
fi
175197
198+
echo Tagging the image for GitHub Registry...
199+
GH_IMAGE="${{ inputs.gh_registry }}/${{ github.repository_owner }}/${{ inputs.project_name }}-${{ matrix.function }}:latest"
200+
# make it lowercase
201+
GH_IMAGE_ID="${GH_IMAGE,,}"
202+
docker tag ${PROJECT_NAME}-${function}:latest $GH_IMAGE_ID
203+
204+
echo Pushing the image into GitHub Registry...
205+
docker push $GH_IMAGE_ID
206+
176207
export SBOM_REPOSITORY_REPORT="sbom-${function}-repository-report"
177208
echo "SBOM_REPOSITORY_REPORT=$SBOM_REPOSITORY_REPORT" >> $GITHUB_ENV
178209
bash -x ${GITHUB_WORKSPACE}/templates/scripts/reports/create-sbom-report.sh
179210
180211
export VULNERABILITIES_REPOSITORY_REPORT="vulnerabilities-${function}-repository-report"
181212
echo "VULNERABILITIES_REPOSITORY_REPORT=$VULNERABILITIES_REPOSITORY_REPORT" >> $GITHUB_ENV
182213
183-
echo "Running the scan-vulnerabilities script in a look with 10 minutes timeout, with 3 retries..."
214+
echo "Running the scan-vulnerabilities script in a look with 5 minutes timeout, with 3 retries..."
184215
retries=3
185-
delay=300 # 5 minutes
216+
delay=30
186217
count=0
187218
until [ $count -ge $retries ]
188219
do
189-
timeout 10m bash -x ${GITHUB_WORKSPACE}/templates/scripts/reports/scan-vulnerabilities.sh && break
220+
timeout 5m bash -x ${GITHUB_WORKSPACE}/templates/scripts/reports/scan-vulnerabilities.sh && break
190221
count=$((count+1))
191222
echo "Attempt $count/$retries failed, retrying after $delay seconds..."
192223
sleep $delay
@@ -275,6 +306,119 @@ jobs:
275306
path: ./${{ env.VULNERABILITIES_SUMMARY_LOGFILE }}
276307
retention-days: 21
277308

309+
snapshot-tests:
310+
name: Snapshot tests
311+
runs-on: ubuntu-latest
312+
permissions:
313+
packages: read
314+
needs:
315+
- get-functions
316+
- build-and-push
317+
env:
318+
SNAPSHOT_TEST_DIR: ${{ inputs.snapshot_test_dir }}
319+
APP_DIR: ${{ inputs.app_dir }}
320+
steps:
321+
- name: Checkout code
322+
uses: actions/checkout@v4
323+
with:
324+
submodules: true
325+
326+
- name: Check runner resources before download
327+
run: |
328+
echo "--- DISK SPACE ---"
329+
df -h
330+
echo "--- MEMORY ---"
331+
free -h
332+
333+
- name: Log in to the GitHub Container registry
334+
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1
335+
with:
336+
registry: ${{ inputs.gh_registry }}
337+
username: ${{ github.actor }}
338+
password: ${{ secrets.GITHUB_TOKEN }}
339+
340+
- name: Generate GHCR override file
341+
working-directory: ${{ inputs.app_dir }}
342+
env:
343+
PROJECT_NAME: ${{ inputs.project_name }}
344+
run: |
345+
346+
echo "Testing the access to get-functions output"
347+
echo ${{ needs.get-functions.outputs.FUNC_NAMES }}
348+
echo "___________________"
349+
350+
echo "Generating compose.ghcr.yaml file..."
351+
echo "services:" > compose.ghcr.yaml
352+
for function in $(echo '${{ needs.get-functions.outputs.FUNC_NAMES }}' | jq -r '.[]'); do
353+
# Skipping services we don't need during local testing on the agent
354+
if [[ "$function" == "web" || "$function" == "wiremock" ]]; then
355+
echo "Skipping ${function}..."
356+
continue
357+
fi
358+
RAW_IMAGE_URL="${{ inputs.gh_registry }}/${{ github.repository_owner }}/${PROJECT_NAME}-${function}:latest"
359+
LOWERCASE_IMAGE_URL="${RAW_IMAGE_URL,,}"
360+
echo " ${function}:" >> compose.ghcr.yaml
361+
echo " image: ${LOWERCASE_IMAGE_URL}" >> compose.ghcr.yaml
362+
echo " build: {}" >> compose.ghcr.yaml
363+
done
364+
echo "--- Generated compose.ghcr.yaml ---"
365+
cat compose.ghcr.yaml
366+
echo "----------------------------------------"
367+
368+
- name: Run application using GHCR images
369+
working-directory: ${{ inputs.app_dir }}
370+
env:
371+
PROJECT_NAME: ${{ inputs.project_name }}
372+
SNAPSHOT_PASSWORD: ${{ secrets.SNAPSHOT_PASSWORD }}
373+
SNAPSHOT_AZURITE_CONNECTION_STRING: ${{ secrets.SNAPSHOT_AZURITE_CONNECTION_STRING }}
374+
SNAPSHOT_DB_NAME: ${{ vars.SNAPSHOT_DB_NAME }}
375+
run: |
376+
377+
echo "Creating .env file for Docker Compose..."
378+
cat > .env <<EOL
379+
PASSWORD=$SNAPSHOT_PASSWORD
380+
AZURITE_CONNECTION_STRING=$SNAPSHOT_AZURITE_CONNECTION_STRING
381+
DB_NAME=$SNAPSHOT_DB_NAME
382+
EOL
383+
384+
echo "Check the .env file"
385+
cat .env
386+
387+
echo "Build the dependencies needed for local operation..."
388+
docker compose -f compose.deps.yaml -p ${PROJECT_NAME} --profile "*" build --no-cache
389+
390+
# Run docker compose using the base file
391+
echo "Run docker compose deps..."
392+
docker compose -f compose.deps.yaml up -d --no-build
393+
echo "Waiting 10s for the depencies to be accessible..."
394+
sleep 10
395+
396+
echo "Run docker compose app with the GHCR..."
397+
docker compose -f compose.data-services.yaml -f compose.core.yaml -f compose.cohort-distribution.yaml -f compose.ghcr.yaml up -d --no-build
398+
echo "Waiting 10s for the app to be accessible..."
399+
sleep 10
400+
401+
- name: Check runner resources after starting the application
402+
run: |
403+
echo "--- DISK SPACE ---"
404+
df -h
405+
echo "--- MEMORY ---"
406+
free -h
407+
408+
- name: print DB logs
409+
run: docker logs db
410+
411+
- name: check container statuses
412+
run: docker ps -a
413+
414+
- name: Run snapshot tests
415+
working-directory: ${{ inputs.snapshot_test_dir }}
416+
run: bash run-snapshot-tests.sh
417+
418+
- name: Teardown application
419+
working-directory: ${{ env.APP_DIR }}
420+
run: docker compose down && docker compose -f compose.deps.yaml down
421+
278422
aggregate-json:
279423
runs-on: ubuntu-latest
280424
needs: build-and-push

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ application/CohortManager/src/Functions/*.json
119119
# Explicitly include test JSON files
120120
!**/playwright-tests/src/tests/e2e/testFiles/**/*.json
121121

122+
# Snapshot tests
123+
!tests/snapshot-tests/*.parquet
124+
!tests/snapshot-tests/appsettings.json
125+
122126
# Act
123127
.act.secrets
124128
.act.vars

application/CohortManager/compose.deps.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ services:
1717
build:
1818
context: ./
1919
dockerfile: ./Set-up/azurite/Dockerfile
20+
image: cohort-manager-deps-azurite-setup
2021
networks: [cohman-network]
2122
depends_on:
2223
- azurite
@@ -64,6 +65,7 @@ services:
6465
build:
6566
context: ./src/Functions/
6667
dockerfile: Shared/DataServices.Migrations/Dockerfile
68+
image: cohort-manager-deps-db-migration
6769
depends_on:
6870
- db
6971
environment:

application/CohortManager/src/Functions/Shared/DataServices.Database/DataServicesContext.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public class DataServicesContext : DbContext
99
DbSet<LanguageCode> languageCodes { get; set; }
1010
DbSet<CurrentPosting> currentPostings { get; set; }
1111
DbSet<ExcludedSMULookup> excludedSMULookups { get; set; }
12-
DbSet<ParticipantManagement> participantManagements { get; set; }
13-
DbSet<ParticipantDemographic> participantDemographics { get; set; }
12+
public DbSet<ParticipantManagement> participantManagements { get; set; }
13+
public DbSet<ParticipantDemographic> participantDemographics { get; set; }
1414
DbSet<GeneCodeLkp> geneCodeLkps { get; set; }
1515
DbSet<HigherRiskReferralReasonLkp> higherRiskReferralReasonLkps { get; set; }
16-
DbSet<ExceptionManagement> exceptionManagements { get; set; }
17-
DbSet<CohortDistribution> cohortDistributions { get; set; }
16+
public DbSet<ExceptionManagement> exceptionManagements { get; set; }
17+
public DbSet<CohortDistribution> cohortDistributions { get; set; }
1818
DbSet<BsSelectRequestAudit> bsSelectRequestAudits { get; set; }
1919
DbSet<ScreeningLkp> screeningLkps { get; set; }
2020
DbSet<BsoOrganisation> bsoOrganisations { get; set; }
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)