2
Copyright (c) 2014 VMware, Inc. All Rights Reserved.
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
8
http://www.apache.org/licenses/LICENSE-2.0
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
24
"github.com/juju/govmomi/vim25/types"
27
var devices = VirtualDeviceList([]types.BaseVirtualDevice{
28
&types.VirtualIDEController{
29
VirtualController: types.VirtualController{
30
VirtualDevice: types.VirtualDevice{
31
DynamicData: types.DynamicData{},
33
DeviceInfo: &types.Description{
34
DynamicData: types.DynamicData{},
39
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
45
Device: []int{3001, 3000},
48
&types.VirtualIDEController{
49
VirtualController: types.VirtualController{
50
VirtualDevice: types.VirtualDevice{
51
DynamicData: types.DynamicData{},
53
DeviceInfo: &types.Description{
54
DynamicData: types.DynamicData{},
59
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
68
&types.VirtualPS2Controller{
69
VirtualController: types.VirtualController{
70
VirtualDevice: types.VirtualDevice{
71
DynamicData: types.DynamicData{},
73
DeviceInfo: &types.Description{
74
DynamicData: types.DynamicData{},
75
Label: "PS2 controller 0",
76
Summary: "PS2 controller 0",
79
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
85
Device: []int{600, 700},
88
&types.VirtualPCIController{
89
VirtualController: types.VirtualController{
90
VirtualDevice: types.VirtualDevice{
91
DynamicData: types.DynamicData{},
93
DeviceInfo: &types.Description{
94
DynamicData: types.DynamicData{},
95
Label: "PCI controller 0",
96
Summary: "PCI controller 0",
99
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
105
Device: []int{500, 12000, 1000, 4000},
108
&types.VirtualSIOController{
109
VirtualController: types.VirtualController{
110
VirtualDevice: types.VirtualDevice{
111
DynamicData: types.DynamicData{},
113
DeviceInfo: &types.Description{
114
DynamicData: types.DynamicData{},
115
Label: "SIO controller 0",
116
Summary: "SIO controller 0",
119
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
128
&types.VirtualKeyboard{
129
VirtualDevice: types.VirtualDevice{
130
DynamicData: types.DynamicData{},
132
DeviceInfo: &types.Description{
133
DynamicData: types.DynamicData{},
138
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
144
&types.VirtualPointingDevice{
145
VirtualDevice: types.VirtualDevice{
146
DynamicData: types.DynamicData{},
148
DeviceInfo: &types.Description{
149
DynamicData: types.DynamicData{},
150
Label: "Pointing device",
151
Summary: "Pointing device; Device",
153
Backing: &types.VirtualPointingDeviceDeviceBackingInfo{
154
VirtualDeviceDeviceBackingInfo: types.VirtualDeviceDeviceBackingInfo{},
155
HostPointingDevice: "autodetect",
157
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
163
&types.VirtualMachineVideoCard{
164
VirtualDevice: types.VirtualDevice{
165
DynamicData: types.DynamicData{},
167
DeviceInfo: &types.Description{
168
DynamicData: types.DynamicData{},
169
Label: "Video card ",
170
Summary: "Video card",
173
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
178
VideoRamSizeInKB: 4096,
180
UseAutoDetect: types.NewBool(false),
181
Enable3DSupport: types.NewBool(false),
182
Use3dRenderer: "automatic",
184
&types.VirtualMachineVMCIDevice{
185
VirtualDevice: types.VirtualDevice{
186
DynamicData: types.DynamicData{},
188
DeviceInfo: &types.Description{
189
DynamicData: types.DynamicData{},
190
Label: "VMCI device",
191
Summary: "Device on the virtual machine PCI bus that provides support for the virtual machine communication interface",
194
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
195
SlotInfo: &types.VirtualDevicePciBusSlotInfo{
196
VirtualDeviceBusSlotInfo: types.VirtualDeviceBusSlotInfo{},
203
AllowUnrestrictedCommunication: types.NewBool(false),
205
&types.VirtualLsiLogicController{
206
VirtualSCSIController: types.VirtualSCSIController{
207
VirtualController: types.VirtualController{
208
VirtualDevice: types.VirtualDevice{
209
DynamicData: types.DynamicData{},
211
DeviceInfo: &types.Description{
212
DynamicData: types.DynamicData{},
213
Label: "SCSI controller 0",
214
Summary: "LSI Logic",
217
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
225
HotAddRemove: types.NewBool(true),
226
SharedBus: "noSharing",
227
ScsiCtlrUnitNumber: 7,
231
VirtualDevice: types.VirtualDevice{
232
DynamicData: types.DynamicData{},
234
DeviceInfo: &types.Description{
235
DynamicData: types.DynamicData{},
236
Label: "CD/DVD drive 1",
237
Summary: "ISO [datastore1] ttylinux-pc_i486-16.1.iso",
239
Backing: &types.VirtualCdromIsoBackingInfo{
240
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
241
VirtualDeviceBackingInfo: types.VirtualDeviceBackingInfo{},
242
FileName: "[datastore1] foo.iso",
243
Datastore: &types.ManagedObjectReference{Type: "Datastore", Value: "53fe43cc-75dc5110-3643-000c2918dc41"},
247
Connectable: &types.VirtualDeviceConnectInfo{
248
DynamicData: types.DynamicData{},
249
StartConnected: true,
250
AllowGuestControl: true,
260
VirtualDevice: types.VirtualDevice{
261
DynamicData: types.DynamicData{},
263
DeviceInfo: &types.Description{
264
DynamicData: types.DynamicData{},
265
Label: "Hard disk 1",
266
Summary: "30,720 KB",
268
Backing: &types.VirtualDiskFlatVer2BackingInfo{
269
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
270
VirtualDeviceBackingInfo: types.VirtualDeviceBackingInfo{},
271
FileName: "[datastore1] bar/bar.vmdk",
272
Datastore: &types.ManagedObjectReference{Type: "Datastore", Value: "53fe43cc-75dc5110-3643-000c2918dc41"},
273
BackingObjectId: "3-3000-0",
275
DiskMode: "persistent",
276
Split: types.NewBool(false),
277
WriteThrough: types.NewBool(false),
278
ThinProvisioned: types.NewBool(false),
279
EagerlyScrub: types.NewBool(true),
280
Uuid: "6000C296-d0af-1209-1975-10c98eae10e4",
281
ContentId: "d46395062e2d1b1790985bdec573b211",
283
Parent: &types.VirtualDiskFlatVer2BackingInfo{
284
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
285
VirtualDeviceBackingInfo: types.VirtualDeviceBackingInfo{},
286
FileName: "[datastore1] ttylinux.vmdk",
287
Datastore: &types.ManagedObjectReference{Type: "Datastore", Value: "53fe43cc-75dc5110-3643-000c2918dc41"},
288
BackingObjectId: "3-3000-1",
290
DiskMode: "persistent",
291
Split: types.NewBool(false),
292
WriteThrough: types.NewBool(false),
293
ThinProvisioned: types.NewBool(false),
294
EagerlyScrub: types.NewBool(true),
295
Uuid: "6000C296-d0af-1209-1975-10c98eae10e4",
296
ContentId: "1c2dad9e1662219e962a620c6d238a7c",
298
Parent: (*types.VirtualDiskFlatVer2BackingInfo)(nil),
300
DigestEnabled: types.NewBool(false),
303
DeltaDiskFormat: "redoLogFormat",
304
DigestEnabled: types.NewBool(false),
307
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
313
CapacityInBytes: 31457280,
314
Shares: &types.SharesInfo{
315
DynamicData: types.DynamicData{},
319
StorageIOAllocation: &types.StorageIOAllocationInfo{
320
DynamicData: types.DynamicData{},
322
Shares: &types.SharesInfo{
323
DynamicData: types.DynamicData{},
329
DiskObjectId: "3-3000",
330
VFlashCacheConfigInfo: (*types.VirtualDiskVFlashCacheConfigInfo)(nil),
333
VirtualDevice: types.VirtualDevice{
334
DynamicData: types.DynamicData{},
336
DeviceInfo: &types.Description{
337
DynamicData: types.DynamicData{},
338
Label: "Hard disk 2",
339
Summary: "10,000,000 KB",
341
Backing: &types.VirtualDiskFlatVer2BackingInfo{
342
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
343
VirtualDeviceBackingInfo: types.VirtualDeviceBackingInfo{},
344
FileName: "[datastore1] bar/disk-201-0.vmdk",
345
Datastore: &types.ManagedObjectReference{Type: "Datastore", Value: "53fe43cc-75dc5110-3643-000c2918dc41"},
346
BackingObjectId: "3-3002-0",
348
DiskMode: "persistent",
349
Split: types.NewBool(false),
350
WriteThrough: types.NewBool(false),
351
ThinProvisioned: types.NewBool(true),
352
EagerlyScrub: types.NewBool(false),
353
Uuid: "6000C293-fde5-4457-5118-dd267ea992a7",
354
ContentId: "90399989b9d520eed6793ab0fffffffe",
356
Parent: (*types.VirtualDiskFlatVer2BackingInfo)(nil),
358
DigestEnabled: types.NewBool(false),
361
Connectable: (*types.VirtualDeviceConnectInfo)(nil),
366
CapacityInKB: 10000000,
367
CapacityInBytes: 10240000000,
368
Shares: &types.SharesInfo{
369
DynamicData: types.DynamicData{},
373
StorageIOAllocation: &types.StorageIOAllocationInfo{
374
DynamicData: types.DynamicData{},
376
Shares: &types.SharesInfo{
377
DynamicData: types.DynamicData{},
383
DiskObjectId: "3-3002",
384
VFlashCacheConfigInfo: (*types.VirtualDiskVFlashCacheConfigInfo)(nil),
387
VirtualEthernetCard: types.VirtualEthernetCard{
388
VirtualDevice: types.VirtualDevice{
389
DynamicData: types.DynamicData{},
391
DeviceInfo: &types.Description{
392
DynamicData: types.DynamicData{},
393
Label: "Network adapter 1",
394
Summary: "VM Network",
396
Backing: &types.VirtualEthernetCardNetworkBackingInfo{
397
VirtualDeviceDeviceBackingInfo: types.VirtualDeviceDeviceBackingInfo{
398
VirtualDeviceBackingInfo: types.VirtualDeviceBackingInfo{},
399
DeviceName: "VM Network",
400
UseAutoDetect: types.NewBool(false),
402
Network: &types.ManagedObjectReference{Type: "Network", Value: "HaNetwork-VM Network"},
403
InPassthroughMode: types.NewBool(false),
405
Connectable: &types.VirtualDeviceConnectInfo{
406
DynamicData: types.DynamicData{},
407
StartConnected: true,
408
AllowGuestControl: true,
412
SlotInfo: &types.VirtualDevicePciBusSlotInfo{
413
VirtualDeviceBusSlotInfo: types.VirtualDeviceBusSlotInfo{},
419
AddressType: "generated",
420
MacAddress: "00:0c:29:93:d7:27",
421
WakeOnLanEnabled: types.NewBool(true),
424
&types.VirtualSerialPort{
425
VirtualDevice: types.VirtualDevice{
426
DynamicData: types.DynamicData{},
428
DeviceInfo: &types.Description{
429
DynamicData: types.DynamicData{},
430
Label: "Serial port 1",
431
Summary: "Remote localhost:0",
433
Backing: &types.VirtualSerialPortURIBackingInfo{
434
VirtualDeviceURIBackingInfo: types.VirtualDeviceURIBackingInfo{
435
VirtualDeviceBackingInfo: types.VirtualDeviceBackingInfo{},
436
ServiceURI: "localhost:0",
441
Connectable: &types.VirtualDeviceConnectInfo{
442
DynamicData: types.DynamicData{},
443
StartConnected: true,
444
AllowGuestControl: true,
456
func TestSelectByType(t *testing.T) {
458
dtype types.BaseVirtualDevice
462
(*types.VirtualCdrom)(nil),
466
(*types.VirtualEthernetCard)(nil),
470
(*types.VirtualDisk)(nil),
474
(*types.VirtualController)(nil),
478
(*types.VirtualIDEController)(nil),
482
(*types.VirtualSCSIController)(nil),
486
(*types.VirtualLsiLogicController)(nil),
490
(*types.ParaVirtualSCSIController)(nil),
495
for _, test := range tests {
496
d := devices.SelectByType(test.dtype)
498
if len(d) != test.expect {
499
t.Errorf("%#v has %d", test.dtype, len(devices))
504
func TestSelectByBackingInfo(t *testing.T) {
505
tests := []types.BaseVirtualDeviceBackingInfo{
506
&types.VirtualEthernetCardNetworkBackingInfo{
507
VirtualDeviceDeviceBackingInfo: types.VirtualDeviceDeviceBackingInfo{
508
DeviceName: "VM Network",
511
&types.VirtualDiskFlatVer2BackingInfo{
512
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
513
FileName: "[datastore1] bar/bar.vmdk",
516
&types.VirtualDiskFlatVer2BackingInfo{
517
Parent: &types.VirtualDiskFlatVer2BackingInfo{
518
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
519
FileName: "[datastore1] ttylinux.vmdk",
523
&types.VirtualCdromIsoBackingInfo{
524
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
525
VirtualDeviceBackingInfo: types.VirtualDeviceBackingInfo{},
526
FileName: "[datastore1] foo.iso",
531
for _, test := range tests {
532
l := devices.SelectByBackingInfo(test)
535
t.Errorf("Expected 1, got %d: %#v", len(l), test)
540
func TestFind(t *testing.T) {
541
for _, device := range devices {
542
name := devices.Name(device)
543
d := devices.Find(name)
544
if name != devices.Name(d) {
545
t.Errorf("expected name: %s, got: %s", name, devices.Name(d))
549
d := devices.Find("enoent")
551
t.Errorf("unexpected: %#v", d)
555
func TestFindController(t *testing.T) {
556
for _, name := range []string{"", "ide-200"} {
557
_, err := devices.FindIDEController(name)
563
for _, name := range []string{"", "lsilogic-1000"} {
564
_, err := devices.FindSCSIController(name)
570
fns := []func() error{
572
_, err := devices.FindIDEController("lsilogic-1000")
576
_, err := devices.FindSCSIController("ide-200")
581
for _, f := range fns {
584
t.Error("should fail")
589
func TestPickController(t *testing.T) {
593
ctype types.BaseVirtualController
598
(*types.VirtualIDEController)(nil), 201, 1,
601
(*types.VirtualSCSIController)(nil), 1000, 0,
604
(*types.VirtualSCSIController)(nil), 1000, 1,
608
for _, test := range tests {
609
c := list.PickController(test.ctype).GetVirtualController()
613
t.Errorf("expected controller key: %d, got: %d\n", test.key, key)
616
unit := list.newUnitNumber(c)
617
if unit != test.unit {
618
t.Errorf("expected unit number: %d, got: %d\n", test.unit, unit)
621
dev := &types.VirtualDevice{
627
list = append(list, dev)
628
c.Device = append(c.Device, dev.Key)
631
if list.PickController((*types.VirtualIDEController)(nil)) != nil {
632
t.Error("should be nil")
635
if list.PickController((*types.VirtualSCSIController)(nil)) == nil {
636
t.Errorf("should not be nil")
640
func TestCreateSCSIController(t *testing.T) {
641
for _, l := range []VirtualDeviceList{SCSIControllerTypes(), devices} {
642
_, err := l.CreateSCSIController("enoent")
644
t.Error("should fail")
647
for _, name := range []string{"", "scsi", "pvscsi", "buslogic", "lsilogic", "lsilogic-sas"} {
648
_, err = l.CreateSCSIController(name)
656
func TestCreateEthernetCard(t *testing.T) {
657
_, err := EthernetCardTypes().CreateEthernetCard("enoent", nil)
659
t.Error("should fail")
662
for _, name := range []string{"", "e1000", "e1000e", "vmxnet3"} {
663
_, err := EthernetCardTypes().CreateEthernetCard(name, nil)
670
func TestCdrom(t *testing.T) {
671
c, err := devices.FindCdrom("")
676
d := devices.Find(devices.Name(c))
678
if c.Key != d.GetVirtualDevice().Key {
679
t.Error("device key mismatch")
682
for _, name := range []string{"enoent", "ide-200"} {
683
c, err = devices.FindCdrom(name)
685
t.Errorf("FindCdrom(%s) should fail", name)
689
c, err = devices.Select(func(device types.BaseVirtualDevice) bool {
690
if _, ok := device.(*types.VirtualCdrom); ok {
697
t.Error("FindCdrom('') should fail")
701
func TestSerialPort(t *testing.T) {
702
device, err := devices.CreateSerialPort()
706
devices.ConnectSerialPort(device, "telnet://:33233", false)
709
func TestPrimaryMacAddress(t *testing.T) {
710
expect := "00:0c:29:93:d7:27"
711
mac := devices.PrimaryMacAddress()
713
t.Errorf("expected: %s, got: %s", expect, mac)
717
func TestBootOrder(t *testing.T) {
718
o := []string{DeviceTypeEthernet, DeviceTypeCdrom, DeviceTypeFloppy, DeviceTypeDisk}
721
n := 4 // 1 ethernet, 1 cdrom, 2 disk
722
order := list.BootOrder(o)
724
t.Errorf("expected %d boot devices, got: %d", n, len(order))
727
list = list.SelectBootOrder(order)
729
t.Errorf("expected %d boot devices, got: %d", n, len(list))
732
// test lookup by name
734
for _, x := range list {
735
names = append(names, list.Name(x))
738
order = list.BootOrder(names)
740
t.Errorf("expected %d boot devices, got: %d", n, len(order))
743
if !reflect.DeepEqual(list, list.SelectBootOrder(order)) {
744
t.Error("boot order mismatch")
748
list = list.Select(func(device types.BaseVirtualDevice) bool {
749
if _, ok := device.(*types.VirtualDisk); ok {
755
n = 2 // 1 ethernet, 1 cdrom
756
order = list.BootOrder(o)
758
t.Errorf("expected %d boot devices, got: %d", n, len(order))
761
if !reflect.DeepEqual(list, list.SelectBootOrder(order)) {
762
t.Error("boot order mismatch")
765
if len(list.BootOrder([]string{DeviceTypeDisk})) != 0 {
766
t.Error("expected 0 disks")
770
func TestName(t *testing.T) {
772
device types.BaseVirtualDevice
776
&types.VirtualCdrom{},
780
&types.VirtualDisk{},
784
&types.VirtualFloppy{},
788
&types.VirtualIDEController{},
792
&types.VirtualMachineVideoCard{},
796
&types.VirtualPointingDevice{},
800
&types.ParaVirtualSCSIController{},
804
&types.VirtualSerialPort{},
809
VirtualEthernetCard: types.VirtualEthernetCard{
810
VirtualDevice: types.VirtualDevice{
819
for _, test := range tests {
820
name := devices.Name(test.device)
821
if name != test.expect {
822
t.Errorf("expected: %s, got: %s", test.expect, name)