From d1b1a395de31c5fb8a88f8cfcadb39cb763f949c Mon Sep 17 00:00:00 2001 From: Peter Guy Date: Fri, 17 Jan 2025 08:59:26 -0800 Subject: [PATCH] Update unit tests to be current with most recent helm unittest syntax (#619) - Switch to the canonical helm unittest isntead of an outdated fork. - Update the tests to work with latest syntax. ### Checklist - [ ] Follow the [manual testing process](https://github.com/sourcegraph/deploy-sourcegraph-helm/blob/main/TEST.md) - [ ] Update [changelog](https://github.com/sourcegraph/deploy-sourcegraph-helm/blob/main/charts/sourcegraph/CHANGELOG.md) - [ ] Update [Kubernetes update doc](https://docs.sourcegraph.com/admin/updates/kubernetes) ### Test plan 1. uninstall the outdated helm unittest fork: `helm plugin uninstall unittest` 2. install the canonical tool: `helm plugin install https://github.com/helm-unittest/helm-unittest` 3. run the unit tests: `helm unittest ./charts/sourcegraph/.` --- TEST.md | 6 +++--- charts/sourcegraph/tests/checksum_test.yaml | 10 +++++----- .../codeInsightsDBAdditionalConfig_test.yaml | 2 +- .../tests/codeIntelDBAdditionalConfig_test.yaml | 2 +- .../sourcegraph/tests/defaultTagPrefix_test.yaml | 16 ++++++++-------- charts/sourcegraph/tests/localDevMode_test.yaml | 8 ++++---- charts/sourcegraph/tests/nodeExporter_test.yaml | 2 +- .../tests/otelCollectorJaeger_test.yaml | 2 +- .../tests/pgsqlAdditionalConfig_test.yaml | 2 +- charts/sourcegraph/tests/pgsqlAuth_test.yaml | 4 ++-- charts/sourcegraph/tests/storageClass_test.yaml | 4 ++-- charts/sourcegraph/tests/worker_test.yaml | 2 +- scripts/ci/helm-unittest.sh | 4 ++-- 13 files changed, 32 insertions(+), 32 deletions(-) diff --git a/TEST.md b/TEST.md index 4c645608..03de411f 100644 --- a/TEST.md +++ b/TEST.md @@ -12,12 +12,12 @@ helm lint charts/sourcegraph/. ## Unit testing -We utilize [helm-unittest](https://github.com/quintush/helm-unittest/), a BDD styled unit test framework, to validate our helm chart. +We utilize [helm-unittest](https://github.com/helm-unittest/helm-unittest/), a BDD styled unit test framework, to validate our helm chart. helm-unittest can be installed with: ```bash -helm plugin install https://github.com/quintush/helm-unittest +helm plugin install https://github.com/helm-unittest/helm-unittest ``` Once the plugin is installed, you can run the unit tests using the following: @@ -28,7 +28,7 @@ helm unittest --helm3 ./charts/sourcegraph/. We currently do not have testing best practices or require unit tests for new changes, so add test cases at your best judgement if possible. -You may check out our [existing test cases](https://github.com/sourcegraph/deploy-sourcegraph-helm/tree/main/charts/sourcegraph/tests) and helm-unittest [docs](https://github.com/quintush/helm-unittest/blob/master/DOCUMENT.md). +You may check out our [existing test cases](https://github.com/sourcegraph/deploy-sourcegraph-helm/tree/main/charts/sourcegraph/tests) and helm-unittest [docs](https://github.com/helm-unittest/helm-unittest/blob/master/DOCUMENT.md). ## Manual testing diff --git a/charts/sourcegraph/tests/checksum_test.yaml b/charts/sourcegraph/tests/checksum_test.yaml index 011207d6..9a91ab9e 100644 --- a/charts/sourcegraph/tests/checksum_test.yaml +++ b/charts/sourcegraph/tests/checksum_test.yaml @@ -10,7 +10,7 @@ tests: existingSecret: "my-secret" asserts: - matchSnapshot: - path: spec.template.metadata.annotations.[checksum/auth] + path: spec.template.metadata.annotations["checksum/auth"] - it: should update checksum when codeintel-db secret is modified set: codeIntelDB: @@ -18,7 +18,7 @@ tests: newkey: "blank" asserts: - matchSnapshot: - path: spec.template.metadata.annotations.[checksum/auth] + path: spec.template.metadata.annotations["checksum/auth"] - it: should update checksum when codeinsights-db secret is modified set: codeInsightsDB: @@ -27,7 +27,7 @@ tests: database: "anotherdb" asserts: - matchSnapshot: - path: spec.template.metadata.annotations.[checksum/auth] + path: spec.template.metadata.annotations["checksum/auth"] - it: should update checksum when redis-store secret is modified set: redisStore: @@ -35,7 +35,7 @@ tests: endpoint: "external-redis:6379" asserts: - matchSnapshot: - path: spec.template.metadata.annotations.[checksum/auth] + path: spec.template.metadata.annotations["checksum/auth"] - it: should update checksum when redis-cache secret is modified set: redisCache: @@ -43,4 +43,4 @@ tests: endpoint: "external-redis:6379" asserts: - matchSnapshot: - path: spec.template.metadata.annotations.[checksum/auth] + path: spec.template.metadata.annotations["checksum/auth"] diff --git a/charts/sourcegraph/tests/codeInsightsDBAdditionalConfig_test.yaml b/charts/sourcegraph/tests/codeInsightsDBAdditionalConfig_test.yaml index 1d9e43d6..0cfe03f9 100644 --- a/charts/sourcegraph/tests/codeInsightsDBAdditionalConfig_test.yaml +++ b/charts/sourcegraph/tests/codeInsightsDBAdditionalConfig_test.yaml @@ -15,7 +15,7 @@ tests: max_connections = 3000000000000000000 asserts: - matchRegex: - path: data.postgresql\.conf + path: data["postgresql.conf"] pattern: max_connections = 3000000000000000000 - it: should fail to render when both codeInsightsDB.additionalConfig and codeInsightsDB.existingConfig are defined set: diff --git a/charts/sourcegraph/tests/codeIntelDBAdditionalConfig_test.yaml b/charts/sourcegraph/tests/codeIntelDBAdditionalConfig_test.yaml index 2a3033f7..00121565 100644 --- a/charts/sourcegraph/tests/codeIntelDBAdditionalConfig_test.yaml +++ b/charts/sourcegraph/tests/codeIntelDBAdditionalConfig_test.yaml @@ -15,7 +15,7 @@ tests: max_connections = 3000000000000000000 asserts: - matchRegex: - path: data.postgresql\.conf + path: data["postgresql.conf"] pattern: max_connections = 2000000000000000000 - it: should fail to render when both codeIntelDB.additionalConfig and codeIntelDB.existingConfig are defined set: diff --git a/charts/sourcegraph/tests/defaultTagPrefix_test.yaml b/charts/sourcegraph/tests/defaultTagPrefix_test.yaml index 722100ae..e0c3c7be 100644 --- a/charts/sourcegraph/tests/defaultTagPrefix_test.yaml +++ b/charts/sourcegraph/tests/defaultTagPrefix_test.yaml @@ -9,10 +9,10 @@ tests: frontend: image: defaultTag: test - matchRegexRaw: - path: "spec.template.spec.containers[0].image" - pattern: | - "[\w-]+\.\w+\.\w+\/.+\/.+:test" + asserts: + - matchRegex: + path: "spec.template.spec.containers[0].image" + pattern: ^[\w-]+\.\w+\.\w+\/.+\/.+:test$ - it: should add defaultTagPrefix template: frontend/sourcegraph-frontend.Deployment.yaml set: @@ -20,7 +20,7 @@ tests: image: defaultTag: test defaultTagPrefix: "prefix-" - matchRegexRaw: - path: "spec.template.spec.containers[0].image" - pattern: | - "[\w-]+\.\w+\.\w+\/.+\/.+:prefix-test" + asserts: + - matchRegex: + path: "spec.template.spec.containers[0].image" + pattern: ^[\w-]+\.\w+\.\w+\/.+\/.+:prefix-test$ diff --git a/charts/sourcegraph/tests/localDevMode_test.yaml b/charts/sourcegraph/tests/localDevMode_test.yaml index d3057913..1a2d13ca 100644 --- a/charts/sourcegraph/tests/localDevMode_test.yaml +++ b/charts/sourcegraph/tests/localDevMode_test.yaml @@ -6,12 +6,12 @@ tests: set: sourcegraph.localDevMode: true asserts: - - isEmpty: - path: spec.template.spec.containers[0].resources + - notExists: + path: "spec.template.spec.containers[0].resources" - it: should have a resource block when localDevMode=false set: sourcegraph.localDevMode: false asserts: - - isNotEmpty: - path: spec.template.spec.containers[0].resources + - exists: + path: "spec.template.spec.containers[0].resources" diff --git a/charts/sourcegraph/tests/nodeExporter_test.yaml b/charts/sourcegraph/tests/nodeExporter_test.yaml index 0ec1f9df..51f5a82d 100644 --- a/charts/sourcegraph/tests/nodeExporter_test.yaml +++ b/charts/sourcegraph/tests/nodeExporter_test.yaml @@ -102,7 +102,7 @@ tests: create: false name: "test-service-account-name" asserts: - - isEmpty: + - notExists: path: spec.template.spec.serviceAccountName template: node-exporter/node-exporter.DaemonSet.yaml diff --git a/charts/sourcegraph/tests/otelCollectorJaeger_test.yaml b/charts/sourcegraph/tests/otelCollectorJaeger_test.yaml index 90dbdb93..a9ae2a3c 100644 --- a/charts/sourcegraph/tests/otelCollectorJaeger_test.yaml +++ b/charts/sourcegraph/tests/otelCollectorJaeger_test.yaml @@ -65,7 +65,7 @@ tests: count: 1 # ConfigMap is rendered template: otel-collector/otel-collector.ConfigMap.yaml - matchRegex: - path: data.[config.yaml] + path: data["config.yaml"] pattern: "endpoint: otlp.service.com:443" # check if element of config is substituted correctly template: otel-collector/otel-collector.ConfigMap.yaml - it: should fail if a configuration is provided and jaeger is enabled diff --git a/charts/sourcegraph/tests/pgsqlAdditionalConfig_test.yaml b/charts/sourcegraph/tests/pgsqlAdditionalConfig_test.yaml index 004d986b..10e515fa 100644 --- a/charts/sourcegraph/tests/pgsqlAdditionalConfig_test.yaml +++ b/charts/sourcegraph/tests/pgsqlAdditionalConfig_test.yaml @@ -16,7 +16,7 @@ tests: max_connections = 3000000000000000000 asserts: - matchRegex: - path: data.postgresql\.conf + path: data["postgresql.conf"] pattern: max_connections = 1000000000000000000 - it: should fail to render when both pgsql.additionalConfig and pgsql.existingConfig are defined set: diff --git a/charts/sourcegraph/tests/pgsqlAuth_test.yaml b/charts/sourcegraph/tests/pgsqlAuth_test.yaml index 8c256671..8c55d528 100644 --- a/charts/sourcegraph/tests/pgsqlAuth_test.yaml +++ b/charts/sourcegraph/tests/pgsqlAuth_test.yaml @@ -18,7 +18,7 @@ tests: path: spec.template.spec.containers[0].env[0].valueFrom.secretKeyRef.name value: "my-secret" - matchSnapshot: - path: spec.template.metadata.annotations.[checksum/pgsql.secret] + path: spec.template.metadata.annotations["checksum/pgsql.secret"] - it: should not generate a secret when existingSecret is passed template: pgsql/pgsql.Secret.yaml set: @@ -62,4 +62,4 @@ tests: path: spec.template.spec.containers[0].env[0].valueFrom.secretKeyRef.name value: "pgsql-auth" - matchSnapshot: - path: spec.template.metadata.annotations.[checksum/pgsql.secret] + path: spec.template.metadata.annotations["checksum/pgsql.secret"] diff --git a/charts/sourcegraph/tests/storageClass_test.yaml b/charts/sourcegraph/tests/storageClass_test.yaml index 8f0ff6e4..04758ecb 100644 --- a/charts/sourcegraph/tests/storageClass_test.yaml +++ b/charts/sourcegraph/tests/storageClass_test.yaml @@ -18,8 +18,8 @@ tests: create: true type: null asserts: - - isNull: - path: parameters + - notExists: + path: parameters.type - it: should have 'allowedTopologies` when storageClass.allowedTopologies is set set: storageClass: diff --git a/charts/sourcegraph/tests/worker_test.yaml b/charts/sourcegraph/tests/worker_test.yaml index b6e4e8c2..72d9db09 100644 --- a/charts/sourcegraph/tests/worker_test.yaml +++ b/charts/sourcegraph/tests/worker_test.yaml @@ -51,7 +51,7 @@ tests: content: name: WORKER_JOB_BLOCKLIST value: job1,job2,job3,job4,rogue-job-1,rogue-job-2 - - isEmpty: + - notExists: path: spec.selector.matchLabels.worker-replica - it: should have the correct worker-0 deployment template: worker/worker.Deployment.yaml diff --git a/scripts/ci/helm-unittest.sh b/scripts/ci/helm-unittest.sh index 01c31e69..c199e77a 100755 --- a/scripts/ci/helm-unittest.sh +++ b/scripts/ci/helm-unittest.sh @@ -3,7 +3,7 @@ set -euf -o pipefail ### Install the helm-unittest plugin -helm plugin install https://github.com/quintush/helm-unittest --version v0.2.11 +helm plugin install https://github.com/helm-unittest/helm-unittest ### Run the helm tests -helm unittest -3q charts/sourcegraph +helm unittest -q charts/sourcegraph