From 72ff70c07f7f351af65d79dc22ab010f2c526149 Mon Sep 17 00:00:00 2001 From: Martin Catty Date: Sat, 23 May 2026 17:40:04 -0400 Subject: [PATCH] test: helm-unittest coverage for all chart templates Add unittest suites for deployment, configmap, and service; fixtures under tests/data; CI job with helm-unittest --verify=false alongside helm lint. --- .github/workflows/ci.yml | 16 +++++- README.md | 9 ++++ tests/README.md | 31 ++++++++++++ tests/configmap_test.yaml | 37 ++++++++++++++ tests/data/test_values.yaml | 37 ++++++++++++++ tests/data/test_values_files.yaml | 28 +++++++++++ tests/deployment_test.yaml | 81 +++++++++++++++++++++++++++++++ tests/service_test.yaml | 27 +++++++++++ 8 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 tests/README.md create mode 100644 tests/configmap_test.yaml create mode 100644 tests/data/test_values.yaml create mode 100644 tests/data/test_values_files.yaml create mode 100644 tests/deployment_test.yaml create mode 100644 tests/service_test.yaml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3d4f5c4..18fb03b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,3 @@ -# Runs when this chart tree is the root of the GitHub repository (e.g. fluid-pub/chart-workload). - name: Helm chart CI on: @@ -23,3 +21,17 @@ jobs: - name: Helm lint run: helm lint . + + unittest: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + + - name: Install Helm + uses: azure/setup-helm@v5 + + - name: Install helm-unittest plugin + run: helm plugin install https://github.com/helm-unittest/helm-unittest --version v0.6.3 --verify=false + + - name: Helm unittest + run: helm unittest . diff --git a/README.md b/README.md index 51f4b14..f32c86c 100644 --- a/README.md +++ b/README.md @@ -52,3 +52,12 @@ extraEnvFrom: - `image.repository` / `image.tag` — workload image (GHCR). - `credentialsSecret` — optional file-based credentials mount. - `service.enabled` — only if the workload exposes HTTP. + +## Chart tests + +```bash +helm plugin install https://github.com/helm-unittest/helm-unittest --version v0.6.3 --verify=false +helm unittest . +``` + +See [`tests/README.md`](tests/README.md). CI runs `helm lint` and `helm unittest .` on pull requests. diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..2ead497 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,31 @@ +# Helm chart unit tests (`fluid-workload`) + +Unit tests for this chart live under [`tests/`](tests/) and run with [helm-unittest](https://github.com/helm-unittest/helm-unittest). + +## Install plugin + +```bash +helm plugin install https://github.com/helm-unittest/helm-unittest --version v0.6.3 --verify=false +``` + +## Run tests + +From the chart root: + +```bash +helm unittest . +``` + +## Coverage + +| Suite | Templates | +|-------|-----------| +| `deployment_test.yaml` | `deployment.yaml` | +| `configmap_test.yaml` | `configmap.yaml` | +| `service_test.yaml` | `service.yaml` | + +Fixtures: [`tests/data/test_values.yaml`](data/test_values.yaml), [`tests/data/test_values_files.yaml`](data/test_values_files.yaml). + +CI runs `helm lint` and `helm unittest .` on every pull request (see [`.github/workflows/ci.yml`](../.github/workflows/ci.yml)). + +When you add or change a template or a values branch, add or update tests in the same change. diff --git a/tests/configmap_test.yaml b/tests/configmap_test.yaml new file mode 100644 index 0000000..81b8e55 --- /dev/null +++ b/tests/configmap_test.yaml @@ -0,0 +1,37 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json +suite: configmap +templates: + - configmap.yaml +tests: + - it: should render ConfigMap from legacy config.content + values: + - ./data/test_values.yaml + asserts: + - isKind: + of: ConfigMap + - matchRegex: + path: metadata.name + pattern: -config$ + - matchRegex: + path: data["config.yaml"] + pattern: "name: example-probe" + + - it: should include schema.yml when schemaContent is set + values: + - ./data/test_values.yaml + set: + config.schemaContent: 'version: "1"' + asserts: + - matchRegex: + path: data["schema.yml"] + pattern: 'version: "1"' + + - it: should prefer config.files over legacy content + values: + - ./data/test_values_files.yaml + asserts: + - matchRegex: + path: data["agent.yml"] + pattern: "agent: custom" + - notExists: + path: data.config.yaml diff --git a/tests/data/test_values.yaml b/tests/data/test_values.yaml new file mode 100644 index 0000000..59a1c83 --- /dev/null +++ b/tests/data/test_values.yaml @@ -0,0 +1,37 @@ +replicaCount: 1 + +workload: + kind: probe + +image: + repository: ghcr.io/fluid-pub/probe-example + tag: "0.1.0" + pullPolicy: IfNotPresent + +config: + files: {} + content: | + name: example-probe + schemaInImage: false + schemaContent: "" + +configMount: + mountPath: /etc/fluid/config + fileName: config.yaml + +credentialsSecret: + enabled: false + secretName: "" + mountPath: /etc/fluid/credentials + items: [] + +service: + enabled: false + type: ClusterIP + port: 8080 + +strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + maxSurge: 0 diff --git a/tests/data/test_values_files.yaml b/tests/data/test_values_files.yaml new file mode 100644 index 0000000..ab5cb28 --- /dev/null +++ b/tests/data/test_values_files.yaml @@ -0,0 +1,28 @@ +replicaCount: 1 + +workload: + kind: agent + +image: + repository: ghcr.io/fluid-pub/fluid-agent-example + tag: "0.1.0" + pullPolicy: IfNotPresent + +config: + files: + agent.yml: | + agent: custom + content: | + ignored: true + schemaInImage: false + schemaContent: "" + +configMount: + mountPath: /etc/fluid/config + fileName: config.yaml + +credentialsSecret: + enabled: false + +service: + enabled: false diff --git a/tests/deployment_test.yaml b/tests/deployment_test.yaml new file mode 100644 index 0000000..25a362b --- /dev/null +++ b/tests/deployment_test.yaml @@ -0,0 +1,81 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json +suite: deployment +templates: + - deployment.yaml +tests: + - it: should render Deployment with workload labels + values: + - ./data/test_values.yaml + asserts: + - isKind: + of: Deployment + - equal: + path: metadata.labels["fluid.io/workload-kind"] + value: probe + - equal: + path: spec.replicas + value: 1 + - equal: + path: spec.template.spec.containers[0].image + value: ghcr.io/fluid-pub/probe-example:0.1.0 + + - it: should mount config directory when schemaInImage is false + values: + - ./data/test_values.yaml + asserts: + - equal: + path: spec.template.spec.containers[0].volumeMounts[0].mountPath + value: /etc/fluid/config + - notExists: + path: spec.template.spec.containers[0].volumeMounts[0].subPath + + - it: should mount main config with subPath when schemaInImage is true and single file + values: + - ./data/test_values.yaml + set: + config.schemaInImage: true + asserts: + - equal: + path: spec.template.spec.containers[0].volumeMounts[0].mountPath + value: /etc/fluid/config/config.yaml + - equal: + path: spec.template.spec.containers[0].volumeMounts[0].subPath + value: config.yaml + + - it: should not expose container ports when service is disabled + values: + - ./data/test_values.yaml + asserts: + - notExists: + path: spec.template.spec.containers[0].ports + + - it: should expose container port when service is enabled + values: + - ./data/test_values.yaml + set: + service.enabled: true + service.port: 9090 + asserts: + - equal: + path: spec.template.spec.containers[0].ports[0].containerPort + value: 9090 + + - it: should mount credentials secret when enabled + values: + - ./data/test_values.yaml + set: + credentialsSecret.enabled: true + credentialsSecret.secretName: probe-credentials + asserts: + - contains: + path: spec.template.spec.volumes + content: + name: credentials + secret: + secretName: probe-credentials + - contains: + path: spec.template.spec.containers[0].volumeMounts + content: + name: credentials + mountPath: /etc/fluid/credentials + readOnly: true diff --git a/tests/service_test.yaml b/tests/service_test.yaml new file mode 100644 index 0000000..b36cdf8 --- /dev/null +++ b/tests/service_test.yaml @@ -0,0 +1,27 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json +suite: service +templates: + - service.yaml +tests: + - it: should not render Service when disabled + values: + - ./data/test_values.yaml + asserts: + - hasDocuments: + count: 0 + + - it: should render Service when enabled + values: + - ./data/test_values.yaml + set: + service.enabled: true + service.port: 8080 + asserts: + - isKind: + of: Service + - equal: + path: spec.ports[0].port + value: 8080 + - equal: + path: spec.ports[0].targetPort + value: http