Add lifecycle Delete and driver-qualified App Restart/Delete methods, CLI vm delete --driver/--keep-files, and frontend driver-aware actions. Extend the second-driver App acceptance to restart and delete while preserving legacy macOS restart fallback and cleanup.
100 lines
3.7 KiB
Go
100 lines
3.7 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
vmlifecycle "github.com/Nwflower/reims-go/internal/lifecycle"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
type appSecondDriver struct {
|
|
id string
|
|
vms []driver.VMInfo
|
|
starts, stops, deletes int
|
|
}
|
|
|
|
func (d *appSecondDriver) ID() string { return d.id }
|
|
func (d *appSecondDriver) DisplayName() plugin.LocalizedText { return plugin.LocalizedText{EnUS: d.id} }
|
|
func (d *appSecondDriver) RuntimeKind() string { return "test-runtime" }
|
|
func (d *appSecondDriver) RuntimeAPIRange() string { return "^1.0.0" }
|
|
func (d *appSecondDriver) Capabilities() []string { return nil }
|
|
func (d *appSecondDriver) CreateSchema() *plugin.SettingsSchema { return nil }
|
|
func (d *appSecondDriver) SettingsSchema() *plugin.SettingsSchema { return nil }
|
|
func (d *appSecondDriver) List(context.Context) ([]driver.VMInfo, error) { return d.vms, nil }
|
|
func (d *appSecondDriver) Show(_ context.Context, name string) (driver.VMInfo, error) {
|
|
for _, vm := range d.vms {
|
|
if vm.Name == name {
|
|
return vm, nil
|
|
}
|
|
}
|
|
return driver.VMInfo{}, os.ErrNotExist
|
|
}
|
|
func (d *appSecondDriver) ValidateCreate(context.Context, driver.CreateRequest) []driver.ValidationIssue {
|
|
return nil
|
|
}
|
|
func (d *appSecondDriver) Create(context.Context, driver.CreateRequest, io.Writer, io.Writer) error {
|
|
return nil
|
|
}
|
|
func (d *appSecondDriver) ValidateSet(context.Context, driver.SetRequest) []driver.ValidationIssue {
|
|
return nil
|
|
}
|
|
func (d *appSecondDriver) Set(context.Context, driver.SetRequest, io.Writer, io.Writer) error {
|
|
return nil
|
|
}
|
|
func (d *appSecondDriver) Start(context.Context, driver.StartRequest) (driver.StartResult, error) {
|
|
d.starts++
|
|
return driver.StartResult{}, nil
|
|
}
|
|
func (d *appSecondDriver) Stop(context.Context, string, bool, io.Writer, io.Writer) error {
|
|
d.stops++
|
|
return nil
|
|
}
|
|
func (d *appSecondDriver) Delete(context.Context, string, bool, io.Writer, io.Writer) error {
|
|
d.deletes++
|
|
return nil
|
|
}
|
|
|
|
func TestAppSecondDriverUserEntry(t *testing.T) {
|
|
const driverID = "test.driver.linux"
|
|
drv := &appSecondDriver{id: driverID, vms: []driver.VMInfo{{Name: "ubuntu", DriverID: driverID, Directory: t.TempDir(), State: "stopped"}}}
|
|
reg := plugin.NewRegistry(nil)
|
|
if err := reg.Register(plugin.PluginDef{Manifest: plugin.Manifest{ID: "test.linux", Version: "1.0.0"},
|
|
Source: plugin.SourceBuiltin, Desired: true, Activate: func(_ context.Context, sc *plugin.Scope) error {
|
|
reg.RegisterGuestDriver(sc, driverID, drv)
|
|
return nil
|
|
}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reg.Reconcile(context.Background())
|
|
router := driver.NewRouter(reg, nil)
|
|
a := &App{cfg: &config.Config{DataRoot: t.TempDir()}, lifecycleSvc: vmlifecycle.New(reg, router, nil),
|
|
transitions: map[string]string{}, pendingChanges: map[string][]PendingChange{}}
|
|
vms, err := a.listVMsInternal(context.Background())
|
|
if err != nil || len(vms) != 1 || vms[0].DriverID != driverID {
|
|
t.Fatalf("list = %+v, %v", vms, err)
|
|
}
|
|
if err := a.StartVMWithDriver(driverID, "ubuntu"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.StopVMWithDriver(driverID, "ubuntu", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.startHandedVM(driverID, "ubuntu"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.RestartVMWithDriver(driverID, "ubuntu"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.DeleteVMWithDriver(driverID, "ubuntu", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if drv.starts != 3 || drv.stops != 2 || drv.deletes != 1 {
|
|
t.Fatalf("starts=%d stops=%d deletes=%d", drv.starts, drv.stops, drv.deletes)
|
|
}
|
|
}
|