Skip to content

Commit 116a17e

Browse files
authored
chore: update example tests (#174)
Update tests for example values files to coalesce values using the built-in Helm handling, rather than our custom rendering. We should only use the struct-based rendering for unit tests.
1 parent ffce697 commit 116a17e

File tree

4 files changed

+430
-119
lines changed

4 files changed

+430
-119
lines changed

tests/defaults_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,19 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/require"
7-
"helm.sh/helm/v3/pkg/chart/loader"
87
)
98

109
// TestDefault loads the chart and checks metadata.
1110
func TestDefault(t *testing.T) {
1211
t.Parallel()
1312

14-
chart, err := loader.LoadDir("..")
15-
require.NoError(t, err, "loaded chart successfully")
16-
require.NotNil(t, chart, "chart must be non-nil")
17-
require.True(t, chart.IsRoot(), "chart must be a root chart")
13+
chart := LoadChart(t)
1814
require.NoError(t, chart.Validate(), "chart has valid metadata")
1915

2016
metadata := chart.Metadata
2117
require.Equal(t, "coder", metadata.Name, "unexpected chart name")
2218
require.False(t, metadata.Deprecated, "chart should not be deprecated")
2319

24-
values, err := ConvertMapToCoderValues(chart.Values, false)
25-
require.NoError(t, err, "converted map to coder values")
26-
require.NotNil(t, values, "values must be non-nil")
27-
coderd := values.Coderd
20+
coderd := chart.OriginalValues.Coderd
2821
require.Equal(t, 1, *coderd.Replicas, "expected 1 replica by default")
2922
}

tests/examples_test.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"helm.sh/helm/v3/pkg/chart/loader"
8+
"helm.sh/helm/v3/pkg/chartutil"
9+
"helm.sh/helm/v3/pkg/engine"
10+
appsv1 "k8s.io/api/apps/v1"
11+
corev1 "k8s.io/api/core/v1"
12+
"k8s.io/utils/pointer"
13+
)
14+
15+
// TestExamples loads the example values files and performs
16+
// some basic checks.
17+
func TestExamples(t *testing.T) {
18+
t.Parallel()
19+
20+
chart, err := loader.LoadDir("..")
21+
require.NoError(t, err, "loaded chart successfully")
22+
require.NotNil(t, chart, "chart must be non-nil")
23+
24+
exampleOpenShift, err := ReadValuesAsMap("../examples/openshift/openshift.values.yaml")
25+
require.NoError(t, err, "failed to load OpenShift example values")
26+
27+
exampleKind, err := ReadValuesAsMap("../examples/kind/kind.values.yaml")
28+
require.NoError(t, err, "failed to load Kind example values")
29+
30+
tests := []struct {
31+
Name string
32+
Values map[string]interface{}
33+
PodSecurityContext *corev1.PodSecurityContext
34+
ContainerSecurityContext *corev1.SecurityContext
35+
}{
36+
{
37+
Name: "default",
38+
Values: nil,
39+
PodSecurityContext: &corev1.PodSecurityContext{
40+
RunAsUser: pointer.Int64(1000),
41+
RunAsGroup: nil,
42+
RunAsNonRoot: pointer.Bool(true),
43+
SeccompProfile: &corev1.SeccompProfile{
44+
Type: corev1.SeccompProfileTypeRuntimeDefault,
45+
LocalhostProfile: nil,
46+
},
47+
},
48+
ContainerSecurityContext: &corev1.SecurityContext{
49+
RunAsUser: nil,
50+
RunAsGroup: nil,
51+
RunAsNonRoot: nil,
52+
Capabilities: nil,
53+
Privileged: nil,
54+
SELinuxOptions: nil,
55+
WindowsOptions: nil,
56+
ReadOnlyRootFilesystem: pointer.Bool(true),
57+
AllowPrivilegeEscalation: pointer.Bool(false),
58+
ProcMount: nil,
59+
SeccompProfile: &corev1.SeccompProfile{
60+
Type: corev1.SeccompProfileTypeRuntimeDefault,
61+
LocalhostProfile: nil,
62+
},
63+
},
64+
}, {
65+
Name: "openshift",
66+
Values: exampleOpenShift,
67+
PodSecurityContext: &corev1.PodSecurityContext{
68+
RunAsUser: nil,
69+
RunAsGroup: nil,
70+
RunAsNonRoot: pointer.Bool(true),
71+
SeccompProfile: nil,
72+
},
73+
ContainerSecurityContext: &corev1.SecurityContext{
74+
RunAsUser: nil,
75+
RunAsGroup: nil,
76+
RunAsNonRoot: nil,
77+
Capabilities: nil,
78+
Privileged: nil,
79+
SELinuxOptions: nil,
80+
WindowsOptions: nil,
81+
ReadOnlyRootFilesystem: pointer.Bool(true),
82+
AllowPrivilegeEscalation: pointer.Bool(false),
83+
ProcMount: nil,
84+
SeccompProfile: nil,
85+
},
86+
},
87+
{
88+
Name: "kind",
89+
Values: exampleKind,
90+
PodSecurityContext: &corev1.PodSecurityContext{
91+
RunAsUser: pointer.Int64(1000),
92+
RunAsNonRoot: pointer.Bool(true),
93+
SeccompProfile: &corev1.SeccompProfile{
94+
Type: corev1.SeccompProfileTypeRuntimeDefault,
95+
},
96+
},
97+
ContainerSecurityContext: &corev1.SecurityContext{
98+
RunAsUser: pointer.Int64(1000),
99+
RunAsGroup: pointer.Int64(1000),
100+
RunAsNonRoot: pointer.Bool(true),
101+
Capabilities: nil,
102+
Privileged: nil,
103+
SELinuxOptions: nil,
104+
WindowsOptions: nil,
105+
ReadOnlyRootFilesystem: pointer.Bool(true),
106+
AllowPrivilegeEscalation: pointer.Bool(false),
107+
ProcMount: nil,
108+
SeccompProfile: &corev1.SeccompProfile{
109+
Type: corev1.SeccompProfileTypeRuntimeDefault,
110+
LocalhostProfile: nil,
111+
},
112+
},
113+
},
114+
}
115+
116+
for _, test := range tests {
117+
test := test
118+
t.Run(test.Name, func(t *testing.T) {
119+
t.Parallel()
120+
121+
values, err := chartutil.ToRenderValues(chart, test.Values, DefaultReleaseOptions(), chartutil.DefaultCapabilities.Copy())
122+
require.NoError(t, err, "failed to generate render values")
123+
124+
manifests, err := engine.Render(chart, values)
125+
require.NoError(t, err, "failed to render chart")
126+
127+
objs, err := LoadObjectsFromManifests(manifests)
128+
require.NoError(t, err, "failed to convert manifests to objects")
129+
130+
// Find the coderd Deployment
131+
var found bool
132+
for _, obj := range objs {
133+
deployment, ok := obj.(*appsv1.Deployment)
134+
if ok && deployment.Name == "coderd" {
135+
found = true
136+
137+
require.Equal(t, test.PodSecurityContext,
138+
deployment.Spec.Template.Spec.SecurityContext,
139+
"expected matching pod securityContext")
140+
require.Len(t, deployment.Spec.Template.Spec.Containers, 1,
141+
"expected one container")
142+
require.Equal(t, test.ContainerSecurityContext,
143+
deployment.Spec.Template.Spec.Containers[0].SecurityContext,
144+
"expected matching container securityContext")
145+
146+
break
147+
}
148+
}
149+
require.True(t, found, "expected coderd deployment in manifests")
150+
})
151+
}
152+
}

tests/security_context_test.go

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)