@@ -7,6 +7,7 @@ package wsmanager
7
7
import (
8
8
"context"
9
9
"encoding/json"
10
+ "errors"
10
11
"fmt"
11
12
"net/rpc"
12
13
"path/filepath"
@@ -48,7 +49,7 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
48
49
CheckoutLocation : "empty" ,
49
50
WorkspaceRoot : "/workspace/empty" ,
50
51
Task : []gitpod.TasksItems {
51
- {Init : "echo \" some output\" > someFile; sleep 20 ; exit 0;" },
52
+ {Init : "echo \" some output\" > someFile; sleep 10 ; exit 0;" },
52
53
},
53
54
},
54
55
{
@@ -57,7 +58,7 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
57
58
CheckoutLocation : "empty" ,
58
59
WorkspaceRoot : "/workspace/empty" ,
59
60
Task : []gitpod.TasksItems {
60
- {Init : "echo \" some output\" > someFile; sleep 20 ; exit 0;" },
61
+ {Init : "echo \" some output\" > someFile; sleep 10 ; exit 0;" },
61
62
},
62
63
FF : []wsmanapi.WorkspaceFeatureFlag {wsmanapi .WorkspaceFeatureFlag_PERSISTENT_VOLUME_CLAIM },
63
64
},
@@ -74,7 +75,7 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
74
75
75
76
// TODO: change to use server API to launch the workspace, so we could run the integration test as the user code flow
76
77
// 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 {
78
79
req .Type = wsmanapi .WorkspaceType_PREBUILD
79
80
80
81
tasks , err := json .Marshal (test .Task )
@@ -103,17 +104,24 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
103
104
t .Fatalf ("cannot launch a workspace: %q" , err )
104
105
}
105
106
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 {
114
109
t .Errorf ("cannot stop workspace: %q" , err )
115
110
}
116
111
}()
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
+ }
117
125
})
118
126
}
119
127
return ctx
@@ -124,8 +132,6 @@ func TestPrebuildWorkspaceTaskSuccess(t *testing.T) {
124
132
}
125
133
126
134
func TestPrebuildWorkspaceTaskFail (t * testing.T ) {
127
- t .Skip ("status never returns HeadlessTaskFailed (exit 1)" )
128
-
129
135
f := features .New ("prebuild" ).
130
136
WithLabel ("component" , "server" ).
131
137
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) {
141
147
req .Type = wsmanapi .WorkspaceType_PREBUILD
142
148
req .Spec .Envvars = append (req .Spec .Envvars , & wsmanapi.EnvironmentVariable {
143
149
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;" }]` ,
145
151
})
146
152
return nil
147
153
}))
@@ -150,15 +156,9 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
150
156
}
151
157
152
158
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 )
162
162
}
163
163
}()
164
164
@@ -173,7 +173,7 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
173
173
return true
174
174
})
175
175
if err != nil {
176
- t .Fatalf ("cannot start workspace: %q" , err )
176
+ t .Fatalf ("failed for wait workspace stop : %q" , err )
177
177
}
178
178
179
179
return ctx
@@ -186,7 +186,7 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
186
186
const (
187
187
prebuildLogPath string = "/workspace/.gitpod"
188
188
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 ;"
190
190
regularPrefix string = "ws-"
191
191
)
192
192
@@ -256,7 +256,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
256
256
// create a prebuild and stop workspace
257
257
// TODO: change to use server API to launch the workspace, so we could run the integration test as the user code flow
258
258
// 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 {
260
260
req .Type = wsmanapi .WorkspaceType_PREBUILD
261
261
req .Spec .Envvars = append (req .Spec .Envvars , & wsmanapi.EnvironmentVariable {
262
262
Name : "GITPOD_TASKS" ,
@@ -278,8 +278,13 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
278
278
if err != nil {
279
279
t .Fatalf ("cannot launch a workspace: %q" , err )
280
280
}
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 )
283
288
if err != nil {
284
289
t .Fatalf ("stop workspace and find snapshot error: %v" , err )
285
290
}
@@ -551,7 +556,7 @@ func TestPrebuildAndRegularWorkspaceDifferentWorkspaceClass(t *testing.T) {
551
556
})
552
557
553
558
// 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 {
555
560
req .Type = wsmanapi .WorkspaceType_PREBUILD
556
561
req .Spec .Class = test .PrebuildWorkspaceClass
557
562
req .Spec .Envvars = append (req .Spec .Envvars , & wsmanapi.EnvironmentVariable {
@@ -574,8 +579,14 @@ func TestPrebuildAndRegularWorkspaceDifferentWorkspaceClass(t *testing.T) {
574
579
if err != nil {
575
580
t .Fatalf ("cannot launch a workspace: %q" , err )
576
581
}
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
+ }()
577
588
578
- prebuildSnapshot , vsInfo , err := stopWorkspaceAndFindSnapshot ( prebuildStopWs , api )
589
+ prebuildSnapshot , vsInfo , err := watchStopWorkspaceAndFindSnapshot ( ctx , ws . Req . Id , api )
579
590
if err != nil {
580
591
t .Fatalf ("stop workspace and find snapshot error: %v" , err )
581
592
}
@@ -821,11 +832,14 @@ func findVolumeSnapshot(ctx context.Context, isPVCEnable bool, instanceID, names
821
832
return vsInfo , nil
822
833
}
823
834
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 )
826
837
if err != nil {
827
838
return "" , nil , err
828
839
}
840
+ if lastStatus .Conditions .HeadlessTaskFailed != "" {
841
+ return "" , nil , errors .New ("unexpected HeadlessTaskFailed condition" )
842
+ }
829
843
if lastStatus == nil || lastStatus .Conditions == nil || lastStatus .Conditions .VolumeSnapshot == nil {
830
844
return "" , nil , nil
831
845
}
0 commit comments