@@ -180,8 +180,22 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
180
180
const (
181
181
prebuildLogPath string = "/workspace/.gitpod"
182
182
prebuildLog string = "'🤙 This task ran as a workspace prebuild'"
183
+ initTask string = "echo \" some output\" > someFile; sleep 90;"
183
184
)
184
185
186
+ // TestOpenWorkspaceFromPrebuild
187
+ // - create a prebuild
188
+ // - open the workspace from prebuild
189
+ // - make sure the .git/ folder with correct permission
190
+ // - make sure either one of the condition mets
191
+ // - the prebuild log message exists
192
+ // - the init task message exists
193
+ // - the init task generated file exists
194
+ //
195
+ // - write a new file foobar.txt
196
+ // - stop the workspace
197
+ // - relaunch the workspace
198
+ // - make sure the file foobar.txt exists
185
199
func TestOpenWorkspaceFromPrebuild (t * testing.T ) {
186
200
f := features .New ("prebuild" ).
187
201
WithLabel ("component" , "ws-manager" ).
@@ -223,7 +237,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
223
237
req .Type = wsmanapi .WorkspaceType_PREBUILD
224
238
req .Spec .Envvars = append (req .Spec .Envvars , & wsmanapi.EnvironmentVariable {
225
239
Name : "GITPOD_TASKS" ,
226
- Value : `[{ "init": "echo \"some output\" > someFile; sleep 60; exit 0;" }]` ,
240
+ Value : fmt . Sprintf ( `[{ "init": %q }]` , initTask ) ,
227
241
})
228
242
req .Spec .FeatureFlags = test .FF
229
243
req .Spec .Initializer = & csapi.WorkspaceInitializer {
@@ -248,6 +262,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
248
262
if err != nil {
249
263
t .Fatalf ("stop workspace and find snapshot error: %v" , err )
250
264
}
265
+ t .Logf ("prebuild snapshot: %s, vsName: %s, vsHandle: %s" , prebuildSnapshot , vsInfo .VolumeSnapshotName , vsInfo .VolumeSnapshotHandle )
251
266
252
267
// launch the workspace from prebuild
253
268
ws , stopWs , err := integration .LaunchWorkspaceDirectly (t , ctx , api , integration .WithRequestModifier (func (req * wsmanapi.StartWorkspaceRequest ) error {
@@ -298,8 +313,11 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
298
313
integration .DeferCloser (t , closer )
299
314
300
315
// check prebuild log message exists
301
- var resp agent.ExecResponse
302
- var checkPrebuildSuccess bool
316
+ // since the message '🤙 This task ran as a workspace prebuild' is generated by
317
+ // a prebuild workspace supervisor, so we add a retry mechanism to make sure that we
318
+ // won't check the message too earlier before the supervisor generated it.
319
+ var grepResp agent.ExecResponse
320
+ var checkPrebuildLog bool
303
321
for i := 0 ; i < 10 ; i ++ {
304
322
err = rsa .Call ("WorkspaceAgent.Exec" , & agent.ExecRequest {
305
323
Dir : prebuildLogPath ,
@@ -308,26 +326,89 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
308
326
"-c" ,
309
327
fmt .Sprintf ("grep %s *" , prebuildLog ),
310
328
},
311
- }, & resp )
312
- if err == nil && resp .ExitCode == 0 && strings .Trim (resp .Stdout , " \t \n " ) != "" {
313
- checkPrebuildSuccess = true
329
+ }, & grepResp )
330
+ if err == nil && grepResp .ExitCode == 0 && strings .Trim (grepResp .Stdout , " \t \n " ) != "" {
331
+ checkPrebuildLog = true
314
332
break
315
333
}
316
-
317
- // wait 3 seconds and check
334
+ t .Logf ("[%d] retry..." , i )
318
335
time .Sleep (3 * time .Second )
319
336
}
320
337
321
- if ! checkPrebuildSuccess {
322
- t .Fatalf ("cannot found the prebuild message %s in %s, err:%v, exitCode:%d, stdout:%s" , prebuildLog , prebuildLogPath , err , resp .ExitCode , resp .Stdout )
338
+ if ! checkPrebuildLog {
339
+ t .Logf ("cannot found the prebuild message %s in %s, err:%v, exitCode:%d, stdout:%s" , prebuildLog , prebuildLogPath , err , grepResp .ExitCode , grepResp .Stdout )
340
+
341
+ // somehow, the prebuild log message '🤙 This task ran as a workspace prebuild' does not exists
342
+ // we fall back to check the the init task message within the /workspace/.gitpod/prebuild-log-* or not
343
+ var grepResp agent.ExecResponse
344
+ var checkInitTaskMsg bool
345
+ err = rsa .Call ("WorkspaceAgent.Exec" , & agent.ExecRequest {
346
+ Dir : prebuildLogPath ,
347
+ Command : "bash" ,
348
+ Args : []string {
349
+ "-c" ,
350
+ fmt .Sprintf ("grep %q *" , initTask ),
351
+ },
352
+ }, & grepResp )
353
+ if err == nil && grepResp .ExitCode == 0 && strings .Trim (grepResp .Stdout , " \t \n " ) != "" {
354
+ checkInitTaskMsg = true
355
+ }
356
+
357
+ if ! checkInitTaskMsg {
358
+ t .Logf ("cannot found the init task message %s in %s, err:%v, exitCode:%d, stdout:%s" , initTask , prebuildLogPath , err , grepResp .ExitCode , grepResp .Stdout )
359
+
360
+ // somehow, the init task message does not exist within the /workspace/.gitpod/prebuild-log-*
361
+ // we fall back to check the file exists or not
362
+ var ls agent.ListDirResponse
363
+ err = rsa .Call ("WorkspaceAgent.ListDir" , & agent.ListDirRequest {
364
+ Dir : test .WorkspaceRoot ,
365
+ }, & ls )
366
+ if err != nil {
367
+ t .Fatal (err )
368
+ }
369
+
370
+ var found bool
371
+ for _ , f := range ls .Files {
372
+ if filepath .Base (f ) == "someFile" {
373
+ found = true
374
+ break
375
+ }
376
+ }
377
+ if ! found {
378
+ t .Fatal ("did not find someFile from previous workspace instance" )
379
+ }
380
+ }
381
+ }
382
+
383
+ // check the files/folders permission under .git/ is not root
384
+ var findUserResp agent.ExecResponse
385
+ var gitDir string = fmt .Sprintf ("%s/%s" , test .WorkspaceRoot , ".git" )
386
+ err = rsa .Call ("WorkspaceAgent.Exec" , & agent.ExecRequest {
387
+ Dir : gitDir ,
388
+ Command : "find" ,
389
+ Args : []string {"-user" , "root" },
390
+ }, & findUserResp )
391
+ if err != nil || findUserResp .ExitCode != 0 || strings .Trim (findUserResp .Stdout , " \t \n " ) != "" {
392
+ t .Fatalf ("incorrect file perimssion under %s folder, err:%v, exitCode:%d, stdout:%s" , gitDir , err , findUserResp .ExitCode , findUserResp .Stdout )
393
+ }
394
+
395
+ var findGroupResp agent.ExecResponse
396
+ err = rsa .Call ("WorkspaceAgent.Exec" , & agent.ExecRequest {
397
+ Dir : gitDir ,
398
+ Command : "find" ,
399
+ Args : []string {"-group" , "root" },
400
+ }, & findGroupResp )
401
+ if err != nil || findGroupResp .ExitCode != 0 || strings .Trim (findGroupResp .Stdout , " \t \n " ) != "" {
402
+ t .Fatalf ("incorrect group perimssion under %s folder, err:%v, exitCode:%d, stdout:%s" , gitDir , err , findGroupResp .ExitCode , findGroupResp .Stdout )
323
403
}
324
404
325
405
// write file foobar.txt and stop the workspace
406
+ var writeFileResp agent.ExecResponse
326
407
err = rsa .Call ("WorkspaceAgent.WriteFile" , & agent.WriteFileRequest {
327
408
Path : fmt .Sprintf ("%s/foobar.txt" , test .WorkspaceRoot ),
328
409
Content : []byte ("hello world" ),
329
410
Mode : 0644 ,
330
- }, & resp )
411
+ }, & writeFileResp )
331
412
if err != nil {
332
413
t .Fatalf ("cannot write file %s" , fmt .Sprintf ("%s/foobar.txt" , test .WorkspaceRoot ))
333
414
}
@@ -343,6 +424,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
343
424
if err != nil {
344
425
t .Fatal (err )
345
426
}
427
+ t .Logf ("vsName: %s, vsHandle: %s" , vsInfo .VolumeSnapshotName , vsInfo .VolumeSnapshotHandle )
346
428
347
429
// reopen the workspace and make sure the file foobar.txt exists
348
430
ws1 , stopWs1 , err := integration .LaunchWorkspaceDirectly (t , ctx , api , integration .WithRequestModifier (func (req * wsmanapi.StartWorkspaceRequest ) error {
@@ -414,7 +496,7 @@ func stopWorkspaceAndFindSnapshot(StopWorkspaceFunc integration.StopWorkspaceFun
414
496
if err != nil {
415
497
return "" , nil , err
416
498
}
417
- if lastStatus == nil && lastStatus .Conditions == nil {
499
+ if lastStatus == nil || lastStatus .Conditions == nil || lastStatus . Conditions . VolumeSnapshot == nil {
418
500
return "" , nil , nil
419
501
}
420
502
return lastStatus .Conditions .Snapshot , lastStatus .Conditions .VolumeSnapshot , nil
0 commit comments