From 5207e1d268deb773c64e9195344a3e40a21083df Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 29 Sep 2014 17:00:45 -0700 Subject: [PATCH] terraform: test ordering when using create before with depedencies --- terraform/context_test.go | 85 +++++++++++++++++++ terraform/terraform_test.go | 14 +++ .../apply-depends-create-before/main.tf | 11 +++ 3 files changed, 110 insertions(+) create mode 100644 terraform/test-fixtures/apply-depends-create-before/main.tf diff --git a/terraform/context_test.go b/terraform/context_test.go index c92e30b40b..4270b7f578 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -1912,6 +1912,91 @@ func TestContextApply_vars(t *testing.T) { } } +func TestContextApply_createBefore_depends(t *testing.T) { + m := testModule(t, "apply-depends-create-before") + h := new(HookRecordApplyOrder) + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + state := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "aws_instance.web": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "require_new": "ami-old", + }, + }, + }, + "aws_instance.lb": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "baz", + Attributes: map[string]string{ + "instance": "bar", + }, + }, + }, + }, + }, + }, + } + ctx := testContext(t, &ContextOpts{ + Module: m, + Hooks: []Hook{h}, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: state, + }) + + if _, err := ctx.Plan(nil); err != nil { + t.Fatalf("err: %s", err) + } + + h.Active = true + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + mod := state.RootModule() + if len(mod.Resources) < 2 { + t.Fatalf("bad: %#v", mod.Resources) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyDependsCreateBeforeStr) + if actual != expected { + t.Fatalf("bad: \n%s\n%s", actual, expected) + } + + // Test that things were managed _in the right order_ + order := h.States + diffs := h.Diffs + if order[0].ID != "bar" || diffs[0].Destroy { + t.Fatalf("should create new instance first: %#v", order) + } + + // Order of the next two is not deterministric, just + // depends on the scheduling. + for i := 1; i <= 2; i++ { + switch order[i].ID { + case "bar": + if !diffs[1].Destroy { + t.Fatalf("missing destroy") + } + case "baz": + default: + t.Fatalf("bad resource") + } + } +} + func TestContextPlan(t *testing.T) { m := testModule(t, "plan-good") p := testProvider("aws") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 3df7013e55..cdcb6ea205 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -150,6 +150,20 @@ aws_instance.foo: type = aws_instance ` +const testTerraformApplyDependsCreateBeforeStr = ` +aws_instance.lb: + ID = foo + instance = foo + type = aws_instance + + Dependencies: + aws_instance.web +aws_instance.web: + ID = foo + require_new = ami-new + type = aws_instance +` + const testTerraformApplyCreateBeforeStr = ` aws_instance.bar: ID = foo diff --git a/terraform/test-fixtures/apply-depends-create-before/main.tf b/terraform/test-fixtures/apply-depends-create-before/main.tf new file mode 100644 index 0000000000..b4666f5bc4 --- /dev/null +++ b/terraform/test-fixtures/apply-depends-create-before/main.tf @@ -0,0 +1,11 @@ + +resource "aws_instance" "web" { + require_new = "ami-new" + lifecycle { + create_before_destroy = true + } +} + +resource "aws_instance" "lb" { + instance = "${aws_instance.web.id}" +}