github.com/vmware/govmomi@v0.43.0/govc/test/library.bats (about) 1 #!/usr/bin/env bats 2 3 load test_helper 4 5 @test "library" { 6 vcsim_env 7 8 run govc library.create my-content 9 assert_success 10 id="$output" 11 12 jid=$(govc library.ls -json /my-content | jq -r .[].id) 13 assert_equal "$id" "$jid" 14 15 # run govc library.ls enoent 16 # assert_failure # TODO: currently exit 0's 17 18 run govc library.ls my-content 19 assert_success "/my-content" 20 21 run govc library.info /my-content 22 assert_success 23 assert_matches "$id" 24 25 # test tags attach,ls,detach for libraries 26 run govc tags.category.create -m "$(new_id)" 27 assert_success 28 category="$output" 29 30 tag_name=$(new_id) 31 run govc tags.create -c "$category" "$tag_name" 32 assert_success 33 tag=$output 34 35 run govc tags.attach "$tag" /my-content 36 assert_success 37 38 run govc tags.attached.ls "$tag_name" 39 assert_success "com.vmware.content.Library:$id" 40 41 run govc tags.attached.ls -r /my-content 42 assert_success "$tag_name" 43 44 run govc tags.attached.ls -r "com.vmware.content.Library:$id" 45 assert_success "$tag_name" 46 47 run govc tags.detach "$tag" /my-content 48 assert_success 49 50 run govc tags.attached.ls "$tag_name" 51 assert_success "" 52 53 run govc library.update -n new-content my-content 54 assert_success 55 56 run govc library.info new-content 57 assert_success 58 assert_matches "$id" 59 60 run govc library.rm /new-content 61 assert_success 62 } 63 64 @test "library.import" { 65 vcsim_env 66 67 run govc library.create my-content 68 assert_success 69 library_id="$output" 70 71 # run govc library.ls enoent 72 # assert_failure # TODO: currently exit 0's 73 74 # run govc library.info enoent 75 # assert_failure # TODO: currently exit 0's 76 77 run govc library.ls /my-content/* 78 assert_success "" 79 80 run govc library.import -n library.bats /my-content library.bats # any file will do 81 assert_success 82 83 run govc library.info /my-content/my-item 84 assert_success 85 assert_matches "$id" 86 87 run govc library.info /my-content/* 88 assert_success 89 assert_matches "$id" 90 91 id=$(govc library.ls -json "/my-content/library.bats" | jq -r .[].id) 92 93 run govc library.info "/my-content/library.bats" 94 assert_success 95 assert_matches "$id" 96 97 run govc library.rm /my-content/library.bats 98 assert_success 99 100 run govc library.import /my-content "$GOVC_IMAGES/$TTYLINUX_NAME.ova" 101 assert_success 102 103 run govc library.ls "/my-content/$TTYLINUX_NAME/*" 104 assert_success 105 assert_matches "$TTYLINUX_NAME.ovf" 106 assert_matches "$TTYLINUX_NAME-disk1.vmdk" 107 108 run govc library.export "/my-content/$TTYLINUX_NAME/*.ovf" - 109 assert_success "$(cat "$GOVC_IMAGES/$TTYLINUX_NAME.ovf")" 110 111 name="$BATS_TMPDIR/govc-$id-export" 112 run govc library.export "/my-content/$TTYLINUX_NAME/*.ovf" "$name" 113 assert_success 114 assert_equal "$(cat "$GOVC_IMAGES/$TTYLINUX_NAME.ovf")" "$(cat "$name")" 115 rm "$name" 116 117 mkdir "$name" 118 run govc library.export "/my-content/$TTYLINUX_NAME" "$name" 119 assert_success 120 assert_equal "$(cat "$GOVC_IMAGES/$TTYLINUX_NAME.ovf")" "$(cat "$name/$TTYLINUX_NAME.ovf")" 121 rm -r "$name" 122 123 run govc library.import /my-content "$GOVC_IMAGES/$TTYLINUX_NAME.ovf" 124 assert_failure # already_exists 125 126 run govc library.rm "/my-content/$TTYLINUX_NAME" 127 assert_success 128 129 run govc library.import /my-content "$GOVC_IMAGES/$TTYLINUX_NAME.ovf" 130 assert_success 131 132 run govc library.ls "/my-content/$TTYLINUX_NAME/*" 133 assert_success 134 assert_matches "$TTYLINUX_NAME.ovf" 135 assert_matches "$TTYLINUX_NAME-disk1.vmdk" 136 137 run govc library.import /my-content "$GOVC_IMAGES/$TTYLINUX_NAME.iso" 138 assert_failure # already_exists 139 140 run govc library.import -n ttylinux-live /my-content "$GOVC_IMAGES/$TTYLINUX_NAME.iso" 141 assert_success 142 143 run govc library.info -l "/my-content/ttylinux-live/$TTYLINUX_NAME.iso" 144 assert_success 145 146 run govc library.info -L /my-content/ttylinux-live/ 147 assert_success 148 assert_matches contentlib- 149 file="$output" 150 run govc datastore.ls "$file" 151 assert_matches "$TTYLINUX_NAME.iso" 152 153 run govc library.ls "/my-content/ttylinux-live/*" 154 assert_success 155 assert_matches "$TTYLINUX_NAME.iso" 156 157 if [ ! -e "$GOVC_IMAGES/ttylinux-latest.ova" ] ; then 158 ln -s "$GOVC_IMAGES/$TTYLINUX_NAME.ova" "$GOVC_IMAGES/ttylinux-latest.ova" 159 fi 160 # test where $name.{ovf,mf} differs from ova name 161 run govc library.import -m my-content "$GOVC_IMAGES/ttylinux-latest.ova" 162 assert_success 163 run govc library.update -n ttylinux_latest my-content/ttylinux-latest 164 assert_success 165 run govc library.ls "/my-content/ttylinux_latest/*" 166 assert_success 167 assert_matches "$TTYLINUX_NAME.ovf" 168 assert_matches "$TTYLINUX_NAME-disk1.vmdk" 169 170 summary="ISO \[${GOVC_DATASTORE}\] contentlib-.*${TTYLINUX_NAME}.iso" 171 172 run govc vm.create -on=false -iso "library:/my-content/ttylinux-live/$TTYLINUX_NAME.iso" library-iso-test 173 assert_success 174 175 run govc device.info -vm library-iso-test cdrom-* 176 assert_success 177 assert_matches "$summary" 178 179 run govc device.cdrom.eject -vm library-iso-test 180 assert_success 181 182 run govc device.cdrom.insert -vm library-iso-test "library:/my-content/ttylinux-live/$TTYLINUX_NAME.iso" 183 assert_success 184 185 run govc device.info -vm library-iso-test cdrom-* 186 assert_success 187 assert_matches "$summary" 188 } 189 190 @test "library.deploy" { 191 vcsim_env 192 193 run govc library.create my-content 194 assert_success 195 library_id="$output" 196 197 # link ovf/ova to datastore so we can test library.import with an http source 198 dir=$(govc datastore.info -json | jq -r .datastores[].info.url) 199 ln -s "$GOVC_IMAGES/$TTYLINUX_NAME."* "$dir" 200 201 run govc library.import -c fake -pull my-content -n invalid-sha1 "https://$(govc env GOVC_URL)/folder/$TTYLINUX_NAME.ovf" 202 assert_failure # invalid checksum 203 204 sum=$(sha256sum "$GOVC_IMAGES/$TTYLINUX_NAME.ovf" | awk '{print $1}') 205 run govc library.import -c "$sum" -pull my-content "https://$(govc env GOVC_URL)/folder/$TTYLINUX_NAME.ovf" 206 assert_success 207 208 run govc library.info -s "/my-content/ttylinux-live/$TTYLINUX_NAME.ovf" 209 assert_success 210 211 run env GOVC_SHOW_UNRELEASED=true govc library.info -S "/my-content/ttylinux-live/$TTYLINUX_NAME.ovf" 212 assert_success 213 214 run govc library.info -l -s /my-content/$TTYLINUX_NAME/$TTYLINUX_NAME.ovf 215 assert_success 216 217 run govc library.deploy "my-content/$TTYLINUX_NAME" ttylinux 218 assert_success 219 220 run govc vm.info ttylinux 221 assert_success 222 223 run govc library.import -pull -c fake -a MD5 -n invalid-md5 my-content "https://$(govc env GOVC_URL)/folder/$TTYLINUX_NAME.ova" 224 assert_failure # invalid checksum 225 226 sum=$(md5sum "$GOVC_IMAGES/$TTYLINUX_NAME.ovf" | awk '{print $1}') 227 run govc library.import -pull -c "$sum" -a MD5 -n ttylinux-unpacked my-content "https://$(govc env GOVC_URL)/folder/$TTYLINUX_NAME.ova" 228 assert_success 229 230 item_id=$(govc library.info -json /my-content/ttylinux-unpacked | jq -r .[].id) 231 assert_equal "$(cat "$GOVC_IMAGES/$TTYLINUX_NAME.ovf")" "$(cat "$dir/contentlib-$library_id/$item_id/$TTYLINUX_NAME.ovf")" 232 233 cat > "$BATS_TMPDIR/ttylinux.json" <<EOF 234 { 235 "DiskProvisioning": "flat", 236 "IPAllocationPolicy": "dhcpPolicy", 237 "IPProtocol": "IPv4", 238 "NetworkMapping": [ 239 { 240 "Name": "nat", 241 "Network": "DC0_DVPG0" 242 } 243 ], 244 "MarkAsTemplate": false, 245 "PowerOn": false, 246 "InjectOvfEnv": false, 247 "WaitForIP": false, 248 "Name": "ttylinux2" 249 } 250 EOF 251 252 run govc library.deploy "my-content/$TTYLINUX_NAME" -options "$BATS_TMPDIR/ttylinux.json" 253 assert_failure # see issue #2599 254 255 run govc library.deploy -options "$BATS_TMPDIR/ttylinux.json" "my-content/$TTYLINUX_NAME" 256 assert_success 257 rm "$BATS_TMPDIR/ttylinux.json" 258 259 run govc vm.info -r ttylinux2 260 assert_success 261 assert_matches DC0_DVPG0 262 assert_matches 32MB 263 assert_matches "1 vCPU" 264 265 run env GOVC_DATASTORE="" govc library.deploy "my-content/$TTYLINUX_NAME" ttylinux3 # datastore is not required 266 assert_success 267 268 run govc vm.destroy ttylinux ttylinux2 ttylinux3 269 assert_success 270 271 config=$(base64 <<<' 272 <obj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="vim25:VirtualMachineConfigSpec" xmlns:vim25="urn:vim25"> 273 <numCPUs>4</numCPUs> 274 <memoryMB>2048</memoryMB> 275 </obj>') 276 277 run env GOVC_SHOW_UNRELEASED=true govc library.deploy -config "$config" "my-content/$TTYLINUX_NAME" ttylinux 278 assert_success 279 280 run govc vm.info -r ttylinux 281 assert_success 282 assert_matches 2048MB 283 assert_matches "4 vCPU" 284 285 item_id=$(govc library.info -json "/my-content/$TTYLINUX_NAME" | jq -r .[].id) 286 287 run govc datastore.rm "contentlib-$library_id/$item_id" # remove library files out-of-band, forcing a deploy error below 288 assert_success 289 290 run govc library.deploy "my-content/$TTYLINUX_NAME" ttylinux2 291 assert_failure 292 } 293 294 @test "library.clone ovf" { 295 vcsim_env 296 297 vm=DC0_H0_VM0 298 item="${vm}_item" 299 300 run govc library.create my-content 301 assert_success 302 303 run govc library.clone -vm $vm -ovf -e -m my-content $item 304 assert_success 305 306 run govc vm.destroy $vm 307 assert_success 308 309 run govc library.ls my-content/ 310 assert_success /my-content/$item 311 312 run govc library.create mirror 313 assert_success 314 315 run govc library.cp /my-content/$item /mirror 316 assert_success 317 318 run govc library.ls mirror/ 319 assert_success /mirror/$item 320 } 321 322 @test "library.deploy vmtx" { 323 vcsim_env 324 325 vm=DC0_H0_VM0 326 item="${vm}_item" 327 328 run govc vm.clone -library enoent -vm $vm $item 329 assert_failure # library does not exist 330 331 run govc library.create my-content 332 assert_success 333 334 run govc library.deploy my-content/$item my-vm 335 assert_failure # vmtx item does not exist 336 337 run govc library.clone -vm $vm my-content $item 338 assert_success 339 340 run govc library.deploy my-content/$item my-vm 341 assert_success 342 343 run govc library.checkout my-content/enoent my-vm-checkout 344 assert_failure # vmtx item does not exist 345 346 run govc library.checkout my-content/$item my-vm-checkout 347 assert_success 348 349 run govc library.checkin -vm my-vm-checkout my-content/enoent 350 assert_failure # vmtx item does not exist 351 352 run govc library.checkin -vm my-vm-checkout my-content/$item 353 assert_success 354 355 run govc object.collect -s vm/$item config.template 356 assert_success "true" 357 358 run govc object.collect -s vm/$item summary.config.template 359 assert_success "true" 360 361 run govc vm.destroy $item 362 assert_success # expected to delete the CL item too 363 364 run govc library.deploy my-content/$item my-vm2 365 assert_failure # $item no longer exists 366 } 367 368 @test "library.vmtx.info" { 369 vcsim_env 370 371 vm=DC0_H0_VM0 372 item="${vm}_item" 373 374 run govc library.create my-content 375 assert_success 376 377 run govc library.clone -vm $vm my-content $item 378 assert_success 379 380 run govc library.vmtx.info my-content/$item 381 assert_success 382 } 383 384 @test "library.pubsub" { 385 vcsim_env 386 387 run govc library.create -pub published-content 388 assert_success 389 id="$output" 390 391 url="https://$(govc env GOVC_URL)/cls/vcsp/lib/$id" 392 393 run govc library.info published-content 394 assert_success 395 assert_matches "Publication:" 396 assert_matches "$url" 397 398 run govc library.import published-content "$GOVC_IMAGES/ttylinux-latest.ova" 399 assert_success 400 401 run govc library.create -sub "$url" my-content 402 assert_success 403 404 run govc library.info my-content 405 assert_success 406 assert_matches "Subscription:" 407 assert_matches "$url" 408 409 run govc library.import my-content "$GOVC_IMAGES/$TTYLINUX_NAME.iso" 410 assert_failure # cannot add items to subscribed libraries 411 412 run govc library.ls my-content/ttylinux-latest/ 413 assert_success 414 assert_matches "/my-content/ttylinux-latest/ttylinux-pc_i486-16.1.ovf" 415 416 run govc library.sync my-content 417 assert_success 418 } 419 420 @test "library.subscriber example" { 421 vcsim_start -ds 3 422 423 ds0=LocalDS_0 424 ds1=LocalDS_1 425 ds2=LocalDS_2 426 pool=DC0_C0/Resources 427 428 # Create a published library with OVA items 429 govc library.create -ds $ds0 -pub ttylinux-pub-ovf 430 431 govc library.import ttylinux-pub-ovf "$GOVC_IMAGES/ttylinux-pc_i486-16.1.ova" 432 433 url="$(govc library.info -U ttylinux-pub-ovf)" 434 echo "$url" 435 436 # Create a library subscribed to the publish-content library 437 govc library.create -ds $ds0 -sub "$url" govc-sub-ovf 438 439 # Create a library to contain VM Templates, and enabling publishing 440 govc library.create -ds $ds0 -pub govc-pub-vmtx 441 442 url="$(govc library.info -U govc-pub-vmtx)" 443 echo "$url" 444 445 # Create vm inventory folder to contain govc-pub-vmtx library templates 446 govc folder.create vm/govc-pub-vmtx 447 448 # Convert govc-sub-ovf's OVA items to VMTX items in the govc-pub-vmtx library 449 govc library.sync -folder govc-pub-vmtx -pool $pool -vmtx govc-pub-vmtx govc-sub-ovf 450 451 # No existing subscribers 452 govc library.subscriber.ls govc-pub-vmtx 453 454 for ds in $ds1 $ds2 ; do 455 # Create a library subscribed to the govc-pub-vmtx library 456 govc library.create -ds $ds -sub "$url" govc-sub-vmtx-$ds 457 458 # Create vm inventory folder to contain sub-content library templates 459 govc folder.create vm/govc-sub-vmtx-$ds 460 461 # Create a subscriber to which the VM Templates can be published 462 govc library.subscriber.create -folder govc-sub-vmtx-$ds -pool $pool govc-pub-vmtx govc-sub-vmtx-$ds 463 done 464 465 govc library.subscriber.ls govc-pub-vmtx | grep govc-sub-vmtx-$ds1 466 govc library.subscriber.ls govc-pub-vmtx | grep govc-sub-vmtx-$ds2 467 468 # Expect 1 VM: govc-pub-vmtx/ttylinux-pc_i486-16.1 469 govc find vm -type f -name govc-* | $xargs -n1 -r govc find -type m 470 471 # Publish entire library 472 govc library.publish govc-pub-vmtx 473 474 # Publish a specific item 475 govc library.publish govc-pub-vmtx/ttylinux-pc_i486-16.1 476 477 # Expect 2 more VMs: govc-sub-vmtx-{$ds1,$ds2} 478 govc find vm -type f -name govc-* | $xargs -n1 govc find -type m 479 480 for ds in $ds1 $ds2 ; do 481 govc vm.clone -link -vm govc-sub-vmtx-$ds/ttylinux-pc_i486-16.1 -ds $ds -pool $pool -folder govc-sub-vmtx-$ds ttylinux 482 done 483 } 484 485 @test "library.create.withpolicy" { 486 vcsim_env 487 488 policy_id=$(govc library.policy.ls -json | jq '.[][0].policy' -r) 489 echo "$policy_id" 490 491 run govc library.create -policy=foo secure-content 492 assert_failure 493 494 run govc library.create -policy "$policy_id" secure-content 495 assert_success 496 497 library_secpol=$(govc library.info -json secure-content | jq '.[].security_policy_id' -r) 498 assert_equal "$library_secpol" "$policy_id" 499 500 run govc library.import secure-content "$GOVC_IMAGES/ttylinux-latest.ova" 501 assert_success 502 503 run govc library.info -json secure-content/ttylinux-latest 504 assert_success 505 506 assert_equal false "$(jq -r <<<"$output" .[].security_compliance)" 507 508 assert_equal NOT_AVAILABLE "$(jq -r <<<"$output" .[].certificate_verification_info.status)" 509 510 run govc library.rm secure-content 511 assert_success 512 } 513 514 @test "library.findbyid" { 515 vcsim_env 516 517 run govc library.create my-content 518 assert_success 519 id="$output" 520 521 run govc library.create my-content 522 assert_success 523 524 run govc library.import my-content library.bats 525 assert_failure # "my-content" matches 2 items 526 527 run govc library.import "$id" library.bats 528 assert_success # using id to find library 529 530 n=$(govc library.info my-content | grep -c Name:) 531 [ "$n" == 2 ] 532 533 n=$(govc library.info "$id" | grep -c Name:) 534 [ "$n" == 1 ] 535 536 run govc library.rm my-content 537 assert_failure # "my-content" matches 2 items 538 539 run govc library.rm "$id" 540 assert_success 541 542 n=$(govc library.info my-content | grep -c Name:) 543 [ "$n" == 1 ] 544 } 545 546 @test "library.trust" { 547 vcsim_env 548 549 run govc library.trust.ls 550 assert_success 551 552 run govc library.trust.info enoent 553 assert_failure # id does not exist 554 555 run govc library.trust.rm enoent 556 assert_failure # id does not exist 557 558 pem=$(new_id) 559 run govc extension.setcert -cert-pem ++ -org govc-library-trust "$pem" # generate a cert for testing 560 assert_success 561 562 run govc library.trust.create "$pem.crt" 563 assert_success 564 565 id=$(govc library.trust.ls | grep O=govc-library-trust | awk '{print $1}') 566 run govc library.trust.info "$id" 567 assert_success 568 569 run govc library.trust.rm "$id" 570 assert_success 571 572 run govc library.trust.info "$id" 573 assert_failure # id does not exist 574 575 date > "$pem.crt" 576 run govc library.trust.create "$id.crt" 577 assert_failure # invalid cert 578 579 # remove generated cert and key 580 rm "$pem".{crt,key} 581 } 582 583 @test "library.session" { 584 vcsim_env 585 586 run govc library.session.ls 587 assert_success 588 589 run govc library.create my-content 590 assert_success 591 592 run govc library.import /my-content "$GOVC_IMAGES/$TTYLINUX_NAME.ova" 593 assert_success 594 595 run govc library.session.ls 596 assert_success 597 assert_matches ttylinux 598 599 run govc library.session.ls -json 600 assert_success 601 602 run govc library.session.ls -json -i 603 assert_success 604 605 n=$(govc library.session.ls -json -i | jq '.files[] | length') 606 assert_equal 2 "$n" # .ovf + .vmdk 607 608 id=$(govc library.session.ls -json | jq -r .sessions[].id) 609 610 run govc library.session.rm -i "$id" ttylinux-pc_i486-16.1.ovf 611 assert_failure # removeFile not allowed in state DONE 612 assert_matches "500 Internal Server Error" 613 614 run govc library.session.rm "$id" 615 assert_success 616 } 617 618 @test "library.probe" { 619 vcsim_env 620 621 export GOVC_SHOW_UNRELEASED=true 622 623 run govc library.probe 624 assert_failure 625 626 run govc library.probe https://www.vmware.com 627 assert_success 628 629 run govc library.probe -f ftp://www.vmware.com 630 if [ "$status" -ne 22 ]; then 631 flunk $(printf "expected failed exit status=22, got status=%d" $status) 632 fi 633 634 run govc library.probe -json ftp://www.vmware.com 635 assert_success 636 assert_matches INVALID_URL 637 } 638 639 @test "library.evict" { 640 vcsim_env 641 642 run govc library.create -pub published-content 643 assert_success 644 id="$output" 645 646 url="https://$(govc env GOVC_URL)/cls/vcsp/lib/$id" 647 648 run govc library.info published-content 649 assert_success 650 assert_matches "Publication:" 651 assert_matches "$url" 652 653 run govc library.import published-content "$GOVC_IMAGES/ttylinux-latest.ova" 654 assert_success 655 656 run govc library.create -sub "$url" -sub-ondemand=true subscribed-content 657 assert_success 658 659 run govc library.info subscribed-content 660 assert_success 661 assert_matches "Subscription:" 662 assert_matches "$url" 663 664 run govc library.ls subscribed-content/ttylinux-latest/ 665 assert_success 666 assert_matches "/subscribed-content/ttylinux-latest/ttylinux-pc_i486-16.1.ovf" 667 668 run govc library.sync subscribed-content/ttylinux-latest 669 assert_success 670 671 # assert cached is false after item sync 672 cached=$(govc library.info subscribed-content/ttylinux-latest | grep Cached: | awk '{print $2}') 673 assert_equal "true" "$cached" 674 675 run govc library.evict subscribed-content/ttylinux-latest 676 assert_success 677 678 # assert cached is false after library item evict 679 cached=$(govc library.info subscribed-content/ttylinux-latest | grep Cached: | awk '{print $2}') 680 assert_equal "false" "$cached" 681 }