From 6c669f75f28eda225b4a99355bc86b02a59bc252 Mon Sep 17 00:00:00 2001 From: utam0k Date: Thu, 15 Sep 2022 11:42:49 +0900 Subject: [PATCH] test: Add a handling if the workspace already has gone when try to delete it --- test/pkg/integration/workspace.go | 136 +++++++++++++++--------------- 1 file changed, 70 insertions(+), 66 deletions(-) diff --git a/test/pkg/integration/workspace.go b/test/pkg/integration/workspace.go index 04460c12612712..41c4fea9c97ae7 100644 --- a/test/pkg/integration/workspace.go +++ b/test/pkg/integration/workspace.go @@ -301,41 +301,52 @@ func stopWsF(t *testing.T, instanceID string, api *ComponentAPI) stopWorkspaceFu break } - if !waitForStop { - wm, err := api.WorkspaceManager() - if err != nil { - return nil, err - } + wm, err := api.WorkspaceManager() + if err != nil { + return nil, err + } - dr, err := wm.DescribeWorkspace(sctx, &wsmanapi.DescribeWorkspaceRequest{ - Id: instanceID, - }) - if err != nil { + dr, err := wm.DescribeWorkspace(sctx, &wsmanapi.DescribeWorkspaceRequest{ + Id: instanceID, + }) + if err != nil { + if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound { + t.Log("the workspace is already gone") + return &wsmanapi.WorkspaceStatus{ + Id: instanceID, + Phase: wsmanapi.WorkspacePhase_STOPPED, + }, nil + } else if err != nil { return nil, err } - s, ok := status.FromError(err) - if ok && s.Code() == codes.NotFound { - return nil, err - } - return dr.Status, err + } + + if !waitForStop { + return dr.Status, nil } var lastStatus *wsmanapi.WorkspaceStatus for { t.Logf("waiting for stopping the workspace: %s", instanceID) lastStatus, err = WaitForWorkspaceStop(sctx, api, instanceID) - if st, ok := status.FromError(err); ok && st.Code() == codes.Unavailable { - api.ClearWorkspaceManagerClientCache() - t.Logf("got %v during waiting for stopping the workspace", st) - time.Sleep(5 * time.Second) - continue + if st, ok := status.FromError(err); ok { + switch st.Code() { + case codes.Unavailable: + api.ClearWorkspaceManagerClientCache() + t.Logf("got %v during waiting for stopping the workspace", st) + time.Sleep(5 * time.Second) + continue + case codes.NotFound: + t.Log("the workspace is already gone") + return lastStatus, nil + } } else if err != nil { return lastStatus, err } break } - return lastStatus, err + return lastStatus, nil } } @@ -414,11 +425,7 @@ func WaitForWorkspaceStart(ctx context.Context, instanceID string, api *Componen } s = resp.GetStatus() - if s == nil { - time.Sleep(10 * time.Second) - continue - } - if s.Id != instanceID { + if s == nil || s.Id != instanceID { time.Sleep(10 * time.Second) continue } @@ -434,13 +441,8 @@ func WaitForWorkspaceStart(ctx context.Context, instanceID string, api *Componen if s.Conditions.Failed != "" { errStatus <- xerrors.Errorf("workspace instance %s failed: %s", instanceID, s.Conditions.Failed) return - } - if s.Phase == wsmanapi.WorkspacePhase_STOPPING { - errStatus <- xerrors.Errorf("workspace instance %s is stopping", instanceID) - return - } - if s.Phase == wsmanapi.WorkspacePhase_STOPPED { - errStatus <- xerrors.Errorf("workspace instance %s has stopped", instanceID) + } else if s.Phase == wsmanapi.WorkspacePhase_STOPPING || s.Phase == wsmanapi.WorkspacePhase_STOPPED { + errStatus <- xerrors.Errorf("workspace instance %s is %s", instanceID, s.Phase) return } } @@ -490,6 +492,13 @@ func WaitForWorkspaceStop(ctx context.Context, api *ComponentAPI, instanceID str return nil, xerrors.Errorf("cannot listen for workspace updates: %q", err) } + _, err = wsman.DescribeWorkspace(ctx, &wsmanapi.DescribeWorkspaceRequest{ + Id: instanceID, + }) + if err != nil { + return nil, err + } + sub, err := wsman.Subscribe(ctx, &wsmanapi.SubscribeRequest{}) if err != nil { return nil, xerrors.Errorf("cannot listen for workspace updates: %q", err) @@ -500,55 +509,50 @@ func WaitForWorkspaceStop(ctx context.Context, api *ComponentAPI, instanceID str done := make(chan *wsmanapi.WorkspaceStatus) errCh := make(chan error) + resetSubscriber := func(subscriber wsmanapi.WorkspaceManager_SubscribeClient, sapi *ComponentAPI) (wsmanapi.WorkspaceManager_SubscribeClient, error) { + subscriber.CloseSend() + sapi.ClearWorkspaceManagerClientCache() + wsman, err := sapi.WorkspaceManager() + if err != nil { + return nil, xerrors.Errorf("cannot listen for workspace updates: %w", err) + } + new_sub, err := wsman.Subscribe(ctx, &wsmanapi.SubscribeRequest{}) + if err != nil { + return nil, xerrors.Errorf("cannot listen for workspace updates: %w", err) + } + return new_sub, nil + } + go func() { - var s *wsmanapi.WorkspaceStatus + var wss *wsmanapi.WorkspaceStatus defer func() { - done <- s + done <- wss close(done) }() for { resp, err := sub.Recv() if err != nil { - if st, ok := status.FromError(err); ok && st.Code() == codes.Unavailable { + if s, ok := status.FromError(err); ok && s.Code() == codes.Unavailable { time.Sleep(10 * time.Second) - sub.CloseSend() - api.ClearWorkspaceManagerClientCache() - wsman, err := api.WorkspaceManager() - if err != nil { - errCh <- xerrors.Errorf("cannot listen for workspace updates: %w", err) - return + sub, err = resetSubscriber(sub, api) + if err == nil { + continue } - sub, err = wsman.Subscribe(ctx, &wsmanapi.SubscribeRequest{}) - if err != nil { - errCh <- xerrors.Errorf("cannot listen for workspace updates: %w", err) - return - } - time.Sleep(10 * time.Second) - continue } errCh <- xerrors.Errorf("workspace update error: %q", err) return } - s = resp.GetStatus() - if s == nil { - continue - } - if s.Id != instanceID { - continue - } - - if s.Conditions.Failed != "" { - // TODO(toru): we have to fix https://github.com/gitpod-io/gitpod/issues/12021 - 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" { - lastStatus = s + if wss = resp.GetStatus(); wss != nil && wss.Id == instanceID { + if wss.Conditions.Failed != "" { + // TODO(toru): we have to fix https://github.com/gitpod-io/gitpod/issues/12021 + 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" { + errCh <- xerrors.Errorf("workspace instance %s failed: %s", instanceID, wss.Conditions.Failed) + } + return + } + if wss.Phase == wsmanapi.WorkspacePhase_STOPPED { return } - errCh <- xerrors.Errorf("workspace instance %s failed: %s", instanceID, s.Conditions.Failed) - return - } - if s.Phase == wsmanapi.WorkspacePhase_STOPPED { - lastStatus = s - return } time.Sleep(10 * time.Second)