From 23ce85cad77174d1be024e759b741bbfb7ac52cd Mon Sep 17 00:00:00 2001
From: Ole Langbehn <ole.langbehn@inoio.de>
Date: Tue, 11 Mar 2025 12:09:33 +0100
Subject: [PATCH] feat(CI): add smoketests to pipeline

---
 .gitlab-ci.yml    | 23 ++++++++++++++++++-----
 smoketest/main.js | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 5 deletions(-)
 create mode 100644 smoketest/main.js

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5ee19c0..d3af140 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -62,13 +62,14 @@ build_docker:
   resource_group: $ENVIRONMENT_ID # never execute terraform in parallel on the same environment
   interruptible: false
 
-.e2e:
+.smoketest:
   stage: "deploy"
-  image: 'europe-north1-docker.pkg.dev/holi-shared/docker-hub-remote/archlinux:base'
+  image: 'europe-north1-docker.pkg.dev/holi-shared/docker/holi-docker/holi-k6-builder'
   script:
-    - API_BASE_URL=`cat "$API_DOMAIN_PATH"`
-    - echo "e2e tests against '$CI_ENVIRONMENT_SLUG' environment go here and against '$API_BASE_URL'"
-    - terraform/scripts/wait-for-ssl.sh "https://${API_BASE_URL}"
+    - API_DOMAIN=$(cat "$API_DOMAIN_PATH")
+    - terraform/environments/scripts/wait-for-ssl.sh "https://${API_DOMAIN}"
+    - BASE_URL="https://${API_DOMAIN}" k6 run smoketest/main.js
+    # TODO should/could we roll back the service to the last working revision on test failure?
 
 staging_deploy:
   extends: .deploy
@@ -81,6 +82,12 @@ staging_deploy:
   only:
     - main
 
+staging_smoketest:
+  extends: .smoketest
+  needs: ['staging_deploy']
+  only:
+    - main
+
 production_deploy:
   extends: .deploy
   allow_failure: false
@@ -92,3 +99,9 @@ production_deploy:
     ENVIRONMENT_ID: production
   only:
     - production
+
+production_smoketest:
+  extends: .smoketest
+  needs: ['production_deploy']
+  only:
+    - production
diff --git a/smoketest/main.js b/smoketest/main.js
new file mode 100644
index 0000000..54305b9
--- /dev/null
+++ b/smoketest/main.js
@@ -0,0 +1,40 @@
+import http from 'k6/http'
+import { check } from 'k6'
+
+// You don't need to change anything in this section, it's k6 glue code.
+// See the default function at the end of the file for defining your smoketest.
+// This configuration only executes 1 test, enough for a smoketest. The smoketest will fail on any check failing.
+const allChecksNeedToPassTreshold = { checks: [{ threshold: 'rate==1', abortOnFail: true }] }
+export const options = {
+  vus: 1,
+  iterations: 1,
+  thresholds: allChecksNeedToPassTreshold,
+}
+
+/**
+ * Performs a GraphQL query and checks the response using the provided function. Fails if any of the provided expectations are not met.
+ * @param {string} query The GraphQL query to perform
+ * @param {(response: http.Response) => Array<boolean>} checkFunction
+ *   A function that takes the HTTP response as an argument and returns an array
+ *   of boolean values, each indicating success or failure of a test.
+ */
+function forGetRequest(path, checkFunction) {
+  const response = http.get(`${__ENV.BASE_URL}${path}`, {
+    headers: { 'Accept': 'application/json' },
+  })
+  checkFunction(response)
+}
+
+// Define your smoketest(s) here.
+export default () => {
+  forGetRequest("/_matrix/client/r0/login", (response) => {
+    check(response, {
+      'is status 200': (r) => r.status === 200,
+    })
+    check(JSON.parse(response.body), {
+      // there can be multiple tests here, e.g.
+      //"contains topics object": (r) => typeof r.data.topics != null,
+      'contains at least one flow': (r) => r.flows && Array.isArray(r.flows) && r.flows.length > 0,
+    })
+  })
+}
-- 
GitLab