Browse Source

Add test to the pipeline

CLDC-3611-performance
Kat 5 months ago
parent
commit
8b8a2caad6
  1. 52
      .github/workflows/review_pipeline.yml
  2. 6
      Dockerfile
  3. 12
      db/seeds.rb
  4. 41
      lib/tasks/performance_test.sh

52
.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]

6
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

12
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",

41
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 '\<meta name=\"csrf-token\"' | sed -n 's/.*content=\"\\([^\\\"]*\\)\".*/\\1/p')
echo "Logging in..."
curl -L -o nul -c login_cookies.txt -b token_cookies.txt -X POST https://review.submit-social-housing-data.levellingup.gov.uk/2623/account/sign-in -d "user[email]=coordinator@example.com" -d "user[password]=$REVIEW_APP_USER_PASSWORD" -d "authenticity_token=$TOKEN"
COOKIES=$(awk '/_data_collector_session/ { print $6, $7 }' login_cookies.txt | tr ' ' '=')
echo "Running performance test..."
ab -n 50 -c 50 -C "$COOKIES" 'https://review.submit-social-housing-data.levellingup.gov.uk/2623/lettings-logs' > 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
Loading…
Cancel
Save