Delete functions that no product code calls, and fold "simple plus WithOptions" pairs into the one signature callers actually use. Rename the live macOS lifecycle functions that were labelled Legacy. The architecture tests now share one source loader and reject functions without a product caller and catalog messages nobody reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
2.5 KiB
Go
80 lines
2.5 KiB
Go
package driver
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
func TestRouterKeepsSameNameAcrossDrivers(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
for _, id := range []string{"test.driver.a", "test.driver.b"} {
|
|
id := id
|
|
if err := reg.Register(plugin.PluginDef{
|
|
Manifest: plugin.Manifest{ID: id + ".plugin", Version: "1.0.0"},
|
|
Source: plugin.SourceBuiltin, Desired: true,
|
|
Activate: func(_ context.Context, sc *plugin.Scope) error {
|
|
reg.RegisterGuestDriver(sc, id, &fakeDriver{id: id, vms: []VMInfo{{Name: "shared", State: "stopped"}}})
|
|
return nil
|
|
},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
reg.Reconcile(context.Background())
|
|
router := NewRouter(reg, nil)
|
|
vms, err := router.ListAllVMs(context.Background())
|
|
if err != nil || len(vms) != 2 {
|
|
t.Fatalf("ListAllVMs = %+v, %v", vms, err)
|
|
}
|
|
_, _, err = router.ResolveVM(context.Background(), "shared")
|
|
var ambiguous *AmbiguousVMError
|
|
if !errors.As(err, &ambiguous) || len(ambiguous.DriverIDs) != 2 {
|
|
t.Fatalf("ResolveVM error = %#v", err)
|
|
}
|
|
d, vm, err := router.ResolveRef(context.Background(), NewVMRef("test.driver.b", "shared"))
|
|
if err != nil || d.ID() != "test.driver.b" || vm.Ref().Key() != "test.driver.b:shared" {
|
|
t.Fatalf("ResolveRef = %v, %+v, %v", d, vm, err)
|
|
}
|
|
}
|
|
|
|
func TestRouterScansDeclaredVMRoots(t *testing.T) {
|
|
dataRoot := t.TempDir()
|
|
vmDir := filepath.Join(dataRoot, "data", "linux", "vms", "ubuntu")
|
|
if err := WriteOwnerDriver(vmDir, "test.driver.linux"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reg := plugin.NewRegistry(nil)
|
|
if err := reg.Register(plugin.PluginDef{Manifest: plugin.Manifest{
|
|
ID: "test.linux", Version: "1.0.0",
|
|
Contributes: plugin.Contributes{GuestDrivers: []plugin.GuestDriverContribution{{
|
|
ID: "test.driver.linux", VMRoots: []string{"data/linux/vms"},
|
|
}}},
|
|
}, Source: plugin.SourceBuiltin}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
router := NewRouter(reg, func() string { return dataRoot })
|
|
vms, err := router.ListAllVMs(context.Background())
|
|
if err != nil || len(vms) != 1 || !vms[0].DriverUnavailable {
|
|
t.Fatalf("ListAllVMs = %+v, %v", vms, err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dataRoot, "data", "reims-ctl", "vms")); !os.IsNotExist(err) {
|
|
t.Fatalf("unexpected implicit root: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestVMRefRoundTrip(t *testing.T) {
|
|
ref := NewVMRef("test.driver.linux", "ubuntu")
|
|
got, ok := ParseVMRef(ref.Key())
|
|
if !ok || got != ref {
|
|
t.Fatalf("ParseVMRef(%q) = %+v, %v", ref.Key(), got, ok)
|
|
}
|
|
if _, ok := ParseVMRef("ubuntu"); ok {
|
|
t.Fatal("bare name accepted as VMRef")
|
|
}
|
|
}
|