203 lines
7.0 KiB
Go
203 lines
7.0 KiB
Go
package runtime
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
)
|
|
|
|
func writeFakeRuntime(t *testing.T, dir string, m Manifest) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Join(dir, "bin"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := json.Marshal(m)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "manifest.json"), raw, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "bin", "reims-ctl.exe"), []byte("dummy"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRemoveRuntime(t *testing.T) {
|
|
root := t.TempDir()
|
|
cfg := &config.Config{DataRoot: root, ActiveRuntime: "3731afd-p3"}
|
|
if err := cfg.EnsureDirs(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
oldDir := filepath.Join(cfg.RuntimesDir(), "3731afd")
|
|
newDir := filepath.Join(cfg.RuntimesDir(), "3731afd-p3")
|
|
writeFakeRuntime(t, oldDir, Manifest{SchemaVersion: 2, Commit: "3731afd", BuiltAt: "2026-11-28T00:00:00Z"})
|
|
writeFakeRuntime(t, newDir, Manifest{SchemaVersion: 2, Commit: "3731afd-p3", BuiltAt: "2026-11-30T00:00:00Z"})
|
|
|
|
// Active runtime cannot be removed.
|
|
if _, err := Remove(cfg, "3731afd-p3"); !errors.Is(err, ErrRemoveActive) {
|
|
t.Fatalf("Remove(active) err = %v, want ErrRemoveActive", err)
|
|
}
|
|
if _, err := os.Stat(newDir); err != nil {
|
|
t.Fatalf("active runtime dir should remain: %v", err)
|
|
}
|
|
|
|
// Missing runtime returns ErrNotFound.
|
|
if _, err := Remove(cfg, "does-not-exist"); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("Remove(missing) err = %v, want ErrNotFound", err)
|
|
}
|
|
|
|
// Inactive runtime is removed cleanly.
|
|
removed, err := Remove(cfg, "3731afd")
|
|
if err != nil {
|
|
t.Fatalf("Remove(inactive) err = %v", err)
|
|
}
|
|
if removed.Commit != "3731afd" {
|
|
t.Fatalf("removed commit = %q, want 3731afd", removed.Commit)
|
|
}
|
|
if _, err := os.Stat(oldDir); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("expected old runtime dir removed, stat err = %v", err)
|
|
}
|
|
|
|
// Only remaining runtime cannot be removed even if ActiveRuntime is cleared.
|
|
cfg.ActiveRuntime = ""
|
|
if _, err := Remove(cfg, "3731afd-p3"); !errors.Is(err, ErrRemoveActive) {
|
|
t.Fatalf("Remove(only remaining) err = %v, want ErrRemoveActive", err)
|
|
}
|
|
}
|
|
|
|
func TestRemoveAfterExternalActivation(t *testing.T) {
|
|
for _, selected := range []string{"next", DefaultKind + "/next", "other/next"} {
|
|
t.Run(selected, func(t *testing.T) {
|
|
cfg := config.Config{DataRoot: t.TempDir(), ActiveRuntime: "old"}
|
|
oldDir := filepath.Join(cfg.RuntimesDir(), "old")
|
|
nextDir := filepath.Join(cfg.RuntimesDir(), "next")
|
|
writeFakeRuntime(t, oldDir, Manifest{Commit: "old"})
|
|
if selected == "other/next" {
|
|
nextDir = filepath.Join(cfg.RuntimesDir(), "other", "next")
|
|
// Keep the default kind's implicit single-runtime fallback out of this case.
|
|
writeFakeRuntime(t, filepath.Join(cfg.RuntimesDir(), "spare"), Manifest{Commit: "spare"})
|
|
}
|
|
writeFakeRuntime(t, nextDir, Manifest{Commit: "next"})
|
|
cli := cfg
|
|
if err := Activate(t.Context(), &cli, selected, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list, err := List(&cli)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, item := range list {
|
|
if item.Active != (item.Dir == nextDir) {
|
|
t.Fatalf("runtime %s active = %t after selecting %s", item.Dir, item.Active, selected)
|
|
}
|
|
}
|
|
if _, err := Remove(&cfg, selected); !errors.Is(err, ErrRemoveActive) {
|
|
t.Fatalf("removing the newly active runtime returned %v", err)
|
|
}
|
|
if _, err := os.Stat(nextDir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Remove(&cfg, "old"); err != nil {
|
|
t.Fatalf("removing the formerly selected runtime: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRemoveWithUnreadableSelection(t *testing.T) {
|
|
cfg := config.Config{DataRoot: t.TempDir(), ActiveRuntime: "old"}
|
|
nextDir := filepath.Join(cfg.RuntimesDir(), "next")
|
|
writeFakeRuntime(t, filepath.Join(cfg.RuntimesDir(), "old"), Manifest{Commit: "old"})
|
|
writeFakeRuntime(t, nextDir, Manifest{Commit: "next"})
|
|
if err := os.WriteFile(filepath.Join(cfg.DataRoot, "reims-go.json"), []byte("{"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Remove(&cfg, "next"); err == nil {
|
|
t.Fatal("removed a runtime without a readable selection")
|
|
}
|
|
if _, err := os.Stat(nextDir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestRemoveRuntimeInUseWindows(t *testing.T) {
|
|
if runtime.GOOS != "windows" {
|
|
t.Skip("Windows file locking semantics only")
|
|
}
|
|
root := t.TempDir()
|
|
cfg := &config.Config{DataRoot: root, ActiveRuntime: "new-rt"}
|
|
if err := cfg.EnsureDirs(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
oldDir := filepath.Join(cfg.RuntimesDir(), "old-rt")
|
|
newDir := filepath.Join(cfg.RuntimesDir(), "new-rt")
|
|
writeFakeRuntime(t, oldDir, Manifest{SchemaVersion: 2, Commit: "old-rt", BuiltAt: "2026-11-28T00:00:00Z"})
|
|
writeFakeRuntime(t, newDir, Manifest{SchemaVersion: 2, Commit: "new-rt", BuiltAt: "2026-11-30T00:00:00Z"})
|
|
|
|
// Hold an open handle inside oldDir to simulate a running VM process.
|
|
lockedFile, err := os.Open(filepath.Join(oldDir, "bin", "reims-ctl.exe"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer lockedFile.Close()
|
|
|
|
if _, err := Remove(cfg, "old-rt"); !errors.Is(err, ErrRuntimeInUse) {
|
|
t.Fatalf("Remove(locked) err = %v, want ErrRuntimeInUse", err)
|
|
}
|
|
// Directory must remain completely intact when rename fails.
|
|
if _, err := os.Stat(filepath.Join(oldDir, "manifest.json")); err != nil {
|
|
t.Fatalf("locked runtime should remain intact: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestReplaceRuntimeInUseWindows(t *testing.T) {
|
|
if runtime.GOOS != "windows" {
|
|
t.Skip("Windows file locking semantics only")
|
|
}
|
|
root := t.TempDir()
|
|
finalDir, bundleDir := filepath.Join(root, "installed"), filepath.Join(root, "bundle")
|
|
writeFakeRuntime(t, finalDir, Manifest{SchemaVersion: 2, Commit: "original"})
|
|
writeFakeRuntime(t, bundleDir, Manifest{SchemaVersion: 2, Commit: "replacement"})
|
|
locked, err := os.Open(filepath.Join(finalDir, "bin", "reims-ctl.exe"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer locked.Close()
|
|
|
|
err = replaceRuntimeDirectory(bundleDir, finalDir)
|
|
if !errors.Is(err, ErrRuntimeInUse) {
|
|
t.Fatalf("want ErrRuntimeInUse, got %v", err)
|
|
}
|
|
for dir, want := range map[string]string{finalDir: "original", bundleDir: "replacement"} {
|
|
manifest, err := readManifest(filepath.Join(dir, "manifest.json"))
|
|
if err != nil || manifest.Commit != want {
|
|
t.Fatalf("runtime %s changed: %+v, %v", dir, manifest, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReplaceRuntimeRecoversBackupAndRollsBack(t *testing.T) {
|
|
root := t.TempDir()
|
|
finalDir := filepath.Join(root, "installed")
|
|
backup := finalDir + ".replace.tmp"
|
|
writeFakeRuntime(t, backup, Manifest{SchemaVersion: 2, Commit: "original"})
|
|
if err := replaceRuntimeDirectory(filepath.Join(root, "missing-bundle"), finalDir); err == nil {
|
|
t.Fatal("activation with a missing bundle succeeded")
|
|
}
|
|
manifest, err := readManifest(filepath.Join(finalDir, "manifest.json"))
|
|
if err != nil || manifest.Commit != "original" {
|
|
t.Fatalf("previous runtime was not restored: %+v, %v", manifest, err)
|
|
}
|
|
if _, err := os.Stat(backup); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("rollback left a backup in place: %v", err)
|
|
}
|
|
}
|