github.com/vmware/govmomi@v0.43.0/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 bytes" { 342 vcsim_env 343 344 host=$(govc find / -type h | head -1) 345 346 # ArrayOfByte with PEM encoded cert 347 govc object.collect -s "$host" config.certificate | \ 348 base64 -d | openssl x509 -text 349 350 # []byte field with PEM encoded cert 351 govc object.collect -s -json "$host" config | jq -r .certificate | \ 352 base64 -d | openssl x509 -text 353 354 # ArrayOfByte with DER encoded cert 355 govc object.collect -s CustomizationSpecManager:CustomizationSpecManager encryptionKey | \ 356 base64 -d | openssl x509 -inform DER -text 357 358 # []byte field with DER encoded cert 359 govc object.collect -o -json CustomizationSpecManager:CustomizationSpecManager | jq -r .encryptionKey | \ 360 base64 -d | openssl x509 -inform DER -text 361 } 362 363 @test "object.collect view" { 364 vcsim_env -dc 2 -folder 1 365 366 run govc object.collect -type m 367 assert_success 368 369 run govc object.collect -type m / -name '*C0*' 370 assert_success 371 372 run govc object.collect -type m / -name 373 assert_success 374 375 run govc object.collect -type m / name runtime.powerState 376 assert_success 377 378 run govc object.collect -type m -type h /F0 name 379 assert_success 380 381 run govc object.collect -type n / name 382 assert_success 383 384 run govc object.collect -type enoent / name 385 assert_failure 386 387 govc object.collect -wait 5m -s -type m ./vm -name foo & 388 pid=$! 389 run govc vm.create -on=false foo 390 assert_success 391 392 wait $pid # wait for object.collect to exit 393 394 run govc object.collect -s -type m ./vm -name foo 395 assert_success 396 397 govc object.collect -wait 5m -s -type d / -name dcx & 398 pid=$! 399 run govc datacenter.create dcx 400 assert_success 401 402 wait $pid # wait for object.collect to exit 403 404 run govc object.collect -s -type d / -name dcx 405 assert_success 406 } 407 408 @test "object.collect raw" { 409 vcsim_env 410 411 govc object.collect -R - <<EOF | grep serverClock 412 <?xml version="1.0" encoding="UTF-8"?> 413 <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> 414 <Body> 415 <CreateFilter xmlns="urn:vim25"> 416 <_this type="PropertyCollector">PropertyCollector</_this> 417 <spec> 418 <propSet> 419 <type>ServiceInstance</type> 420 <all>true</all> 421 </propSet> 422 <objectSet> 423 <obj type="ServiceInstance">ServiceInstance</obj> 424 </objectSet> 425 </spec> 426 <partialUpdates>false</partialUpdates> 427 </CreateFilter> 428 </Body> 429 </Envelope> 430 EOF 431 432 govc object.collect -O | grep types.CreateFilter 433 govc object.collect -O -json | jq . 434 } 435 436 @test "object.collect index" { 437 vcsim_env 438 439 export GOVC_VM=/DC0/vm/DC0_H0_VM0 440 441 # NOTE: '-o' flag uses RetrievePropertiesEx() and mo.ObjectContentToType() 442 # By default, WaitForUpdatesEx() is used with raw types.ObjectContent 443 444 run govc object.collect -o $GOVC_VM 'config.hardware[4000]' 445 assert_failure 446 447 run govc object.collect -o $GOVC_VM 'config.hardware.device[4000' 448 assert_failure 449 450 run govc object.collect -o $GOVC_VM 'config.hardware.device["4000"]' 451 assert_failure # Key is int, not string 452 453 run govc object.collect -o -json $GOVC_VM 'config.hardware.device[4000]' 454 assert_success 455 456 run jq -r .config.hardware.device[].deviceInfo.label <<<"$output" 457 assert_success ethernet-0 458 459 run govc object.collect -o $GOVC_VM 'config.hardware.device[4000].enoent' 460 assert_failure # InvalidProperty 461 462 run govc object.collect -o -json $GOVC_VM 'config.hardware.device[4000].deviceInfo.label' 463 assert_success 464 465 run govc object.collect -s $GOVC_VM 'config.hardware.device[4000].deviceInfo.label' 466 assert_success ethernet-0 467 468 run govc object.collect -o $GOVC_VM 'config.extraConfig[guestinfo.a]' 469 assert_failure # string Key requires quotes 470 471 run govc object.collect -o $GOVC_VM 'config["guestinfo.a"]' 472 assert_failure 473 474 run govc object.collect -o $GOVC_VM 'config.extraConfig["guestinfo.a"]' 475 assert_success # Key does not exist, not an error 476 477 run govc vm.change -e "guestinfo.a=1" -e "guestinfo.b=2" 478 assert_success 479 480 run govc object.collect -json $GOVC_VM 'config.extraConfig["guestinfo.b"]' 481 assert_success 482 483 run jq -r .[].val.value <<<"$output" 484 assert_success 2 485 486 run govc object.collect -o -json $GOVC_VM 'config.extraConfig["guestinfo.b"]' 487 assert_success 488 489 run jq -r .config.extraConfig[].value <<<"$output" 490 assert_success 2 491 492 run govc object.collect -s $GOVC_VM 'config.extraConfig["guestinfo.b"].value' 493 assert_success 2 494 } 495 496 @test "object.find" { 497 vcsim_env -ds 2 498 499 unset GOVC_DATACENTER 500 501 run govc find "/enoent" 502 assert_failure 503 504 run govc find 505 assert_success 506 507 run govc find . 508 assert_success 509 510 run govc find / 511 assert_success 512 513 govc find -json / | jq . 514 515 run govc find . -type HostSystem 516 assert_success 517 518 dc=$(govc find / -type Datacenter | head -1) 519 520 run govc find "$dc" -maxdepth 0 521 assert_output "$dc" 522 523 run govc find "$dc/vm" -maxdepth 0 524 assert_output "$dc/vm" 525 526 run govc find "$dc" -maxdepth 1 -type Folder 527 assert_success 528 # /<datacenter>/{vm,network,host,datastore} 529 [ ${#lines[@]} -eq 4 ] 530 531 folder=$(govc find -type Folder -name vm) 532 533 vm=$(new_empty_vm) 534 535 run govc find . -name "$vm" 536 assert_output "$folder/$vm" 537 538 run govc find -p "$folder/$vm" -type Datacenter 539 assert_output "$dc" 540 541 run govc find "$folder" -name "$vm" 542 assert_output "$folder/$vm" 543 544 # moref for VM Network 545 net=$(govc find -i network -name "$(basename "$GOVC_NETWORK")") 546 547 # $vm.network.contains($net) == true 548 run govc find . -type m -name "$vm" -network "$net" 549 assert_output "$folder/$vm" 550 551 # remove network reference 552 run govc device.remove -vm "$vm" ethernet-* 553 assert_success 554 555 # $vm.network.contains($net) == false 556 run govc find . -type VirtualMachine -name "$vm" -network "$net" 557 assert_output "" 558 559 run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOn 560 assert_output "" 561 562 run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOff 563 assert_output "$folder/$vm" 564 565 run govc vm.power -on "$vm" 566 assert_success 567 568 run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOff 569 assert_output "" 570 571 run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOn 572 assert_output "$folder/$vm" 573 574 # output paths should be relative to "." in these cases 575 export GOVC_DATACENTER=$dc 576 577 folder="./vm" 578 579 run govc find . -name "$vm" 580 assert_output "$folder/$vm" 581 582 run govc find "$folder" -name "$vm" 583 assert_output "$folder/$vm" 584 585 # Make sure property filter doesn't match when guest is unset for $vm (issue 1089) 586 run govc find "$folder" -type m -guest.ipAddress 0.0.0.0 587 assert_output "" 588 589 run govc fields.add -type Datastore ds-mode 590 assert_success 591 592 run govc fields.add -type Datastore ds-other 593 assert_success 594 595 run govc fields.set ds-mode prod datastore/LocalDS_0 596 assert_success 597 598 run govc fields.set ds-other prod datastore/LocalDS_1 599 assert_success 600 601 run govc fields.set ds-mode test datastore/LocalDS_1 602 assert_success 603 604 run govc fields.set ds-other foo datastore/LocalDS_1 605 assert_success 606 607 key=$(govc fields.ls | grep ds-mode | awk '{print $1}') 608 609 run govc find -type s / -customValue "$key:prod" # match specific key:val 610 assert_success /DC0/datastore/LocalDS_0 611 612 run govc find -type s / -customValue "*:test" # match any key:val 613 assert_success /DC0/datastore/LocalDS_1 614 615 run govc find -type s / -customValue "$key:*" # match specific key w/ any val 616 assert_matches /DC0/datastore/LocalDS_0 617 assert_matches /DC0/datastore/LocalDS_1 618 619 run govc find -type s / -customValue 0:dev # value doesn't match any entity 620 assert_success "" 621 } 622 623 @test "object.find multi root" { 624 vcsim_env -dc 2 625 626 run govc find vm 627 assert_success # ok as there is 1 "vm" folder relative to $GOVC_DATACENTER 628 629 run govc folder.create vm/{one,two} 630 assert_success 631 632 run govc find vm/one 633 assert_success 634 635 run govc find vm/on* 636 assert_success 637 638 run govc find vm/*o* 639 assert_failure # matches 2 objects 640 641 run govc folder.create vm/{one,two}/vm 642 assert_success 643 644 # prior to forcing Finder list mode, this would have failed since ManagedObjectList("vm") would have returned 645 # all 3 folders named "vm": "vm", "vm/one/vm", "vm/two/vm" 646 run govc find vm 647 assert_success 648 649 unset GOVC_DATACENTER 650 run govc find vm 651 assert_failure # without Datacenter specified, there are 0 "vm" folders relative to the root folder 652 653 run govc find -l / 654 assert_success 655 656 run govc find -l -i / 657 assert_success 658 assert_matches :domain-c # ClusterComputeResource moid value 659 assert_matches :group- # Folder moid value 660 assert_matches :resgroup- # ResourcePool moid value 661 assert_matches :dvs- # DistributedVirtualSwitch moid value 662 } 663 664 @test "object.method" { 665 vcsim_env_todo 666 667 vm=$(govc find vm -type m | head -1) 668 669 run govc object.method -enable=false -name NoSuchMethod "$vm" 670 assert_failure 671 672 run govc object.method -enable=false -name Destroy_Task enoent 673 assert_failure 674 675 run govc object.collect -s "$vm" disabledMethod 676 ! assert_matches "Destroy_Task" "$output" 677 678 run govc object.method -enable=false -name Destroy_Task "$vm" 679 assert_success 680 681 run govc object.collect -s "$vm" disabledMethod 682 assert_matches "Destroy_Task" "$output" 683 684 run govc object.method -enable -name Destroy_Task "$vm" 685 assert_success 686 687 run govc object.collect -s "$vm" disabledMethod 688 ! assert_matches "Destroy_Task" "$output" 689 } 690 691 @test "object.save" { 692 vcsim_env 693 694 dir="$BATS_TMPDIR/$(new_id)" 695 run govc object.save -v -d "$dir" /DC0/vm 696 assert_success 697 698 n=$(ls "$dir"/*.xml | wc -l) 699 rm -rf "$dir" 700 assert_equal 10 "$n" 701 } 702 703 @test "tree" { 704 vcsim_start -dc 2 -folder 1 -pod 1 -nsx 1 -pool 2 705 706 run govc tree 707 assert_success 708 709 run govc tree /DC0 710 assert_success 711 }