github.com/vmware/govmomi@v0.37.1/govc/test/datastore.bats (about) 1 #!/usr/bin/env bats 2 3 load test_helper 4 5 upload_file() { 6 name=$(new_id) 7 8 echo "Hello world" | govc datastore.upload - "$name" 9 assert_success 10 11 echo "$name" 12 } 13 14 @test "datastore.ls" { 15 vcsim_env -esx 16 17 name=$(upload_file) 18 19 # Single argument 20 run govc datastore.ls "${name}" 21 assert_success 22 [ ${#lines[@]} -eq 1 ] 23 24 # Multiple arguments 25 run govc datastore.ls "${name}" "${name}" 26 assert_success 27 [ ${#lines[@]} -eq 2 ] 28 29 # Pattern argument 30 run govc datastore.ls "./govc-test-*" 31 assert_success 32 [ ${#lines[@]} -ge 1 ] 33 34 # Long listing 35 run govc datastore.ls -l "./govc-test-*" 36 assert_success 37 assert_equal "12B" $(awk '{ print $1 }' <<<${output}) 38 } 39 40 @test "datastore.ls-R" { 41 vcsim_env -esx 42 43 dir=$(new_id) 44 45 run govc datastore.mkdir "$dir" 46 assert_success 47 48 for name in one two three ; do 49 echo "$name world" | govc datastore.upload - "$dir/file-$name" 50 run govc datastore.mkdir -p "$dir/dir-$name/subdir-$name" 51 run govc datastore.mkdir -p "$dir/dir-$name/.hidden" 52 assert_success 53 echo "$name world" | govc datastore.upload - "$dir/dir-$name/.hidden/other-$name" 54 echo "$name world" | govc datastore.upload - "$dir/dir-$name/other-$name" 55 echo "$name world" | govc datastore.upload - "$dir/dir-$name/subdir-$name/last-$name" 56 done 57 58 # without -R 59 json=$(govc datastore.ls -json -l -p "$dir") 60 result=$(jq -r .[].file[].path <<<"$json" | wc -l) 61 [ "$result" -eq 6 ] 62 63 result=$(jq -r .[].folderPath <<<"$json" | wc -l) 64 [ "$result" -eq 1 ] 65 66 # with -R 67 json=$(govc datastore.ls -json -l -p -R "$dir") 68 result=$(jq -r .[].file[].path <<<"$json" | wc -l) 69 [ "$result" -eq 15 ] 70 71 result=$(jq -r .[].folderPath <<<"$json" | wc -l) 72 [ "$result" -eq 7 ] 73 74 # with -R -a 75 json=$(govc datastore.ls -json -l -p -R -a "$dir") 76 result=$(jq -r .[].file[].path <<<"$json" | wc -l) 77 [ "$result" -eq 21 ] 78 79 result=$(jq -r .[].folderPath <<<"$json" | wc -l) 80 [ "$result" -eq 10 ] 81 } 82 83 @test "datastore.rm" { 84 vcsim_env -esx 85 86 name=$(upload_file) 87 88 # Not found is a failure 89 run govc datastore.rm "${name}.notfound" 90 assert_failure 91 92 # Not found is NOT a failure with the force flag 93 run govc datastore.rm -f "${name}.notfound" 94 assert_success 95 assert_empty "${output}" 96 97 # Verify the file is present 98 run govc datastore.ls "${name}" 99 assert_success 100 101 # Delete the file 102 run govc datastore.rm "${name}" 103 assert_success 104 assert_empty "${output}" 105 106 # Verify the file is gone 107 run govc datastore.ls "${name}" 108 assert_failure 109 } 110 111 @test "datastore.info" { 112 vcsim_env 113 114 run govc datastore.info enoent 115 assert_failure 116 117 run govc datastore.info 118 assert_success 119 [ ${#lines[@]} -gt 1 ] 120 121 run govc datastore.info -H 122 assert_failure 123 124 cluster=$(dirname "$GOVC_RESOURCE_POOL") 125 126 hosts=($(govc object.collect -s -d $'\n' "$cluster" host)) 127 run govc datastore.info -H "${hosts[@]}" 128 assert_success 129 130 run govc cluster.add -cluster "$cluster" -hostname test.example.com -username user -password pass 131 assert_success 132 133 run govc datastore.create -type local -name tmpds -path "$BATS_TMPDIR" test.example.com 134 assert_success 135 136 hosts=($(govc object.collect -s -d $'\n' "$cluster" host)) 137 run govc datastore.info -H "${hosts[@]}" 138 assert_failure # host test.example.com hast no shared datastores 139 } 140 141 142 @test "datastore.mkdir" { 143 vcsim_env -esx 144 145 name=$(new_id) 146 147 # Not supported datastore type is a failure 148 run govc datastore.mkdir -namespace "notfound" 149 assert_failure 150 assert_matches "govc: ServerFaultCode: .*" "${output}" 151 152 run govc datastore.mkdir "${name}" 153 assert_success 154 assert_empty "${output}" 155 156 # Verify the dir is present 157 run govc datastore.ls "${name}" 158 assert_success 159 160 # Delete the dir on an unsupported datastore type is a failure 161 run govc datastore.rm -namespace "${name}" 162 assert_failure 163 assert_matches "govc: ServerFaultCode: .*" "${output}" 164 165 # Delete the dir 166 run govc datastore.rm "${name}" 167 assert_success 168 assert_empty "${output}" 169 170 # Verify the dir is gone 171 run govc datastore.ls "${name}" 172 assert_failure 173 } 174 175 @test "datastore.download" { 176 vcsim_env -esx 177 178 name=$(upload_file) 179 run govc datastore.download "$name" - 180 assert_success 181 assert_output "Hello world" 182 183 run govc datastore.download "$name" "$BATS_TMPDIR/$name" 184 assert_success 185 run cat "$BATS_TMPDIR/$name" 186 assert_output "Hello world" 187 rm "$BATS_TMPDIR/$name" 188 } 189 190 @test "datastore.upload" { 191 vcsim_env -esx 192 193 name=$(new_id) 194 echo -n "Hello world" | govc datastore.upload - "$name" 195 196 run govc datastore.download "$name" - 197 assert_success 198 assert_output "Hello world" 199 200 run env GOVMOMI_DATASTORE_ACCESS_SCHEME=invalid govc datastore.upload datastore.bats datastore.bats 201 assert_failure 202 203 run env GOVMOMI_DATASTORE_ACCESS_SCHEME=https govc datastore.upload datastore.bats datastore.bats 204 assert_success 205 } 206 207 @test "datastore.tail" { 208 esx_env 209 210 run govc datastore.tail "enoent/enoent.log" 211 assert_failure 212 213 id=$(new_id) 214 govc vm.create "$id" 215 govc vm.power -off "$id" 216 217 # test with .log (> bufSize) and .vmx (< bufSize) 218 for file in "$id/vmware.log" "$id/$id.vmx" ; do 219 log=$(govc datastore.download "$file" -) 220 221 for n in 0 1 5 10 123 456 7890 ; do 222 expect=$(tail -n $n <<<"$log") 223 224 run govc datastore.tail -n $n "$file" 225 assert_output "$expect" 226 227 expect=$(tail -c $n <<<"$log") 228 229 run govc datastore.tail -c $n "$file" 230 assert_output "$expect" 231 done 232 done 233 } 234 235 @test "datastore.disk" { 236 esx_env 237 238 id=$(new_id) 239 vmdk="$id/$id.vmdk" 240 241 run govc datastore.mkdir "$id" 242 assert_success 243 244 run govc datastore.disk.create "$vmdk" 245 assert_success 246 247 run govc datastore.disk.info "$vmdk" 248 assert_success 249 250 run govc datastore.disk.info -uuid "$vmdk" 251 assert_success 252 253 run govc datastore.rm "$vmdk" 254 assert_success 255 256 run govc datastore.mkdir -p "$id" 257 assert_success 258 259 run govc datastore.disk.create "$vmdk" 260 assert_success 261 262 id=$(new_id) 263 run govc vm.create -on=false -link -disk "$vmdk" "$id" 264 assert_success 265 266 run govc datastore.disk.info -d "$vmdk" 267 assert_success 268 269 run govc datastore.disk.info -p=false "$vmdk" 270 assert_success 271 272 run govc datastore.disk.info -c "$vmdk" 273 assert_success 274 275 run govc datastore.disk.info -json "$vmdk" 276 assert_success 277 278 # should fail due to: ddb.deletable=false 279 run govc datastore.rm "$vmdk" 280 assert_failure 281 282 run govc datastore.rm -f "$vmdk" 283 assert_success 284 285 # one more time, but rm the directory w/o -f 286 run govc datastore.mkdir -p "$id" 287 assert_success 288 289 run govc datastore.disk.create "$vmdk" 290 assert_success 291 292 id=$(new_id) 293 run govc vm.create -on=false -link -disk "$vmdk" "$id" 294 assert_success 295 296 run govc datastore.rm "$(dirname "$vmdk")" 297 assert_success 298 } 299 300 @test "datastore.cp" { 301 vcsim_env -dc 2 -ds 2 302 303 id=$(new_id) 304 vmdk="$id/$id.vmdk" 305 306 # GOVC_DATACENTER and GOVC_DATACENTER are set during these tests 307 run govc datastore.mkdir "$id" 308 assert_success 309 310 run govc datastore.disk.create "$vmdk" 311 assert_success 312 313 clone="$id/$id-2.vmdk" 314 run govc datastore.cp "$vmdk" "$clone" 315 assert_success 316 317 # Specifying -dc and -ds flags in the tests below 318 unset GOVC_DATASTORE GOVC_DATACENTER 319 320 run govc datastore.ls -dc DC0 -ds LocalDS_0 "$clone" 321 assert_success # created this file above 322 323 run govc datastore.ls -dc DC0 -ds LocalDS_1 "$clone" 324 assert_failure # should not exist in DS_1 325 326 run govc datastore.ls -dc DC1 -ds LocalDS_1 "$clone" 327 assert_failure # should not exist in DC1 DS_1 328 329 run govc datastore.mkdir -dc DC1 -ds LocalDS_1 "$id" 330 assert_success 331 332 for op in cp mv ; do 333 run govc datastore.ls -dc DC1 -ds LocalDS_0 "$clone" 334 assert_failure # should not exist in DC1 DS_0 335 336 # From DC0 DS_0 to DC1 DS_1 337 run govc datastore.$op -dc DC0 -ds LocalDS_0 -dc-target DC1 -ds-target LocalDS_1 "$clone" "$clone" 338 assert_success 339 340 run govc datastore.ls -dc DC1 -ds LocalDS_1 "$clone" 341 assert_success # now the file exists 342 343 run govc datastore.rm -dc DC1 -ds LocalDS_1 "$clone" 344 assert_success 345 done 346 } 347 348 @test "datastore.disk.info" { 349 esx_env 350 351 import_ttylinux_vmdk 352 353 run govc datastore.disk.info 354 assert_failure 355 356 run govc datastore.disk.info enoent 357 assert_failure 358 359 run govc datastore.disk.info "$GOVC_TEST_VMDK" 360 assert_success 361 362 run govc datastore.disk.info -d "$GOVC_TEST_VMDK" 363 assert_success 364 365 run govc datastore.disk.info -c "$GOVC_TEST_VMDK" 366 assert_success 367 } 368 369 @test "datastore.disk.inflate" { 370 esx_env 371 372 id=$(new_id) 373 vmdk="$id/$id.vmdk" 374 375 run govc datastore.mkdir "$id" 376 assert_success 377 378 run govc datastore.disk.create -size 10MB "$vmdk" 379 assert_success 380 381 type=$(govc datastore.disk.info -json "$vmdk" | jq -r .[].diskType) 382 [ "$type" = "thin" ] 383 384 run govc datastore.disk.inflate "$vmdk" 385 assert_success 386 387 type=$(govc datastore.disk.info -json "$vmdk" | jq -r .[].diskType) 388 [ "$type" = "eagerZeroedThick" ] 389 390 run govc datastore.disk.shrink "$vmdk" 391 assert_success 392 393 run govc datastore.disk.shrink -copy "$vmdk" 394 assert_success 395 396 run govc datastore.disk.shrink -copy=false "$vmdk" 397 assert_success 398 } 399 400 @test "datastore.cluster" { 401 vcsim_env 402 403 pod=/DC0/datastore/DC0_POD0 404 run govc folder.create -pod $pod 405 assert_success 406 407 run govc datastore.cluster.info 408 assert_success 409 410 run govc folder.create -pod $pod 411 assert_failure # duplicate name 412 413 run govc datastore.cluster.change -drs-enabled -drs-mode manual $pod 414 assert_success 415 416 run govc datastore.cluster.info -json 417 assert_success 418 }