-
Notifications
You must be signed in to change notification settings - Fork 25
/
test_lpar.py
661 lines (545 loc) · 22.6 KB
/
test_lpar.py
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# Copyright 2016-2017 IBM Corp. All Rights Reserved.
#
# 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.
"""
Unit tests for _lpar module.
"""
from __future__ import absolute_import, print_function
import pytest
import re
import copy
import mock
from zhmcclient import Client, Lpar, HTTPError, StatusTimeout
from zhmcclient_mock import FakedSession, LparActivateHandler, \
LparDeactivateHandler, LparLoadHandler
from tests.common.utils import assert_resources
# Object IDs and names of our faked LPARs:
LPAR1_OID = 'lpar1-oid'
LPAR1_NAME = 'lpar 1'
LPAR2_OID = 'lpar2-oid'
LPAR2_NAME = 'lpar 2'
class TestLpar(object):
"""All tests for Lpar and LparManager classes."""
def setup_method(self):
"""
Set up a faked session, and add a faked CPC in classic mode without any
child resources.
"""
self.session = FakedSession('fake-host', 'fake-hmc', '2.13.1', '1.8')
self.session.retry_timeout_config.status_timeout = 1
self.client = Client(self.session)
self.faked_cpc = self.session.hmc.cpcs.add({
'object-id': 'fake-cpc1-oid',
# object-uri is set up automatically
'parent': None,
'class': 'cpc',
'name': 'fake-cpc1-name',
'description': 'CPC #1 (classic mode)',
'status': 'active',
'dpm-enabled': False,
'is-ensemble-member': False,
'iml-mode': 'lpar',
})
self.cpc = self.client.cpcs.find(name='fake-cpc1-name')
def add_lpar1(self):
"""Add lpar 1 (type linux)."""
faked_lpar = self.faked_cpc.lpars.add({
'object-id': LPAR1_OID,
# object-uri will be automatically set
'parent': self.faked_cpc.uri,
'class': 'logical-partition',
'name': LPAR1_NAME,
'description': 'LPAR #1 (Linux)',
'status': 'operating',
'activation-mode': 'linux',
})
return faked_lpar
def add_lpar2(self):
"""Add lpar 2 (type ssc)."""
faked_lpar = self.faked_cpc.lpars.add({
'object-id': LPAR2_OID,
# object-uri will be automatically set
'parent': self.faked_cpc.uri,
'class': 'logical-partition',
'name': LPAR2_NAME,
'description': 'LPAR #2 (SSC)',
'status': 'operating',
'activation-mode': 'ssc',
})
return faked_lpar
def test_lparmanager_initial_attrs(self):
"""Test initial attributes of LparManager."""
lpar_mgr = self.cpc.lpars
# Verify all public properties of the manager object
assert lpar_mgr.resource_class == Lpar
assert lpar_mgr.session == self.session
assert lpar_mgr.parent == self.cpc
assert lpar_mgr.cpc == self.cpc
# TODO: Test for LparManager.__repr__()
@pytest.mark.parametrize(
"full_properties_kwargs, prop_names", [
(dict(),
['object-uri', 'name', 'status']),
(dict(full_properties=False),
['object-uri', 'name', 'status']),
(dict(full_properties=True),
None),
]
)
def test_lparmanager_list_full_properties(
self, full_properties_kwargs, prop_names):
"""Test LparManager.list() with full_properties."""
# Add two faked LPARs
faked_lpar1 = self.add_lpar1()
faked_lpar2 = self.add_lpar2()
exp_faked_lpars = [faked_lpar1, faked_lpar2]
lpar_mgr = self.cpc.lpars
# Execute the code to be tested
lpars = lpar_mgr.list(**full_properties_kwargs)
assert_resources(lpars, exp_faked_lpars, prop_names)
@pytest.mark.parametrize(
"filter_args, exp_names", [
({'object-id': LPAR1_OID},
[LPAR1_NAME]),
({'object-id': LPAR2_OID},
[LPAR2_NAME]),
({'object-id': [LPAR1_OID, LPAR2_OID]},
[LPAR1_NAME, LPAR2_NAME]),
({'object-id': [LPAR1_OID, LPAR1_OID]},
[LPAR1_NAME]),
({'object-id': LPAR1_OID + 'foo'},
[]),
({'object-id': [LPAR1_OID, LPAR2_OID + 'foo']},
[LPAR1_NAME]),
({'object-id': [LPAR2_OID + 'foo', LPAR1_OID]},
[LPAR1_NAME]),
({'name': LPAR1_NAME},
[LPAR1_NAME]),
({'name': LPAR2_NAME},
[LPAR2_NAME]),
({'name': [LPAR1_NAME, LPAR2_NAME]},
[LPAR1_NAME, LPAR2_NAME]),
({'name': LPAR1_NAME + 'foo'},
[]),
({'name': [LPAR1_NAME, LPAR2_NAME + 'foo']},
[LPAR1_NAME]),
({'name': [LPAR2_NAME + 'foo', LPAR1_NAME]},
[LPAR1_NAME]),
({'name': [LPAR1_NAME, LPAR1_NAME]},
[LPAR1_NAME]),
({'name': '.*lpar 1'},
[LPAR1_NAME]),
({'name': 'lpar 1.*'},
[LPAR1_NAME]),
({'name': 'lpar .'},
[LPAR1_NAME, LPAR2_NAME]),
({'name': '.par 1'},
[LPAR1_NAME]),
({'name': '.+'},
[LPAR1_NAME, LPAR2_NAME]),
({'name': 'lpar 1.+'},
[]),
({'name': '.+lpar 1'},
[]),
({'name': LPAR1_NAME,
'object-id': LPAR1_OID},
[LPAR1_NAME]),
({'name': LPAR1_NAME,
'object-id': LPAR1_OID + 'foo'},
[]),
({'name': LPAR1_NAME + 'foo',
'object-id': LPAR1_OID},
[]),
({'name': LPAR1_NAME + 'foo',
'object-id': LPAR1_OID + 'foo'},
[]),
]
)
def test_lparmanager_list_filter_args(self, filter_args, exp_names):
"""Test LparManager.list() with filter_args."""
# Add two faked LPARs
self.add_lpar1()
self.add_lpar2()
lpar_mgr = self.cpc.lpars
# Execute the code to be tested
lpars = lpar_mgr.list(filter_args=filter_args)
assert len(lpars) == len(exp_names)
if exp_names:
names = [p.properties['name'] for p in lpars]
assert set(names) == set(exp_names)
def test_lpar_repr(self):
"""Test Lpar.__repr__()."""
# Add a faked LPAR
faked_lpar = self.add_lpar1()
lpar_mgr = self.cpc.lpars
lpar = lpar_mgr.find(name=faked_lpar.name)
# Execute the code to be tested
repr_str = repr(lpar)
repr_str = repr_str.replace('\n', '\\n')
# We check just the begin of the string:
assert re.match(r'^{classname}\s+at\s+0x{id:08x}\s+\(\\n.*'.
format(classname=lpar.__class__.__name__,
id=id(lpar)),
repr_str)
@pytest.mark.parametrize(
"lpar_name", [
LPAR1_NAME,
LPAR2_NAME,
]
)
@pytest.mark.parametrize(
"input_props", [
{},
{'description': 'New lpar description'},
{'acceptable-status': ['operating', 'not-operating'],
'description': 'New lpar description'},
{'ssc-master-userid': None,
'ssc-master-pw': None},
]
)
def test_lpar_update_properties(self, input_props, lpar_name):
"""Test Lpar.update_properties()."""
# Add faked lpars
self.add_lpar1()
self.add_lpar2()
lpar_mgr = self.cpc.lpars
lpar = lpar_mgr.find(name=lpar_name)
lpar.pull_full_properties()
saved_properties = copy.deepcopy(lpar.properties)
# Execute the code to be tested
lpar.update_properties(properties=input_props)
# Verify that the resource object already reflects the property
# updates.
for prop_name in saved_properties:
if prop_name in input_props:
exp_prop_value = input_props[prop_name]
else:
exp_prop_value = saved_properties[prop_name]
assert prop_name in lpar.properties
prop_value = lpar.properties[prop_name]
assert prop_value == exp_prop_value
# Refresh the resource object and verify that the resource object
# still reflects the property updates.
lpar.pull_full_properties()
for prop_name in saved_properties:
if prop_name in input_props:
exp_prop_value = input_props[prop_name]
else:
exp_prop_value = saved_properties[prop_name]
assert prop_name in lpar.properties
prop_value = lpar.properties[prop_name]
assert prop_value == exp_prop_value
@pytest.mark.parametrize(
"initial_profile, profile_kwargs, exp_profile, exp_profile_exc", [
('', dict(),
None, HTTPError({'http-status': 500, 'reason': 263})),
(LPAR1_NAME, dict(),
LPAR1_NAME, None),
(LPAR2_NAME, dict(),
None, HTTPError({'http-status': 500, 'reason': 263})),
('', dict(activation_profile_name=LPAR1_NAME),
LPAR1_NAME, None),
(LPAR1_NAME, dict(activation_profile_name=LPAR1_NAME),
LPAR1_NAME, None),
(LPAR2_NAME, dict(activation_profile_name=LPAR1_NAME),
LPAR1_NAME, None),
('', dict(activation_profile_name=LPAR2_NAME),
None, HTTPError({'http-status': 500, 'reason': 263})),
(LPAR1_NAME, dict(activation_profile_name=LPAR2_NAME),
None, HTTPError({'http-status': 500, 'reason': 263})),
(LPAR2_NAME, dict(activation_profile_name=LPAR2_NAME),
None, HTTPError({'http-status': 500, 'reason': 263})),
]
)
@pytest.mark.parametrize(
"initial_status, status_kwargs, act_exp_status, exp_status_exc", [
('not-activated', dict(), # Verify that force has a default
'not-operating', None),
('not-activated', dict(force=False),
'not-operating', None),
('not-activated', dict(force=True),
'not-operating', None),
('not-operating', dict(force=False),
'not-operating', None),
('not-operating', dict(force=True),
'not-operating', None),
('operating', dict(), # Verify that force default is False
'not-operating', HTTPError({'http-status': 500, 'reason': 263})),
('operating', dict(force=False),
'not-operating', HTTPError({'http-status': 500, 'reason': 263})),
('operating', dict(force=True),
'not-operating', None),
('exceptions', dict(force=False),
'not-operating', None),
('exceptions', dict(force=True),
'not-operating', None),
('not-activated', dict(),
'exceptions', StatusTimeout(None, None, None, None)),
('not-activated', dict(allow_status_exceptions=False),
'exceptions', StatusTimeout(None, None, None, None)),
('not-activated', dict(allow_status_exceptions=True),
'exceptions', None),
]
)
@mock.patch.object(LparActivateHandler, 'get_status')
def test_lpar_activate(
self, get_status_mock,
initial_status, status_kwargs, act_exp_status, exp_status_exc,
initial_profile, profile_kwargs, exp_profile, exp_profile_exc):
"""Test Lpar.activate()."""
# Add a faked LPAR
faked_lpar = self.add_lpar1()
faked_lpar.properties['status'] = initial_status
faked_lpar.properties['next-activation-profile-name'] = initial_profile
lpar_mgr = self.cpc.lpars
lpar = lpar_mgr.find(name=faked_lpar.name)
input_kwargs = dict(status_kwargs, **profile_kwargs)
exp_excs = []
if exp_status_exc:
exp_excs.append(exp_status_exc)
if exp_profile_exc:
exp_excs.append(exp_profile_exc)
get_status_mock.return_value = act_exp_status
if exp_excs:
with pytest.raises(Exception) as exc_info:
# Execute the code to be tested
lpar.activate(**input_kwargs)
exc = exc_info.value
exp_exc_classes = [e.__class__ for e in exp_excs]
assert isinstance(exc, tuple(exp_exc_classes))
if isinstance(exc, HTTPError):
exp_httperror = [e for e in exp_excs
if isinstance(e, HTTPError)][0]
assert exc.http_status == exp_httperror.http_status
assert exc.reason == exp_httperror.reason
else:
# Execute the code to be tested.
ret = lpar.activate(**input_kwargs)
assert ret is None
lpar.pull_full_properties()
status = lpar.get_property('status')
assert status == act_exp_status
last_profile_name = lpar.get_property(
'last-used-activation-profile')
assert last_profile_name == exp_profile
@pytest.mark.parametrize(
"initial_status, input_kwargs, act_exp_status, exp_status_exc", [
('not-activated', dict(), # Verify that force has a default
'not-activated', HTTPError({'http-status': 500, 'reason': 263})),
('not-activated', dict(force=False),
'not-activated', HTTPError({'http-status': 500, 'reason': 263})),
('not-activated', dict(force=True),
'not-activated', None),
('not-operating', dict(force=False),
'not-activated', None),
('not-operating', dict(force=True),
'not-activated', None),
('operating', dict(), # Verify that force default is False
'not-activated', HTTPError({'http-status': 500, 'reason': 263})),
('operating', dict(force=False),
'not-activated', HTTPError({'http-status': 500, 'reason': 263})),
('operating', dict(force=True),
'not-activated', None),
('exceptions', dict(force=False),
'not-activated', None),
('exceptions', dict(force=True),
'not-activated', None),
('not-operating', dict(),
'exceptions', StatusTimeout(None, None, None, None)),
('not-operating', dict(allow_status_exceptions=False),
'exceptions', StatusTimeout(None, None, None, None)),
('not-operating', dict(allow_status_exceptions=True),
'exceptions', None),
]
)
@mock.patch.object(LparDeactivateHandler, 'get_status')
def test_lpar_deactivate(
self, get_status_mock,
initial_status, input_kwargs, act_exp_status, exp_status_exc):
"""Test Lpar.deactivate()."""
# Add a faked LPAR
faked_lpar = self.add_lpar1()
faked_lpar.properties['status'] = initial_status
lpar_mgr = self.cpc.lpars
lpar = lpar_mgr.find(name=faked_lpar.name)
get_status_mock.return_value = act_exp_status
exp_excs = []
if exp_status_exc:
exp_excs.append(exp_status_exc)
if exp_excs:
with pytest.raises(Exception) as exc_info:
# Execute the code to be tested
lpar.deactivate(**input_kwargs)
exc = exc_info.value
exp_exc_classes = [e.__class__ for e in exp_excs]
assert isinstance(exc, tuple(exp_exc_classes))
if isinstance(exc, HTTPError):
exp_httperror = [e for e in exp_excs
if isinstance(e, HTTPError)][0]
assert exc.http_status == exp_httperror.http_status
assert exc.reason == exp_httperror.reason
else:
# Execute the code to be tested.
ret = lpar.deactivate(**input_kwargs)
assert ret is None
lpar.pull_full_properties()
status = lpar.get_property('status')
assert status == act_exp_status
@pytest.mark.parametrize(
"initial_loadparm, loadparm_kwargs, exp_loadparm, exp_loadparm_exc", [
(None, dict(),
'', None),
(None, dict(load_parameter='abcd'),
'abcd', None),
('abcd', dict(),
'abcd', None),
('fooo', dict(load_parameter='abcd'),
'abcd', None),
]
)
@pytest.mark.parametrize(
"initial_loadaddr, loadaddr_kwargs, exp_loadaddr, exp_loadaddr_exc", [
(None, dict(),
None, HTTPError({'http-status': 400, 'reason': 5})),
(None, dict(load_address='5176'),
'5176', None),
('5176', dict(),
'5176', None),
('1234', dict(load_address='5176'),
'5176', None),
]
)
@pytest.mark.parametrize(
"initial_status, status_kwargs, act_exp_status, exp_status_exc"
", initial_stored_status, exp_stored_status, exp_store_status_exc", [
('not-activated', dict(),
'operating', HTTPError({'http-status': 409, 'reason': 0}),
None, None, None),
('not-activated', dict(force=False),
'operating', HTTPError({'http-status': 409, 'reason': 0}),
None, None, None),
('not-activated', dict(force=True),
'operating', HTTPError({'http-status': 409, 'reason': 0}),
None, None, None),
('not-operating', dict(force=False),
'operating', None,
None, None, None),
('not-operating', dict(force=True),
'operating', None,
None, None, None),
('operating', dict(),
'operating', HTTPError({'http-status': 500, 'reason': 263}),
None, None, None),
('operating', dict(force=False),
'operating', HTTPError({'http-status': 500, 'reason': 263}),
None, None, None),
('operating', dict(force=True),
'operating', None,
None, None, None),
('exceptions', dict(force=False),
'operating', None,
None, None, None),
('exceptions', dict(force=True),
'operating', None,
None, None, None),
('not-operating', dict(),
'exceptions', StatusTimeout(None, None, None, None),
None, None, None),
('not-operating', dict(allow_status_exceptions=False),
'exceptions', StatusTimeout(None, None, None, None),
None, None, None),
('not-operating', dict(allow_status_exceptions=True),
'exceptions', None,
None, None, None),
('not-operating', dict(store_status_indicator=False),
'operating', None,
None, None, None),
('not-operating', dict(store_status_indicator=True),
'operating', None,
None, 'not-operating', None),
]
)
@pytest.mark.parametrize(
"initial_memory, memory_kwargs, exp_memory, exp_memory_exc", [
('foobar', dict(),
'', None),
('foobar', dict(clear_indicator=False),
'foobar', None),
('foobar', dict(clear_indicator=True),
'', None),
]
)
@mock.patch.object(LparLoadHandler, 'get_status')
def test_lpar_load(
self, get_status_mock,
initial_status, status_kwargs, act_exp_status, exp_status_exc,
initial_loadaddr, loadaddr_kwargs, exp_loadaddr, exp_loadaddr_exc,
initial_loadparm, loadparm_kwargs, exp_loadparm, exp_loadparm_exc,
initial_memory, memory_kwargs, exp_memory, exp_memory_exc,
initial_stored_status, exp_stored_status, exp_store_status_exc):
"""Test Lpar.load()."""
# Add a faked LPAR
faked_lpar = self.add_lpar1()
faked_lpar.properties['status'] = initial_status
faked_lpar.properties['last-used-load-address'] = initial_loadaddr
faked_lpar.properties['last-used-load-parameter'] = initial_loadparm
faked_lpar.properties['memory'] = initial_memory
lpar_mgr = self.cpc.lpars
lpar = lpar_mgr.find(name=faked_lpar.name)
input_kwargs = dict(status_kwargs, **loadaddr_kwargs)
input_kwargs.update(**loadparm_kwargs)
input_kwargs.update(**memory_kwargs)
exp_excs = []
if exp_status_exc:
exp_excs.append(exp_status_exc)
if exp_loadaddr_exc:
exp_excs.append(exp_loadaddr_exc)
if exp_loadparm_exc:
exp_excs.append(exp_loadparm_exc)
if exp_memory_exc:
exp_excs.append(exp_memory_exc)
if exp_store_status_exc:
exp_excs.append(exp_store_status_exc)
get_status_mock.return_value = act_exp_status
if exp_excs:
with pytest.raises(Exception) as exc_info:
# Execute the code to be tested
lpar.load(**input_kwargs)
exc = exc_info.value
exp_exc_classes = [e.__class__ for e in exp_excs]
assert isinstance(exc, tuple(exp_exc_classes))
if isinstance(exc, HTTPError):
exp_httperror = [e for e in exp_excs
if isinstance(e, HTTPError)][0]
assert exc.http_status == exp_httperror.http_status
assert exc.reason == exp_httperror.reason
else:
# Execute the code to be tested.
ret = lpar.load(**input_kwargs)
assert ret is None
lpar.pull_full_properties()
status = lpar.get_property('status')
assert status == act_exp_status
last_loadaddr = lpar.get_property('last-used-load-address')
assert last_loadaddr == exp_loadaddr
last_loadparm = lpar.get_property('last-used-load-parameter')
assert last_loadparm == exp_loadparm
last_memory = lpar.get_property('memory')
assert last_memory == exp_memory
stored_status = lpar.get_property('stored-status')
assert stored_status == exp_stored_status
# TODO: Test for Lpar.scsi_load()
# TODO: Test for Lpar.scsi_dump()
# TODO: Test for Lpar.stop()
# TODO: Test for Lpar.reset_clear()
# TODO: Test for Lpar.psw_restart()