From 465f3f2676e75e2575aa85397828c670149daf8e Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Tue, 23 Sep 2014 11:10:23 -0700 Subject: [PATCH] terraform: test create-before-destroy with failed destroy --- terraform/context_test.go | 58 +++++++++++++++++++++++++++++++++++++ terraform/terraform_test.go | 6 ++++ 2 files changed, 64 insertions(+) diff --git a/terraform/context_test.go b/terraform/context_test.go index 1aa682395..f6de07a6a 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -984,6 +984,64 @@ func TestContextApply_error_createBeforeDestroy(t *testing.T) { } } +func TestContextApply_errorDestroy_createBeforeDestroy(t *testing.T) { + c := testConfig(t, "apply-error-create-before") + p := testProvider("aws") + 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, + }) + p.ApplyFn = func(info *InstanceInfo, is *InstanceState, id *InstanceDiff) (*InstanceState, error) { + // Fail the destroy! + if id.Destroy { + return is, fmt.Errorf("error") + } + + // Create should work + is = &InstanceState{ + ID: "foo", + } + return is, nil + } + p.DiffFn = testDiffFn + + if _, err := ctx.Plan(nil); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err == nil { + t.Fatal("should have error") + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyErrorDestroyCreateBeforeDestroyStr) + if actual != expected { + t.Fatalf("bad: actual:\n%s\n\nexpected:\n%s", actual, expected) + } +} + func TestContextApply_provisionerResourceRef(t *testing.T) { m := testModule(t, "apply-provisioner-resource-ref") p := testProvider("aws") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index abc103393..f3cebdcc6 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -260,6 +260,12 @@ aws_instance.bar: require_new = abc ` +const testTerraformApplyErrorDestroyCreateBeforeDestroyStr = ` +aws_instance.bar: (1 tainted) + ID = foo + Tainted ID 1 = bar +` + const testTerraformApplyErrorPartialStr = ` aws_instance.bar: ID = bar