| 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 TestUbuntuGuestChoicesMatchSupportedHostArchitectures(t *testing.T) { |
| 19 | for _, host := range []struct { |
| 20 | platform, arch string |
| 21 | available bool |
| 22 | }{ |
| 23 | {"darwin", "arm64", true}, {"windows", "amd64", true}, {"linux", "amd64", true}, |
| 24 | {"darwin", "amd64", false}, {"windows", "arm64", false}, {"linux", "arm64", false}, |
| 25 | } { |
| 26 | if got := linuxGuestSystems(host.platform, host.arch); (len(got) == 1 && got[0] == guestos.Ubuntu2204) != host.available { |
| 27 | t.Fatalf("%s/%s: %v", host.platform, host.arch, got) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestUbuntuCatalogPlanPreservesArchitectureAndResumeChoice(t *testing.T) { |
| 33 | for _, arch := range []string{"amd64", "arm64"} { |
| 34 | plan, err := linuxGuestImagePlan(t.Context(), t.TempDir(), guestos.Ubuntu2204, arch) |
| 35 | if err != nil || plan.Host.Arch != arch || !strings.Contains(plan.URL, "-"+arch+".img") || plan.Bytes <= 0 { |
| 36 | t.Fatalf("%s: %+v %v", arch, plan, err) |
| 37 | } |
| 38 | } |
| 39 | s := testServer(t) |
| 40 | plan, err := linuxGuestImagePlan(t.Context(), s.profile, guestos.Ubuntu2204, runtime.GOARCH) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | install := guestos.Installation{Version: 1, ID: uuid.New(), System: guestos.Ubuntu2204, Source: plan, SourceKind: guestos.LinuxImageSource} |
| 45 | if err := guestos.SaveInstallation(s.profile, install); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | view, err := s.guestInstallView() |
| 49 | if err != nil || view.Pending == nil || view.Pending.SourceKind != guestos.LinuxImageSource || !view.Reselect { |
| 50 | t.Fatalf("cross-host resume: %+v %v", view, err) |
| 51 | } |
| 52 | if err := os.MkdirAll(filepath.Join(s.profile, "state", "lima", "base-building"), 0700); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | view, err = s.guestInstallView() |
| 56 | if err != nil || view.Reselect { |
| 57 | t.Fatalf("started disk can be reselected: %+v %v", view, err) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestUbuntuSelectionUsesInstallerAndRequiresTrustedDesktop(t *testing.T) { |
| 62 | for _, trusted := range []bool{false, true} { |
| 63 | s := testServer(t) |
| 64 | |
| 65 | s.op.Lock() |
| 66 | r := httptest.NewRequest(http.MethodPost, "/api/start", strings.NewReader(`{"guest_system":"ubuntu-22.04"}`)) |
| 67 | r.Header.Set("Content-Type", "application/json") |
| 68 | if trusted { |
| 69 | r.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) |
| 70 | } |
| 71 | w := httptest.NewRecorder() |
| 72 | s.start(w, r) |
| 73 | job := s.jobs.snapshot() |
| 74 | s.jobs.cancelRunning() |
| 75 | s.op.Unlock() |
| 76 | s.jobs.workers.Wait() |
| 77 | if trusted { |
| 78 | var response Job |
| 79 | if w.Code != http.StatusOK || json.Unmarshal(w.Body.Bytes(), &response) != nil || response.Kind != "guest-install" || job.Kind != "guest-install" { |
| 80 | t.Fatalf("Ubuntu went through generic Debian startup: %d %s", w.Code, w.Body.String()) |
| 81 | } |
| 82 | } else if w.Code == http.StatusOK { |
| 83 | t.Fatal("untrusted page started installation") |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestUbuntuBoundSystemCannotBeStartedAsDebian(t *testing.T) { |
| 89 | s := testServer(t) |
| 90 | plan, err := linuxGuestImagePlan(t.Context(), s.profile, guestos.Ubuntu2204, runtime.GOARCH) |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | plan.Path = filepath.Join(s.profile, "ubuntu.img") |
| 95 | if err := hostimage.WriteLinuxTemplate(filepath.Join(s.profile, "state"), plan); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | w := request(s, http.MethodPost, "/api/start", s.origin, `{"guest_system":"debian-13"}`, true) |
| 99 | if w.Code != http.StatusConflict || s.jobs.snapshot().ID != "" { |
| 100 | t.Fatalf("changed existing Ubuntu: %d %s", w.Code, w.Body.String()) |
| 101 | } |
| 102 | } |