Skip to content

Commit 9e5d1f1

Browse files
committed
terraform: add test to verify orphan outputs in modules are removed
For hashicorp#7598 This doesn't work with the old graph, we guard it as such.
1 parent 61a1501 commit 9e5d1f1

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

terraform/context_apply_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/hashicorp/terraform/config/module"
15+
"github.com/hashicorp/terraform/helper/experiment"
1516
)
1617

1718
func TestContext2Apply_basic(t *testing.T) {
@@ -2113,6 +2114,58 @@ func TestContext2Apply_outputOrphan(t *testing.T) {
21132114
}
21142115
}
21152116

2117+
func TestContext2Apply_outputOrphanModule(t *testing.T) {
2118+
if !experiment.Enabled(experiment.X_newApply) {
2119+
t.SkipNow()
2120+
}
2121+
2122+
m := testModule(t, "apply-output-orphan-module")
2123+
p := testProvider("aws")
2124+
p.ApplyFn = testApplyFn
2125+
p.DiffFn = testDiffFn
2126+
2127+
state := &State{
2128+
Modules: []*ModuleState{
2129+
&ModuleState{
2130+
Path: []string{"root", "child"},
2131+
Outputs: map[string]*OutputState{
2132+
"foo": &OutputState{
2133+
Type: "string",
2134+
Value: "bar",
2135+
},
2136+
"bar": &OutputState{
2137+
Type: "string",
2138+
Value: "baz",
2139+
},
2140+
},
2141+
},
2142+
},
2143+
}
2144+
2145+
ctx := testContext2(t, &ContextOpts{
2146+
Module: m,
2147+
Providers: map[string]ResourceProviderFactory{
2148+
"aws": testProviderFuncFixed(p),
2149+
},
2150+
State: state,
2151+
})
2152+
2153+
if _, err := ctx.Plan(); err != nil {
2154+
t.Fatalf("err: %s", err)
2155+
}
2156+
2157+
state, err := ctx.Apply()
2158+
if err != nil {
2159+
t.Fatalf("err: %s", err)
2160+
}
2161+
2162+
actual := strings.TrimSpace(state.String())
2163+
expected := strings.TrimSpace(testTerraformApplyOutputOrphanModuleStr)
2164+
if actual != expected {
2165+
t.Fatalf("bad: \n%s", actual)
2166+
}
2167+
}
2168+
21162169
func TestContext2Apply_providerComputedVar(t *testing.T) {
21172170
m := testModule(t, "apply-provider-computed")
21182171
p := testProvider("aws")

terraform/terraform_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,14 @@ Outputs:
501501
foo = bar
502502
`
503503

504+
const testTerraformApplyOutputOrphanModuleStr = `
505+
module.child:
506+
<no state>
507+
Outputs:
508+
509+
foo = bar
510+
`
511+
504512
const testTerraformApplyProvisionerStr = `
505513
aws_instance.bar:
506514
ID = foo
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
output "foo" { value = "bar" }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module "child" {
2+
source = "./child"
3+
}

0 commit comments

Comments
 (0)