Skip to content

Commit

Permalink
vcsim: propagate CustomizeVM MacAddress to Virtual NIC
Browse files Browse the repository at this point in the history
The object.VirtualMachine.WaitForNetIP method depends on both the Virtual NIC's MacAddress and
the MacAddress reported by vmware-tools (guest.net[].macaddress property).

With this we can enable WaitForNetIP testing (govc vm.ip) against vcsim.
  • Loading branch information
dougm authored and Yue Yin committed Jan 12, 2022
1 parent 1c559c0 commit 738dc41
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
20 changes: 12 additions & 8 deletions govc/test/vm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,36 @@
load test_helper

@test "vm.ip" {
esx_env
vcsim_env -autostart=false

id=$(new_ttylinux_vm)
id=/DC0/vm/DC0_H0_VM0

mac=00:50:56:83:3a:5d
run govc vm.customize -vm $id -mac $mac -ip 10.0.0.1 -netmask 255.255.0.0 -type Linux
assert_success

run govc vm.power -on $id
assert_success

run govc vm.ip $id
run govc vm.ip -wait 5s $id
assert_success

run govc vm.ip -a -v4 $id
run govc vm.ip -wait 5s -a -v4 $id
assert_success

run govc vm.ip -n $(vm_mac $id) $id
run govc vm.ip -wait 5s -n $mac $id
assert_success

run govc vm.ip -n ethernet-0 $id
run govc vm.ip -wait 5s -n ethernet-0 $id
assert_success

ip=$(govc vm.ip $id)
ip=$(govc vm.ip -wait 5s $id)

# add a second nic
run govc vm.network.add -vm $id "VM Network"
assert_success

res=$(govc vm.ip -n ethernet-0 $id)
res=$(govc vm.ip -wait 5s -n ethernet-0 $id)
assert_equal $ip $res
}

Expand Down
7 changes: 6 additions & 1 deletion simulator/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1888,10 +1888,15 @@ func (vm *VirtualMachine) customize(ctx *Context) {
hostname = customizeName(vm, c.UserData.ComputerName)
}

cards := object.VirtualDeviceList(vm.Config.Hardware.Device).SelectByType((*types.VirtualEthernetCard)(nil))

for i, s := range vm.imc.NicSettingMap {
nic := &vm.Guest.Net[i]
if s.MacAddress != "" {
nic.MacAddress = s.MacAddress
nic.MacAddress = strings.ToLower(s.MacAddress) // MacAddress in guest will always be lowercase
card := cards[i].(types.BaseVirtualEthernetCard).GetVirtualEthernetCard()
card.MacAddress = s.MacAddress // MacAddress in Virtual NIC can be any case
card.AddressType = string(types.VirtualEthernetCardMacTypeManual)
}
if nic.DnsConfig == nil {
nic.DnsConfig = new(types.NetDnsConfigInfo)
Expand Down

0 comments on commit 738dc41

Please sign in to comment.