github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/modules/terraform/workspace_test.go (about) 1 package terraform 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/gruntwork-io/terratest/modules/files" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestWorkspaceNew(t *testing.T) { 13 t.Parallel() 14 15 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", t.Name()) 16 if err != nil { 17 t.Fatal(err) 18 } 19 20 options := &Options{ 21 TerraformDir: testFolder, 22 } 23 24 out := WorkspaceSelectOrNew(t, options, "terratest") 25 26 assert.Equal(t, "terratest", out) 27 } 28 29 func TestWorkspaceIllegalName(t *testing.T) { 30 t.Parallel() 31 32 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", t.Name()) 33 if err != nil { 34 t.Fatal(err) 35 } 36 37 options := &Options{ 38 TerraformDir: testFolder, 39 } 40 41 out, err := WorkspaceSelectOrNewE(t, options, "###@@@&&&") 42 43 assert.Error(t, err) 44 assert.Equal(t, "", out, "%q should be an empty string", out) 45 } 46 47 func TestWorkspaceSelect(t *testing.T) { 48 t.Parallel() 49 50 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", t.Name()) 51 if err != nil { 52 t.Fatal(err) 53 } 54 55 options := &Options{ 56 TerraformDir: testFolder, 57 } 58 59 out := WorkspaceSelectOrNew(t, options, "terratest") 60 assert.Equal(t, "terratest", out) 61 62 out = WorkspaceSelectOrNew(t, options, "default") 63 assert.Equal(t, "default", out) 64 } 65 66 func TestWorkspaceApply(t *testing.T) { 67 t.Parallel() 68 69 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", t.Name()) 70 if err != nil { 71 t.Fatal(err) 72 } 73 74 options := &Options{ 75 TerraformDir: testFolder, 76 } 77 78 WorkspaceSelectOrNew(t, options, "Terratest") 79 out := InitAndApply(t, options) 80 81 assert.Contains(t, out, "Hello, Terratest") 82 } 83 84 func TestIsExistingWorkspace(t *testing.T) { 85 t.Parallel() 86 87 testCases := []struct { 88 out string 89 name string 90 expected bool 91 }{ 92 {" default\n* foo\n", "default", true}, 93 {"* default\n foo\n", "default", true}, 94 {" foo\n* default\n", "default", true}, 95 {"* foo\n default\n", "default", true}, 96 {" foo\n* bar\n", "default", false}, 97 {"* foo\n bar\n", "default", false}, 98 {" default\n* foobar\n", "foo", false}, 99 {"* default\n foobar\n", "foo", false}, 100 {" default\n* foo\n", "foobar", false}, 101 {"* default\n foo\n", "foobar", false}, 102 {"* default\n foo\n", "foo", true}, 103 } 104 105 for _, testCase := range testCases { 106 actual := isExistingWorkspace(testCase.out, testCase.name) 107 assert.Equal(t, testCase.expected, actual, "Out: %q, Name: %q", testCase.out, testCase.name) 108 } 109 } 110 111 func TestNameMatchesWorkspace(t *testing.T) { 112 t.Parallel() 113 114 testCases := []struct { 115 name string 116 workspace string 117 expected bool 118 }{ 119 {"default", " default", true}, 120 {"default", "* default", true}, 121 {"default", "", false}, 122 {"foo", " foobar", false}, 123 {"foo", "* foobar", false}, 124 {"foobar", " foo", false}, 125 {"foobar", "* foo", false}, 126 {"foo", " foo", true}, 127 {"foo", "* foo", true}, 128 } 129 130 for _, testCase := range testCases { 131 actual := nameMatchesWorkspace(testCase.name, testCase.workspace) 132 assert.Equal(t, testCase.expected, actual, "Name: %q, Workspace: %q", testCase.name, testCase.workspace) 133 } 134 } 135 136 func TestWorkspaceDeleteE(t *testing.T) { 137 t.Parallel() 138 139 // state describes an expected status when a given testCase begins 140 type state struct { 141 workspaces []string 142 current string 143 } 144 145 // testCase describes a named test case with a state, args and expcted results 146 type testCase struct { 147 name string 148 initialState state 149 toDeleteWorkspace string 150 expectedCurrent string 151 expectedError error 152 } 153 154 testCases := []testCase{ 155 { 156 name: "delete another existing workspace and stay on current", 157 initialState: state{ 158 workspaces: []string{"staging", "production"}, 159 current: "staging", 160 }, 161 toDeleteWorkspace: "production", 162 expectedCurrent: "staging", 163 expectedError: nil, 164 }, 165 { 166 name: "delete current workspace and switch to a specified", 167 initialState: state{ 168 workspaces: []string{"staging", "production"}, 169 current: "production", 170 }, 171 toDeleteWorkspace: "production", 172 expectedCurrent: "default", 173 expectedError: nil, 174 }, 175 { 176 name: "delete a non existing workspace should trigger an error", 177 initialState: state{ 178 workspaces: []string{"staging", "production"}, 179 current: "staging", 180 }, 181 toDeleteWorkspace: "hellothere", 182 expectedCurrent: "staging", 183 expectedError: WorkspaceDoesNotExist("hellothere"), 184 }, 185 { 186 name: "delete the default workspace triggers an error", 187 initialState: state{ 188 workspaces: []string{"staging", "production"}, 189 current: "staging", 190 }, 191 toDeleteWorkspace: "default", 192 expectedCurrent: "staging", 193 expectedError: &UnsupportedDefaultWorkspaceDeletion{}, 194 }, 195 } 196 197 for _, testCase := range testCases { 198 testCase := testCase 199 t.Run(testCase.name, func(t *testing.T) { 200 t.Parallel() 201 testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", testCase.name) 202 require.NoError(t, err) 203 204 options := &Options{ 205 TerraformDir: testFolder, 206 } 207 208 // Set up pre-existing environment based on test case description 209 for _, existingWorkspace := range testCase.initialState.workspaces { 210 _, err = RunTerraformCommandE(t, options, "workspace", "new", existingWorkspace) 211 require.NoError(t, err) 212 } 213 // Switch to the specified workspace 214 _, err = RunTerraformCommandE(t, options, "workspace", "select", testCase.initialState.current) 215 require.NoError(t, err) 216 217 // Testing time, wooohoooo 218 gotResult, gotErr := WorkspaceDeleteE(t, options, testCase.toDeleteWorkspace) 219 220 // Check for errors 221 if testCase.expectedError != nil { 222 assert.True(t, errors.As(gotErr, &testCase.expectedError)) 223 } else { 224 assert.NoError(t, gotErr) 225 // Check for results 226 assert.Equal(t, testCase.expectedCurrent, gotResult) 227 assert.False(t, isExistingWorkspace(RunTerraformCommand(t, options, "workspace", "list"), testCase.toDeleteWorkspace)) 228 } 229 }) 230 231 } 232 }