~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/govmomi/object/virtual_device_list_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2014 VMware, Inc. All Rights Reserved.
 
3
 
 
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
 
7
 
 
8
    http://www.apache.org/licenses/LICENSE-2.0
 
9
 
 
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.
 
15
*/
 
16
 
 
17
package object
 
18
 
 
19
import (
 
20
        "math/rand"
 
21
        "reflect"
 
22
        "testing"
 
23
 
 
24
        "github.com/juju/govmomi/vim25/types"
 
25
)
 
26
 
 
27
var devices = VirtualDeviceList([]types.BaseVirtualDevice{
 
28
        &types.VirtualIDEController{
 
29
                VirtualController: types.VirtualController{
 
30
                        VirtualDevice: types.VirtualDevice{
 
31
                                DynamicData: types.DynamicData{},
 
32
                                Key:         200,
 
33
                                DeviceInfo: &types.Description{
 
34
                                        DynamicData: types.DynamicData{},
 
35
                                        Label:       "IDE 0",
 
36
                                        Summary:     "IDE 0",
 
37
                                },
 
38
                                Backing:       nil,
 
39
                                Connectable:   (*types.VirtualDeviceConnectInfo)(nil),
 
40
                                SlotInfo:      nil,
 
41
                                ControllerKey: 0,
 
42
                                UnitNumber:    0,
 
43
                        },
 
44
                        BusNumber: 0,
 
45
                        Device:    []int{3001, 3000},
 
46
                },
 
47
        },
 
48
        &types.VirtualIDEController{
 
49
                VirtualController: types.VirtualController{
 
50
                        VirtualDevice: types.VirtualDevice{
 
51
                                DynamicData: types.DynamicData{},
 
52
                                Key:         201,
 
53
                                DeviceInfo: &types.Description{
 
54
                                        DynamicData: types.DynamicData{},
 
55
                                        Label:       "IDE 1",
 
56
                                        Summary:     "IDE 1",
 
57
                                },
 
58
                                Backing:       nil,
 
59
                                Connectable:   (*types.VirtualDeviceConnectInfo)(nil),
 
60
                                SlotInfo:      nil,
 
61
                                ControllerKey: 0,
 
62
                                UnitNumber:    0,
 
63
                        },
 
64
                        BusNumber: 1,
 
65
                        Device:    []int{3002},
 
66
                },
 
67
        },
 
68
        &types.VirtualPS2Controller{
 
69
                VirtualController: types.VirtualController{
 
70
                        VirtualDevice: types.VirtualDevice{
 
71
                                DynamicData: types.DynamicData{},
 
72
                                Key:         300,
 
73
                                DeviceInfo: &types.Description{
 
74
                                        DynamicData: types.DynamicData{},
 
75
                                        Label:       "PS2 controller 0",
 
76
                                        Summary:     "PS2 controller 0",
 
77
                                },
 
78
                                Backing:       nil,
 
79
                                Connectable:   (*types.VirtualDeviceConnectInfo)(nil),
 
80
                                SlotInfo:      nil,
 
81
                                ControllerKey: 0,
 
82
                                UnitNumber:    0,
 
83
                        },
 
84
                        BusNumber: 0,
 
85
                        Device:    []int{600, 700},
 
86
                },
 
87
        },
 
88
        &types.VirtualPCIController{
 
89
                VirtualController: types.VirtualController{
 
90
                        VirtualDevice: types.VirtualDevice{
 
91
                                DynamicData: types.DynamicData{},
 
92
                                Key:         100,
 
93
                                DeviceInfo: &types.Description{
 
94
                                        DynamicData: types.DynamicData{},
 
95
                                        Label:       "PCI controller 0",
 
96
                                        Summary:     "PCI controller 0",
 
97
                                },
 
98
                                Backing:       nil,
 
99
                                Connectable:   (*types.VirtualDeviceConnectInfo)(nil),
 
100
                                SlotInfo:      nil,
 
101
                                ControllerKey: 0,
 
102
                                UnitNumber:    0,
 
103
                        },
 
104
                        BusNumber: 0,
 
105
                        Device:    []int{500, 12000, 1000, 4000},
 
106
                },
 
107
        },
 
108
        &types.VirtualSIOController{
 
109
                VirtualController: types.VirtualController{
 
110
                        VirtualDevice: types.VirtualDevice{
 
111
                                DynamicData: types.DynamicData{},
 
112
                                Key:         400,
 
113
                                DeviceInfo: &types.Description{
 
114
                                        DynamicData: types.DynamicData{},
 
115
                                        Label:       "SIO controller 0",
 
116
                                        Summary:     "SIO controller 0",
 
117
                                },
 
118
                                Backing:       nil,
 
119
                                Connectable:   (*types.VirtualDeviceConnectInfo)(nil),
 
120
                                SlotInfo:      nil,
 
121
                                ControllerKey: 0,
 
122
                                UnitNumber:    0,
 
123
                        },
 
124
                        BusNumber: 0,
 
125
                        Device:    []int{9000},
 
126
                },
 
127
        },
 
128
        &types.VirtualKeyboard{
 
129
                VirtualDevice: types.VirtualDevice{
 
130
                        DynamicData: types.DynamicData{},
 
131
                        Key:         600,
 
132
                        DeviceInfo: &types.Description{
 
133
                                DynamicData: types.DynamicData{},
 
134
                                Label:       "Keyboard ",
 
135
                                Summary:     "Keyboard",
 
136
                        },
 
137
                        Backing:       nil,
 
138
                        Connectable:   (*types.VirtualDeviceConnectInfo)(nil),
 
139
                        SlotInfo:      nil,
 
140
                        ControllerKey: 300,
 
141
                        UnitNumber:    0,
 
142
                },
 
143
        },
 
144
        &types.VirtualPointingDevice{
 
145
                VirtualDevice: types.VirtualDevice{
 
146
                        DynamicData: types.DynamicData{},
 
147
                        Key:         700,
 
148
                        DeviceInfo: &types.Description{
 
149
                                DynamicData: types.DynamicData{},
 
150
                                Label:       "Pointing device",
 
151
                                Summary:     "Pointing device; Device",
 
152
                        },
 
153
                        Backing: &types.VirtualPointingDeviceDeviceBackingInfo{
 
154
                                VirtualDeviceDeviceBackingInfo: types.VirtualDeviceDeviceBackingInfo{},
 
155
                                HostPointingDevice:             "autodetect",
 
156
                        },
 
157
                        Connectable:   (*types.VirtualDeviceConnectInfo)(nil),
 
158
                        SlotInfo:      nil,
 
159
                        ControllerKey: 300,
 
160
                        UnitNumber:    1,
 
161
                },
 
162
        },
 
163
        &types.VirtualMachineVideoCard{
 
164
                VirtualDevice: types.VirtualDevice{
 
165
                        DynamicData: types.DynamicData{},
 
166
                        Key:         500,
 
167
                        DeviceInfo: &types.Description{
 
168
                                DynamicData: types.DynamicData{},
 
169
                                Label:       "Video card ",
 
170
                                Summary:     "Video card",
 
171
                        },
 
172
                        Backing:       nil,
 
173
                        Connectable:   (*types.VirtualDeviceConnectInfo)(nil),
 
174
                        SlotInfo:      nil,
 
175
                        ControllerKey: 100,
 
176
                        UnitNumber:    0,
 
177
                },
 
178
                VideoRamSizeInKB: 4096,
 
179
                NumDisplays:      1,
 
180
                UseAutoDetect:    types.NewBool(false),
 
181
                Enable3DSupport:  types.NewBool(false),
 
182
                Use3dRenderer:    "automatic",
 
183
        },
 
184
        &types.VirtualMachineVMCIDevice{
 
185
                VirtualDevice: types.VirtualDevice{
 
186
                        DynamicData: types.DynamicData{},
 
187
                        Key:         12000,
 
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",
 
192
                        },
 
193
                        Backing:     nil,
 
194
                        Connectable: (*types.VirtualDeviceConnectInfo)(nil),
 
195
                        SlotInfo: &types.VirtualDevicePciBusSlotInfo{
 
196
                                VirtualDeviceBusSlotInfo: types.VirtualDeviceBusSlotInfo{},
 
197
                                PciSlotNumber:            33,
 
198
                        },
 
199
                        ControllerKey: 100,
 
200
                        UnitNumber:    17,
 
201
                },
 
202
                Id: 1754519335,
 
203
                AllowUnrestrictedCommunication: types.NewBool(false),
 
204
        },
 
205
        &types.VirtualLsiLogicController{
 
206
                VirtualSCSIController: types.VirtualSCSIController{
 
207
                        VirtualController: types.VirtualController{
 
208
                                VirtualDevice: types.VirtualDevice{
 
209
                                        DynamicData: types.DynamicData{},
 
210
                                        Key:         1000,
 
211
                                        DeviceInfo: &types.Description{
 
212
                                                DynamicData: types.DynamicData{},
 
213
                                                Label:       "SCSI controller 0",
 
214
                                                Summary:     "LSI Logic",
 
215
                                        },
 
216
                                        Backing:       nil,
 
217
                                        Connectable:   (*types.VirtualDeviceConnectInfo)(nil),
 
218
                                        SlotInfo:      nil,
 
219
                                        ControllerKey: 100,
 
220
                                        UnitNumber:    3,
 
221
                                },
 
222
                                BusNumber: 0,
 
223
                                Device:    nil,
 
224
                        },
 
225
                        HotAddRemove:       types.NewBool(true),
 
226
                        SharedBus:          "noSharing",
 
227
                        ScsiCtlrUnitNumber: 7,
 
228
                },
 
229
        },
 
230
        &types.VirtualCdrom{
 
231
                VirtualDevice: types.VirtualDevice{
 
232
                        DynamicData: types.DynamicData{},
 
233
                        Key:         3001,
 
234
                        DeviceInfo: &types.Description{
 
235
                                DynamicData: types.DynamicData{},
 
236
                                Label:       "CD/DVD drive 1",
 
237
                                Summary:     "ISO [datastore1] ttylinux-pc_i486-16.1.iso",
 
238
                        },
 
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"},
 
244
                                        BackingObjectId:          "",
 
245
                                },
 
246
                        },
 
247
                        Connectable: &types.VirtualDeviceConnectInfo{
 
248
                                DynamicData:       types.DynamicData{},
 
249
                                StartConnected:    true,
 
250
                                AllowGuestControl: true,
 
251
                                Connected:         false,
 
252
                                Status:            "untried",
 
253
                        },
 
254
                        SlotInfo:      nil,
 
255
                        ControllerKey: 200,
 
256
                        UnitNumber:    1,
 
257
                },
 
258
        },
 
259
        &types.VirtualDisk{
 
260
                VirtualDevice: types.VirtualDevice{
 
261
                        DynamicData: types.DynamicData{},
 
262
                        Key:         3000,
 
263
                        DeviceInfo: &types.Description{
 
264
                                DynamicData: types.DynamicData{},
 
265
                                Label:       "Hard disk 1",
 
266
                                Summary:     "30,720 KB",
 
267
                        },
 
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",
 
274
                                },
 
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",
 
282
                                ChangeId:        "",
 
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",
 
289
                                        },
 
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",
 
297
                                        ChangeId:        "",
 
298
                                        Parent:          (*types.VirtualDiskFlatVer2BackingInfo)(nil),
 
299
                                        DeltaDiskFormat: "",
 
300
                                        DigestEnabled:   types.NewBool(false),
 
301
                                        DeltaGrainSize:  0,
 
302
                                },
 
303
                                DeltaDiskFormat: "redoLogFormat",
 
304
                                DigestEnabled:   types.NewBool(false),
 
305
                                DeltaGrainSize:  0,
 
306
                        },
 
307
                        Connectable:   (*types.VirtualDeviceConnectInfo)(nil),
 
308
                        SlotInfo:      nil,
 
309
                        ControllerKey: 200,
 
310
                        UnitNumber:    0,
 
311
                },
 
312
                CapacityInKB:    30720,
 
313
                CapacityInBytes: 31457280,
 
314
                Shares: &types.SharesInfo{
 
315
                        DynamicData: types.DynamicData{},
 
316
                        Shares:      1000,
 
317
                        Level:       "normal",
 
318
                },
 
319
                StorageIOAllocation: &types.StorageIOAllocationInfo{
 
320
                        DynamicData: types.DynamicData{},
 
321
                        Limit:       -1,
 
322
                        Shares: &types.SharesInfo{
 
323
                                DynamicData: types.DynamicData{},
 
324
                                Shares:      1000,
 
325
                                Level:       "normal",
 
326
                        },
 
327
                        Reservation: 0,
 
328
                },
 
329
                DiskObjectId:          "3-3000",
 
330
                VFlashCacheConfigInfo: (*types.VirtualDiskVFlashCacheConfigInfo)(nil),
 
331
        },
 
332
        &types.VirtualDisk{
 
333
                VirtualDevice: types.VirtualDevice{
 
334
                        DynamicData: types.DynamicData{},
 
335
                        Key:         3002,
 
336
                        DeviceInfo: &types.Description{
 
337
                                DynamicData: types.DynamicData{},
 
338
                                Label:       "Hard disk 2",
 
339
                                Summary:     "10,000,000 KB",
 
340
                        },
 
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",
 
347
                                },
 
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",
 
355
                                ChangeId:        "",
 
356
                                Parent:          (*types.VirtualDiskFlatVer2BackingInfo)(nil),
 
357
                                DeltaDiskFormat: "",
 
358
                                DigestEnabled:   types.NewBool(false),
 
359
                                DeltaGrainSize:  0,
 
360
                        },
 
361
                        Connectable:   (*types.VirtualDeviceConnectInfo)(nil),
 
362
                        SlotInfo:      nil,
 
363
                        ControllerKey: 201,
 
364
                        UnitNumber:    0,
 
365
                },
 
366
                CapacityInKB:    10000000,
 
367
                CapacityInBytes: 10240000000,
 
368
                Shares: &types.SharesInfo{
 
369
                        DynamicData: types.DynamicData{},
 
370
                        Shares:      1000,
 
371
                        Level:       "normal",
 
372
                },
 
373
                StorageIOAllocation: &types.StorageIOAllocationInfo{
 
374
                        DynamicData: types.DynamicData{},
 
375
                        Limit:       -1,
 
376
                        Shares: &types.SharesInfo{
 
377
                                DynamicData: types.DynamicData{},
 
378
                                Shares:      1000,
 
379
                                Level:       "normal",
 
380
                        },
 
381
                        Reservation: 0,
 
382
                },
 
383
                DiskObjectId:          "3-3002",
 
384
                VFlashCacheConfigInfo: (*types.VirtualDiskVFlashCacheConfigInfo)(nil),
 
385
        },
 
386
        &types.VirtualE1000{
 
387
                VirtualEthernetCard: types.VirtualEthernetCard{
 
388
                        VirtualDevice: types.VirtualDevice{
 
389
                                DynamicData: types.DynamicData{},
 
390
                                Key:         4000,
 
391
                                DeviceInfo: &types.Description{
 
392
                                        DynamicData: types.DynamicData{},
 
393
                                        Label:       "Network adapter 1",
 
394
                                        Summary:     "VM Network",
 
395
                                },
 
396
                                Backing: &types.VirtualEthernetCardNetworkBackingInfo{
 
397
                                        VirtualDeviceDeviceBackingInfo: types.VirtualDeviceDeviceBackingInfo{
 
398
                                                VirtualDeviceBackingInfo: types.VirtualDeviceBackingInfo{},
 
399
                                                DeviceName:               "VM Network",
 
400
                                                UseAutoDetect:            types.NewBool(false),
 
401
                                        },
 
402
                                        Network:           &types.ManagedObjectReference{Type: "Network", Value: "HaNetwork-VM Network"},
 
403
                                        InPassthroughMode: types.NewBool(false),
 
404
                                },
 
405
                                Connectable: &types.VirtualDeviceConnectInfo{
 
406
                                        DynamicData:       types.DynamicData{},
 
407
                                        StartConnected:    true,
 
408
                                        AllowGuestControl: true,
 
409
                                        Connected:         false,
 
410
                                        Status:            "untried",
 
411
                                },
 
412
                                SlotInfo: &types.VirtualDevicePciBusSlotInfo{
 
413
                                        VirtualDeviceBusSlotInfo: types.VirtualDeviceBusSlotInfo{},
 
414
                                        PciSlotNumber:            32,
 
415
                                },
 
416
                                ControllerKey: 100,
 
417
                                UnitNumber:    7,
 
418
                        },
 
419
                        AddressType:      "generated",
 
420
                        MacAddress:       "00:0c:29:93:d7:27",
 
421
                        WakeOnLanEnabled: types.NewBool(true),
 
422
                },
 
423
        },
 
424
        &types.VirtualSerialPort{
 
425
                VirtualDevice: types.VirtualDevice{
 
426
                        DynamicData: types.DynamicData{},
 
427
                        Key:         9000,
 
428
                        DeviceInfo: &types.Description{
 
429
                                DynamicData: types.DynamicData{},
 
430
                                Label:       "Serial port 1",
 
431
                                Summary:     "Remote localhost:0",
 
432
                        },
 
433
                        Backing: &types.VirtualSerialPortURIBackingInfo{
 
434
                                VirtualDeviceURIBackingInfo: types.VirtualDeviceURIBackingInfo{
 
435
                                        VirtualDeviceBackingInfo: types.VirtualDeviceBackingInfo{},
 
436
                                        ServiceURI:               "localhost:0",
 
437
                                        Direction:                "client",
 
438
                                        ProxyURI:                 "",
 
439
                                },
 
440
                        },
 
441
                        Connectable: &types.VirtualDeviceConnectInfo{
 
442
                                DynamicData:       types.DynamicData{},
 
443
                                StartConnected:    true,
 
444
                                AllowGuestControl: true,
 
445
                                Connected:         false,
 
446
                                Status:            "untried",
 
447
                        },
 
448
                        SlotInfo:      nil,
 
449
                        ControllerKey: 400,
 
450
                        UnitNumber:    0,
 
451
                },
 
452
                YieldOnPoll: true,
 
453
        },
 
454
})
 
455
 
 
456
func TestSelectByType(t *testing.T) {
 
457
        tests := []struct {
 
458
                dtype  types.BaseVirtualDevice
 
459
                expect int
 
460
        }{
 
461
                {
 
462
                        (*types.VirtualCdrom)(nil),
 
463
                        1,
 
464
                },
 
465
                {
 
466
                        (*types.VirtualEthernetCard)(nil),
 
467
                        1,
 
468
                },
 
469
                {
 
470
                        (*types.VirtualDisk)(nil),
 
471
                        2,
 
472
                },
 
473
                {
 
474
                        (*types.VirtualController)(nil),
 
475
                        6,
 
476
                },
 
477
                {
 
478
                        (*types.VirtualIDEController)(nil),
 
479
                        2,
 
480
                },
 
481
                {
 
482
                        (*types.VirtualSCSIController)(nil),
 
483
                        1,
 
484
                },
 
485
                {
 
486
                        (*types.VirtualLsiLogicController)(nil),
 
487
                        1,
 
488
                },
 
489
                {
 
490
                        (*types.ParaVirtualSCSIController)(nil),
 
491
                        0,
 
492
                },
 
493
        }
 
494
 
 
495
        for _, test := range tests {
 
496
                d := devices.SelectByType(test.dtype)
 
497
 
 
498
                if len(d) != test.expect {
 
499
                        t.Errorf("%#v has %d", test.dtype, len(devices))
 
500
                }
 
501
        }
 
502
}
 
503
 
 
504
func TestSelectByBackingInfo(t *testing.T) {
 
505
        tests := []types.BaseVirtualDeviceBackingInfo{
 
506
                &types.VirtualEthernetCardNetworkBackingInfo{
 
507
                        VirtualDeviceDeviceBackingInfo: types.VirtualDeviceDeviceBackingInfo{
 
508
                                DeviceName: "VM Network",
 
509
                        },
 
510
                },
 
511
                &types.VirtualDiskFlatVer2BackingInfo{
 
512
                        VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
 
513
                                FileName: "[datastore1] bar/bar.vmdk",
 
514
                        },
 
515
                },
 
516
                &types.VirtualDiskFlatVer2BackingInfo{
 
517
                        Parent: &types.VirtualDiskFlatVer2BackingInfo{
 
518
                                VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
 
519
                                        FileName: "[datastore1] ttylinux.vmdk",
 
520
                                },
 
521
                        },
 
522
                },
 
523
                &types.VirtualCdromIsoBackingInfo{
 
524
                        VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
 
525
                                VirtualDeviceBackingInfo: types.VirtualDeviceBackingInfo{},
 
526
                                FileName:                 "[datastore1] foo.iso",
 
527
                        },
 
528
                },
 
529
        }
 
530
 
 
531
        for _, test := range tests {
 
532
                l := devices.SelectByBackingInfo(test)
 
533
 
 
534
                if len(l) != 1 {
 
535
                        t.Errorf("Expected 1, got %d: %#v", len(l), test)
 
536
                }
 
537
        }
 
538
}
 
539
 
 
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))
 
546
                }
 
547
        }
 
548
 
 
549
        d := devices.Find("enoent")
 
550
        if d != nil {
 
551
                t.Errorf("unexpected: %#v", d)
 
552
        }
 
553
}
 
554
 
 
555
func TestFindController(t *testing.T) {
 
556
        for _, name := range []string{"", "ide-200"} {
 
557
                _, err := devices.FindIDEController(name)
 
558
                if err != nil {
 
559
                        t.Error(err)
 
560
                }
 
561
        }
 
562
 
 
563
        for _, name := range []string{"", "lsilogic-1000"} {
 
564
                _, err := devices.FindSCSIController(name)
 
565
                if err != nil {
 
566
                        t.Error(err)
 
567
                }
 
568
        }
 
569
 
 
570
        fns := []func() error{
 
571
                func() error {
 
572
                        _, err := devices.FindIDEController("lsilogic-1000")
 
573
                        return err
 
574
                },
 
575
                func() error {
 
576
                        _, err := devices.FindSCSIController("ide-200")
 
577
                        return err
 
578
                },
 
579
        }
 
580
 
 
581
        for _, f := range fns {
 
582
                err := f()
 
583
                if err == nil {
 
584
                        t.Error("should fail")
 
585
                }
 
586
        }
 
587
}
 
588
 
 
589
func TestPickController(t *testing.T) {
 
590
        list := devices
 
591
 
 
592
        tests := []struct {
 
593
                ctype types.BaseVirtualController
 
594
                key   int
 
595
                unit  int
 
596
        }{
 
597
                {
 
598
                        (*types.VirtualIDEController)(nil), 201, 1,
 
599
                },
 
600
                {
 
601
                        (*types.VirtualSCSIController)(nil), 1000, 0,
 
602
                },
 
603
                {
 
604
                        (*types.VirtualSCSIController)(nil), 1000, 1,
 
605
                },
 
606
        }
 
607
 
 
608
        for _, test := range tests {
 
609
                c := list.PickController(test.ctype).GetVirtualController()
 
610
 
 
611
                key := c.Key
 
612
                if key != test.key {
 
613
                        t.Errorf("expected controller key: %d, got: %d\n", test.key, key)
 
614
                }
 
615
 
 
616
                unit := list.newUnitNumber(c)
 
617
                if unit != test.unit {
 
618
                        t.Errorf("expected unit number: %d, got: %d\n", test.unit, unit)
 
619
                }
 
620
 
 
621
                dev := &types.VirtualDevice{
 
622
                        Key:           rand.Int(),
 
623
                        UnitNumber:    unit,
 
624
                        ControllerKey: key,
 
625
                }
 
626
 
 
627
                list = append(list, dev)
 
628
                c.Device = append(c.Device, dev.Key)
 
629
        }
 
630
 
 
631
        if list.PickController((*types.VirtualIDEController)(nil)) != nil {
 
632
                t.Error("should be nil")
 
633
        }
 
634
 
 
635
        if list.PickController((*types.VirtualSCSIController)(nil)) == nil {
 
636
                t.Errorf("should not be nil")
 
637
        }
 
638
}
 
639
 
 
640
func TestCreateSCSIController(t *testing.T) {
 
641
        for _, l := range []VirtualDeviceList{SCSIControllerTypes(), devices} {
 
642
                _, err := l.CreateSCSIController("enoent")
 
643
                if err == nil {
 
644
                        t.Error("should fail")
 
645
                }
 
646
 
 
647
                for _, name := range []string{"", "scsi", "pvscsi", "buslogic", "lsilogic", "lsilogic-sas"} {
 
648
                        _, err = l.CreateSCSIController(name)
 
649
                        if err != nil {
 
650
                                t.Error(err)
 
651
                        }
 
652
                }
 
653
        }
 
654
}
 
655
 
 
656
func TestCreateEthernetCard(t *testing.T) {
 
657
        _, err := EthernetCardTypes().CreateEthernetCard("enoent", nil)
 
658
        if err == nil {
 
659
                t.Error("should fail")
 
660
        }
 
661
 
 
662
        for _, name := range []string{"", "e1000", "e1000e", "vmxnet3"} {
 
663
                _, err := EthernetCardTypes().CreateEthernetCard(name, nil)
 
664
                if err != nil {
 
665
                        t.Error(err)
 
666
                }
 
667
        }
 
668
}
 
669
 
 
670
func TestCdrom(t *testing.T) {
 
671
        c, err := devices.FindCdrom("")
 
672
        if err != nil {
 
673
                t.Error(err)
 
674
        }
 
675
 
 
676
        d := devices.Find(devices.Name(c))
 
677
 
 
678
        if c.Key != d.GetVirtualDevice().Key {
 
679
                t.Error("device key mismatch")
 
680
        }
 
681
 
 
682
        for _, name := range []string{"enoent", "ide-200"} {
 
683
                c, err = devices.FindCdrom(name)
 
684
                if err == nil {
 
685
                        t.Errorf("FindCdrom(%s) should fail", name)
 
686
                }
 
687
        }
 
688
 
 
689
        c, err = devices.Select(func(device types.BaseVirtualDevice) bool {
 
690
                if _, ok := device.(*types.VirtualCdrom); ok {
 
691
                        return false
 
692
                }
 
693
                return true
 
694
        }).FindCdrom("")
 
695
 
 
696
        if err == nil {
 
697
                t.Error("FindCdrom('') should fail")
 
698
        }
 
699
}
 
700
 
 
701
func TestSerialPort(t *testing.T) {
 
702
        device, err := devices.CreateSerialPort()
 
703
        if err != nil {
 
704
                t.Error(err)
 
705
        }
 
706
        devices.ConnectSerialPort(device, "telnet://:33233", false)
 
707
}
 
708
 
 
709
func TestPrimaryMacAddress(t *testing.T) {
 
710
        expect := "00:0c:29:93:d7:27"
 
711
        mac := devices.PrimaryMacAddress()
 
712
        if expect != mac {
 
713
                t.Errorf("expected: %s, got: %s", expect, mac)
 
714
        }
 
715
}
 
716
 
 
717
func TestBootOrder(t *testing.T) {
 
718
        o := []string{DeviceTypeEthernet, DeviceTypeCdrom, DeviceTypeFloppy, DeviceTypeDisk}
 
719
        list := devices
 
720
 
 
721
        n := 4 // 1 ethernet, 1 cdrom, 2 disk
 
722
        order := list.BootOrder(o)
 
723
        if len(order) != n {
 
724
                t.Errorf("expected %d boot devices, got: %d", n, len(order))
 
725
        }
 
726
 
 
727
        list = list.SelectBootOrder(order)
 
728
        if len(list) != n {
 
729
                t.Errorf("expected %d boot devices, got: %d", n, len(list))
 
730
        }
 
731
 
 
732
        // test lookup by name
 
733
        var names []string
 
734
        for _, x := range list {
 
735
                names = append(names, list.Name(x))
 
736
        }
 
737
 
 
738
        order = list.BootOrder(names)
 
739
        if len(order) != n {
 
740
                t.Errorf("expected %d boot devices, got: %d", n, len(order))
 
741
        }
 
742
 
 
743
        if !reflect.DeepEqual(list, list.SelectBootOrder(order)) {
 
744
                t.Error("boot order mismatch")
 
745
        }
 
746
 
 
747
        // remove disks
 
748
        list = list.Select(func(device types.BaseVirtualDevice) bool {
 
749
                if _, ok := device.(*types.VirtualDisk); ok {
 
750
                        return false
 
751
                }
 
752
                return true
 
753
        })
 
754
 
 
755
        n = 2 // 1 ethernet, 1 cdrom
 
756
        order = list.BootOrder(o)
 
757
        if len(order) != n {
 
758
                t.Errorf("expected %d boot devices, got: %d", n, len(order))
 
759
        }
 
760
 
 
761
        if !reflect.DeepEqual(list, list.SelectBootOrder(order)) {
 
762
                t.Error("boot order mismatch")
 
763
        }
 
764
 
 
765
        if len(list.BootOrder([]string{DeviceTypeDisk})) != 0 {
 
766
                t.Error("expected 0 disks")
 
767
        }
 
768
}
 
769
 
 
770
func TestName(t *testing.T) {
 
771
        tests := []struct {
 
772
                device types.BaseVirtualDevice
 
773
                expect string
 
774
        }{
 
775
                {
 
776
                        &types.VirtualCdrom{},
 
777
                        "cdrom-0",
 
778
                },
 
779
                {
 
780
                        &types.VirtualDisk{},
 
781
                        "disk-0-0",
 
782
                },
 
783
                {
 
784
                        &types.VirtualFloppy{},
 
785
                        "floppy-0",
 
786
                },
 
787
                {
 
788
                        &types.VirtualIDEController{},
 
789
                        "ide-0",
 
790
                },
 
791
                {
 
792
                        &types.VirtualMachineVideoCard{},
 
793
                        "video-0",
 
794
                },
 
795
                {
 
796
                        &types.VirtualPointingDevice{},
 
797
                        "pointing-0",
 
798
                },
 
799
                {
 
800
                        &types.ParaVirtualSCSIController{},
 
801
                        "pvscsi-0",
 
802
                },
 
803
                {
 
804
                        &types.VirtualSerialPort{},
 
805
                        "serialport-0",
 
806
                },
 
807
                {
 
808
                        &types.VirtualE1000{
 
809
                                VirtualEthernetCard: types.VirtualEthernetCard{
 
810
                                        VirtualDevice: types.VirtualDevice{
 
811
                                                UnitNumber: 7,
 
812
                                        },
 
813
                                },
 
814
                        },
 
815
                        "ethernet-0",
 
816
                },
 
817
        }
 
818
 
 
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)
 
823
                }
 
824
        }
 
825
}