refactor vcenter simulator setup

This commit is contained in:
sylviamoss 2020-09-22 16:44:03 +02:00
parent 7b1af2c4e8
commit b881a59bc8
4 changed files with 77 additions and 92 deletions

View File

@ -50,24 +50,73 @@ func newVMName() string {
return fmt.Sprintf("test-%v", rand.Intn(1000)) return fmt.Sprintf("test-%v", rand.Intn(1000))
} }
func NewSimulatorServer(model *simulator.Model) (*simulator.Server, error) { type VCenterSimulator struct {
err := model.Create() model *simulator.Model
server *simulator.Server
driver *VCenterDriver
}
func NewCustomVCenterSimulator(model *simulator.Model) (*VCenterSimulator, error) {
sim := new(VCenterSimulator)
sim.model = model
server, err := sim.NewSimulatorServer()
if err != nil {
sim.Close()
return nil, err
}
sim.server = server
driver, err := sim.NewSimulatorDriver()
if err != nil {
sim.Close()
return nil, err
}
sim.driver = driver
return sim, nil
}
func NewVCenterSimulator() (*VCenterSimulator, error) {
model := simulator.VPX()
model.Machine = 1
return NewCustomVCenterSimulator(model)
}
func (s *VCenterSimulator) Close() {
if s.model != nil {
s.model.Remove()
}
if s.server != nil {
s.server.Close()
}
}
//Simulator shortcut to choose any pre created VM.
func (s *VCenterSimulator) ChooseSimulatorPreCreatedVM() (VirtualMachine, *simulator.VirtualMachine) {
machine := simulator.Map.Any("VirtualMachine").(*simulator.VirtualMachine)
ref := machine.Reference()
vm := s.driver.NewVM(&ref)
return vm, machine
}
func (s *VCenterSimulator) NewSimulatorServer() (*simulator.Server, error) {
err := s.model.Create()
if err != nil { if err != nil {
return nil, err return nil, err
} }
model.Service.RegisterEndpoints = true s.model.Service.RegisterEndpoints = true
model.Service.TLS = new(tls.Config) s.model.Service.TLS = new(tls.Config)
model.Service.ServeMux = http.NewServeMux() s.model.Service.ServeMux = http.NewServeMux()
return model.Service.NewServer(), nil return s.model.Service.NewServer(), nil
} }
func NewSimulatorDriver(s *simulator.Server) (*VCenterDriver, error) { func (s *VCenterSimulator) NewSimulatorDriver() (*VCenterDriver, error) {
ctx := context.TODO() ctx := context.TODO()
user := &url.Userinfo{} user := &url.Userinfo{}
s.URL.User = user s.server.URL.User = user
soapClient := soap.NewClient(s.URL, true) soapClient := soap.NewClient(s.server.URL, true)
vimClient, err := vim25.NewClient(ctx, soapClient) vimClient, err := vim25.NewClient(ctx, soapClient)
if err != nil { if err != nil {
return nil, err return nil, err
@ -104,11 +153,3 @@ func NewSimulatorDriver(s *simulator.Server) (*VCenterDriver, error) {
} }
return d, nil return d, nil
} }
//Simulator shortcut to choose any pre created VM.
func ChooseSimulatorPreCreatedVM(driverSim *VCenterDriver) VirtualMachine {
machine := simulator.Map.Any("VirtualMachine").(*simulator.VirtualMachine)
ref := machine.Reference()
vm := driverSim.NewVM(&ref)
return vm
}

View File

@ -7,21 +7,13 @@ import (
) )
func TestVCenterDriver_FindResourcePool(t *testing.T) { func TestVCenterDriver_FindResourcePool(t *testing.T) {
model := simulator.VPX() sim, err := NewVCenterSimulator()
defer model.Remove()
s, err := NewSimulatorServer(model)
if err != nil { if err != nil {
t.Fatalf("should not fail: %s", err.Error()) t.Fatalf("should not fail: %s", err.Error())
} }
defer s.Close() defer sim.Close()
driverSim, err := NewSimulatorDriver(s) res, err := sim.driver.FindResourcePool("", "DC0_H0", "")
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
res, err := driverSim.FindResourcePool("", "DC0_H0", "")
if err != nil { if err != nil {
t.Fatalf("should not fail: %s", err.Error()) t.Fatalf("should not fail: %s", err.Error())
} }
@ -47,19 +39,13 @@ func TestVCenterDriver_FindResourcePoolStandaloneESX(t *testing.T) {
model.DelayConfig.MethodDelay = opts.DelayConfig.MethodDelay model.DelayConfig.MethodDelay = opts.DelayConfig.MethodDelay
model.DelayConfig.DelayJitter = opts.DelayConfig.DelayJitter model.DelayConfig.DelayJitter = opts.DelayConfig.DelayJitter
s, err := NewSimulatorServer(model) sim, err := NewCustomVCenterSimulator(model)
if err != nil { if err != nil {
t.Fatalf("should not fail: %s", err.Error()) t.Fatalf("should not fail: %s", err.Error())
} }
defer s.Close() defer sim.Close()
driverSim, err := NewSimulatorDriver(s) res, err := sim.driver.FindResourcePool("", "localhost.localdomain", "")
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
//
res, err := driverSim.FindResourcePool("", "localhost.localdomain", "")
if err != nil { if err != nil {
t.Fatalf("should not fail: %s", err.Error()) t.Fatalf("should not fail: %s", err.Error())
} }
@ -72,7 +58,7 @@ func TestVCenterDriver_FindResourcePoolStandaloneESX(t *testing.T) {
} }
// Invalid resource name should look for default resource pool // Invalid resource name should look for default resource pool
res, err = driverSim.FindResourcePool("", "localhost.localdomain", "invalid") res, err = sim.driver.FindResourcePool("", "localhost.localdomain", "invalid")
if err != nil { if err != nil {
t.Fatalf("should not fail: %s", err.Error()) t.Fatalf("should not fail: %s", err.Error())
} }

View File

@ -5,27 +5,17 @@ import (
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/vim25/types" "github.com/vmware/govmomi/vim25/types"
) )
func TestVirtualMachineDriver_FindAndAddSATAController(t *testing.T) { func TestVirtualMachineDriver_FindAndAddSATAController(t *testing.T) {
model := simulator.VPX() sim, err := NewVCenterSimulator()
model.Machine = 1
defer model.Remove()
s, err := NewSimulatorServer(model)
if err != nil { if err != nil {
t.Fatalf("should not fail: %s", err.Error()) t.Fatalf("should not fail: %s", err.Error())
} }
defer s.Close() defer sim.Close()
driverSim, err := NewSimulatorDriver(s) vm, _ := sim.ChooseSimulatorPreCreatedVM()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
vm := ChooseSimulatorPreCreatedVM(driverSim)
_, err = vm.FindSATAController() _, err = vm.FindSATAController()
if err != nil && !strings.Contains(err.Error(), "no available SATA controller") { if err != nil && !strings.Contains(err.Error(), "no available SATA controller") {
@ -49,23 +39,13 @@ func TestVirtualMachineDriver_FindAndAddSATAController(t *testing.T) {
} }
func TestVirtualMachineDriver_CreateAndRemoveCdrom(t *testing.T) { func TestVirtualMachineDriver_CreateAndRemoveCdrom(t *testing.T) {
model := simulator.VPX() sim, err := NewVCenterSimulator()
model.Machine = 1
defer model.Remove()
s, err := NewSimulatorServer(model)
if err != nil { if err != nil {
t.Fatalf("should not fail: %s", err.Error()) t.Fatalf("should not fail: %s", err.Error())
} }
defer s.Close() defer sim.Close()
driverSim, err := NewSimulatorDriver(s) vm, _ := sim.ChooseSimulatorPreCreatedVM()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
//Simulator shortcut to choose any pre created VM.
vm := ChooseSimulatorPreCreatedVM(driverSim)
// Add SATA Controller // Add SATA Controller
if err := vm.AddSATAController(); err != nil { if err := vm.AddSATAController(); err != nil {
@ -118,23 +98,13 @@ func TestVirtualMachineDriver_CreateAndRemoveCdrom(t *testing.T) {
} }
func TestVirtualMachineDriver_EjectCdrom(t *testing.T) { func TestVirtualMachineDriver_EjectCdrom(t *testing.T) {
model := simulator.VPX() sim, err := NewVCenterSimulator()
model.Machine = 1
defer model.Remove()
s, err := NewSimulatorServer(model)
if err != nil { if err != nil {
t.Fatalf("should not fail: %s", err.Error()) t.Fatalf("should not fail: %s", err.Error())
} }
defer s.Close() defer sim.Close()
driverSim, err := NewSimulatorDriver(s) vm, _ := sim.ChooseSimulatorPreCreatedVM()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
//Simulator shortcut to choose any pre created VM.
vm := ChooseSimulatorPreCreatedVM(driverSim)
// Add SATA Controller // Add SATA Controller
if err := vm.AddSATAController(); err != nil { if err := vm.AddSATAController(); err != nil {

View File

@ -28,25 +28,13 @@ func (vm *ReconfigureFail) ReconfigVMTask(req *types.ReconfigVM_Task) soap.HasFa
} }
func TestVirtualMachineDriver_Configure(t *testing.T) { func TestVirtualMachineDriver_Configure(t *testing.T) {
model := simulator.VPX() sim, err := NewVCenterSimulator()
model.Machine = 1
defer model.Remove()
s, err := NewSimulatorServer(model)
if err != nil { if err != nil {
t.Fatalf("should not fail: %s", err.Error()) t.Fatalf("should not fail: %s", err.Error())
} }
defer s.Close() defer sim.Close()
driverSim, err := NewSimulatorDriver(s) vm, machine := sim.ChooseSimulatorPreCreatedVM()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
//Simulator shortcut to choose any pre created VM.
machine := simulator.Map.Any("VirtualMachine").(*simulator.VirtualMachine)
ref := machine.Reference()
vm := driverSim.NewVM(&ref)
// Happy test // Happy test
hardwareConfig := &HardwareConfig{ hardwareConfig := &HardwareConfig{