From c01e5dce2ad7d1535eab640c6d1e1105faf86e4d Mon Sep 17 00:00:00 2001 From: Sylvia Moss Date: Fri, 4 Jun 2021 15:30:45 +0200 Subject: [PATCH] add address type when updating macaddres at clone (#73) --- builder/vsphere/driver/vm.go | 6 +++- builder/vsphere/driver/vm_test.go | 59 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/builder/vsphere/driver/vm.go b/builder/vsphere/driver/vm.go index 4fb9fa4f..659e0bd9 100644 --- a/builder/vsphere/driver/vm.go +++ b/builder/vsphere/driver/vm.go @@ -385,7 +385,11 @@ func (vm *VirtualMachineDriver) Clone(ctx context.Context, config *CloneConfig) current := adapter.GetVirtualEthernetCard() current.Backing = backing - current.MacAddress = config.MacAddress + + if config.MacAddress != "" { + current.AddressType = string(types.VirtualEthernetCardMacTypeManual) + current.MacAddress = config.MacAddress + } config := &types.VirtualDeviceConfigSpec{ Device: adapter.(types.BaseVirtualDevice), diff --git a/builder/vsphere/driver/vm_test.go b/builder/vsphere/driver/vm_test.go index 18653897..67572cae 100644 --- a/builder/vsphere/driver/vm_test.go +++ b/builder/vsphere/driver/vm_test.go @@ -185,3 +185,62 @@ func TestVirtualMachineDriver_CloneWithPrimaryDiskResize(t *testing.T) { t.Fatalf("unexpected disk size for primary disk: %d", disks[2].CapacityInKB) } } + +func TestVirtualMachineDriver_CloneWithMacAddress(t *testing.T) { + sim, err := NewVCenterSimulator() + if err != nil { + t.Fatalf("should not fail: %s", err.Error()) + } + defer sim.Close() + + _, datastore := sim.ChooseSimulatorPreCreatedDatastore() + vm, _ := sim.ChooseSimulatorPreCreatedVM() + + devices, err := vm.Devices() + if err != nil { + t.Fatalf("unexpected error %s", err.Error()) + } + + adapter, err := findNetworkAdapter(devices) + if err != nil { + t.Fatalf("unexpected error %s", err.Error()) + } + + network := adapter.GetVirtualEthernetCard() + oldMacAddress := network.MacAddress + + newMacAddress := "D4:B4:D4:96:70:26" + config := &CloneConfig{ + Name: "mock name", + Host: "DC0_H0", + Datastore: datastore.Name, + Network: "/DC0/network/VM Network", + MacAddress: newMacAddress, + } + + ctx := context.TODO() + clonedVM, err := vm.Clone(ctx, config) + if err != nil { + t.Fatalf("unexpected error %s", err.Error()) + } + + devices, err = clonedVM.Devices() + if err != nil { + t.Fatalf("unexpected error %s", err.Error()) + } + adapter, err = findNetworkAdapter(devices) + if err != nil { + t.Fatalf("unexpected error %s", err.Error()) + } + + network = adapter.GetVirtualEthernetCard() + if network.AddressType != string(types.VirtualEthernetCardMacTypeManual) { + t.Fatalf("unexpected address type") + } + if network.MacAddress == oldMacAddress { + t.Fatalf("expected mac address to be different from old one") + } + if network.MacAddress != newMacAddress { + t.Fatalf("unexpected mac address") + } +}