github.com/vmware/govmomi@v0.37.1/govc/test/object.bats (about) 1 #!/usr/bin/env bats 2 3 load test_helper 4 5 @test "object.destroy" { 6 vcsim_env 7 8 run govc object.destroy "/enoent" 9 assert_failure 10 11 run govc object.destroy 12 assert_failure 13 14 vm=$(new_id) 15 run govc vm.create "$vm" 16 assert_success 17 18 # fails when powered on 19 run govc object.destroy "vm/$vm" 20 assert_failure 21 22 run govc vm.power -off "$vm" 23 assert_success 24 25 run govc object.destroy "vm/$vm" 26 assert_success 27 } 28 29 @test "object.rename" { 30 vcsim_env 31 32 run govc object.rename "/enoent" "nope" 33 assert_failure 34 35 vm=$(new_id) 36 run govc vm.create -on=false "$vm" 37 assert_success 38 39 run govc object.rename "vm/$vm" "${vm}-renamed" 40 assert_success 41 42 run govc object.rename "vm/$vm" "${vm}-renamed" 43 assert_failure 44 45 run govc object.destroy "vm/${vm}-renamed" 46 assert_success 47 } 48 49 @test "object.mv" { 50 vcsim_env 51 52 folder=$(new_id) 53 54 run govc folder.create "vm/$folder" 55 assert_success 56 57 for _ in $(seq 1 3) ; do 58 vm=$(new_id) 59 run govc vm.create -folder "$folder" "$vm" 60 assert_success 61 done 62 63 result=$(govc ls "vm/$folder" | wc -l) 64 [ "$result" -eq "3" ] 65 66 run govc folder.create "vm/${folder}-2" 67 assert_success 68 69 run govc object.mv "vm/$folder/*" "vm/${folder}-2" 70 assert_success 71 72 result=$(govc ls "vm/${folder}-2" | wc -l) 73 [ "$result" -eq "3" ] 74 75 result=$(govc ls "vm/$folder" | wc -l) 76 [ "$result" -eq "0" ] 77 } 78 79 @test "object.collect" { 80 vcsim_env 81 82 run govc object.collect 83 assert_success 84 85 run govc object.collect -json 86 assert_success 87 88 run govc object.collect - 89 assert_success 90 91 run govc object.collect -json - 92 assert_success 93 94 run govc object.collect - content 95 assert_success 96 97 run govc object.collect -json - content 98 assert_success 99 100 root=$(govc object.collect - content | grep content.rootFolder | awk '{print $3}') 101 102 dc=$(govc object.collect "$root" childEntity | awk '{print $3}' | cut -d, -f1) 103 104 hostFolder=$(govc object.collect "$dc" hostFolder | awk '{print $3}') 105 106 cr=$(govc object.collect "$hostFolder" childEntity | awk '{print $3}' | cut -d, -f1) 107 108 host=$(govc object.collect "$cr" host | awk '{print $3}' | cut -d, -f1) 109 110 run govc object.collect "$host" 111 assert_success 112 113 run govc object.collect "$host" hardware 114 assert_success 115 116 run govc object.collect "$host" hardware.systemInfo 117 assert_success 118 119 uuid=$(govc object.collect "$host" hardware.systemInfo.uuid | awk '{print $3}') 120 uuid_s=$(govc object.collect -s "$host" hardware.systemInfo.uuid) 121 assert_equal "$uuid" "$uuid_s" 122 123 run govc object.collect "$(govc ls host | head -n1)" 124 assert_success 125 126 # test against slice of interface 127 setting=$(govc object.collect -s - content.setting) 128 result=$(govc object.collect -s "$setting" setting) 129 assert_equal "..." "$result" 130 131 # test against an interface field 132 run govc object.collect 'network/VM Network' summary 133 assert_success 134 135 run govc object.collect -dump -o 'network/VM Network' 136 assert_success 137 gofmt <<<"$output" 138 139 run govc object.collect -json -o 'network/VM Network' 140 assert_success 141 jq . <<<"$output" 142 } 143 144 @test "object.collect vcsim" { 145 vcsim_env -app 1 -pool 1 146 147 # test that {Cluster}ComputeResource and HostSystem network fields have the expected refs 148 for obj in DC0_C0 DC0_C0/DC0_C0_H0 DC0_H0 DC0_H0/DC0_H0; do 149 run govc object.collect /DC0/host/$obj network 150 assert_success 151 echo "obj=$obj" 152 assert_matches "DistributedVirtualPortgroup:" 153 assert_matches "Network:" 154 done 155 156 run govc object.collect -s -type ClusterComputeResource / configStatus 157 assert_success green 158 159 run govc object.collect -s -type ClusterComputeResource / effectiveRole # []int32 -> ArrayOfInt 160 assert_number 161 162 run govc object.collect -s -type ComputeResource / configStatus 163 assert_success "$(printf "green\ngreen")" 164 165 run govc object.collect -s -type ComputeResource / effectiveRole 166 assert_number 167 168 run govc object.collect -s -type Datacenter / effectiveRole 169 assert_number 170 171 run govc object.collect -s -type Datastore / effectiveRole 172 assert_number 173 174 run govc object.collect -s -type DistributedVirtualPortgroup / config.key 175 assert_matches dvportgroup- 176 177 run govc object.collect -s -type DistributedVirtualPortgroup / config.name 178 assert_matches DC0_DVPG0 179 assert_matches DVS0-DVUplinks- 180 181 run govc object.collect -s -type DistributedVirtualPortgroup / effectiveRole 182 assert_number 183 184 run govc object.collect -s -type DistributedVirtualSwitch / effectiveRole 185 assert_number 186 187 run govc object.collect -s -type DistributedVirtualSwitch / summary.name 188 assert_success DVS0 189 190 run govc object.collect -s -type DistributedVirtualSwitch / summary.productInfo.name 191 assert_success DVS 192 193 run govc object.collect -s -type DistributedVirtualSwitch / summary.productInfo.vendor 194 assert_success "VMware, Inc." 195 196 run govc object.collect -s -type DistributedVirtualSwitch / summary.productInfo.version 197 assert_success 6.5.0 198 199 run govc object.collect -s -type DistributedVirtualSwitch / summary.uuid 200 assert_matches "-" 201 202 run govc object.collect -s -type Folder / effectiveRole 203 assert_number 204 205 run govc object.collect -json -type HostSystem / config.storageDevice.scsiLun 206 assert_matches /vmfs/devices 207 208 run govc object.collect -json -type HostSystem / config.storageDevice.scsiTopology 209 assert_matches host.ScsiTopology 210 211 run govc object.collect -s -type HostSystem / effectiveRole 212 assert_number 213 214 run govc object.collect -s -type Network / effectiveRole 215 assert_number 216 217 run govc object.collect -s -type ResourcePool / resourcePool 218 # DC0_C0/Resources has 1 child ResourcePool and 1 child VirtualApp 219 assert_matches "ResourcePool:" 220 assert_matches "VirtualApp:" 221 222 run govc object.collect -s -type VirtualApp / effectiveRole 223 assert_number 224 225 run govc object.collect -s -type VirtualApp / name 226 assert_success DC0_C0_APP0 227 228 run govc object.collect -s -type VirtualApp / owner 229 assert_matches ":" 230 231 run govc object.collect -s -type VirtualApp / parent 232 assert_matches ":" 233 234 run govc object.collect -s -type VirtualApp / resourcePool 235 assert_success "" # no VirtualApp children 236 237 run govc object.collect -s -type VirtualApp / summary.config.cpuAllocation.limit 238 assert_number 239 240 run govc object.collect -s -type VirtualApp / summary.config.cpuAllocation.reservation 241 assert_number 242 243 run govc object.collect -s -type VirtualApp / summary.config.memoryAllocation.limit 244 assert_number 245 246 run govc object.collect -s -type VirtualApp / summary.config.memoryAllocation.reservation 247 assert_number 248 249 run govc object.collect -s -type VirtualApp / vm 250 assert_matches "VirtualMachine:" 251 252 run govc object.collect -s -type VirtualMachine / config.tools.toolsVersion 253 assert_number 254 255 run govc object.collect -s -type VirtualMachine / effectiveRole 256 assert_number 257 258 run govc object.collect -s -type VirtualMachine / summary.guest.toolsStatus 259 assert_matches toolsNotInstalled 260 261 run govc object.collect -s -type VirtualMachine / config.npivPortWorldWideName # []int64 -> ArrayOfLong 262 assert_success 263 264 run govc object.collect -s -type VirtualMachine / config.vmxConfigChecksum # []uint8 -> ArrayOfByte 265 assert_success 266 267 run govc object.collect -s /DC0/vm/DC0_H0_VM0 config.hardware.numCoresPerSocket 268 assert_success 1 269 270 run govc object.collect -s -type ClusterComputeResource / summary.effectiveCpu 271 assert_number 272 273 run govc object.collect -s -type ClusterComputeResource / summary.effectiveMemory 274 assert_number 275 276 run govc object.collect -s -type ClusterComputeResource / summary.numCpuCores 277 assert_number 278 279 run govc object.collect -s -type ClusterComputeResource / summary.numCpuThreads 280 assert_number 281 282 run govc object.collect -s -type ClusterComputeResource / summary.numEffectiveHosts 283 assert_number 284 285 run govc object.collect -s -type ClusterComputeResource / summary.numHosts 286 assert_number 287 288 run govc object.collect -s -type ClusterComputeResource / summary.totalCpu 289 assert_number 290 291 run govc object.collect -s -type ClusterComputeResource / summary.totalMemory 292 assert_number 293 294 run govc object.collect -s -type ComputeResource / summary.effectiveCpu 295 assert_number 296 297 run govc object.collect -s -type ComputeResource / summary.effectiveMemory 298 assert_number 299 300 run govc object.collect -s -type ComputeResource / summary.numCpuCores 301 assert_number 302 303 run govc object.collect -s -type ComputeResource / summary.numCpuThreads 304 assert_number 305 306 run govc object.collect -s -type ComputeResource / summary.numEffectiveHosts 307 assert_number 308 309 run govc object.collect -s -type ComputeResource / summary.numHosts 310 assert_number 311 312 run govc object.collect -s -type ComputeResource / summary.totalCpu 313 assert_number 314 315 run govc object.collect -s -type ComputeResource / summary.totalMemory 316 assert_number 317 318 run govc object.collect -s -type Network / summary.accessible 319 assert_success "$(printf "true\ntrue\ntrue")" 320 321 run govc object.collect -s -type Network / summary.ipPoolName 322 assert_success "" 323 324 # check that uuid and instanceUuid are set under both config and summary.config 325 for prop in config summary.config ; do 326 uuids=$(govc object.collect -s -type m / "$prop.uuid" | sort) 327 [ -n "$uuids" ] 328 iuuids=$(govc object.collect -s -type m / "$prop.instanceUuid" | sort) 329 [ -n "$iuuids" ] 330 331 [ "$uuids" != "$iuuids" ] 332 done 333 334 govc vm.create -g ubuntu64Guest my-ubuntu 335 assert_success 336 337 govc object.collect -type m / -guest.guestFamily linuxGuest 338 assert_success 339 } 340 341 @test "object.collect view" { 342 vcsim_env -dc 2 -folder 1 343 344 run govc object.collect -type m 345 assert_success 346 347 run govc object.collect -type m / -name '*C0*' 348 assert_success 349 350 run govc object.collect -type m / -name 351 assert_success 352 353 run govc object.collect -type m / name runtime.powerState 354 assert_success 355 356 run govc object.collect -type m -type h /F0 name 357 assert_success 358 359 run govc object.collect -type n / name 360 assert_success 361 362 run govc object.collect -type enoent / name 363 assert_failure 364 365 govc object.collect -wait 5m -s -type m ./vm -name foo & 366 pid=$! 367 run govc vm.create -on=false foo 368 assert_success 369 370 wait $pid # wait for object.collect to exit 371 372 run govc object.collect -s -type m ./vm -name foo 373 assert_success 374 375 govc object.collect -wait 5m -s -type d / -name dcx & 376 pid=$! 377 run govc datacenter.create dcx 378 assert_success 379 380 wait $pid # wait for object.collect to exit 381 382 run govc object.collect -s -type d / -name dcx 383 assert_success 384 } 385 386 @test "object.collect raw" { 387 vcsim_env 388 389 govc object.collect -R - <<EOF | grep serverClock 390 <?xml version="1.0" encoding="UTF-8"?> 391 <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> 392 <Body> 393 <CreateFilter xmlns="urn:vim25"> 394 <_this type="PropertyCollector">PropertyCollector</_this> 395 <spec> 396 <propSet> 397 <type>ServiceInstance</type> 398 <all>true</all> 399 </propSet> 400 <objectSet> 401 <obj type="ServiceInstance">ServiceInstance</obj> 402 </objectSet> 403 </spec> 404 <partialUpdates>false</partialUpdates> 405 </CreateFilter> 406 </Body> 407 </Envelope> 408 EOF 409 410 govc object.collect -O | grep types.CreateFilter 411 govc object.collect -O -json | jq . 412 } 413 414 @test "object.find" { 415 vcsim_env -ds 2 416 417 unset GOVC_DATACENTER 418 419 run govc find "/enoent" 420 assert_failure 421 422 run govc find 423 assert_success 424 425 run govc find . 426 assert_success 427 428 run govc find / 429 assert_success 430 431 govc find -json / | jq . 432 433 run govc find . -type HostSystem 434 assert_success 435 436 dc=$(govc find / -type Datacenter | head -1) 437 438 run govc find "$dc" -maxdepth 0 439 assert_output "$dc" 440 441 run govc find "$dc/vm" -maxdepth 0 442 assert_output "$dc/vm" 443 444 run govc find "$dc" -maxdepth 1 -type Folder 445 assert_success 446 # /<datacenter>/{vm,network,host,datastore} 447 [ ${#lines[@]} -eq 4 ] 448 449 folder=$(govc find -type Folder -name vm) 450 451 vm=$(new_empty_vm) 452 453 run govc find . -name "$vm" 454 assert_output "$folder/$vm" 455 456 run govc find -p "$folder/$vm" -type Datacenter 457 assert_output "$dc" 458 459 run govc find "$folder" -name "$vm" 460 assert_output "$folder/$vm" 461 462 # moref for VM Network 463 net=$(govc find -i network -name "$(basename "$GOVC_NETWORK")") 464 465 # $vm.network.contains($net) == true 466 run govc find . -type m -name "$vm" -network "$net" 467 assert_output "$folder/$vm" 468 469 # remove network reference 470 run govc device.remove -vm "$vm" ethernet-* 471 assert_success 472 473 # $vm.network.contains($net) == false 474 run govc find . -type VirtualMachine -name "$vm" -network "$net" 475 assert_output "" 476 477 run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOn 478 assert_output "" 479 480 run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOff 481 assert_output "$folder/$vm" 482 483 run govc vm.power -on "$vm" 484 assert_success 485 486 run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOff 487 assert_output "" 488 489 run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOn 490 assert_output "$folder/$vm" 491 492 # output paths should be relative to "." in these cases 493 export GOVC_DATACENTER=$dc 494 495 folder="./vm" 496 497 run govc find . -name "$vm" 498 assert_output "$folder/$vm" 499 500 run govc find "$folder" -name "$vm" 501 assert_output "$folder/$vm" 502 503 # Make sure property filter doesn't match when guest is unset for $vm (issue 1089) 504 run govc find "$folder" -type m -guest.ipAddress 0.0.0.0 505 assert_output "" 506 507 run govc fields.add -type Datastore ds-mode 508 assert_success 509 510 run govc fields.add -type Datastore ds-other 511 assert_success 512 513 run govc fields.set ds-mode prod datastore/LocalDS_0 514 assert_success 515 516 run govc fields.set ds-other prod datastore/LocalDS_1 517 assert_success 518 519 run govc fields.set ds-mode test datastore/LocalDS_1 520 assert_success 521 522 run govc fields.set ds-other foo datastore/LocalDS_1 523 assert_success 524 525 key=$(govc fields.ls | grep ds-mode | awk '{print $1}') 526 527 run govc find -type s / -customValue "$key:prod" # match specific key:val 528 assert_success /DC0/datastore/LocalDS_0 529 530 run govc find -type s / -customValue "*:test" # match any key:val 531 assert_success /DC0/datastore/LocalDS_1 532 533 run govc find -type s / -customValue "$key:*" # match specific key w/ any val 534 assert_matches /DC0/datastore/LocalDS_0 535 assert_matches /DC0/datastore/LocalDS_1 536 537 run govc find -type s / -customValue 0:dev # value doesn't match any entity 538 assert_success "" 539 } 540 541 @test "object.find multi root" { 542 vcsim_env -dc 2 543 544 run govc find vm 545 assert_success # ok as there is 1 "vm" folder relative to $GOVC_DATACENTER 546 547 run govc folder.create vm/{one,two} 548 assert_success 549 550 run govc find vm/one 551 assert_success 552 553 run govc find vm/on* 554 assert_success 555 556 run govc find vm/*o* 557 assert_failure # matches 2 objects 558 559 run govc folder.create vm/{one,two}/vm 560 assert_success 561 562 # prior to forcing Finder list mode, this would have failed since ManagedObjectList("vm") would have returned 563 # all 3 folders named "vm": "vm", "vm/one/vm", "vm/two/vm" 564 run govc find vm 565 assert_success 566 567 unset GOVC_DATACENTER 568 run govc find vm 569 assert_failure # without Datacenter specified, there are 0 "vm" folders relative to the root folder 570 571 run govc find -l / 572 assert_success 573 574 run govc find -l -i / 575 assert_success 576 assert_matches :domain-c # ClusterComputeResource moid value 577 assert_matches :group- # Folder moid value 578 assert_matches :resgroup- # ResourcePool moid value 579 assert_matches :dvs- # DistributedVirtualSwitch moid value 580 } 581 582 @test "object.method" { 583 vcsim_env_todo 584 585 vm=$(govc find vm -type m | head -1) 586 587 run govc object.method -enable=false -name NoSuchMethod "$vm" 588 assert_failure 589 590 run govc object.method -enable=false -name Destroy_Task enoent 591 assert_failure 592 593 run govc object.collect -s "$vm" disabledMethod 594 ! assert_matches "Destroy_Task" "$output" 595 596 run govc object.method -enable=false -name Destroy_Task "$vm" 597 assert_success 598 599 run govc object.collect -s "$vm" disabledMethod 600 assert_matches "Destroy_Task" "$output" 601 602 run govc object.method -enable -name Destroy_Task "$vm" 603 assert_success 604 605 run govc object.collect -s "$vm" disabledMethod 606 ! assert_matches "Destroy_Task" "$output" 607 } 608 609 @test "object.save" { 610 vcsim_env 611 612 dir="$BATS_TMPDIR/$(new_id)" 613 run govc object.save -v -d "$dir" /DC0/vm 614 assert_success 615 616 n=$(ls "$dir"/*.xml | wc -l) 617 rm -rf "$dir" 618 assert_equal 10 "$n" 619 } 620 621 @test "tree" { 622 vcsim_start -dc 2 -folder 1 -pod 1 -nsx 1 -pool 2 623 624 run govc tree 625 assert_success 626 627 run govc tree /DC0 628 assert_success 629 }