83 lines
2.7 KiB
Go
83 lines
2.7 KiB
Go
package driver
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
// hostedFake is a hosted driver that lists bare entries and returns the full
|
|
// VM, endpoints included, from Show.
|
|
type hostedFake struct {
|
|
fakeDriver
|
|
shows atomic.Int32
|
|
}
|
|
|
|
func (h *hostedFake) Capabilities() []string { return []string{CapHosted, CapSSH} }
|
|
func (h *hostedFake) Show(_ context.Context, n string) (VMInfo, error) {
|
|
h.shows.Add(1)
|
|
ready := true
|
|
return VMInfo{Name: n, DriverID: h.id, State: "running", Ready: &ready,
|
|
Endpoints: []VMEndpoint{{Kind: EndpointWSL, Distro: n, Ready: true}}}, nil
|
|
}
|
|
|
|
func routerWithHosted(t *testing.T, d *hostedFake) *Router {
|
|
t.Helper()
|
|
reg := plugin.NewRegistry(nil)
|
|
_ = reg.Register(plugin.PluginDef{
|
|
Manifest: plugin.Manifest{ID: d.id + ".plugin", Version: "1.0.0"},
|
|
Source: plugin.SourceBuiltin, Desired: true,
|
|
Activate: func(_ context.Context, sc *plugin.Scope) error {
|
|
reg.RegisterGuestDriver(sc, d.id, d)
|
|
return nil
|
|
},
|
|
})
|
|
reg.Reconcile(context.Background())
|
|
return NewRouter(reg, nil)
|
|
}
|
|
|
|
func TestHostedVMWithoutReportFallsBackToShow(t *testing.T) {
|
|
d := &hostedFake{fakeDriver: fakeDriver{id: "test.driver.hosted", vms: []VMInfo{{Name: "dev", State: "running"}}}}
|
|
router := routerWithHosted(t, d)
|
|
table := NewStateTable()
|
|
router.SetStateTable(table)
|
|
|
|
vms, err := router.ListAllVMs(context.Background())
|
|
if err != nil || len(vms) != 1 {
|
|
t.Fatalf("ListAllVMs = %+v, %v", vms, err)
|
|
}
|
|
if d.shows.Load() != 1 || len(vms[0].Endpoints) != 1 || vms[0].Ready == nil || !*vms[0].Ready {
|
|
t.Fatalf("hosted VM without a report did not get Show's detail: shows=%d vm=%+v", d.shows.Load(), vms[0])
|
|
}
|
|
|
|
// A fresh report from the driver makes the Show call unnecessary.
|
|
ready := false
|
|
if err := table.Report(ReportedState{Ref: VMRef{DriverID: d.id, Name: "dev"}, State: "running", Ready: &ready}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
vms, err = router.ListAllVMs(context.Background())
|
|
if err != nil || len(vms) != 1 {
|
|
t.Fatalf("ListAllVMs = %+v, %v", vms, err)
|
|
}
|
|
if d.shows.Load() != 1 {
|
|
t.Fatalf("a fresh report still triggered Show (%d calls)", d.shows.Load())
|
|
}
|
|
if vms[0].Ready == nil || *vms[0].Ready {
|
|
t.Fatalf("the report was not merged: %+v", vms[0])
|
|
}
|
|
}
|
|
|
|
func TestNonHostedVMNeverFallsBackToShow(t *testing.T) {
|
|
d := &hostedFake{fakeDriver: fakeDriver{id: "test.driver.plain", vms: []VMInfo{{Name: "dev", State: "running", Capabilities: []string{CapSSH}}}}}
|
|
router := routerWithHosted(t, d)
|
|
router.SetStateTable(NewStateTable())
|
|
if _, err := router.ListAllVMs(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if d.shows.Load() != 0 {
|
|
t.Fatalf("a non-hosted VM triggered %d Show calls", d.shows.Load())
|
|
}
|
|
}
|