From 3f741346088846f6594135b65b39d885abfc91d7 Mon Sep 17 00:00:00 2001 From: Akihiro Uchida Date: Tue, 5 Jul 2022 18:16:03 +0900 Subject: [PATCH] revert SetupTestAccWithApply and directly write setup Use ... to keep testcase codes --- tfexec/terraform_test.go | 36 ++++++++++++++++++++++++-- tfexec/test_helper.go | 6 ++--- tfmigrate/multi_state_migrator_test.go | 4 +-- tfmigrate/state_import_action_test.go | 2 +- tfmigrate/state_migrator_test.go | 4 +-- tfmigrate/state_mv_action_test.go | 2 +- tfmigrate/state_rm_action_test.go | 2 +- 7 files changed, 43 insertions(+), 13 deletions(-) diff --git a/tfexec/terraform_test.go b/tfexec/terraform_test.go index 55d3a4e..e585371 100644 --- a/tfexec/terraform_test.go +++ b/tfexec/terraform_test.go @@ -100,7 +100,7 @@ resource "aws_security_group" "foo" {} resource "aws_security_group" "bar" {} ` workspace := "work1" - terraformCLI := SetupTestAccWithApply(t, workspace, backend+source, nil) + terraformCLI := SetupTestAccWithApply(t, workspace, backend+source) updatedSource := ` resource "aws_security_group" "foo2" {} @@ -216,7 +216,39 @@ resource "aws_security_group" "bar" {} ` workspace := "work1" backendConfig := []string{"bucket=tfstate-test"} - terraformCLI := SetupTestAccWithApply(t, workspace, backend+source, backendConfig) + e := SetupTestAcc(t, source) + terraformCLI := NewTerraformCLI(e) + ctx := context.Background() + + var args = []string{"-input=false", "-no-color"} + for _, b := range backendConfig { + args = append(args, fmt.Sprintf("-backend-config=%s", b)) + } + err := terraformCLI.Init(ctx, args...) + if err != nil { + t.Fatalf("failed to run terraform init: %s", err) + } + + //default workspace always exists so don't try to create it + if workspace != "default" { + err = terraformCLI.WorkspaceNew(ctx, workspace) + if err != nil { + t.Fatalf("failed to run terraform workspace new %s : %s", workspace, err) + } + } + + err = terraformCLI.Apply(ctx, nil, "-input=false", "-no-color", "-auto-approve") + if err != nil { + t.Fatalf("failed to run terraform apply: %s", err) + } + + // destroy resources after each test not to have any state. + t.Cleanup(func() { + err := terraformCLI.Destroy(ctx, "-input=false", "-no-color", "-auto-approve") + if err != nil { + t.Fatalf("failed to run terraform destroy: %s", err) + } + }) updatedSource := ` resource "aws_security_group" "foo2" {} diff --git a/tfexec/test_helper.go b/tfexec/test_helper.go index 451830f..895a72c 100644 --- a/tfexec/test_helper.go +++ b/tfexec/test_helper.go @@ -283,16 +283,14 @@ provider "aws" { // SetupTestAccWithApply is an acceptance test helper for initializing a // temporary work directory and applying a given source. -func SetupTestAccWithApply(t *testing.T, workspace string, source string, backendConfig []string) TerraformCLI { +func SetupTestAccWithApply(t *testing.T, workspace string, source string) TerraformCLI { t.Helper() e := SetupTestAcc(t, source) tf := NewTerraformCLI(e) ctx := context.Background() - var args = []string{"-input=false", "-no-color"} - args = append(args, backendConfig...) - err := tf.Init(ctx, args...) + err := tf.Init(ctx, "-input=false", "-no-color") if err != nil { t.Fatalf("failed to run terraform init: %s", err) } diff --git a/tfmigrate/multi_state_migrator_test.go b/tfmigrate/multi_state_migrator_test.go index 9e8fd58..74eff5e 100644 --- a/tfmigrate/multi_state_migrator_test.go +++ b/tfmigrate/multi_state_migrator_test.go @@ -243,9 +243,9 @@ func TestAccMultiStateMigratorApply(t *testing.T) { //setup the initial files and states fromBackend := tfexec.GetTestAccBackendS3Config(t.Name() + "/fromDir") - fromTf := tfexec.SetupTestAccWithApply(t, tc.fromWorkspace, fromBackend+tc.fromSource, nil) + fromTf := tfexec.SetupTestAccWithApply(t, tc.fromWorkspace, fromBackend+tc.fromSource) toBackend := tfexec.GetTestAccBackendS3Config(t.Name() + "/toDir") - toTf := tfexec.SetupTestAccWithApply(t, tc.toWorkspace, toBackend+tc.toSource, nil) + toTf := tfexec.SetupTestAccWithApply(t, tc.toWorkspace, toBackend+tc.toSource) //update terraform resource files for migration tfexec.UpdateTestAccSource(t, fromTf, fromBackend+tc.fromUpdatedSource) diff --git a/tfmigrate/state_import_action_test.go b/tfmigrate/state_import_action_test.go index 625269c..c2918fd 100644 --- a/tfmigrate/state_import_action_test.go +++ b/tfmigrate/state_import_action_test.go @@ -23,7 +23,7 @@ resource "aws_iam_user" "baz" { name = "baz" } ` - tf := tfexec.SetupTestAccWithApply(t, "default", backend+source, nil) + tf := tfexec.SetupTestAccWithApply(t, "default", backend+source) ctx := context.Background() _, err := tf.StateRm(ctx, nil, []string{"aws_iam_user.foo", "aws_iam_user.baz"}) diff --git a/tfmigrate/state_migrator_test.go b/tfmigrate/state_migrator_test.go index 8029bb0..39bd499 100644 --- a/tfmigrate/state_migrator_test.go +++ b/tfmigrate/state_migrator_test.go @@ -149,7 +149,7 @@ resource "aws_iam_user" "qux" { name = "qux" } ` - tf := tfexec.SetupTestAccWithApply(t, tc.workspace, backend+source, nil) + tf := tfexec.SetupTestAccWithApply(t, tc.workspace, backend+source) ctx := context.Background() updatedSource := ` @@ -228,7 +228,7 @@ func TestAccStateMigratorApplyForce(t *testing.T) { resource "aws_security_group" "foo" {} resource "aws_security_group" "bar" {} ` - tf := tfexec.SetupTestAccWithApply(t, "default", backend+source, nil) + tf := tfexec.SetupTestAccWithApply(t, "default", backend+source) ctx := context.Background() updatedSource := ` diff --git a/tfmigrate/state_mv_action_test.go b/tfmigrate/state_mv_action_test.go index 7124670..a7eb638 100644 --- a/tfmigrate/state_mv_action_test.go +++ b/tfmigrate/state_mv_action_test.go @@ -17,7 +17,7 @@ resource "aws_security_group" "foo" {} resource "aws_security_group" "bar" {} resource "aws_security_group" "baz" {} ` - tf := tfexec.SetupTestAccWithApply(t, "default", backend+source, nil) + tf := tfexec.SetupTestAccWithApply(t, "default", backend+source) ctx := context.Background() updatedSource := ` diff --git a/tfmigrate/state_rm_action_test.go b/tfmigrate/state_rm_action_test.go index a434050..6832609 100644 --- a/tfmigrate/state_rm_action_test.go +++ b/tfmigrate/state_rm_action_test.go @@ -18,7 +18,7 @@ resource "aws_security_group" "bar" {} resource "aws_security_group" "baz" {} resource "aws_security_group" "qux" {} ` - tf := tfexec.SetupTestAccWithApply(t, "default", backend+source, nil) + tf := tfexec.SetupTestAccWithApply(t, "default", backend+source) ctx := context.Background() updatedSource := `