Add typed snapshot, CoW clone, resize and compaction contracts. Move macOS disk mechanics behind the driver, preserve progress and pending UI behavior, and pass driver identity through App, CLI and frontend entry points.
65 lines
2.7 KiB
Go
65 lines
2.7 KiB
Go
package lifecycle
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
func (d *testDriver) ListSnapshots(_ context.Context, name string) ([]driver.DiskSnapshot, error) {
|
|
d.storageCalls = append(d.storageCalls, "list:"+name)
|
|
return []driver.DiskSnapshot{{ID: d.id, Name: "before"}}, nil
|
|
}
|
|
func (d *testDriver) RunSnapshot(_ context.Context, name, operation, snapName string, _, _ io.Writer) error {
|
|
d.storageCalls = append(d.storageCalls, operation+":"+name+":"+snapName)
|
|
return nil
|
|
}
|
|
func (d *testDriver) CloneCoW(_ context.Context, sourceName, cloneName string, _, _ io.Writer) error {
|
|
d.storageCalls = append(d.storageCalls, "clone:"+sourceName+":"+cloneName)
|
|
return nil
|
|
}
|
|
func (d *testDriver) ResizeDisk(_ context.Context, name, newSize string, _, _ io.Writer) (driver.DiskResizeResult, error) {
|
|
d.storageCalls = append(d.storageCalls, "resize:"+name+":"+newSize)
|
|
return driver.DiskResizeResult{Path: d.id, NewVirtualBytes: 1}, nil
|
|
}
|
|
func (d *testDriver) CompactDisk(_ context.Context, name string, _, _ io.Writer) (driver.DiskCompactResult, error) {
|
|
d.storageCalls = append(d.storageCalls, "compact:"+name)
|
|
return driver.DiskCompactResult{Path: d.id}, nil
|
|
}
|
|
|
|
func TestStorageOperationsResolveSameNameByDriver(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
a := &testDriver{id: "test.driver.a", vms: []driver.VMInfo{{Name: "dev", DriverID: "test.driver.a", State: "stopped"}}}
|
|
b := &testDriver{id: "test.driver.b", vms: []driver.VMInfo{{Name: "dev", DriverID: "test.driver.b", State: "stopped"}}}
|
|
registerTestDriver(t, reg, a)
|
|
registerTestDriver(t, reg, b)
|
|
reg.Reconcile(context.Background())
|
|
svc := New(reg, driver.NewRouter(reg, nil), nil)
|
|
ctx := context.Background()
|
|
snaps, err := svc.ListSnapshots(ctx, driver.NewVMRef(a.id, "dev"))
|
|
if err != nil || len(snaps) != 1 || snaps[0].ID != a.id {
|
|
t.Fatalf("ListSnapshots = %+v, %v", snaps, err)
|
|
}
|
|
if err := svc.RunSnapshot(ctx, driver.NewVMRef(a.id, "dev"), "create", "before", io.Discard, io.Discard); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := svc.CloneCoW(ctx, driver.NewVMRef(a.id, "dev"), "copy", io.Discard, io.Discard); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := svc.ResizeDisk(ctx, driver.NewVMRef(b.id, "dev"), "128G", io.Discard, io.Discard); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := svc.CompactDisk(ctx, driver.NewVMRef(b.id, "dev"), io.Discard, io.Discard); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(a.storageCalls) != 3 || len(b.storageCalls) != 2 {
|
|
t.Fatalf("calls a=%v b=%v", a.storageCalls, b.storageCalls)
|
|
}
|
|
if _, err := svc.ListSnapshots(ctx, driver.VMRef{Name: "dev"}); err == nil {
|
|
t.Fatal("ambiguous same-name VM was resolved without a driver")
|
|
}
|
|
}
|