github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/cloud/e2e/backend_apply_before_init_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/muratcelep/terraform/not-internal/e2e"
    10  )
    11  
    12  func Test_backend_apply_before_init(t *testing.T) {
    13  	skipIfMissingEnvVar(t)
    14  	// t.Parallel()
    15  	skipWithoutRemoteTerraformVersion(t)
    16  
    17  	cases := map[string]struct {
    18  		operations []operationSets
    19  	}{
    20  		"terraform apply with cloud block - blank state": {
    21  			operations: []operationSets{
    22  				{
    23  					prep: func(t *testing.T, orgName, dir string) {
    24  						wsName := "new-workspace"
    25  						tfBlock := terraformConfigCloudBackendName(orgName, wsName)
    26  						writeMainTF(t, tfBlock, dir)
    27  					},
    28  					commands: []tfCommand{
    29  						{
    30  							command:           []string{"apply"},
    31  							expectedCmdOutput: `Terraform Cloud initialization required: please run "terraform init"`,
    32  							expectError:       true,
    33  						},
    34  					},
    35  				},
    36  			},
    37  		},
    38  		"terraform apply with cloud block - local state": {
    39  			operations: []operationSets{
    40  				{
    41  					prep: func(t *testing.T, orgName, dir string) {
    42  						tfBlock := terraformConfigLocalBackend()
    43  						writeMainTF(t, tfBlock, dir)
    44  					},
    45  					commands: []tfCommand{
    46  						{
    47  							command:           []string{"init"},
    48  							expectedCmdOutput: `Successfully configured the backend "local"!`,
    49  						},
    50  						{
    51  							command:         []string{"apply", "-auto-approve"},
    52  							postInputOutput: []string{`Apply complete!`},
    53  						},
    54  					},
    55  				},
    56  				{
    57  					prep: func(t *testing.T, orgName, dir string) {
    58  						wsName := "new-workspace"
    59  						tfBlock := terraformConfigCloudBackendName(orgName, wsName)
    60  						writeMainTF(t, tfBlock, dir)
    61  					},
    62  					commands: []tfCommand{
    63  						{
    64  							command:           []string{"apply"},
    65  							expectedCmdOutput: `Terraform Cloud initialization required: please run "terraform init"`,
    66  							expectError:       true,
    67  						},
    68  					},
    69  				},
    70  			},
    71  		},
    72  	}
    73  
    74  	for name, tc := range cases {
    75  		tc := tc
    76  		t.Run(name, func(t *testing.T) {
    77  			// t.Parallel()
    78  			organization, cleanup := createOrganization(t)
    79  			defer cleanup()
    80  			exp, err := expect.NewConsole(defaultOpts()...)
    81  			if err != nil {
    82  				t.Fatal(err)
    83  			}
    84  			defer exp.Close()
    85  
    86  			tmpDir, err := ioutil.TempDir("", "terraform-test")
    87  			if err != nil {
    88  				t.Fatal(err)
    89  			}
    90  			defer os.RemoveAll(tmpDir)
    91  
    92  			tf := e2e.NewBinary(terraformBin, tmpDir)
    93  			tf.AddEnv(cliConfigFileEnv)
    94  			defer tf.Close()
    95  
    96  			for _, op := range tc.operations {
    97  				op.prep(t, organization.Name, tf.WorkDir())
    98  				for _, tfCmd := range op.commands {
    99  					cmd := tf.Cmd(tfCmd.command...)
   100  					cmd.Stdin = exp.Tty()
   101  					cmd.Stdout = exp.Tty()
   102  					cmd.Stderr = exp.Tty()
   103  
   104  					err = cmd.Start()
   105  					if err != nil {
   106  						t.Fatal(err)
   107  					}
   108  
   109  					if tfCmd.expectedCmdOutput != "" {
   110  						got, err := exp.ExpectString(tfCmd.expectedCmdOutput)
   111  						if err != nil {
   112  							t.Fatalf("error while waiting for output\nwant: %s\nerror: %s\noutput\n%s", tfCmd.expectedCmdOutput, err, got)
   113  						}
   114  					}
   115  
   116  					lenInput := len(tfCmd.userInput)
   117  					lenInputOutput := len(tfCmd.postInputOutput)
   118  					if lenInput > 0 {
   119  						for i := 0; i < lenInput; i++ {
   120  							input := tfCmd.userInput[i]
   121  							exp.SendLine(input)
   122  							// use the index to find the corresponding
   123  							// output that matches the input.
   124  							if lenInputOutput-1 >= i {
   125  								output := tfCmd.postInputOutput[i]
   126  								_, err := exp.ExpectString(output)
   127  								if err != nil {
   128  									t.Fatal(err)
   129  								}
   130  							}
   131  						}
   132  					}
   133  
   134  					err = cmd.Wait()
   135  					if err != nil && !tfCmd.expectError {
   136  						t.Fatal(err)
   137  					}
   138  				}
   139  			}
   140  		})
   141  	}
   142  }