Skip to content

Commit 2dfa983

Browse files
committed
test: fall back to check the init task message and file exits
Signed-off-by: JenTing Hsiao <[email protected]>
1 parent f61eacf commit 2dfa983

File tree

1 file changed

+94
-12
lines changed

1 file changed

+94
-12
lines changed

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

Lines changed: 94 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,22 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
180180
const (
181181
prebuildLogPath string = "/workspace/.gitpod"
182182
prebuildLog string = "'🤙 This task ran as a workspace prebuild'"
183+
initTask string = "echo \"some output\" > someFile; sleep 90;"
183184
)
184185

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
185199
func TestOpenWorkspaceFromPrebuild(t *testing.T) {
186200
f := features.New("prebuild").
187201
WithLabel("component", "ws-manager").
@@ -223,7 +237,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
223237
req.Type = wsmanapi.WorkspaceType_PREBUILD
224238
req.Spec.Envvars = append(req.Spec.Envvars, &wsmanapi.EnvironmentVariable{
225239
Name: "GITPOD_TASKS",
226-
Value: `[{ "init": "echo \"some output\" > someFile; sleep 60; exit 0;" }]`,
240+
Value: fmt.Sprintf(`[{ "init": %q }]`, initTask),
227241
})
228242
req.Spec.FeatureFlags = test.FF
229243
req.Spec.Initializer = &csapi.WorkspaceInitializer{
@@ -248,6 +262,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
248262
if err != nil {
249263
t.Fatalf("stop workspace and find snapshot error: %v", err)
250264
}
265+
t.Logf("prebuild snapshot: %s, vsName: %s, vsHandle: %s", prebuildSnapshot, vsInfo.VolumeSnapshotName, vsInfo.VolumeSnapshotHandle)
251266

252267
// launch the workspace from prebuild
253268
ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
@@ -298,8 +313,11 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
298313
integration.DeferCloser(t, closer)
299314

300315
// 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
303321
for i := 0; i < 10; i++ {
304322
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
305323
Dir: prebuildLogPath,
@@ -308,26 +326,89 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
308326
"-c",
309327
fmt.Sprintf("grep %s *", prebuildLog),
310328
},
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
314332
break
315333
}
316-
317-
// wait 3 seconds and check
334+
t.Logf("[%d] retry...", i)
318335
time.Sleep(3 * time.Second)
319336
}
320337

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)
323403
}
324404

325405
// write file foobar.txt and stop the workspace
406+
var writeFileResp agent.ExecResponse
326407
err = rsa.Call("WorkspaceAgent.WriteFile", &agent.WriteFileRequest{
327408
Path: fmt.Sprintf("%s/foobar.txt", test.WorkspaceRoot),
328409
Content: []byte("hello world"),
329410
Mode: 0644,
330-
}, &resp)
411+
}, &writeFileResp)
331412
if err != nil {
332413
t.Fatalf("cannot write file %s", fmt.Sprintf("%s/foobar.txt", test.WorkspaceRoot))
333414
}
@@ -343,6 +424,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
343424
if err != nil {
344425
t.Fatal(err)
345426
}
427+
t.Logf("vsName: %s, vsHandle: %s", vsInfo.VolumeSnapshotName, vsInfo.VolumeSnapshotHandle)
346428

347429
// reopen the workspace and make sure the file foobar.txt exists
348430
ws1, stopWs1, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
@@ -414,7 +496,7 @@ func stopWorkspaceAndFindSnapshot(StopWorkspaceFunc integration.StopWorkspaceFun
414496
if err != nil {
415497
return "", nil, err
416498
}
417-
if lastStatus == nil && lastStatus.Conditions == nil {
499+
if lastStatus == nil || lastStatus.Conditions == nil || lastStatus.Conditions.VolumeSnapshot == nil {
418500
return "", nil, nil
419501
}
420502
return lastStatus.Conditions.Snapshot, lastStatus.Conditions.VolumeSnapshot, nil

0 commit comments

Comments
 (0)