@@ -195,6 +195,97 @@ var _ = Describe("Workspace controller", Ordered, func() {
195195 return err == tfc .ErrResourceNotFound
196196 }).Should (BeTrue ())
197197 })
198+ It ("can destroy delete a workspace when the destroy was retried manually after failing" , func () {
199+ if cloudEndpoint != tfcDefaultAddress {
200+ Skip ("Does not run against TFC, skip this test" )
201+ }
202+ instance .Spec .AllowDestroyPlan = true
203+ instance .Spec .DeletionPolicy = appv1alpha2 .DeletionPolicyDestroy
204+ createWorkspace (instance )
205+ workspaceID := instance .Status .WorkspaceID
206+
207+ cv := createAndUploadConfigurationVersion (instance .Status .WorkspaceID , "hoi" )
208+ Eventually (func () bool {
209+ listOpts := tfc.ListOptions {
210+ PageNumber : 1 ,
211+ PageSize : maxPageSize ,
212+ }
213+ for listOpts .PageNumber != 0 {
214+ runs , err := tfClient .Runs .List (ctx , workspaceID , & tfc.RunListOptions {
215+ ListOptions : listOpts ,
216+ })
217+ Expect (err ).To (Succeed ())
218+ for _ , r := range runs .Items {
219+ if r .ConfigurationVersion .ID == cv .ID {
220+ return r .Status == tfc .RunApplied
221+ }
222+ }
223+ listOpts .PageNumber = runs .NextPage
224+ }
225+ return false
226+ }).Should (BeTrue ())
227+
228+ // create an errored ConfigurationVersion for the delete to fail
229+ cv = createAndUploadErroredConfigurationVersion (instance .Status .WorkspaceID , false )
230+
231+ Expect (k8sClient .Delete (ctx , instance )).To (Succeed ())
232+
233+ var destroyRunID string
234+ Eventually (func () bool {
235+ ws , err := tfClient .Workspaces .ReadByID (ctx , workspaceID )
236+ Expect (err ).To (Succeed ())
237+ Expect (ws ).ToNot (BeNil ())
238+ Expect (ws .CurrentRun ).ToNot (BeNil ())
239+ run , err := tfClient .Runs .Read (ctx , ws .CurrentRun .ID )
240+ Expect (err ).To (Succeed ())
241+ Expect (run ).ToNot (BeNil ())
242+ destroyRunID = run .ID
243+
244+ return run .IsDestroy
245+ }).Should (BeTrue ())
246+
247+ Eventually (func () bool {
248+ run , _ := tfClient .Runs .Read (ctx , destroyRunID )
249+ if run .Status == tfc .RunErrored {
250+ return true
251+ }
252+
253+ return false
254+ }).Should (BeTrue ())
255+
256+ // put back a working configuration
257+ cv = createAndUploadConfigurationVersion (instance .Status .WorkspaceID , "hoi" )
258+
259+ // start a new destroy run manually
260+ run , err := tfClient .Runs .Create (ctx , tfc.RunCreateOptions {
261+ IsDestroy : tfc .Bool (true ),
262+ Message : tfc .String (runMessage ),
263+ Workspace : & tfc.Workspace {
264+ ID : workspaceID ,
265+ },
266+ })
267+ Expect (err ).To (Succeed ())
268+ Expect (run ).ToNot (BeNil ())
269+
270+ var newDestroyRunID string
271+ Eventually (func () bool {
272+ ws , err := tfClient .Workspaces .ReadByID (ctx , workspaceID )
273+ Expect (err ).To (Succeed ())
274+ Expect (ws ).ToNot (BeNil ())
275+ Expect (ws .CurrentRun ).ToNot (BeNil ())
276+ run , err := tfClient .Runs .Read (ctx , ws .CurrentRun .ID )
277+ Expect (err ).To (Succeed ())
278+ Expect (run ).ToNot (BeNil ())
279+ newDestroyRunID = run .ID
280+
281+ return run .IsDestroy && newDestroyRunID != destroyRunID
282+ }).Should (BeTrue ())
283+
284+ Eventually (func () bool {
285+ _ , err := tfClient .Workspaces .ReadByID (ctx , workspaceID )
286+ return err == tfc .ErrResourceNotFound
287+ }).Should (BeTrue ())
288+ })
198289 It ("can force delete a workspace" , func () {
199290 instance .Spec .DeletionPolicy = appv1alpha2 .DeletionPolicyForce
200291 createWorkspace (instance )
0 commit comments