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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	expect "github.com/Netflix/go-expect"
    10  	tfe "github.com/hashicorp/go-tfe"
    11  	"github.com/hugorut/terraform/src/e2e"
    12  	tfversion "github.com/hugorut/terraform/version"
    13  )
    14  
    15  func terraformConfigRequiredVariable(org, name string) string {
    16  	return fmt.Sprintf(`
    17  terraform {
    18    cloud {
    19      hostname = "%s"
    20      organization = "%s"
    21  
    22      workspaces {
    23        name = "%s"
    24      }
    25    }
    26  }
    27  
    28  variable "foo" {
    29    type = string
    30  }
    31  
    32  variable "baz" {
    33  	type = string
    34  }
    35  
    36  output "test_cli" {
    37    value = var.foo
    38  }
    39  
    40  output "test_env" {
    41    value = var.baz
    42  }
    43  
    44  `, tfeHostname, org, name)
    45  }
    46  
    47  func Test_cloud_run_variables(t *testing.T) {
    48  	skipIfMissingEnvVar(t)
    49  	skipWithoutRemoteTerraformVersion(t)
    50  
    51  	cases := testCases{
    52  		"run variables from CLI arg": {
    53  			operations: []operationSets{
    54  				{
    55  					prep: func(t *testing.T, orgName, dir string) {
    56  						wsName := "new-workspace"
    57  						_ = createWorkspace(t, orgName, tfe.WorkspaceCreateOptions{
    58  							Name:             tfe.String(wsName),
    59  							TerraformVersion: tfe.String(tfversion.String()),
    60  						})
    61  						tfBlock := terraformConfigRequiredVariable(orgName, wsName)
    62  						writeMainTF(t, tfBlock, dir)
    63  					},
    64  					commands: []tfCommand{
    65  						{
    66  							command:           []string{"init"},
    67  							expectedCmdOutput: `Terraform Cloud has been successfully initialized!`,
    68  						},
    69  						{
    70  							command:           []string{"plan", "-var", "foo=bar"},
    71  							expectedCmdOutput: `  + test_cli = "bar"`,
    72  						},
    73  						{
    74  							command:           []string{"plan", "-var", "foo=bar"},
    75  							expectedCmdOutput: `  + test_env = "qux"`,
    76  						},
    77  					},
    78  				},
    79  			},
    80  		},
    81  	}
    82  
    83  	for name, tc := range cases {
    84  		t.Run(name, func(t *testing.T) {
    85  			organization, cleanup := createOrganization(t)
    86  			defer cleanup()
    87  			exp, err := expect.NewConsole(defaultOpts()...)
    88  			if err != nil {
    89  				t.Fatal(err)
    90  			}
    91  			defer exp.Close()
    92  
    93  			tmpDir, err := ioutil.TempDir("", "terraform-test")
    94  			if err != nil {
    95  				t.Fatal(err)
    96  			}
    97  			defer os.RemoveAll(tmpDir)
    98  
    99  			tf := e2e.NewBinary(terraformBin, tmpDir)
   100  			tf.AddEnv("TF_CLI_ARGS=-no-color")
   101  			tf.AddEnv("TF_VAR_baz=qux")
   102  			tf.AddEnv(cliConfigFileEnv)
   103  			defer tf.Close()
   104  
   105  			for _, op := range tc.operations {
   106  				op.prep(t, organization.Name, tf.WorkDir())
   107  				for _, tfCmd := range op.commands {
   108  					cmd := tf.Cmd(tfCmd.command...)
   109  					cmd.Stdin = exp.Tty()
   110  					cmd.Stdout = exp.Tty()
   111  					cmd.Stderr = exp.Tty()
   112  
   113  					err = cmd.Start()
   114  					if err != nil {
   115  						t.Fatal(err)
   116  					}
   117  
   118  					if tfCmd.expectedCmdOutput != "" {
   119  						got, err := exp.ExpectString(tfCmd.expectedCmdOutput)
   120  						if err != nil {
   121  							t.Fatalf("error while waiting for output\nwant: %s\nerror: %s\noutput\n%s", tfCmd.expectedCmdOutput, err, got)
   122  						}
   123  					}
   124  
   125  					lenInput := len(tfCmd.userInput)
   126  					lenInputOutput := len(tfCmd.postInputOutput)
   127  					if lenInput > 0 {
   128  						for i := 0; i < lenInput; i++ {
   129  							input := tfCmd.userInput[i]
   130  							exp.SendLine(input)
   131  							// use the index to find the corresponding
   132  							// output that matches the input.
   133  							if lenInputOutput-1 >= i {
   134  								output := tfCmd.postInputOutput[i]
   135  								_, err := exp.ExpectString(output)
   136  								if err != nil {
   137  									t.Fatalf(`Expected command output "%s", but got %v `, tfCmd.expectedCmdOutput, err)
   138  								}
   139  							}
   140  						}
   141  					}
   142  
   143  					err = cmd.Wait()
   144  					if err != nil && !tfCmd.expectError {
   145  						t.Fatal(err)
   146  					}
   147  				}
   148  
   149  				if tc.validations != nil {
   150  					tc.validations(t, organization.Name)
   151  				}
   152  			}
   153  		})
   154  	}
   155  }