refactor vcenter simulator setup
This commit is contained in:
parent
7b1af2c4e8
commit
b881a59bc8
|
@ -50,24 +50,73 @@ func newVMName() string {
|
|||
return fmt.Sprintf("test-%v", rand.Intn(1000))
|
||||
}
|
||||
|
||||
func NewSimulatorServer(model *simulator.Model) (*simulator.Server, error) {
|
||||
err := model.Create()
|
||||
type VCenterSimulator struct {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
model.Service.RegisterEndpoints = true
|
||||
model.Service.TLS = new(tls.Config)
|
||||
model.Service.ServeMux = http.NewServeMux()
|
||||
return model.Service.NewServer(), nil
|
||||
s.model.Service.RegisterEndpoints = true
|
||||
s.model.Service.TLS = new(tls.Config)
|
||||
s.model.Service.ServeMux = http.NewServeMux()
|
||||
return s.model.Service.NewServer(), nil
|
||||
}
|
||||
|
||||
func NewSimulatorDriver(s *simulator.Server) (*VCenterDriver, error) {
|
||||
func (s *VCenterSimulator) NewSimulatorDriver() (*VCenterDriver, error) {
|
||||
ctx := context.TODO()
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -104,11 +153,3 @@ func NewSimulatorDriver(s *simulator.Server) (*VCenterDriver, error) {
|
|||
}
|
||||
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
|
||||
}
|
||||
|
|
|
@ -7,21 +7,13 @@ import (
|
|||
)
|
||||
|
||||
func TestVCenterDriver_FindResourcePool(t *testing.T) {
|
||||
model := simulator.VPX()
|
||||
defer model.Remove()
|
||||
|
||||
s, err := NewSimulatorServer(model)
|
||||
sim, err := NewVCenterSimulator()
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
defer s.Close()
|
||||
defer sim.Close()
|
||||
|
||||
driverSim, err := NewSimulatorDriver(s)
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
|
||||
res, err := driverSim.FindResourcePool("", "DC0_H0", "")
|
||||
res, err := sim.driver.FindResourcePool("", "DC0_H0", "")
|
||||
if err != nil {
|
||||
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.DelayJitter = opts.DelayConfig.DelayJitter
|
||||
|
||||
s, err := NewSimulatorServer(model)
|
||||
sim, err := NewCustomVCenterSimulator(model)
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
defer s.Close()
|
||||
defer sim.Close()
|
||||
|
||||
driverSim, err := NewSimulatorDriver(s)
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
|
||||
//
|
||||
res, err := driverSim.FindResourcePool("", "localhost.localdomain", "")
|
||||
res, err := sim.driver.FindResourcePool("", "localhost.localdomain", "")
|
||||
if err != nil {
|
||||
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
|
||||
res, err = driverSim.FindResourcePool("", "localhost.localdomain", "invalid")
|
||||
res, err = sim.driver.FindResourcePool("", "localhost.localdomain", "invalid")
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
|
|
|
@ -5,27 +5,17 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/vmware/govmomi/simulator"
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
|
||||
func TestVirtualMachineDriver_FindAndAddSATAController(t *testing.T) {
|
||||
model := simulator.VPX()
|
||||
model.Machine = 1
|
||||
defer model.Remove()
|
||||
|
||||
s, err := NewSimulatorServer(model)
|
||||
sim, err := NewVCenterSimulator()
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
defer s.Close()
|
||||
defer sim.Close()
|
||||
|
||||
driverSim, err := NewSimulatorDriver(s)
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
|
||||
vm := ChooseSimulatorPreCreatedVM(driverSim)
|
||||
vm, _ := sim.ChooseSimulatorPreCreatedVM()
|
||||
|
||||
_, err = vm.FindSATAController()
|
||||
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) {
|
||||
model := simulator.VPX()
|
||||
model.Machine = 1
|
||||
defer model.Remove()
|
||||
|
||||
s, err := NewSimulatorServer(model)
|
||||
sim, err := NewVCenterSimulator()
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
defer s.Close()
|
||||
defer sim.Close()
|
||||
|
||||
driverSim, err := NewSimulatorDriver(s)
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
|
||||
//Simulator shortcut to choose any pre created VM.
|
||||
vm := ChooseSimulatorPreCreatedVM(driverSim)
|
||||
vm, _ := sim.ChooseSimulatorPreCreatedVM()
|
||||
|
||||
// Add SATA Controller
|
||||
if err := vm.AddSATAController(); err != nil {
|
||||
|
@ -118,23 +98,13 @@ func TestVirtualMachineDriver_CreateAndRemoveCdrom(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestVirtualMachineDriver_EjectCdrom(t *testing.T) {
|
||||
model := simulator.VPX()
|
||||
model.Machine = 1
|
||||
defer model.Remove()
|
||||
|
||||
s, err := NewSimulatorServer(model)
|
||||
sim, err := NewVCenterSimulator()
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
defer s.Close()
|
||||
defer sim.Close()
|
||||
|
||||
driverSim, err := NewSimulatorDriver(s)
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
|
||||
//Simulator shortcut to choose any pre created VM.
|
||||
vm := ChooseSimulatorPreCreatedVM(driverSim)
|
||||
vm, _ := sim.ChooseSimulatorPreCreatedVM()
|
||||
|
||||
// Add SATA Controller
|
||||
if err := vm.AddSATAController(); err != nil {
|
||||
|
|
|
@ -28,25 +28,13 @@ func (vm *ReconfigureFail) ReconfigVMTask(req *types.ReconfigVM_Task) soap.HasFa
|
|||
}
|
||||
|
||||
func TestVirtualMachineDriver_Configure(t *testing.T) {
|
||||
model := simulator.VPX()
|
||||
model.Machine = 1
|
||||
defer model.Remove()
|
||||
|
||||
s, err := NewSimulatorServer(model)
|
||||
sim, err := NewVCenterSimulator()
|
||||
if err != nil {
|
||||
t.Fatalf("should not fail: %s", err.Error())
|
||||
}
|
||||
defer s.Close()
|
||||
defer sim.Close()
|
||||
|
||||
driverSim, err := NewSimulatorDriver(s)
|
||||
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)
|
||||
vm, machine := sim.ChooseSimulatorPreCreatedVM()
|
||||
|
||||
// Happy test
|
||||
hardwareConfig := &HardwareConfig{
|
||||
|
|
Loading…
Reference in New Issue