2017-11-13 16:02:46 -05:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2020-08-31 10:26:48 -04:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2017-11-13 16:02:46 -05:00
|
|
|
"fmt"
|
2018-10-31 17:42:24 -04:00
|
|
|
"math/rand"
|
2020-08-31 10:26:48 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2018-10-30 18:10:57 -04:00
|
|
|
"os"
|
2017-11-13 16:02:46 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-08-31 10:26:48 -04:00
|
|
|
|
|
|
|
"github.com/vmware/govmomi"
|
|
|
|
"github.com/vmware/govmomi/find"
|
|
|
|
"github.com/vmware/govmomi/session"
|
|
|
|
"github.com/vmware/govmomi/simulator"
|
|
|
|
"github.com/vmware/govmomi/vapi/rest"
|
|
|
|
"github.com/vmware/govmomi/vim25"
|
|
|
|
"github.com/vmware/govmomi/vim25/soap"
|
2017-11-13 16:02:46 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Defines whether acceptance tests should be run
|
2018-01-24 08:35:04 -05:00
|
|
|
const TestHostName = "esxi-1.vsphere65.test"
|
2017-11-13 16:02:46 -05:00
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func newTestDriver(t *testing.T) Driver {
|
2018-10-30 18:10:57 -04:00
|
|
|
username := os.Getenv("VSPHERE_USERNAME")
|
|
|
|
if username == "" {
|
|
|
|
username = "root"
|
|
|
|
}
|
|
|
|
password := os.Getenv("VSPHERE_PASSWORD")
|
|
|
|
if password == "" {
|
|
|
|
password = "jetbrains"
|
|
|
|
}
|
|
|
|
|
2020-07-14 05:45:19 -04:00
|
|
|
d, err := NewDriver(&ConnectConfig{
|
2018-01-23 14:51:05 -05:00
|
|
|
VCenterServer: "vcenter.vsphere65.test",
|
2018-10-30 18:10:57 -04:00
|
|
|
Username: username,
|
|
|
|
Password: password,
|
2017-11-13 16:02:46 -05:00
|
|
|
InsecureConnection: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot connect: %v", err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func newVMName() string {
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
return fmt.Sprintf("test-%v", rand.Intn(1000))
|
|
|
|
}
|
2020-08-31 10:26:48 -04:00
|
|
|
|
2020-09-22 10:44:03 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-22 11:42:35 -04:00
|
|
|
//Simulator shortcut to choose any pre created Datastore.
|
|
|
|
func (s *VCenterSimulator) ChooseSimulatorPreCreatedDatastore() (Datastore, *simulator.Datastore) {
|
|
|
|
ds := simulator.Map.Any("Datastore").(*simulator.Datastore)
|
|
|
|
ref := ds.Reference()
|
|
|
|
datastore := s.driver.NewDatastore(&ref)
|
|
|
|
return datastore, ds
|
|
|
|
}
|
|
|
|
|
|
|
|
//Simulator shortcut to choose any pre created Host.
|
|
|
|
func (s *VCenterSimulator) ChooseSimulatorPreCreatedHost() (*Host, *simulator.HostSystem) {
|
|
|
|
h := simulator.Map.Any("HostSystem").(*simulator.HostSystem)
|
|
|
|
ref := h.Reference()
|
|
|
|
host := s.driver.NewHost(&ref)
|
|
|
|
return host, h
|
|
|
|
}
|
|
|
|
|
2020-09-22 10:44:03 -04:00
|
|
|
func (s *VCenterSimulator) NewSimulatorServer() (*simulator.Server, error) {
|
|
|
|
err := s.model.Create()
|
2020-08-31 10:26:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-22 10:44:03 -04:00
|
|
|
s.model.Service.RegisterEndpoints = true
|
|
|
|
s.model.Service.TLS = new(tls.Config)
|
|
|
|
s.model.Service.ServeMux = http.NewServeMux()
|
|
|
|
return s.model.Service.NewServer(), nil
|
2020-08-31 10:26:48 -04:00
|
|
|
}
|
|
|
|
|
2020-09-22 10:44:03 -04:00
|
|
|
func (s *VCenterSimulator) NewSimulatorDriver() (*VCenterDriver, error) {
|
2020-08-31 10:26:48 -04:00
|
|
|
ctx := context.TODO()
|
|
|
|
user := &url.Userinfo{}
|
2020-09-22 10:44:03 -04:00
|
|
|
s.server.URL.User = user
|
2020-08-31 10:26:48 -04:00
|
|
|
|
2020-09-22 10:44:03 -04:00
|
|
|
soapClient := soap.NewClient(s.server.URL, true)
|
2020-08-31 10:26:48 -04:00
|
|
|
vimClient, err := vim25.NewClient(ctx, soapClient)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vimClient.RoundTripper = session.KeepAlive(vimClient.RoundTripper, 10*time.Minute)
|
|
|
|
client := &govmomi.Client{
|
|
|
|
Client: vimClient,
|
|
|
|
SessionManager: session.NewManager(vimClient),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.SessionManager.Login(ctx, user)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
finder := find.NewFinder(client.Client, false)
|
|
|
|
datacenter, err := finder.DatacenterOrDefault(ctx, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
finder.SetDatacenter(datacenter)
|
|
|
|
|
|
|
|
d := &VCenterDriver{
|
|
|
|
ctx: ctx,
|
|
|
|
client: client,
|
|
|
|
vimClient: vimClient,
|
|
|
|
restClient: &RestClient{
|
|
|
|
client: rest.NewClient(vimClient),
|
|
|
|
credentials: user,
|
|
|
|
},
|
|
|
|
datacenter: datacenter,
|
|
|
|
finder: finder,
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|