forked from gruntwork-io/helm-kubernetes-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s_service_canary_deployment_template_test.go
66 lines (53 loc) · 2.64 KB
/
k8s_service_canary_deployment_template_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// +build all tpl
// NOTE: We use build flags to differentiate between template tests and integration tests so that you can conveniently
// run just the template tests. See the test README for more information.
package test
import (
"fmt"
"path/filepath"
"strings"
"testing"
"github.com/ghodss/yaml"
"github.com/gruntwork-io/terratest/modules/helm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Test that setting canary.enabled = false will cause the helm template to not render the canary Deployment resource
func TestK8SServiceCanaryEnabledFalseDoesNotCreateCanaryDeployment(t *testing.T) {
t.Parallel()
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")},
SetValues: map[string]string{"canary.enabled": "false"},
}
_, err = helm.RenderTemplateE(t, options, helmChartPath, "canary", []string{"templates/canarydeployment.yaml"})
require.Error(t, err)
}
// Test that configuring a canary deployment will render to a manifest with a container that is clearly a canary
func TestK8SServiceCanaryEnabledCreatesCanaryDeployment(t *testing.T) {
t.Parallel()
helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service"))
require.NoError(t, err)
// We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined.
// We then use SetValues to override all the defaults.
options := &helm.Options{
ValuesFiles: []string{
filepath.Join("..", "charts", "k8s-service", "linter_values.yaml"),
filepath.Join("fixtures", "canary_deployment_values.yaml"),
},
}
out := helm.RenderTemplate(t, options, helmChartPath, "canary", []string{"templates/canarydeployment.yaml"})
// We take the output and render it to a map to validate it has created a canary deployment or not
rendered := map[string]interface{}{}
err = yaml.Unmarshal([]byte(out), &rendered)
assert.NoError(t, err)
assert.NotEqual(t, 0, len(rendered))
// Inspect the name of the rendered canary deployment
nameField := rendered["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["containers"].([]interface{})[0].(map[string]interface{})["name"]
nameString := fmt.Sprintf("%v", nameField)
// Ensure the name contains the string "-canary"
assert.True(t, strings.Contains(string(nameString), "-canary"))
}