113 lines
4.7 KiB
Go
113 lines
4.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
|
|
}
|
|
|
|
var storageCaps = []string{driver.CapSnapshot, driver.CapCowClone, driver.CapResize}
|
|
|
|
func (d *testDriver) Phase(_ context.Context, _ string) (string, error) {
|
|
if d.phase == "" {
|
|
return driver.PhaseNormal, nil
|
|
}
|
|
return d.phase, nil
|
|
}
|
|
|
|
func TestStorageOperationsResolveSameNameByDriver(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
a := &testDriver{id: "test.driver.a", caps: storageCaps, vms: []driver.VMInfo{{Name: "dev", DriverID: "test.driver.a", State: "stopped"}}}
|
|
b := &testDriver{id: "test.driver.b", caps: storageCaps, 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")
|
|
}
|
|
}
|
|
|
|
// A driver that satisfies a storage interface in Go but did not declare the
|
|
// matching capability bit is refused by the host.
|
|
func TestStorageRefusesUndeclaredCapability(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
d := &testDriver{id: "test.driver.nocap", caps: []string{driver.CapSnapshot}, vms: []driver.VMInfo{{Name: "dev", DriverID: "test.driver.nocap", State: "stopped"}}}
|
|
registerTestDriver(t, reg, d)
|
|
reg.Reconcile(context.Background())
|
|
svc := New(reg, driver.NewRouter(reg, nil), nil)
|
|
ctx := context.Background()
|
|
ref := driver.NewVMRef(d.id, "dev")
|
|
if _, err := svc.ListSnapshots(ctx, ref); err != nil {
|
|
t.Fatalf("declared snapshot capability refused: %v", err)
|
|
}
|
|
if err := svc.CloneCoW(ctx, ref, "copy", io.Discard, io.Discard); err == nil {
|
|
t.Fatal("CloneCoW ran without the cow_clone capability")
|
|
}
|
|
if _, err := svc.ResizeDisk(ctx, ref, "128G", io.Discard, io.Discard); err == nil {
|
|
t.Fatal("ResizeDisk ran without the resize capability")
|
|
}
|
|
if _, err := svc.CompactDisk(ctx, ref, io.Discard, io.Discard); err == nil {
|
|
t.Fatal("CompactDisk ran without the resize capability")
|
|
}
|
|
if len(d.storageCalls) != 1 {
|
|
t.Fatalf("driver was called for refused operations: %v", d.storageCalls)
|
|
}
|
|
}
|
|
|
|
func TestPhaseUsesDriverReporter(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
d := &testDriver{id: "test.driver.phase", phase: driver.PhaseRecovery, vms: []driver.VMInfo{{Name: "dev", DriverID: "test.driver.phase", State: "running"}}}
|
|
registerTestDriver(t, reg, d)
|
|
reg.Reconcile(context.Background())
|
|
svc := New(reg, driver.NewRouter(reg, nil), nil)
|
|
got, err := svc.Phase(context.Background(), driver.NewVMRef(d.id, "dev"))
|
|
if err != nil || got != driver.PhaseRecovery {
|
|
t.Fatalf("Phase = %q, %v", got, err)
|
|
}
|
|
}
|