-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathresolve_variable_references_test.go
More file actions
150 lines (132 loc) · 4.11 KB
/
resolve_variable_references_test.go
File metadata and controls
150 lines (132 loc) · 4.11 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package mutator
import (
"testing"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/cli/bundle/config/variable"
"github.com/databricks/cli/libs/telemetry/protos"
"github.com/databricks/databricks-sdk-go/service/pipelines"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestResolveVariableReferencesDetectsNestedVarRef(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
Name: "${var.env_${var.region}}",
},
},
}
diags := bundle.Apply(t.Context(), b, ResolveVariableReferencesWithoutResources())
// The nested reference won't resolve, but we should still detect it.
_ = diags
assert.Contains(t, b.Metrics.BoolValues, protos.BoolMapEntry{Key: "nested_var_reference_used", Value: true})
}
func TestResolveVariableReferencesNoNestedVarRef(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
Name: "${var.env}",
},
},
}
diags := bundle.Apply(t.Context(), b, ResolveVariableReferencesWithoutResources())
_ = diags
for _, entry := range b.Metrics.BoolValues {
assert.NotEqual(t, "nested_var_reference_used", entry.Key)
}
}
func TestResolveVariableReferencesWithSourceLinkedDeployment(t *testing.T) {
testCases := []struct {
enabled bool
assert func(t *testing.T, b *bundle.Bundle)
}{
{
true,
func(t *testing.T, b *bundle.Bundle) {
// Variables that use workspace file path should have SyncRootValue during resolution phase
require.Equal(t, "sync/root/path", b.Config.Resources.Pipelines["pipeline1"].Configuration["source"])
// The file path itself should remain the same
require.Equal(t, "file/path", b.Config.Workspace.FilePath)
},
},
{
false,
func(t *testing.T, b *bundle.Bundle) {
require.Equal(t, "file/path", b.Config.Resources.Pipelines["pipeline1"].Configuration["source"])
require.Equal(t, "file/path", b.Config.Workspace.FilePath)
},
},
}
for _, testCase := range testCases {
b := &bundle.Bundle{
SyncRootPath: "sync/root/path",
Config: config.Root{
Presets: config.Presets{
SourceLinkedDeployment: &testCase.enabled,
},
Workspace: config.Workspace{
FilePath: "file/path",
},
Resources: config.Resources{
Pipelines: map[string]*resources.Pipeline{
"pipeline1": {
CreatePipeline: pipelines.CreatePipeline{
Configuration: map[string]string{
"source": "${workspace.file_path}",
},
},
},
},
},
},
}
diags := bundle.Apply(t.Context(), b, ResolveVariableReferencesOnlyResources("workspace"))
require.NoError(t, diags.Error())
testCase.assert(t, b)
}
}
func TestResolveVariableReferencesRoundsNoReferences(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
Name: "literal-name",
},
},
}
diags := bundle.Apply(t.Context(), b, ResolveVariableReferencesWithoutResources())
require.NoError(t, diags.Error())
// No references means a single round with no updates, so gt_1 should not be set.
for _, entry := range b.Metrics.BoolValues {
assert.NotEqual(t, "variable_resolution_rounds_gt_1", entry.Key)
assert.NotEqual(t, "variable_resolution_rounds_gt_3", entry.Key)
}
}
func TestResolveVariableReferencesRoundsGt1MultiRound(t *testing.T) {
// Set up a chain: bundle.name -> var.a -> var.b -> literal.
// This requires 2 rounds to fully resolve.
b := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
Name: "${var.a}",
},
Variables: map[string]*variable.Variable{
"a": {
Value: "${var.b}",
},
"b": {
Value: "final",
},
},
},
}
diags := bundle.Apply(t.Context(), b, ResolveVariableReferencesWithoutResources())
require.NoError(t, diags.Error())
assert.Equal(t, "final", b.Config.Bundle.Name)
assert.Contains(t, b.Metrics.BoolValues, protos.BoolMapEntry{Key: "variable_resolution_rounds_gt_1", Value: true})
// 2 rounds should not trigger gt_3.
for _, entry := range b.Metrics.BoolValues {
assert.NotEqual(t, "variable_resolution_rounds_gt_3", entry.Key)
}
}