File: test_manager.py

package info (click to toggle)
python-proliantutils 2.16.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,684 kB
  • sloc: python: 58,655; makefile: 163; sh: 2
file content (581 lines) | stat: -rw-r--r-- 29,261 bytes parent folder | download | duplicates (3)
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
# Copyright 2014 Hewlett-Packard Development Company, L.P.
#
# 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.

import time
from unittest import mock

import testtools

from proliantutils import exception
from proliantutils.hpssa import manager
from proliantutils.hpssa import objects
from proliantutils.tests.hpssa import raid_constants


@mock.patch.object(objects.Server, '_get_all_details')
class ManagerTestCases(testtools.TestCase):

    def _test_create_configuration_with_disk_input(self,
                                                   controller_exec_cmd_mock,
                                                   get_all_details_mock):
        ld1 = {'size_gb': 50,
               'raid_level': '1',
               'controller': 'Smart Array P822 in Slot 2',
               'physical_disks': ['5I:1:1',
                                  '5I:1:2']}
        ld2 = {'size_gb': 100,
               'raid_level': '5',
               'controller': 'Smart Array P822 in Slot 2',
               'physical_disks': ['5I:1:3',
                                  '5I:1:4',
                                  '6I:1:5']}

        raid_info = {'logical_disks': [ld1, ld2]}

        current_config = manager.create_configuration(raid_info)

        ld1_drives = '5I:1:1,5I:1:2'
        ld2_drives = '5I:1:3,5I:1:4,6I:1:5'
        controller_exec_cmd_mock.assert_any_call("create",
                                                 "type=logicaldrive",
                                                 "drives=%s" % ld2_drives,
                                                 "raid=5",
                                                 "size=%d" % (100 * 1024),
                                                 process_input='y')
        # Verify that we created the 50GB disk the last.
        controller_exec_cmd_mock.assert_called_with("create",
                                                    "type=logicaldrive",
                                                    "drives=%s" % ld1_drives,
                                                    "raid=1",
                                                    "size=%d" % (50 * 1024),
                                                    process_input='y')

        ld1_ret = [x for x in current_config['logical_disks']
                   if x['raid_level'] == '1'][0]
        ld2_ret = [x for x in current_config['logical_disks']
                   if x['raid_level'] == '5'][0]

        self.assertIsNotNone(ld1_ret['root_device_hint']['wwn'])
        self.assertIsNotNone(ld2_ret['root_device_hint']['wwn'])
        self.assertIsNotNone(ld1_ret['volume_name'])
        self.assertIsNotNone(ld2_ret['volume_name'])

        # Assert physical disk info
        pds_active = [x['id'] for x in current_config['physical_disks']
                      if x['status'] == 'active']
        pds_ready = [x['id'] for x in current_config['physical_disks']
                     if x['status'] == 'ready']
        pds_active_expected = ['5I:1:3', '5I:1:4', '6I:1:5',
                               '5I:1:1', '5I:1:2']
        pds_ready_expected = ['6I:1:6', '6I:1:7']
        self.assertEqual(sorted(pds_active_expected), sorted(pds_active))
        self.assertEqual(sorted(pds_ready_expected), sorted(pds_ready))

    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_create_configuration_with_disk_input_create_succeeds(
            self, controller_exec_cmd_mock, get_all_details_mock):
        no_drives = raid_constants.HPSSA_NO_DRIVES
        one_drive = raid_constants.HPSSA_ONE_DRIVE_100GB_RAID_5
        two_drives = raid_constants.HPSSA_TWO_DRIVES_100GB_RAID5_50GB_RAID1
        get_all_details_mock.side_effect = [no_drives, one_drive, two_drives]
        self._test_create_configuration_with_disk_input(
            controller_exec_cmd_mock, get_all_details_mock)

    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_create_configuration_with_disk_input_create_fails(
            self, controller_exec_cmd_mock, get_all_details_mock):
        no_drives = raid_constants.HPSSA_NO_DRIVES
        one_drive = raid_constants.HPSSA_ONE_DRIVE_100GB_RAID_5
        get_all_details_mock.side_effect = [no_drives, one_drive, one_drive]
        ex = self.assertRaises(exception.HPSSAOperationError,
                               self._test_create_configuration_with_disk_input,
                               controller_exec_cmd_mock, get_all_details_mock)
        self.assertIn("raid_level '1' and size 50 GB not found", str(ex))

    def test_create_configuration_invalid_logical_disks(self,
                                                        get_all_details_mock):

        drives = raid_constants.HPSSA_NO_DRIVES
        get_all_details_mock.return_value = drives
        raid_info = {}
        self.assertRaises(exception.InvalidInputError,
                          manager.create_configuration,
                          raid_info)

        raid_info = {'logical_disks': 'foo'}
        self.assertRaises(exception.InvalidInputError,
                          manager.create_configuration,
                          raid_info)

        no_drives = raid_constants.HPSSA_NO_DRIVES
        get_all_details_mock.return_value = no_drives
        raid_info = {'logical_disks': [
                     {'size_gb': 50,
                      'raid_level': '1',
                      'controller': 'Smart Array P822 in Slot 0',
                      'physical_disks': ["6I:1:5", "6I:1:6"]}]}
        msg = ("Invalid Input: Unable to find controller named 'Smart Array "
               "P822 in Slot 0'. The available controllers are "
               "'Smart Array P822 in Slot 2'.")
        ex = self.assertRaises(exception.InvalidInputError,
                               manager.create_configuration,
                               raid_info)
        self.assertEqual(msg, str(ex))

    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_create_configuration_without_disk_input_succeeds(
            self, controller_exec_cmd_mock, get_all_details_mock):
        no_drives = raid_constants.HPSSA_NO_DRIVES
        one_drive = raid_constants.HPSSA_ONE_DRIVE_100GB_RAID_5
        two_drives = raid_constants.HPSSA_TWO_DRIVES_100GB_RAID5_50GB_RAID1
        get_all_details_mock.side_effect = [no_drives, one_drive, two_drives]
        raid_info = {'logical_disks': [{'size_gb': 50,
                                        'raid_level': '1'},
                                       {'size_gb': 100,
                                        'raid_level': '5'}]}
        current_config = manager.create_configuration(raid_info)
        controller_exec_cmd_mock.assert_any_call("create",
                                                 "type=logicaldrive",
                                                 mock.ANY,
                                                 "raid=5",
                                                 "size=%d" % (100 * 1024),
                                                 process_input='y')
        # Verify that we created the 50GB disk the last.
        controller_exec_cmd_mock.assert_called_with("create",
                                                    "type=logicaldrive",
                                                    mock.ANY,
                                                    "raid=1",
                                                    "size=%d" % (50 * 1024),
                                                    process_input='y')

        ld1_ret = [x for x in current_config['logical_disks']
                   if x['raid_level'] == '1'][0]
        ld2_ret = [x for x in current_config['logical_disks']
                   if x['raid_level'] == '5'][0]
        self.assertEqual('0x600508b1001cc42c',
                         ld2_ret['root_device_hint']['wwn'])
        self.assertEqual('0x600508b1001ce1e1',
                         ld1_ret['root_device_hint']['wwn'])

    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_create_configuration_without_disk_input_fails_on_disk_type(
            self, controller_exec_cmd_mock, get_all_details_mock):
        no_drives = raid_constants.HPSSA_NO_DRIVES
        one_drive = raid_constants.HPSSA_ONE_DRIVE_100GB_RAID_5
        two_drives = raid_constants.HPSSA_TWO_DRIVES_100GB_RAID5_50GB_RAID1
        get_all_details_mock.side_effect = [no_drives, one_drive, two_drives]
        raid_info = {'logical_disks': [{'size_gb': 50,
                                        'raid_level': '1',
                                        'disk_type': 'ssd'},
                                       {'size_gb': 100,
                                        'raid_level': '5',
                                        'disk_type': 'hdd'}]}
        exc = self.assertRaises(exception.PhysicalDisksNotFoundError,
                                manager.create_configuration,
                                raid_info)
        self.assertIn("of size 50 GB and raid level 1", str(exc))

    def test_create_configuration_hba_enabled(self, get_all_details_mock):
        drives = raid_constants.HPSSA_HBA_MODE
        get_all_details_mock.return_value = drives

        raid_info = {'logical_disks': 'foo'}

        msg = ("An error was encountered while doing ssa configuration: None"
               " of the available SSA controllers Smart Array P822 in "
               "Slot 3 have RAID enabled")
        ex = self.assertRaises(exception.HPSSAOperationError,
                               manager.create_configuration,
                               raid_info)
        self.assertIn(msg, str(ex))

    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_create_configuration_share_physical_disks(
            self, controller_exec_cmd_mock, get_all_details_mock):
        no_drives = raid_constants.HPSSA_NO_DRIVES_3_PHYSICAL_DISKS
        one_drive = raid_constants.ONE_DRIVE_RAID_1
        two_drives = raid_constants.TWO_DRIVES_50GB_RAID1
        get_all_details_mock.side_effect = [no_drives, one_drive, two_drives]
        controller_exec_cmd_mock.side_effect = [
            (None, None),
            (raid_constants.DRIVE_2_RAID_1_OKAY_TO_SHARE, None),
            (None, None)]
        raid_info = {'logical_disks': [{'size_gb': 50,
                                        'share_physical_disks': True,
                                        'number_of_physical_disks': 2,
                                        'raid_level': '0',
                                        'disk_type': 'hdd'},
                                       {'size_gb': 50,
                                        'share_physical_disks': True,
                                        'raid_level': '1',
                                        'disk_type': 'hdd'}]}
        raid_info = manager.create_configuration(raid_info)
        ld1 = raid_info['logical_disks'][0]
        ld2 = raid_info['logical_disks'][1]
        self.assertEqual('Smart Array P822 in Slot 2', ld1['controller'])
        self.assertEqual('Smart Array P822 in Slot 2', ld2['controller'])
        self.assertEqual(sorted(['5I:1:1', '5I:1:2']),
                         sorted(ld1['physical_disks']))
        self.assertEqual(sorted(['5I:1:1', '5I:1:2']),
                         sorted(ld2['physical_disks']))
        controller_exec_cmd_mock.assert_any_call(
            'create', 'type=logicaldrive', 'drives=5I:1:1,5I:1:2',
            'raid=1', 'size=51200', process_input='y')
        controller_exec_cmd_mock.assert_any_call(
            'array', 'A', 'create', 'type=logicaldrive', 'raid=0', 'size=?',
            dont_transform_to_hpssa_exception=True)
        controller_exec_cmd_mock.assert_any_call(
            'array', 'A', 'create', 'type=logicaldrive', 'raid=0',
            'size=51200', process_input='y')

    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_create_configuration_share_nonshare_physical_disks(
            self, controller_exec_cmd_mock, get_all_details_mock):
        no_drives = raid_constants.HPSSA_NO_DRIVES_3_PHYSICAL_DISKS
        one_drive = raid_constants.ONE_DRIVE_RAID_1
        two_drives = raid_constants.TWO_DRIVES_50GB_RAID1
        get_all_details_mock.side_effect = [no_drives, one_drive, two_drives]
        controller_exec_cmd_mock.side_effect = [
            (None, None),
            (raid_constants.DRIVE_2_RAID_1_OKAY_TO_SHARE, None),
            (None, None)]
        raid_info = {'logical_disks': [{'size_gb': 50,
                                        'raid_level': '1',
                                        'disk_type': 'hdd'},
                                       {'size_gb': 50,
                                        'share_physical_disks': True,
                                        'raid_level': '0',
                                        'disk_type': 'hdd'}]}
        raid_info = manager.create_configuration(raid_info)
        ld1 = raid_info['logical_disks'][0]
        ld2 = raid_info['logical_disks'][1]
        self.assertEqual('Smart Array P822 in Slot 2', ld1['controller'])
        self.assertEqual('Smart Array P822 in Slot 2', ld2['controller'])
        self.assertEqual(sorted(['5I:1:1', '5I:1:2']),
                         sorted(ld1['physical_disks']))
        self.assertEqual(sorted(['5I:1:1', '5I:1:2']),
                         sorted(ld2['physical_disks']))
        controller_exec_cmd_mock.assert_any_call(
            'create', 'type=logicaldrive', 'drives=5I:1:1,5I:1:2',
            'raid=1', 'size=51200', process_input='y')
        controller_exec_cmd_mock.assert_any_call(
            'create', 'type=logicaldrive', 'drives=5I:1:3', 'raid=0',
            'size=51200', process_input='y')

    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_create_configuration_max_as_size_gb(
            self, controller_exec_cmd_mock, get_all_details_mock):
        no_drives = raid_constants.NO_DRIVES_HPSSA_7_DISKS
        one_drive = raid_constants.ONE_DRIVE_RAID_1_50_GB
        two_drives = raid_constants.TWO_DRIVES_50GB_RAID1_MAXGB_RAID5
        get_all_details_mock.side_effect = [no_drives, one_drive, two_drives]
        raid_info = {'logical_disks': [{'size_gb': 50,
                                        'raid_level': '1',
                                        'disk_type': 'hdd'},
                                       {'size_gb': 'MAX',
                                        'raid_level': '5',
                                        'disk_type': 'hdd'}]}
        raid_info = manager.create_configuration(raid_info)
        ld1 = raid_info['logical_disks'][0]
        ld2 = raid_info['logical_disks'][1]
        self.assertEqual('Smart Array P822 in Slot 3', ld1['controller'])
        self.assertEqual('Smart Array P822 in Slot 3', ld2['controller'])
        self.assertEqual(sorted(['5I:1:1', '5I:1:2']),
                         sorted(ld1['physical_disks']))
        self.assertEqual(sorted(['5I:1:3', '5I:1:4', '6I:1:5']),
                         sorted(ld2['physical_disks']))
        controller_exec_cmd_mock.assert_any_call(
            'create', 'type=logicaldrive', 'drives=5I:1:1,5I:1:2',
            'raid=1', 'size=51200', process_input='y')
        controller_exec_cmd_mock.assert_any_call(
            'create', 'type=logicaldrive', 'drives=5I:1:3,5I:1:4,6I:1:5',
            'raid=5', process_input='y')

    def test__sort_shared_logical_disks(self, get_all_details_mock):
        logical_disk_sorted_expected = [
            {'size_gb': 500, 'disk_type': 'hdd', 'raid_level': '1'},
            {'share_physical_disks': True, 'size_gb': 450, 'disk_type': 'hdd',
             'number_of_physical_disks': 6, 'raid_level': '0'},
            {'share_physical_disks': True, 'size_gb': 200, 'disk_type': 'hdd',
             'raid_level': '1+0'},
            {'share_physical_disks': True, 'size_gb': 200, 'disk_type': 'hdd',
             'raid_level': '0'},
            {'share_physical_disks': True, 'size_gb': 100, 'disk_type': 'hdd',
             'raid_level': '0'}]
        logical_disks = [{'size_gb': 500,
                          'disk_type': 'hdd',
                          'raid_level': '1'},
                         {'share_physical_disks': True,
                          'size_gb': 450,
                          'disk_type': 'hdd',
                          'number_of_physical_disks': 6,
                          'raid_level': '0'},
                         {'share_physical_disks': True,
                          'size_gb': 200,
                          'disk_type': 'hdd',
                          'raid_level': '1+0'},
                         {'share_physical_disks': True,
                          'size_gb': 200,
                          'disk_type': 'hdd',
                          'raid_level': '0'},
                         {'share_physical_disks': True,
                          'size_gb': 100,
                          'disk_type': 'hdd',
                          'raid_level': '0'}]
        logical_disks_sorted = manager._sort_shared_logical_disks(
            logical_disks)
        self.assertEqual(logical_disks_sorted, logical_disk_sorted_expected)

    def test__sort_shared_logical_disks_raid10(self, get_all_details_mock):
        logical_disk_sorted_expected = [
            {'size_gb': 600, 'disk_type': 'hdd', 'raid_level': '1'},
            {'share_physical_disks': False, 'size_gb': 400, 'disk_type': 'hdd',
             'raid_level': '1+0'},
            {'share_physical_disks': False, 'size_gb': 100, 'disk_type': 'hdd',
             'raid_level': '5'},
            {'share_physical_disks': True, 'size_gb': 550, 'disk_type': 'hdd',
             'raid_level': '1'},
            {'share_physical_disks': True, 'size_gb': 200, 'disk_type': 'hdd',
             'raid_level': '1+0'},
            {'share_physical_disks': True, 'size_gb': 450, 'disk_type': 'hdd',
             'number_of_physical_disks': 5, 'raid_level': '0'},
            {'share_physical_disks': True, 'size_gb': 300, 'disk_type': 'hdd',
             'raid_level': '5'}]
        logical_disks = [
            {'size_gb': 600, 'disk_type': 'hdd', 'raid_level': '1'},
            {'share_physical_disks': True, 'size_gb': 550, 'disk_type': 'hdd',
             'raid_level': '1'},
            {'share_physical_disks': True, 'size_gb': 450, 'disk_type': 'hdd',
             'number_of_physical_disks': 5, 'raid_level': '0'},
            {'share_physical_disks': False, 'size_gb': 400, 'disk_type': 'hdd',
             'raid_level': '1+0'},
            {'share_physical_disks': True, 'size_gb': 300, 'disk_type': 'hdd',
             'raid_level': '5'},
            {'share_physical_disks': True, 'size_gb': 200, 'disk_type': 'hdd',
             'raid_level': '1+0'},
            {'share_physical_disks': False, 'size_gb': 100, 'disk_type': 'hdd',
             'raid_level': '5'}]
        logical_disks_sorted = manager._sort_shared_logical_disks(
            logical_disks)
        self.assertEqual(logical_disks_sorted, logical_disk_sorted_expected)

    @mock.patch.object(manager, 'get_configuration')
    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_delete_configuration(self, controller_exec_cmd_mock,
                                  get_configuration_mock,
                                  get_all_details_mock):
        get_all_details_mock.return_value = raid_constants.HPSSA_ONE_DRIVE
        get_configuration_mock.return_value = 'foo'

        ret = manager.delete_configuration()

        controller_exec_cmd_mock.assert_called_with(
            "logicaldrive", "all", "delete", "forced")
        get_configuration_mock.assert_called_once_with()
        self.assertEqual('foo', ret)

    @mock.patch.object(manager, 'get_configuration')
    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_delete_configuration_no_arrays(
            self, controller_exec_cmd_mock,
            get_configuration_mock, get_all_details_mock):

        get_all_details_mock.return_value = raid_constants.HPSSA_NO_DRIVES
        get_configuration_mock.return_value = 'foo'

        ret = manager.delete_configuration()

        self.assertFalse(controller_exec_cmd_mock.called)
        get_configuration_mock.assert_called_once_with()
        self.assertEqual('foo', ret)

    @mock.patch.object(manager, 'get_configuration')
    def test_delete_configuration_hba_enabled(self, get_configuration,
                                              get_all_details_mock):
        drives = raid_constants.HPSSA_HBA_MODE
        get_all_details_mock.return_value = drives

        msg = ("An error was encountered while doing ssa configuration: None"
               " of the available SSA controllers Smart Array P822 in "
               "Slot 3 have RAID enabled")
        ex = self.assertRaises(exception.HPSSAOperationError,
                               manager.delete_configuration)
        self.assertIn(msg, str(ex))

    def test_get_configuration(self, get_all_details_mock):

        get_all_details_mock.return_value = raid_constants.HPSSA_ONE_DRIVE

        raid_info_returned = manager.get_configuration()

        ld1_expected = {'size_gb': 557,
                        'raid_level': '1',
                        'controller': 'Smart Array P822 in Slot 2',
                        'physical_disks': ['5I:1:1',
                                           '5I:1:2'],
                        'volume_name': '01F42227PDVTF0BRH5T0MOAB64',
                        'root_device_hint': {
                            'wwn': '0x600508b1001c321c'}}

        # NOTE(rameshg87: Cannot directly compare because
        # of 'physical_disks' key.
        ld1_returned = raid_info_returned['logical_disks'][0]
        self.assertEqual(ld1_expected['size_gb'],
                         ld1_returned['size_gb'])
        self.assertEqual(ld1_expected['raid_level'],
                         ld1_returned['raid_level'])
        self.assertEqual(ld1_expected['controller'],
                         ld1_returned['controller'])
        self.assertEqual(ld1_expected['volume_name'],
                         ld1_returned['volume_name'])
        self.assertEqual(ld1_expected['root_device_hint'],
                         ld1_returned['root_device_hint'])
        self.assertEqual(sorted(ld1_expected['physical_disks']),
                         sorted(ld1_returned['physical_disks']))

        # Assert physical disk info
        pds_active = [x['id'] for x in raid_info_returned['physical_disks']
                      if x['status'] == 'active']
        pds_ready = [x['id'] for x in raid_info_returned['physical_disks']
                     if x['status'] == 'ready']
        pds_active_expected = ['5I:1:1', '5I:1:2']
        pds_ready_expected = ['6I:1:6', '6I:1:7', '5I:1:3',
                              '5I:1:4', '6I:1:5']
        self.assertEqual(sorted(pds_active_expected), sorted(pds_active))
        self.assertEqual(sorted(pds_ready_expected), sorted(pds_ready))

    def test__select_controllers_by_hba(self, get_all_details_mock):
        get_all_details_mock.return_value = raid_constants.HPSSA_HBA_MODE

        server = objects.Server()
        select_controllers = lambda x: not x.properties.get('HBA Mode Enabled',
                                                            False)

        msg = ("An error was encountered while doing ssa configuration: "
               "None of the available SSA controllers Smart Array P822 "
               "in Slot 3 have Raid enabled.")
        ex = self.assertRaises(exception.HPSSAOperationError,
                               manager._select_controllers_by,
                               server, select_controllers, 'Raid enabled')
        self.assertIn(msg, str(ex))

    def test__select_controllers_by(self, get_all_details_mock):
        get_all_details_mock.return_value = raid_constants.HPSSA_NO_DRIVES

        server = objects.Server()
        select_controllers = lambda x: not x.properties.get('HBA Mode Enabled',
                                                            False)
        ctrl_expected = server.controllers

        manager._select_controllers_by(server, select_controllers,
                                       'Raid enabled')
        self.assertEqual(ctrl_expected, server.controllers)

    @mock.patch.object(time, 'sleep')
    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_erase_devices(self, controller_exec_cmd_mock,
                           sleep_mock,
                           get_all_details_mock):
        erase_drive = raid_constants.SSA_ERASE_DRIVE
        erase_complete = raid_constants.SSA_ERASE_COMPLETE
        cmd_args = []
        cmd_args.append("pd 1I:2:1")
        cmd_args.extend(['modify', 'erase',
                         'erasepattern=overwrite',
                         'unrestricted=off',
                         'forced'])
        expt_ret = {
            'Smart Array P440 in Slot 2': {
                '1I:2:1': 'Erase Complete. Reenable Before Using.',
                'Summary': ('Sanitize Erase performed on the disks attached to'
                            ' the controller.')}}
        get_all_details_mock.side_effect = [erase_drive, erase_complete,
                                            erase_complete]

        ret = manager.erase_devices()
        self.assertTrue(controller_exec_cmd_mock.called)
        controller_exec_cmd_mock.assert_any_call(*cmd_args)
        self.assertEqual(expt_ret, ret)
        self.assertFalse(sleep_mock.called)

    @mock.patch.object(time, 'sleep')
    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_erase_devices_in_progress(self, controller_exec_cmd_mock,
                                       sleep_mock,
                                       get_all_details_mock):

        erase_drive = raid_constants.SSA_ERASE_DRIVE
        erase_progress = raid_constants.SSA_ERASE_IN_PROGRESS
        erase_complete = raid_constants.SSA_ERASE_COMPLETE

        expt_ret = {
            'Smart Array P440 in Slot 2': {
                '1I:2:1': 'Erase Complete. Reenable Before Using.',
                'Summary': ('Sanitize Erase performed on the disks attached to'
                            ' the controller.')}}
        get_all_details_mock.side_effect = [erase_drive, erase_progress,
                                            erase_complete, erase_complete]

        ret = manager.erase_devices()
        self.assertTrue(controller_exec_cmd_mock.called)
        self.assertEqual(expt_ret, ret)
        self.assertTrue(sleep_mock.called)

    @mock.patch.object(time, 'sleep')
    @mock.patch.object(objects.Controller, 'execute_cmd')
    def test_erase_devices_not_supported(self, controller_exec_cmd_mock,
                                         sleep_mock,
                                         get_all_details_mock):
        erase_not_supported = raid_constants.SSA_ERASE_NOT_SUPPORTED
        erase_complete = raid_constants.SSA_ERASE_COMPLETE_NOT_SUPPORTED
        erase_progress = raid_constants.SSA_ERASE_IN_PROGRESS_NOT_SUPPORTED
        get_all_details_mock.side_effect = [erase_not_supported,
                                            erase_progress,
                                            erase_complete, erase_complete]
        value = ("Drive 1I:2:1: This operation is not supported in this "
                 "physical drive")
        controller_exec_cmd_mock.return_value = value
        expt_ret = {
            'Smart Array P440 in Slot 2': {
                '1I:2:1': 'Erase Complete. Reenable Before Using.',
                'Summary': ('Drives overwritten with zeros because '
                            'sanitize erase is not supported on the '
                            'controller.')
            }
        }

        ret = manager.erase_devices()
        self.assertEqual(expt_ret, ret)
        self.assertTrue(controller_exec_cmd_mock.called)
        self.assertTrue(sleep_mock.called)


class RaidConfigValidationTestCases(testtools.TestCase):

    def test_validate_fails_min_disks_number(self):
        raid_config = {'logical_disks':
                       [{'size_gb': 100,
                         'raid_level': '5',
                         'number_of_physical_disks': 2}]}
        msg = "RAID level 5 requires at least 3 disks"
        self.assertRaisesRegex(exception.InvalidInputError, msg,
                               manager.validate, raid_config)

    def test_validate_fails_min_physical_disks(self):
        raid_config = {'logical_disks':
                       [{'size_gb': 100, 'raid_level': '5',
                         'physical_disks': ['foo']}]}
        msg = "RAID level 5 requires at least 3 disks"
        self.assertRaisesRegex(exception.InvalidInputError, msg,
                               manager.validate, raid_config)