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)
This commit is contained in:
Barrett Clark 2021-11-17 16:00:47 -06:00 committed by Martin Atkins
parent 7d6d31eff8
commit 7aeaec9b48
6 changed files with 72 additions and 73 deletions

View File

@ -179,72 +179,76 @@ func Test_terraform_apply_autoApprove(t *testing.T) {
}, },
}, },
} }
for _, tc := range cases { for name, tc := range cases {
organization, cleanup := createOrganization(t) tc := tc
defer cleanup() t.Run(name, func(t *testing.T) {
exp, err := expect.NewConsole(defaultOpts()...) // t.Parallel()
if err != nil { organization, cleanup := createOrganization(t)
t.Fatal(err) defer cleanup()
} exp, err := expect.NewConsole(defaultOpts()...)
defer exp.Close() if err != nil {
t.Fatal(err)
}
defer exp.Close()
tmpDir, err := ioutil.TempDir("", "terraform-test") tmpDir, err := ioutil.TempDir("", "terraform-test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
tf := e2e.NewBinary(terraformBin, tmpDir) tf := e2e.NewBinary(terraformBin, tmpDir)
tf.AddEnv(cliConfigFileEnv) tf.AddEnv(cliConfigFileEnv)
defer tf.Close() defer tf.Close()
for _, op := range tc.operations { for _, op := range tc.operations {
op.prep(t, organization.Name, tf.WorkDir()) op.prep(t, organization.Name, tf.WorkDir())
for _, tfCmd := range op.commands { for _, tfCmd := range op.commands {
cmd := tf.Cmd(tfCmd.command...) cmd := tf.Cmd(tfCmd.command...)
cmd.Stdin = exp.Tty() cmd.Stdin = exp.Tty()
cmd.Stdout = exp.Tty() cmd.Stdout = exp.Tty()
cmd.Stderr = exp.Tty() cmd.Stderr = exp.Tty()
err = cmd.Start() err = cmd.Start()
if err != nil {
t.Fatal(err)
}
if tfCmd.expectedCmdOutput != "" {
got, err := exp.ExpectString(tfCmd.expectedCmdOutput)
if err != nil { 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) if tfCmd.expectedCmdOutput != "" {
lenInputOutput := len(tfCmd.postInputOutput) got, err := exp.ExpectString(tfCmd.expectedCmdOutput)
if lenInput > 0 { if err != nil {
for i := 0; i < lenInput; i++ { t.Fatalf("error while waiting for output\nwant: %s\nerror: %s\noutput\n%s", tfCmd.expectedCmdOutput, err, got)
input := tfCmd.userInput[i] }
exp.SendLine(input) }
// use the index to find the corresponding
// output that matches the input. lenInput := len(tfCmd.userInput)
if lenInputOutput-1 >= i { lenInputOutput := len(tfCmd.postInputOutput)
output := tfCmd.postInputOutput[i] if lenInput > 0 {
_, err := exp.ExpectString(output) for i := 0; i < lenInput; i++ {
if err != nil { input := tfCmd.userInput[i]
t.Fatal(err) 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() err = cmd.Wait()
if err != nil { if err != nil && !tfCmd.expectError {
t.Fatal(err) t.Fatal(err)
}
} }
} }
}
if tc.validations != nil { if tc.validations != nil {
tc.validations(t, organization.Name) tc.validations(t, organization.Name)
} }
})
} }
} }

View File

@ -11,7 +11,7 @@ import (
func Test_backend_apply_before_init(t *testing.T) { func Test_backend_apply_before_init(t *testing.T) {
skipIfMissingEnvVar(t) skipIfMissingEnvVar(t)
t.Parallel() // t.Parallel()
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
cases := map[string]struct { cases := map[string]struct {
@ -74,7 +74,7 @@ func Test_backend_apply_before_init(t *testing.T) {
for name, tc := range cases { for name, tc := range cases {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() // t.Parallel()
organization, cleanup := createOrganization(t) organization, cleanup := createOrganization(t)
defer cleanup() defer cleanup()
exp, err := expect.NewConsole(defaultOpts()...) exp, err := expect.NewConsole(defaultOpts()...)
@ -130,6 +130,7 @@ func Test_backend_apply_before_init(t *testing.T) {
} }
} }
} }
err = cmd.Wait() err = cmd.Wait()
if err != nil && !tfCmd.expectError { if err != nil && !tfCmd.expectError {
t.Fatal(err) t.Fatal(err)

View File

@ -11,7 +11,7 @@ import (
func Test_init_with_empty_tags(t *testing.T) { func Test_init_with_empty_tags(t *testing.T) {
skipIfMissingEnvVar(t) skipIfMissingEnvVar(t)
t.Parallel() // t.Parallel()
skipWithoutRemoteTerraformVersion(t) skipWithoutRemoteTerraformVersion(t)
cases := map[string]struct { cases := map[string]struct {
@ -41,7 +41,7 @@ func Test_init_with_empty_tags(t *testing.T) {
for name, tc := range cases { for name, tc := range cases {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() // t.Parallel()
organization, cleanup := createOrganization(t) organization, cleanup := createOrganization(t)
defer cleanup() defer cleanup()
exp, err := expect.NewConsole(defaultOpts()...) exp, err := expect.NewConsole(defaultOpts()...)

View File

@ -62,10 +62,8 @@ func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) {
{ {
command: []string{"init"}, command: []string{"init"},
expectedCmdOutput: `Do you want to copy only your current workspace?`, expectedCmdOutput: `Do you want to copy only your current workspace?`,
userInput: []string{"yes", "yes"}, userInput: []string{"yes"},
postInputOutput: []string{ postInputOutput: []string{`Terraform Cloud has been successfully initialized!`},
`Do you want to copy existing state to Terraform Cloud?`,
`Terraform Cloud has been successfully initialized!`},
}, },
{ {
command: []string{"workspace", "show"}, command: []string{"workspace", "show"},
@ -129,10 +127,8 @@ func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) {
{ {
command: []string{"init"}, command: []string{"init"},
expectedCmdOutput: `Do you want to copy only your current workspace?`, expectedCmdOutput: `Do you want to copy only your current workspace?`,
userInput: []string{"yes", "yes"}, userInput: []string{"yes"},
postInputOutput: []string{ postInputOutput: []string{`Terraform Cloud has been successfully initialized!`},
`Do you want to copy existing state to Terraform Cloud?`,
`Terraform Cloud has been successfully initialized!`},
}, },
{ {
command: []string{"workspace", "list"}, command: []string{"workspace", "list"},
@ -197,10 +193,8 @@ func Test_migrate_multi_to_tfc_cloud_name_strategy(t *testing.T) {
{ {
command: []string{"init"}, command: []string{"init"},
expectedCmdOutput: `Do you want to copy only your current workspace?`, expectedCmdOutput: `Do you want to copy only your current workspace?`,
userInput: []string{"yes", "yes"}, userInput: []string{"yes"},
postInputOutput: []string{ postInputOutput: []string{`Terraform Cloud has been successfully initialized!`},
`Do you want to copy existing state to Terraform Cloud?`,
`Terraform Cloud has been successfully initialized!`},
}, },
{ {
command: []string{"workspace", "select", "default"}, 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 { for name, tc := range cases {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() // t.Parallel()
organization, cleanup := createOrganization(t) organization, cleanup := createOrganization(t)
defer cleanup() defer cleanup()
exp, err := expect.NewConsole(defaultOpts()...) 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 { for name, tc := range cases {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() // t.Parallel()
organization, cleanup := createOrganization(t) organization, cleanup := createOrganization(t)
defer cleanup() defer cleanup()
exp, err := expect.NewConsole(defaultOpts()...) exp, err := expect.NewConsole(defaultOpts()...)

View File

@ -131,7 +131,7 @@ func Test_migrate_single_to_tfc(t *testing.T) {
for name, tc := range cases { for name, tc := range cases {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() // t.Parallel()
organization, cleanup := createOrganization(t) organization, cleanup := createOrganization(t)
defer cleanup() defer cleanup()
exp, err := expect.NewConsole(defaultOpts()...) exp, err := expect.NewConsole(defaultOpts()...)

View File

@ -49,7 +49,7 @@ func Test_migrate_tfc_to_other(t *testing.T) {
for name, tc := range cases { for name, tc := range cases {
tc := tc tc := tc
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
t.Parallel() // t.Parallel()
organization, cleanup := createOrganization(t) organization, cleanup := createOrganization(t)
defer cleanup() defer cleanup()
exp, err := expect.NewConsole(defaultOpts()...) exp, err := expect.NewConsole(defaultOpts()...)