Keep same-named VMs from different drivers, reject ambiguous bare-name resolution, and add exact ResolveRef routing with driver-qualified keys. Declare DataRoot-relative VM roots in guest driver manifests, validate traversal and absolute paths, and scan declarations for unavailable drivers. Add data layout v3 for driver-qualified per-VM plugin settings, preserve explicit values during migration, and pass VMRef keys from the settings UI.
80 lines
2.6 KiB
Go
80 lines
2.6 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 := NewRouterWithDataRoot(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")
|
|
}
|
|
}
|