Skip to content

Agent Mesh

charts: add helm-unittest template tests

Tomas Tormo7fa1d8ab29b4
diff --git a/Makefile b/Makefile
index f5c6a237..aa965981 100644
--- a/Makefile
+++ b/Makefile
@@ -197,6 +197,12 @@ helm-lint:
lint: fmt helm-lint
hack/lint.sh
+# fast chart template checks; no cluster needed
+.PHONY: helm-test
+helm-test:
+ @helm plugin list 2>/dev/null | grep -q '^unittest' || helm plugin install https://github.com/helm-unittest/helm-unittest --verify=false
+ helm unittest charts/sam-mesh
+
.PHONY: verify
verify:
./hack/verify-generated.sh
diff --git a/charts/sam-mesh/tests/bootstrap-job_test.yaml b/charts/sam-mesh/tests/bootstrap-job_test.yaml
new file mode 100644
index 00000000..9be914f7
--- /dev/null
+++ b/charts/sam-mesh/tests/bootstrap-job_test.yaml
@@ -0,0 +1,34 @@
+suite: bootstrap job
+templates:
+ - templates/bootstrap-job.yaml
+release:
+ name: sam-mesh
+tests:
+ - it: role only touches the router-token secret
+ documentSelector:
+ path: kind
+ value: Role
+ asserts:
+ - equal:
+ path: rules[0].verbs
+ value: [create]
+ - equal:
+ path: rules[1].resourceNames
+ value: [sam-mesh-router-token]
+ - equal:
+ path: rules[1].verbs
+ value: [get, patch]
+ - lengthEqual:
+ path: rules
+ count: 2
+
+ - it: job pod runs as the numeric curl uid with resources set
+ documentSelector:
+ path: kind
+ value: Job
+ asserts:
+ - equal:
+ path: spec.template.spec.securityContext.runAsUser
+ value: 100
+ - exists:
+ path: spec.template.spec.containers[0].resources.limits
diff --git a/charts/sam-mesh/tests/console-deployment_test.yaml b/charts/sam-mesh/tests/console-deployment_test.yaml
new file mode 100644
index 00000000..4b4c7426
--- /dev/null
+++ b/charts/sam-mesh/tests/console-deployment_test.yaml
@@ -0,0 +1,35 @@
+suite: console deployment
+templates:
+ # secrets.yaml must be loaded for the checksum/secrets include to resolve
+ - templates/console-deployment.yaml
+ - templates/secrets.yaml
+release:
+ name: sam-mesh
+tests:
+ - it: pins the numeric uids for the app and the curl init container
+ template: templates/console-deployment.yaml
+ asserts:
+ - equal:
+ path: spec.template.spec.securityContext.runAsUser
+ value: 65532
+ - equal:
+ path: spec.template.spec.initContainers[0].securityContext.runAsUser
+ value: 100
+
+ - it: rolls the pods when the shared secret changes
+ template: templates/console-deployment.yaml
+ set:
+ controlPlane.adminToken: aaa
+ database.postgres.password: p
+ asserts:
+ - matchRegex:
+ path: spec.template.metadata.annotations["checksum/secrets"]
+ pattern: ^[0-9a-f]{64}$
+
+ - it: console.enabled=false renders nothing
+ template: templates/console-deployment.yaml
+ set:
+ console.enabled: false
+ asserts:
+ - hasDocuments:
+ count: 0
diff --git a/charts/sam-mesh/tests/control-plane-deployment_test.yaml b/charts/sam-mesh/tests/control-plane-deployment_test.yaml
new file mode 100644
index 00000000..7b9f34d4
--- /dev/null
+++ b/charts/sam-mesh/tests/control-plane-deployment_test.yaml
@@ -0,0 +1,59 @@
+suite: control plane deployment
+templates:
+ # secrets.yaml must be loaded for the checksum/secrets include to resolve
+ - templates/control-plane-deployment.yaml
+ - templates/secrets.yaml
+release:
+ name: sam-mesh
+tests:
+ - it: pins the numeric distroless uid so runAsNonRoot is verifiable
+ template: templates/control-plane-deployment.yaml
+ asserts:
+ - equal:
+ path: spec.template.spec.securityContext.runAsNonRoot
+ value: true
+ - equal:
+ path: spec.template.spec.securityContext.runAsUser
+ value: 65532
+
+ - it: rolls the pods when the shared secret changes
+ template: templates/control-plane-deployment.yaml
+ set:
+ controlPlane.adminToken: aaa
+ database.postgres.password: p
+ asserts:
+ - matchRegex:
+ path: spec.template.metadata.annotations["checksum/secrets"]
+ pattern: ^[0-9a-f]{64}$
+
+ - it: uses the default image tag when none is pinned
+ template: templates/control-plane-deployment.yaml
+ asserts:
+ - equal:
+ path: spec.template.spec.containers[0].image
+ value: sam-control-plane:local
+
+ - it: external postgres points the DSN at the configured host
+ template: templates/control-plane-deployment.yaml
+ set:
+ database.postgres.deployInternal: false
+ database.postgres.host: ext-db
+ asserts:
+ - matchRegex:
+ path: spec.template.spec.containers[0].args[2]
+ pattern: "@ext-db:5432"
+
+ - it: scheduling knobs pass through to the pod spec
+ template: templates/control-plane-deployment.yaml
+ set:
+ controlPlane.nodeSelector.disk: ssd
+ controlPlane.tolerations:
+ - key: dedicated
+ asserts:
+ - equal:
+ path: spec.template.spec.nodeSelector.disk
+ value: ssd
+ - contains:
+ path: spec.template.spec.tolerations
+ content:
+ key: dedicated
diff --git a/charts/sam-mesh/tests/db-statefulset_test.yaml b/charts/sam-mesh/tests/db-statefulset_test.yaml
new file mode 100644
index 00000000..7d3090e7
--- /dev/null
+++ b/charts/sam-mesh/tests/db-statefulset_test.yaml
@@ -0,0 +1,37 @@
+suite: db statefulset
+templates:
+ - templates/db-statefulset.yaml
+release:
+ name: sam-mesh
+tests:
+ - it: probes postgres with pg_isready
+ documentSelector:
+ path: kind
+ value: StatefulSet
+ asserts:
+ - equal:
+ path: spec.template.spec.containers[0].readinessProbe.exec.command[0]
+ value: pg_isready
+ - equal:
+ path: spec.template.spec.containers[0].livenessProbe.exec.command[0]
+ value: pg_isready
+
+ - it: scheduling knobs pass through to the pod spec
+ set:
+ database.postgres.tolerations:
+ - key: dedicated
+ documentSelector:
+ path: kind
+ value: StatefulSet
+ asserts:
+ - contains:
+ path: spec.template.spec.tolerations
+ content:
+ key: dedicated
+
+ - it: external postgres renders nothing
+ set:
+ database.postgres.deployInternal: false
+ asserts:
+ - hasDocuments:
+ count: 0
diff --git a/charts/sam-mesh/tests/gateway_test.yaml b/charts/sam-mesh/tests/gateway_test.yaml
new file mode 100644
index 00000000..23b69ced
--- /dev/null
+++ b/charts/sam-mesh/tests/gateway_test.yaml
@@ -0,0 +1,62 @@
+suite: gateway
+templates:
+ - templates/gateway.yaml
+release:
+ name: sam-mesh
+tests:
+ - it: disabled by default renders nothing
+ asserts:
+ - hasDocuments:
+ count: 0
+
+ - it: enabling it requires gateway.className
+ set:
+ gateway.enabled: true
+ asserts:
+ - failedTemplate:
+ errorPattern: gateway.className is required when gateway.enabled is true
+
+ - it: routes enrollment plus the console redirect and rewrite by default
+ set:
+ gateway.enabled: true
+ gateway.className: x
+ documentSelector:
+ path: kind
+ value: HTTPRoute
+ asserts:
+ # enrollment surface + consolePath 302 + consolePath URLRewrite
+ - lengthEqual:
+ path: spec.rules
+ count: 3
+
+ - it: empty consolePath leaves the console unrouted
+ set:
+ gateway.enabled: true
+ gateway.className: x
+ gateway.consolePath: ""
+ documentSelector:
+ path: kind
+ value: HTTPRoute
+ asserts:
+ - lengthEqual:
+ path: spec.rules
+ count: 1
+
+ - it: adminRoute adds the /admin rule
+ set:
+ gateway.enabled: true
+ gateway.className: x
+ gateway.adminRoute: true
+ documentSelector:
+ path: kind
+ value: HTTPRoute
+ asserts:
+ - lengthEqual:
+ path: spec.rules
+ count: 4
+ - contains:
+ path: spec.rules[1].matches
+ content:
+ path:
+ type: PathPrefix
+ value: /admin
diff --git a/charts/sam-mesh/tests/router-statefulset_test.yaml b/charts/sam-mesh/tests/router-statefulset_test.yaml
new file mode 100644
index 00000000..eb54ee90
--- /dev/null
+++ b/charts/sam-mesh/tests/router-statefulset_test.yaml
@@ -0,0 +1,109 @@
+suite: router statefulset
+templates:
+ - templates/router-statefulset.yaml
+release:
+ # collapses fullname to "sam-mesh" (see _helpers.tpl)
+ name: sam-mesh
+tests:
+ - it: pins the numeric distroless uid so runAsNonRoot is verifiable
+ documentSelector:
+ path: kind
+ value: StatefulSet
+ asserts:
+ - equal:
+ path: spec.template.spec.securityContext.runAsNonRoot
+ value: true
+ - equal:
+ path: spec.template.spec.securityContext.runAsUser
+ value: 65532
+ - equal:
+ path: spec.template.spec.initContainers[0].securityContext.runAsUser
+ value: 100
+
+ - it: announces the service DNS address when hostPort is unset
+ documentSelector:
+ path: kind
+ value: StatefulSet
+ asserts:
+ - contains:
+ path: spec.template.spec.containers[0].args
+ content: --external-addr=/dns4/sam-mesh-router/tcp/4501
+ - notExists:
+ path: spec.template.spec.containers[0].ports[0].hostPort
+
+ - it: hostPort binds both protocols and announces the node IP
+ set:
+ router.hostPort: 4501
+ documentSelector:
+ path: kind
+ value: StatefulSet
+ asserts:
+ - equal:
+ path: spec.template.spec.containers[0].ports[0].hostPort
+ value: 4501
+ - equal:
+ path: spec.template.spec.containers[0].ports[1].hostPort
+ value: 4501
+ - contains:
+ path: spec.template.spec.containers[0].args
+ content: --external-addr=/ip4/$(HOST_IP)/tcp/4501
+ - contains:
+ path: spec.template.spec.containers[0].args
+ content: --external-addr=/ip4/$(HOST_IP)/udp/4501/quic-v1
+
+ - it: externalAddrs overrides the derived announcement
+ set:
+ router.hostPort: 4501
+ router.externalAddrs:
+ - /dns4/x/tcp/1
+ documentSelector:
+ path: kind
+ value: StatefulSet
+ asserts:
+ - contains:
+ path: spec.template.spec.containers[0].args
+ content: --external-addr=/dns4/x/tcp/1
+ - notContains:
+ path: spec.template.spec.containers[0].args
+ content: --external-addr=/ip4/$(HOST_IP)/tcp/4501
+
+ - it: keeps the key on a PVC, not emptyDir
+ documentSelector:
+ path: kind
+ value: StatefulSet
+ asserts:
+ - equal:
+ path: spec.volumeClaimTemplates[0].metadata.name
+ value: router-data
+ - equal:
+ path: spec.volumeClaimTemplates[0].spec.resources.requests.storage
+ value: 1Gi
+ - notExists:
+ path: spec.template.spec.volumes
+
+ - it: useOidcToken swaps the bootstrap token for a projected JWT
+ set:
+ router.useOidcToken: true
+ documentSelector:
+ path: kind
+ value: StatefulSet
+ asserts:
+ - contains:
+ path: spec.template.spec.containers[0].args
+ content: --jwt-path=/var/run/secrets/tokens/sam-token
+ - notContains:
+ path: spec.template.spec.containers[0].args
+ content: --bootstrap-token=$(BOOTSTRAP_TOKEN)
+ - exists:
+ path: spec.template.spec.volumes[0].projected
+
+ - it: image.tag overrides global.imageTag for the router only
+ set:
+ router.image.tag: v9
+ documentSelector:
+ path: kind
+ value: StatefulSet
+ asserts:
+ - equal:
+ path: spec.template.spec.containers[0].image
+ value: sam-router:v9