From 8b8a2caad62dacae47279280f6dda7da68b88aae Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 10 Sep 2024 09:26:49 +0100 Subject: [PATCH] Add test to the pipeline --- .github/workflows/review_pipeline.yml | 52 +++++++++++++++++++++++++++ Dockerfile | 6 ++++ db/seeds.rb | 12 +++++++ lib/tasks/performance_test.sh | 41 +++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 lib/tasks/performance_test.sh diff --git a/.github/workflows/review_pipeline.yml b/.github/workflows/review_pipeline.yml index a1c5e1e2e..68b9d479c 100644 --- a/.github/workflows/review_pipeline.yml +++ b/.github/workflows/review_pipeline.yml @@ -15,6 +15,11 @@ defaults: run: shell: bash +env: + app_repo_role: arn:aws:iam::815624722760:role/core-application-repo + aws_region: eu-west-2 + repository: core + jobs: infra: name: Deploy review app infrastructure @@ -38,6 +43,53 @@ jobs: permissions: id-token: write + performance: + needs: [code] + runs-on: ubuntu-latest + permissions: + id-token: write + + steps: + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v3 + with: + aws-region: ${{ env.aws_region }} + role-to-assume: ${{ env.app_repo_role }} + + - name: Configure AWS credentials for the environment + uses: aws-actions/configure-aws-credentials@v3 + with: + aws-region: eu-west-2 + role-to-assume: arn:aws:iam::837698168072:role/core-dev-deployment + role-chaining: true + + - name: Run Performance Test + env: + email: performance_testing_user@example.com + password: password + ad_hoc_task_definition: core-review-${{ github.event.pull_request.number }}-ad-hoc + cluster: core-review-${{ github.event.pull_request.number }}-app + service: core-review-${{ github.event.pull_request.number }}-app + run: | + echo $cluster + network=$(aws ecs describe-services --cluster $cluster --services $service --query services[0].networkConfiguration) + overrides='{ + "containerOverrides": [{ + "name": "app", + "command": ["sh", "./lib/tasks/performance_test.sh"] + }] + }' + arn=$(aws ecs run-task --cluster $cluster --task-definition $ad_hoc_task_definition --network-configuration "$network" --overrides "$overrides" --group migrations --launch-type FARGATE --query tasks[0].taskArn) + + echo "Waiting for performance tests to run" + task_id=${arn##*/} + task_id=${task_id%*\"} + + aws ecs wait tasks-stopped --cluster $cluster --tasks $task_id + + succeeded=$(aws ecs describe-tasks --cluster $cluster --tasks $task_id --query "tasks[0].containers[0].exitCode == '0'") + if [ "$succeeded" == "true" ]; then exit 0; else exit 1; fi + comment: name: Add link to PR needs: [code] diff --git a/Dockerfile b/Dockerfile index b26aad246..ea987666b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,6 +31,9 @@ EXPOSE ${PORT} RUN adduser --system --no-create-home nonroot +RUN apk add curl +RUN apk add apache2-utils + FROM base as test RUN bundle config set without "" @@ -67,6 +70,9 @@ RUN mkdir -p tmp log RUN chown -R nonroot tmp log RUN chown nonroot db/schema.rb +RUN mkdir -p performance_test +RUN chown -R nonroot performance_test + USER nonroot CMD bundle exec rails s -e ${RAILS_ENV} -p ${PORT} --binding=0.0.0.0 diff --git a/db/seeds.rb b/db/seeds.rb index b58f7e0a8..e4e791f27 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -235,6 +235,18 @@ unless Rails.env.test? create_data_protection_confirmation(user) end + User.find_or_create_by!( + name: "Coordinator", + email: "performance_testing_user@example.com", + organisation: org, + role: "data_coordinator", + is_dpo: true, + ) do |user| + user.password = "password" + user.confirmed_at = Time.zone.now + user.is_dpo = true + end + support_user = User.find_or_create_by!( name: "Support", email: "support@example.com", diff --git a/lib/tasks/performance_test.sh b/lib/tasks/performance_test.sh new file mode 100644 index 000000000..7e8b38e1f --- /dev/null +++ b/lib/tasks/performance_test.sh @@ -0,0 +1,41 @@ +cd performance_test +echo "Get token" +TOKEN=$(curl -c token_cookies.txt -s https://review.submit-social-housing-data.levellingup.gov.uk/2623/account/sign-in | grep '\ performance_test_results.txt +file="performance_test_results.txt" + +failed_requests=$(grep "Failed requests:" "$file" | awk '{print $3}') +non_2xx_responses=$(grep "Non-2xx responses:" "$file" | awk '{print $3}') +time_per_request_all=$(grep "Time per request:" "$file" | awk 'NR==2{print $4}') +requests_per_second=$(grep "Requests per second:" "$file" | awk '{print $4}') + + +if [ "$failed_requests" -gt 0 ]; then + echo "Performance test failed - $failed_requests failed requests" + exit 1 +fi + +if [ "$non_2xx_responses" -ne 0 ] && [ -n "$non_2xx_responses" ]; then + echo "Test failed: There were $non_2xx_responses non-2xx responses." + exit 1 +fi + +if (( $(echo "$time_per_request_all > 25" | bc -l) )); then + echo "Performance test failed - Time per request across all concurrent requests is more than 25 ms: $time_per_request_all ms" + exit 1 +fi + +if (( $(echo "$requests_per_second < 20" | bc -l) )); then + echo "Performance test failed - Requests per second is less than 20: $requests_per_second" + exit 1 +fi + +echo "Test passed: No failed requests and no non-2xx responses." +exit 0