From 27602bea721a678ad8529c90becbc77e6ee8a9cb Mon Sep 17 00:00:00 2001 From: Julien Francoz Date: Thu, 7 Dec 2023 13:45:42 +0100 Subject: [PATCH 1/2] add check job after deployment --- .github/workflows/cd.yml | 7 +++++++ .github/workflows/check.yml | 28 ++++++++++++++++++++++++++++ .github/workflows/deploy.yml | 7 +++++++ 3 files changed, 42 insertions(+) create mode 100644 .github/workflows/check.yml diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 9c3c9ef0c..44f63d80b 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -93,3 +93,10 @@ jobs: cloudflare-api-token: ${{ secrets.CF_API_TOKEN }} cloudflare-zone-id: ${{ secrets.CF_ZONE_ID }} keycloak-admin-password: ${{ secrets.KEYCLOAK_ADMIN_PASSWORD }} + + check: + name: Check + needs: [ deploy ] + uses: ./.github/workflows/check.yml + with: + url: ${{needs.deploy.outputs.url}} diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 000000000..22844c0b3 --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,28 @@ +name: Deploy + +on: + workflow_call: + inputs: + url: + type: string + description: URL + +jobs: + check: + name: Check + runs-on: ubuntu-latest + permissions: + contents: 'read' + id-token: 'write' + steps: + - + name: Checkout + uses: actions/checkout@v4 + + - name: Run local k6 test + uses: grafana/k6-action@v0.2.0 + with: + filename: k6/script.js + flags: --out json=results.json + env: + TARGET: ${{ inputs.url }} diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 0143b5b30..92e6909ee 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -32,6 +32,9 @@ on: keycloak-admin-password: description: Keycloak Admin Password required: true + outputs: + url: + value: ${{ jobs.deploy.outputs.url }} jobs: deploy: @@ -43,6 +46,8 @@ jobs: permissions: contents: 'read' id-token: 'write' + outputs: + url: ${{ steps.deploy.outputs.url }} steps: - name: Checkout @@ -81,6 +86,7 @@ jobs: # Release name MUST start with a letter - name: Deploy + id: deploy run: | if [ "${{ github.event_name }}" == "push" ]; then release=prod @@ -91,6 +97,7 @@ jobs: namespace=$release url=$release-demo.api-platform.com fi + echo "url=$url" >> "$GITHUB_OUTPUT" cors="https://$url|http://localhost|https://localhost|http://localhost:3000" set -o pipefail helm upgrade $release ./helm/api-platform -f ./helm/api-platform/values.yaml \ From d6c6ea4b0afbec37b1817e93bff7c97f2d937df1 Mon Sep 17 00:00:00 2001 From: Julien Francoz Date: Thu, 7 Dec 2023 15:00:32 +0100 Subject: [PATCH 2/2] add k6 test script --- k6/script.js | 43 +++++++++++++++++++++++++++++++++++++++++++ k6/test.sh | 18 ++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 k6/script.js create mode 100755 k6/test.sh diff --git a/k6/script.js b/k6/script.js new file mode 100644 index 000000000..f1b563d12 --- /dev/null +++ b/k6/script.js @@ -0,0 +1,43 @@ +import { check, sleep } from "k6"; +import http from "k6/http"; + +export let options = { + maxRedirects: 0, + scenarios: { + default: { + executor: 'per-vu-iterations', + vus: 1, + iterations: 1, + } + }, + thresholds: { + http_req_duration: [{threshold: 'p(95)<5000', abortOnFail: true}], //units in miliseconds 60000ms = 1m + http_req_failed: [{threshold: 'rate<0.01', abortOnFail: true}], // http errors should be less than 1% + checks: [{threshold: 'rate>0.95', abortOnFail: true}], // checks must success more than 99% + }, +}; + +const target=__ENV.TARGET; +console.log(`Running test on ${target}`); + +export default function() { + check_https_redirect(); + check_api_link_header(); + sleep(1); +} + +function check_https_redirect() { + var r = http.get(`http://${target}/`); + check(r, { + "http request: status is 301": (r) => r.status === 301, + "http request: redirection location ok": (r) => r.headers["Location"] === `https://${target}/`, + }); +} + +function check_api_link_header() { + var r = http.get(`https://${target}/books?page=1`); + check(r, { + "books API: status is 200": (r) => r.status === 200, + "books API: Link in https": (r) => r.headers["Link"].match(`https://${target}/docs.jsonld`), + }); +} diff --git a/k6/test.sh b/k6/test.sh new file mode 100755 index 000000000..d45ad2ad9 --- /dev/null +++ b/k6/test.sh @@ -0,0 +1,18 @@ +#!/bin/bash +cd $(dirname $0) +if [ -z "$TARGET" ]; then + echo "Missing TARGET=pr-xxx-demo.api-platform.com" 1>&2 + exit 1 +fi +docker run \ + --name k6 \ + --rm -i \ + -v $(pwd):/test \ + -w /test \ + -p 5665:5665 \ + -e TARGET=$TARGET \ + ghcr.io/szkiba/xk6-dashboard:latest \ + run \ + --http-debug \ + --out=dashboard \ + ./script.js