2017-11-13 16:02:46 -05:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"context"
|
2017-11-13 16:02:46 -05:00
|
|
|
"log"
|
|
|
|
"net"
|
2017-11-14 16:19:32 -05:00
|
|
|
"testing"
|
2017-11-13 16:02:46 -05:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-11-14 16:19:32 -05:00
|
|
|
func TestVMAcc_clone(t *testing.T) {
|
2020-01-08 13:22:55 -05:00
|
|
|
t.Skip("Acceptance tests not configured yet.")
|
2017-11-14 16:19:32 -05:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
config *CloneConfig
|
2020-08-31 04:34:41 -04:00
|
|
|
checkFunction func(*testing.T, VirtualMachine, *CloneConfig)
|
2017-11-14 16:19:32 -05:00
|
|
|
}{
|
|
|
|
{"Default", &CloneConfig{}, cloneDefaultCheck},
|
|
|
|
{"LinkedClone", &CloneConfig{LinkedClone: true}, cloneLinkedCloneCheck},
|
|
|
|
{"Folder", &CloneConfig{LinkedClone: true, Folder: "folder1/folder2"}, cloneFolderCheck},
|
|
|
|
{"ResourcePool", &CloneConfig{LinkedClone: true, ResourcePool: "pool1/pool2"}, cloneResourcePoolCheck},
|
|
|
|
{"Configure", &CloneConfig{LinkedClone: true}, configureCheck},
|
|
|
|
{"Configure_RAMReserveAll", &CloneConfig{LinkedClone: true}, configureRAMReserveAllCheck},
|
|
|
|
{"StartAndStop", &CloneConfig{LinkedClone: true}, startAndStopCheck},
|
|
|
|
{"Template", &CloneConfig{LinkedClone: true}, templateCheck},
|
|
|
|
{"Snapshot", &CloneConfig{}, snapshotCheck},
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
|
2017-11-14 16:19:32 -05:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2018-01-24 08:35:04 -05:00
|
|
|
tc.config.Host = TestHostName
|
2017-11-14 16:19:32 -05:00
|
|
|
tc.config.Name = newVMName()
|
|
|
|
|
|
|
|
templateName := "alpine"
|
|
|
|
d := newTestDriver(t)
|
|
|
|
|
|
|
|
template, err := d.FindVM(templateName) // Don't destroy this VM!
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot find template vm '%v': %v", templateName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Clonning VM")
|
2018-05-16 09:53:32 -04:00
|
|
|
vm, err := template.Clone(context.TODO(), tc.config)
|
2017-11-14 16:19:32 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot clone vm '%v': %v", templateName, err)
|
|
|
|
}
|
|
|
|
|
2018-01-24 08:35:04 -05:00
|
|
|
defer destroyVM(t, vm, tc.config.Name)
|
2017-11-14 16:19:32 -05:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Running check function")
|
|
|
|
tc.checkFunction(t, vm, tc.config)
|
|
|
|
})
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func cloneDefaultCheck(t *testing.T, vm VirtualMachine, config *CloneConfig) {
|
|
|
|
d := vm.(*VirtualMachineDriver).driver
|
2017-11-13 16:02:46 -05:00
|
|
|
|
|
|
|
// Check that the clone can be found by its name
|
2017-11-14 16:19:32 -05:00
|
|
|
if _, err := d.FindVM(config.Name); err != nil {
|
|
|
|
t.Errorf("Cannot find created vm '%v': %v", config.Name, err)
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
vmInfo, err := vm.Info("name", "parent", "runtime.host", "resourcePool", "datastore", "layoutEx.disk")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read VM properties: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-14 16:19:32 -05:00
|
|
|
if vmInfo.Name != config.Name {
|
|
|
|
t.Errorf("Invalid VM name: expected '%v', got '%v'", config.Name, vmInfo.Name)
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
f := d.NewFolder(vmInfo.Parent)
|
|
|
|
folderPath, err := f.Path()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read folder name: %v", err)
|
|
|
|
}
|
|
|
|
if folderPath != "" {
|
|
|
|
t.Errorf("Invalid folder: expected '/', got '%v'", folderPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
h := d.NewHost(vmInfo.Runtime.Host)
|
|
|
|
hostInfo, err := h.Info("name")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot read host properties: ", err)
|
|
|
|
}
|
2018-01-24 08:35:04 -05:00
|
|
|
if hostInfo.Name != TestHostName {
|
|
|
|
t.Errorf("Invalid host name: expected '%v', got '%v'", TestHostName, hostInfo.Name)
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
p := d.NewResourcePool(vmInfo.ResourcePool)
|
|
|
|
poolPath, err := p.Path()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read resource pool name: %v", err)
|
|
|
|
}
|
|
|
|
if poolPath != "" {
|
2017-11-14 16:19:32 -05:00
|
|
|
t.Errorf("Invalid resource pool: expected '/', got '%v'", poolPath)
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
dsr := vmInfo.Datastore[0].Reference()
|
|
|
|
ds := d.NewDatastore(&dsr)
|
|
|
|
dsInfo, err := ds.Info("name")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Cannot read datastore properties: ", err)
|
|
|
|
}
|
|
|
|
if dsInfo.Name != "datastore1" {
|
|
|
|
t.Errorf("Invalid datastore name: expected '%v', got '%v'", "datastore1", dsInfo.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(vmInfo.LayoutEx.Disk[0].Chain) != 1 {
|
|
|
|
t.Error("Not a full clone")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func configureCheck(t *testing.T, vm VirtualMachine, _ *CloneConfig) {
|
2017-11-13 16:02:46 -05:00
|
|
|
log.Printf("[DEBUG] Configuring the vm")
|
2017-11-14 16:19:32 -05:00
|
|
|
hwConfig := &HardwareConfig{
|
2018-03-23 04:56:03 -04:00
|
|
|
CPUs: 2,
|
|
|
|
CPUReservation: 1000,
|
|
|
|
CPULimit: 1500,
|
|
|
|
RAM: 2048,
|
|
|
|
RAMReservation: 1024,
|
|
|
|
MemoryHotAddEnabled: true,
|
|
|
|
CpuHotAddEnabled: true,
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
2019-07-13 13:56:43 -04:00
|
|
|
err := vm.Configure(hwConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to configure VM: %v", err)
|
|
|
|
}
|
2017-11-13 16:02:46 -05:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Running checks")
|
|
|
|
vmInfo, err := vm.Info("config")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read VM properties: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cpuSockets := vmInfo.Config.Hardware.NumCPU
|
2017-11-14 16:19:32 -05:00
|
|
|
if cpuSockets != hwConfig.CPUs {
|
|
|
|
t.Errorf("VM should have %v CPU sockets, got %v", hwConfig.CPUs, cpuSockets)
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
|
2018-05-12 07:19:17 -04:00
|
|
|
cpuReservation := *vmInfo.Config.CpuAllocation.Reservation
|
2017-11-14 16:19:32 -05:00
|
|
|
if cpuReservation != hwConfig.CPUReservation {
|
|
|
|
t.Errorf("VM should have CPU reservation for %v Mhz, got %v", hwConfig.CPUReservation, cpuReservation)
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
|
2018-05-12 07:19:17 -04:00
|
|
|
cpuLimit := *vmInfo.Config.CpuAllocation.Limit
|
2017-11-14 16:19:32 -05:00
|
|
|
if cpuLimit != hwConfig.CPULimit {
|
|
|
|
t.Errorf("VM should have CPU reservation for %v Mhz, got %v", hwConfig.CPULimit, cpuLimit)
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ram := vmInfo.Config.Hardware.MemoryMB
|
2017-11-14 16:19:32 -05:00
|
|
|
if int64(ram) != hwConfig.RAM {
|
|
|
|
t.Errorf("VM should have %v MB of RAM, got %v", hwConfig.RAM, ram)
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
|
2018-05-12 07:19:17 -04:00
|
|
|
ramReservation := *vmInfo.Config.MemoryAllocation.Reservation
|
2017-11-14 16:19:32 -05:00
|
|
|
if ramReservation != hwConfig.RAMReservation {
|
|
|
|
t.Errorf("VM should have RAM reservation for %v MB, got %v", hwConfig.RAMReservation, ramReservation)
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
2018-03-23 04:56:03 -04:00
|
|
|
|
|
|
|
cpuHotAdd := vmInfo.Config.CpuHotAddEnabled
|
|
|
|
if *cpuHotAdd != hwConfig.CpuHotAddEnabled {
|
|
|
|
t.Errorf("VM should have CPU hot add set to %v, got %v", hwConfig.CpuHotAddEnabled, cpuHotAdd)
|
|
|
|
}
|
|
|
|
|
|
|
|
memoryHotAdd := vmInfo.Config.MemoryHotAddEnabled
|
|
|
|
if *memoryHotAdd != hwConfig.MemoryHotAddEnabled {
|
|
|
|
t.Errorf("VM should have Memroy hot add set to %v, got %v", hwConfig.MemoryHotAddEnabled, memoryHotAdd)
|
|
|
|
}
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func configureRAMReserveAllCheck(t *testing.T, vm VirtualMachine, _ *CloneConfig) {
|
2017-11-14 16:19:32 -05:00
|
|
|
log.Printf("[DEBUG] Configuring the vm")
|
2019-07-13 13:56:43 -04:00
|
|
|
err := vm.Configure(&HardwareConfig{RAMReserveAll: true})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to configure VM: %v", err)
|
|
|
|
}
|
2017-11-14 16:19:32 -05:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Running checks")
|
|
|
|
vmInfo, err := vm.Info("config")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read VM properties: %v", err)
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
2017-11-14 16:19:32 -05:00
|
|
|
|
|
|
|
if *vmInfo.Config.MemoryReservationLockedToMax != true {
|
|
|
|
t.Errorf("VM should have all RAM reserved")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func cloneLinkedCloneCheck(t *testing.T, vm VirtualMachine, _ *CloneConfig) {
|
2017-11-14 16:19:32 -05:00
|
|
|
vmInfo, err := vm.Info("layoutEx.disk")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read VM properties: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(vmInfo.LayoutEx.Disk[0].Chain) != 2 {
|
|
|
|
t.Error("Not a linked clone")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func cloneFolderCheck(t *testing.T, vm VirtualMachine, config *CloneConfig) {
|
2017-11-14 16:19:32 -05:00
|
|
|
vmInfo, err := vm.Info("parent")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read VM properties: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
f := vm.(*VirtualMachineDriver).driver.NewFolder(vmInfo.Parent)
|
2017-11-14 16:19:32 -05:00
|
|
|
path, err := f.Path()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read folder name: %v", err)
|
|
|
|
}
|
|
|
|
if path != config.Folder {
|
|
|
|
t.Errorf("Wrong folder. expected: %v, got: %v", config.Folder, path)
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func cloneResourcePoolCheck(t *testing.T, vm VirtualMachine, config *CloneConfig) {
|
2017-11-14 16:19:32 -05:00
|
|
|
vmInfo, err := vm.Info("resourcePool")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read VM properties: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
p := vm.(*VirtualMachineDriver).driver.NewResourcePool(vmInfo.ResourcePool)
|
2017-11-14 16:19:32 -05:00
|
|
|
path, err := p.Path()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read resource pool name: %v", err)
|
|
|
|
}
|
|
|
|
if path != config.ResourcePool {
|
|
|
|
t.Errorf("Wrong folder. expected: %v, got: %v", config.ResourcePool, path)
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 16:02:46 -05:00
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func startAndStopCheck(t *testing.T, vm VirtualMachine, config *CloneConfig) {
|
2017-11-14 16:19:32 -05:00
|
|
|
stopper := startVM(t, vm, config.Name)
|
2017-11-13 16:02:46 -05:00
|
|
|
defer stopper()
|
|
|
|
|
2020-06-04 01:35:29 -04:00
|
|
|
switch ip, err := vm.WaitForIP(context.TODO(), nil); {
|
2017-11-13 16:02:46 -05:00
|
|
|
case err != nil:
|
2017-11-14 16:19:32 -05:00
|
|
|
t.Errorf("Cannot obtain IP address from created vm '%v': %v", config.Name, err)
|
2017-11-13 16:02:46 -05:00
|
|
|
case net.ParseIP(ip) == nil:
|
|
|
|
t.Errorf("'%v' is not a valid ip address", ip)
|
|
|
|
}
|
|
|
|
|
2019-07-13 13:56:43 -04:00
|
|
|
err := vm.StartShutdown()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to initiate guest shutdown: %v", err)
|
|
|
|
}
|
2017-11-13 16:02:46 -05:00
|
|
|
log.Printf("[DEBUG] Waiting max 1m0s for shutdown to complete")
|
2019-07-13 13:56:43 -04:00
|
|
|
err = vm.WaitForShutdown(context.TODO(), 1*time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to wait for giest shutdown: %v", err)
|
|
|
|
}
|
2017-11-13 16:02:46 -05:00
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func snapshotCheck(t *testing.T, vm VirtualMachine, config *CloneConfig) {
|
2017-11-14 16:19:32 -05:00
|
|
|
stopper := startVM(t, vm, config.Name)
|
2017-11-13 16:02:46 -05:00
|
|
|
defer stopper()
|
|
|
|
|
2019-07-13 13:56:43 -04:00
|
|
|
err := vm.CreateSnapshot("test-snapshot")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create snapshot: %v", err)
|
|
|
|
}
|
2017-11-13 16:02:46 -05:00
|
|
|
|
|
|
|
vmInfo, err := vm.Info("layoutEx.disk")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot read VM properties: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
layers := len(vmInfo.LayoutEx.Disk[0].Chain)
|
|
|
|
if layers != 2 {
|
|
|
|
t.Errorf("VM should have a single snapshot. expected 2 disk layers, got %v", layers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func templateCheck(t *testing.T, vm VirtualMachine, _ *CloneConfig) {
|
2019-07-13 13:56:43 -04:00
|
|
|
err := vm.ConvertToTemplate()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to convert to template: %v", err)
|
|
|
|
}
|
2017-11-13 16:02:46 -05:00
|
|
|
vmInfo, err := vm.Info("config.template")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot read VM properties: %v", err)
|
|
|
|
} else if !vmInfo.Config.Template {
|
|
|
|
t.Error("Not a template")
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 16:19:32 -05:00
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func startVM(t *testing.T, vm VirtualMachine, vmName string) (stopper func()) {
|
2017-11-14 16:19:32 -05:00
|
|
|
log.Printf("[DEBUG] Starting the vm")
|
|
|
|
if err := vm.PowerOn(); err != nil {
|
|
|
|
t.Fatalf("Cannot start vm '%v': %v", vmName, err)
|
|
|
|
}
|
|
|
|
return func() {
|
|
|
|
log.Printf("[DEBUG] Powering off the vm")
|
|
|
|
if err := vm.PowerOff(); err != nil {
|
|
|
|
t.Errorf("Cannot power off started vm '%v': %v", vmName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 08:35:04 -05:00
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func destroyVM(t *testing.T, vm VirtualMachine, vmName string) {
|
2018-01-24 08:35:04 -05:00
|
|
|
log.Printf("[DEBUG] Deleting the VM")
|
|
|
|
if err := vm.Destroy(); err != nil {
|
|
|
|
t.Errorf("!!! ERROR DELETING VM '%v': %v!!!", vmName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the clone is no longer exists
|
2020-08-31 04:34:41 -04:00
|
|
|
if _, err := vm.(*VirtualMachineDriver).driver.FindVM(vmName); err == nil {
|
2018-01-24 08:35:04 -05:00
|
|
|
t.Errorf("!!! STILL CAN FIND VM '%v'. IT MIGHT NOT HAVE BEEN DELETED !!!", vmName)
|
|
|
|
}
|
|
|
|
}
|