github.com/vmware/govmomi@v0.51.0/vim25/types/deep_copy_test.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package types 6 7 import ( 8 "testing" 9 10 "github.com/google/go-cmp/cmp" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestMustDeepCopy(t *testing.T) { 15 newConfigSpec := func() VirtualMachineConfigSpec { 16 return VirtualMachineConfigSpec{ 17 Name: "vm-001", 18 GuestId: "otherGuest", 19 Files: &VirtualMachineFileInfo{VmPathName: "[datastore1]"}, 20 NumCPUs: 1, 21 MemoryMB: 128, 22 DeviceChange: []BaseVirtualDeviceConfigSpec{ 23 &VirtualDeviceConfigSpec{ 24 Operation: VirtualDeviceConfigSpecOperationAdd, 25 Device: &VirtualLsiLogicController{VirtualSCSIController{ 26 SharedBus: VirtualSCSISharingNoSharing, 27 VirtualController: VirtualController{ 28 BusNumber: 0, 29 VirtualDevice: VirtualDevice{ 30 Key: 1000, 31 }, 32 }, 33 }}, 34 }, 35 &VirtualDeviceConfigSpec{ 36 Operation: VirtualDeviceConfigSpecOperationAdd, 37 FileOperation: VirtualDeviceConfigSpecFileOperationCreate, 38 Device: &VirtualDisk{ 39 VirtualDevice: VirtualDevice{ 40 Key: 0, 41 ControllerKey: 1000, 42 UnitNumber: NewInt32(10), 43 Backing: &VirtualDiskFlatVer2BackingInfo{ 44 DiskMode: string(VirtualDiskModePersistent), 45 ThinProvisioned: NewBool(true), 46 VirtualDeviceFileBackingInfo: VirtualDeviceFileBackingInfo{ 47 FileName: "[datastore1]", 48 }, 49 }, 50 }, 51 CapacityInKB: 4000000, 52 }, 53 }, 54 &VirtualDeviceConfigSpec{ 55 Operation: VirtualDeviceConfigSpecOperationAdd, 56 Device: &VirtualE1000{VirtualEthernetCard{ 57 VirtualDevice: VirtualDevice{ 58 Key: 0, 59 DeviceInfo: &Description{ 60 Label: "Network Adapter 1", 61 Summary: "VM Network", 62 }, 63 Backing: &VirtualEthernetCardNetworkBackingInfo{ 64 VirtualDeviceDeviceBackingInfo: VirtualDeviceDeviceBackingInfo{ 65 DeviceName: "VM Network", 66 }, 67 }, 68 }, 69 AddressType: string(VirtualEthernetCardMacTypeGenerated), 70 }}, 71 }, 72 }, 73 ExtraConfig: []BaseOptionValue{ 74 &OptionValue{Key: "bios.bootOrder", Value: "ethernet0"}, 75 }, 76 } 77 } 78 79 t.Run("a string", func(t *testing.T) { 80 t.Parallel() 81 var dst AnyType 82 assert.NotPanics(t, func() { 83 dst = MustDeepCopy("hello") 84 }) 85 assert.Equal(t, "hello", dst) 86 }) 87 88 t.Run("a *uint8", func(t *testing.T) { 89 t.Parallel() 90 var dst AnyType 91 assert.NotPanics(t, func() { 92 dst = MustDeepCopy(New(uint8(42))) 93 }) 94 assert.Equal(t, &[]uint8{42}[0], dst) 95 }) 96 97 t.Run("a VirtualMachineConfigSpec", func(t *testing.T) { 98 t.Parallel() 99 var dst AnyType 100 assert.NotPanics(t, func() { 101 dst = MustDeepCopy(newConfigSpec()) 102 }) 103 assert.Equal(t, newConfigSpec(), dst) 104 }) 105 106 t.Run("a *VirtualMachineConfigSpec", func(t *testing.T) { 107 t.Parallel() 108 var dst AnyType 109 assert.NotPanics(t, func() { 110 dst = MustDeepCopy(New(newConfigSpec())) 111 }) 112 assert.Equal(t, New(newConfigSpec()), dst) 113 }) 114 115 t.Run("a **VirtualMachineConfigSpec", func(t *testing.T) { 116 t.Parallel() 117 118 var dst AnyType 119 exp := newConfigSpec() 120 ptrExp := &exp 121 ptrPtrExp := &ptrExp 122 123 assert.NotPanics(t, func() { 124 dst = MustDeepCopy(New(New(newConfigSpec()))) 125 }) 126 127 assert.Equal(t, ptrPtrExp, dst) 128 129 exp.Name += "-not-equal" 130 assert.NotEqual(t, ptrPtrExp, dst) 131 }) 132 133 t.Run("a VirtualMachineConfigSpec with nil DeviceChange vs empty", func(t *testing.T) { 134 t.Parallel() 135 136 t.Run("src is nil, exp is nil", func(t *testing.T) { 137 t.Parallel() 138 var dst AnyType 139 exp, src := newConfigSpec(), newConfigSpec() 140 exp.DeviceChange = nil 141 src.DeviceChange = nil 142 assert.NotPanics(t, func() { 143 dst = MustDeepCopy(src) 144 }) 145 assert.Equal(t, exp, dst, cmp.Diff(exp, dst)) 146 }) 147 148 t.Run("src is empty, exp is nil", func(t *testing.T) { 149 t.Parallel() 150 var dst AnyType 151 exp, src := newConfigSpec(), newConfigSpec() 152 exp.DeviceChange = nil 153 src.DeviceChange = []BaseVirtualDeviceConfigSpec{} 154 assert.NotPanics(t, func() { 155 dst = MustDeepCopy(src) 156 }) 157 assert.Equal(t, exp, dst, cmp.Diff(exp, dst)) 158 }) 159 160 }) 161 }