|
6 | 6 | package test |
7 | 7 |
|
8 | 8 | import ( |
| 9 | + "path/filepath" |
9 | 10 | "strings" |
10 | 11 | "testing" |
11 | 12 |
|
| 13 | + "github.com/ghodss/yaml" |
| 14 | + "github.com/gruntwork-io/terratest/modules/helm" |
12 | 15 | "github.com/gruntwork-io/terratest/modules/random" |
13 | 16 | "github.com/stretchr/testify/assert" |
14 | 17 | "github.com/stretchr/testify/require" |
15 | 18 | ) |
16 | 19 |
|
| 20 | +// Test that setting serviceAccount.create = true will cause the helm template to render the Service Account resource |
| 21 | +func TestK8SServiceAccountCreateTrueCreatesServiceAccount(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + randomSAName := strings.ToLower(random.UniqueId()) |
| 24 | + |
| 25 | + helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service")) |
| 26 | + require.NoError(t, err) |
| 27 | + |
| 28 | + // We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined. |
| 29 | + // We then use SetValues to override all the defaults. |
| 30 | + options := &helm.Options{ |
| 31 | + ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")}, |
| 32 | + SetValues: map[string]string{"serviceAccount.name": randomSAName, "serviceAccount.create": "true"}, |
| 33 | + } |
| 34 | + out := helm.RenderTemplate(t, options, helmChartPath, []string{"templates/serviceaccount.yaml"}) |
| 35 | + |
| 36 | + // We take the output and render it to a map to validate it has created a service account output or not |
| 37 | + rendered := map[string]interface{}{} |
| 38 | + err = yaml.Unmarshal([]byte(out), &rendered) |
| 39 | + assert.NoError(t, err) |
| 40 | + assert.NotEqual(t, 0, len(rendered)) |
| 41 | + assert.Equal(t, randomSAName, rendered["metadata"].(map[string]interface{})["name"]) |
| 42 | +} |
| 43 | + |
| 44 | +// Test that setting serviceAccount.create = false will cause the helm template to not render the Service Account |
| 45 | +// resource |
| 46 | +func TestK8SServiceAccountCreateFalse(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + randomSAName := strings.ToLower(random.UniqueId()) |
| 49 | + |
| 50 | + helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service")) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + // We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined. |
| 54 | + // We then use SetValues to override all the defaults. |
| 55 | + options := &helm.Options{ |
| 56 | + ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")}, |
| 57 | + SetValues: map[string]string{"serviceAccount.name": randomSAName, "serviceAccount.create": "false"}, |
| 58 | + } |
| 59 | + out := helm.RenderTemplate(t, options, helmChartPath, []string{"templates/serviceaccount.yaml"}) |
| 60 | + |
| 61 | + // We take the output and render it to a map to validate it has created a service account output or not |
| 62 | + rendered := map[string]interface{}{} |
| 63 | + err = yaml.Unmarshal([]byte(out), &rendered) |
| 64 | + assert.NoError(t, err) |
| 65 | + assert.Equal(t, 0, len(rendered)) |
| 66 | +} |
| 67 | + |
17 | 68 | func TestK8SServiceServiceAccountInjection(t *testing.T) { |
18 | 69 | t.Parallel() |
19 | 70 | randomSAName := strings.ToLower(random.UniqueId()) |
|
0 commit comments