From bce9b664d8a499708592b15b4711212435e7ad2f Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Tue, 23 Sep 2014 11:13:50 -0700 Subject: [PATCH] terraform: test happy path create-before-destroy --- terraform/context_test.go | 52 +++++++++++++++++++ terraform/terraform_test.go | 7 +++ .../apply-good-create-before/main.tf | 6 +++ 3 files changed, 65 insertions(+) create mode 100644 terraform/test-fixtures/apply-good-create-before/main.tf diff --git a/terraform/context_test.go b/terraform/context_test.go index f6de07a6a..7129bde58 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -582,6 +582,58 @@ func TestContextApply(t *testing.T) { } } +func TestContextApply_createBeforeDestroy(t *testing.T) { + c := testConfig(t, "apply-good-create-before") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + state := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "aws_instance.bar": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "require_new": "abc", + }, + }, + }, + }, + }, + }, + } + ctx := testContext(t, &ContextOpts{ + Config: c, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: state, + }) + + if _, err := ctx.Plan(nil); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + mod := state.RootModule() + if len(mod.Resources) != 1 { + t.Fatalf("bad: %#v", mod.Resources) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyCreateBeforeStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + func TestContextApply_Minimal(t *testing.T) { m := testModule(t, "apply-minimal") p := testProvider("aws") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index f3cebdcc6..3df7013e5 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -150,6 +150,13 @@ aws_instance.foo: type = aws_instance ` +const testTerraformApplyCreateBeforeStr = ` +aws_instance.bar: + ID = foo + require_new = xyz + type = aws_instance +` + const testTerraformApplyCancelStr = ` aws_instance.foo: ID = foo diff --git a/terraform/test-fixtures/apply-good-create-before/main.tf b/terraform/test-fixtures/apply-good-create-before/main.tf new file mode 100644 index 000000000..c7c2776eb --- /dev/null +++ b/terraform/test-fixtures/apply-good-create-before/main.tf @@ -0,0 +1,6 @@ +resource "aws_instance" "bar" { + require_new = "xyz" + lifecycle { + create_before_destroy = true + } +}