Skip to content

Commit

Permalink
uncomment test
Browse files Browse the repository at this point in the history
  • Loading branch information
chilledornaments committed Dec 15, 2022
1 parent 5d0e65f commit 56a2e8d
Showing 1 changed file with 99 additions and 97 deletions.
196 changes: 99 additions & 97 deletions modules/terraform/workspace_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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 @@ -131,100 +133,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 56a2e8d

Please sign in to comment.