Skip to content

Commit 6c669f7

Browse files
utam0kroboquat
authored andcommitted
test: Add a handling if the workspace already has gone when try to delete it
1 parent 312b6b8 commit 6c669f7

File tree

1 file changed

+70
-66
lines changed

1 file changed

+70
-66
lines changed

test/pkg/integration/workspace.go

Lines changed: 70 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -301,41 +301,52 @@ func stopWsF(t *testing.T, instanceID string, api *ComponentAPI) stopWorkspaceFu
301301
break
302302
}
303303

304-
if !waitForStop {
305-
wm, err := api.WorkspaceManager()
306-
if err != nil {
307-
return nil, err
308-
}
304+
wm, err := api.WorkspaceManager()
305+
if err != nil {
306+
return nil, err
307+
}
309308

310-
dr, err := wm.DescribeWorkspace(sctx, &wsmanapi.DescribeWorkspaceRequest{
311-
Id: instanceID,
312-
})
313-
if err != nil {
309+
dr, err := wm.DescribeWorkspace(sctx, &wsmanapi.DescribeWorkspaceRequest{
310+
Id: instanceID,
311+
})
312+
if err != nil {
313+
if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound {
314+
t.Log("the workspace is already gone")
315+
return &wsmanapi.WorkspaceStatus{
316+
Id: instanceID,
317+
Phase: wsmanapi.WorkspacePhase_STOPPED,
318+
}, nil
319+
} else if err != nil {
314320
return nil, err
315321
}
316322

317-
s, ok := status.FromError(err)
318-
if ok && s.Code() == codes.NotFound {
319-
return nil, err
320-
}
321-
return dr.Status, err
323+
}
324+
325+
if !waitForStop {
326+
return dr.Status, nil
322327
}
323328

324329
var lastStatus *wsmanapi.WorkspaceStatus
325330
for {
326331
t.Logf("waiting for stopping the workspace: %s", instanceID)
327332
lastStatus, err = WaitForWorkspaceStop(sctx, api, instanceID)
328-
if st, ok := status.FromError(err); ok && st.Code() == codes.Unavailable {
329-
api.ClearWorkspaceManagerClientCache()
330-
t.Logf("got %v during waiting for stopping the workspace", st)
331-
time.Sleep(5 * time.Second)
332-
continue
333+
if st, ok := status.FromError(err); ok {
334+
switch st.Code() {
335+
case codes.Unavailable:
336+
api.ClearWorkspaceManagerClientCache()
337+
t.Logf("got %v during waiting for stopping the workspace", st)
338+
time.Sleep(5 * time.Second)
339+
continue
340+
case codes.NotFound:
341+
t.Log("the workspace is already gone")
342+
return lastStatus, nil
343+
}
333344
} else if err != nil {
334345
return lastStatus, err
335346
}
336347
break
337348
}
338-
return lastStatus, err
349+
return lastStatus, nil
339350
}
340351
}
341352

@@ -414,11 +425,7 @@ func WaitForWorkspaceStart(ctx context.Context, instanceID string, api *Componen
414425
}
415426

416427
s = resp.GetStatus()
417-
if s == nil {
418-
time.Sleep(10 * time.Second)
419-
continue
420-
}
421-
if s.Id != instanceID {
428+
if s == nil || s.Id != instanceID {
422429
time.Sleep(10 * time.Second)
423430
continue
424431
}
@@ -434,13 +441,8 @@ func WaitForWorkspaceStart(ctx context.Context, instanceID string, api *Componen
434441
if s.Conditions.Failed != "" {
435442
errStatus <- xerrors.Errorf("workspace instance %s failed: %s", instanceID, s.Conditions.Failed)
436443
return
437-
}
438-
if s.Phase == wsmanapi.WorkspacePhase_STOPPING {
439-
errStatus <- xerrors.Errorf("workspace instance %s is stopping", instanceID)
440-
return
441-
}
442-
if s.Phase == wsmanapi.WorkspacePhase_STOPPED {
443-
errStatus <- xerrors.Errorf("workspace instance %s has stopped", instanceID)
444+
} else if s.Phase == wsmanapi.WorkspacePhase_STOPPING || s.Phase == wsmanapi.WorkspacePhase_STOPPED {
445+
errStatus <- xerrors.Errorf("workspace instance %s is %s", instanceID, s.Phase)
444446
return
445447
}
446448
}
@@ -490,6 +492,13 @@ func WaitForWorkspaceStop(ctx context.Context, api *ComponentAPI, instanceID str
490492
return nil, xerrors.Errorf("cannot listen for workspace updates: %q", err)
491493
}
492494

495+
_, err = wsman.DescribeWorkspace(ctx, &wsmanapi.DescribeWorkspaceRequest{
496+
Id: instanceID,
497+
})
498+
if err != nil {
499+
return nil, err
500+
}
501+
493502
sub, err := wsman.Subscribe(ctx, &wsmanapi.SubscribeRequest{})
494503
if err != nil {
495504
return nil, xerrors.Errorf("cannot listen for workspace updates: %q", err)
@@ -500,55 +509,50 @@ func WaitForWorkspaceStop(ctx context.Context, api *ComponentAPI, instanceID str
500509

501510
done := make(chan *wsmanapi.WorkspaceStatus)
502511
errCh := make(chan error)
512+
resetSubscriber := func(subscriber wsmanapi.WorkspaceManager_SubscribeClient, sapi *ComponentAPI) (wsmanapi.WorkspaceManager_SubscribeClient, error) {
513+
subscriber.CloseSend()
514+
sapi.ClearWorkspaceManagerClientCache()
515+
wsman, err := sapi.WorkspaceManager()
516+
if err != nil {
517+
return nil, xerrors.Errorf("cannot listen for workspace updates: %w", err)
518+
}
519+
new_sub, err := wsman.Subscribe(ctx, &wsmanapi.SubscribeRequest{})
520+
if err != nil {
521+
return nil, xerrors.Errorf("cannot listen for workspace updates: %w", err)
522+
}
523+
return new_sub, nil
524+
}
525+
503526
go func() {
504-
var s *wsmanapi.WorkspaceStatus
527+
var wss *wsmanapi.WorkspaceStatus
505528
defer func() {
506-
done <- s
529+
done <- wss
507530
close(done)
508531
}()
509532
for {
510533
resp, err := sub.Recv()
511534
if err != nil {
512-
if st, ok := status.FromError(err); ok && st.Code() == codes.Unavailable {
535+
if s, ok := status.FromError(err); ok && s.Code() == codes.Unavailable {
513536
time.Sleep(10 * time.Second)
514-
sub.CloseSend()
515-
api.ClearWorkspaceManagerClientCache()
516-
wsman, err := api.WorkspaceManager()
517-
if err != nil {
518-
errCh <- xerrors.Errorf("cannot listen for workspace updates: %w", err)
519-
return
537+
sub, err = resetSubscriber(sub, api)
538+
if err == nil {
539+
continue
520540
}
521-
sub, err = wsman.Subscribe(ctx, &wsmanapi.SubscribeRequest{})
522-
if err != nil {
523-
errCh <- xerrors.Errorf("cannot listen for workspace updates: %w", err)
524-
return
525-
}
526-
time.Sleep(10 * time.Second)
527-
continue
528541
}
529542
errCh <- xerrors.Errorf("workspace update error: %q", err)
530543
return
531544
}
532-
s = resp.GetStatus()
533-
if s == nil {
534-
continue
535-
}
536-
if s.Id != instanceID {
537-
continue
538-
}
539-
540-
if s.Conditions.Failed != "" {
541-
// TODO(toru): we have to fix https://github.com/gitpod-io/gitpod/issues/12021
542-
if s.Conditions.Failed == "The container could not be located when the pod was deleted. The container used to be Running" || s.Conditions.Failed == "The container could not be located when the pod was terminated" {
543-
lastStatus = s
545+
if wss = resp.GetStatus(); wss != nil && wss.Id == instanceID {
546+
if wss.Conditions.Failed != "" {
547+
// TODO(toru): we have to fix https://github.com/gitpod-io/gitpod/issues/12021
548+
if wss.Conditions.Failed != "The container could not be located when the pod was deleted. The container used to be Running" && wss.Conditions.Failed != "The container could not be located when the pod was terminated" {
549+
errCh <- xerrors.Errorf("workspace instance %s failed: %s", instanceID, wss.Conditions.Failed)
550+
}
551+
return
552+
}
553+
if wss.Phase == wsmanapi.WorkspacePhase_STOPPED {
544554
return
545555
}
546-
errCh <- xerrors.Errorf("workspace instance %s failed: %s", instanceID, s.Conditions.Failed)
547-
return
548-
}
549-
if s.Phase == wsmanapi.WorkspacePhase_STOPPED {
550-
lastStatus = s
551-
return
552556
}
553557

554558
time.Sleep(10 * time.Second)

0 commit comments

Comments
 (0)