Files
Reims-Go/internal/installer/installer_test.go
T
NwflowerandClaude Opus 5.5 2f43182f0c feat: offline installer downloads, task progress events, Win10 cold reboot
Offline installers (roadmap M1.3):
- internal/installer drives `reims-util --json offline-installer` and parses
  its JSON event stream (upstream reims-vgpu 3731afd, reims_util.rs:423-509)
- downloads hold installers/.lock like reims-ctl, so the two never write the
  same file; checked against the real reims-ctl (TestLockExcludesReimsCtl)
- pause = stop the process, rerun resumes; deletes go through
  `reims-ctl downloads delete`, which refuses files in use or downloading
- CLI: reims-go installer list | download | cached | delete
- Wails bindings: ListOfflineReleases, DownloadInstaller, ListInstallers,
  DeleteInstaller

Task events (ui-redesign B1):
- tasks.go tracks long-running work and emits "reims:task" (throttled to
  200 ms per task); VM creation turns reims-ctl's progress lines into task
  progress and logs each step once
- the status bar shows the current task with a progress bar, details in a
  tooltip and a pause button; the frontend regex merging is removed

Win10 cold reboot workaround:
- host probes whether WinHvPlatform.dll lacks WHvResetPartition (Win10 19045)
- internal/qmp watches a dedicated QMP channel for guest resets; with
  -no-reboot the app restarts the VM in a fresh partition
- architecture.md documents download routing and the workaround

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 03:35:03 -07:00

291 lines
9.6 KiB
Go

package installer
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// The test binary doubles as a fake reims-util: with FAKE_REIMS_UTIL set it
// prints that scenario's lines and exits instead of running tests.
func TestMain(m *testing.M) {
if scenario := os.Getenv("FAKE_REIMS_UTIL"); scenario != "" {
fakeUtil(scenario)
return
}
os.Exit(m.Run())
}
func fakeUtil(scenario string) {
// Record the arguments for the test to check.
if p := os.Getenv("FAKE_REIMS_UTIL_ARGS"); p != "" {
os.WriteFile(p, []byte(strings.Join(os.Args[1:], "\n")), 0o644)
}
switch scenario {
case "download":
fmt.Println(`{"event":"step","message":"Downloading the macOS Tahoe 26.7.1 installer"}`)
fmt.Println(`{"event":"progress","done":0,"total":2000000000}`)
fmt.Println(`{"event":"progress","done":1000000000,"total":2000000000}`)
fmt.Println(`not json`)
fmt.Println(`{"event":"step","message":"OVMF's code"}`)
fmt.Println(`{"event":"progress","done":5,"total":null}`)
fmt.Println(`{"event":"note","message":"Wrote it"}`)
fmt.Println(`{"event":"done","result":{"installer":{"version":"26.7.1","build":"25G241"},"image":"C:\\x\\macOS-Tahoe-26.7.1-25G241-offline-installer.img"}}`)
case "list":
fmt.Println(`{"event":"step","message":"Reading Apple's software update catalog"}`)
fmt.Println(`{"event":"done","result":[{"product":"142-54128","title":"macOS Tahoe","version":"26.7.3","build":"25G342","posted":"2026-11-16","size":18388869343,"url":"https://x","digest":"ab"},{"product":"042-45246","title":"macOS Big Sur","version":"11.7.10","build":"20G1427","posted":"2023-09-11","size":12416490165}]}`)
case "error":
fmt.Println(`{"event":"error","message":"no offline installer for \"x\""}`)
os.Exit(1)
case "crash":
fmt.Fprintln(os.Stderr, "oops")
os.Exit(2)
case "hang":
fmt.Println(`{"event":"step","message":"Downloading"}`)
time.Sleep(30 * time.Second)
}
os.Exit(0)
}
func fakeClient(t *testing.T, scenario string) (*Client, string) {
t.Helper()
dir := t.TempDir()
args := filepath.Join(dir, "args.txt")
return &Client{
Util: os.Args[0],
Env: append(os.Environ(), "FAKE_REIMS_UTIL="+scenario, "FAKE_REIMS_UTIL_ARGS="+args),
InstallersDir: filepath.Join(dir, "installers"),
LockPoll: 20 * time.Millisecond,
}, args
}
func TestDownloadReportsStepsProgressAndResult(t *testing.T) {
c, argsFile := fakeClient(t, "download")
var got []Update
r, err := c.DownloadOffline(context.Background(), "26.7.1", func(u Update) { got = append(got, u) })
if err != nil {
t.Fatal(err)
}
if r.Version != "26.7.1" || r.Build != "25G241" || !strings.HasSuffix(r.Image, "offline-installer.img") {
t.Fatalf("result = %+v", r)
}
args, _ := os.ReadFile(argsFile)
want := strings.Join([]string{"--json", "offline-installer", "26.7.1", "-o", c.InstallersDir}, "\n")
if string(args) != want {
t.Fatalf("args = %q, want %q", args, want)
}
if len(got) != 6 {
t.Fatalf("updates = %+v", got)
}
if got[0].Kind != KindStep || got[0].Message != "downloading the macOS Tahoe 26.7.1 installer" {
t.Fatalf("step = %+v", got[0])
}
if got[2].Kind != KindProgress || got[2].Percent != 50 || got[2].Total != 2_000_000_000 || got[2].Step != got[0].Message {
t.Fatalf("progress = %+v", got[2])
}
if got[3].Message != "OVMF's code" {
t.Fatalf("names keep their case: %+v", got[3])
}
if got[4].Total != 0 || got[4].Percent != -1 {
t.Fatalf("unknown total = %+v", got[4])
}
if got[5].Kind != KindNote || got[5].Message != "Wrote it" {
t.Fatalf("note = %+v", got[5])
}
}
func TestListOfflineReadsReleases(t *testing.T) {
c, _ := fakeClient(t, "list")
list, err := c.ListOffline(context.Background(), nil)
if err != nil {
t.Fatal(err)
}
if len(list) != 2 || list[0].Version != "26.7.3" || list[0].Build != "25G342" || list[0].Size != 18388869343 || list[1].Title != "macOS Big Sur" {
t.Fatalf("list = %+v", list)
}
}
func TestErrorsComeFromTheErrorEventElseStderr(t *testing.T) {
c, _ := fakeClient(t, "error")
_, err := c.DownloadOffline(context.Background(), "x", nil)
if err == nil || err.Error() != `no offline installer for "x"` {
t.Fatalf("err = %v", err)
}
c, _ = fakeClient(t, "crash")
_, err = c.DownloadOffline(context.Background(), "x", nil)
if !errors.Is(err, ErrUtilFailed) || !strings.HasSuffix(err.Error(), "oops") {
t.Fatalf("err = %v", err)
}
if _, err := c.DownloadOffline(context.Background(), " list ", nil); err == nil {
t.Fatal("list is not a version")
}
}
func TestCancelStopsTheDownload(t *testing.T) {
c, _ := fakeClient(t, "hang")
ctx, cancel := context.WithCancel(context.Background())
started := make(chan struct{}, 1)
done := make(chan error, 1)
go func() {
_, err := c.DownloadOffline(ctx, "tahoe", func(u Update) {
if u.Kind == KindStep {
started <- struct{}{}
}
})
done <- err
}()
<-started
cancel()
select {
case err := <-done:
if !errors.Is(err, context.Canceled) {
t.Fatalf("err = %v", err)
}
case <-time.After(10 * time.Second):
t.Fatal("download did not stop")
}
// The lock is released: another download can start.
if c.Busy() {
t.Fatal("lock still held after cancel")
}
}
func TestDownloadWaitsForTheLock(t *testing.T) {
c, _ := fakeClient(t, "download")
if err := os.MkdirAll(c.InstallersDir, 0o755); err != nil {
t.Fatal(err)
}
held, err := c.lock(context.Background(), func(Update) {})
if err != nil {
t.Fatal(err)
}
if !c.Busy() {
t.Fatal("Busy should see the held lock")
}
notes := make(chan string, 8)
done := make(chan error, 1)
go func() {
_, err := c.DownloadOffline(context.Background(), "26.7.1", func(u Update) {
if u.Kind == KindNote {
notes <- u.Message
}
})
done <- err
}()
if n := <-notes; n != waitingNote {
t.Fatalf("first note = %q", n)
}
select {
case <-done:
t.Fatal("download ran while the lock was held")
case <-time.After(150 * time.Millisecond):
}
held()
if err := <-done; err != nil {
t.Fatal(err)
}
}
func TestDescribeNamesLikeReimsCtl(t *testing.T) {
cases := map[string]Cached{
"macOS-Tahoe-26.6.2-25G83-offline-installer.img": {Kind: "offline", Title: "macOS Tahoe", Version: "26.6.2", Build: "25G83"},
"macOS-Big-Sur-11.7.10-20G1427-offline-installer.img": {Kind: "offline", Title: "macOS Big Sur", Version: "11.7.10", Build: "20G1427"},
"macOS-Tahoe-online-installer.dmg": {Kind: "online", Title: "macOS Tahoe"},
"UniversalMac_27.2.1_26B131_Restore.ipsw.part": {Kind: "ipsw", Title: "macOS", Version: "27.2.1", Build: "26B131", Partial: true},
"notes.txt": {Kind: "other"},
}
for name, want := range cases {
got := Describe(name)
want.Name = name
if got != want {
t.Errorf("Describe(%q) = %+v, want %+v", name, got, want)
}
}
}
func TestListCachedSkipsHiddenFiles(t *testing.T) {
c, _ := fakeClient(t, "download")
os.MkdirAll(c.InstallersDir, 0o755)
os.WriteFile(filepath.Join(c.InstallersDir, ".lock"), nil, 0o644)
os.WriteFile(filepath.Join(c.InstallersDir, "macOS-Tahoe-online-installer.dmg"), []byte("abc"), 0o644)
os.Mkdir(filepath.Join(c.InstallersDir, "sub"), 0o755)
list, err := c.ListCached()
if err != nil {
t.Fatal(err)
}
if len(list) != 1 || list[0].Bytes != 3 || list[0].Kind != "online" {
t.Fatalf("list = %+v", list)
}
empty := &Client{InstallersDir: filepath.Join(t.TempDir(), "none")}
if l, err := empty.ListCached(); err != nil || l != nil {
t.Fatalf("missing dir: %v %v", l, err)
}
}
func TestParseCtlLine(t *testing.T) {
// Formats from reims_util.rs:480-488 and its test at :777-786.
u, _ := ParseCtlLine("downloading the macOS Tahoe 26 recovery image: 45% of 2.0 GB")
if u.Kind != KindProgress || u.Step != "downloading the macOS Tahoe 26 recovery image" || u.Percent != 45 || u.Total != 2_000_000_000 {
t.Fatalf("percent line = %+v", u)
}
u, _ = ParseCtlLine("macos-tahoe: downloading OpenCore 1.0.8: 5% of 11 MB")
if u.Step != "macos-tahoe: downloading OpenCore 1.0.8" || u.Total != 11_000_000 {
t.Fatalf("ctl-prefixed line = %+v", u)
}
u, _ = ParseCtlLine("downloading the installer: 300 MB")
if u.Kind != KindProgress || u.Percent != -1 || u.Done != 300_000_000 {
t.Fatalf("size line = %+v", u)
}
u, _ = ParseCtlLine("creating the disk")
if u.Kind != KindNote || u.Message != "creating the disk" {
t.Fatalf("note = %+v", u)
}
if _, ok := ParseCtlLine(" "); ok {
t.Fatal("blank line")
}
}
// TestLockExcludesReimsCtl checks the lock against the real reims-ctl: while we
// hold installers/.lock, `reims-ctl downloads delete` must refuse a file as
// "being downloaded" (downloads.rs:139-159). Runs only with REIMS_GO_TEST_CTL
// set to a reims-ctl binary.
func TestLockExcludesReimsCtl(t *testing.T) {
ctl := os.Getenv("REIMS_GO_TEST_CTL")
if ctl == "" {
t.Skip("set REIMS_GO_TEST_CTL to a reims-ctl binary")
}
root := t.TempDir()
dataDir := filepath.Join(root, "data", "reims-ctl")
inst := filepath.Join(dataDir, "installers")
os.MkdirAll(inst, 0o755)
name := "macOS-Tahoe-online-installer.dmg"
os.WriteFile(filepath.Join(inst, name), []byte("x"), 0o644)
env := append(os.Environ(),
"REIMS_CTL_DATA_DIR="+dataDir,
"REIMS_CTL_CONFIG_DIR="+filepath.Join(root, "config"),
"XDG_CACHE_HOME="+filepath.Join(root, "cache"),
)
c := &Client{Ctl: ctl, Env: env, InstallersDir: inst}
release, err := c.lock(context.Background(), func(Update) {})
if err != nil {
t.Fatal(err)
}
err = c.DeleteCached(context.Background(), name, nil)
if err == nil || !strings.Contains(err.Error(), "being downloaded") {
release()
t.Fatalf("reims-ctl ignored our lock: %v", err)
}
release()
if err := c.DeleteCached(context.Background(), name, nil); err != nil {
t.Fatalf("delete after unlock: %v", err)
}
if _, err := os.Stat(filepath.Join(inst, name)); !os.IsNotExist(err) {
t.Fatal("file still there")
}
}