2021-09-22 23:53:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
expect "github.com/Netflix/go-expect"
|
|
|
|
"github.com/hashicorp/terraform/internal/e2e"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_migrate_tfc_to_other(t *testing.T) {
|
2021-11-12 18:00:24 +01:00
|
|
|
skipIfMissingEnvVar(t)
|
2021-09-22 23:53:33 +02:00
|
|
|
cases := map[string]struct {
|
|
|
|
operations []operationSets
|
|
|
|
}{
|
|
|
|
"migrate from cloud to local backend": {
|
|
|
|
operations: []operationSets{
|
|
|
|
{
|
|
|
|
prep: func(t *testing.T, orgName, dir string) {
|
|
|
|
wsName := "new-workspace"
|
|
|
|
tfBlock := terraformConfigCloudBackendName(orgName, wsName)
|
|
|
|
writeMainTF(t, tfBlock, dir)
|
|
|
|
},
|
|
|
|
commands: []tfCommand{
|
|
|
|
{
|
2021-10-09 14:47:12 +02:00
|
|
|
command: []string{"init"},
|
2021-10-19 23:06:41 +02:00
|
|
|
expectedCmdOutput: `Terraform Cloud has been successfully initialized!`,
|
2021-09-22 23:53:33 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
prep: func(t *testing.T, orgName, dir string) {
|
|
|
|
tfBlock := terraformConfigLocalBackend()
|
|
|
|
writeMainTF(t, tfBlock, dir)
|
|
|
|
},
|
|
|
|
commands: []tfCommand{
|
|
|
|
{
|
2021-10-09 14:47:12 +02:00
|
|
|
command: []string{"init", "-migrate-state"},
|
|
|
|
expectedCmdOutput: `Migrating state from Terraform Cloud to another backend is not yet implemented.`,
|
|
|
|
expectError: true,
|
2021-09-22 23:53:33 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-22 23:53: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-09-22 23:53:33 +02:00
|
|
|
|
2021-11-03 21:47:14 +01:00
|
|
|
tf := e2e.NewBinary(terraformBin, tmpDir)
|
|
|
|
tf.AddEnv(cliConfigFileEnv)
|
|
|
|
defer tf.Close()
|
2021-09-22 23:53: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-09-22 23:53:33 +02:00
|
|
|
|
2021-11-03 21:47:14 +01:00
|
|
|
err = cmd.Start()
|
2021-09-22 23:53:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-03 21:47:14 +01:00
|
|
|
if tfCmd.expectedCmdOutput != "" {
|
2021-11-17 00:03:43 +01:00
|
|
|
got, err := exp.ExpectString(tfCmd.expectedCmdOutput)
|
2021-11-03 21:47:14 +01:00
|
|
|
if err != nil {
|
2021-11-17 00:03:43 +01:00
|
|
|
t.Fatalf("error while waiting for output\nwant: %s\nerror: %s\noutput\n%s", tfCmd.expectedCmdOutput, err, got)
|
2021-11-03 21:47:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09 14:47:12 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
2021-11-03 21:47:14 +01:00
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil && !tfCmd.expectError {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-03 21:47:14 +01:00
|
|
|
})
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
}
|