Skip to content

Commit 89ab770

Browse files
authored
Add helm_template_test.values (#228)
Address part 2 of #226 - Duplicate the `values` argument from `helm_template` into `helm_template_test`. - Update a test of `helm_template_test` to uses `values` and also so that it detects that the extra values files are in fact used.
1 parent db0cb9b commit 89ab770

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

helm/private/runner/runner.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ func loadTemplatePatterns(path string) (map[string][]string, error) {
7575
return data, nil
7676
}
7777

78+
// Flag type for a list of values passed as mutiple arguments.
79+
type valuesList []string
80+
81+
// Provide the flag.Value interface in the simplest possible way.
82+
func (vs *valuesList) Set(value string) error {
83+
*vs = append(*vs, value)
84+
return nil
85+
}
86+
87+
// Provide the flag.Value interface in the simplest possible way.
88+
func (vs *valuesList) String() string {
89+
return fmt.Sprint(*vs)
90+
}
91+
7892
func main() {
7993
// Get the file path for args
8094
argsRlocation := os.Getenv("RULES_HELM_HELM_RUNNER_ARGS_FILE")
@@ -92,11 +106,14 @@ func main() {
92106

93107
internalArgs, helmArgs := parseArgsUpToDashDash(argv)
94108

109+
var values valuesList
110+
95111
// Setup flags for helm, chart, registry_url, and image_pushers
96112
rawHelmPath := flag.String("helm", "", "Path to helm binary")
97113
rawHelmPluginsPath := flag.String("helm_plugins", "", "The path to helm plugins.")
98114
rawChartPath := flag.String("chart", "", "Path to Helm .tgz file")
99115
rawImagePushers := flag.String("image_pushers", "", "Comma-separated list of image pusher executables")
116+
flag.Var(&values, "values", "Extra Values files (maye be used mutiple times).")
100117

101118
// Parse command line arguments
102119
flag.CommandLine.Parse(internalArgs)
@@ -131,6 +148,11 @@ func main() {
131148
}
132149
}
133150

151+
for _, value := range values {
152+
// Translate to correct path and forward to helm.
153+
helmArgs = append(helmArgs, "--values", helm_utils.GetRunfile(value))
154+
}
155+
134156
var imagePushers []string
135157
if *rawImagePushers != "" {
136158
for _, pusher := range strings.Split(*rawImagePushers, ",") {

helm/private/template.bzl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ def _helm_template_test_impl(ctx):
1212
ctx.label,
1313
))
1414

15+
if ctx.attr.installer and ctx.files.values:
16+
fail("values is not supported with installer")
17+
1518
template_patterns = None
1619
if ctx.attr.template_patterns:
1720
template_patterns = ctx.actions.declare_file("{}.template_patterns.json".format(ctx.label.name))
@@ -38,6 +41,10 @@ def _helm_template_test_impl(ctx):
3841
args.add("-chart", rlocationpath(pkg_info.chart, ctx.workspace_name))
3942
args.add("-helm", rlocationpath(toolchain.helm, ctx.workspace_name))
4043
args.add("-helm_plugins", rlocationpath(toolchain.helm_plugins, ctx.workspace_name))
44+
45+
for values in ctx.files.values:
46+
args.add("--values", rlocationpath(values, ctx.workspace_name))
47+
4148
args.add("--")
4249
args.add("template")
4350
args.add(rlocationpath(pkg_info.chart, ctx.workspace_name))
@@ -47,7 +54,7 @@ def _helm_template_test_impl(ctx):
4754
content = args,
4855
)
4956

50-
runfiles = runfiles.merge(ctx.runfiles([
57+
runfiles = runfiles.merge(ctx.runfiles(ctx.files.values + [
5158
args_file,
5259
ctx.executable._runner,
5360
toolchain.helm,
@@ -108,6 +115,10 @@ helm_template_test = rule(
108115
"template_patterns": attr.string_list_dict(
109116
doc = "A mapping of template paths to regex patterns required to match.",
110117
),
118+
"values": attr.label_list(
119+
doc = "Values files to pass to `helm template --values`.",
120+
allow_files = [".yml", ".yaml"],
121+
),
111122
"_copier": attr.label(
112123
cfg = "exec",
113124
executable = True,

tests/template_with_values/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ helm_template(
2222
helm_template_test(
2323
name = "template_with_values_template_test",
2424
chart = ":template_with_values",
25+
template_patterns = {
26+
"template-with-values/templates/deployment.yaml": [
27+
" replicas: 10\\b", # overriden by template_values.yaml
28+
],
29+
},
30+
values = [
31+
":template_values.yaml",
32+
],
2533
)

0 commit comments

Comments
 (0)