forked from oVirt/go-ovirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readers_test.go
377 lines (351 loc) · 14 KB
/
readers_test.go
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
//
// Copyright (c) 2017 Joey <majunjiev@gmail.com>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package ovirtsdk
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestXMLClusterReadOne(t *testing.T) {
assert := assert.New(t)
xmlstring := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cluster href="/ovirt-engine/api/clusters/00000002-0002-0002-0002-000000000310" id="00000002-0002-0002-0002-000000000310">
<actions>
<link href="/ovirt-engine/api/clusters/00000002-0002-0002-0002-000000000310/resetemulatedmachine" rel="resetemulatedmachine"/>
</actions>
<name>Default</name>
<description>The default server cluster</description>
<link href="/ovirt-engine/api/clusters/00000002-0002-0002-0002-000000000310/networks" rel="networks"/>
<link href="/ovirt-engine/api/clusters/00000002-0002-0002-0002-000000000310/permissions" rel="permissions"/>
<link href="/ovirt-engine/api/clusters/00000002-0002-0002-0002-000000000310/glustervolumes" rel="glustervolumes"/>
<link href="/ovirt-engine/api/clusters/00000002-0002-0002-0002-000000000310/glusterhooks" rel="glusterhooks"/>
<link href="/ovirt-engine/api/clusters/00000002-0002-0002-0002-000000000310/affinitygroups" rel="affinitygroups"/>
<link href="/ovirt-engine/api/clusters/00000002-0002-0002-0002-000000000310/cpuprofiles" rel="cpuprofiles"/>
<ballooning_enabled>false</ballooning_enabled>
<cpu>
<architecture>x86_64</architecture>
<type>Intel SandyBridge Family</type>
</cpu>
<error_handling>
<on_error>migrate</on_error>
</error_handling>
<fencing_policy>
<enabled>true</enabled>
<skip_if_connectivity_broken>
<enabled>false</enabled>
<threshold>50</threshold>
</skip_if_connectivity_broken>
<skip_if_sd_active>
<enabled>false</enabled>
</skip_if_sd_active>
</fencing_policy>
<gluster_service>false</gluster_service>
<ha_reservation>false</ha_reservation>
<ksm>
<enabled>true</enabled>
<merge_across_nodes>true</merge_across_nodes>
</ksm>
<maintenance_reason_required>false</maintenance_reason_required>
<memory_policy>
<over_commit>
<percent>100</percent>
</over_commit>
<transparent_hugepages>
<enabled>true</enabled>
</transparent_hugepages>
</memory_policy>
<migration>
<auto_converge>inherit</auto_converge>
<bandwidth>
<assignment_method>auto</assignment_method>
</bandwidth>
<compressed>inherit</compressed>
<policy id="80554327-0569-496b-bdeb-fcbbf52b827b"/>
</migration>
<optional_reason>false</optional_reason>
<required_rng_sources>
<required_rng_source>random</required_rng_source>
<required_rng_source>get</required_rng_source>
<required_rng_source>post</required_rng_source>
</required_rng_sources>
<switch_type>legacy</switch_type>
<threads_as_cores>false</threads_as_cores>
<trusted_service>false</trusted_service>
<tunnel_migration>false</tunnel_migration>
<version>
<major>4</major>
<minor>0</minor>
</version>
<virt_service>true</virt_service>
<data_center href="/ovirt-engine/api/datacenters/00000001-0001-0001-0001-0000000002ed" id="00000001-0001-0001-0001-0000000002ed"/>
<scheduling_policy href="/ovirt-engine/api/schedulingpolicies/b4ed2332-a7ac-4d5f-9596-99a439cb2812" id="b4ed2332-a7ac-4d5f-9596-99a439cb2812"/>
</cluster>
`
reader := NewXMLReader([]byte(xmlstring))
cluster, err := XMLClusterReadOne(reader, nil, "")
assert.Nil(err)
assert.NotNil(cluster)
// Cluster>Id
// assert.Equal("00000002-0002-0002-0002-000000000310", *cluster.Id)
// Cluster>Name
name, ok := cluster.Name()
assert.True(ok)
assert.Equal("Default", name, "Name should be `Default`")
// Cluster>CPU>Architecture
cpu, ok := cluster.Cpu()
assert.True(ok)
arch, ok := cpu.Architecture()
assert.True(ok)
assert.Equal(Architecture("x86_64"), arch, "CPU Arch should be `x86_64`")
// Cluster>BallooningEnabled
ballooningEnabled, ok := cluster.BallooningEnabled()
assert.True(ok)
assert.False(ballooningEnabled, "Cluster>BallooningEnabled should be false")
// Cluster>Description
desc, ok := cluster.Description()
assert.True(ok)
assert.Equal("The default server cluster", desc)
// Cluster>ErrorHandling>OnError>MigrateOnError
errorHandling, ok := cluster.ErrorHandling()
assert.True(ok)
errorHandlingOnError, ok := errorHandling.OnError()
assert.Equal(MigrateOnError("migrate"), errorHandlingOnError)
// Cluster>FencingPolicy
fencingPolicy, ok := cluster.FencingPolicy()
assert.True(ok)
// >Enable
fencingPolicyEnabled, ok := fencingPolicy.Enabled()
assert.True(ok)
assert.True(fencingPolicyEnabled, "Cluster>FencingPolicy>Enable should be true")
// >SkipIfConnectivityBroken>Threshold
skipIfConnectiveBroken, ok := fencingPolicy.SkipIfConnectivityBroken()
assert.True(ok)
threshold, ok := skipIfConnectiveBroken.Threshold()
assert.True(ok)
assert.Equal(int64(50), threshold,
"Cluster>FencingPolicy>SkipIfConnectivityBroken>Threshold should be 50")
skipIfSdActive, ok := fencingPolicy.SkipIfSdActive()
assert.True(ok)
sdActiveEnable, ok := skipIfSdActive.Enabled()
assert.True(ok)
assert.False(sdActiveEnable, "Cluster>FencingPolicy>SkipIfSdActive>Enabled should be false")
// Cluster>SchedulingPolicy
schedulingPolicy, ok := cluster.SchedulingPolicy()
assert.True(ok)
schedulingPolicyID, ok := schedulingPolicy.Id()
assert.True(ok)
assert.Equal("b4ed2332-a7ac-4d5f-9596-99a439cb2812", schedulingPolicyID)
schedulingPolicyHref, ok := schedulingPolicy.Href()
assert.True(ok)
assert.Equal("/ovirt-engine/api/schedulingpolicies/b4ed2332-a7ac-4d5f-9596-99a439cb2812", schedulingPolicyHref)
// Cluster Links
glusterhooks, ok := cluster.GlusterHooks()
assert.True(ok)
href, ok := glusterhooks.Href()
assert.True(ok)
assert.Equal("/ovirt-engine/api/clusters/00000002-0002-0002-0002-000000000310/glusterhooks", href)
}
func TestXMLErrorHandlingReadOne(t *testing.T) {
assert := assert.New(t)
xmlstring := `
<error_handling>
<on_error>migrate</on_error>
</error_handling>`
reader := NewXMLReader([]byte(xmlstring))
eh, err := XMLErrorHandlingReadOne(reader, nil, "")
assert.Nil(err)
assert.NotNil(eh)
onError, ok := eh.OnError()
assert.True(ok)
assert.Equal(MigrateOnError("migrate"), onError)
}
func TestXMLCPUReadOne(t *testing.T) {
assert := assert.New(t)
xmlstring := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cpu>
<architecture>x86_64</architecture>
<type>Intel SandyBridge Family</type>
</cpu>
`
reader := NewXMLReader([]byte(xmlstring))
cpu, err := XMLCpuReadOne(reader, nil, "")
assert.Nil(err)
cpuArch, ok := cpu.Architecture()
assert.True(ok)
assert.Equal(Architecture("x86_64"), cpuArch)
cpuType, ok := cpu.Type()
assert.True(ok)
assert.Equal("Intel SandyBridge Family", cpuType)
}
func TestArchitectureReadMany(t *testing.T) {
assert := assert.New(t)
xmlstring := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<architectures>
<architecture>x86_64</architecture>
<architecture>x86</architecture>
<architecture>mips</architecture>
<architecture>powerpc</architecture>
</architectures>
`
reader := NewXMLReader([]byte(xmlstring))
archs, err := XMLArchitectureReadMany(reader, nil)
assert.Nil(err)
assert.Equal([]Architecture{
Architecture("x86_64"), Architecture("x86"),
Architecture("mips"), Architecture("powerpc")}, archs)
}
func TestFaultReadOne(t *testing.T) {
assert := assert.New(t)
xmlstring := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<architectures>
<architecture>x86_64</architecture>
<architecture>x86</architecture>
<architecture>mips</architecture>
<architecture>powerpc</architecture>
</architectures>`
reader := NewXMLReader([]byte(xmlstring))
fault, err := XMLFaultReadOne(reader, nil, "")
assert.Nil(fault)
err2, ok := err.(XMLTagNotMatchError)
assert.True(ok)
assert.Equal("architectures", err2.ActualTag)
assert.Equal("fault", err2.ExpectedTag)
}
// To cover the issue: https://github.com/oVirt/ovirt-engine-sdk-go/issues/121
func TestGlusterBrickReadMany(t *testing.T) {
assert := assert.New(t)
xmlstring := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bricks>
<actions>
<link href="/ovirt-engine/api/clusters/5b6d20ce-00b5-019a-02d2-00000000037c/glustervolumes/c667523c-7b0d-4602-82ff-3a721e760835/glusterbricks/migrate" rel="migrate"/>
<link href="/ovirt-engine/api/clusters/5b6d20ce-00b5-019a-02d2-00000000037c/glustervolumes/c667523c-7b0d-4602-82ff-3a721e760835/glusterbricks/stopmigrate" rel="stopmigrate"/>
<link href="/ovirt-engine/api/clusters/5b6d20ce-00b5-019a-02d2-00000000037c/glustervolumes/c667523c-7b0d-4602-82ff-3a721e760835/glusterbricks/activate" rel="activate"/>
</actions>
<brick href="/ovirt-engine/api/clusters/5b6d20ce-00b5-019a-02d2-00000000037c/glustervolumes/c667523c-7b0d-4602-82ff-3a721e760835/glusterbricks/06de18a8-8534-4ce4-9328-ff487ac1355f" id="06de18a8-8534-4ce4-9328-ff487ac1355f">
<actions>
<link href="/ovirt-engine/api/clusters/5b6d20ce-00b5-019a-02d2-00000000037c/glustervolumes/c667523c-7b0d-4602-82ff-3a721e760835/glusterbricks/06de18a8-8534-4ce4-9328-ff487ac1355f/replace" rel="replace"/>
</actions>
<name>10.1.87.223:/tt112</name>
<link href="/ovirt-engine/api/clusters/5b6d20ce-00b5-019a-02d2-00000000037c/glustervolumes/c667523c-7b0d-4602-82ff-3a721e760835/glusterbricks/06de18a8-8534-4ce4-9328-ff487ac1355f/statistics" rel="statistics"/>
<brick_dir>/tt112</brick_dir>
<server_id>75b39f00-fa03-410c-b29c-db80cf162a87</server_id>
<status>up</status>
<gluster_volume href="/ovirt-engine/api/clusters/5b6d20ce-00b5-019a-02d2-00000000037c/glustervolumes/c667523c-7b0d-4602-82ff-3a721e760835" id="c667523c-7b0d-4602-82ff-3a721e760835"/>
</brick>
</bricks>
`
reader := NewXMLReader([]byte(xmlstring))
bricks, err := XMLGlusterBrickReadMany(reader, nil)
assert.Nil(err)
assert.NotNil(bricks)
assert.Equal(1, len(bricks.Slice()))
}
func TestNetworkReadOne(t *testing.T) {
assert := assert.New(t)
xmlstring := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<network href="/ovirt-engine/api/clusters/ee0a136a-941d-11ea-bdb1-525400d65c05/networks/bf9915f3-c4e8-400b-9d38-17e6772bcd5d" id="bf9915f3-c4e8-400b-9d38-17e6772bcd5d">
<name>ocp</name>
<description/>
<comment/>
<display>false</display>
<dns_resolver_configuration>
<name_servers>
<name_server>8.8.8.8</name_server>
</name_servers>
</dns_resolver_configuration>
<mtu>0</mtu>
<required>true</required>
<status>non_operational</status>
<stp>false</stp>
<usages>
<usage>vm</usage>
</usages>
<vdsm_name>ocp</vdsm_name>
<cluster href="/ovirt-engine/api/clusters/ee0a136a-941d-11ea-bdb1-525400d65c05" id="ee0a136a-941d-11ea-bdb1-525400d65c05"/>
<data_center href="/ovirt-engine/api/datacenters/ee081c4a-941d-11ea-9cf8-525400d65c05" id="ee081c4a-941d-11ea-9cf8-525400d65c05"/>
</network>
`
reader := NewXMLReader([]byte(xmlstring))
network, err := XMLNetworkReadOne(reader, nil, "")
assert.Nil(err)
mtu, ok := network.Mtu()
assert.True(ok)
assert.Equal(int64(0), mtu)
}
func TestNetworkReadMany(t *testing.T) {
assert := assert.New(t)
xmlstring := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<networks>
<network href="/ovirt-engine/api/clusters/ee0a136a-941d-11ea-bdb1-525400d65c05/networks/acfcf2fa-dd36-4047-9da9-5e349516a679" id="acfcf2fa-dd36-4047-9da9-5e349516a679">
<name>lab</name>
<description>Lab network</description>
<comment/>
<display>false</display>
<mtu>0</mtu>
<required>true</required>
<status>non_operational</status>
<stp>false</stp>
<usages>
<usage>vm</usage>
</usages>
<vdsm_name>lab</vdsm_name>
<cluster href="/ovirt-engine/api/clusters/ee0a136a-941d-11ea-bdb1-525400d65c05" id="ee0a136a-941d-11ea-bdb1-525400d65c05"/>
<data_center href="/ovirt-engine/api/datacenters/ee081c4a-941d-11ea-9cf8-525400d65c05" id="ee081c4a-941d-11ea-9cf8-525400d65c05"/>
</network>
<network href="/ovirt-engine/api/clusters/ee0a136a-941d-11ea-bdb1-525400d65c05/networks/bf9915f3-c4e8-400b-9d38-17e6772bcd5d" id="bf9915f3-c4e8-400b-9d38-17e6772bcd5d">
<name>ocp</name>
<description/>
<comment/>
<display>false</display>
<dns_resolver_configuration>
<name_servers>
<name_server>8.8.8.8</name_server>
</name_servers>
</dns_resolver_configuration>
<mtu>0</mtu>
<required>true</required>
<status>non_operational</status>
<stp>false</stp>
<usages>
<usage>vm</usage>
</usages>
<vdsm_name>ocp</vdsm_name>
<cluster href="/ovirt-engine/api/clusters/ee0a136a-941d-11ea-bdb1-525400d65c05" id="ee0a136a-941d-11ea-bdb1-525400d65c05"/>
<data_center href="/ovirt-engine/api/datacenters/ee081c4a-941d-11ea-9cf8-525400d65c05" id="ee081c4a-941d-11ea-9cf8-525400d65c05"/>
</network>
<network href="/ovirt-engine/api/clusters/ee0a136a-941d-11ea-bdb1-525400d65c05/networks/d73e5b4c-f527-4057-b071-65b60302286c" id="d73e5b4c-f527-4057-b071-65b60302286c">
<name>ocpnr</name>
<description/>
<comment/>
<display>false</display>
<mtu>0</mtu>
<required>true</required>
<status>non_operational</status>
<stp>false</stp>
<usages>
<usage>vm</usage>
</usages>
<vdsm_name>ocpnr</vdsm_name>
<cluster href="/ovirt-engine/api/clusters/ee0a136a-941d-11ea-bdb1-525400d65c05" id="ee0a136a-941d-11ea-bdb1-525400d65c05"/>
<data_center href="/ovirt-engine/api/datacenters/ee081c4a-941d-11ea-9cf8-525400d65c05" id="ee081c4a-941d-11ea-9cf8-525400d65c05"/>
</network>
</networks>
`
reader := NewXMLReader([]byte(xmlstring))
networks, err := XMLNetworkReadMany(reader, nil)
assert.Nil(err)
assert.NotNil(networks)
assert.Equal(3, len(networks.Slice()))
}