Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Yevgeniy Brikman <[email protected]>
  • Loading branch information
dduportal and brikis98 committed Mar 22, 2021
1 parent 5399bb0 commit c684c68
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 130 deletions.
14 changes: 14 additions & 0 deletions modules/terraform/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,17 @@ type PanicWhileParsingVarFile struct {
func (err PanicWhileParsingVarFile) Error() string {
return fmt.Sprintf("Recovering panic while parsing '%s'. Got error of type '%v': %v", err.ConfigFile, reflect.TypeOf(err.RecoveredValue), err.RecoveredValue)
}

// UnsupportedDefaultWorkspaceDeletion is returned when user tries to delete the workspace "default"
type UnsupportedDefaultWorkspaceDeletion struct{}

func (err *UnsupportedDefaultWorkspaceDeletion) Error() string {
return "Deleting the workspace 'default' is not supported"
}

// WorkspaceDoesNotExist is returned when user tries to delete a workspace which does not exist
type WorkspaceDoesNotExist string

func (err WorkspaceDoesNotExist) Error() string {
return fmt.Sprintf("The workspace %q does not exist.", string(err))
}
19 changes: 12 additions & 7 deletions modules/terraform/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

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

// WorkspaceSelectOrNew runs terraform workspace with the given options and the workspace name
Expand Down Expand Up @@ -66,23 +67,29 @@ func nameMatchesWorkspace(name string, workspace string) bool {
// If the workspace to delete is the current one, then it tries to switch to the "default" workspace.
// Deleting the workspace "default" is not supported.
func WorkspaceDeleteE(t testing.TestingT, options *Options, name string) (string, error) {
currentWorkspace := RunTerraformCommand(t, options, "workspace", "show")
currentWorkspace, err := RunTerraformCommandE(t, options, "workspace", "show")
if err != nil {
return currentWorkspace, err
}

if name == "default" {
return currentWorkspace, fmt.Errorf("Deleting the workspace 'default' is not supported")
return currentWorkspace, &UnsupportedDefaultWorkspaceDeletion{}
}

out, err := RunTerraformCommandE(t, options, "workspace", "list")
if err != nil {
return currentWorkspace, err
}
if !isExistingWorkspace(out, name) {
return currentWorkspace, fmt.Errorf("The workspace %q does not exist.", name)
return currentWorkspace, WorkspaceDoesNotExist(name)
}

// Switch workspace before deleting if it is the current
if currentWorkspace == name {
currentWorkspace = WorkspaceSelectOrNew(t, options, "default")
currentWorkspace, err = WorkspaceSelectOrNewE(t, options, "default")
if err != nil {
return currentWorkspace, err
}
}

// delete workspace
Expand All @@ -101,8 +108,6 @@ func WorkspaceDelete(t testing.TestingT, options *Options, name string) string {
}

out, err := WorkspaceDeleteE(t, options, name)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
return out
}
171 changes: 48 additions & 123 deletions modules/terraform/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"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 @@ -131,7 +132,6 @@ func TestNameMatchesWorkspace(t *testing.T) {
}
}

// Please note that this test depends on other functions that should be mocked to be a unit test.
func TestWorkspaceDeleteE(t *testing.T) {
t.Parallel()

Expand All @@ -143,11 +143,11 @@ func TestWorkspaceDeleteE(t *testing.T) {

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

testCases := []testCase{
Expand All @@ -157,150 +157,75 @@ func TestWorkspaceDeleteE(t *testing.T) {
workspaces: []string{"staging", "production"},
current: "staging",
},
toDeleteWorkspace: "production",
expectedCurrent: "staging",
expectedErrorMessage: "",
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",
expectedErrorMessage: "",
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",
expectedErrorMessage: "The workspace \"hellothere\" does not exist.",
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",
expectedErrorMessage: "Deleting the workspace 'default' is not supported",
toDeleteWorkspace: "default",
expectedCurrent: "staging",
expectedError: &UnsupportedDefaultWorkspaceDeletion{},
},
}

for _, tt := range testCases {
testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", tt.name)
if err != nil {
t.Fatal(err)
}

options := &Options{
TerraformDir: testFolder,
}

// Set up pre-existing environment based on test case description
for _, existingWorkspace := range tt.initialState.workspaces {
_, err = RunTerraformCommandE(t, options, "workspace", "new", existingWorkspace)
if err != nil {
t.Fatal(err)
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,
}
}
// Switch to the specified workspace
_, err = RunTerraformCommandE(t, options, "workspace", "select", tt.initialState.current)
if err != nil {
t.Fatal(err)
}

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

// Check for errors
if tt.expectedErrorMessage != "" {
assert.Error(t, gotErr)
assert.Equal(t, tt.expectedErrorMessage, gotErr.Error())
} else {
assert.Nil(t, gotErr)
// Check for results
assert.Equal(t, tt.expectedCurrent, gotResult)
assert.False(t, isExistingWorkspace(RunTerraformCommand(t, options, "workspace", "list"), tt.toDeleteWorkspace))
}

}
}

// Please note that this test depends on other functions that should be mocked to be a unit test.
func TestWorkspaceDelete(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
}

testCases := []testCase{
{
name: "delete another existing workspace and stay on current",
initialState: state{
workspaces: []string{"staging", "production"},
current: "staging",
},
toDeleteWorkspace: "production",
expectedCurrent: "staging",
},
{
name: "delete current workspace and switch to a specified",
initialState: state{
workspaces: []string{"staging", "production"},
current: "production",
},
toDeleteWorkspace: "production",
expectedCurrent: "default",
},
}

for _, tt := range testCases {
testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", tt.name)
if err != nil {
t.Fatal(err)
}

options := &Options{
TerraformDir: testFolder,
}

// Set up pre-existing environment based on test case description
for _, existingWorkspace := range tt.initialState.workspaces {
_, err = RunTerraformCommandE(t, options, "workspace", "new", existingWorkspace)
if err != nil {
t.Fatal(err)
// 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.EqualError(t, gotErr, testCase.expectedError.Error())
} 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))
}
}
// Switch to the specified workspace
_, err = RunTerraformCommandE(t, options, "workspace", "select", tt.initialState.current)
if err != nil {
t.Fatal(err)
}

// Testing time, wooohoooo
gotResult := WorkspaceDelete(t, options, tt.toDeleteWorkspace)

// Check for results
assert.Equal(t, tt.expectedCurrent, gotResult)
assert.False(t, isExistingWorkspace(RunTerraformCommand(t, options, "workspace", "list"), tt.toDeleteWorkspace))
})

}
}

0 comments on commit c684c68

Please sign in to comment.