|
| 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 | +} |
0 commit comments