packer-cn/builder/vsphere/driver/vm_cdrom_test.go

168 lines
4.1 KiB
Go
Raw Normal View History

2020-09-18 03:57:01 -04:00
package driver
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/vmware/govmomi/vim25/types"
)
func TestVirtualMachineDriver_FindAndAddSATAController(t *testing.T) {
2020-09-22 10:44:03 -04:00
sim, err := NewVCenterSimulator()
2020-09-18 03:57:01 -04:00
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
2020-09-22 10:44:03 -04:00
defer sim.Close()
2020-09-18 03:57:01 -04:00
2020-09-22 10:44:03 -04:00
vm, _ := sim.ChooseSimulatorPreCreatedVM()
2020-09-18 03:57:01 -04:00
_, err = vm.FindSATAController()
if err != nil && !strings.Contains(err.Error(), "no available SATA controller") {
t.Fatalf("unexpected error: %s", err.Error())
}
if err == nil {
t.Fatalf("vm should not have sata controller")
}
if err := vm.AddSATAController(); err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
sc, err := vm.FindSATAController()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
if sc == nil {
t.Fatalf("SATA controller wasn't added properly")
}
}
func TestVirtualMachineDriver_CreateAndRemoveCdrom(t *testing.T) {
2020-09-22 10:44:03 -04:00
sim, err := NewVCenterSimulator()
2020-09-18 03:57:01 -04:00
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
2020-09-22 10:44:03 -04:00
defer sim.Close()
2020-09-18 03:57:01 -04:00
2020-09-22 10:44:03 -04:00
vm, _ := sim.ChooseSimulatorPreCreatedVM()
2020-09-18 03:57:01 -04:00
// Add SATA Controller
if err := vm.AddSATAController(); err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
// Verify if controller was created
sc, err := vm.FindSATAController()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
if sc == nil {
t.Fatalf("SATA controller wasn't added properly")
}
// Create CDROM
controller := sc.GetVirtualController()
cdrom, err := vm.CreateCdrom(controller)
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
if cdrom == nil {
t.Fatalf("CDrom wasn't created properly")
}
// Verify if CDROM was created
devices, err := vm.Devices()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
cdroms := devices.SelectByType((*types.VirtualCdrom)(nil))
if len(cdroms) != 1 {
t.Fatalf("unexpected numbers of cdrom: %d", len(cdroms))
}
// Remove CDROM
err = vm.RemoveCdroms()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
// Verify if CDROM was removed
devices, err = vm.Devices()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
cdroms = devices.SelectByType((*types.VirtualCdrom)(nil))
if len(cdroms) != 0 {
t.Fatalf("unexpected numbers of cdrom: %d", len(cdroms))
}
}
func TestVirtualMachineDriver_EjectCdrom(t *testing.T) {
2020-09-22 10:44:03 -04:00
sim, err := NewVCenterSimulator()
2020-09-18 03:57:01 -04:00
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
2020-09-22 10:44:03 -04:00
defer sim.Close()
2020-09-18 03:57:01 -04:00
2020-09-22 10:44:03 -04:00
vm, _ := sim.ChooseSimulatorPreCreatedVM()
2020-09-18 03:57:01 -04:00
// Add SATA Controller
if err := vm.AddSATAController(); err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
// Verify if controller was created
sc, err := vm.FindSATAController()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
if sc == nil {
t.Fatalf("SATA controller wasn't added properly")
}
// Create CDROM
controller := sc.GetVirtualController()
cdrom, err := vm.CreateCdrom(controller)
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
if cdrom == nil {
t.Fatalf("CDrom wasn't created properly")
}
// Verify if CDROM was created
devices, err := vm.Devices()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
cdroms := devices.SelectByType((*types.VirtualCdrom)(nil))
if len(cdroms) != 1 {
t.Fatalf("unexpected numbers of cdrom: %d", len(cdroms))
}
// Remove CDROM
err = vm.EjectCdroms()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
// Verify if CDROM was removed
devices, err = vm.Devices()
if err != nil {
t.Fatalf("should not fail: %s", err.Error())
}
cdroms = devices.SelectByType((*types.VirtualCdrom)(nil))
if len(cdroms) != 1 {
t.Fatalf("unexpected numbers of cdrom: %d", len(cdroms))
}
cd, ok := cdroms[0].(*types.VirtualCdrom)
if !ok {
t.Fatalf("Wrong cdrom type")
}
if diff := cmp.Diff(cd.Backing, &types.VirtualCdromRemotePassthroughBackingInfo{}); diff != "" {
t.Fatalf("Wrong cdrom backing info: %s", diff)
}
if diff := cmp.Diff(cd.Connectable, &types.VirtualDeviceConnectInfo{}); diff != "" {
t.Fatalf("Wrong cdrom connect info: %s", diff)
}
}