diff --git a/terraform/context_test.go b/terraform/context_test.go index 5ee9a8c71..9d21c21bc 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -4693,6 +4693,132 @@ func TestContext2Apply_taint(t *testing.T) { } } +func TestContext2Apply_taintDep(t *testing.T) { + m := testModule(t, "apply-taint-dep") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + s := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "aws_instance.foo": &ResourceState{ + Type: "aws_instance", + Tainted: []*InstanceState{ + &InstanceState{ + ID: "baz", + Attributes: map[string]string{ + "num": "2", + "type": "aws_instance", + }, + }, + }, + }, + "aws_instance.bar": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "foo": "baz", + "num": "2", + "type": "aws_instance", + }, + }, + }, + }, + }, + }, + } + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + if p, err := ctx.Plan(nil); err != nil { + t.Fatalf("err: %s", err) + } else { + t.Logf("plan: %s", p) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyTaintDepStr) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + +func TestContext2Apply_taintDepRequiresNew(t *testing.T) { + m := testModule(t, "apply-taint-dep-requires-new") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + s := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "aws_instance.foo": &ResourceState{ + Type: "aws_instance", + Tainted: []*InstanceState{ + &InstanceState{ + ID: "baz", + Attributes: map[string]string{ + "num": "2", + "type": "aws_instance", + }, + }, + }, + }, + "aws_instance.bar": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "foo": "baz", + "num": "2", + "type": "aws_instance", + }, + }, + }, + }, + }, + }, + } + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: s, + }) + + if p, err := ctx.Plan(nil); err != nil { + t.Fatalf("err: %s", err) + } else { + t.Logf("plan: %s", p) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyTaintDepRequireNewStr) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + func TestContext2Apply_unknownAttribute(t *testing.T) { m := testModule(t, "apply-unknown") p := testProvider("aws") @@ -4945,7 +5071,13 @@ func testApplyFn( } result := &InstanceState{ - ID: id, + ID: id, + Attributes: make(map[string]string), + } + + // Copy all the prior attributes + for k, v := range s.Attributes { + result.Attributes[k] = v } if d != nil { diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 4372ff77b..d063555bf 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -433,6 +433,36 @@ aws_instance.bar: type = aws_instance ` +const testTerraformApplyTaintDepStr = ` +aws_instance.bar: + ID = bar + foo = foo + num = 2 + type = aws_instance + + Dependencies: + aws_instance.foo +aws_instance.foo: + ID = foo + num = 2 + type = aws_instance +` + +const testTerraformApplyTaintDepRequireNewStr = ` +aws_instance.bar: + ID = foo + foo = foo + require_new = yes + type = aws_instance + + Dependencies: + aws_instance.foo +aws_instance.foo: + ID = foo + num = 2 + type = aws_instance +` + const testTerraformApplyOutputStr = ` aws_instance.bar: ID = foo diff --git a/terraform/test-fixtures/apply-taint-dep-requires-new/main.tf b/terraform/test-fixtures/apply-taint-dep-requires-new/main.tf new file mode 100644 index 000000000..1295a1bca --- /dev/null +++ b/terraform/test-fixtures/apply-taint-dep-requires-new/main.tf @@ -0,0 +1,10 @@ +resource "aws_instance" "foo" { + id = "foo" + num = "2" +} + +resource "aws_instance" "bar" { + id = "bar" + foo = "${aws_instance.foo.id}" + require_new = "yes" +} diff --git a/terraform/test-fixtures/apply-taint-dep/main.tf b/terraform/test-fixtures/apply-taint-dep/main.tf new file mode 100644 index 000000000..1191781a9 --- /dev/null +++ b/terraform/test-fixtures/apply-taint-dep/main.tf @@ -0,0 +1,10 @@ +resource "aws_instance" "foo" { + id = "foo" + num = "2" +} + +resource "aws_instance" "bar" { + id = "bar" + num = "2" + foo = "${aws_instance.foo.id}" +}