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