rbvmomi 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/runner.rb ADDED
@@ -0,0 +1,3 @@
1
+ %w(deserialization deserialization emit_request parse_response).each do |x|
2
+ require "test_#{x}"
3
+ end
data/test/test.rb ADDED
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rbvmomi'
3
+ include RbVmomi
4
+
5
+ vim = RbVmomi.connect ENV['RBVMOMI_URI']
6
+
7
+ rootFolder = vim.serviceInstance.RetrieveServiceContent.rootFolder
8
+
9
+ dc = rootFolder.childEntity.first
10
+ vmFolder = dc.vmFolder
11
+ vms = vmFolder.childEntity
12
+ hosts = dc.hostFolder.childEntity
13
+ rp = hosts.first.resourcePool
14
+
15
+ vm_cfg = {
16
+ name: 'vm',
17
+ guestId: 'otherGuest64',
18
+ files: { vmPathName: '[datastore1]' },
19
+ numCPUs: 2,
20
+ memoryMB: 3072,
21
+ deviceChange: [
22
+ {
23
+ operation: :add,
24
+ device: VIM.VirtualLsiLogicController(
25
+ key: 1000,
26
+ busNumber: 0,
27
+ sharedBus: :noSharing,
28
+ )
29
+ }, {
30
+ operation: :add,
31
+ fileOperation: :create,
32
+ device: VIM.VirtualDisk(
33
+ key: 0,
34
+ backing: VIM.VirtualDiskFlatVer2BackingInfo(
35
+ fileName: '[datastore1]',
36
+ diskMode: :persistent,
37
+ thinProvisioned: true,
38
+ ),
39
+ controllerKey: 1000,
40
+ unitNumber: 0,
41
+ capacityInKB: 4000000,
42
+ )
43
+ }, {
44
+ operation: :add,
45
+ device: VIM.VirtualE1000(
46
+ key: 0,
47
+ deviceInfo: {
48
+ label: 'Network Adapter 1',
49
+ summary: 'VM Network',
50
+ },
51
+ backing: VIM.VirtualEthernetCardNetworkBackingInfo(
52
+ deviceName: 'VM Network',
53
+ ),
54
+ addressType: 'generated'
55
+ )
56
+ }
57
+ ],
58
+ extraConfig: [
59
+ {
60
+ key: 'bios.bootOrder',
61
+ value: XSD.string('ethernet0')
62
+ }
63
+ ]
64
+ }
65
+
66
+ N = 2
67
+ create_tasks = (0...N).map { vmFolder.CreateVM_Task(:config => vm_cfg, :pool => rp) }
68
+ destroy_tasks = create_tasks.map { |x| x.wait_task.Destroy_Task }
69
+ destroy_tasks.each { |x| x.wait_task }
@@ -0,0 +1,324 @@
1
+ require 'test/unit'
2
+ require 'rbvmomi'
3
+ include RbVmomi
4
+
5
+ class DeserializationTest < Test::Unit::TestCase
6
+ def setup
7
+ @soap = RbVmomi::Soap.new(ns: 'urn:vim25', rev: '4.0')
8
+ end
9
+
10
+ def check str, expected, type
11
+ got = @soap.xml2obj Nokogiri(str).root, type
12
+
13
+ puts "expected:"
14
+ pp expected
15
+ puts
16
+ puts "got:"
17
+ pp got
18
+ puts
19
+
20
+ assert_equal expected, got
21
+ end
22
+
23
+ def test_moref
24
+ check <<-EOS, VIM.Folder(nil, 'ha-folder-root'), 'Folder'
25
+ <root type="Folder">ha-folder-root</root>
26
+ EOS
27
+
28
+ check <<-EOS, VIM.Datacenter(nil, 'ha-datacenter'), 'ManagedObjectReference'
29
+ <ManagedObjectReference type="Datacenter" xsi:type="ManagedObjectReference">ha-datacenter</ManagedObjectReference>
30
+ EOS
31
+ end
32
+
33
+ def test_dataobject
34
+ obj = VIM.DatastoreSummary(
35
+ capacity: 1000,
36
+ accessible: true,
37
+ datastore: VIM.Datastore(nil, "foo"),
38
+ freeSpace: 31,
39
+ multipleHostAccess: false,
40
+ name: "baz",
41
+ type: "VMFS",
42
+ url: "http://foo/",
43
+ dynamicProperty: []
44
+ )
45
+
46
+ check <<-EOS, obj, 'DatastoreSummary'
47
+ <root>
48
+ <capacity>1000</capacity>
49
+ <accessible>1</accessible>
50
+ <datastore type="Datastore">foo</datastore>
51
+ <freeSpace>31</freeSpace>
52
+ <multipleHostAccess>false</multipleHostAccess>
53
+ <name>baz</name>
54
+ <type>VMFS</type>
55
+ <url>http://foo/</url>
56
+ </root>
57
+ EOS
58
+ end
59
+
60
+ def test_enum
61
+ check <<-EOS, 'add', 'ConfigSpecOperation'
62
+ <root>add</root>
63
+ EOS
64
+ end
65
+
66
+ def test_array
67
+ obj = VIM.ObjectContent(
68
+ obj: VIM.Folder(nil, 'ha-folder-root'),
69
+ dynamicProperty: [],
70
+ missingSet: [],
71
+ propSet: [
72
+ VIM.DynamicProperty(
73
+ name: 'childEntity',
74
+ val: [
75
+ VIM.Datacenter(nil, 'ha-datacenter')
76
+ ]
77
+ )
78
+ ]
79
+ )
80
+
81
+ check <<-EOS, obj, 'ObjectContent'
82
+ <root xmlns:xsi="#{RbVmomi::Soap::NS_XSI}">
83
+ <obj type="Folder">ha-folder-root</obj>
84
+ <propSet>
85
+ <name>childEntity</name>
86
+ <val xsi:type="ArrayOfManagedObjectReference">
87
+ <ManagedObjectReference type="Datacenter" xsi:type="ManagedObjectReference">ha-datacenter</ManagedObjectReference>
88
+ </val>
89
+ </propSet>
90
+ </root>
91
+ EOS
92
+ end
93
+
94
+ def test_array2
95
+ obj = VIM.DVPortStatus(
96
+ dynamicProperty: [],
97
+ linkUp: true,
98
+ blocked: false,
99
+ vmDirectPathGen2InactiveReasonNetwork: [],
100
+ vmDirectPathGen2InactiveReasonOther: [],
101
+ vlanIds: [
102
+ VIM::NumericRange(dynamicProperty: [], start: 5, end: 7),
103
+ VIM::NumericRange(dynamicProperty: [], start: 10, end: 20),
104
+ ]
105
+ )
106
+
107
+ check <<-EOS, obj, 'DVPortStatus'
108
+ <root>
109
+ <linkUp>1</linkUp>
110
+ <blocked>false</blocked>
111
+ <vlanIds>
112
+ <start>5</start>
113
+ <end>7</end>
114
+ </vlanIds>
115
+ <vlanIds>
116
+ <start>10</start>
117
+ <end>20</end>
118
+ </vlanIds>
119
+ </root>
120
+ EOS
121
+ end
122
+
123
+ def test_empty_array
124
+ obj = VIM.DVPortStatus(
125
+ dynamicProperty: [],
126
+ vmDirectPathGen2InactiveReasonNetwork: [],
127
+ vmDirectPathGen2InactiveReasonOther: [],
128
+ linkUp: true,
129
+ blocked: false,
130
+ vlanIds: []
131
+ )
132
+
133
+ check <<-EOS, obj, 'DVPortStatus'
134
+ <root>
135
+ <linkUp>1</linkUp>
136
+ <blocked>false</blocked>
137
+ </root>
138
+ EOS
139
+ end
140
+
141
+ def test_fault
142
+ obj = VIM.LocalizedMethodFault(
143
+ localizedMessage: "The attempted operation cannot be performed in the current state (Powered off).",
144
+ fault: VIM.InvalidPowerState(
145
+ requestedState: 'poweredOn',
146
+ existingState: 'poweredOff',
147
+ faultMessage: []
148
+ )
149
+ )
150
+
151
+ check <<-EOS, obj, "LocalizedMethodFault"
152
+ <error xmlns:xsi="#{RbVmomi::Soap::NS_XSI}">
153
+ <fault xsi:type="InvalidPowerState">
154
+ <requestedState>poweredOn</requestedState>
155
+ <existingState>poweredOff</existingState>
156
+ </fault>
157
+ <localizedMessage>The attempted operation cannot be performed in the current state (Powered off).</localizedMessage>
158
+ </error>
159
+ EOS
160
+ end
161
+
162
+ def test_wait_for_updates
163
+ obj = VIM.UpdateSet(
164
+ version: '7',
165
+ dynamicProperty: [],
166
+ filterSet: [
167
+ VIM.PropertyFilterUpdate(
168
+ dynamicProperty: [],
169
+ filter: VIM.PropertyFilter(nil, "session[528BA5EB-335B-4AF6-B49C-6160CF5E8D5B]71E3AC7E-7927-4D9E-8BC3-522769F22DAF"),
170
+ missingSet: [],
171
+ objectSet: [
172
+ VIM.ObjectUpdate(
173
+ dynamicProperty: [],
174
+ kind: 'enter',
175
+ obj: VIM.VirtualMachine(nil, 'vm-1106'),
176
+ missingSet: [],
177
+ changeSet: [
178
+ VIM.PropertyChange(
179
+ dynamicProperty: [],
180
+ name: 'runtime.powerState',
181
+ op: 'assign',
182
+ val: 'poweredOn'
183
+ )
184
+ ]
185
+ )
186
+ ]
187
+ )
188
+ ]
189
+ )
190
+
191
+ check <<-EOS, obj, "UpdateSet"
192
+ <returnval xmlns:xsi="#{RbVmomi::Soap::NS_XSI}">
193
+ <version>7</version>
194
+ <filterSet>
195
+ <filter type="PropertyFilter">session[528BA5EB-335B-4AF6-B49C-6160CF5E8D5B]71E3AC7E-7927-4D9E-8BC3-522769F22DAF</filter>
196
+ <objectSet>
197
+ <kind>enter</kind>
198
+ <obj type="VirtualMachine">vm-1106</obj>
199
+ <changeSet>
200
+ <name>runtime.powerState</name>
201
+ <op>assign</op>
202
+ <val xsi:type="VirtualMachinePowerState">poweredOn</val>
203
+ </changeSet>
204
+ </objectSet>
205
+ </filterSet>
206
+ </returnval>
207
+ EOS
208
+ end
209
+
210
+ def test_binary
211
+ obj = "\x00foo\x01bar\x02baz"
212
+ check <<-EOS, obj, 'xsd:base64Binary'
213
+ <root>AGZvbwFiYXICYmF6</root>
214
+ EOS
215
+ end
216
+
217
+ def test_hba
218
+ obj = VIM::HostBlockHba(
219
+ dynamicProperty: [],
220
+ key: 'key-vim.host.BlockHba-vmhba0',
221
+ device: 'vmhba0',
222
+ bus: 0,
223
+ status: 'unknown',
224
+ model: 'Virtual Machine Chipset',
225
+ driver: 'ata_piix',
226
+ pci: '00:07.1')
227
+
228
+ check <<-EOS, obj, "HostBlockHba"
229
+ <hostBusAdapter xsi:type="HostBlockHba">
230
+ <key>key-vim.host.BlockHba-vmhba0</key>
231
+ <device>vmhba0</device>
232
+ <bus>0</bus>
233
+ <status>unknown</status>
234
+ <model>Virtual Machine Chipset</model>
235
+ <driver>ata_piix</driver>
236
+ <pci>00:07.1</pci>
237
+ </hostBusAdapter>
238
+ EOS
239
+ end
240
+
241
+ def test_runtime_state
242
+ obj = VIM::VirtualMachineDeviceRuntimeInfoVirtualEthernetCardRuntimeState(
243
+ dynamicProperty: [],
244
+ vmDirectPathGen2Active: false,
245
+ vmDirectPathGen2InactiveReasonOther: ["vmNptIncompatibleHost"],
246
+ vmDirectPathGen2InactiveReasonVm: []
247
+ )
248
+ check <<-EOS, obj, 'VirtualMachineDeviceRuntimeInfoDeviceRuntimeState'
249
+ <runtimeState xsi:type="VirtualMachineDeviceRuntimeInfoVirtualEthernetCardRuntimeState" xmlns:xsi="#{RbVmomi::Soap::NS_XSI}">
250
+ <vmDirectPathGen2Active>false</vmDirectPathGen2Active>
251
+ <vmDirectPathGen2InactiveReasonOther>vmNptIncompatibleHost</vmDirectPathGen2InactiveReasonOther>
252
+ </runtimeState>
253
+ EOS
254
+ end
255
+
256
+ def test_runtime_info
257
+ obj = RbVmomi::VIM::VirtualMachineRuntimeInfo(
258
+ bootTime: Time.parse('2010-08-20 05:44:35 UTC'),
259
+ connectionState: "connected",
260
+ device: [RbVmomi::VIM::VirtualMachineDeviceRuntimeInfo(
261
+ dynamicProperty: [],
262
+ key: 4000,
263
+ runtimeState: RbVmomi::VIM::VirtualMachineDeviceRuntimeInfoVirtualEthernetCardRuntimeState(
264
+ dynamicProperty: [],
265
+ vmDirectPathGen2Active: false,
266
+ vmDirectPathGen2InactiveReasonOther: [],
267
+ vmDirectPathGen2InactiveReasonVm: ["vmNptIncompatibleAdapterType"]
268
+ )
269
+ ),
270
+ RbVmomi::VIM::VirtualMachineDeviceRuntimeInfo(
271
+ dynamicProperty: [],
272
+ key: 4001,
273
+ runtimeState: RbVmomi::VIM::VirtualMachineDeviceRuntimeInfoVirtualEthernetCardRuntimeState(
274
+ dynamicProperty: [],
275
+ vmDirectPathGen2Active: false,
276
+ vmDirectPathGen2InactiveReasonOther: ["vmNptIncompatibleHost"],
277
+ vmDirectPathGen2InactiveReasonVm: []
278
+ )
279
+ )],
280
+ dynamicProperty: [],
281
+ faultToleranceState: "notConfigured",
282
+ host: RbVmomi::VIM::HostSystem(nil, "host-32"),
283
+ maxCpuUsage: 5612,
284
+ maxMemoryUsage: 3072,
285
+ memoryOverhead: 128671744,
286
+ numMksConnections: 1,
287
+ powerState: "poweredOn",
288
+ recordReplayState: "inactive",
289
+ suspendInterval: 0,
290
+ toolsInstallerMounted: false
291
+ )
292
+
293
+ check <<-EOS, obj, 'VirtualMachineRuntimeInfo'
294
+ <val xsi:type="VirtualMachineRuntimeInfo" xmlns:xsi="#{RbVmomi::Soap::NS_XSI}">
295
+ <device>
296
+ <runtimeState xsi:type="VirtualMachineDeviceRuntimeInfoVirtualEthernetCardRuntimeState">
297
+ <vmDirectPathGen2Active>false</vmDirectPathGen2Active>
298
+ <vmDirectPathGen2InactiveReasonVm>vmNptIncompatibleAdapterType</vmDirectPathGen2InactiveReasonVm>
299
+ </runtimeState>
300
+ <key>4000</key>
301
+ </device>
302
+ <device>
303
+ <runtimeState xsi:type="VirtualMachineDeviceRuntimeInfoVirtualEthernetCardRuntimeState">
304
+ <vmDirectPathGen2Active>false</vmDirectPathGen2Active>
305
+ <vmDirectPathGen2InactiveReasonOther>vmNptIncompatibleHost</vmDirectPathGen2InactiveReasonOther>
306
+ </runtimeState>
307
+ <key>4001</key>
308
+ </device>
309
+ <host type="HostSystem">host-32</host>
310
+ <connectionState>connected</connectionState>
311
+ <powerState>poweredOn</powerState>
312
+ <faultToleranceState>notConfigured</faultToleranceState>
313
+ <toolsInstallerMounted>false</toolsInstallerMounted>
314
+ <bootTime>2010-08-20T05:44:35.0Z</bootTime>
315
+ <suspendInterval>0</suspendInterval>
316
+ <memoryOverhead>128671744</memoryOverhead>
317
+ <maxCpuUsage>5612</maxCpuUsage>
318
+ <maxMemoryUsage>3072</maxMemoryUsage>
319
+ <numMksConnections>1</numMksConnections>
320
+ <recordReplayState>inactive</recordReplayState>
321
+ </val>
322
+ EOS
323
+ end
324
+ end
@@ -0,0 +1,110 @@
1
+ require 'test/unit'
2
+ require 'rbvmomi'
3
+ include RbVmomi
4
+
5
+ class EmitRequestTest < Test::Unit::TestCase
6
+ MO = VIM::VirtualMachine(nil, "foo")
7
+
8
+ def check desc, str, this, params
9
+ soap = RbVmomi::Soap.new(ns: 'urn:vim25', rev: '4.0')
10
+ xml = Builder::XmlMarkup.new :indent => 2
11
+ soap.emit_request xml, 'root', desc, this, params
12
+
13
+ puts "expected:"
14
+ puts str
15
+ puts
16
+ puts "got:"
17
+ puts xml.target!
18
+ puts
19
+
20
+ assert_equal str, xml.target!
21
+ end
22
+
23
+ def test_string_array
24
+ desc = [
25
+ {
26
+ 'name' => 'blah',
27
+ 'is-array' => true,
28
+ 'is-optional' => true,
29
+ 'wsdl_type' => 'xsd:string',
30
+ }
31
+ ]
32
+
33
+ check desc, <<-EOS, MO, blah: ['a', 'b', 'c']
34
+ <root xmlns="urn:vim25">
35
+ <_this type="VirtualMachine">foo</_this>
36
+ <blah>a</blah>
37
+ <blah>b</blah>
38
+ <blah>c</blah>
39
+ </root>
40
+ EOS
41
+ end
42
+
43
+ def test_required_param
44
+ desc = [
45
+ {
46
+ 'name' => 'blah',
47
+ 'is-array' => false,
48
+ 'is-optional' => false,
49
+ 'wsdl_type' => 'xsd:string',
50
+ }
51
+ ]
52
+
53
+ check desc, <<-EOS, MO, blah: 'a'
54
+ <root xmlns="urn:vim25">
55
+ <_this type="VirtualMachine">foo</_this>
56
+ <blah>a</blah>
57
+ </root>
58
+ EOS
59
+
60
+ assert_raise RuntimeError do
61
+ check desc, <<-EOS, MO, {}
62
+ <root xmlns="urn:vim25">
63
+ <_this type="VirtualMachine">foo</_this>
64
+ </root>
65
+ EOS
66
+ end
67
+ end
68
+
69
+ def test_optional_param
70
+ desc = [
71
+ {
72
+ 'name' => 'blah',
73
+ 'is-array' => false,
74
+ 'is-optional' => true,
75
+ 'wsdl_type' => 'xsd:string',
76
+ }
77
+ ]
78
+
79
+ check desc, <<-EOS, MO, {}
80
+ <root xmlns="urn:vim25">
81
+ <_this type="VirtualMachine">foo</_this>
82
+ </root>
83
+ EOS
84
+ end
85
+
86
+ def test_string_key
87
+ desc = [
88
+ {
89
+ 'name' => 'blah',
90
+ 'is-array' => false,
91
+ 'is-optional' => false,
92
+ 'wsdl_type' => 'xsd:string',
93
+ }
94
+ ]
95
+
96
+ check desc, <<-EOS, MO, 'blah' => 'a'
97
+ <root xmlns="urn:vim25">
98
+ <_this type="VirtualMachine">foo</_this>
99
+ <blah>a</blah>
100
+ </root>
101
+ EOS
102
+ end
103
+
104
+ def disabled_test_required_property
105
+ assert_raise RuntimeError do
106
+ VIM::AboutInfo()
107
+ end
108
+ end
109
+ end
110
+