Skip to content

Commit 1ae3708

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 1ae3708

File tree

1 file changed

+89
-11
lines changed

1 file changed

+89
-11
lines changed

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

Lines changed: 89 additions & 11 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
"path/filepath"
1213
"reflect"
@@ -180,8 +181,22 @@ func TestPrebuildWorkspaceTaskFail(t *testing.T) {
180181
const (
181182
prebuildLogPath string = "/workspace/.gitpod"
182183
prebuildLog string = "'🤙 This task ran as a workspace prebuild'"
184+
initTask string = "echo \"some output\" > someFile; sleep 20; exit 0;"
183185
)
184186

187+
// TestOpenWorkspaceFromPrebuild
188+
// - create a prebuild
189+
// - open the workspace from prebuild
190+
// - make sure the .git/ folder with correct permission
191+
// - make sure either one of the condition mets
192+
// - the prebuild log message exists
193+
// - the init task message exists
194+
// - the init task generated file exists
195+
//
196+
// - write a new file foobar.txt
197+
// - stop the workspace
198+
// - relaunch the workspace
199+
// - make sure the file foobar.txt exists
185200
func TestOpenWorkspaceFromPrebuild(t *testing.T) {
186201
f := features.New("prebuild").
187202
WithLabel("component", "ws-manager").
@@ -208,7 +223,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
208223
},
209224
}
210225

211-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(10*len(tests))*time.Minute)
226+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(15*len(tests))*time.Minute)
212227
defer cancel()
213228

214229
for _, test := range tests {
@@ -223,7 +238,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
223238
req.Type = wsmanapi.WorkspaceType_PREBUILD
224239
req.Spec.Envvars = append(req.Spec.Envvars, &wsmanapi.EnvironmentVariable{
225240
Name: "GITPOD_TASKS",
226-
Value: `[{ "init": "echo \"some output\" > someFile; sleep 60; exit 0;" }]`,
241+
Value: fmt.Sprintf(`[{ "init": %q }]`, initTask),
227242
})
228243
req.Spec.FeatureFlags = test.FF
229244
req.Spec.Initializer = &csapi.WorkspaceInitializer{
@@ -248,6 +263,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
248263
if err != nil {
249264
t.Fatalf("stop workspace and find snapshot error: %v", err)
250265
}
266+
t.Logf("prebuild snapshot: %s, vsName: %s, vsHandle: %s", prebuildSnapshot, vsInfo.VolumeSnapshotName, vsInfo.VolumeSnapshotHandle)
251267

252268
// launch the workspace from prebuild
253269
ws, stopWs, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
@@ -297,9 +313,29 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
297313
})
298314
integration.DeferCloser(t, closer)
299315

300-
// check prebuild log message exists
316+
// check the files/folders permission under .git/
301317
var resp agent.ExecResponse
302-
var checkPrebuildSuccess bool
318+
var gitDir string = fmt.Sprintf("%s/%s", test.WorkspaceRoot, ".git")
319+
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
320+
Dir: gitDir,
321+
Command: "find",
322+
Args: []string{"-user", "root"},
323+
}, &resp)
324+
if err != nil || resp.ExitCode != 0 || strings.Trim(resp.Stdout, " \t\n") != "" {
325+
t.Fatalf("incorrect file perimssion under %s folder, err:%v, exitCode:%d, stdout:%s", gitDir, err, resp.ExitCode, resp.Stdout)
326+
}
327+
328+
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
329+
Dir: gitDir,
330+
Command: "find",
331+
Args: []string{"-group", "root"},
332+
}, &resp)
333+
if err != nil || resp.ExitCode != 0 || strings.Trim(resp.Stdout, " \t\n") != "" {
334+
t.Fatalf("incorrect group perimssion under %s folder, err:%v, exitCode:%d, stdout:%s", gitDir, err, resp.ExitCode, resp.Stdout)
335+
}
336+
337+
// check prebuild log message exists
338+
var checkPrebuildLog bool
303339
for i := 0; i < 10; i++ {
304340
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
305341
Dir: prebuildLogPath,
@@ -310,16 +346,57 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
310346
},
311347
}, &resp)
312348
if err == nil && resp.ExitCode == 0 && strings.Trim(resp.Stdout, " \t\n") != "" {
313-
checkPrebuildSuccess = true
349+
checkPrebuildLog = true
314350
break
315351
}
316352

317-
// wait 3 seconds and check
318-
time.Sleep(3 * time.Second)
353+
// wait 6 seconds and check
354+
time.Sleep(6 * time.Second)
319355
}
320356

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)
357+
if !checkPrebuildLog {
358+
// somehow, the prebuild log message '🤙 This task ran as a workspace prebuild' does not exists
359+
// we fall back to check the the init task message within the /workspace/.gitpod/prebuild-log-* or not
360+
t.Logf("cannot found the prebuild message %s in %s, err:%v, exitCode:%d, stdout:%s", prebuildLog, prebuildLogPath, err, resp.ExitCode, resp.Stdout)
361+
362+
// check the init task message exists
363+
var checkInitTaskMsg bool
364+
err = rsa.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
365+
Dir: prebuildLogPath,
366+
Command: "bash",
367+
Args: []string{
368+
"-c",
369+
fmt.Sprintf("grep %q *", initTask),
370+
},
371+
}, &resp)
372+
if err == nil && resp.ExitCode == 0 && strings.Trim(resp.Stdout, " \t\n") != "" {
373+
checkInitTaskMsg = true
374+
}
375+
376+
if !checkInitTaskMsg {
377+
t.Logf("cannot found the init task message %s in %s, err:%v, exitCode:%d, stdout:%s", initTask, prebuildLogPath, err, resp.ExitCode, resp.Stdout)
378+
379+
// somehow, the init task message does not exist within the /workspace/.gitpod/prebuild-log-*
380+
// we fall back to check the file exists or not
381+
var ls agent.ListDirResponse
382+
err = rsa.Call("WorkspaceAgent.ListDir", &agent.ListDirRequest{
383+
Dir: test.WorkspaceRoot,
384+
}, &ls)
385+
if err != nil {
386+
t.Fatal(err)
387+
}
388+
389+
var found bool
390+
for _, f := range ls.Files {
391+
if filepath.Base(f) == "someFile" {
392+
found = true
393+
break
394+
}
395+
}
396+
if !found {
397+
t.Fatal("did not find someFile from previous workspace instance")
398+
}
399+
}
323400
}
324401

325402
// write file foobar.txt and stop the workspace
@@ -343,6 +420,7 @@ func TestOpenWorkspaceFromPrebuild(t *testing.T) {
343420
if err != nil {
344421
t.Fatal(err)
345422
}
423+
t.Logf("prebuild snapshot: %s, vsName: %s, vsHandle: %s", prebuildSnapshot, vsInfo.VolumeSnapshotName, vsInfo.VolumeSnapshotHandle)
346424

347425
// reopen the workspace and make sure the file foobar.txt exists
348426
ws1, stopWs1, err := integration.LaunchWorkspaceDirectly(t, ctx, api, integration.WithRequestModifier(func(req *wsmanapi.StartWorkspaceRequest) error {
@@ -414,8 +492,8 @@ func stopWorkspaceAndFindSnapshot(StopWorkspaceFunc integration.StopWorkspaceFun
414492
if err != nil {
415493
return "", nil, err
416494
}
417-
if lastStatus == nil && lastStatus.Conditions == nil {
418-
return "", nil, nil
495+
if lastStatus == nil || lastStatus.Conditions == nil || lastStatus.Conditions.VolumeSnapshot == nil {
496+
return "", nil, errors.New("cannot find the last snapshot")
419497
}
420498
return lastStatus.Conditions.Snapshot, lastStatus.Conditions.VolumeSnapshot, nil
421499
}

0 commit comments

Comments
 (0)