90 lines
3.8 KiB
Go
90 lines
3.8 KiB
Go
package driver
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
type fakeDriver struct {
|
|
id string
|
|
vms []VMInfo
|
|
}
|
|
|
|
func (f *fakeDriver) ID() string { return f.id }
|
|
func (f *fakeDriver) DisplayName() plugin.LocalizedText { return plugin.LocalizedText{EnUS: f.id} }
|
|
func (f *fakeDriver) RuntimeKind() string { return "qemu-ovmf" }
|
|
func (f *fakeDriver) RuntimeAPIRange() string { return "^1.0.0" }
|
|
func (f *fakeDriver) Capabilities() []string { return []string{CapCloudInit, CapSSH} }
|
|
func (f *fakeDriver) CreateSchema() *plugin.SettingsSchema { return nil }
|
|
func (f *fakeDriver) SettingsSchema() *plugin.SettingsSchema { return nil }
|
|
func (f *fakeDriver) List(context.Context) ([]VMInfo, error) { return f.vms, nil }
|
|
func (f *fakeDriver) Show(_ context.Context, n string) (VMInfo, error) { return VMInfo{Name: n, DriverID: f.id}, nil }
|
|
func (f *fakeDriver) ValidateCreate(context.Context, CreateRequest) []ValidationIssue { return nil }
|
|
func (f *fakeDriver) Create(context.Context, CreateRequest, io.Writer, io.Writer) error { return nil }
|
|
func (f *fakeDriver) ValidateSet(context.Context, SetRequest) []ValidationIssue { return nil }
|
|
func (f *fakeDriver) Set(context.Context, SetRequest, io.Writer, io.Writer) error { return nil }
|
|
func (f *fakeDriver) Start(context.Context, string, io.Writer, io.Writer) error { return nil }
|
|
func (f *fakeDriver) Stop(context.Context, string, bool, io.Writer, io.Writer) error { return nil }
|
|
func (f *fakeDriver) Delete(context.Context, string, bool, io.Writer, io.Writer) error { return nil }
|
|
|
|
func TestOwnerMarkerZeroMigrationAndRouterUnavailable(t *testing.T) {
|
|
vmRoot := t.TempDir()
|
|
legacyVM := filepath.Join(vmRoot, "macos-tahoe")
|
|
_ = os.MkdirAll(legacyVM, 0o755)
|
|
if got := ReadOwnerDriver(legacyVM); got != BuiltinReimsMacOSDriverID {
|
|
t.Fatalf("unmarked VM owner = %q, want %q", got, BuiltinReimsMacOSDriverID)
|
|
}
|
|
|
|
linuxVM := filepath.Join(vmRoot, "ubuntu-dev")
|
|
if err := WriteOwnerDriver(linuxVM, "reims.driver.linux"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := ReadOwnerDriver(linuxVM); got != "reims.driver.linux" {
|
|
t.Fatalf("marked VM owner = %q, want reims.driver.linux", got)
|
|
}
|
|
|
|
reg := plugin.NewRegistry(nil)
|
|
_ = reg.Register(plugin.PluginDef{
|
|
Manifest: plugin.Manifest{ID: "builtin.driver.reims-macos", Version: "1.0.0"},
|
|
Source: plugin.SourceBuiltin,
|
|
Desired: true,
|
|
Activate: func(_ context.Context, sc *plugin.Scope) error {
|
|
reg.RegisterGuestDriver(sc, BuiltinReimsMacOSDriverID, &fakeDriver{
|
|
id: BuiltinReimsMacOSDriverID,
|
|
vms: []VMInfo{{Name: "macos-tahoe", State: "stopped"}},
|
|
})
|
|
return nil
|
|
},
|
|
})
|
|
reg.Reconcile(context.Background())
|
|
|
|
router := NewRouter(reg, func() string { return vmRoot })
|
|
all, err := router.ListAllVMs(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(all) != 2 {
|
|
t.Fatalf("expected 2 VMs (1 active + 1 unavailable), got %+v", all)
|
|
}
|
|
foundUnavail := false
|
|
for _, vm := range all {
|
|
if vm.Name == "ubuntu-dev" {
|
|
foundUnavail = vm.DriverUnavailable && vm.State == "driver_unavailable" && vm.DriverID == "reims.driver.linux"
|
|
}
|
|
}
|
|
if !foundUnavail {
|
|
t.Fatalf("ubuntu-dev should be marked driver_unavailable: %+v", all)
|
|
}
|
|
|
|
// CheckDriverInUse must detect ubuntu-dev even when driver is disabled.
|
|
inUse, _, err := router.CheckDriverInUse(context.Background(), "reims.driver.linux")
|
|
if err != nil || !inUse {
|
|
t.Fatalf("expected reims.driver.linux in use, got inUse=%v err=%v", inUse, err)
|
|
}
|
|
}
|