terraform: fix crash if depends_on used when state has orphans

This commit is contained in:
Mitchell Hashimoto 2014-10-11 13:20:11 -07:00
parent 1510f12efc
commit 129e4fc453
2 changed files with 48 additions and 0 deletions

View File

@ -744,6 +744,11 @@ func graphAddExplicitDeps(g *depgraph.Graph) {
if !ok { if !ok {
continue continue
} }
if rn.Config == nil {
// Orphan. It can't be depended on or have depends (explicit)
// anyways.
continue
}
rs[rn.Resource.Id] = n rs[rn.Resource.Id] = n
if rn.Config != nil && len(rn.Config.DependsOn) > 0 { if rn.Config != nil && len(rn.Config.DependsOn) > 0 {

View File

@ -97,6 +97,37 @@ func TestGraph_dependsOnCount(t *testing.T) {
} }
} }
func TestGraph_dependsOnWithOrphan(t *testing.T) {
m := testModule(t, "graph-depends-on")
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: []string{"root"},
Resources: map[string]*ResourceState{
"aws_instance.old": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
},
},
},
},
}
g, err := Graph(&GraphOpts{Module: m, State: state})
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTerraformGraphDependsOrphanStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
func TestGraph_modules(t *testing.T) { func TestGraph_modules(t *testing.T) {
m := testModule(t, "graph-modules") m := testModule(t, "graph-modules")
@ -1012,6 +1043,18 @@ root
root -> aws_instance.web root -> aws_instance.web
` `
const testTerraformGraphDependsOrphanStr = `
root: root
aws_instance.db
aws_instance.db -> aws_instance.web
aws_instance.old
aws_instance.web
root
root -> aws_instance.db
root -> aws_instance.old
root -> aws_instance.web
`
const testTerraformGraphDiffStr = ` const testTerraformGraphDiffStr = `
root: root root: root
aws_instance.foo aws_instance.foo