Skip to content

Commit eb20db9

Browse files
committed
terraform: test that data sources can reference other data sources
1 parent 69ce353 commit eb20db9

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎terraform/context_refresh_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,43 @@ func TestContext2Refresh_dataState(t *testing.T) {
640640
}
641641
}
642642

643+
func TestContext2Refresh_dataStateRefData(t *testing.T) {
644+
p := testProvider("null")
645+
m := testModule(t, "refresh-data-ref-data")
646+
state := &State{
647+
Modules: []*ModuleState{
648+
&ModuleState{
649+
Path: rootModulePath,
650+
// Intentionally no resources since data resources are
651+
// supposed to refresh themselves even if they didn't
652+
// already exist.
653+
Resources: map[string]*ResourceState{},
654+
},
655+
},
656+
}
657+
ctx := testContext2(t, &ContextOpts{
658+
Module: m,
659+
Providers: map[string]ResourceProviderFactory{
660+
"null": testProviderFuncFixed(p),
661+
},
662+
State: state,
663+
})
664+
665+
p.ReadDataDiffFn = testDataDiffFn
666+
p.ReadDataApplyFn = testDataApplyFn
667+
668+
s, err := ctx.Refresh()
669+
if err != nil {
670+
t.Fatalf("err: %s", err)
671+
}
672+
673+
actual := strings.TrimSpace(s.String())
674+
expected := strings.TrimSpace(testTerraformRefreshDataRefDataStr)
675+
if actual != expected {
676+
t.Fatalf("bad:\n\n%s\n\n%s", actual, expected)
677+
}
678+
}
679+
643680
func TestContext2Refresh_tainted(t *testing.T) {
644681
p := testProvider("aws")
645682
m := testModule(t, "refresh-basic")

‎terraform/context_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ func testContext2(t *testing.T, opts *ContextOpts) *Context {
7979
return ctx
8080
}
8181

82+
func testDataApplyFn(
83+
info *InstanceInfo,
84+
d *InstanceDiff) (*InstanceState, error) {
85+
return testApplyFn(info, new(InstanceState), d)
86+
}
87+
88+
func testDataDiffFn(
89+
info *InstanceInfo,
90+
c *ResourceConfig) (*InstanceDiff, error) {
91+
return testDiffFn(info, new(InstanceState), c)
92+
}
93+
8294
func testApplyFn(
8395
info *InstanceInfo,
8496
s *InstanceState,

‎terraform/terraform_test.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,3 +1492,17 @@ hcl_instance.hcltest:
14921492
foo.1 = b
14931493
type = hcl_instance
14941494
`
1495+
1496+
const testTerraformRefreshDataRefDataStr = `
1497+
data.null_data_source.bar:
1498+
ID = foo
1499+
bar = yes
1500+
type = null_data_source
1501+
1502+
Dependencies:
1503+
data.null_data_source.foo
1504+
data.null_data_source.foo:
1505+
ID = foo
1506+
foo = yes
1507+
type = null_data_source
1508+
`
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
data "null_data_source" "foo" {
2+
foo = "yes"
3+
}
4+
5+
data "null_data_source" "bar" {
6+
bar = "${data.null_data_source.foo.foo}"
7+
}

0 commit comments

Comments
 (0)