github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/cloud/e2e/run_variables_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	tfe "github.com/hashicorp/go-tfe"
    11  	tfversion "github.com/terramate-io/tf/version"
    12  )
    13  
    14  func terraformConfigRequiredVariable(org, name string) string {
    15  	return fmt.Sprintf(`
    16  terraform {
    17    cloud {
    18      hostname = "%s"
    19      organization = "%s"
    20  
    21      workspaces {
    22        name = "%s"
    23      }
    24    }
    25  }
    26  
    27  variable "foo" {
    28    type = string
    29  }
    30  
    31  variable "baz" {
    32  	type = string
    33  }
    34  
    35  output "test_cli" {
    36    value = var.foo
    37  }
    38  
    39  output "test_env" {
    40    value = var.baz
    41  }
    42  
    43  `, tfeHostname, org, name)
    44  }
    45  
    46  func Test_cloud_run_variables(t *testing.T) {
    47  	t.Parallel()
    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  	testRunner(t, cases, 1, "TF_CLI_ARGS=-no-color", "TF_VAR_baz=qux")
    84  }