From 7aeaec9b48e4e84f898de28980ea7ffb1e0dcacb Mon Sep 17 00:00:00 2001 From: Barrett Clark Date: Wed, 17 Nov 2021 16:00:47 -0600 Subject: [PATCH] Update tests for new prompts * Remove double question to migrate * Remove parallelism (it has been flaky) * Add subtests to apply_auto_approve_test (it was overlooked before) --- internal/cloud/e2e/apply_auto_approve_test.go | 110 +++++++++--------- .../e2e/backend_apply_before_init_test.go | 5 +- .../cloud/e2e/init_with_empty_tags_test.go | 4 +- .../e2e/migrate_state_multi_to_tfc_test.go | 22 ++-- .../e2e/migrate_state_single_to_tfc_test.go | 2 +- .../e2e/migrate_state_tfc_to_other_test.go | 2 +- 6 files changed, 72 insertions(+), 73 deletions(-) diff --git a/internal/cloud/e2e/apply_auto_approve_test.go b/internal/cloud/e2e/apply_auto_approve_test.go index 2a0e23682..c31a1ad2f 100644 --- a/internal/cloud/e2e/apply_auto_approve_test.go +++ b/internal/cloud/e2e/apply_auto_approve_test.go @@ -179,72 +179,76 @@ func Test_terraform_apply_autoApprove(t *testing.T) { }, }, } - for _, tc := range cases { - organization, cleanup := createOrganization(t) - defer cleanup() - exp, err := expect.NewConsole(defaultOpts()...) - if err != nil { - t.Fatal(err) - } - defer exp.Close() + for name, tc := range cases { + tc := tc + t.Run(name, func(t *testing.T) { + // t.Parallel() + organization, cleanup := createOrganization(t) + defer cleanup() + exp, err := expect.NewConsole(defaultOpts()...) + if err != nil { + t.Fatal(err) + } + defer exp.Close() - tmpDir, err := ioutil.TempDir("", "terraform-test") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmpDir) + tmpDir, err := ioutil.TempDir("", "terraform-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) - tf := e2e.NewBinary(terraformBin, tmpDir) - tf.AddEnv(cliConfigFileEnv) - defer tf.Close() + tf := e2e.NewBinary(terraformBin, tmpDir) + tf.AddEnv(cliConfigFileEnv) + defer tf.Close() - for _, op := range tc.operations { - op.prep(t, organization.Name, tf.WorkDir()) - for _, tfCmd := range op.commands { - cmd := tf.Cmd(tfCmd.command...) - cmd.Stdin = exp.Tty() - cmd.Stdout = exp.Tty() - cmd.Stderr = exp.Tty() + for _, op := range tc.operations { + op.prep(t, organization.Name, tf.WorkDir()) + for _, tfCmd := range op.commands { + cmd := tf.Cmd(tfCmd.command...) + cmd.Stdin = exp.Tty() + cmd.Stdout = exp.Tty() + cmd.Stderr = exp.Tty() - err = cmd.Start() - if err != nil { - t.Fatal(err) - } - - if tfCmd.expectedCmdOutput != "" { - got, err := exp.ExpectString(tfCmd.expectedCmdOutput) + err = cmd.Start() if err != nil { - t.Fatalf("error while waiting for output\nwant: %s\nerror: %s\noutput\n%s", tfCmd.expectedCmdOutput, err, got) + t.Fatal(err) } - } - lenInput := len(tfCmd.userInput) - lenInputOutput := len(tfCmd.postInputOutput) - if lenInput > 0 { - for i := 0; i < lenInput; i++ { - input := tfCmd.userInput[i] - exp.SendLine(input) - // use the index to find the corresponding - // output that matches the input. - if lenInputOutput-1 >= i { - output := tfCmd.postInputOutput[i] - _, err := exp.ExpectString(output) - if err != nil { - t.Fatal(err) + if tfCmd.expectedCmdOutput != "" { + got, err := exp.ExpectString(tfCmd.expectedCmdOutput) + if err != nil { + t.Fatalf("error while waiting for output\nwant: %s\nerror: %s\noutput\n%s", tfCmd.expectedCmdOutput, err, got) + } + } + + lenInput := len(tfCmd.userInput) + lenInputOutput := len(tfCmd.postInputOutput) + if lenInput > 0 { + for i := 0; i < lenInput; i++ { + input := tfCmd.userInput[i] + exp.SendLine(input) + // use the index to find the corresponding + // output that matches the input. + if lenInputOutput-1 >= i { + output := tfCmd.postInputOutput[i] + _, err := exp.ExpectString(output) + if err != nil { + t.Fatal(err) + } } } } - } - err = cmd.Wait() - if err != nil { - t.Fatal(err) + err = cmd.Wait() + if err != nil && !tfCmd.expectError { + t.Fatal(err) + } } } - } - if tc.validations != nil { - tc.validations(t, organization.Name) - } + if tc.validations != nil { + tc.validations(t, organization.Name) + } + }) } } diff --git a/internal/cloud/e2e/backend_apply_before_init_test.go b/internal/cloud/e2e/backend_apply_before_init_test.go index d89852649..9b4ff5508 100644 --- a/internal/cloud/e2e/backend_apply_before_init_test.go +++ b/internal/cloud/e2e/backend_apply_before_init_test.go @@ -11,7 +11,7 @@ import ( func Test_backend_apply_before_init(t *testing.T) { skipIfMissingEnvVar(t) - t.Parallel() + // t.Parallel() skipWithoutRemoteTerraformVersion(t) cases := map[string]struct { @@ -74,7 +74,7 @@ func Test_backend_apply_before_init(t *testing.T) { for name, tc := range cases { tc := tc t.Run(name, func(t *testing.T) { - t.Parallel() + // t.Parallel() organization, cleanup := createOrganization(t) defer cleanup() exp, err := expect.NewConsole(defaultOpts()...) @@ -130,6 +130,7 @@ func Test_backend_apply_before_init(t *testing.T) { } } } + err = cmd.Wait() if err != nil && !tfCmd.expectError { t.Fatal(err) diff --git a/internal/cloud/e2e/init_with_empty_tags_test.go b/internal/cloud/e2e/init_with_empty_tags_test.go index 6c03b1052..c608e0973 100644 --- a/internal/cloud/e2e/init_with_empty_tags_test.go +++ b/internal/cloud/e2e/init_with_empty_tags_test.go @@ -11,7 +11,7 @@ import ( func Test_init_with_empty_tags(t *testing.T) { skipIfMissingEnvVar(t) - t.Parallel() + // t.Parallel() skipWithoutRemoteTerraformVersion(t) cases := map[string]struct { @@ -41,7 +41,7 @@ func Test_init_with_empty_tags(t *testing.T) { for name, tc := range cases { tc := tc t.Run(name, func(t *testing.T) { - t.Parallel() + // t.Parallel() organization, cleanup := createOrganization(t) defer cleanup() exp, err := expect.NewConsole(defaultOpts()...) diff --git a/internal/cloud/e2e/migrate_state_multi_to_tfc_test.go b/internal/cloud/e2e/migrate_state_multi_to_tfc_test.go index f06c15974..74b750f2c 100644 --- a/internal/cloud/e2e/migrate_state_multi_to_tfc_test.go +++ b/internal/cloud/e2e/migrate_state_multi_to_tfc_test.go @@ -62,10 +62,8 @@ func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) { { command: []string{"init"}, expectedCmdOutput: `Do you want to copy only your current workspace?`, - userInput: []string{"yes", "yes"}, - postInputOutput: []string{ - `Do you want to copy existing state to Terraform Cloud?`, - `Terraform Cloud has been successfully initialized!`}, + userInput: []string{"yes"}, + postInputOutput: []string{`Terraform Cloud has been successfully initialized!`}, }, { command: []string{"workspace", "show"}, @@ -129,10 +127,8 @@ func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) { { command: []string{"init"}, expectedCmdOutput: `Do you want to copy only your current workspace?`, - userInput: []string{"yes", "yes"}, - postInputOutput: []string{ - `Do you want to copy existing state to Terraform Cloud?`, - `Terraform Cloud has been successfully initialized!`}, + userInput: []string{"yes"}, + postInputOutput: []string{`Terraform Cloud has been successfully initialized!`}, }, { command: []string{"workspace", "list"}, @@ -197,10 +193,8 @@ func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) { { command: []string{"init"}, expectedCmdOutput: `Do you want to copy only your current workspace?`, - userInput: []string{"yes", "yes"}, - postInputOutput: []string{ - `Do you want to copy existing state to Terraform Cloud?`, - `Terraform Cloud has been successfully initialized!`}, + userInput: []string{"yes"}, + postInputOutput: []string{`Terraform Cloud has been successfully initialized!`}, }, { command: []string{"workspace", "select", "default"}, @@ -233,7 +227,7 @@ func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) { for name, tc := range cases { tc := tc t.Run(name, func(t *testing.T) { - t.Parallel() + // t.Parallel() organization, cleanup := createOrganization(t) defer cleanup() exp, err := expect.NewConsole(defaultOpts()...) @@ -521,7 +515,7 @@ func Test_migrate_multi_to_tfc_cloud_tags_strategy(t *testing.T) { for name, tc := range cases { tc := tc t.Run(name, func(t *testing.T) { - t.Parallel() + // t.Parallel() organization, cleanup := createOrganization(t) defer cleanup() exp, err := expect.NewConsole(defaultOpts()...) diff --git a/internal/cloud/e2e/migrate_state_single_to_tfc_test.go b/internal/cloud/e2e/migrate_state_single_to_tfc_test.go index 4865a780d..064256aac 100644 --- a/internal/cloud/e2e/migrate_state_single_to_tfc_test.go +++ b/internal/cloud/e2e/migrate_state_single_to_tfc_test.go @@ -131,7 +131,7 @@ func Test_migrate_single_to_tfc(t *testing.T) { for name, tc := range cases { tc := tc t.Run(name, func(t *testing.T) { - t.Parallel() + // t.Parallel() organization, cleanup := createOrganization(t) defer cleanup() exp, err := expect.NewConsole(defaultOpts()...) diff --git a/internal/cloud/e2e/migrate_state_tfc_to_other_test.go b/internal/cloud/e2e/migrate_state_tfc_to_other_test.go index 9de96ab01..4840a0f85 100644 --- a/internal/cloud/e2e/migrate_state_tfc_to_other_test.go +++ b/internal/cloud/e2e/migrate_state_tfc_to_other_test.go @@ -49,7 +49,7 @@ func Test_migrate_tfc_to_other(t *testing.T) { for name, tc := range cases { tc := tc t.Run(name, func(t *testing.T) { - t.Parallel() + // t.Parallel() organization, cleanup := createOrganization(t) defer cleanup() exp, err := expect.NewConsole(defaultOpts()...)