2021-10-21 13:19:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
expect "github.com/Netflix/go-expect"
|
|
|
|
"github.com/hashicorp/terraform/internal/e2e"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_backend_apply_before_init(t *testing.T) {
|
2021-11-01 19:31:41 +01:00
|
|
|
t.Parallel()
|
2021-10-28 01:07:12 +02:00
|
|
|
skipWithoutRemoteTerraformVersion(t)
|
|
|
|
|
2021-10-21 13:19:33 +02:00
|
|
|
cases := map[string]struct {
|
|
|
|
operations []operationSets
|
|
|
|
}{
|
|
|
|
"terraform apply with cloud block - blank state": {
|
|
|
|
operations: []operationSets{
|
|
|
|
{
|
|
|
|
prep: func(t *testing.T, orgName, dir string) {
|
|
|
|
wsName := "new-workspace"
|
|
|
|
tfBlock := terraformConfigCloudBackendName(orgName, wsName)
|
|
|
|
writeMainTF(t, tfBlock, dir)
|
|
|
|
},
|
|
|
|
commands: []tfCommand{
|
|
|
|
{
|
|
|
|
command: []string{"apply"},
|
2021-10-28 08:33:18 +02:00
|
|
|
expectedCmdOutput: `Terraform Cloud initialization required, please run "terraform init"`,
|
2021-10-21 13:19:33 +02:00
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"terraform apply with cloud block - local state": {
|
|
|
|
operations: []operationSets{
|
|
|
|
{
|
|
|
|
prep: func(t *testing.T, orgName, dir string) {
|
|
|
|
tfBlock := terraformConfigLocalBackend()
|
|
|
|
writeMainTF(t, tfBlock, dir)
|
|
|
|
},
|
|
|
|
commands: []tfCommand{
|
|
|
|
{
|
|
|
|
command: []string{"init"},
|
|
|
|
expectedCmdOutput: `Successfully configured the backend "local"!`,
|
|
|
|
},
|
|
|
|
{
|
2021-11-03 21:47:14 +01:00
|
|
|
command: []string{"apply", "-auto-approve"},
|
|
|
|
postInputOutput: []string{`Apply complete!`},
|
2021-10-21 13:19:33 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
prep: func(t *testing.T, orgName, dir string) {
|
|
|
|
wsName := "new-workspace"
|
|
|
|
tfBlock := terraformConfigCloudBackendName(orgName, wsName)
|
|
|
|
writeMainTF(t, tfBlock, dir)
|
|
|
|
},
|
|
|
|
commands: []tfCommand{
|
|
|
|
{
|
|
|
|
command: []string{"apply"},
|
2021-10-28 08:33:18 +02:00
|
|
|
expectedCmdOutput: `Terraform Cloud initialization required, please run "terraform init"`,
|
2021-10-21 13:19:33 +02:00
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
2021-11-03 21:47:14 +01:00
|
|
|
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()
|
2021-10-21 13:19:33 +02:00
|
|
|
|
2021-11-03 21:47:14 +01:00
|
|
|
tmpDir, err := ioutil.TempDir("", "terraform-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
2021-10-21 13:19:33 +02:00
|
|
|
|
2021-11-03 21:47:14 +01:00
|
|
|
tf := e2e.NewBinary(terraformBin, tmpDir)
|
|
|
|
tf.AddEnv(cliConfigFileEnv)
|
|
|
|
defer tf.Close()
|
2021-10-21 13:19:33 +02:00
|
|
|
|
2021-11-03 21:47:14 +01:00
|
|
|
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()
|
2021-10-21 13:19:33 +02:00
|
|
|
|
2021-11-03 21:47:14 +01:00
|
|
|
err = cmd.Start()
|
2021-10-21 13:19:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-03 21:47:14 +01:00
|
|
|
if tfCmd.expectedCmdOutput != "" {
|
|
|
|
_, err := exp.ExpectString(tfCmd.expectedCmdOutput)
|
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
}
|
2021-10-21 13:19:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 21:47:14 +01:00
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil && !tfCmd.expectError {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-10-21 13:19:33 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-03 21:47:14 +01:00
|
|
|
})
|
2021-10-21 13:19:33 +02:00
|
|
|
}
|
|
|
|
}
|