| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/google/uuid" |
| 14 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 15 | "github.com/sylvinus/agent-vm/internal/hostimage" |
| 16 | ) |
| 17 | |
| 18 | func TestHostClonePreviewReusesPendingChoiceAndExistingDisk(t *testing.T) { |
| 19 | if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" { |
| 20 | t.Skip("fixture uses the Apple Silicon installation kit") |
| 21 | } |
| 22 | s := testServer(t) |
| 23 | s.exe = filepath.Join(s.profile, "bin", "cage") |
| 24 | for _, name := range []string{"prepare-macos-media.sh", "provision-macos-workbench.sh", "project/cage-sshfs", "native-browser/macos-native-browser.mjs", "cage-finish-initial-setup"} { |
| 25 | path := filepath.Join(filepath.Dir(s.exe), "guest-os", name) |
| 26 | if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if err := os.WriteFile(path, []byte("fixture"), 0600); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | } |
| 33 | plan := hostimage.Plan{ |
| 34 | Host: hostimage.Identity{System: "macos", Version: "26.5", Build: "25F71", Arch: "arm64"}, |
| 35 | Version: "26.5", Build: "25F71", Format: "ipsw", Bytes: 1 << 30, |
| 36 | URL: "https://updates.cdn-apple.com/fixture.ipsw", Digest: "sha256:" + strings.Repeat("a", 64), |
| 37 | } |
| 38 | install := guestos.Installation{Version: 1, ID: uuid.New(), System: "macos", Source: plan} |
| 39 | if err := guestos.SaveInstallation(s.profile, install); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | for _, started := range []bool{false, true} { |
| 43 | if started { |
| 44 | if err := os.MkdirAll(filepath.Join(s.profile, "state", "lima", "mac"), 0700); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | } |
| 48 | r := httptest.NewRequest(http.MethodGet, "/api/environment/guest-systems/clone", nil) |
| 49 | r.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) |
| 50 | w := httptest.NewRecorder() |
| 51 | s.hostClone(w, r) |
| 52 | if w.Code != http.StatusOK { |
| 53 | t.Fatalf("preview: %d %s", w.Code, w.Body.String()) |
| 54 | } |
| 55 | var result struct { |
| 56 | Plan hostimage.Plan `json:"plan"` |
| 57 | MinimumFreeGiB int `json:"minimum_free_gib"` |
| 58 | InstallationStarted bool `json:"installation_started"` |
| 59 | } |
| 60 | if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | minimum := 33 |
| 64 | if started { |
| 65 | minimum = 1 |
| 66 | } |
| 67 | if result.Plan != plan || result.MinimumFreeGiB != minimum || result.InstallationStarted != started { |
| 68 | t.Fatalf("pending source or remaining disk budget changed: %+v", result) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestPendingLinuxCloneRetryReturnsToInstallerAdmission(t *testing.T) { |
| 74 | if runtime.GOOS == "linux" { |
| 75 | t.Skip("this case verifies rejection on a different host family") |
| 76 | } |
| 77 | s := testServer(t) |
| 78 | install := guestos.Installation{ |
| 79 | Version: 1, ID: uuid.New(), System: "ubuntu-22.04", |
| 80 | Source: hostimage.Plan{ |
| 81 | Host: hostimage.Identity{System: "ubuntu-22.04", Version: "22.04", Arch: "amd64"}, |
| 82 | Version: "22.04", Format: "qcow2", Path: filepath.Join(s.profile, "ubuntu.img"), |
| 83 | Digest: "sha256:" + strings.Repeat("a", 64), |
| 84 | }, |
| 85 | } |
| 86 | if err := guestos.SaveInstallation(s.profile, install); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | for _, body := range []string{`{}`, `{"guest_system":"ubuntu-22.04"}`} { |
| 90 | r := httptest.NewRequest(http.MethodPost, "/api/start", strings.NewReader(body)) |
| 91 | r.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) |
| 92 | w := httptest.NewRecorder() |
| 93 | s.start(w, r) |
| 94 | if w.Code == http.StatusOK || s.jobs.snapshot().Running { |
| 95 | t.Fatal("pending clone bypassed installer admission through the generic start path") |
| 96 | } |
| 97 | } |
| 98 | } |