Skip to content

Commit cd9a3c2

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 cd9a3c2

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

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

+50-8
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ 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 20; exit 0;"
183184
)
184185

185186
func TestOpenWorkspaceFromPrebuild(t *testing.T) {
@@ -223,7 +224,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
223224
req.Type = wsmanapi.WorkspaceType_PREBUILD
224225
req.Spec.Envvars = append(req.Spec.Envvars, &wsmanapi.EnvironmentVariable{
225226
Name: "GITPOD_TASKS",
226-
Value: `[{ "init": "echo \"some output\" > someFile; sleep 60; exit 0;" }]`,
227+
Value: fmt.Sprintf(`[{ "init": %q }]`, initTask),
227228
})
228229
req.Spec.FeatureFlags = test.FF
229230
req.Spec.Initializer = &csapi.WorkspaceInitializer{
@@ -298,8 +299,8 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
298299
integration.DeferCloser(t, closer)
299300

300301
// check prebuild log message exists
302+
var checkPrebuildLogSuccess bool
301303
var resp agent.ExecResponse
302-
var checkPrebuildSuccess bool
303304
for i := 0; i < 10; i++ {
304305
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
305306
Dir: prebuildLogPath,
@@ -310,16 +311,57 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
310311
},
311312
}, &resp)
312313
if err == nil && resp.ExitCode == 0 && strings.Trim(resp.Stdout, " \t\n") != "" {
313-
checkPrebuildSuccess = true
314+
checkPrebuildLogSuccess = true
314315
break
315316
}
316317

317-
// wait 3 seconds and check
318-
time.Sleep(3 * time.Second)
318+
// wait 6 seconds and check
319+
time.Sleep(6 * time.Second)
319320
}
320321

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)
322+
if !checkPrebuildLogSuccess {
323+
// somehow, the prebuild log message '🤙 This task ran as a workspace prebuild' does not exists
324+
// we fall back to check the the init task message within the /workspace/.gitpod/prebuild-log-* or not
325+
t.Logf("cannot found the prebuild message %s in %s, err:%v, exitCode:%d, stdout:%s", prebuildLog, prebuildLogPath, err, resp.ExitCode, resp.Stdout)
326+
327+
// check the init task message exists
328+
var checkInitTaskMsgSuccess bool
329+
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
330+
Dir: prebuildLogPath,
331+
Command: "bash",
332+
Args: []string{
333+
"-c",
334+
fmt.Sprintf("grep %q *", initTask),
335+
},
336+
}, &resp)
337+
if err == nil && resp.ExitCode == 0 && strings.Trim(resp.Stdout, " \t\n") != "" {
338+
checkInitTaskMsgSuccess = true
339+
}
340+
341+
if !checkInitTaskMsgSuccess {
342+
t.Logf("cannot found the init task message %s in %s, err:%v, exitCode:%d, stdout:%s", initTask, prebuildLogPath, err, resp.ExitCode, resp.Stdout)
343+
344+
// somehow, the init task message does not exist within the /workspace/.gitpod/prebuild-log-*
345+
// we fall back to check the file exists or not
346+
var ls agent.ListDirResponse
347+
err = rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{
348+
Dir: test.WorkspaceRoot,
349+
}, &ls)
350+
if err != nil {
351+
t.Fatal(err)
352+
}
353+
354+
var found bool
355+
for _, f := range ls.Files {
356+
if filepath.Base(f) == "someFile" {
357+
found = true
358+
break
359+
}
360+
}
361+
if !found {
362+
t.Fatal("did not find someFile from previous workspace instance")
363+
}
364+
}
323365
}
324366

325367
// write file foobar.txt and stop the workspace
@@ -414,7 +456,7 @@ func stopWorkspaceAndFindSnapshot(StopWorkspaceFunc integration.StopWorkspaceFun
414456
if err != nil {
415457
return "", nil, err
416458
}
417-
if lastStatus == nil && lastStatus.Conditions == nil {
459+
if lastStatus == nil || lastStatus.Conditions == nil || lastStatus.Conditions.VolumeSnapshot == nil {
418460
return "", nil, nil
419461
}
420462
return lastStatus.Conditions.Snapshot, lastStatus.Conditions.VolumeSnapshot, nil

0 commit comments

Comments
 (0)