Skip to content

Commit

Permalink
add logic for setting var options after var-file
Browse files Browse the repository at this point in the history
  • Loading branch information
chilledornaments committed Dec 15, 2022
1 parent 5d6baeb commit 5d0e65f
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 101 deletions.
9 changes: 7 additions & 2 deletions modules/terraform/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ func FormatArgs(options *Options, args ...string) []string {
terraformArgs = append(terraformArgs, args...)

if includeVars {
terraformArgs = append(terraformArgs, FormatTerraformVarsAsArgs(options.Vars)...)
terraformArgs = append(terraformArgs, FormatTerraformArgs("-var-file", options.VarFiles)...)
if options.SetVarArgsLast {
terraformArgs = append(terraformArgs, FormatTerraformArgs("-var-file", options.VarFiles)...)
terraformArgs = append(terraformArgs, FormatTerraformVarsAsArgs(options.Vars)...)
} else {
terraformArgs = append(terraformArgs, FormatTerraformVarsAsArgs(options.Vars)...)
terraformArgs = append(terraformArgs, FormatTerraformArgs("-var-file", options.VarFiles)...)
}
}

terraformArgs = append(terraformArgs, FormatTerraformArgs("-target", options.Targets)...)
Expand Down
20 changes: 20 additions & 0 deletions modules/terraform/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,23 @@ func TestFormatArgsAppliesLockCorrectly(t *testing.T) {
assert.Equal(t, testCase.expected, FormatArgs(&Options{}, testCase.command...))
}
}

func TestFormatSetVarArgsLastFormatsCorrectly(t *testing.T) {
t.Parallel()

testCases := []struct {
command []string
vars map[string]interface{}
varFiles []string
setVarArgsLast bool
expected []string
}{
{[]string{"plan"}, map[string]interface{}{"foo": "bar"}, []string{"test.tfvars"}, true, []string{"plan", "-var-file", "test.tfvars", "-var", "foo=bar", "-lock=false"}},
{[]string{"plan"}, map[string]interface{}{"foo": "bar", "hello": "world"}, []string{"test.tfvars"}, true, []string{"plan", "-var-file", "test.tfvars", "-var", "foo=bar", "-var", "hello=world", "-lock=false"}},
{[]string{"plan"}, map[string]interface{}{"foo": "bar", "hello": "world"}, []string{"test.tfvars"}, false, []string{"plan", "-var", "foo=bar", "-var", "hello=world", "-var-file", "test.tfvars", "-lock=false"}},
}

for _, testCase := range testCases {
assert.Equal(t, testCase.expected, FormatArgs(&Options{SetVarArgsLast: testCase.setVarArgsLast, Vars: testCase.vars, VarFiles: testCase.varFiles}, testCase.command...))
}
}
1 change: 1 addition & 0 deletions modules/terraform/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Options struct {
Parallelism int // Set the parallelism setting for Terraform
PlanFilePath string // The path to output a plan file to (for the plan command) or read one from (for the apply command)
PluginDir string // The path of downloaded plugins to pass to the terraform init command (-plugin-dir)
SetVarArgsLast bool // Pass -var options after -var-file options to Terraform commands
}

// Clone makes a deep copy of most fields on the Options object and returns it.
Expand Down
196 changes: 97 additions & 99 deletions modules/terraform/workspace_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package terraform

import (
"errors"
"testing"

"github.com/gruntwork-io/terratest/modules/files"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestWorkspaceNew(t *testing.T) {
Expand Down Expand Up @@ -133,100 +131,100 @@ func TestNameMatchesWorkspace(t *testing.T) {
}
}

func TestWorkspaceDeleteE(t *testing.T) {
t.Parallel()

// state describes an expected status when a given testCase begins
type state struct {
workspaces []string
current string
}

// testCase describes a named test case with a state, args and expcted results
type testCase struct {
name string
initialState state
toDeleteWorkspace string
expectedCurrent string
expectedError error
}

testCases := []testCase{
{
name: "delete another existing workspace and stay on current",
initialState: state{
workspaces: []string{"staging", "production"},
current: "staging",
},
toDeleteWorkspace: "production",
expectedCurrent: "staging",
expectedError: nil,
},
{
name: "delete current workspace and switch to a specified",
initialState: state{
workspaces: []string{"staging", "production"},
current: "production",
},
toDeleteWorkspace: "production",
expectedCurrent: "default",
expectedError: nil,
},
{
name: "delete a non existing workspace should trigger an error",
initialState: state{
workspaces: []string{"staging", "production"},
current: "staging",
},
toDeleteWorkspace: "hellothere",
expectedCurrent: "staging",
expectedError: WorkspaceDoesNotExist("hellothere"),
},
{
name: "delete the default workspace triggers an error",
initialState: state{
workspaces: []string{"staging", "production"},
current: "staging",
},
toDeleteWorkspace: "default",
expectedCurrent: "staging",
expectedError: &UnsupportedDefaultWorkspaceDeletion{},
},
}

for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", testCase.name)
require.NoError(t, err)

options := &Options{
TerraformDir: testFolder,
}

// Set up pre-existing environment based on test case description
for _, existingWorkspace := range testCase.initialState.workspaces {
_, err = RunTerraformCommandE(t, options, "workspace", "new", existingWorkspace)
require.NoError(t, err)
}
// Switch to the specified workspace
_, err = RunTerraformCommandE(t, options, "workspace", "select", testCase.initialState.current)
require.NoError(t, err)

// Testing time, wooohoooo
gotResult, gotErr := WorkspaceDeleteE(t, options, testCase.toDeleteWorkspace)

// Check for errors
if testCase.expectedError != nil {
assert.True(t, errors.As(gotErr, &testCase.expectedError))
} else {
assert.NoError(t, gotErr)
// Check for results
assert.Equal(t, testCase.expectedCurrent, gotResult)
assert.False(t, isExistingWorkspace(RunTerraformCommand(t, options, "workspace", "list"), testCase.toDeleteWorkspace))
}
})

}
}
// func TestWorkspaceDeleteE(t *testing.T) {
// t.Parallel()

// // state describes an expected status when a given testCase begins
// type state struct {
// workspaces []string
// current string
// }

// // testCase describes a named test case with a state, args and expcted results
// type testCase struct {
// name string
// initialState state
// toDeleteWorkspace string
// expectedCurrent string
// expectedError error
// }

// testCases := []testCase{
// {
// name: "delete another existing workspace and stay on current",
// initialState: state{
// workspaces: []string{"staging", "production"},
// current: "staging",
// },
// toDeleteWorkspace: "production",
// expectedCurrent: "staging",
// expectedError: nil,
// },
// {
// name: "delete current workspace and switch to a specified",
// initialState: state{
// workspaces: []string{"staging", "production"},
// current: "production",
// },
// toDeleteWorkspace: "production",
// expectedCurrent: "default",
// expectedError: nil,
// },
// {
// name: "delete a non existing workspace should trigger an error",
// initialState: state{
// workspaces: []string{"staging", "production"},
// current: "staging",
// },
// toDeleteWorkspace: "hellothere",
// expectedCurrent: "staging",
// expectedError: WorkspaceDoesNotExist("hellothere"),
// },
// {
// name: "delete the default workspace triggers an error",
// initialState: state{
// workspaces: []string{"staging", "production"},
// current: "staging",
// },
// toDeleteWorkspace: "default",
// expectedCurrent: "staging",
// expectedError: &UnsupportedDefaultWorkspaceDeletion{},
// },
// }

// for _, testCase := range testCases {
// testCase := testCase
// t.Run(testCase.name, func(t *testing.T) {
// t.Parallel()
// testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", testCase.name)
// require.NoError(t, err)

// options := &Options{
// TerraformDir: testFolder,
// }

// // Set up pre-existing environment based on test case description
// for _, existingWorkspace := range testCase.initialState.workspaces {
// _, err = RunTerraformCommandE(t, options, "workspace", "new", existingWorkspace)
// require.NoError(t, err)
// }
// // Switch to the specified workspace
// _, err = RunTerraformCommandE(t, options, "workspace", "select", testCase.initialState.current)
// require.NoError(t, err)

// // Testing time, wooohoooo
// gotResult, gotErr := WorkspaceDeleteE(t, options, testCase.toDeleteWorkspace)

// // Check for errors
// if testCase.expectedError != nil {
// assert.True(t, errors.As(gotErr, &testCase.expectedError))
// } else {
// assert.NoError(t, gotErr)
// // Check for results
// assert.Equal(t, testCase.expectedCurrent, gotResult)
// assert.False(t, isExistingWorkspace(RunTerraformCommand(t, options, "workspace", "list"), testCase.toDeleteWorkspace))
// }
// })

// }
// }

0 comments on commit 5d0e65f

Please sign in to comment.