@@ -12,6 +12,7 @@ import (
1212 . "github.com/onsi/gomega"
1313
1414 "github.com/google/go-cmp/cmp"
15+ "github.com/vmware/govmomi/object"
1516 vimtypes "github.com/vmware/govmomi/vim25/types"
1617
1718 "github.com/vmware-tanzu/vm-operator/pkg/util/ptr"
@@ -250,6 +251,87 @@ var _ = Describe("CreateResizeConfigSpec Devices", func() {
250251 nil ),
251252 )
252253 })
254+
255+ Context ("Adding new devices when existing devices exist" , func () {
256+ It ("Add the correct new devices" , func () {
257+ expectedDev1 := & vimtypes.VirtualSerialPort {}
258+ expectedDev1 .Key = - 101
259+ expectedDev1 .Backing = & vimtypes.VirtualSerialPortDeviceBackingInfo {
260+ VirtualDeviceDeviceBackingInfo : vimtypes.VirtualDeviceDeviceBackingInfo {
261+ DeviceName : "one" ,
262+ },
263+ }
264+ expectedDev2 := & vimtypes.VirtualSerialPort {}
265+ expectedDev2 .Key = - 102
266+ expectedDev2 .Backing = & vimtypes.VirtualSerialPortDeviceBackingInfo {
267+ VirtualDeviceDeviceBackingInfo : vimtypes.VirtualDeviceDeviceBackingInfo {
268+ DeviceName : "two" ,
269+ },
270+ }
271+ expectedDev3 := & vimtypes.VirtualSerialPort {}
272+ expectedDev3 .Key = - 103
273+ expectedDev3 .Backing = & vimtypes.VirtualSerialPortDeviceBackingInfo {
274+ VirtualDeviceDeviceBackingInfo : vimtypes.VirtualDeviceDeviceBackingInfo {
275+ DeviceName : "three" ,
276+ },
277+ }
278+
279+ currentDev := & vimtypes.VirtualSerialPort {}
280+ currentDev .Key = 9000
281+ currentDev .Backing = & vimtypes.VirtualSerialPortDeviceBackingInfo {
282+ VirtualDeviceDeviceBackingInfo : vimtypes.VirtualDeviceDeviceBackingInfo {
283+ DeviceName : "one" ,
284+ },
285+ }
286+
287+ ci := vimtypes.VirtualMachineConfigInfo {}
288+ ci .Hardware .Device = append (ci .Hardware .Device , currentDev )
289+
290+ devChanges , err := (object.VirtualDeviceList {expectedDev1 , expectedDev2 , expectedDev3 }).ConfigSpec (AddOp )
291+ Expect (err ).ToNot (HaveOccurred ())
292+ cs := vimtypes.VirtualMachineConfigSpec {}
293+ cs .DeviceChange = devChanges
294+
295+ actualCS , err := resize .CreateResizeConfigSpec (ctx , ci , cs )
296+ Expect (err ).ToNot (HaveOccurred ())
297+
298+ // The first dev matches so should add just the two new devices.
299+ Expect (actualCS .DeviceChange ).To (HaveLen (2 ))
300+ Expect (actualCS .DeviceChange ).To (ConsistOf (devChanges [1 :]))
301+ })
302+
303+ It ("should add correct devices when no current devices exist" , func () {
304+ expectedDev1 := & vimtypes.VirtualSerialPort {}
305+ expectedDev1 .Key = - 201
306+ expectedDev1 .Backing = & vimtypes.VirtualSerialPortDeviceBackingInfo {
307+ VirtualDeviceDeviceBackingInfo : vimtypes.VirtualDeviceDeviceBackingInfo {
308+ DeviceName : "one" ,
309+ },
310+ }
311+ expectedDev2 := & vimtypes.VirtualSerialPort {}
312+ expectedDev2 .Key = - 202
313+ expectedDev2 .Backing = & vimtypes.VirtualSerialPortDeviceBackingInfo {
314+ VirtualDeviceDeviceBackingInfo : vimtypes.VirtualDeviceDeviceBackingInfo {
315+ DeviceName : "two" ,
316+ },
317+ }
318+
319+ // No current devices.
320+ ci := vimtypes.VirtualMachineConfigInfo {}
321+
322+ devChanges , err := (object.VirtualDeviceList {expectedDev1 , expectedDev2 }).ConfigSpec (AddOp )
323+ Expect (err ).ToNot (HaveOccurred ())
324+ cs := vimtypes.VirtualMachineConfigSpec {}
325+ cs .DeviceChange = devChanges
326+
327+ actualCS , err := resize .CreateResizeConfigSpec (ctx , ci , cs )
328+ Expect (err ).ToNot (HaveOccurred ())
329+
330+ // Should add the two new devices.
331+ Expect (actualCS .DeviceChange ).To (HaveLen (2 ))
332+ Expect (actualCS .DeviceChange ).To (ConsistOf (devChanges ))
333+ })
334+ })
253335})
254336
255337func devChangeEntry (
0 commit comments