Skip to content

Commit 387f82b

Browse files
committed
add helm unittest for imagepullsecret
1 parent 98e73fc commit 387f82b

File tree

8 files changed

+99
-26
lines changed

8 files changed

+99
-26
lines changed

.github/workflows/ci.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ jobs:
4949
- name: Set up chart-testing
5050
uses: helm/[email protected]
5151

52+
- name: Install helm unittest
53+
run: helm plugin install https://github.com/helm-unittest/helm-unittest.git
54+
5255
- name: Extract tag
5356
id: extract_tag
5457
run: |
@@ -61,6 +64,9 @@ jobs:
6164
- name: Run chart-testing (lint)
6265
run: ct lint --validate-maintainers=false --charts vaas-helm/charts/vaas
6366

67+
- name: Run helm unittest
68+
run: helm unittest --strict charts/vaas
69+
6470
- name: Install Minikube
6571
uses: manusa/[email protected]
6672
with:

charts/vaas/templates/gateway/_helpers.tpl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,27 @@ If release name contains chart name it will be used as a full name.
2525

2626
{{- define "gateway.imagePullSecrets" -}}
2727
{{- $ips := .Values.global.imagePullSecrets | default (list) -}}
28+
29+
{{- range $i, $e := $ips }}
30+
{{- if not (kindIs "map" $e) -}}
31+
{{- fail (printf "global.imagePullSecrets[%d] must be an object with 'name' (or 'secretName'), not %s" $i (kindOf $e)) -}}
32+
{{- end -}}
33+
{{- $n := (get $e "name") | default (get $e "secretName") -}}
34+
{{- if not $n -}}
35+
{{- fail (printf "global.imagePullSecrets[%d] must contain key 'name' or 'secretName'. Got keys: %v" $i (keys $e)) -}}
36+
{{- end -}}
37+
{{- end }}
38+
2839
{{- $hasIps := gt (len $ips) 0 -}}
2940
{{- $hasLocal := .Values.imagePullSecret -}}
3041
{{- $hasGlobalImagePullSecret := ((.Values.global).secret).imagePullSecret -}}
3142
{{- $hasGlobalDockerconfig := ((.Values.global).secret).dockerconfigjson -}}
3243

3344
{{- if or $hasIps $hasLocal $hasGlobalImagePullSecret $hasGlobalDockerconfig }}
3445
imagePullSecrets:
35-
{{- range $i, $entry := $ips }}
36-
{{- if not (kindIs "map" $entry) -}}
37-
{{- fail (printf "global.imagePullSecrets[%d] must be an object with 'name' (or 'secretName'), not %s" $i (kindOf $entry)) -}}
38-
{{- end -}}
39-
{{- $name := (get $entry "name") | default (get $entry "secretName") -}}
40-
{{- if not $name -}}
41-
{{- fail (printf "global.imagePullSecrets[%d] must contain key 'name' or 'secretName'. Got keys: %v" $i (keys $entry)) -}}
42-
{{- end }}
43-
- name: {{ $name }}
46+
{{- range $i, $e := $ips }}
47+
- name: {{ (get $e "name") | default (get $e "secretName") }}
4448
{{- end }}
45-
4649
{{- if $hasLocal }}
4750
- name: {{ include "gateway.fullname" . }}-image-pull-secret
4851
{{- end }}
@@ -57,6 +60,7 @@ imagePullSecrets:
5760
{{- end -}}
5861
{{- end -}}
5962

63+
6064
{{/*
6165
Create chart name and version as used by the chart label.
6266
*/}}

charts/vaas/templates/gdscan/_helpers.tpl

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,27 @@ app.kubernetes.io/managed-by: {{ .Release.Service }}
4444

4545
{{- define "gdscan.imagePullSecrets" -}}
4646
{{- $ips := .Values.global.imagePullSecrets | default (list) -}}
47+
48+
{{- range $i, $e := $ips }}
49+
{{- if not (kindIs "map" $e) -}}
50+
{{- fail (printf "global.imagePullSecrets[%d] must be an object with 'name' (or 'secretName'), not %s" $i (kindOf $e)) -}}
51+
{{- end -}}
52+
{{- $n := (get $e "name") | default (get $e "secretName") -}}
53+
{{- if not $n -}}
54+
{{- fail (printf "global.imagePullSecrets[%d] must contain key 'name' or 'secretName'. Got keys: %v" $i (keys $e)) -}}
55+
{{- end -}}
56+
{{- end }}
57+
4758
{{- $hasIps := gt (len $ips) 0 -}}
4859
{{- $hasLocal := .Values.imagePullSecret -}}
4960
{{- $hasGlobalImagePullSecret := ((.Values.global).secret).imagePullSecret -}}
5061
{{- $hasGlobalDockerconfig := ((.Values.global).secret).dockerconfigjson -}}
5162

5263
{{- if or $hasIps $hasLocal $hasGlobalImagePullSecret $hasGlobalDockerconfig }}
5364
imagePullSecrets:
54-
{{- range $i, $entry := $ips }}
55-
{{- if kindIs "string" $entry }}
56-
- name: {{ $entry }}
57-
{{- else if kindIs "map" $entry }}
58-
{{- if hasKey $entry "name" }}
59-
- name: {{ get $entry "name" }}
60-
{{- else if hasKey $entry "secretName" }}
61-
- name: {{ get $entry "secretName" }}
62-
{{- else }}
63-
{{- fail (printf "global.imagePullSecrets[%d] must have key 'name' (or 'secretName'). Got keys: %v" $i (keys $entry)) }}
64-
{{- end }}
65-
{{- else }}
66-
{{- fail (printf "global.imagePullSecrets[%d] has unsupported kind %s (type %s)" $i (kindOf $entry) (typeOf $entry)) }}
67-
{{- end }}
65+
{{- range $i, $e := $ips }}
66+
- name: {{ (get $e "name") | default (get $e "secretName") }}
6867
{{- end }}
69-
7068
{{- if $hasLocal }}
7169
- name: {{ include "gdscan.fullname" . }}-image-pull-secret
7270
{{- end }}
@@ -77,7 +75,7 @@ imagePullSecrets:
7775
- name: {{ include "gdscan.fullname" . }}-global-dockerconfigjson
7876
{{- end }}
7977
{{- else -}}
80-
{{- fail "You have to set at least one imagePullSecret (global.imagePullSecrets, imagePullSecret, global.secret.imagePullSecret or global.secret.dockerconfigjson)" }}
78+
{{- fail "You have to set at least one imagePullSecret: use global.imagePullSecrets (objects with 'name'/'secretName') or set imagePullSecret/global.secret.*" }}
8179
{{- end -}}
8280
{{- end -}}
8381

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{- if .Values._tests.enableImagePullSecretsTest }}
2+
apiVersion: v1
3+
kind: Pod
4+
metadata:
5+
name: imagepullsecrets-test
6+
spec:
7+
{{- include "gateway.imagePullSecrets" . | nindent 2 }}
8+
containers:
9+
- name: noop
10+
image: scratch
11+
{{- end }}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
suite: imagePullSecrets helper
2+
templates:
3+
- templates/imagepullsecrets-test.yaml
4+
5+
tests:
6+
- it: renders with object {name}
7+
values: [values/ok_object.yaml]
8+
asserts:
9+
- isKind: { of: Pod }
10+
- contains:
11+
path: spec.imagePullSecrets
12+
content: { name: ghcr-pull }
13+
14+
- it: renders with global.secret.* only
15+
values: [values/ok_global_secret.yaml]
16+
asserts:
17+
- isKind: { of: Pod }
18+
- contains:
19+
path: spec.imagePullSecrets
20+
content: { name: gateway-global-image-pull-secret }
21+
- contains:
22+
path: spec.imagePullSecrets
23+
content: { name: gateway-global-dockerconfigjson }
24+
25+
- it: fails on string entry
26+
values: [values/fail_string.yaml]
27+
asserts:
28+
- failedTemplate:
29+
errorPattern: "must be an object with 'name'"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_tests:
2+
enableImagePullSecretsTest: true
3+
global:
4+
imagePullSecrets:
5+
- my-secret
6+
redis:
7+
enabled: false
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
_tests:
2+
enableImagePullSecretsTest: true
3+
4+
global:
5+
imagePullSecrets: []
6+
secret:
7+
imagePullSecret: "e30K"
8+
dockerconfigjson: "e30K"
9+
10+
redis:
11+
enabled: false
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_tests:
2+
enableImagePullSecretsTest: true
3+
global:
4+
imagePullSecrets:
5+
- name: ghcr-pull
6+
redis:
7+
enabled: false

0 commit comments

Comments
 (0)