github.com/vmware/govmomi@v0.43.0/govc/test/vcsim.bats (about) 1 #!/usr/bin/env bats 2 3 load test_helper 4 5 @test "vcsim rbvmomi" { 6 if ! ruby -e "require 'rbvmomi'" ; then 7 skip "requires rbvmomi" 8 fi 9 10 vcsim_env 11 12 ruby ./vcsim_test.rb "$(govc env -x GOVC_URL_PORT)" 13 } 14 15 @test "vcsim powercli" { 16 require_docker 17 18 vcsim_env -l 0.0.0.0:0 19 20 server=$(govc env -x GOVC_URL_HOST) 21 port=$(govc env -x GOVC_URL_PORT) 22 23 # docker run --rm projects.registry.vmware.com/pez/powerclicore@sha256:09b29f69c0653f871f6d569f7c4c03c952909f68a27e9792ef2f7c8653235668 /usr/bin/pwsh -f - <<EOF 24 docker run --rm ghcr.io/embano1/powerclicore@sha256:09b29f69c0653f871f6d569f7c4c03c952909f68a27e9792ef2f7c8653235668 /usr/bin/pwsh -f - <<EOF 25 Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:\$false | Out-Null 26 Connect-VIServer -Server $server -Port $port -User user -Password pass 27 28 Get-VM 29 Get-VIEvent 30 Get-VirtualNetwork 31 EOF 32 } 33 34 @test "vcsim examples" { 35 vcsim_env 36 37 # compile + run examples against vcsim 38 for main in ../../examples/*/main.go ; do 39 # TODO: #2476 40 if [[ $main =~ alarm ]]; then 41 continue 42 fi 43 run go run "$main" -insecure -url "$GOVC_URL" 44 assert_success 45 done 46 } 47 48 @test "vcsim about" { 49 vcsim_env -dc 2 -cluster 3 -vm 0 -ds 0 50 51 url="https://$(govc env GOVC_URL)" 52 53 run curl -skf "$url/about" 54 assert_matches "CurrentTime" # 1 param (without Context) 55 assert_matches "TerminateSession" # 2 params (with Context) 56 assert_matches "CnsAttachVolume" # method from namespace vsan 57 assert_matches "PbmCreate" # method from namespace pbm 58 59 run curl -skf "$url/debug/vars" 60 assert_success 61 62 model=$(curl -sfk "$url/debug/vars" | jq .vcsim.model) 63 [ "$(jq .datacenter <<<"$model")" == "2" ] 64 [ "$(jq .cluster <<<"$model")" == "6" ] 65 [ "$(jq .machine <<<"$model")" == "0" ] 66 [ "$(jq .datastore <<<"$model")" == "0" ] 67 68 run govc about 69 assert_success 70 assert_matches "govmomi simulator" 71 } 72 73 @test "truncated WaitForUpdatesEx" { 74 total=142 # 100 is the default MaxObjectUpdates 75 vcsim_env -standalone-host 0 -vm $total 76 77 url="https://$(govc env GOVC_URL)" 78 79 model=$(curl -sfk "$url/debug/vars" | jq .vcsim.model) 80 vms=$(jq .machine <<<"$model") 81 assert_equal $total "$vms" # sanity check 82 83 run govc object.collect -type m / name 84 assert_success 85 [ ${#lines[@]} -eq "$total" ] 86 } 87 88 @test "vcsim host placement" { 89 vcsim_start -dc 0 90 91 # https://github.com/vmware/govmomi/issues/1258 92 id=$(new_id) 93 govc datacenter.create DC0 94 govc cluster.create comp 95 govc cluster.add -cluster comp -hostname test.host.com -username user -password pass 96 govc cluster.add -cluster comp -hostname test2.host.com -username user -password pass 97 govc datastore.create -type local -name vol6 -path "$BATS_TMPDIR" test.host.com 98 govc pool.create comp/Resources/testPool 99 govc vm.create -c 1 -ds vol6 -g centos64Guest -pool testPool -m 4096 "$id" 100 govc vm.destroy "$id" 101 } 102 103 @test "vcsim host config.port" { 104 vcsim_start -dc 0 105 url=$(govc env GOVC_URL) 106 port=$(govc env -x GOVC_URL_PORT) 107 vcsim_stop 108 109 vcsim_start -l "$url" # reuse free port selection from above 110 111 run govc object.collect -s -type h host/DC0_H0 summary.config.port 112 assert_success "$port" 113 ports=$(govc object.collect -s -type h / summary.config.port | uniq -u | wc -l) 114 assert_equal 0 "$ports" # all host ports should be the same value 115 116 vcsim_stop 117 118 VCSIM_HOST_PORT_UNIQUE=true vcsim_start -l "$url" 119 120 hosts=$(curl -sk "https://$url/debug/vars" | jq .vcsim.model.host) 121 ports=$(govc object.collect -s -type h / summary.config.port | uniq -u | wc -l) 122 assert_equal "$ports" "$hosts" # all host ports should be unique 123 grep -v "$port" <<<"$ports" # host ports should not include vcsim port 124 } 125 126 @test "vcsim set vm properties" { 127 vcsim_env 128 129 vm=/DC0/vm/DC0_H0_VM0 130 131 run govc object.collect $vm guest.ipAddress 132 assert_success "" 133 134 run govc vm.change -vm $vm -e SET.guest.ipAddress=10.0.0.1 135 assert_success 136 137 run govc object.collect -s $vm guest.ipAddress 138 assert_success "10.0.0.1" 139 140 run govc vm.ip $vm 141 assert_success "10.0.0.1" 142 143 run govc object.collect -s $vm summary.guest.ipAddress 144 assert_success "10.0.0.1" 145 146 netip=$(govc object.collect -json -o $vm guest.net | jq -r .guest.net[].ipAddress[0]) 147 [ "$netip" = "10.0.0.1" ] 148 149 run govc vm.info -vm.ip 10.0.0.1 150 assert_success 151 152 run govc object.collect -s $vm guest.hostName 153 assert_success "" 154 155 run govc vm.change -vm $vm -e SET.guest.hostName=localhost.localdomain 156 assert_success 157 158 run govc object.collect -s $vm guest.hostName 159 assert_success "localhost.localdomain" 160 161 run govc object.collect -s $vm summary.guest.hostName 162 assert_success "localhost.localdomain" 163 164 run govc vm.info -vm.dns localhost.localdomain 165 assert_success 166 167 uuid=$(vcsim uuidgen) 168 run govc vm.change -vm $vm -e SET.config.uuid="$uuid" 169 assert_success 170 171 run govc object.collect -s $vm config.uuid 172 assert_success "$uuid" 173 174 govc import.ovf -options - "$GOVC_IMAGES/$TTYLINUX_NAME.ovf" <<EOF 175 { 176 "PropertyMapping": [ 177 { 178 "Key": "SET.guest.ipAddress", 179 "Value": "10.0.0.42" 180 } 181 ], 182 "PowerOn": true, 183 "WaitForIP": true 184 } 185 EOF 186 187 run govc vm.ip "$TTYLINUX_NAME" 188 assert_success "10.0.0.42" 189 190 run govc vm.destroy "$TTYLINUX_NAME" 191 assert_success 192 193 govc import.ovf -options - "$GOVC_IMAGES/$TTYLINUX_NAME.ovf" <<EOF 194 { 195 "PropertyMapping": [ 196 { 197 "Key": "ip0", 198 "Value": "10.0.0.43" 199 } 200 ], 201 "PowerOn": true, 202 "WaitForIP": true 203 } 204 EOF 205 206 run govc vm.ip "$TTYLINUX_NAME" 207 assert_success "10.0.0.43" 208 } 209 210 @test "vcsim vm.create" { 211 vcsim_env 212 213 # VM uuids are stable, based on path to .vmx 214 run govc object.collect -s vm/DC0_H0_VM0 config.uuid config.instanceUuid 215 assert_success "$(printf "265104de-1472-547c-b873-6dc7883fb6cb\nb4689bed-97f0-5bcd-8a4c-07477cc8f06f")" 216 217 dups=$(govc object.collect -s -type m / config.uuid | sort | uniq -d | wc -l) 218 assert_equal 0 "$dups" 219 220 run govc object.collect -s host/DC0_H0/DC0_H0 summary.hardware.uuid 221 assert_success dcf7fb3c-4a1c-5a05-b730-5e09f3704e2f 222 223 dups=$(govc object.collect -s -type m / summary.hardware.uuid | sort | uniq -d | wc -l) 224 assert_equal 0 "$dups" 225 226 run govc vm.create foo.yakity 227 assert_success 228 229 run govc vm.create bar.yakity 230 assert_success 231 } 232 233 @test "vcsim issue #1251" { 234 vcsim_env 235 236 govc object.collect -type ComputeResource -n 1 / name & 237 pid=$! 238 # give some time for the above to park in WaitForUpdates, 239 # otherwise the rename may happen first and collect will hang as it missed the update 240 sleep .2 241 242 run govc object.rename /DC0/host/DC0_C0 DC0_C0b 243 assert_success 244 245 wait $pid 246 247 govc object.collect -type ClusterComputeResource -n 1 / name & 248 pid=$! 249 sleep .2 250 251 run govc object.rename /DC0/host/DC0_C0b DC0_C0 252 assert_success 253 254 wait $pid 255 } 256 257 @test "vcsim issue #3396" { 258 vcsim_env 259 260 govc pool.create /DC0/host/DC0_C0/Resources/foo 261 govc pool.create /DC0/host/DC0_C0/Resources/foo/bar 262 govc find -l /DC0/host/DC0_C0/Resources 263 govc pool.destroy /DC0/host/DC0_C0/Resources/foo 264 # prior to the fix, "bar"'s Parent was still "foo", causing the following to hang 265 govc find -l /DC0/host/DC0_C0/Resources 266 } 267 268 @test "vcsim run container" { 269 require_docker 270 271 vcsim_env -autostart=false 272 273 vm=DC0_H0_VM0 274 name=$(docker_name $vm) 275 276 if docker inspect "$name" ; then 277 flunk "$vm container still exists" 278 fi 279 280 run govc vm.change -vm $vm -e RUN.container=nginx 281 assert_success 282 283 run govc vm.power -on $vm 284 assert_success 285 286 if ! docker inspect "$name" ; then 287 flunk "$vm container does not exist" 288 fi 289 290 ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$name") 291 run govc object.collect -s vm/$vm guest.ipAddress 292 assert_success "$ip" 293 294 run govc object.collect -s vm/$vm summary.guest.ipAddress 295 assert_success "$ip" 296 297 netip=$(govc object.collect -json -o vm/$vm guest.net | jq -r .guest.net[].ipAddress[0]) 298 [ "$netip" = "$ip" ] 299 300 run govc vm.ip $vm # covers VirtualMachine.WaitForIP 301 assert_success "$ip" 302 303 run govc vm.ip -a $vm # covers VirtualMachine.WaitForNetIP 304 assert_success "$ip" 305 306 run govc vm.ip -n ethernet-0 $vm # covers VirtualMachine.WaitForNetIP 307 assert_success "$ip" 308 309 run govc vm.power -s $vm 310 assert_success 311 312 # wait for power state == off after guest shutdown above 313 run govc object.collect -s vm/$vm -runtime.powerState poweredOff 314 assert_success 315 316 run docker inspect -f '{{.State.Status}}' "$name" 317 assert_success "exited" 318 319 run govc vm.power -on $vm 320 assert_success 321 322 run docker inspect -f '{{.State.Status}}' "$name" 323 assert_success "running" 324 325 run govc vm.destroy $vm 326 assert_success 327 328 if docker inspect "$name" ; then 329 flunk "$vm container still exists" 330 fi 331 332 vm=DC0_H0_VM1 333 name=$(docker_name $vm) 334 335 # test json encoded args 336 run govc vm.change -vm $vm -e RUN.container="[\"-v\", \"$PWD:/usr/share/nginx/html:ro\", \"nginx\"]" 337 assert_success 338 339 # test bash -c args parsing 340 run govc vm.change -vm $vm -e RUN.container="-v '$PWD:/usr/share/nginx/html:ro' nginx" 341 assert_success 342 343 run govc vm.power -on $vm 344 assert_success 345 346 run docker inspect "$name" 347 assert_success 348 349 ip=$(govc object.collect -s vm/$vm guest.ipAddress) 350 run docker run --rm curlimages/curl curl -f "http://$ip/vcsim.bats" 351 assert_success 352 353 # test suspend/resume 354 run docker inspect -f '{{.State.Status}}' "$name" 355 assert_success "running" 356 357 run govc vm.power -suspend $vm 358 assert_success 359 360 run docker inspect -f '{{.State.Status}}' "$name" 361 assert_success "paused" 362 363 run govc vm.power -on $vm 364 assert_success 365 366 run docker inspect -f '{{.State.Status}}' "$name" 367 assert_success "running" 368 369 run docker volume inspect "$name--dmi" 370 assert_success 371 372 run govc vm.destroy $vm 373 assert_success 374 375 run docker volume inspect "$name" 376 assert_failure 377 378 vm=DC0_C0_RP0_VM0 379 name=$(docker_name $vm) 380 381 run govc vm.change -vm $vm -e RUN.container="busybox grep VMware- /sys/class/dmi/id/product_serial" 382 assert_success 383 384 run govc vm.power -on $vm 385 assert_success 386 387 run docker inspect -f '{{.State.ExitCode}}' "$name" 388 assert_success "0" 389 390 run govc vm.destroy $vm 391 assert_success 392 393 vm=DC0_C0_RP0_VM1 394 name=$(docker_name $vm) 395 396 run govc vm.change -vm $vm -e RUN.container="busybox sh -c 'sleep \$VMX_GUESTINFO_SLEEP'" -e guestinfo.sleep=500 397 assert_success 398 399 run govc vm.power -on $vm 400 assert_success 401 402 run docker inspect -f '{{.State.Status}}' "$name" 403 assert_success "running" 404 405 # stopping vcsim should remove the containers and volumes 406 vcsim_stop 407 408 run docker inspect "$name" 409 assert_failure 410 411 run docker volume inspect "$name" 412 assert_failure 413 } 414 415 @test "vcsim listen" { 416 vcsim_start -dc 0 417 url=$(govc option.ls vcsim.server.url) 418 [[ "$url" == *"https://127.0.0.1:"* ]] 419 vcsim_stop 420 421 vcsim_start -dc 0 -l 0.0.0.0:0 422 url=$(govc option.ls vcsim.server.url) 423 [[ "$url" != *"https://127.0.0.1:"* ]] 424 [[ "$url" != *"https://[::]:"* ]] 425 vcsim_stop 426 } 427 428 @test "vcsim vapi auth" { 429 vcsim_env 430 431 url=$(govc env GOVC_URL) 432 433 run curl -fsk "https://$url/rest/com/vmware/cis/tagging/tag" 434 [ "$status" -ne 0 ] # not authenticated 435 436 run curl -fsk -X POST "https://$url/rest/com/vmware/cis/session" 437 [ "$status" -ne 0 ] # no basic auth header 438 439 run curl -fsk -X POST --user user: "https://$url/rest/com/vmware/cis/session" 440 [ "$status" -ne 0 ] # no password 441 442 run curl -fsk -X POST --user "$USER:pass" "https://$url/rest/com/vmware/cis/session" 443 assert_success # login with user:pass 444 445 id=$(jq -r .value <<<"$output") 446 447 run curl -fsk "https://$url/rest/com/vmware/cis/session" 448 [ "$status" -ne 0 ] # no header or cookie 449 450 run curl -fsk "https://$url/rest/com/vmware/cis/session" -H "vmware-api-session-id:$id" 451 assert_success # valid session header 452 453 user=$(jq -r .value.user <<<"$output") 454 assert_equal "$USER" "$user" 455 } 456 457 @test "vcsim auth" { 458 vcsim_start 459 460 run env GOVC_USERNAME=anybody GOVC_PASSWORD=anything govc ls -u "$(govc env GOVC_URL)" 461 assert_success 462 463 vcsim_stop 464 465 vcsim_start -username nobody -password nothing 466 467 run govc ls 468 assert_success 469 470 run govc tags.ls 471 assert_success 472 473 run env GOVC_USERNAME=nobody GOVC_PASSWORD=nothing govc ls -u "$(govc env GOVC_URL)" 474 assert_success 475 476 run env GOVC_USERNAME=nobody GOVC_PASSWORD=nothing govc tags.ls -u "$(govc env GOVC_URL)" 477 assert_success 478 479 run govc ls -u "user:pass@$(govc env GOVC_URL)" 480 assert_failure 481 482 run govc tags.ls -u "user:pass@$(govc env GOVC_URL)" 483 assert_failure 484 485 run env GOVC_USERNAME=user GOVC_PASSWORD=pass govc ls -u "$(govc env GOVC_URL)" 486 assert_failure 487 488 run env GOVC_USERNAME=user GOVC_PASSWORD=pass govc tags.ls -u "$(govc env GOVC_URL)" 489 assert_failure 490 491 run govc sso.user.create -p pass user 492 assert_success 493 494 run govc ls -u "user:pass@$(govc env GOVC_URL)" 495 assert_success 496 497 run govc tags.ls -u "user:pass@$(govc env GOVC_URL)" 498 assert_success 499 500 vcsim_stop 501 502 dir=$($mktemp --tmpdir -d govc-test-XXXXX 2>/dev/null || $mktemp -d -t govc-test-XXXXX) 503 echo nobody > "$dir/username" 504 echo nothing > "$dir/password" 505 506 vcsim_start -username "$dir/username" -password "$dir/password" 507 508 run govc ls 509 assert_success 510 511 run env GOVC_USERNAME="$dir/username" GOVC_PASSWORD="$dir/password" govc ls -u "$(govc env GOVC_URL)" 512 assert_success 513 514 run govc ls -u "user:pass@$(govc env GOVC_URL)" 515 assert_failure 516 517 vcsim_stop 518 519 rm -rf "$dir" 520 } 521 522 @test "vcsim ovftool" { 523 if ! ovftool -h >/dev/null ; then 524 skip "requires ovftool" 525 fi 526 527 vcsim_env 528 529 url=$(govc env GOVC_URL) 530 531 run ovftool --noSSLVerify --acceptAllEulas -ds=LocalDS_0 --network=DC0_DVPG0 "$GOVC_IMAGES/$TTYLINUX_NAME.ova" "vi://user:pass@$url/DC0/host/DC0_C0/DC0_C0_H1" 532 assert_success 533 534 run govc vm.destroy "$TTYLINUX_NAME" 535 assert_success 536 } 537 538 @test "vcsim model load" { 539 vcsim_start 540 dir="$BATS_TMPDIR/$(new_id)" 541 govc object.save -v -d "$dir" 542 vcsim_stop 543 544 vcsim_env -load "$dir" 545 rm -rf "$dir" 546 547 govc object.collect -s -type h / configManager.networkSystem | xargs -n1 -I% govc object.collect -s % dnsConfig 548 549 objs=$(govc find / | wc -l) 550 assert_equal 23 "$objs" 551 552 run govc cluster.add -cluster DC0_C0 -hostname DC0_C0_H0-clone -username DC0_C0_H0 -password pass -noverify 553 assert_success 554 objs=$(govc find / | wc -l) 555 assert_equal 24 "$objs" 556 557 run govc host.add -hostname DC0_H0-clone -username DC0_H0 -password pass -noverify 558 assert_success 559 objs=$(govc find / | wc -l) 560 assert_equal 27 "$objs" # ComputeResource + ResourcePool + HostSystem 561 562 run govc host.portgroup.add -host DC0_H0 -vswitch vSwitch0 bridge 563 assert_success # issue #2016 564 } 565 566 @test "vcsim trace file" { 567 file="$BATS_TMPDIR/$(new_id).trace" 568 569 vcsim_start -trace-file "$file" 570 571 run govc ls 572 assert_success 573 574 vcsim_stop 575 576 run ls -l "$file" 577 assert_success 578 579 rm "$file" 580 }