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

core: fix bug detecting deeply nested module orphans #5022

Merged
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
85 changes: 85 additions & 0 deletions terraform/context_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,91 @@ func TestContext2Plan_moduleOrphans(t *testing.T) {
}
}

// https://github.com/hashicorp/terraform/issues/3114
func TestContext2Plan_moduleOrphansWithProvisioner(t *testing.T) {
m := testModule(t, "plan-modules-remove-provisioners")
p := testProvider("aws")
pr := testProvisioner()
p.DiffFn = testDiffFn
s := &State{
Modules: []*ModuleState{
&ModuleState{
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.top": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "top",
},
},
},
},
&ModuleState{
Path: []string{"root", "parent", "childone"},
Resources: map[string]*ResourceState{
"aws_instance.foo": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "baz",
},
},
},
},
&ModuleState{
Path: []string{"root", "parent", "childtwo"},
Resources: map[string]*ResourceState{
"aws_instance.foo": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "baz",
},
},
},
},
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
Provisioners: map[string]ResourceProvisionerFactory{
"shell": testProvisionerFuncFixed(pr),
},
State: s,
})

plan, err := ctx.Plan()
if err != nil {
t.Fatalf("err: %s", err)
}

actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(`
DIFF:

module.parent.childone:
DESTROY: aws_instance.foo
module.parent.childtwo:
DESTROY: aws_instance.foo

STATE:

aws_instance.top:
ID = top

module.parent.childone:
aws_instance.foo:
ID = baz
module.parent.childtwo:
aws_instance.foo:
ID = baz
`)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
}

func TestContext2Plan_moduleProviderInherit(t *testing.T) {
var l sync.Mutex
var calls []string
Expand Down
17 changes: 16 additions & 1 deletion terraform/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,23 @@ func (s *State) ModuleOrphans(path []string, c *config.Config) [][]string {
continue
}

orphanPath := m.Path[:len(path)+1]

// Don't double-add if we've already added this orphan (which can happen if
// there are multiple nested sub-modules that get orphaned together).
alreadyAdded := false
for _, o := range orphans {
if reflect.DeepEqual(o, orphanPath) {
alreadyAdded = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not going to probably matter but you can break after this line safely.

break
}
}
if alreadyAdded {
continue
}

// Add this orphan
orphans = append(orphans, m.Path[:len(path)+1])
orphans = append(orphans, orphanPath)
}

return orphans
Expand Down
25 changes: 25 additions & 0 deletions terraform/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,31 @@ func TestStateModuleOrphans_nilConfig(t *testing.T) {
}
}

func TestStateModuleOrphans_deepNestedNilConfig(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
},
&ModuleState{
Path: []string{RootModuleName, "parent", "childfoo"},
},
&ModuleState{
Path: []string{RootModuleName, "parent", "childbar"},
},
},
}

actual := state.ModuleOrphans(RootModulePath, nil)
expected := [][]string{
[]string{RootModuleName, "parent"},
}

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}

func TestStateEqual(t *testing.T) {
cases := []struct {
Result bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "aws_instance" "top" {}

# module "test" {
# source = "./parent"
# }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resource "aws_instance" "foo" {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "childone" {
source = "./child"
}

module "childtwo" {
source = "./child"
}