Skip to content

Commit

Permalink
add address type when updating macaddres at clone (hashicorp#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylviamoss authored Jun 4, 2021
1 parent 16a2c1c commit 396dde4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
6 changes: 5 additions & 1 deletion builder/vsphere/driver/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
59 changes: 59 additions & 0 deletions builder/vsphere/driver/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 396dde4

Please sign in to comment.