Skip to content

Commit bd411e6

Browse files
committed
test: add helm-unittest suite for chart templates
- 29 tests across 6 suites covering all custom templates - credentials, examples, gateway, init-dashboards, opensearch-exporter - Tests conditional rendering, custom values, labels, annotations - Wrapper script runs helm lint + helm unittest - Requires helm-unittest plugin
1 parent a8b819a commit bd411e6

7 files changed

Lines changed: 361 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
suite: opensearch credentials secret
2+
templates:
3+
- templates/opensearch-credentials-secret.yaml
4+
tests:
5+
- it: should render with default credentials
6+
asserts:
7+
- isKind:
8+
of: Secret
9+
- equal:
10+
path: stringData.username
11+
value: admin
12+
- equal:
13+
path: stringData.password
14+
value: "My_password_123!@#"
15+
16+
- it: should use custom credentials
17+
set:
18+
opensearchUsername: myuser
19+
opensearchPassword: "S3cret!"
20+
asserts:
21+
- equal:
22+
path: stringData.username
23+
value: myuser
24+
- equal:
25+
path: stringData.password
26+
value: "S3cret!"
27+
28+
- it: should include standard labels
29+
asserts:
30+
- exists:
31+
path: metadata.labels["helm.sh/chart"]
32+
- exists:
33+
path: metadata.labels["app.kubernetes.io/managed-by"]
34+
35+
- it: should respect fullnameOverride
36+
set:
37+
fullnameOverride: custom
38+
asserts:
39+
- equal:
40+
path: metadata.name
41+
value: custom-opensearch-credentials
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
suite: example agents
2+
templates:
3+
- templates/examples.yaml
4+
tests:
5+
- it: should render all example deployments and services by default
6+
asserts:
7+
- hasDocuments:
8+
count: 9 # 4 services + 5 deployments
9+
- containsDocument:
10+
kind: Deployment
11+
apiVersion: apps/v1
12+
name: example-weather-agent
13+
any: true
14+
- containsDocument:
15+
kind: Deployment
16+
apiVersion: apps/v1
17+
name: example-events-agent
18+
any: true
19+
- containsDocument:
20+
kind: Deployment
21+
apiVersion: apps/v1
22+
name: example-travel-planner
23+
any: true
24+
- containsDocument:
25+
kind: Deployment
26+
apiVersion: apps/v1
27+
name: example-mcp-server
28+
any: true
29+
- containsDocument:
30+
kind: Deployment
31+
apiVersion: apps/v1
32+
name: example-canary
33+
any: true
34+
- containsDocument:
35+
kind: Service
36+
apiVersion: v1
37+
name: weather-agent
38+
any: true
39+
40+
- it: should not render when disabled
41+
set:
42+
examples.enabled: false
43+
asserts:
44+
- hasDocuments:
45+
count: 0
46+
47+
- it: should set OTEL_EXPORTER_OTLP_ENDPOINT on weather-agent
48+
documentSelector:
49+
path: metadata.name
50+
value: example-weather-agent
51+
asserts:
52+
- contains:
53+
path: spec.template.spec.containers[0].env
54+
content:
55+
name: OTEL_EXPORTER_OTLP_ENDPOINT
56+
value: "http://RELEASE-NAME-opentelemetry-collector:4317"
57+
58+
- it: should set canary interval from values
59+
set:
60+
examples.canary.interval: "60"
61+
documentSelector:
62+
path: metadata.name
63+
value: example-canary
64+
asserts:
65+
- contains:
66+
path: spec.template.spec.containers[0].env
67+
content:
68+
name: CANARY_INTERVAL
69+
value: "60"
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
suite: gateway
2+
templates:
3+
- templates/gateway.yaml
4+
tests:
5+
- it: should not render when disabled
6+
asserts:
7+
- hasDocuments:
8+
count: 0
9+
10+
- it: should render Gateway and HTTPRoute when enabled
11+
set:
12+
gateway:
13+
enabled: true
14+
provider: envoy
15+
className: eg
16+
host: dashboards.example.com
17+
tls:
18+
secretName: my-tls
19+
asserts:
20+
- hasDocuments:
21+
count: 2
22+
- containsDocument:
23+
kind: Gateway
24+
apiVersion: gateway.networking.k8s.io/v1
25+
any: true
26+
- containsDocument:
27+
kind: HTTPRoute
28+
apiVersion: gateway.networking.k8s.io/v1
29+
any: true
30+
31+
- it: should set gatewayClassName
32+
set:
33+
gateway:
34+
enabled: true
35+
provider: envoy
36+
className: eg
37+
documentIndex: 0
38+
asserts:
39+
- equal:
40+
path: spec.gatewayClassName
41+
value: eg
42+
43+
- it: should set hostname on Gateway and HTTPRoute
44+
set:
45+
gateway:
46+
enabled: true
47+
provider: envoy
48+
className: eg
49+
host: dashboards.example.com
50+
asserts:
51+
- equal:
52+
path: spec.listeners[0].hostname
53+
value: dashboards.example.com
54+
documentIndex: 0
55+
- contains:
56+
path: spec.hostnames
57+
content: dashboards.example.com
58+
documentIndex: 1
59+
60+
- it: should include certificateRefs for envoy with TLS
61+
set:
62+
gateway:
63+
enabled: true
64+
provider: envoy
65+
className: eg
66+
tls:
67+
secretName: my-cert
68+
documentIndex: 0
69+
asserts:
70+
- equal:
71+
path: spec.listeners[0].tls.certificateRefs[0].name
72+
value: my-cert
73+
74+
- it: should not include certificateRefs for aws provider
75+
set:
76+
gateway:
77+
enabled: true
78+
provider: aws
79+
className: amazon-vpc-lattice
80+
documentIndex: 0
81+
asserts:
82+
- notExists:
83+
path: spec.listeners[0].tls.certificateRefs
84+
85+
- it: should set TLS terminate mode
86+
set:
87+
gateway:
88+
enabled: true
89+
provider: envoy
90+
className: eg
91+
documentIndex: 0
92+
asserts:
93+
- equal:
94+
path: spec.listeners[0].tls.mode
95+
value: Terminate
96+
97+
- it: should route to opensearch-dashboards on port 5601
98+
set:
99+
gateway:
100+
enabled: true
101+
provider: envoy
102+
className: eg
103+
documentIndex: 1
104+
asserts:
105+
- equal:
106+
path: spec.rules[0].backendRefs[0].port
107+
value: 5601
108+
109+
- it: should apply custom annotations
110+
set:
111+
gateway:
112+
enabled: true
113+
provider: aws
114+
className: amazon-vpc-lattice
115+
annotations:
116+
application-networking.k8s.aws/certificate-arn: "arn:aws:acm:us-east-1:123:certificate/abc"
117+
documentIndex: 0
118+
asserts:
119+
- equal:
120+
path: metadata.annotations["application-networking.k8s.aws/certificate-arn"]
121+
value: "arn:aws:acm:us-east-1:123:certificate/abc"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
suite: init dashboards configmap
2+
templates:
3+
- templates/init-dashboards-configmap.yaml
4+
tests:
5+
- it: should render when dashboards enabled
6+
asserts:
7+
- isKind:
8+
of: ConfigMap
9+
- isAPIVersion:
10+
of: v1
11+
12+
- it: should not render when dashboards disabled
13+
set:
14+
opensearch-dashboards:
15+
enabled: false
16+
asserts:
17+
- hasDocuments:
18+
count: 0
19+
20+
- it: should use post-install hook
21+
asserts:
22+
- equal:
23+
path: metadata.annotations["helm.sh/hook"]
24+
value: post-install,post-upgrade
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
suite: init dashboards job
2+
templates:
3+
- templates/init-dashboards-job.yaml
4+
tests:
5+
- it: should render when dashboards enabled
6+
asserts:
7+
- isKind:
8+
of: Job
9+
- isAPIVersion:
10+
of: batch/v1
11+
12+
- it: should not render when dashboards disabled
13+
set:
14+
opensearch-dashboards:
15+
enabled: false
16+
asserts:
17+
- hasDocuments:
18+
count: 0
19+
20+
- it: should use post-install hook
21+
asserts:
22+
- equal:
23+
path: metadata.annotations["helm.sh/hook"]
24+
value: post-install,post-upgrade
25+
26+
- it: should read credentials from secret
27+
asserts:
28+
- contains:
29+
path: spec.template.spec.containers[0].env
30+
content:
31+
name: OPENSEARCH_USER
32+
valueFrom:
33+
secretKeyRef:
34+
name: RELEASE-NAME-observability-stack-opensearch-credentials
35+
key: username
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
suite: opensearch exporter
2+
templates:
3+
- templates/opensearch-exporter.yaml
4+
tests:
5+
- it: should render deployment and service by default
6+
asserts:
7+
- hasDocuments:
8+
count: 2
9+
- containsDocument:
10+
kind: Deployment
11+
apiVersion: apps/v1
12+
any: true
13+
- containsDocument:
14+
kind: Service
15+
apiVersion: v1
16+
any: true
17+
18+
- it: should not render when disabled
19+
set:
20+
opensearchExporter.enabled: false
21+
asserts:
22+
- hasDocuments:
23+
count: 0
24+
25+
- it: should use configured image
26+
documentIndex: 0
27+
asserts:
28+
- equal:
29+
path: spec.template.spec.containers[0].image
30+
value: "prometheuscommunity/elasticsearch-exporter:v1.10.0"
31+
32+
- it: should use custom credentials in env
33+
set:
34+
opensearchUsername: myuser
35+
opensearchPassword: "S3cret!"
36+
documentIndex: 0
37+
asserts:
38+
- contains:
39+
path: spec.template.spec.containers[0].env
40+
content:
41+
name: ES_USERNAME
42+
value: myuser
43+
- contains:
44+
path: spec.template.spec.containers[0].env
45+
content:
46+
name: ES_PASSWORD
47+
value: "S3cret!"
48+
49+
- it: should expose metrics port 9114
50+
documentIndex: 1
51+
asserts:
52+
- contains:
53+
path: spec.ports
54+
content:
55+
port: 9114
56+
targetPort: 9114
57+
name: metrics

test/helm-test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
# Helm lint + unit tests for the observability-stack chart.
3+
# Requires: helm, helm-unittest plugin (helm plugin install https://github.com/helm-unittest/helm-unittest.git)
4+
# Usage: ./test/helm-test.sh
5+
set -euo pipefail
6+
7+
CHART="charts/observability-stack"
8+
9+
echo "==> helm lint"
10+
helm lint "$CHART"
11+
12+
echo ""
13+
echo "==> helm unittest"
14+
helm unittest "$CHART"

0 commit comments

Comments
 (0)