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