Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix variable overrides in targets for non-string variables #1397

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions bundle/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,15 +408,19 @@ func rewriteShorthands(v dyn.Value) (dyn.Value, error) {

// For each variable, normalize its contents if it is a single string.
return dyn.Map(target, "variables", dyn.Foreach(func(_ dyn.Path, variable dyn.Value) (dyn.Value, error) {
if variable.Kind() != dyn.KindString {
switch variable.Kind() {

case dyn.KindString, dyn.KindBool, dyn.KindFloat, dyn.KindInt:
// Rewrite the variable to a map with a single key called "default".
// This conforms to the variable type. Normalization back to the typed
// configuration will convert this to a string if necessary.
return dyn.NewValue(map[string]dyn.Value{
"default": variable,
}, variable.Location()), nil

default:
return variable, nil
}

// Rewrite the variable to a map with a single key called "default".
// This conforms to the variable type.
return dyn.NewValue(map[string]dyn.Value{
"default": variable,
}, variable.Location()), nil
}))
}))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
bundle:
name: foobar

resources:
pipelines:
my_pipeline:
name: ${var.foo}
continuous: ${var.baz}
clusters:
- num_workers: ${var.bar}



variables:
foo:
default: "a_string"
description: "A string variable"

bar:
default: 42
description: "An integer variable"

baz:
default: true
description: "A boolean variable"

targets:
use-default-variable-values:

override-string-variable:
variables:
foo: "overridden_string"

override-int-variable:
variables:
bar: 43

override-both-bool-and-string-variables:
variables:
foo: "overridden_string"
baz: false
49 changes: 49 additions & 0 deletions bundle/tests/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,52 @@ func TestVariablesWithTargetLookupOverrides(t *testing.T) {
assert.Equal(t, "cluster: some-test-cluster", b.Config.Variables["d"].Lookup.String())
assert.Equal(t, "instance-pool: some-test-instance-pool", b.Config.Variables["e"].Lookup.String())
}

func TestVariableTargetOverrides(t *testing.T) {
var tcases = []struct {
targetName string
pipelineName string
pipelineContinuous bool
pipelineNumWorkers int
}{
{
"use-default-variable-values",
"a_string",
true,
42,
},
{
"override-string-variable",
"overridden_string",
true,
42,
},
{
"override-int-variable",
"a_string",
true,
43,
},
{
"override-both-bool-and-string-variables",
"overridden_string",
false,
42,
},
}

for _, tcase := range tcases {
t.Run(tcase.targetName, func(t *testing.T) {
b := loadTarget(t, "./variables/variable_overrides_in_target", tcase.targetName)
diags := bundle.Apply(context.Background(), b, bundle.Seq(
mutator.SetVariables(),
mutator.ResolveVariableReferences("variables")),
)
require.NoError(t, diags.Error())

assert.Equal(t, tcase.pipelineName, b.Config.Resources.Pipelines["my_pipeline"].Name)
assert.Equal(t, tcase.pipelineContinuous, b.Config.Resources.Pipelines["my_pipeline"].Continuous)
assert.Equal(t, tcase.pipelineNumWorkers, b.Config.Resources.Pipelines["my_pipeline"].Clusters[0].NumWorkers)
})
}
}
Loading