| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/google/uuid" |
| 10 | "github.com/sylvinus/agent-vm/internal/hostimage" |
| 11 | ) |
| 12 | |
| 13 | func TestHostSourcePublishesOneVerifiedMediaReceipt(t *testing.T) { |
| 14 | profile := t.TempDir() |
| 15 | source := hostimage.Plan{Host: hostimage.Identity{System: Windows11, Version: "25H2", Build: "26200.9", Edition: "Professional", Language: "zh-CN", Arch: "amd64"}, Version: "25H2", Build: "26200.8", Format: "esd", URL: "http://dl.delivery.mp.microsoft.com/example.esd", Digest: "sha256:" + strings.Repeat("a", 64), Bytes: 10} |
| 16 | install := Installation{Version: 1, ID: uuid.New(), System: Windows11, EditorTerms: true, Source: source} |
| 17 | if err := SaveInstallation(profile, install); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | original, _ := os.ReadFile(filepath.Join(profile, installRecord)) |
| 21 | ready := source |
| 22 | ready.Path = filepath.Join(profile, "system.esd") |
| 23 | ready.Modified = 123 |
| 24 | if err := RecordPreparedMedia(profile, install.ID, ready); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | loaded, err := LoadInstallation(profile) |
| 28 | if err != nil || loaded.Media != ready.Path || loaded.Source != ready { |
| 29 | t.Fatalf("loaded: %+v %v", loaded, err) |
| 30 | } |
| 31 | if err := SaveInstallation(profile, *loaded); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | if err := RecordPreparedMedia(profile, install.ID, ready); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | after, _ := os.ReadFile(filepath.Join(profile, installRecord)) |
| 38 | if string(original) != string(after) { |
| 39 | t.Fatal("changed original installation choice") |
| 40 | } |
| 41 | changed := ready |
| 42 | changed.Host.Edition = "Enterprise" |
| 43 | if err := RecordPreparedMedia(profile, install.ID, changed); err == nil { |
| 44 | t.Fatal("accepted another edition") |
| 45 | } |
| 46 | if err := RecordPreparedMedia(profile, uuid.New(), ready); err == nil { |
| 47 | t.Fatal("accepted another installation") |
| 48 | } |
| 49 | changed = ready |
| 50 | changed.Path = filepath.Join(profile, "other.esd") |
| 51 | if err := RecordPreparedMedia(profile, install.ID, changed); err == nil { |
| 52 | t.Fatal("replaced published media") |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestLegacyInstallationOmitsSource(t *testing.T) { |
| 57 | profile := t.TempDir() |
| 58 | if err := SaveInstallation(profile, Installation{Version: 1, ID: uuid.New(), System: MacOS}); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | data, _ := os.ReadFile(filepath.Join(profile, installRecord)) |
| 62 | if strings.Contains(string(data), `"source"`) { |
| 63 | t.Fatal("legacy choice acquired source metadata") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestWindowsCloneCleanupTracksRetainedCredential(t *testing.T) { |
| 68 | profile := t.TempDir() |
| 69 | source := hostimage.Plan{ |
| 70 | Host: hostimage.Identity{System: Windows11, Version: "25H2", Edition: "Professional", Language: "zh-CN", Arch: "amd64"}, |
| 71 | Version: "25H2", Build: "26200.8", Format: "esd", Path: filepath.Join(profile, "system.esd"), |
| 72 | } |
| 73 | install := Installation{Version: 1, ID: uuid.New(), System: Windows11, EditorTerms: true, Source: source} |
| 74 | if err := SaveInstallation(profile, install); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | path := filepath.Join(profile, "windows-setup-"+strings.ReplaceAll(install.ID.String(), "-", "")+".dpapi") |
| 78 | if err := os.WriteFile(path, []byte("encrypted-fixture"), 0600); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | pending, err := WindowsCloneCleanupPending(profile) |
| 82 | if err != nil || !pending { |
| 83 | t.Fatalf("retained credential bypassed cleanup: %v %v", pending, err) |
| 84 | } |
| 85 | if err := os.Remove(path); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | pending, err = WindowsCloneCleanupPending(profile) |
| 89 | if err != nil || pending { |
| 90 | t.Fatalf("completed cleanup still blocks activation: %v %v", pending, err) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestLinuxCloneKeepsItsChoiceAfterBaseInstallation(t *testing.T) { |
| 95 | for _, name := range []string{"base-building", "base"} { |
| 96 | t.Run(name, func(t *testing.T) { |
| 97 | profile := t.TempDir() |
| 98 | install := Installation{ |
| 99 | Version: 1, ID: uuid.New(), System: "ubuntu-22.04", |
| 100 | Source: hostimage.Plan{ |
| 101 | Host: hostimage.Identity{System: "ubuntu-22.04", Version: "22.04", Arch: "amd64"}, |
| 102 | Version: "22.04", Format: "qcow2", Path: filepath.Join(profile, "ubuntu.img"), |
| 103 | Digest: "sha256:" + strings.Repeat("a", 64), |
| 104 | }, |
| 105 | } |
| 106 | if err := SaveInstallation(profile, install); err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | disk := filepath.Join(profile, "state", "lima", name) |
| 110 | if err := os.MkdirAll(disk, 0700); err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | unstarted, err := UnstartedInstallation(profile, install) |
| 114 | if err != nil || unstarted { |
| 115 | t.Fatalf("existing base was treated as an unstarted choice: %v %v", unstarted, err) |
| 116 | } |
| 117 | if err := ClearUnstartedInstallation(profile, install.ID); err == nil { |
| 118 | t.Fatal("cleared a choice that already produced a base disk") |
| 119 | } |
| 120 | if err := FinishLinuxInstallation(profile, install.ID); err == nil { |
| 121 | t.Fatal("finished without a durable Linux choice") |
| 122 | } |
| 123 | if pending, err := LoadInstallation(profile); err != nil || pending == nil { |
| 124 | t.Fatal("lost installation recovery information", err) |
| 125 | } |
| 126 | if err := hostimage.WriteLinuxTemplate(filepath.Join(profile, "state"), install.Source); err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | if err := FinishLinuxInstallation(profile, install.ID); err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | if _, err := os.Stat(disk); err != nil { |
| 133 | t.Fatal("finishing the installation affected its disk", err) |
| 134 | } |
| 135 | }) |
| 136 | } |
| 137 | } |