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