| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "sync/atomic" |
| 10 | "testing" |
| 11 | |
| 12 | "golang.org/x/crypto/ssh" |
| 13 | ) |
| 14 | |
| 15 | func TestMacOSProjectWaitsForLoginAndRestoresAutomatically(t *testing.T) { |
| 16 | var loggedIn atomic.Bool |
| 17 | guest := newMacWorkspaceGuest(t, func(channel ssh.Channel, command string) (uint32, bool) { |
| 18 | if strings.Contains(command, "/dev/console") && !loggedIn.Load() { |
| 19 | return 73, true |
| 20 | } |
| 21 | return 0, false |
| 22 | }) |
| 23 | guest.approved.Store(true) |
| 24 | s := guest.s |
| 25 | s.restoreProjectShares() |
| 26 | if len(s.projectErrors) != 1 || s.projectErrors[0].Recovery != "guest-login" || len(s.projectShares) != 0 { |
| 27 | t.Fatalf("missing actionable login state: %+v", s.projectErrors) |
| 28 | } |
| 29 | data, _ := json.Marshal(map[string]any{"workspace": guest.hostPath, "add": false}) |
| 30 | recorder := httptest.NewRecorder() |
| 31 | request := httptest.NewRequest(http.MethodPost, "/api/projects/open", bytes.NewReader(data)) |
| 32 | request.Header.Set("Content-Type", "application/json") |
| 33 | s.openProject(recorder, request) |
| 34 | if recorder.Code != http.StatusBadRequest || recorder.Header().Get("X-Cage-Recovery") != "guest-login" { |
| 35 | t.Fatalf("project open lost its recovery action: %d %s", recorder.Code, recorder.Body.String()) |
| 36 | } |
| 37 | s.retryMacOSProjectShares(t.Context()) |
| 38 | if len(s.projectShares) != 0 { |
| 39 | t.Fatal("mount started before guest GUI login") |
| 40 | } |
| 41 | loggedIn.Store(true) |
| 42 | s.retryMacOSProjectShares(t.Context()) |
| 43 | defer s.closeProjectShares() |
| 44 | if len(s.projectErrors) != 0 || s.projectShares[guest.hostPath] == nil { |
| 45 | t.Fatalf("login did not restore the saved project: %+v", s.projectErrors) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestMacOSProjectPrepareReportsSpecificRecovery(t *testing.T) { |
| 50 | for _, sample := range []struct { |
| 51 | exit uint32 |
| 52 | recovery string |
| 53 | }{ |
| 54 | {73, "guest-login"}, {74, "environment-update"}, {76, "environment-restart"}, |
| 55 | {77, "project-directory"}, {78, "project-directory"}, {79, "environment-update"}, {255, "project-retry"}, |
| 56 | } { |
| 57 | t.Run(sample.recovery, func(t *testing.T) { |
| 58 | guest := newMacWorkspaceGuest(t, func(channel ssh.Channel, command string) (uint32, bool) { |
| 59 | if strings.Contains(command, "/dev/console") { |
| 60 | return sample.exit, true |
| 61 | } |
| 62 | return 0, false |
| 63 | }) |
| 64 | guest.s.restoreProjectShares() |
| 65 | if len(guest.s.projectErrors) != 1 || guest.s.projectErrors[0].Recovery != sample.recovery { |
| 66 | t.Fatalf("exit %d: %+v", sample.exit, guest.s.projectErrors) |
| 67 | } |
| 68 | }) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestMacOSFailedUnsavedProjectDoesNotBecomeBackgroundGrant(t *testing.T) { |
| 73 | guest := newMacWorkspaceGuest(t, func(channel ssh.Channel, command string) (uint32, bool) { |
| 74 | if strings.Contains(command, "/dev/console") { |
| 75 | return 73, true |
| 76 | } |
| 77 | return 0, false |
| 78 | }) |
| 79 | if _, err := guest.s.connectProject(t.TempDir(), true); err == nil { |
| 80 | t.Fatal("expected the guest login precondition") |
| 81 | } |
| 82 | if len(guest.s.projectErrors) != 0 || len(guest.s.projectShares) != 0 { |
| 83 | t.Fatal("an uncompleted new project was scheduled for background sharing") |
| 84 | } |
| 85 | } |