github.com/hugorut/terraform@v1.1.3/src/cloud/e2e/init_with_empty_tags_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_init_with_empty_tags(t *testing.T) {
    13  	skipIfMissingEnvVar(t)
    14  	// t.Parallel()
    15  	skipWithoutRemoteTerraformVersion(t)
    16  
    17  	cases := map[string]struct {
    18  		operations []operationSets
    19  	}{
    20  		"terraform init with cloud block - no tagged workspaces exist yet": {
    21  			operations: []operationSets{
    22  				{
    23  					prep: func(t *testing.T, orgName, dir string) {
    24  						wsTag := "emptytag"
    25  						tfBlock := terraformConfigCloudBackendTags(orgName, wsTag)
    26  						writeMainTF(t, tfBlock, dir)
    27  					},
    28  					commands: []tfCommand{
    29  						{
    30  							command:           []string{"init"},
    31  							expectedCmdOutput: `There are no workspaces with the configured tags`,
    32  							userInput:         []string{"emptytag-prod"},
    33  							postInputOutput:   []string{`Terraform Cloud has been successfully initialized!`},
    34  						},
    35  					},
    36  				},
    37  			},
    38  		},
    39  	}
    40  
    41  	for name, tc := range cases {
    42  		tc := tc
    43  		t.Run(name, func(t *testing.T) {
    44  			// t.Parallel()
    45  			organization, cleanup := createOrganization(t)
    46  			defer cleanup()
    47  			exp, err := expect.NewConsole(defaultOpts()...)
    48  			if err != nil {
    49  				t.Fatal(err)
    50  			}
    51  			defer exp.Close()
    52  
    53  			tmpDir, err := ioutil.TempDir("", "terraform-test")
    54  			if err != nil {
    55  				t.Fatal(err)
    56  			}
    57  			defer os.RemoveAll(tmpDir)
    58  
    59  			tf := e2e.NewBinary(terraformBin, tmpDir)
    60  			tf.AddEnv(cliConfigFileEnv)
    61  			defer tf.Close()
    62  
    63  			for _, op := range tc.operations {
    64  				op.prep(t, organization.Name, tf.WorkDir())
    65  				for _, tfCmd := range op.commands {
    66  					cmd := tf.Cmd(tfCmd.command...)
    67  					cmd.Stdin = exp.Tty()
    68  					cmd.Stdout = exp.Tty()
    69  					cmd.Stderr = exp.Tty()
    70  
    71  					err = cmd.Start()
    72  					if err != nil {
    73  						t.Fatal(err)
    74  					}
    75  
    76  					if tfCmd.expectedCmdOutput != "" {
    77  						got, err := exp.ExpectString(tfCmd.expectedCmdOutput)
    78  						if err != nil {
    79  							t.Fatalf("error while waiting for output\nwant: %s\nerror: %s\noutput\n%s", tfCmd.expectedCmdOutput, err, got)
    80  						}
    81  					}
    82  
    83  					lenInput := len(tfCmd.userInput)
    84  					lenInputOutput := len(tfCmd.postInputOutput)
    85  					if lenInput > 0 {
    86  						for i := 0; i < lenInput; i++ {
    87  							input := tfCmd.userInput[i]
    88  							exp.SendLine(input)
    89  							// use the index to find the corresponding
    90  							// output that matches the input.
    91  							if lenInputOutput-1 >= i {
    92  								output := tfCmd.postInputOutput[i]
    93  								_, err := exp.ExpectString(output)
    94  								if err != nil {
    95  									t.Fatal(err)
    96  								}
    97  							}
    98  						}
    99  					}
   100  					err = cmd.Wait()
   101  					if err != nil && !tfCmd.expectError {
   102  						t.Fatal(err)
   103  					}
   104  				}
   105  			}
   106  		})
   107  	}
   108  }