|
| 1 | +package template_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + appsv1 "k8s.io/api/apps/v1" |
| 10 | + |
| 11 | + "github.com/gruntwork-io/terratest/modules/helm" |
| 12 | + "github.com/gruntwork-io/terratest/modules/k8s" |
| 13 | + "github.com/gruntwork-io/terratest/modules/random" |
| 14 | +) |
| 15 | + |
| 16 | +func TestChartTemplateSecurityEnabled(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + // Path to the helm chart we will test |
| 20 | + helmChartPath, err := filepath.Abs("../../charts") |
| 21 | + releaseName := "marklogic-sec-test" |
| 22 | + t.Log(helmChartPath, releaseName) |
| 23 | + require.NoError(t, err) |
| 24 | + |
| 25 | + // Set up the namespace; confirm that the template renders the expected value for the namespace. |
| 26 | + namespaceName := "marklogic-" + strings.ToLower(random.UniqueId()) |
| 27 | + t.Logf("Namespace: %s\n", namespaceName) |
| 28 | + |
| 29 | + // Setup the args for helm install |
| 30 | + options := &helm.Options{ |
| 31 | + SetValues: map[string]string{ |
| 32 | + "image.repository": "marklogicdb/marklogic-db", |
| 33 | + "image.tag": "latest", |
| 34 | + "persistence.enabled": "false", |
| 35 | + "securityContext.enabled": "true", |
| 36 | + "securityContext.runAsUser": "1000", |
| 37 | + "securityContext.runAsNonRoot": "true", |
| 38 | + "securityContext.allowPrivilegeEscalation": "false", |
| 39 | + }, |
| 40 | + KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName), |
| 41 | + } |
| 42 | + |
| 43 | + // render the tempate |
| 44 | + output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/statefulset.yaml"}) |
| 45 | + |
| 46 | + var statefulset appsv1.Deployment |
| 47 | + helm.UnmarshalK8SYaml(t, output, &statefulset) |
| 48 | + |
| 49 | + // Verify the name and namespace matches |
| 50 | + require.Equal(t, namespaceName, statefulset.Namespace) |
| 51 | + |
| 52 | + // Verify the image matches |
| 53 | + expectedImage := "marklogicdb/marklogic-db:latest" |
| 54 | + statefulSetContainers := statefulset.Spec.Template.Spec.Containers |
| 55 | + require.Equal(t, len(statefulSetContainers), 1) |
| 56 | + require.Equal(t, statefulSetContainers[0].Image, expectedImage) |
| 57 | +} |
0 commit comments