Skip to content

Commit

Permalink
test: Add a handling if the workspace already has gone when try to de…
Browse files Browse the repository at this point in the history
…lete it
  • Loading branch information
utam0k authored and roboquat committed Sep 15, 2022
1 parent 312b6b8 commit 6c669f7
Showing 1 changed file with 70 additions and 66 deletions.
136 changes: 70 additions & 66 deletions test/pkg/integration/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 6c669f7

Please sign in to comment.