Skip to content

Commit 372a892

Browse files
committed
add tests for dynamic<->static block overrides
1 parent 1c41920 commit 372a892

4 files changed

Lines changed: 147 additions & 2 deletions

File tree

override_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,89 @@ func TestMergeBlock(t *testing.T) {
185185
assert.Empty(t, blocks[0].Labels())
186186
})
187187

188+
t.Run("MixedStaticDynamicSuppression", func(t *testing.T) {
189+
t.Parallel()
190+
primary := parseBlock(t, `resource "a" "b" {
191+
option {
192+
name = "static"
193+
}
194+
dynamic "option" {
195+
for_each = var.list
196+
content {
197+
name = option.value
198+
}
199+
}
200+
}`)
201+
override := parseBlock(t, `resource "a" "b" {
202+
option {
203+
name = "replaced"
204+
}
205+
}`)
206+
mergeBlock(primary, override)
207+
208+
blocks := primary.Body().Blocks()
209+
require.Len(t, blocks, 1)
210+
assert.Equal(t, "option", blocks[0].Type())
211+
assert.Empty(t, blocks[0].Labels())
212+
})
213+
214+
t.Run("MixedStaticDynamicSuppressionByDynamic", func(t *testing.T) {
215+
t.Parallel()
216+
primary := parseBlock(t, `resource "a" "b" {
217+
option {
218+
name = "static"
219+
}
220+
dynamic "option" {
221+
for_each = var.list
222+
content {
223+
name = option.value
224+
}
225+
}
226+
}`)
227+
override := parseBlock(t, `resource "a" "b" {
228+
dynamic "option" {
229+
for_each = var.other
230+
content {
231+
name = option.value
232+
}
233+
}
234+
}`)
235+
mergeBlock(primary, override)
236+
237+
blocks := primary.Body().Blocks()
238+
require.Len(t, blocks, 1)
239+
assert.Equal(t, "dynamic", blocks[0].Type())
240+
require.Len(t, blocks[0].Labels(), 1)
241+
assert.Equal(t, "option", blocks[0].Labels()[0])
242+
})
243+
244+
t.Run("StaticSuppressionByMixedOverride", func(t *testing.T) {
245+
t.Parallel()
246+
primary := parseBlock(t, `resource "a" "b" {
247+
option {
248+
name = "old"
249+
}
250+
}`)
251+
override := parseBlock(t, `resource "a" "b" {
252+
option {
253+
name = "static"
254+
}
255+
dynamic "option" {
256+
for_each = var.list
257+
content {
258+
name = option.value
259+
}
260+
}
261+
}`)
262+
mergeBlock(primary, override)
263+
264+
blocks := primary.Body().Blocks()
265+
require.Len(t, blocks, 2)
266+
assert.Equal(t, "option", blocks[0].Type())
267+
assert.Equal(t, "dynamic", blocks[1].Type())
268+
assert.Equal(t, "option", blocks[1].Labels()[0])
269+
})
270+
188271
t.Run("NoNestedBlocksInOverride", func(t *testing.T) {
189272
t.Parallel()
190273
primary := parseBlock(t, `resource "a" "b" {

preview_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,10 @@ func Test_Extract(t *testing.T) {
612612
name: "override",
613613
dir: "override",
614614
params: map[string]assertParam{
615-
"region": ap().value("ap").def("ap").optVals("ap"),
616-
"size": ap().value("100").def("100").optVals("10", "50", "100"),
615+
"region": ap().value("ap").def("ap").optVals("ap"),
616+
"size": ap().value("100").def("100").optVals("10", "50", "100"),
617+
"static_to_dynamic": ap().value("a").def("a").optVals("a", "b", "c"),
618+
"dynamic_to_static": ap().value("x").def("x").optVals("x", "y"),
617619
},
618620
presets: map[string]assertPreset{
619621
"dev-override": aPre().value("region", "ap"),
@@ -623,6 +625,7 @@ func Test_Extract(t *testing.T) {
623625
},
624626
variables: map[string]assertVariable{
625627
"string_to_number": av().def(cty.NumberIntVal(40)).typeEq(cty.Number),
628+
"zones": av().def(cty.SetVal([]cty.Value{cty.StringVal("a"), cty.StringVal("b"), cty.StringVal("c")})).typeEq(cty.Set(cty.String)),
626629
},
627630
},
628631
} {

testdata/override/a_override.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ data "coder_workspace_tags" "tags" {
3939
}
4040
}
4141

42+
# Override static options with dynamic.
43+
data "coder_parameter" "static_to_dynamic" {
44+
dynamic "option" {
45+
for_each = var.zones
46+
content {
47+
name = option.value
48+
value = option.value
49+
}
50+
}
51+
}
52+
53+
# Override dynamic options with static.
54+
data "coder_parameter" "dynamic_to_static" {
55+
option {
56+
name = "X"
57+
value = "x"
58+
}
59+
option {
60+
name = "Y"
61+
value = "y"
62+
}
63+
}
64+
4265
# Override variable.
4366
variable "string_to_number" {
4467
type = number

testdata/override/main.tf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,42 @@ data "coder_workspace_tags" "tags" {
5151
}
5252
}
5353

54+
variable "zones" {
55+
type = set(string)
56+
default = ["a", "b", "c"]
57+
}
58+
59+
# Static options, will be overridden by dynamic "option" in a_override.
60+
data "coder_parameter" "static_to_dynamic" {
61+
name = "static_to_dynamic"
62+
type = "string"
63+
default = "a"
64+
65+
option {
66+
name = "A"
67+
value = "a"
68+
}
69+
option {
70+
name = "B"
71+
value = "b"
72+
}
73+
}
74+
75+
# Dynamic options, will be overridden by static option blocks in a_override.
76+
data "coder_parameter" "dynamic_to_static" {
77+
name = "dynamic_to_static"
78+
type = "string"
79+
default = "x"
80+
81+
dynamic "option" {
82+
for_each = var.zones
83+
content {
84+
name = option.value
85+
value = option.value
86+
}
87+
}
88+
}
89+
5490
variable "string_to_number" {
5591
type = string
5692
default = "foo"

0 commit comments

Comments
 (0)