Files
Reims-Go/internal/runtime/kinds_test.go
T
Nwflower aacaec535d Merge origin/main into feat/vmref-driver-start
Adapt Intel VM suspend/resume to driver.SuspendDriver and lifecycle.Service, move native VM close confirmation into internal/host, migrate vm-suspend legacy capability detection, drop moved runtime build tooling, and regenerate i18n and Wails bindings.
2026-12-01 03:47:20 -07:00

109 lines
4.0 KiB
Go

package runtime
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/Nwflower/reims-go/internal/config"
)
func TestKindRegistryAndCapabilities(t *testing.T) {
kr := NewKindRegistry()
if _, ok := kr.Get(DefaultKind); !ok {
t.Fatal("missing builtin reims-vgpu kind")
}
if err := kr.Register(KindSpec{Kind: "qemu-ovmf", NameEn: "QEMU + OVMF", PluginID: "plugin.linux"}); err != nil {
t.Fatalf("register kind: %v", err)
}
if len(kr.List()) != 2 {
t.Fatalf("expected 2 kinds, got %d", len(kr.List()))
}
kr.Unregister("qemu-ovmf", "plugin.linux")
if _, ok := kr.Get("qemu-ovmf"); ok {
t.Fatal("expected qemu-ovmf to be unregistered")
}
m := Manifest{Commit: "abc1234", Patches: []Patch{{Name: "qemu-whpx-nested-virt-nonfatal.patch"}}}
if m.EffectiveKind() != DefaultKind {
t.Fatalf("EffectiveKind = %q", m.EffectiveKind())
}
if len(m.EffectiveCapabilities()) != 0 {
t.Fatalf("patch names must not infer capabilities: %v", m.EffectiveCapabilities())
}
declared := Manifest{Capabilities: []string{"nested-virt-nonfatal", "win32-unicode-input", "nested-virt-nonfatal"}}
if !declared.HasCapability("nested-virt-nonfatal") || !declared.HasCapability("win32-unicode-input") {
t.Fatalf("declared capabilities = %v", declared.EffectiveCapabilities())
}
if got := len(declared.EffectiveCapabilities()); got != 2 {
t.Fatalf("capability dedup = %d", got)
}
if !(Manifest{Capabilities: []string{"vm-suspend"}}).HasCapability("vm-suspend") {
t.Fatal("explicit vm-suspend capability should be reported")
}
}
func TestMultiKindCoexistenceAndCompatibility(t *testing.T) {
root := t.TempDir()
cfg := &config.Config{DataRoot: root, ActiveRuntime: "abc1234"}
if err := cfg.EnsureDirs(); err != nil {
t.Fatal(err)
}
// Legacy layout: runtimes/abc1234/manifest.json
legacyDir := filepath.Join(cfg.RuntimesDir(), "abc1234")
_ = os.MkdirAll(legacyDir, 0o755)
legacyM, _ := json.Marshal(Manifest{SchemaVersion: 2, Commit: "abc1234", BuiltAt: "2026-11-28T00:00:00Z"})
_ = os.WriteFile(filepath.Join(legacyDir, "manifest.json"), legacyM, 0o644)
// New kind layout: runtimes/qemu-ovmf/9.2.0/manifest.json
ovmfDir := filepath.Join(cfg.RuntimesDir(), "qemu-ovmf", "9.2.0")
_ = os.MkdirAll(ovmfDir, 0o755)
ovmfM, _ := json.Marshal(Manifest{
SchemaVersion: 2,
Kind: "qemu-ovmf",
RuntimeAPI: "2.0.0",
Commit: "9.2.0",
BuiltAt: "2026-11-29T00:00:00Z",
Capabilities: []string{"whpx", "virtio-gpu"},
})
_ = os.WriteFile(filepath.Join(ovmfDir, "manifest.json"), ovmfM, 0o644)
list, err := List(cfg)
if err != nil || len(list) != 2 {
t.Fatalf("List = %d (err=%v), want 2", len(list), err)
}
reimsRT, err := ResolveForKind(cfg, DefaultKind)
if err != nil || reimsRT.Commit != "abc1234" {
t.Fatalf("ResolveForKind(reims-vgpu) = %+v, err=%v", reimsRT, err)
}
ovmfRT, err := ResolveForKind(cfg, "qemu-ovmf")
if err != nil || ovmfRT.Commit != "9.2.0" {
t.Fatalf("ResolveForKind(qemu-ovmf) = %+v, err=%v", ovmfRT, err)
}
// Compatibility checks.
if err := CheckCompatibility("qemu-ovmf", "^2.0.0", *ovmfRT); err != nil {
t.Fatalf("expected compatible, got %v", err)
}
if err := CheckCompatibility("qemu-ovmf", "^1.0.0", *ovmfRT); err == nil {
t.Fatal("expected incompatible runtime_api error")
}
if err := CheckCompatibility("reims-vgpu", "^1.0.0", *ovmfRT); err == nil {
t.Fatal("expected kind mismatch error")
}
// Third-party provenance guard.
if err := ValidateThirdPartyProvenance(Manifest{Origin: "third-party", Channel: "stable", License: "GPL-2.0", SourcePackage: "src.tar.zst"}); err == nil {
t.Fatal("third-party cannot enter stable channel")
}
if err := ValidateThirdPartyProvenance(Manifest{Origin: "third-party", Channel: "community", License: "GPL-2.0"}); err == nil {
t.Fatal("third-party missing source_package should fail")
}
if err := ValidateThirdPartyProvenance(Manifest{Origin: "third-party", Channel: "community", License: "GPL-2.0", SourcePackage: "src.tar.zst"}); err != nil {
t.Fatalf("valid third-party provenance rejected: %v", err)
}
}