terraform: record dependency to self (other index)

Fixes #10313

The new graph wasn't properly recording resource dependencies to a
specific index of itself. For example: `foo.bar.2` depending on
`foo.bar.0` wasn't shown in the state when it should've been.

This adds a test to verify this and fixes it.
This commit is contained in:
Mitchell Hashimoto 2016-11-23 09:25:20 -08:00
parent 546caf9909
commit f50c7acf95
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 94 additions and 1 deletions

View File

@ -3611,6 +3611,66 @@ func TestContext2Apply_provisionerMultiSelfRef(t *testing.T) {
}
}
func TestContext2Apply_provisionerMultiSelfRefSingle(t *testing.T) {
var lock sync.Mutex
order := make([]string, 0, 5)
m := testModule(t, "apply-provisioner-multi-self-ref-single")
p := testProvider("aws")
pr := testProvisioner()
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
pr.ApplyFn = func(rs *InstanceState, c *ResourceConfig) error {
lock.Lock()
defer lock.Unlock()
val, ok := c.Config["order"]
if !ok {
t.Fatalf("bad value for order: %v %#v", val, c)
}
order = append(order, val.(string))
return nil
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
Provisioners: map[string]ResourceProvisionerFactory{
"shell": testProvisionerFuncFixed(pr),
},
})
if _, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testTerraformApplyProvisionerMultiSelfRefSingleStr)
if actual != expected {
t.Fatalf("bad: \n%s", actual)
}
// Verify apply was invoked
if !pr.ApplyCalled {
t.Fatalf("provisioner not invoked")
}
// Verify our result
sort.Strings(order)
expectedOrder := []string{"0", "1", "2"}
if !reflect.DeepEqual(order, expectedOrder) {
t.Fatalf("bad: %#v", order)
}
}
func TestContext2Apply_provisionerMultiSelfRefCount(t *testing.T) {
var lock sync.Mutex
commands := make([]string, 0, 5)

View File

@ -47,7 +47,10 @@ func (n *NodeApplyableResource) EvalTree() EvalNode {
// code for this that we've used for a long time.
var stateDeps []string
{
oldN := &graphNodeExpandedResource{Resource: n.Config}
oldN := &graphNodeExpandedResource{
Resource: n.Config,
Index: addr.Index,
}
stateDeps = oldN.StateDependencies()
}

View File

@ -603,6 +603,27 @@ aws_instance.foo.2:
type = aws_instance
`
const testTerraformApplyProvisionerMultiSelfRefSingleStr = `
aws_instance.foo.0:
ID = foo
foo = number 0
type = aws_instance
aws_instance.foo.1:
ID = foo
foo = number 1
type = aws_instance
Dependencies:
aws_instance.foo.0
aws_instance.foo.2:
ID = foo
foo = number 2
type = aws_instance
Dependencies:
aws_instance.foo.0
`
const testTerraformApplyProvisionerDiffStr = `
aws_instance.bar:
ID = foo

View File

@ -0,0 +1,9 @@
resource "aws_instance" "foo" {
count = 3
foo = "number ${count.index}"
provisioner "shell" {
command = "${aws_instance.foo.0.foo}"
order = "${count.index}"
}
}