File: object.bats

package info (click to toggle)
golang-github-vmware-govmomi 0.24.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,848 kB
  • sloc: sh: 2,285; lisp: 1,560; ruby: 948; xml: 139; makefile: 54
file content (615 lines) | stat: -rwxr-xr-x 15,969 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
#!/usr/bin/env bats

load test_helper

@test "object.destroy" {
  vcsim_env

  run govc object.destroy "/enoent"
  assert_failure

  run govc object.destroy
  assert_failure

  vm=$(new_id)
  run govc vm.create "$vm"
  assert_success

  # fails when powered on
  run govc object.destroy "vm/$vm"
  assert_failure

  run govc vm.power -off "$vm"
  assert_success

  run govc object.destroy "vm/$vm"
  assert_success
}

@test "object.rename" {
  vcsim_env

  run govc object.rename "/enoent" "nope"
  assert_failure

  vm=$(new_id)
  run govc vm.create -on=false "$vm"
  assert_success

  run govc object.rename "vm/$vm" "${vm}-renamed"
  assert_success

  run govc object.rename "vm/$vm" "${vm}-renamed"
  assert_failure

  run govc object.destroy "vm/${vm}-renamed"
  assert_success
}

@test "object.mv" {
  vcsim_env

  folder=$(new_id)

  run govc folder.create "vm/$folder"
  assert_success

  for _ in $(seq 1 3) ; do
    vm=$(new_id)
    run govc vm.create -folder "$folder" "$vm"
    assert_success
  done

  result=$(govc ls "vm/$folder" | wc -l)
  [ "$result" -eq "3" ]

  run govc folder.create "vm/${folder}-2"
  assert_success

  run govc object.mv "vm/$folder/*" "vm/${folder}-2"
  assert_success

  result=$(govc ls "vm/${folder}-2" | wc -l)
  [ "$result" -eq "3" ]

  result=$(govc ls "vm/$folder" | wc -l)
  [ "$result" -eq "0" ]
}

@test "object.collect" {
  vcsim_env

  run govc object.collect
  assert_success

  run govc object.collect -json
  assert_success

  run govc object.collect -
  assert_success

  run govc object.collect -json -
  assert_success

  run govc object.collect - content
  assert_success

  run govc object.collect -json - content
  assert_success

  root=$(govc object.collect - content | grep content.rootFolder | awk '{print $3}')

  dc=$(govc object.collect "$root" childEntity | awk '{print $3}' | cut -d, -f1)

  hostFolder=$(govc object.collect "$dc" hostFolder | awk '{print $3}')

  cr=$(govc object.collect "$hostFolder" childEntity | awk '{print $3}' | cut -d, -f1)

  host=$(govc object.collect "$cr" host | awk '{print $3}' | cut -d, -f1)

  run govc object.collect "$host"
  assert_success

  run govc object.collect "$host" hardware
  assert_success

  run govc object.collect "$host" hardware.systemInfo
  assert_success

  uuid=$(govc object.collect "$host" hardware.systemInfo.uuid | awk '{print $3}')
  uuid_s=$(govc object.collect -s "$host" hardware.systemInfo.uuid)
  assert_equal "$uuid" "$uuid_s"

  run govc object.collect "$(govc ls host | head -n1)"
  assert_success

  # test against slice of interface
  setting=$(govc object.collect -s - content.setting)
  result=$(govc object.collect -s "$setting" setting)
  assert_equal "..." "$result"

  # test against an interface field
  run govc object.collect 'network/VM Network' summary
  assert_success

  run govc object.collect -dump -o 'network/VM Network'
  assert_success
  gofmt <<<"$output"

  run govc object.collect -json -o 'network/VM Network'
  assert_success
  jq . <<<"$output"
}

@test "object.collect vcsim" {
  vcsim_env -app 1 -pool 1

  # test that {Cluster}ComputeResource and HostSystem network fields have the expected refs
  for obj in DC0_C0 DC0_C0/DC0_C0_H0 DC0_H0 DC0_H0/DC0_H0; do
    run govc object.collect /DC0/host/$obj network
    assert_success
    echo "obj=$obj"
    assert_matches "DistributedVirtualPortgroup:"
    assert_matches "Network:"
  done

  run govc object.collect -s -type ClusterComputeResource / configStatus
  assert_success green

  run govc object.collect -s -type ClusterComputeResource / effectiveRole # []int32 -> ArrayOfInt
  assert_number

  run govc object.collect -s -type ComputeResource / configStatus
  assert_success "$(printf "green\ngreen")"

  run govc object.collect -s -type ComputeResource / effectiveRole
  assert_number

  run govc object.collect -s -type Datacenter / effectiveRole
  assert_number

  run govc object.collect -s -type Datastore / effectiveRole
  assert_number

  run govc object.collect -s -type DistributedVirtualPortgroup / config.key
  assert_matches dvportgroup-

  run govc object.collect -s -type DistributedVirtualPortgroup / config.name
  assert_matches DC0_DVPG0
  assert_matches DVS0-DVUplinks-

  run govc object.collect -s -type DistributedVirtualPortgroup / effectiveRole
  assert_number

  run govc object.collect -s -type DistributedVirtualSwitch / effectiveRole
  assert_number

  run govc object.collect -s -type DistributedVirtualSwitch / summary.name
  assert_success DVS0

  run govc object.collect -s -type DistributedVirtualSwitch / summary.productInfo.name
  assert_success DVS

  run govc object.collect -s -type DistributedVirtualSwitch / summary.productInfo.vendor
  assert_success "VMware, Inc."

  run govc object.collect -s -type DistributedVirtualSwitch / summary.productInfo.version
  assert_success 6.5.0

  run govc object.collect -s -type DistributedVirtualSwitch / summary.uuid
  assert_matches "-"

  run govc object.collect -s -type Folder / effectiveRole
  assert_number

  run govc object.collect -json -type HostSystem / config.storageDevice.scsiLun
  assert_matches /vmfs/devices

  run govc object.collect -json -type HostSystem / config.storageDevice.scsiTopology
  assert_matches host.ScsiTopology

  run govc object.collect -s -type HostSystem / effectiveRole
  assert_number

  run govc object.collect -s -type Network / effectiveRole
  assert_number

  run govc object.collect -s -type ResourcePool / resourcePool
  # DC0_C0/Resources has 1 child ResourcePool and 1 child VirtualApp
  assert_matches "ResourcePool:"
  assert_matches "VirtualApp:"

  run govc object.collect -s -type VirtualApp / effectiveRole
  assert_number

  run govc object.collect -s -type VirtualApp / name
  assert_success DC0_C0_APP0

  run govc object.collect -s -type VirtualApp / owner
  assert_matches ":"

  run govc object.collect -s -type VirtualApp / parent
  assert_matches ":"

  run govc object.collect -s -type VirtualApp / resourcePool
  assert_success "" # no VirtualApp children

  run govc object.collect -s -type VirtualApp / summary.config.cpuAllocation.limit
  assert_number

  run govc object.collect -s -type VirtualApp / summary.config.cpuAllocation.reservation
  assert_number

  run govc object.collect -s -type VirtualApp / summary.config.memoryAllocation.limit
  assert_number

  run govc object.collect -s -type VirtualApp / summary.config.memoryAllocation.reservation
  assert_number

  run govc object.collect -s -type VirtualApp / vm
  assert_matches "VirtualMachine:"

  run govc object.collect -s -type VirtualMachine / config.tools.toolsVersion
  assert_number

  run govc object.collect -s -type VirtualMachine / effectiveRole
  assert_number

  run govc object.collect -s -type VirtualMachine / summary.guest.toolsStatus
  assert_matches toolsNotInstalled

  run govc object.collect -s -type VirtualMachine / config.npivPortWorldWideName # []int64 -> ArrayOfLong
  assert_success

  run govc object.collect -s -type VirtualMachine / config.vmxConfigChecksum # []uint8 -> ArrayOfByte
  assert_success

  run govc object.collect -s /DC0/vm/DC0_H0_VM0 config.hardware.numCoresPerSocket
  assert_success 1

  run govc object.collect -s -type ClusterComputeResource / summary.effectiveCpu
  assert_number

  run govc object.collect -s -type ClusterComputeResource / summary.effectiveMemory
  assert_number

  run govc object.collect -s -type ClusterComputeResource / summary.numCpuCores
  assert_number

  run govc object.collect -s -type ClusterComputeResource / summary.numCpuThreads
  assert_number

  run govc object.collect -s -type ClusterComputeResource / summary.numEffectiveHosts
  assert_number

  run govc object.collect -s -type ClusterComputeResource / summary.numHosts
  assert_number

  run govc object.collect -s -type ClusterComputeResource / summary.totalCpu
  assert_number

  run govc object.collect -s -type ClusterComputeResource / summary.totalMemory
  assert_number

  run govc object.collect -s -type ComputeResource / summary.effectiveCpu
  assert_number

  run govc object.collect -s -type ComputeResource / summary.effectiveMemory
  assert_number

  run govc object.collect -s -type ComputeResource / summary.numCpuCores
  assert_number

  run govc object.collect -s -type ComputeResource / summary.numCpuThreads
  assert_number

  run govc object.collect -s -type ComputeResource / summary.numEffectiveHosts
  assert_number

  run govc object.collect -s -type ComputeResource / summary.numHosts
  assert_number

  run govc object.collect -s -type ComputeResource / summary.totalCpu
  assert_number

  run govc object.collect -s -type ComputeResource / summary.totalMemory
  assert_number

  run govc object.collect -s -type Network / summary.accessible
  assert_success "$(printf "true\ntrue\ntrue")"

  run govc object.collect -s -type Network / summary.ipPoolName
  assert_success ""

  # check that uuid and instanceUuid are set under both config and summary.config
  for prop in config summary.config ; do
    uuids=$(govc object.collect -s -type m / "$prop.uuid" | sort)
    [ -n "$uuids" ]
    iuuids=$(govc object.collect -s -type m / "$prop.instanceUuid" | sort)
    [ -n "$iuuids" ]

    [ "$uuids" != "$iuuids" ]
  done

  govc vm.create -g ubuntu64Guest my-ubuntu
  assert_success

  govc object.collect -type m / -guest.guestFamily linuxGuest
  assert_success
}

@test "object.collect view" {
  vcsim_env -dc 2 -folder 1

  run govc object.collect -type m
  assert_success

  run govc object.collect -type m / -name '*C0*'
  assert_success

  run govc object.collect -type m / -name
  assert_success

  run govc object.collect -type m / name runtime.powerState
  assert_success

  run govc object.collect -type m -type h /F0 name
  assert_success

  run govc object.collect -type n / name
  assert_success

  run govc object.collect -type enoent / name
  assert_failure

  govc object.collect -wait 5m -s -type m ./vm -name foo &
  pid=$!
  run govc vm.create -on=false foo
  assert_success

  wait $pid # wait for object.collect to exit

  run govc object.collect -s -type m ./vm -name foo
  assert_success

  govc object.collect -wait 5m -s -type d / -name dcx &
  pid=$!
  run govc datacenter.create dcx
  assert_success

  wait $pid # wait for object.collect to exit

  run govc object.collect -s -type d / -name dcx
  assert_success
}

@test "object.collect raw" {
  vcsim_env

  govc object.collect -R - <<EOF | grep serverClock
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
 <Body>
  <CreateFilter xmlns="urn:vim25">
   <_this type="PropertyCollector">PropertyCollector</_this>
   <spec>
    <propSet>
     <type>ServiceInstance</type>
     <all>true</all>
    </propSet>
    <objectSet>
     <obj type="ServiceInstance">ServiceInstance</obj>
    </objectSet>
   </spec>
   <partialUpdates>false</partialUpdates>
  </CreateFilter>
 </Body>
</Envelope>
EOF

  govc object.collect -O | grep types.CreateFilter
  govc object.collect -O -json | jq .
}

@test "object.find" {
  vcsim_env -ds 2

  unset GOVC_DATACENTER

  run govc find "/enoent"
  assert_failure

  run govc find
  assert_success

  run govc find .
  assert_success

  run govc find /
  assert_success

  govc find -json / | jq .

  run govc find . -type HostSystem
  assert_success

  dc=$(govc find / -type Datacenter | head -1)

  run govc find "$dc" -maxdepth 0
  assert_output "$dc"

  run govc find "$dc/vm" -maxdepth 0
  assert_output "$dc/vm"

  run govc find "$dc" -maxdepth 1 -type Folder
  assert_success
  # /<datacenter>/{vm,network,host,datastore}
  [ ${#lines[@]} -eq 4 ]

  folder=$(govc find -type Folder -name vm)

  vm=$(new_empty_vm)

  run govc find . -name "$vm"
  assert_output "$folder/$vm"

  run govc find -p "$folder/$vm" -type Datacenter
  assert_output "$dc"

  run govc find "$folder" -name "$vm"
  assert_output "$folder/$vm"

  # moref for VM Network
  net=$(govc find -i network -name "$(basename "$GOVC_NETWORK")")

  # $vm.network.contains($net) == true
  run govc find . -type m -name "$vm" -network "$net"
  assert_output "$folder/$vm"

  # remove network reference
  run govc device.remove -vm "$vm" ethernet-*
  assert_success

  # $vm.network.contains($net) == false
  run govc find . -type VirtualMachine -name "$vm" -network "$net"
  assert_output ""

  run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOn
  assert_output ""

  run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOff
  assert_output "$folder/$vm"

  run govc vm.power -on "$vm"
  assert_success

  run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOff
  assert_output ""

  run govc find "$folder" -type VirtualMachine -name "govc-test-*" -runtime.powerState poweredOn
  assert_output "$folder/$vm"

  # output paths should be relative to "." in these cases
  export GOVC_DATACENTER=$dc

  folder="./vm"

  run govc find . -name "$vm"
  assert_output "$folder/$vm"

  run govc find "$folder" -name "$vm"
  assert_output "$folder/$vm"

  # Make sure property filter doesn't match when guest is unset for $vm (issue 1089)
  run govc find "$folder" -type m -guest.ipAddress 0.0.0.0
  assert_output ""

  run govc fields.add -type Datastore ds-mode
  assert_success

  run govc fields.add -type Datastore ds-other
  assert_success

  run govc fields.set ds-mode prod datastore/LocalDS_0
  assert_success

  run govc fields.set ds-other prod datastore/LocalDS_1
  assert_success

  run govc fields.set ds-mode test datastore/LocalDS_1
  assert_success

  run govc fields.set ds-other foo datastore/LocalDS_1
  assert_success

  key=$(govc fields.ls | grep ds-mode | awk '{print $1}')

  run govc find -type s / -customValue "$key:prod" # match specific key:val
  assert_success /DC0/datastore/LocalDS_0

  run govc find -type s / -customValue "*:test" # match any key:val
  assert_success /DC0/datastore/LocalDS_1

  run govc find -type s / -customValue "$key:*" # match specific key w/ any val
  assert_matches /DC0/datastore/LocalDS_0
  assert_matches /DC0/datastore/LocalDS_1

  run govc find -type s / -customValue 0:dev # value doesn't match any entity
  assert_success ""
}

@test "object.find multi root" {
  vcsim_env -dc 2

  run govc find vm
  assert_success # ok as there is 1 "vm" folder relative to $GOVC_DATACENTER

  run govc folder.create vm/{one,two}
  assert_success

  run govc find vm/one
  assert_success

  run govc find vm/on*
  assert_success

  run govc find vm/*o*
  assert_failure # matches 2 objects

  run govc folder.create vm/{one,two}/vm
  assert_success

  # prior to forcing Finder list mode, this would have failed since ManagedObjectList("vm") would have returned
  # all 3 folders named "vm": "vm", "vm/one/vm", "vm/two/vm"
  run govc find vm
  assert_success

  unset GOVC_DATACENTER
  run govc find vm
  assert_failure # without Datacenter specified, there are 0 "vm" folders relative to the root folder

  run govc find -l /
  assert_success

  run govc find -l -i /
  assert_success
}

@test "object.method" {
  vcsim_env_todo

  vm=$(govc find vm -type m | head -1)

  run govc object.method -enable=false -name NoSuchMethod "$vm"
  assert_failure

  run govc object.method -enable=false -name Destroy_Task enoent
  assert_failure

  run govc object.collect -s "$vm" disabledMethod
  ! assert_matches "Destroy_Task" "$output"

  run govc object.method -enable=false -name Destroy_Task "$vm"
  assert_success

  run govc object.collect -s "$vm" disabledMethod
  assert_matches "Destroy_Task" "$output"

  run govc object.method -enable -name Destroy_Task "$vm"
  assert_success

  run govc object.collect -s "$vm" disabledMethod
  ! assert_matches "Destroy_Task" "$output"
}

@test "object.save" {
  vcsim_env

  dir="$BATS_TMPDIR/$(new_id)"
  run govc object.save -v -d "$dir" /DC0/vm
  assert_success

  n=$(ls "$dir"/*.xml | wc -l)
  rm -rf "$dir"
  assert_equal 6 "$n"
}