Skip to content

Commit 8e8eedc

Browse files
committed
[integration-test] make workspace integtation test pass
1 parent 4ae897e commit 8e8eedc

File tree

1 file changed

+46
-32
lines changed

1 file changed

+46
-32
lines changed

test/tests/components/ws-manager/prebuild_test.go

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package wsmanager
77
import (
88
"context"
99
"encoding/json"
10+
"errors"
1011
"fmt"
1112
"net/rpc"
1213
"path/filepath"
@@ -48,7 +49,7 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
4849
CheckoutLocation: "empty",
4950
WorkspaceRoot: "/workspace/empty",
5051
Task: []gitpod.TasksItems{
51-
{Init: "echo \"some output\" > someFile; sleep 20; exit 0;"},
52+
{Init: "echo \"some output\" > someFile; sleep 10; exit 0;"},
5253
},
5354
},
5455
{
@@ -57,7 +58,7 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
5758
CheckoutLocation: "empty",
5859
WorkspaceRoot: "/workspace/empty",
5960
Task: []gitpod.TasksItems{
60-
{Init: "echo \"some output\" > someFile; sleep 20; exit 0;"},
61+
{Init: "echo \"some output\" > someFile; sleep 10; exit 0;"},
6162
},
6263
FF: []wsmanapi.WorkspaceFeatureFlag{wsmanapi.WorkspaceFeatureFlag_PERSISTENT_VOLUME_CLAIM},
6364
},
@@ -74,7 +75,7 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
7475

7576
// TODO: change to use server API to launch the workspace, so we could run the integration test as the user code flow
7677
// which is client -> server -> ws-manager rather than client -> ws-manager directly
77-
_, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
78+
ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
7879
req.Type = wsmanapi.WorkspaceType_PREBUILD
7980

8081
tasks, err := json.Marshal(test.Task)
@@ -103,17 +104,24 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
103104
t.Fatalf("cannot launch a workspace: %q", err)
104105
}
105106
defer func() {
106-
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
107-
defer scancel()
108-
109-
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
110-
defer sapi.Done(t)
111-
112-
_, err = stopWs(true, sapi)
113-
if err != nil {
107+
// stop workspace in defer function to prevent we forget to stop the workspace
108+
if err := stopWorkspace(t, cfg, stopWs); err != nil {
114109
t.Errorf("cannot stop workspace: %q", err)
115110
}
116111
}()
112+
_, err = integration.WaitForWorkspace(ctx, api, ws.Req.Id, func(status *wsmanapi.WorkspaceStatus) bool {
113+
if status.Phase != wsmanapi.WorkspacePhase_STOPPED {
114+
return false
115+
}
116+
if status.Conditions.HeadlessTaskFailed != "" {
117+
t.Logf("Conditions: %v", status.Conditions)
118+
t.Fatal("unexpected HeadlessTaskFailed condition")
119+
}
120+
return true
121+
})
122+
if err != nil {
123+
t.Fatalf("failed for wait workspace stop: %q", err)
124+
}
117125
})
118126
}
119127
return ctx
@@ -124,8 +132,6 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
124132
}
125133

126134
func TestPrebuildWorkspaceTaskFail(t *testing.T) {
127-
t.Skip("status never returns HeadlessTaskFailed (exit 1)")
128-
129135
f := features.New("prebuild").
130136
WithLabel("component", "server").
131137
Assess("it should create a prebuild and fail after running the defined tasks", func(_ context.Context, t *testing.T, cfg *envconf.Config) context.Context {
@@ -141,7 +147,7 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
141147
req.Type = wsmanapi.WorkspaceType_PREBUILD
142148
req.Spec.Envvars = append(req.Spec.Envvars, &wsmanapi.EnvironmentVariable{
143149
Name: "GITPOD_TASKS",
144-
Value: `[{ "init": "echo \"some output\" > someFile; sleep 20; exit 1;" }]`,
150+
Value: `[{ "init": "echo \"some output\" > someFile; sleep 10; exit 1;" }]`,
145151
})
146152
return nil
147153
}))
@@ -150,15 +156,9 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
150156
}
151157

152158
defer func() {
153-
sctx, scancel := context.WithTimeout(context.Background(), 5*time.Minute)
154-
defer scancel()
155-
156-
sapi := integration.NewComponentAPI(sctx, cfg.Namespace(), kubeconfig, cfg.Client())
157-
defer sapi.Done(t)
158-
159-
_, err = stopWs(true, sapi)
160-
if err != nil {
161-
t.Fatal(err)
159+
// stop workspace in defer function to prevent we forget to stop the workspace
160+
if err := stopWorkspace(t, cfg, stopWs); err != nil {
161+
t.Errorf("cannot stop workspace: %q", err)
162162
}
163163
}()
164164

@@ -173,7 +173,7 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
173173
return true
174174
})
175175
if err != nil {
176-
t.Fatalf("cannot start workspace: %q", err)
176+
t.Fatalf("failed for wait workspace stop: %q", err)
177177
}
178178

179179
return ctx
@@ -186,7 +186,7 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
186186
const (
187187
prebuildLogPath string = "/workspace/.gitpod"
188188
prebuildLog string = "'🤙 This task ran as a workspace prebuild'"
189-
initTask string = "echo \"some output\" > someFile; sleep 90;"
189+
initTask string = "echo \"some output\" > someFile; sleep 10;"
190190
regularPrefix string = "ws-"
191191
)
192192

@@ -256,7 +256,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
256256
// create a prebuild and stop workspace
257257
// TODO: change to use server API to launch the workspace, so we could run the integration test as the user code flow
258258
// which is client -> server -> ws-manager rather than client -> ws-manager directly
259-
_, prebuildStopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
259+
ws, prebuildStopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
260260
req.Type = wsmanapi.WorkspaceType_PREBUILD
261261
req.Spec.Envvars = append(req.Spec.Envvars, &wsmanapi.EnvironmentVariable{
262262
Name: "GITPOD_TASKS",
@@ -278,8 +278,13 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
278278
if err != nil {
279279
t.Fatalf("cannot launch a workspace: %q", err)
280280
}
281-
282-
prebuildSnapshot, prebuildVSInfo, err := stopWorkspaceAndFindSnapshot(prebuildStopWs, api)
281+
defer func() {
282+
// stop workspace in defer function to prevent we forget to stop the workspace
283+
if err := stopWorkspace(t, cfg, prebuildStopWs); err != nil {
284+
t.Errorf("cannot stop workspace: %q", err)
285+
}
286+
}()
287+
prebuildSnapshot, prebuildVSInfo, err := watchStopWorkspaceAndFindSnapshot(ctx, ws.Req.Id, api)
283288
if err != nil {
284289
t.Fatalf("stop workspace and find snapshot error: %v", err)
285290
}
@@ -551,7 +556,7 @@ func TestPrebuildAndRegularWorkspaceDifferentWorkspaceClass(t *testing.T) {
551556
})
552557

553558
// create a prebuild and stop workspace
554-
_, prebuildStopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
559+
ws, prebuildStopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
555560
req.Type = wsmanapi.WorkspaceType_PREBUILD
556561
req.Spec.Class = test.PrebuildWorkspaceClass
557562
req.Spec.Envvars = append(req.Spec.Envvars, &wsmanapi.EnvironmentVariable{
@@ -574,8 +579,14 @@ func TestPrebuildAndRegularWorkspaceDifferentWorkspaceClass(t *testing.T) {
574579
if err != nil {
575580
t.Fatalf("cannot launch a workspace: %q", err)
576581
}
582+
defer func() {
583+
// stop workspace in defer function to prevent we forget to stop the workspace
584+
if err := stopWorkspace(t, cfg, prebuildStopWs); err != nil {
585+
t.Errorf("cannot stop workspace: %q", err)
586+
}
587+
}()
577588

578-
prebuildSnapshot, vsInfo, err := stopWorkspaceAndFindSnapshot(prebuildStopWs, api)
589+
prebuildSnapshot, vsInfo, err := watchStopWorkspaceAndFindSnapshot(ctx, ws.Req.Id, api)
579590
if err != nil {
580591
t.Fatalf("stop workspace and find snapshot error: %v", err)
581592
}
@@ -821,11 +832,14 @@ func findVolumeSnapshot(ctx context.Context, isPVCEnable bool, instanceID, names
821832
return vsInfo, nil
822833
}
823834

824-
func stopWorkspaceAndFindSnapshot(StopWorkspaceFunc integration.StopWorkspaceFunc, api *integration.ComponentAPI) (string, *wsmanapi.VolumeSnapshotInfo, error) {
825-
lastStatus, err := StopWorkspaceFunc(true, api)
835+
func watchStopWorkspaceAndFindSnapshot(ctx context.Context, instanceId string, api *integration.ComponentAPI) (string, *wsmanapi.VolumeSnapshotInfo, error) {
836+
lastStatus, err := integration.WaitForWorkspaceStop(ctx, api, instanceId)
826837
if err != nil {
827838
return "", nil, err
828839
}
840+
if lastStatus.Conditions.HeadlessTaskFailed != "" {
841+
return "", nil, errors.New("unexpected HeadlessTaskFailed condition")
842+
}
829843
if lastStatus == nil || lastStatus.Conditions == nil || lastStatus.Conditions.VolumeSnapshot == nil {
830844
return "", nil, nil
831845
}

0 commit comments

Comments
 (0)