diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index aa95cfd26e4dae24e490a837c47848538190fa0b..c33dd3d458f18aef6c2b5d89b60752ef182e7e17 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -34,12 +34,13 @@ variables: resource_group: $ENVIRONMENT_ID # never execute terraform in parallel on the same environment interruptible: false -.e2e: - image: 'europe-north1-docker.pkg.dev/holi-shared/docker-hub-remote/archlinux:latest' +.smoketest: + image: 'europe-north1-docker.pkg.dev/holi-shared/docker/holi-docker/holi-k6-builder' script: - - API_DOMAIN=`cat $API_DOMAIN_PATH` - - echo "e2e tests against $CI_ENVIRONMENT_SLUG environment go here and against $API_DOMAIN" + - API_DOMAIN=$(cat $API_DOMAIN_PATH) - terraform/environments/scripts/wait-for-ssl.sh "https://${API_DOMAIN}" + - BASE_URL="https://${API_DOMAIN}/graphql" k6 run smoketest/main.js + # TODO should/could we roll back the service to the last working revision on test failure? # end job templates @@ -48,13 +49,13 @@ variables: ## common steps cache_lint_test: - image: 'europe-north1-docker.pkg.dev/holi-shared/docker-hub-remote/denoland/deno:alpine-1.45.2' + image: 'europe-north1-docker.pkg.dev/holi-shared/docker-hub-remote/denoland/deno:2.0.0' stage: 'test' script: - - deno cache --lock=deno.lock app/deps.ts app/dev_deps.ts + - deno cache --allow-import --lock=deno.lock app/deps.ts app/dev_deps.ts - deno fmt --check - deno lint - - deno test + - deno test --allow-import # You can override the included template(s) by including variable overrides # SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings @@ -98,8 +99,8 @@ review_deploy: - production - /^noenv\/.*/ -review_e2e: - extends: .e2e +review_smoketest: + extends: .smoketest needs: ['review_deploy'] except: - main @@ -134,7 +135,7 @@ review_destroy: - terraform/environments/scripts/destroy-env.sh $CI_ENVIRONMENT_SLUG # can't use rules here: https://gitlab.com/gitlab-org/gitlab/-/issues/34077 when: manual - allow_failure: false + allow_failure: true except: - main - production @@ -156,19 +157,18 @@ staging_deploy: only: - main -staging_trigger_unified-api_redeployment: +staging_smoketest: + extends: .smoketest needs: ['staging_deploy'] - trigger: - project: 'app/holi-unified-api' - branch: 'main' only: - main + resource_group: unified-api-staging -staging_e2e: - extends: .e2e - # if staging_e2e would actually run tests (other than ensuring SSL works), we'd have to - # wait for the unified-api pipeline to finish. - needs: ['staging_deploy'] +staging_trigger_unified-api_redeployment: + needs: ['staging_smoketest'] + trigger: + project: 'app/holi-unified-api' + branch: 'main' only: - main @@ -187,10 +187,17 @@ production_deploy: only: - production -production_trigger_unified-api_redeployment: +production_smoketest: + extends: .smoketest needs: ['production_deploy'] + only: + - production + +production_trigger_unified-api_redeployment: + needs: ['production_smoketest'] trigger: project: 'app/holi-unified-api' branch: 'production' only: - production + resource_group: unified-api-production diff --git a/Dockerfile b/Dockerfile index cb3842a5b57e55e67e1589ab1b3257c617114ab2..0adb2ffd6f704d99d01d8589a52b54bd086b2ce5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM europe-north1-docker.pkg.dev/holi-shared/docker-hub-remote/denoland/deno:alpine-1.45.2 +FROM europe-north1-docker.pkg.dev/holi-shared/docker-hub-remote/denoland/deno:2.0.0 # The port that your application listens to. EXPOSE 8002 @@ -10,7 +10,7 @@ COPY deno.lock . COPY deno.json . COPY ./app ./app -RUN deno cache --lock=deno.lock app/deps.ts app/dev_deps.ts -RUN deno cache --lock=deno.lock app/main.ts +RUN deno cache --allow-import --lock=deno.lock app/deps.ts app/dev_deps.ts +RUN deno cache --allow-import --lock=deno.lock app/main.ts -CMD ["deno", "run", "--allow-net", "--allow-env", "--lock=deno.lock", "--cached-only", "app/main.ts"] +CMD ["deno", "run", "--allow-import", "--allow-net", "--allow-env", "--lock=deno.lock", "--cached-only", "app/main.ts"] diff --git a/app/goodnews.ts b/app/goodnews.ts index edd89fb84f66fdcfcae9057c0acb8ccdaedbbd24..bb364a1dec23328b91092b7fc6261e6d3a380967 100644 --- a/app/goodnews.ts +++ b/app/goodnews.ts @@ -103,7 +103,8 @@ const fetchPage = const fetchResult = await fetch(url) const resultJson = await fetchResult.json() return resultJson - } catch (err) { + // deno-lint-ignore no-explicit-any + } catch (err: any) { logger.error('fetching articles failed: ' + err.message) throw err } finally { diff --git a/deno.json b/deno.json index 585843597393e746f860217ff0758c220cac3b60..0952ad56cc684bc2d2c3528f30514b8bb276d9e1 100644 --- a/deno.json +++ b/deno.json @@ -4,14 +4,14 @@ "lint": "deno lint", "fmt": "deno fmt", "fmt:check": "deno fmt --check", - "test": "deno test", + "test": "deno test --allow-import ", "updateDeps": "deno cache --allow-import --lock=deno.lock --lock-write app/deps.ts app/dev_deps.ts", "install": "deno cache --allow-import --reload --lock=deno.lock app/deps.ts app/dev_deps.ts", "cache": "deno cache --allow-import app/main.ts", "dev": "deno run --allow-import --allow-net --allow-env --lock=deno.lock --cached-only --watch app/main.ts", "start": "deno run --allow-import --allow-net --allow-env --lock=deno.lock --cached-only app/main.ts", "docker": "docker build -t goodnews-api . && docker run -it --init -p 8002:8002 goodnews-api", - "coverage": "deno test --coverage=coverage && deno coverage coverage", + "coverage": "deno test --allow-import --coverage=coverage && deno coverage coverage", "pre-commit": { "cmd": "vr lint && vr fmt:check", "gitHook": "pre-commit" @@ -22,6 +22,8 @@ "lineWidth": 120, "singleQuote": true, "semiColons": false, - "exclude": ["*.md"] + "exclude": [ + "*.md" + ] } } diff --git a/smoketest/main.js b/smoketest/main.js new file mode 100644 index 0000000000000000000000000000000000000000..f84e54bff22d8470fc7c1a78a7c02a8b4cdbb208 --- /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 forQuery(query, checkFunction) { + const response = http.post(`${__ENV.BASE_URL}`, JSON.stringify({ query }), { + headers: { 'Content-Type': 'application/json' }, + }) + checkFunction(response) +} + +// Define your smoketest(s) here. +export default () => { + forQuery(`{articles{articles{id}}}`, (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 articles': (r) => Array.isArray(r.data.articles.articles) && r.data.articles.articles.length > 0, + }) + }) +}