github.com/vmware/govmomi@v0.43.0/govc/test/network.bats (about) 1 #!/usr/bin/env bats 2 3 load test_helper 4 5 @test "network dvs" { 6 vcsim_env 7 8 run govc dvs.create -discovery-protocol cdp -product-version 6.6.0 -mtu 1500 DVS1 9 assert_success 10 11 dvs=$(govc object.collect -o -json network/DVS1) 12 13 assert_equal cdp "$(jq -r .config.linkDiscoveryProtocolConfig.protocol <<<"$dvs")" 14 assert_equal 1500 "$(jq -r .config.maxMtu <<<"$dvs")" 15 assert_equal 6.6.0 "$(jq -r .summary.productInfo.version <<<"$dvs")" 16 17 run govc dvs.add -dvs DVS1 DC0_H0 18 assert_success 19 20 run govc events -type DvsHostJoinedEvent 21 assert_success 22 assert_matches "DC0_H0 joined the vSphere Distributed Switch DVS1" 23 } 24 25 @test "network dvs backing" { 26 vcsim_env 27 28 # DVS backed network by default (from vcsim_env) 29 vm=$(new_empty_vm) 30 31 eth0=$(govc device.ls -vm $vm | grep ethernet- | awk '{print $1}') 32 run govc device.info -vm $vm $eth0 33 assert_success 34 35 summary=$(govc device.info -vm $vm $eth0 | grep Summary: | awk '{print $2}') 36 assert_equal "DVSwitch:" $summary 37 38 run govc device.remove -vm $vm $eth0 39 assert_success 40 41 eth0=$(govc device.ls -vm $vm | grep ethernet- | awk '{print $1}') 42 [ -z "$eth0" ] 43 44 # Standard network backing 45 run govc vm.network.add -vm $vm -net "VM Network" 46 assert_success 47 48 eth0=$(govc device.ls -vm $vm | grep ethernet- | awk '{print $1}') 49 50 run govc device.info -vm $vm $eth0 51 assert_success 52 53 summary=$(govc device.info -vm $vm $eth0 | grep Summary: | awk -F: '{print $2}') 54 assert_equal "VM Network" "$(collapse_ws $summary)" 55 56 run govc device.remove -vm $vm $eth0 57 assert_success 58 59 run govc device.remove -vm $vm $eth0 60 assert_failure "govc: device '$eth0' not found" 61 62 # Test PG's with the same name 63 run govc dvs.create DVS1 # DVS0 already exists 64 assert_success 65 66 run govc dvs.portgroup.add -dvs DVS0 -type ephemeral NSX-dvpg 67 assert_success 68 69 uuid=$(govc object.collect -s network/NSX-dvpg config.logicalSwitchUuid) 70 sid=$(govc object.collect -s network/NSX-dvpg config.segmentId) 71 moid=$(govc ls -i network/NSX-dvpg) 72 73 run govc dvs.portgroup.add -dvs DVS1 -type ephemeral NSX-dvpg 74 assert_success 75 76 run govc vm.network.add -vm $vm -net NSX-dvpg 77 assert_failure # resolves to multiple networks 78 79 run govc vm.network.add -vm $vm -net DVS0/NSX-dvpg 80 assert_success # switch_name/portgroup_name is unique 81 82 # Add a 2nd PG to the same switch, with the same name 83 run govc dvs.portgroup.add -dvs DVS0 -type ephemeral NSX-dvpg 84 assert_success 85 86 run govc vm.network.add -vm $vm -net NSX-dvpg 87 assert_failure # resolves to multiple networks 88 89 run govc vm.network.add -vm $vm -net DVS0/NSX-dvpg 90 assert_failure # switch_name/portgroup_name not is unique 91 92 run govc vm.network.add -vm $vm -net "$uuid" 93 assert_success # switch uuid is unique 94 95 run govc vm.network.add -vm $vm -net "$sid" 96 assert_success # segment id is unique 97 98 run govc vm.network.add -vm $vm -net "$moid" 99 assert_success # moid is unique 100 } 101 102 @test "network change backing" { 103 vcsim_env 104 105 vm=$(new_empty_vm) 106 107 eth0=$(govc device.ls -vm $vm | grep ethernet- | awk '{print $1}') 108 run govc vm.network.change -vm $vm $eth0 enoent 109 assert_failure "govc: network 'enoent' not found" 110 111 run govc vm.network.change -vm $vm enoent "VM Network" 112 assert_failure "govc: device 'enoent' not found" 113 114 run govc vm.network.change -vm $vm $eth0 "VM Network" 115 assert_success 116 117 run govc vm.network.change -vm $vm $eth0 118 assert_success 119 120 unset GOVC_NETWORK 121 run govc vm.network.change -vm $vm $eth0 122 assert_failure "govc: default network resolves to multiple instances, please specify" 123 124 run govc vm.power -on $vm 125 assert_success 126 run govc vm.power -off $vm 127 128 mac=$(vm_mac $vm) 129 run govc vm.network.change -vm $vm -net "VM Network" $eth0 130 assert_success 131 132 # verify we didn't change the mac address 133 run govc vm.power -on $vm 134 assert_success 135 assert_equal $mac $(vm_mac $vm) 136 } 137 138 @test "network standard backing" { 139 vcsim_env 140 141 vm=$(new_empty_vm) 142 143 run govc device.info -vm $vm ethernet-* 144 assert_success 145 146 run govc device.remove -vm $vm ethernet-* 147 assert_success 148 149 run govc device.info -vm $vm ethernet-* 150 assert_failure 151 152 run govc vm.network.add -vm $vm enoent 153 assert_failure "govc: network 'enoent' not found" 154 155 run govc vm.network.add -vm $vm "VM Network" 156 assert_success 157 158 run govc device.info -vm $vm ethernet-* 159 assert_success 160 161 dups=$(govc vm.info -json '*' | jq -r '.virtualMachines[].config.hardware.device[].macAddress | select(. != null)' | uniq -d) 162 if [ -n "$dups" ] ; then 163 flunk "duplicate MACs: $dups" 164 fi 165 } 166 167 @test "network adapter" { 168 vcsim_env 169 170 vm=$(new_id) 171 run govc vm.create -on=false -net.adapter=enoent $vm 172 assert_failure "govc: unknown ethernet card type 'enoent'" 173 174 vm=$(new_id) 175 run govc vm.create -on=false -net.adapter=vmxnet3 $vm 176 assert_success 177 178 eth0=$(govc device.ls -vm $vm | grep ethernet- | awk '{print $1}') 179 type=$(govc device.info -vm $vm $eth0 | grep Type: | awk -F: '{print $2}') 180 assert_equal "VirtualVmxnet3" $(collapse_ws $type) 181 182 run govc vm.network.add -vm $vm -net.adapter e1000e "VM Network" 183 assert_success 184 185 eth1=$(govc device.ls -vm $vm | grep ethernet- | grep -v $eth0 | awk '{print $1}') 186 type=$(govc device.info -vm $vm $eth1 | grep Type: | awk -F: '{print $2}') 187 assert_equal "VirtualE1000e" $(collapse_ws $type) 188 189 # validate each NIC has a unique MAC 190 macs=$(govc device.info -vm "$vm" -json ethernet-* | jq -r .devices[].macAddress | uniq | wc -l) 191 assert_equal 2 "$macs" 192 193 # validate -net.protocol. VM Network not compatible with vmxnet3vrdma, so create on dvgp under existing DVS0 194 run govc dvs.portgroup.add -dvs DVS0 -type ephemeral NSX-dvpg 195 assert_success 196 197 # add a valid vmxnet3vrdma adapter with valid protocal 198 run govc vm.network.add -vm $vm -net.adapter vmxnet3vrdma -net "DVS0/NSX-dvpg" -net.protocol=rocev2 199 assert_success 200 201 # add a valid vmxnet3vrdma adapter with valid protocal 202 run govc vm.network.add -vm $vm -net.adapter vmxnet3vrdma -net "DVS0/NSX-dvpg" -net.protocol=rocev1 203 assert_success 204 205 # invalid value for -net.protocol 206 run govc vm.network.add -vm $vm -net.adapter vmxnet3vrdma -net "DVS0/NSX-dvpg" -net.protocol=what 207 assert_failure "govc: invalid device protocol 'what'" 208 209 # invalid combination for -net.adapter and -net.protocol 210 run govc vm.network.add -vm $vm -net.adapter e1000e -net "DVS0/NSX-dvpg" -net.protocol=rocev2 211 assert_failure "govc: device protocol is only supported for vmxnet3vrdma at the moment" 212 } 213 214 @test "network flag required" { 215 vcsim_env 216 217 # -net flag is required when there are multiple networks 218 unset GOVC_NETWORK 219 run govc vm.create -on=false $(new_id) 220 assert_failure "govc: default network resolves to multiple instances, please specify" 221 } 222 223 @test "network change hardware address" { 224 vcsim_env -esx 225 226 mac="00:00:0f$(dd bs=1 count=3 if=/dev/random 2>/dev/null | hexdump -v -e '/1 ":%02x"')" 227 vm=$(new_id) 228 run govc vm.create -on=false $vm 229 assert_success 230 231 run govc vm.network.change -vm $vm -net.address $mac ethernet-0 232 assert_success 233 234 run govc vm.power -on $vm 235 assert_success 236 237 assert_equal $mac $(vm_mac $vm) 238 } 239 240 @test "dvs.portgroup" { 241 vcsim_env 242 id=$(new_id) 243 244 run govc dvs.create "$id" 245 assert_success 246 247 run govc events -type DvsCreatedEvent 248 assert_success 249 assert_matches "vSphere Distributed Switch $id was created" 250 251 run govc events -type DVPortgroupCreatedEvent 252 assert_success 253 assert_matches "was added to switch" 254 255 local host=$GOVC_HOST 256 257 run govc dvs.add -dvs "$id" "$host" 258 assert_success 259 260 run govc dvs.portgroup.add -dvs "$id" -type earlyBinding -nports 16 "${id}-ExternalNetwork" 261 assert_success 262 263 nports=$(govc object.collect -s "network/${id}-ExternalNetwork" portKeys | awk -F, '{print NF}') 264 [ "$nports" = "16" ] 265 266 run govc dvs.portgroup.add -dvs "$id" -type ephemeral -vlan 3122 "${id}-InternalNetwork" 267 assert_success 268 269 info=$(govc dvs.portgroup.info "$id" | grep VlanId: | uniq | grep 3122) 270 [ -n "$info" ] 271 272 run govc dvs.portgroup.change -vlan 3123 "${id}-InternalNetwork" 273 assert_success 274 275 info=$(govc dvs.portgroup.info "$id" | grep VlanId: | uniq | grep 3123) 276 [ -n "$info" ] 277 278 info=$(govc dvs.portgroup.info -json "$id" | jq '.port[].config.setting.vlan | select(.vlanId == 3123)') 279 [ -n "$info" ] 280 281 info=$(govc dvs.portgroup.info -json "$id" | jq '.port[].config.setting.vlan | select(.vlanId == 7777)') 282 [ -z "$info" ] 283 284 run govc object.destroy "network/${id}-ExternalNetwork" "network/${id}-InternalNetwork" "network/${id}" 285 assert_success 286 287 run govc events -type DvsDestroyedEvent 288 assert_success 289 assert_matches "vSphere Distributed Switch $id in DC0 was deleted" 290 291 run govc events -type DVPortgroupDestroyedEvent 292 assert_success 293 [ ${#lines[@]} -eq 2 ] 294 }