github.com/vmware/govmomi@v0.37.2/govc/test/vm.bats (about)

     1  #!/usr/bin/env bats
     2  
     3  load test_helper
     4  
     5  @test "vm.ip" {
     6    vcsim_env -autostart=false
     7  
     8    id=/DC0/vm/DC0_H0_VM0
     9  
    10    mac=00:50:56:83:3a:5d
    11    run govc vm.customize -vm $id -mac $mac -ip 10.0.0.1 -netmask 255.255.0.0 -type Linux
    12    assert_success
    13  
    14    run govc vm.power -on $id
    15    assert_success
    16  
    17    run govc vm.ip -wait 5s $id
    18    assert_success
    19  
    20    run govc vm.ip -wait 5s -a -v4 $id
    21    assert_success
    22  
    23    run govc vm.ip -wait 5s -n $mac $id
    24    assert_success
    25  
    26    run govc vm.ip -wait 5s -n ethernet-0 $id
    27    assert_success
    28  
    29    ip=$(govc vm.ip -wait 5s $id)
    30  
    31    # add a second nic
    32    run govc vm.network.add -vm $id "VM Network"
    33    assert_success
    34  
    35    res=$(govc vm.ip -wait 5s -n ethernet-0 $id)
    36    assert_equal $ip $res
    37  }
    38  
    39  @test "vm.ip capital MAC" {
    40    vcsim_env -autostart=false
    41  
    42    id=/DC0/vm/DC0_H0_VM0
    43  
    44    mac=00:50:56:83:3A:5D
    45    run govc vm.customize -vm $id -mac $mac -ip 10.0.0.1 -netmask 255.255.0.0 -type Linux
    46    assert_success
    47  
    48    run govc vm.power -on $id
    49    assert_success
    50  
    51    run govc vm.ip -wait 5s $id
    52    assert_success
    53  
    54    run govc vm.ip -wait 5s -a -v4 $id
    55    assert_success
    56  
    57    run govc vm.ip -wait 5s -n $mac $id
    58    assert_success
    59  
    60    run govc vm.ip -wait 5s -n ethernet-0 $id
    61    assert_success
    62  
    63    ip=$(govc vm.ip -wait 5s $id)
    64  
    65    # add a second nic
    66    run govc vm.network.add -vm $id "VM Network"
    67    assert_success
    68  
    69    res=$(govc vm.ip -wait 5s -n ethernet-0 $id)
    70    assert_equal $ip $res
    71  }
    72  
    73  @test "vm.ip -esxcli" {
    74    esx_env
    75  
    76    ok=$(govc host.esxcli system settings advanced list -o /Net/GuestIPHack | grep ^IntValue: | awk '{print $2}')
    77    if [ "$ok" != "1" ] ; then
    78      skip "/Net/GuestIPHack=0"
    79    fi
    80    id=$(new_ttylinux_vm)
    81  
    82    run govc vm.power -on $id
    83    assert_success
    84  
    85    run govc vm.ip -esxcli $id
    86    assert_success
    87  
    88    ip_esxcli=$output
    89  
    90    run govc vm.ip $id
    91    assert_success
    92    ip_tools=$output
    93  
    94    assert_equal $ip_esxcli $ip_tools
    95  }
    96  
    97  @test "vm.create" {
    98    unset GOVC_DATASTORE
    99    vcsim_start
   100  
   101    run govc cluster.create empty-cluster
   102    assert_success
   103  
   104    id=$(new_id)
   105    run govc vm.create -on=false "$id"
   106    assert_failure # -pool must be specified
   107  
   108    run govc vm.create -pool DC0_C0/Resources "$id"
   109    assert_success
   110  
   111    id=$(new_id)
   112    run govc vm.create -cluster enoent "$id"
   113    assert_failure # cluster does not exist
   114  
   115    run govc vm.create -cluster empty-cluster "$id"
   116    assert_failure # cluster has no hosts
   117  
   118    run govc vm.create -cluster DC0_C0 "$id"
   119    assert_success
   120  
   121    run govc vm.create -force -cluster DC0_C0 "$id"
   122    assert_success # create vm with the same name
   123  
   124    run govc vm.create -cluster DC0_C0 "my:vm"
   125    assert_success # vm has special characters (moref)
   126  
   127    run govc object.collect -s vm/my:vm name
   128    assert_success my:vm
   129  }
   130  
   131  @test "vm.change" {
   132    vcsim_env
   133  
   134    id=DC0_H0_VM0
   135  
   136    run govc vm.change -g ubuntu64Guest -m 1024 -c 2 -vm $id
   137    assert_success
   138  
   139    run govc vm.info $id
   140    assert_success
   141    assert_matches "buntu"
   142    assert_line "Memory: 1024MB"
   143    assert_line "CPU: 2 vCPU(s)"
   144  
   145    # test extraConfig
   146    run govc vm.change -e "guestinfo.a=1" -e "guestinfo.b=2" -vm $id
   147    assert_success
   148  
   149    run govc vm.info -e $id
   150    assert_success
   151    assert_line "guestinfo.a: 1"
   152    assert_line "guestinfo.b: 2"
   153  
   154    # test extraConfigFile
   155    run govc vm.change -f "guestinfo.c=this_is_not_an_existing.file" -vm $id
   156    assert_failure
   157  
   158    echo -n "3" > "$BATS_TMPDIR/extraConfigFile.conf"
   159    run govc vm.change -f "guestinfo.d=$BATS_TMPDIR/extraConfigFile.conf" -vm $id
   160    assert_success
   161  
   162    run govc vm.info -e $id
   163    assert_success
   164    assert_line "guestinfo.d: 3"
   165  
   166    run govc vm.change -sync-time-with-host=false -vm $id
   167    assert_success
   168  
   169    run govc vm.info -t $id
   170    assert_success
   171    assert_line "SyncTimeWithHost: false"
   172  
   173    run govc vm.change -sync-time-with-host=true -vm $id
   174    assert_success
   175  
   176    run govc vm.info -t $id
   177    assert_success
   178    assert_line "SyncTimeWithHost: true"
   179  
   180    run govc object.collect -s "vm/$id" config.memoryAllocation.reservation
   181    assert_success 0
   182  
   183    govc vm.change -vm "$id" -mem.reservation 1024
   184  
   185    run govc object.collect -s "vm/$id" config.memoryAllocation.reservation
   186    assert_success 1024
   187  
   188    run govc vm.change -annotation $$ -vm "$id"
   189    assert_success
   190  
   191    run govc object.collect -s "vm/$id" config.annotation
   192    assert_success $$
   193  
   194    uuid=$(vcsim uuidgen)
   195    run govc vm.change -vm $id -uuid "$uuid"
   196    assert_success
   197    run govc object.collect -s "vm/$id" config.uuid
   198    assert_success "$uuid"
   199  
   200    nid=$(new_id)
   201    run govc vm.change -name $nid -vm $id
   202    assert_success
   203  
   204    run govc vm.info $id
   205    [ ${#lines[@]} -eq 0 ]
   206  
   207    run govc vm.info $nid
   208    [ ${#lines[@]} -gt 0 ]
   209  }
   210  
   211  @test "vm.change vcsim" {
   212    vcsim_env
   213  
   214    run govc vm.change -vm DC0_H0_VM0 -latency fail
   215    assert_failure
   216  
   217    run govc object.collect -s vm/DC0_H0_VM0 config.latencySensitivity.level
   218    assert_success normal
   219  
   220    run govc vm.change -vm DC0_H0_VM0 -latency high
   221    assert_success
   222  
   223    run govc object.collect -s vm/DC0_H0_VM0 config.latencySensitivity.level
   224    assert_success high
   225  }
   226  
   227  @test "vm.power" {
   228    vcsim_env -autostart=false
   229  
   230    vm=DC0_H0_VM0
   231  
   232    run vm_power_state $vm
   233    assert_success "poweredOff"
   234  
   235    run govc vm.power $vm
   236    assert_failure
   237  
   238    run govc vm.power -on -off $vm
   239    assert_failure
   240  
   241    # off -> on
   242    run govc vm.power -on $vm
   243    assert_success
   244    run vm_power_state $vm
   245    assert_success "poweredOn"
   246    run govc vm.power -on $vm
   247    assert_failure # already powered on
   248  
   249    # on -> shutdown
   250    run govc vm.power -s $vm
   251    assert_success
   252    run vm_power_state $vm
   253    assert_success "poweredOff"
   254    run govc vm.power -off $vm
   255    assert_failure # already powered off
   256    run govc vm.power -on $vm
   257    assert_success
   258  
   259    # on -> suspended
   260    run govc vm.power -suspend $vm
   261    assert_success
   262    run vm_power_state $vm
   263    assert_success "suspended"
   264    run govc vm.power -suspend $vm
   265    assert_failure # already suspended
   266  
   267    # suspended -> on
   268    run govc vm.power -on $vm
   269    assert_success
   270    run vm_power_state $vm
   271    assert_success "poweredOn"
   272  
   273    # on -> standby
   274    run govc vm.power -standby $vm
   275    assert_success
   276    run vm_power_state $vm
   277    assert_success "suspended"
   278    run govc vm.power -standby $vm
   279    assert_failure # already suspended
   280  }
   281  
   282  @test "vm.power -on -M" {
   283    for esx in true false ; do
   284      vcsim_env -esx=$esx -autostart=false
   285  
   286      vms=($(govc find / -type m | sort))
   287  
   288      # All VMs are off with -autostart=false
   289      off=($(govc find / -type m -runtime.powerState poweredOff | sort))
   290      assert_equal "${vms[*]}" "${off[*]}"
   291  
   292      # Power on 1 VM to test that -M is idempotent
   293      run govc vm.power -on "${vms[0]}"
   294      assert_success
   295  
   296      run govc vm.power -on -M "${vms[@]}"
   297      assert_success
   298  
   299      # All VMs should be powered on now
   300      on=($(govc find / -type m -runtime.powerState poweredOn | sort))
   301      assert_equal "${vms[*]}" "${on[*]}"
   302  
   303      vcsim_stop
   304    done
   305  }
   306  
   307  @test "vm.power -force" {
   308    vcsim_env
   309  
   310    vm=$(new_id)
   311    govc vm.create $vm
   312  
   313    run govc vm.power -r $vm
   314    assert_failure
   315  
   316    run govc vm.power -r -force $vm
   317    assert_success
   318  
   319    run govc vm.power -s $vm
   320    assert_success
   321  
   322    run govc vm.power -off $vm
   323    assert_failure
   324  
   325    run govc vm.power -off -force $vm
   326    assert_success
   327  
   328    run govc vm.destroy $vm
   329    assert_success
   330  
   331    run govc vm.power -off $vm
   332    assert_failure
   333  
   334    run govc vm.power -off -force $vm
   335    assert_failure
   336  }
   337  
   338  @test "vm.destroy" {
   339    vcsim_env
   340  
   341    vm=$(new_id)
   342    govc vm.create $vm
   343  
   344    # destroy powers off vm before destruction
   345    run govc vm.destroy $vm
   346    assert_success
   347  
   348    run govc vm.destroy '*'
   349    assert_success
   350  
   351    run govc find / -type m
   352    assert_success "" # expect all VMs are gone
   353  }
   354  
   355  @test "vm.create pvscsi" {
   356    vcsim_env
   357  
   358    vm=$(new_id)
   359    govc vm.create -on=false -disk.controller pvscsi $vm
   360  
   361    result=$(govc device.ls -vm $vm | grep pvscsi- | wc -l)
   362    [ $result -eq 1 ]
   363  
   364    result=$(govc device.ls -vm $vm | grep lsilogic- | wc -l)
   365    [ $result -eq 0 ]
   366  
   367    vm=$(new_id)
   368    govc vm.create -on=false -disk.controller pvscsi -disk=1GB $vm
   369  }
   370  
   371  @test "vm.create in cluster" {
   372    vcsim_env
   373  
   374    # using GOVC_HOST and its resource pool
   375    run govc vm.create -on=false $(new_id)
   376    assert_success
   377  
   378    # using no -host and the default resource pool for DC0
   379    unset GOVC_HOST
   380    run govc vm.create -on=false $(new_id)
   381    assert_success
   382  }
   383  
   384  @test "vm.create -datastore-cluster" {
   385    vcsim_env -pod 1 -ds 3
   386  
   387    pod=/DC0/datastore/DC0_POD0
   388    id=$(new_id)
   389  
   390    run govc vm.create -disk 10M -datastore-cluster $pod "$id"
   391    assert_failure
   392  
   393    run govc object.mv /DC0/datastore/LocalDS_{1,2} $pod
   394    assert_success
   395  
   396    run govc vm.create -disk 10M -datastore-cluster $pod "$id"
   397    assert_success
   398  }
   399  
   400  @test "vm.info" {
   401    vcsim_env -esx
   402  
   403    local num=3
   404  
   405    local prefix=$(new_id)
   406  
   407    for x in $(seq $num)
   408    do
   409      local id="${prefix}-${x}"
   410  
   411      # If VM is not found: No output, exit code==0
   412      run govc vm.info $id
   413      assert_success
   414      [ ${#lines[@]} -eq 0 ]
   415  
   416      # If VM is not found (using -json flag): Valid json output, exit code==0
   417      run env GOVC_INDENT=false govc vm.info -json $id
   418      assert_success
   419      assert_line "{\"virtualMachines\":null}"
   420  
   421      run govc vm.info -dump $id
   422      assert_success
   423  
   424      run govc vm.create -on=false $id
   425      assert_success
   426  
   427      local info=$(govc vm.info -r $id)
   428      local found=$(grep Name: <<<"$info" | wc -l)
   429      [ "$found" -eq 1 ]
   430  
   431      # test that mo names are printed
   432      found=$(grep Host: <<<"$info" | awk '{print $2}')
   433      [ -n "$found" ]
   434      found=$(grep Storage: <<<"$info" | awk '{print $2}')
   435      [ -n "$found" ]
   436      found=$(grep Network: <<<"$info" | awk '{print $2}')
   437      [ -n "$found" ]
   438    done
   439  
   440    # test find slice
   441    local slice=$(govc vm.info ${prefix}-*)
   442    local found=$(grep Name: <<<"$slice" | wc -l)
   443    [ "$found" -eq $num ]
   444  
   445    # test -r
   446    found=$(grep Storage: <<<"$slice" | wc -l)
   447    [ "$found" -eq 0 ]
   448    found=$(grep Network: <<<"$slice" | wc -l)
   449    [ "$found" -eq 0 ]
   450    slice=$(govc vm.info -r ${prefix}-*)
   451    found=$(grep Storage: <<<"$slice" | wc -l)
   452    [ "$found" -eq $num ]
   453    found=$(grep Network: <<<"$slice" | wc -l)
   454    [ "$found" -eq $num ]
   455  
   456    # test extraConfig
   457    run govc vm.change -e "guestinfo.a=2" -vm $id
   458    assert_success
   459    run govc vm.info -e $id
   460    assert_success
   461    assert_line "guestinfo.a: 2"
   462    run govc vm.change -e "guestinfo.a=" -vm $id
   463    assert_success
   464    refute_line "guestinfo.a: 2"
   465  
   466    # test extraConfigFile
   467    run govc vm.change -f "guestinfo.b=this_is_not_an_existing.file" -vm $id
   468    assert_failure
   469    echo -n "3" > "$BATS_TMPDIR/extraConfigFile.conf"
   470    run govc vm.change -f "guestinfo.b=$BATS_TMPDIR/extraConfigFile.conf" -vm $id
   471    assert_success
   472    run govc vm.info -e $id
   473    assert_success
   474    assert_line "guestinfo.b: 3"
   475    run govc vm.change -f "guestinfo.b=" -vm $id
   476    assert_success
   477    refute_line "guestinfo.b: 3"
   478  
   479    # test optional bool Config
   480    run govc vm.change -nested-hv-enabled=true -vm "$id"
   481    assert_success
   482  
   483    hv=$(govc vm.info -json "$id" | jq '.[][0].config.nestedHVEnabled')
   484    assert_equal "$hv" "true"
   485  }
   486  
   487  @test "vm.info multi dc" {
   488    vcsim_start -dc 2
   489  
   490    run govc vm.info /DC1/vm/DC1_H0_VM1
   491    assert_success
   492  
   493    run govc vm.info DC1_H0_VM1
   494    assert_failure
   495  
   496    run govc vm.info -vm.ipath /DC1/vm/DC1_H0_VM1
   497    assert_success
   498    uuid=$(grep "UUID:" <<<"$output" | awk '{print $2}')
   499  
   500    run govc vm.info -vm.ipath DC1_H0_VM1
   501    assert_failure
   502  
   503    run govc vm.info -vm.uuid enoent
   504    assert_failure
   505  
   506    run govc vm.info -vm.uuid "$uuid"
   507    assert_failure
   508  
   509    run govc vm.info -dc DC1 -vm.uuid "$uuid"
   510    assert_success
   511  }
   512  
   513  @test "vm.create linked ide disk" {
   514    esx_env
   515  
   516    import_ttylinux_vmdk
   517  
   518    vm=$(new_id)
   519  
   520    run govc vm.create -disk $GOVC_TEST_VMDK -disk.controller ide -on=false $vm
   521    assert_success
   522  
   523    run govc device.info -vm $vm disk-200-0
   524    assert_success
   525    assert_line "Controller: ide-200"
   526  }
   527  
   528  @test "vm.create linked scsi disk" {
   529    esx_env
   530  
   531    import_ttylinux_vmdk
   532  
   533    vm=$(new_id)
   534  
   535    run govc vm.create -disk enoent -on=false $vm
   536    assert_failure "govc: cannot stat '[${GOVC_DATASTORE##*/}] enoent': No such file"
   537  
   538    run govc vm.create -disk $GOVC_TEST_VMDK -on=false $vm
   539    assert_success
   540  
   541    run govc device.info -vm $vm disk-1000-0
   542    assert_success
   543    assert_line "Controller: lsilogic-1000"
   544    assert_line "Parent: [${GOVC_DATASTORE##*/}] $GOVC_TEST_VMDK"
   545    assert_line "File: [${GOVC_DATASTORE##*/}] $vm/${vm}.vmdk"
   546  }
   547  
   548  @test "vm.create scsi disk" {
   549    esx_env
   550  
   551    import_ttylinux_vmdk
   552  
   553    vm=$(new_id)
   554  
   555    run govc vm.create -disk enoent -on=false $vm
   556    assert_failure "govc: cannot stat '[${GOVC_DATASTORE##*/}] enoent': No such file"
   557  
   558    run govc vm.create -disk $GOVC_TEST_VMDK -on=false -link=false $vm
   559    assert_success
   560  
   561    run govc device.info -vm $vm disk-1000-0
   562    assert_success
   563    assert_line "Controller: lsilogic-1000"
   564    refute_line "Parent: [${GOVC_DATASTORE##*/}] $GOVC_TEST_VMDK"
   565    assert_line "File: [${GOVC_DATASTORE##*/}] $GOVC_TEST_VMDK"
   566  }
   567  
   568  @test "vm.create scsi disk with datastore argument" {
   569    esx_env
   570  
   571    import_ttylinux_vmdk
   572  
   573    vm=$(new_id)
   574  
   575    run govc vm.create -disk="${GOVC_TEST_VMDK}" -disk-datastore="${GOVC_DATASTORE}" -on=false -link=false $vm
   576    assert_success
   577  
   578    run govc device.info -vm $vm disk-1000-0
   579    assert_success
   580    assert_line "File: [${GOVC_DATASTORE##*/}] $GOVC_TEST_VMDK"
   581  }
   582  
   583  @test "vm.create iso" {
   584    vcsim_env -esx
   585  
   586    upload_iso
   587  
   588    vm=$(new_id)
   589  
   590    run govc vm.create -iso enoent -on=false $vm
   591    assert_failure "govc: cannot stat '[${GOVC_DATASTORE##*/}] enoent': No such file"
   592  
   593    run govc vm.create -iso $GOVC_TEST_ISO -on=false $vm
   594    assert_success
   595  
   596    run govc device.info -vm $vm cdrom-*
   597    assert_success
   598    assert_line "Type: VirtualCdrom"
   599    assert_line "Summary: ISO [${GOVC_DATASTORE##*/}] $GOVC_TEST_ISO"
   600  }
   601  
   602  @test "vm.create iso with datastore argument" {
   603    vcsim_env
   604  
   605    upload_iso
   606  
   607    vm=$(new_id)
   608  
   609    run govc vm.create -iso="${GOVC_TEST_ISO}" -iso-datastore="${GOVC_DATASTORE}" -on=false $vm
   610    assert_success
   611  
   612    run govc device.info -vm $vm cdrom-*
   613    assert_success
   614    assert_line "Summary: ISO [${GOVC_DATASTORE##*/}] $GOVC_TEST_ISO"
   615  }
   616  
   617  @test "vm.disk.create empty vm" {
   618    vcsim_env
   619  
   620    vm=$(new_empty_vm)
   621  
   622    local name=$(new_id)
   623  
   624    run govc vm.disk.create -vm "$vm" -name "$name" -size 1G
   625    assert_success
   626    result=$(govc device.ls -vm "$vm" | grep -c disk-)
   627    [ "$result" -eq 1 ]
   628    govc device.info -json -vm "$vm" disk-* | jq .devices[].backing.sharing | grep -v sharingMultiWriter
   629  
   630    name=$(new_id)
   631  
   632    run govc vm.disk.create -vm "$vm" -name "$vm/$name" -size 2G
   633    assert_success
   634  
   635    result=$(govc device.ls -vm "$vm" | grep -c disk-)
   636    [ "$result" -eq 2 ]
   637  }
   638  
   639  @test "vm.disk.share" {
   640    esx_env
   641  
   642    vm=$(new_empty_vm)
   643  
   644    run govc vm.disk.create -vm "$vm" -name "$vm/shared.vmdk" -size 1G -eager -thick -sharing sharingMultiWriter
   645    assert_success
   646    govc device.info -json -vm "$vm" disk-* | jq .devices[].backing.sharing | grep sharingMultiWriter
   647  
   648    run govc vm.power -on "$vm"
   649    assert_success
   650  
   651    vm2=$(new_empty_vm)
   652  
   653    run govc vm.disk.attach -vm "$vm2" -link=false -disk "$vm/shared.vmdk"
   654    assert_success
   655  
   656    run govc vm.power -on "$vm2"
   657    assert_failure # requires sharingMultiWriter
   658  
   659    run govc device.remove -vm "$vm2" -keep disk-1000-0
   660    assert_success
   661  
   662    run govc vm.disk.attach -vm "$vm2" -link=false -sharing sharingMultiWriter -disk "$vm/shared.vmdk"
   663    assert_success
   664  
   665    run govc vm.power -on "$vm2"
   666    assert_success
   667  
   668    run govc vm.power -off "$vm"
   669    assert_success
   670  
   671    run govc vm.disk.change -vm "$vm" -disk.filePath "[$GOVC_DATASTORE] $vm/shared.vmdk" -sharing sharingNone
   672    assert_success
   673  
   674    ! govc device.info -json -vm "$vm" disk-* | jq .devices[].backing.sharing | grep sharingMultiWriter
   675  }
   676  
   677  @test "vm.disk.create" {
   678    vcsim_env
   679  
   680    vm=$(new_id)
   681  
   682    govc vm.create -on=false "$vm"
   683    assert_success
   684  
   685    name=$(new_id)
   686  
   687    run govc vm.disk.create -vm "$vm" -name "$vm/$name" -size 1M
   688    assert_success
   689    disk=$(govc device.ls -vm "$vm" disk-* | awk '{print $1}')
   690    result=$(grep -c disk- <<<"$disk")
   691    [ "$result" -eq 1 ]
   692  
   693    run govc vm.disk.change -vm "$vm" -disk.name "$disk" -size 2M
   694    assert_success
   695  
   696    run govc vm.disk.change -vm "$vm" -disk.name "$disk" -size 1M
   697    assert_failure # cannot shrink disk
   698  }
   699  
   700  @test "vm.disk.attach" {
   701    esx_env
   702  
   703    import_ttylinux_vmdk
   704  
   705    vm=$(new_id)
   706  
   707    govc vm.create -disk $GOVC_TEST_VMDK -on=false $vm
   708    result=$(govc device.ls -vm $vm | grep disk- | wc -l)
   709    [ $result -eq 1 ]
   710  
   711    id=$(new_id)
   712    run govc import.vmdk $GOVC_TEST_VMDK_SRC $id
   713    assert_success
   714  
   715    run govc vm.disk.attach -vm $vm -link=false -disk enoent.vmdk
   716    assert_failure "govc: File [${GOVC_DATASTORE##*/}] enoent.vmdk was not found"
   717  
   718    run govc vm.disk.attach -vm $vm -disk enoent.vmdk
   719    assert_failure "govc: Invalid configuration for device '0'."
   720  
   721    run govc vm.disk.attach -vm $vm -disk $id/$(basename $GOVC_TEST_VMDK) -controller lsilogic-1000
   722    assert_success
   723    result=$(govc device.ls -vm $vm | grep disk- | wc -l)
   724    [ $result -eq 2 ]
   725  }
   726  
   727  @test "vm.create new disk with datastore argument" {
   728    vcsim_env
   729  
   730    vm=$(new_id)
   731  
   732    run govc vm.create -disk="1GiB" -ds="${GOVC_DATASTORE}" -on=false -link=false $vm
   733    assert_success
   734  
   735    run govc device.info -vm $vm disk-*
   736    assert_success
   737    assert_line "File: [${GOVC_DATASTORE##*/}] ${vm}/${vm}.vmdk"
   738  }
   739  
   740  @test "vm.create new disk with datastore cluster argument" {
   741    vcsim_env -pod 1 -ds 3
   742  
   743    vm=$(new_id)
   744  
   745    run govc object.mv /DC0/datastore/LocalDS_{1,2} /DC0/datastore/DC0_POD0
   746    assert_success
   747  
   748    run govc vm.create -disk="1GiB" -datastore-cluster=/DC0/datastore/DC0_POD0 -on=false -link=false "$vm"
   749    assert_success
   750  
   751    run govc device.info -vm $vm disk-*
   752    assert_success
   753  }
   754  
   755  @test "vm.register" {
   756    vcsim_env
   757  
   758    run govc vm.unregister enoent
   759    assert_failure
   760  
   761    vm=$(new_empty_vm)
   762  
   763    run govc vm.unregister "$vm"
   764    assert_success
   765  
   766    run govc vm.register "$vm/${vm}.vmx"
   767    assert_success
   768  }
   769  
   770  @test "vm.register vcsim" {
   771    vcsim_env -autostart=false
   772  
   773    host=$GOVC_HOST
   774    pool=$GOVC_RESOURCE_POOL
   775  
   776    unset GOVC_HOST GOVC_RESOURCE_POOL
   777  
   778    vm=DC0_H0_VM0
   779  
   780    run govc vm.unregister $vm
   781    assert_success
   782  
   783    run govc vm.register "$vm/${vm}.vmx"
   784    assert_failure # -pool is required
   785  
   786    run govc vm.register -pool "$pool" "$vm/${vm}.vmx"
   787    assert_success
   788  
   789    run govc vm.unregister $vm
   790    assert_success
   791  
   792    run govc vm.register -template -pool "$pool" "$vm/${vm}.vmx"
   793    assert_failure # -pool is not allowed w/ template
   794  
   795    run govc vm.register -template -host "$host" "$vm/${vm}.vmx"
   796    assert_success
   797  }
   798  
   799  @test "vm.clone" {
   800    vcsim_env
   801  
   802    vm="DC0_H0_VM0"
   803    clone=$(new_id)
   804  
   805    run govc vm.clone -vm "$vm" -host.ipath /DC0/host/DC0_C0/DC0_C0_H0 -annotation $$ "$clone"
   806    assert_success
   807  
   808    backing=$(govc device.info -json -vm "$clone" disk-* | jq .devices[].backing)
   809    assert_equal false "$(jq .eagerlyScrub <<<"$backing")"
   810    assert_equal true "$(jq .thinProvisioned <<<"$backing")"
   811  
   812    run govc object.collect -s "/$GOVC_DATACENTER/vm/$clone" config.annotation
   813    assert_success $$
   814  
   815    clone=$(new_id)
   816    run govc vm.clone -vm "$vm" -snapshot X "$clone"
   817    assert_failure
   818  
   819    run govc snapshot.create -vm "$vm" X
   820    assert_success
   821  
   822    run govc vm.clone -vm "$vm" -snapshot X "$clone"
   823    assert_success
   824  
   825    clone=$(new_id)
   826    run govc vm.clone -cluster enoent -vm "$vm" "$clone"
   827    assert_failure
   828  
   829    run govc vm.clone -cluster DC0_C0 -vm "$vm" "$clone"
   830    assert_success
   831  
   832    run govc vm.clone -cluster DC0_C0 -vm "$vm" "$clone"
   833    assert_failure # already exists
   834  
   835    run govc datastore.cp "$clone"/"$clone".vmx "$clone"/"$clone".vmx.copy
   836    run govc vm.destroy "$clone"
   837    run govc datastore.mv "$clone"/"$clone".vmx.copy "$clone"/"$clone".vmx # leave vmx file
   838    run govc vm.clone -force -vm "$vm" "$clone"
   839    assert_success # clone vm with the same name vmx file
   840  
   841    vm=$(new_empty_vm)
   842    run govc vm.disk.create -vm "$vm" -thick -eager -size 10M -name "$vm/data.vmdk"
   843    assert_success
   844  
   845    backing=$(govc device.info -json -vm "$vm" disk-* | jq .devices[].backing)
   846    assert_equal true "$(jq .eagerlyScrub <<<"$backing")"
   847    assert_equal false "$(jq .thinProvisioned <<<"$backing")"
   848  
   849    clone=$(new_id)
   850    run govc vm.clone -vm "$vm" "$clone"
   851    assert_success
   852  
   853    backing=$(govc device.info -json -vm "$clone" disk-* | jq .devices[].backing)
   854    assert_equal true "$(jq .eagerlyScrub <<<"$backing")"
   855    assert_equal false "$(jq .thinProvisioned <<<"$backing")"
   856  
   857    # test that each vm has a unique vmdk path
   858    for item in fileName uuid;  do
   859      items=$(govc object.collect -json -type m / config.hardware.device | \
   860                jq ".changeSet[].val._value[].backing.$item | select(. != null)")
   861  
   862      nitems=$(wc -l <<<"$items")
   863      uitems=$(sort -u <<<"$items" | wc -l)
   864      assert_equal "$nitems" "$uitems"
   865    done
   866  }
   867  
   868  @test "vm.clone change resources" {
   869    vcsim_env
   870  
   871    vm=$(new_empty_vm)
   872    clone=$(new_id)
   873  
   874    run govc vm.info -r "$vm"
   875    assert_success
   876    assert_line "Network: $(basename "$GOVC_NETWORK")" # DVPG0
   877  
   878    run govc vm.clone -m 1024 -c 2 -net "VM Network" -vm "$vm" "$clone"
   879    assert_success
   880  
   881    run govc vm.info -r "$clone"
   882    assert_success
   883    assert_line "Memory: 1024MB"
   884    assert_line "CPU: 2 vCPU(s)"
   885    assert_line "Network: VM Network"
   886  
   887    # Remove all NICs from source vm
   888    run govc device.remove -vm "$vm" "$(govc device.ls -vm "$vm" | grep ethernet- | awk '{print $1}')"
   889    assert_success
   890  
   891    clone=$(new_id)
   892  
   893    mac=00:00:0f:a7:a0:f1
   894    run govc vm.clone -net "VM Network" -net.address $mac -vm "$vm" "$clone"
   895    assert_success
   896  
   897    run govc vm.info -r "$clone"
   898    assert_success
   899    assert_line "Network: VM Network"
   900  
   901    run govc device.info -vm "$clone"
   902    assert_success
   903    assert_line "MAC Address: $mac"
   904  }
   905  
   906  @test "vm.clone usage" {
   907    # validate we require -vm flag
   908    run govc vm.clone enoent
   909    assert_failure
   910  }
   911  
   912  @test "vm.migrate" {
   913    vcsim_env -cluster 2
   914  
   915    host0=/DC0/host/DC0_C0/DC0_C0_H0
   916    host1=/DC0/host/DC0_C0/DC0_C0_H1
   917    moid0=$(govc find -maxdepth 0 -i $host0)
   918    moid1=$(govc find -maxdepth 0 -i $host1)
   919  
   920    vm=$(new_id)
   921    run govc vm.create -on=false -host $host0 "$vm"
   922    assert_success
   923  
   924    # assert VM is on H0
   925    run govc object.collect "vm/$vm" -runtime.host "$moid0"
   926    assert_success
   927  
   928    # WaitForUpdates until the VM runtime.host changes to H1
   929    govc object.collect "vm/$vm" -runtime.host "$moid1" &
   930    pid=$!
   931  
   932    # migrate from H0 to H1
   933    run govc vm.migrate -host $host1 "$vm"
   934    assert_success
   935  
   936    run govc events -type VmMigratedEvent "vm/$vm"
   937    assert_success
   938    assert_matches "Migration of virtual machine"
   939  
   940    wait $pid
   941  
   942    # (re-)assert VM is now on H1
   943    run govc object.collect "vm/$vm" -runtime.host "$moid1"
   944    assert_success
   945  
   946    # migrate from C0 to C1
   947    run govc vm.migrate -pool DC0_C1/Resources "$vm"
   948    assert_success
   949  
   950    run govc folder.create vm/new-folder
   951    assert_success
   952  
   953    run govc object.collect -s "vm/$vm" parent
   954    assert_success
   955  
   956    uuid=$(govc object.collect -s "vm/$vm" config.uuid)
   957    run govc vm.migrate -folder vm/new-folder -vm.uuid "$uuid"
   958    assert_success
   959  
   960    run govc object.collect -s "vm/new-folder/$vm" parent
   961    assert_success
   962  }
   963  
   964  @test "object name with slash" {
   965    vcsim_env
   966  
   967    vm=DC0_H0_VM0
   968  
   969    name="$vm/with-slash"
   970  
   971    # rename VM to include a '/'
   972    run govc vm.change -vm "$vm" -name "$name"
   973    assert_success
   974  
   975    path=$(govc ls "vm/$name")
   976  
   977    run govc vm.info "$name"
   978    assert_success
   979    assert_line "Name: $name"
   980    assert_line "Path: $path"
   981  
   982    run govc vm.info "$path"
   983    assert_success
   984    assert_line "Name: $name"
   985    assert_line "Path: $path"
   986  
   987    run govc find vm -name "$name"
   988    assert_success "vm/$name"
   989  
   990    # create a portgroup where name includes a '/'
   991    net=$(new_id)/with-slash
   992  
   993    run govc host.portgroup.add -vswitch vSwitch0 "$net"
   994    assert_success
   995  
   996    run govc vm.network.change -vm "$name" -net "$net" ethernet-0
   997    assert_success
   998  
   999    # change VM eth0 to use network that includes a '/' in the name
  1000    run govc device.info -vm "$name" ethernet-0
  1001    assert_success
  1002    assert_line "Summary: $net"
  1003  
  1004    run govc host.portgroup.remove "$net"
  1005    assert_success
  1006  }
  1007  
  1008  @test "vm.console" {
  1009    esx_env
  1010  
  1011    vm=$(new_empty_vm)
  1012  
  1013    run govc vm.console "$vm"
  1014    assert_success
  1015  
  1016    run govc vm.console -wss "$vm"
  1017    assert_failure
  1018  
  1019    run govc vm.power -on "$vm"
  1020    assert_success
  1021  
  1022    run govc vm.console "$vm"
  1023    assert_success
  1024  
  1025    run govc vm.console -wss "$vm"
  1026    assert_success
  1027  
  1028    run govc vm.console -capture - "$vm"
  1029    assert_success
  1030  }
  1031  
  1032  @test "vm.upgrade" {
  1033    vcsim_env
  1034  
  1035    vm=$(new_id)
  1036  
  1037    run govc vm.create -on=false -version 0.5 "$vm"
  1038    assert_failure
  1039  
  1040    run govc vm.create -on=false -version 5.5 "$vm"
  1041    assert_success
  1042  
  1043    run govc object.collect -s "vm/$vm" config.version
  1044    assert_success "vmx-10"
  1045  
  1046    run govc vm.upgrade -vm "$vm"
  1047    assert_success
  1048  
  1049    version=$(govc object.collect -s "vm/$vm" config.version)
  1050    [[ "$version" > "vmx-10" ]]
  1051  
  1052    run govc vm.upgrade -vm "$vm"
  1053    assert_failure
  1054  
  1055    run govc vm.create -on=false -version vmx-11 "$(new_id)"
  1056    assert_success
  1057  }
  1058  
  1059  @test "vm.markastemplate" {
  1060    vcsim_env
  1061  
  1062    id=$(new_id)
  1063  
  1064    run govc vm.create -on=true "$id"
  1065    assert_success
  1066  
  1067    run govc vm.change -vm "$id" -e testing=123
  1068    assert_success
  1069  
  1070    run govc vm.markastemplate "$id"
  1071    assert_failure
  1072  
  1073    run govc vm.power -off "$id"
  1074    assert_success
  1075  
  1076    run govc vm.markasvm "$id"
  1077    assert_failure # already a vm
  1078  
  1079    run govc vm.markastemplate "$id"
  1080    assert_success
  1081  
  1082    run govc vm.markastemplate "$id"
  1083    assert_failure # already a template
  1084  
  1085    run govc vm.change -vm "$id" -e testing=456
  1086    assert_failure # template reconfigure only allows name and annotation change
  1087  
  1088    run govc vm.change -vm "$id" -annotation testing123
  1089    assert_success
  1090  
  1091    run govc vm.power -on "$id"
  1092    assert_failure
  1093  
  1094    run govc vm.clone -vm "$id" -on=false new-vm
  1095    assert_success
  1096  
  1097    run govc vm.markasvm "$id"
  1098    assert_success
  1099  }
  1100  
  1101  @test "vm.option.info" {
  1102    vcsim_env
  1103  
  1104    run govc vm.option.info -host "$GOVC_HOST"
  1105    assert_success
  1106  
  1107    run govc vm.option.info -cluster "$(dirname "$GOVC_HOST")"
  1108    assert_success
  1109  
  1110    run govc vm.option.info -vm DC0_H0_VM0
  1111    assert_success
  1112  
  1113    family=$(govc vm.option.info -json ubuntu64Guest | jq -r .guestOSDescriptor[].family)
  1114    assert_equal linuxGuest "$family"
  1115  
  1116    family=$(govc vm.option.info -json windows8_64Guest | jq -r .guestOSDescriptor[].family)
  1117    assert_equal windowsGuest "$family"
  1118  
  1119    run govc vm.option.info enoent
  1120    assert_success  # returns the entire simulator.GuestID list
  1121    [ ${#lines[@]} -ge 100 ]
  1122  }
  1123  
  1124  @test "vm.target.info" {
  1125    vcsim_env
  1126  
  1127    run govc vm.target.info -host "$GOVC_HOST"
  1128    assert_success
  1129  
  1130    run govc vm.target.info -cluster "$(dirname "$GOVC_HOST")"
  1131    assert_success
  1132  
  1133    run govc vm.target.info -vm DC0_H0_VM0
  1134    assert_success
  1135  
  1136    run govc vm.target.info -json
  1137    assert_success
  1138  }
  1139  
  1140  @test "vm.customize" {
  1141    vcsim_env
  1142  
  1143    run govc vm.customize -vm DC0_H0_VM0 -ip 10.0.0.42 -netmask 255.255.0.0 vcsim-linux-static
  1144    assert_failure # power must be off
  1145  
  1146    run govc vm.power -off DC0_H0_VM0
  1147    assert_success
  1148  
  1149    run govc vm.customize -vm DC0_H0_VM0 -ip 10.0.0.42 -netmask 255.255.0.0 vcsim-linux-static
  1150    assert_success
  1151  
  1152    run govc vm.customize -vm DC0_H0_VM0 -ip 10.0.0.42 -netmask 255.255.0.0 vcsim-linux-static
  1153    assert_failure # pending customization
  1154  
  1155    run govc vm.power -on DC0_H0_VM0
  1156    assert_success
  1157  
  1158    run govc object.collect -s vm/DC0_H0_VM0 guest.ipAddress
  1159    assert_success 10.0.0.42
  1160  
  1161    run govc object.collect -s vm/DC0_H0_VM0 guest.hostName
  1162    assert_success vcsim-1
  1163  
  1164    run govc vm.power -off DC0_H0_VM0
  1165    assert_success
  1166  
  1167    run govc vm.customize -vm DC0_H0_VM0 -ip 10.0.0.43 -netmask 255.255.0.0 -domain HOME -tz D -name windoze vcsim-windows-static
  1168    assert_success
  1169  
  1170    run govc vm.power -on DC0_H0_VM0
  1171    assert_success
  1172  
  1173    run govc object.collect -s vm/DC0_H0_VM0 guest.ipAddress
  1174    assert_success 10.0.0.43
  1175  
  1176    run govc object.collect -s vm/DC0_H0_VM0 summary.guest.ipAddress
  1177    assert_success 10.0.0.43
  1178  
  1179    run govc object.collect -s vm/DC0_H0_VM0 summary.guest.hostName
  1180    assert_success windoze
  1181  
  1182    run govc vm.info DC0_H0_VM0
  1183    assert_success
  1184    assert_matches 10.0.0.43
  1185  
  1186    run govc object.collect -s vm/DC0_H0_VM0 guest.hostName
  1187    assert_success windoze
  1188  
  1189    run govc vm.power -off DC0_H0_VM0
  1190    assert_success
  1191  
  1192    run govc vm.customize -vm DC0_H0_VM0 -ip 10.0.0.44 -netmask 255.255.0.0 -type Windows
  1193    assert_success
  1194  
  1195    run govc vm.power -on DC0_H0_VM0
  1196    assert_success
  1197  
  1198    run govc object.collect -s vm/DC0_H0_VM0 guest.ipAddress
  1199    assert_success 10.0.0.44
  1200  
  1201    run govc vm.power -off DC0_H0_VM0
  1202    assert_success
  1203  
  1204    run govc vm.customize -vm DC0_H0_VM0 -type Linux
  1205    assert_failure # no -ip specified
  1206  
  1207    run govc vm.customize -vm DC0_H0_VM0 -ip 10.0.0.45 -netmask 255.255.0.0 -type Linux -dns-server 1.1.1.1 -dns-suffix example.com
  1208    assert_success
  1209  
  1210    run govc vm.power -on DC0_H0_VM0
  1211    assert_success
  1212  
  1213    run govc object.collect -s vm/DC0_H0_VM0 guest.ipAddress
  1214    assert_success 10.0.0.45
  1215  
  1216    host=$(govc ls -L "$(govc object.collect -s vm/DC0_H0_VM0 runtime.host)")
  1217    run govc host.maintenance.enter "$host"
  1218    assert_success
  1219  
  1220    run govc vm.power -off DC0_H0_VM0
  1221    assert_success
  1222  
  1223    run govc vm.power -on DC0_H0_VM0
  1224    assert_failure # InvalidState
  1225  
  1226    run govc vm.customize -vm DC0_H0_VM0 -ip 10.0.0.45 -netmask 255.255.0.0 -type Linux
  1227    assert_failure # InvalidState
  1228  
  1229    run govc host.maintenance.exit "$host"
  1230    assert_success
  1231  
  1232    run govc vm.customize -vm DC0_H0_VM0 -ip 10.0.0.45 -netmask 255.255.0.0 -type Linux
  1233    assert_success
  1234  }