github.com/hugorut/terraform@v1.1.3/src/cloud/e2e/migrate_state_tfc_to_other_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	expect "github.com/Netflix/go-expect"
     9  	"github.com/hugorut/terraform/src/e2e"
    10  )
    11  
    12  func Test_migrate_tfc_to_other(t *testing.T) {
    13  	skipIfMissingEnvVar(t)
    14  	cases := map[string]struct {
    15  		operations []operationSets
    16  	}{
    17  		"migrate from cloud to local backend": {
    18  			operations: []operationSets{
    19  				{
    20  					prep: func(t *testing.T, orgName, dir string) {
    21  						wsName := "new-workspace"
    22  						tfBlock := terraformConfigCloudBackendName(orgName, wsName)
    23  						writeMainTF(t, tfBlock, dir)
    24  					},
    25  					commands: []tfCommand{
    26  						{
    27  							command:           []string{"init"},
    28  							expectedCmdOutput: `Terraform Cloud has been successfully initialized!`,
    29  						},
    30  					},
    31  				},
    32  				{
    33  					prep: func(t *testing.T, orgName, dir string) {
    34  						tfBlock := terraformConfigLocalBackend()
    35  						writeMainTF(t, tfBlock, dir)
    36  					},
    37  					commands: []tfCommand{
    38  						{
    39  							command:           []string{"init"},
    40  							expectedCmdOutput: `Migrating state from Terraform Cloud to another backend is not yet implemented.`,
    41  							expectError:       true,
    42  						},
    43  					},
    44  				},
    45  			},
    46  		},
    47  	}
    48  
    49  	for name, tc := range cases {
    50  		tc := tc
    51  		t.Run(name, func(t *testing.T) {
    52  			// t.Parallel()
    53  			organization, cleanup := createOrganization(t)
    54  			defer cleanup()
    55  			exp, err := expect.NewConsole(defaultOpts()...)
    56  			if err != nil {
    57  				t.Fatal(err)
    58  			}
    59  			defer exp.Close()
    60  
    61  			tmpDir, err := ioutil.TempDir("", "terraform-test")
    62  			if err != nil {
    63  				t.Fatal(err)
    64  			}
    65  			defer os.RemoveAll(tmpDir)
    66  
    67  			tf := e2e.NewBinary(terraformBin, tmpDir)
    68  			tf.AddEnv(cliConfigFileEnv)
    69  			defer tf.Close()
    70  
    71  			for _, op := range tc.operations {
    72  				op.prep(t, organization.Name, tf.WorkDir())
    73  				for _, tfCmd := range op.commands {
    74  					cmd := tf.Cmd(tfCmd.command...)
    75  					cmd.Stdin = exp.Tty()
    76  					cmd.Stdout = exp.Tty()
    77  					cmd.Stderr = exp.Tty()
    78  
    79  					err = cmd.Start()
    80  					if err != nil {
    81  						t.Fatal(err)
    82  					}
    83  
    84  					if tfCmd.expectedCmdOutput != "" {
    85  						got, err := exp.ExpectString(tfCmd.expectedCmdOutput)
    86  						if err != nil {
    87  							t.Fatalf("error while waiting for output\nwant: %s\nerror: %s\noutput\n%s", tfCmd.expectedCmdOutput, err, got)
    88  						}
    89  					}
    90  
    91  					lenInput := len(tfCmd.userInput)
    92  					lenInputOutput := len(tfCmd.postInputOutput)
    93  					if lenInput > 0 {
    94  						for i := 0; i < lenInput; i++ {
    95  							input := tfCmd.userInput[i]
    96  							exp.SendLine(input)
    97  							// use the index to find the corresponding
    98  							// output that matches the input.
    99  							if lenInputOutput-1 >= i {
   100  								output := tfCmd.postInputOutput[i]
   101  								_, err := exp.ExpectString(output)
   102  								if err != nil {
   103  									t.Fatal(err)
   104  								}
   105  							}
   106  						}
   107  					}
   108  					err = cmd.Wait()
   109  					if err != nil && !tfCmd.expectError {
   110  						t.Fatal(err)
   111  					}
   112  				}
   113  			}
   114  		})
   115  	}
   116  }