forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
84 lines (74 loc) · 3.21 KB
/
template.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package helm
import (
"encoding/json"
"path/filepath"
"testing"
"github.com/ghodss/yaml"
"github.com/gruntwork-io/gruntwork-cli/errors"
"github.com/stretchr/testify/require"
"github.com/gruntwork-io/terratest/modules/files"
)
// RenderTemplate runs `helm template` to render the template given the provided options and returns stdout/stderr from
// the template command. If you pass in templateFiles, this will only render those templates. This function will fail
// the test if there is an error rendering the template.
func RenderTemplate(t *testing.T, options *Options, chartDir string, templateFiles []string) string {
out, err := RenderTemplateE(t, options, chartDir, templateFiles)
require.NoError(t, err)
return out
}
// RenderTemplateE runs `helm template` to render the template given the provided options and returns stdout/stderr from
// the template command. If you pass in templateFiles, this will only render those templates.
func RenderTemplateE(t *testing.T, options *Options, chartDir string, templateFiles []string) (string, error) {
// First, verify the charts dir exists
absChartDir, err := filepath.Abs(chartDir)
if err != nil {
return "", errors.WithStackTrace(err)
}
if !files.FileExists(chartDir) {
return "", errors.WithStackTrace(ChartNotFoundError{chartDir})
}
// Now construct the args
// We first construct the template args
args := []string{}
args, err = getValuesArgsE(t, options, args...)
if err != nil {
return "", err
}
for _, templateFile := range templateFiles {
// validate this is a valid template file
absTemplateFile := filepath.Join(absChartDir, templateFile)
if !files.FileExists(absTemplateFile) {
return "", errors.WithStackTrace(TemplateFileNotFoundError{Path: templateFile, ChartDir: absChartDir})
}
// Note: we only get the abs template file path to check it actually exists, but the `helm template` command
// expects the relative path from the chart.
args = append(args, "-x", templateFile)
}
// ... and add the chart at the end as the command expects
args = append(args, chartDir)
// Finally, call out to helm template command
return RunHelmCommandAndGetOutputE(t, options, "template", args...)
}
// UnmarshalK8SYaml is the same as UnmarshalK8SYamlE, but will fail the test if there is an error.
func UnmarshalK8SYaml(t *testing.T, yamlData string, destinationObj interface{}) {
require.NoError(t, UnmarshalK8SYamlE(t, yamlData, destinationObj))
}
// UnmarshalK8SYamlE can be used to take template outputs and unmarshal them into the corresponding client-go struct. For
// example, suppose you render the template into a Deployment object. You can unmarshal the yaml as follows:
//
// var deployment appsv1.Deployment
// UnmarshalK8SYamlE(t, renderedOutput, &deployment)
//
// At the end of this, the deployment variable will be populated.
func UnmarshalK8SYamlE(t *testing.T, yamlData string, destinationObj interface{}) error {
// NOTE: the client-go library can only decode json, so we will first convert the yaml to json before unmarshaling
jsonData, err := yaml.YAMLToJSON([]byte(yamlData))
if err != nil {
return errors.WithStackTrace(err)
}
err = json.Unmarshal(jsonData, destinationObj)
if err != nil {
return errors.WithStackTrace(err)
}
return nil
}