Skip to content

Commit 8e39e69

Browse files
sd109JohnGarbutt
andauthored
Add helm chart linting and snapshot testing (#192)
Co-authored-by: John Garbutt <[email protected]>
1 parent 978e851 commit 8e39e69

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed

.github/workflows/helm-lint.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# NOTE: This workflow can be run locally using https://github.com/nektos/act with:
2+
# act -W .github/workflows/helm-lint.yaml workflow_call -s GITHUB_TOKEN=$(gh auth token)
3+
name: Helm Lint
4+
on:
5+
workflow_call:
6+
inputs:
7+
ref:
8+
type: string
9+
description: The Git ref under test.
10+
required: true
11+
12+
jobs:
13+
lint:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
with:
19+
ref: ${{ inputs.ref }}
20+
fetch-depth: 0
21+
22+
- name: Set up Helm
23+
uses: azure/setup-helm@v4
24+
with:
25+
version: v3.15.3
26+
27+
- name: Set up chart-testing
28+
uses: helm/chart-testing-action@v2
29+
30+
- name: Run chart-testing (lint)
31+
run: |-
32+
ct lint \
33+
--target-branch ${{ github.event.repository.default_branch }} \
34+
--charts chart/ \
35+
--validate-maintainers=false
36+
37+
- name: Run template validation
38+
run: |-
39+
helm template foo chart \
40+
| docker run -i --rm ghcr.io/yannh/kubeconform:latest \
41+
--strict --summary
42+
43+
- name: Run manifest snapshot test
44+
run: docker run -i --rm -v $(pwd):/apps helmunittest/helm-unittest chart

.github/workflows/test-pr.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ jobs:
3131
with:
3232
ref: ${{ github.event.pull_request.head.sha }}
3333

34+
# Run the chart linting on every PR, even from external repos
35+
lint:
36+
uses: ./.github/workflows/lint.yaml
37+
with:
38+
ref: ${{ github.event.pull_request.head.sha }}
39+
3440
# This job exists so that PRs from outside the main repo are rejected
3541
fail_on_remote:
3642
runs-on: ubuntu-latest

chart/.helmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
.idea/
2222
*.tmproj
2323
.vscode/
24+
# Helm unit-test files
25+
tests/
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
templated manifests should match snapshot:
2+
1: |
3+
apiVersion: rbac.authorization.k8s.io/v1
4+
kind: ClusterRole
5+
metadata:
6+
labels:
7+
app.kubernetes.io/instance: RELEASE-NAME
8+
app.kubernetes.io/managed-by: Helm
9+
app.kubernetes.io/name: cluster-api-janitor-openstack
10+
app.kubernetes.io/version: main
11+
helm.sh/chart: cluster-api-janitor-openstack-0.1.0
12+
name: release-name-cluster-api-janitor-openstack
13+
rules:
14+
- apiGroups:
15+
- ""
16+
resources:
17+
- namespaces
18+
verbs:
19+
- list
20+
- watch
21+
- apiGroups:
22+
- ""
23+
- events.k8s.io
24+
resources:
25+
- events
26+
verbs:
27+
- create
28+
- apiGroups:
29+
- ""
30+
resources:
31+
- secrets
32+
verbs:
33+
- get
34+
- delete
35+
- apiGroups:
36+
- infrastructure.cluster.x-k8s.io
37+
resources:
38+
- openstackclusters
39+
verbs:
40+
- list
41+
- get
42+
- watch
43+
- patch
44+
- apiGroups:
45+
- apiextensions.k8s.io
46+
resources:
47+
- customresourcedefinitions
48+
verbs:
49+
- list
50+
- get
51+
- watch
52+
2: |
53+
apiVersion: rbac.authorization.k8s.io/v1
54+
kind: ClusterRoleBinding
55+
metadata:
56+
labels:
57+
app.kubernetes.io/instance: RELEASE-NAME
58+
app.kubernetes.io/managed-by: Helm
59+
app.kubernetes.io/name: cluster-api-janitor-openstack
60+
app.kubernetes.io/version: main
61+
helm.sh/chart: cluster-api-janitor-openstack-0.1.0
62+
name: release-name-cluster-api-janitor-openstack
63+
roleRef:
64+
apiGroup: rbac.authorization.k8s.io
65+
kind: ClusterRole
66+
name: release-name-cluster-api-janitor-openstack
67+
subjects:
68+
- kind: ServiceAccount
69+
name: release-name-cluster-api-janitor-openstack
70+
namespace: NAMESPACE
71+
3: |
72+
apiVersion: apps/v1
73+
kind: Deployment
74+
metadata:
75+
labels:
76+
app.kubernetes.io/instance: RELEASE-NAME
77+
app.kubernetes.io/managed-by: Helm
78+
app.kubernetes.io/name: cluster-api-janitor-openstack
79+
app.kubernetes.io/version: main
80+
helm.sh/chart: cluster-api-janitor-openstack-0.1.0
81+
name: release-name-cluster-api-janitor-openstack
82+
spec:
83+
replicas: 1
84+
selector:
85+
matchLabels:
86+
app.kubernetes.io/instance: RELEASE-NAME
87+
app.kubernetes.io/name: cluster-api-janitor-openstack
88+
strategy:
89+
type: Recreate
90+
template:
91+
metadata:
92+
labels:
93+
app.kubernetes.io/instance: RELEASE-NAME
94+
app.kubernetes.io/name: cluster-api-janitor-openstack
95+
spec:
96+
containers:
97+
- env:
98+
- name: CAPI_JANITOR_DEFAULT_VOLUMES_POLICY
99+
value: delete
100+
image: ghcr.io/azimuth-cloud/cluster-api-janitor-openstack:main
101+
imagePullPolicy: IfNotPresent
102+
name: operator
103+
resources: {}
104+
securityContext:
105+
allowPrivilegeEscalation: false
106+
capabilities:
107+
drop:
108+
- ALL
109+
readOnlyRootFilesystem: true
110+
volumeMounts:
111+
- mountPath: /tmp
112+
name: tmp
113+
securityContext:
114+
runAsNonRoot: true
115+
serviceAccountName: release-name-cluster-api-janitor-openstack
116+
volumes:
117+
- emptyDir: {}
118+
name: tmp
119+
4: |
120+
apiVersion: v1
121+
kind: ServiceAccount
122+
metadata:
123+
labels:
124+
app.kubernetes.io/instance: RELEASE-NAME
125+
app.kubernetes.io/managed-by: Helm
126+
app.kubernetes.io/name: cluster-api-janitor-openstack
127+
app.kubernetes.io/version: main
128+
helm.sh/chart: cluster-api-janitor-openstack-0.1.0
129+
name: release-name-cluster-api-janitor-openstack

chart/tests/snapshot_test.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# To update manifest snapshots run helm unittest plugin with -u option:
2+
# docker run -i --rm -v $(pwd):/apps helmunittest/helm-unittest -u chart
3+
suite: Manifest snapshot tests
4+
tests:
5+
- it: templated manifests should match snapshot
6+
asserts:
7+
- matchSnapshot: {}

0 commit comments

Comments
 (0)