| 1 | package hostimage_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/sylvinus/agent-vm/internal/hostimage" |
| 12 | "github.com/sylvinus/agent-vm/internal/limaembed" |
| 13 | ) |
| 14 | |
| 15 | func TestUbuntu2204PinnedImageAndPersistentTemplate(t *testing.T) { |
| 16 | template, err := limaembed.ReadImageTemplate("ubuntu-22.04") |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | identity := hostimage.Identity{System: "ubuntu-22.04", Version: "22.04", Arch: "amd64"} |
| 21 | state := t.TempDir() |
| 22 | plan, err := hostimage.Resolve(context.Background(), identity, state, t.TempDir(), template, nil) |
| 23 | if err != nil || !strings.Contains(plan.URL, "/jammy/release-20261106/") || len(plan.Digest) != 71 { |
| 24 | t.Fatalf("source: %+v %v", plan, err) |
| 25 | } |
| 26 | plan.Path = filepath.Join(state, "ubuntu.img") |
| 27 | if err := hostimage.WriteLinuxTemplate(state, plan); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | saved, err := hostimage.Load(filepath.Join(state, "guest-media")) |
| 31 | if err != nil || saved != plan { |
| 32 | t.Fatalf("persistent media: %+v %v", saved, err) |
| 33 | } |
| 34 | path, err := hostimage.LinuxTemplate(state) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | data, err := os.ReadFile(path) |
| 39 | var config struct{ Images []map[string]string } |
| 40 | if err != nil || json.Unmarshal(data, &config) != nil || len(config.Images) != 1 || config.Images[0]["location"] != plan.Path { |
| 41 | t.Fatalf("template changed source or added fallbacks: %s %v", data, err) |
| 42 | } |
| 43 | plan.URL = "https://cloud-images.ubuntu.com.evil.test/ubuntu.img" |
| 44 | if err := plan.Validate(); err == nil { |
| 45 | t.Fatal("accepted an untrusted Ubuntu image source") |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestUbuntuChoiceNeverFallsBackAfterMetadataLoss(t *testing.T) { |
| 50 | for _, removed := range []string{"host-system.yaml", "host-system-source.json"} { |
| 51 | state := t.TempDir() |
| 52 | template, _ := limaembed.ReadImageTemplate("ubuntu-22.04") |
| 53 | plan, err := hostimage.Resolve(t.Context(), hostimage.Identity{System: "ubuntu-22.04", Version: "22.04", Arch: "arm64"}, state, state, template, nil) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | plan.Path = filepath.Join(state, "ubuntu.img") |
| 58 | if err := hostimage.WriteLinuxTemplate(state, plan); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if system, err := hostimage.LinuxSystem(state); err != nil || system != "ubuntu-22.04" { |
| 62 | t.Fatalf("identity: %q %v", system, err) |
| 63 | } |
| 64 | if err := os.Remove(filepath.Join(state, "guest-media", removed)); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | if _, err := hostimage.LinuxTemplate(state); err == nil { |
| 68 | t.Fatalf("silently fell back after losing %s", removed) |
| 69 | } |
| 70 | } |
| 71 | } |