github.com/cycloidio/terraform@v1.1.10-0.20220513142504-76d5c768dc63/cloud/e2e/run_variables_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	tfe "github.com/hashicorp/go-tfe"
     8  	tfversion "github.com/cycloidio/terraform/version"
     9  )
    10  
    11  func terraformConfigRequiredVariable(org, name string) string {
    12  	return fmt.Sprintf(`
    13  terraform {
    14    cloud {
    15      hostname = "%s"
    16      organization = "%s"
    17  
    18      workspaces {
    19        name = "%s"
    20      }
    21    }
    22  }
    23  
    24  variable "foo" {
    25    type = string
    26  }
    27  
    28  variable "baz" {
    29  	type = string
    30  }
    31  
    32  output "test_cli" {
    33    value = var.foo
    34  }
    35  
    36  output "test_env" {
    37    value = var.baz
    38  }
    39  
    40  `, tfeHostname, org, name)
    41  }
    42  
    43  func Test_cloud_run_variables(t *testing.T) {
    44  	t.Parallel()
    45  	skipIfMissingEnvVar(t)
    46  	skipWithoutRemoteTerraformVersion(t)
    47  
    48  	cases := testCases{
    49  		"run variables from CLI arg": {
    50  			operations: []operationSets{
    51  				{
    52  					prep: func(t *testing.T, orgName, dir string) {
    53  						wsName := "new-workspace"
    54  						_ = createWorkspace(t, orgName, tfe.WorkspaceCreateOptions{
    55  							Name:             tfe.String(wsName),
    56  							TerraformVersion: tfe.String(tfversion.String()),
    57  						})
    58  						tfBlock := terraformConfigRequiredVariable(orgName, wsName)
    59  						writeMainTF(t, tfBlock, dir)
    60  					},
    61  					commands: []tfCommand{
    62  						{
    63  							command:           []string{"init"},
    64  							expectedCmdOutput: `Terraform Cloud has been successfully initialized!`,
    65  						},
    66  						{
    67  							command:           []string{"plan", "-var", "foo=bar"},
    68  							expectedCmdOutput: `  + test_cli = "bar"`,
    69  						},
    70  						{
    71  							command:           []string{"plan", "-var", "foo=bar"},
    72  							expectedCmdOutput: `  + test_env = "qux"`,
    73  						},
    74  					},
    75  				},
    76  			},
    77  		},
    78  	}
    79  
    80  	testRunner(t, cases, 1, "TF_CLI_ARGS=-no-color", "TF_VAR_baz=qux")
    81  }