| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/sylvinus/agent-vm/internal/cage" |
| 11 | ) |
| 12 | |
| 13 | func TestStorageFirstSelectionAndMigrationThroughWorkbench(t *testing.T) { |
| 14 | s := testServer(t) |
| 15 | const endpoint = "/api/environment/storage" |
| 16 | query := func() cage.StorageLocation { |
| 17 | t.Helper() |
| 18 | r := request(s, "GET", endpoint, s.origin, "", true) |
| 19 | var loc cage.StorageLocation |
| 20 | if r.Code != 200 || json.Unmarshal(r.Body.Bytes(), &loc) != nil { |
| 21 | t.Fatalf("%d %s", r.Code, r.Body.String()) |
| 22 | } |
| 23 | return loc |
| 24 | } |
| 25 | previous := query() |
| 26 | if previous.HasEnvironment { |
| 27 | t.Fatal("fresh profile reported an environment") |
| 28 | } |
| 29 | target := filepath.Join(t.TempDir(), "Cage 空间") |
| 30 | write := func(target string, prior cage.StorageLocation) Job { |
| 31 | t.Helper() |
| 32 | body, _ := json.Marshal(map[string]any{"directory": target, "previous": prior.Directory, "migrate": prior.HasEnvironment}) |
| 33 | r := request(s, "POST", endpoint, s.origin, string(body), true) |
| 34 | if r.Code != http.StatusOK { |
| 35 | t.Fatal(r.Code, r.Body.String()) |
| 36 | } |
| 37 | return awaitJobIdle(t, &s.jobs) |
| 38 | } |
| 39 | if job := write(target, previous); job.Error != "" { |
| 40 | t.Fatal(job) |
| 41 | } |
| 42 | location := query() |
| 43 | real, err := filepath.EvalSymlinks(target) |
| 44 | if err != nil || location.Directory != real || !location.Custom { |
| 45 | t.Fatal(location, real, err) |
| 46 | } |
| 47 | for _, path := range []string{target, filepath.Dir(target), filepath.Join(target, "state")} { |
| 48 | if _, err := validateWorkspace(path, s.profile); err == nil { |
| 49 | t.Fatal("environment storage exposed as project", path) |
| 50 | } |
| 51 | } |
| 52 | project := t.TempDir() |
| 53 | if err := s.rememberSharedProject(project); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if job := write(filepath.Join(project, "private-disks"), location); job.Error == "" { |
| 57 | t.Fatal("saved project accepted as environment storage") |
| 58 | } |
| 59 | staleTarget := filepath.Join(t.TempDir(), "old-window") |
| 60 | if job := write(staleTarget, previous); job.Error == "" { |
| 61 | t.Fatal("stale window redirected the active environment", job) |
| 62 | } |
| 63 | if _, err := os.Stat(staleTarget); !os.IsNotExist(err) { |
| 64 | t.Fatal("stale request created a destination", err) |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | relativeDisk := filepath.Join("state", "lima", "fixture", "diffdisk") |
| 70 | if err := os.MkdirAll(filepath.Dir(filepath.Join(target, relativeDisk)), 0700); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | if err := os.WriteFile(filepath.Join(target, relativeDisk), []byte("isolated VM disk"), 0600); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | second := filepath.Join(t.TempDir(), "Cage 环境") |
| 77 | if job := write(second, location); job.Error == "" { |
| 78 | t.Fatal("unconfirmed migration was accepted") |
| 79 | } |
| 80 | from := query() |
| 81 | if !from.HasEnvironment { |
| 82 | t.Fatal("disk was not discovered") |
| 83 | } |
| 84 | if job := write(second, from); job.Error != "" { |
| 85 | t.Fatal(job) |
| 86 | } |
| 87 | moved := query() |
| 88 | if !moved.HasEnvironment || moved.Backup != from.Directory { |
| 89 | t.Fatal(moved) |
| 90 | } |
| 91 | for _, root := range []string{moved.Backup, moved.Directory} { |
| 92 | data, err := os.ReadFile(filepath.Join(root, relativeDisk)) |
| 93 | if err != nil || string(data) != "isolated VM disk" { |
| 94 | t.Fatal(root, err) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestStorageChangeIsLocalAndRequiresUnlockedSession(t *testing.T) { |
| 100 | s := testServer(t) |
| 101 | const endpoint = "/api/environment/storage" |
| 102 | s.remote = newRemoteGateway(s.profile) |
| 103 | defer s.remote.manager.Close() |
| 104 | s.remote.setRemote(true) |
| 105 | if r := request(s, "POST", endpoint, s.origin, "{}", true); r.Code != http.StatusForbidden { |
| 106 | t.Fatal("remote connection routed a storage request", r.Code) |
| 107 | } |
| 108 | s.remote.setRemote(false) |
| 109 | s.account.lock() |
| 110 | if r := request(s, "POST", endpoint, s.origin, "{}", true); r.Code != http.StatusLocked { |
| 111 | t.Fatal("locked user could change storage", r.Code) |
| 112 | } |
| 113 | } |