|
1 | 1 | package terraform
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/gruntwork-io/terratest/modules/files"
|
7 | 8 | "github.com/stretchr/testify/assert"
|
| 9 | + "github.com/stretchr/testify/require" |
8 | 10 | )
|
9 | 11 |
|
10 | 12 | func TestWorkspaceNew(t *testing.T) {
|
@@ -131,100 +133,100 @@ func TestNameMatchesWorkspace(t *testing.T) {
|
131 | 133 | }
|
132 | 134 | }
|
133 | 135 |
|
134 |
| -// func TestWorkspaceDeleteE(t *testing.T) { |
135 |
| -// t.Parallel() |
136 |
| - |
137 |
| -// // state describes an expected status when a given testCase begins |
138 |
| -// type state struct { |
139 |
| -// workspaces []string |
140 |
| -// current string |
141 |
| -// } |
142 |
| - |
143 |
| -// // testCase describes a named test case with a state, args and expcted results |
144 |
| -// type testCase struct { |
145 |
| -// name string |
146 |
| -// initialState state |
147 |
| -// toDeleteWorkspace string |
148 |
| -// expectedCurrent string |
149 |
| -// expectedError error |
150 |
| -// } |
151 |
| - |
152 |
| -// testCases := []testCase{ |
153 |
| -// { |
154 |
| -// name: "delete another existing workspace and stay on current", |
155 |
| -// initialState: state{ |
156 |
| -// workspaces: []string{"staging", "production"}, |
157 |
| -// current: "staging", |
158 |
| -// }, |
159 |
| -// toDeleteWorkspace: "production", |
160 |
| -// expectedCurrent: "staging", |
161 |
| -// expectedError: nil, |
162 |
| -// }, |
163 |
| -// { |
164 |
| -// name: "delete current workspace and switch to a specified", |
165 |
| -// initialState: state{ |
166 |
| -// workspaces: []string{"staging", "production"}, |
167 |
| -// current: "production", |
168 |
| -// }, |
169 |
| -// toDeleteWorkspace: "production", |
170 |
| -// expectedCurrent: "default", |
171 |
| -// expectedError: nil, |
172 |
| -// }, |
173 |
| -// { |
174 |
| -// name: "delete a non existing workspace should trigger an error", |
175 |
| -// initialState: state{ |
176 |
| -// workspaces: []string{"staging", "production"}, |
177 |
| -// current: "staging", |
178 |
| -// }, |
179 |
| -// toDeleteWorkspace: "hellothere", |
180 |
| -// expectedCurrent: "staging", |
181 |
| -// expectedError: WorkspaceDoesNotExist("hellothere"), |
182 |
| -// }, |
183 |
| -// { |
184 |
| -// name: "delete the default workspace triggers an error", |
185 |
| -// initialState: state{ |
186 |
| -// workspaces: []string{"staging", "production"}, |
187 |
| -// current: "staging", |
188 |
| -// }, |
189 |
| -// toDeleteWorkspace: "default", |
190 |
| -// expectedCurrent: "staging", |
191 |
| -// expectedError: &UnsupportedDefaultWorkspaceDeletion{}, |
192 |
| -// }, |
193 |
| -// } |
194 |
| - |
195 |
| -// for _, testCase := range testCases { |
196 |
| -// testCase := testCase |
197 |
| -// t.Run(testCase.name, func(t *testing.T) { |
198 |
| -// t.Parallel() |
199 |
| -// testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-workspace", testCase.name) |
200 |
| -// require.NoError(t, err) |
201 |
| - |
202 |
| -// options := &Options{ |
203 |
| -// TerraformDir: testFolder, |
204 |
| -// } |
205 |
| - |
206 |
| -// // Set up pre-existing environment based on test case description |
207 |
| -// for _, existingWorkspace := range testCase.initialState.workspaces { |
208 |
| -// _, err = RunTerraformCommandE(t, options, "workspace", "new", existingWorkspace) |
209 |
| -// require.NoError(t, err) |
210 |
| -// } |
211 |
| -// // Switch to the specified workspace |
212 |
| -// _, err = RunTerraformCommandE(t, options, "workspace", "select", testCase.initialState.current) |
213 |
| -// require.NoError(t, err) |
214 |
| - |
215 |
| -// // Testing time, wooohoooo |
216 |
| -// gotResult, gotErr := WorkspaceDeleteE(t, options, testCase.toDeleteWorkspace) |
217 |
| - |
218 |
| -// // Check for errors |
219 |
| -// if testCase.expectedError != nil { |
220 |
| -// assert.True(t, errors.As(gotErr, &testCase.expectedError)) |
221 |
| -// } else { |
222 |
| -// assert.NoError(t, gotErr) |
223 |
| -// // Check for results |
224 |
| -// assert.Equal(t, testCase.expectedCurrent, gotResult) |
225 |
| -// assert.False(t, isExistingWorkspace(RunTerraformCommand(t, options, "workspace", "list"), testCase.toDeleteWorkspace)) |
226 |
| -// } |
227 |
| -// }) |
228 |
| - |
229 |
| -// } |
230 |
| -// } |
| 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 | +} |
0 commit comments