summaryrefslogtreecommitdiff
path: root/ironic/tests/unit/drivers/modules/irmc/test_management.py
blob: 953db6a142ea07929f99669d489f2c85b7014056 (plain)
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# Copyright 2015 FUJITSU LIMITED
#
# 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.

"""
Test class for iRMC Management Driver
"""

import os
from unittest import mock
import xml.etree.ElementTree as ET

from ironic.common import boot_devices
from ironic.common import exception
from ironic.common import states
from ironic.conductor import task_manager
from ironic.conductor import utils as manager_utils
from ironic.drivers.modules import fake
from ironic.drivers.modules import ipmitool
from ironic.drivers.modules.irmc import common as irmc_common
from ironic.drivers.modules.irmc import management as irmc_management
from ironic.drivers.modules.irmc import power as irmc_power
from ironic.drivers.modules.redfish import management as redfish_management
from ironic.drivers.modules.redfish import utils as redfish_util
from ironic.drivers import utils as driver_utils
from ironic.tests.unit.drivers.modules.irmc import test_common
from ironic.tests.unit.drivers import third_party_driver_mock_specs \
    as mock_specs


@mock.patch.object(irmc_management.irmc, 'elcm',
                   spec_set=mock_specs.SCCICLIENT_IRMC_ELCM_SPEC)
@mock.patch.object(manager_utils, 'node_power_action',
                   specset=True, autospec=True)
@mock.patch.object(irmc_power.IRMCPower, 'get_power_state',
                   return_value=states.POWER_ON,
                   specset=True, autospec=True)
class IRMCManagementFunctionsTestCase(test_common.BaseIRMCTest):
    def setUp(self):
        super(IRMCManagementFunctionsTestCase, self).setUp()
        self.info = irmc_common.parse_driver_info(self.node)

        irmc_management.irmc.scci.SCCIError = Exception
        irmc_management.irmc.scci.SCCIInvalidInputError = ValueError

    def test_backup_bios_config(self, mock_get_power, mock_power_action,
                                mock_elcm):
        self.config(clean_priority_restore_irmc_bios_config=10, group='irmc')
        bios_config = {'Server': {'System': {'BiosConfig': {'key1': 'val1'}}}}
        mock_elcm.backup_bios_config.return_value = {
            'bios_config': bios_config}
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            irmc_management.backup_bios_config(task)

            self.assertEqual(bios_config, task.node.driver_internal_info[
                'irmc_bios_config'])
            self.assertEqual(1, mock_elcm.backup_bios_config.call_count)

    def test_backup_bios_config_skipped(self, mock_get_power,
                                        mock_power_action, mock_elcm):
        self.config(clean_priority_restore_irmc_bios_config=0, group='irmc')
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            irmc_management.backup_bios_config(task)

            self.assertNotIn('irmc_bios_config',
                             task.node.driver_internal_info)
            self.assertFalse(mock_elcm.backup_bios_config.called)

    def test_backup_bios_config_failed(self, mock_get_power,
                                       mock_power_action, mock_elcm):
        self.config(clean_priority_restore_irmc_bios_config=10, group='irmc')
        mock_elcm.backup_bios_config.side_effect = Exception

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            self.assertRaises(exception.IRMCOperationError,
                              irmc_management.backup_bios_config,
                              task)
            self.assertNotIn('irmc_bios_config',
                             task.node.driver_internal_info)
            self.assertEqual(1, mock_elcm.backup_bios_config.call_count)

    def test__restore_bios_config(self, mock_get_power, mock_power_action,
                                  mock_elcm):

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            # Set bios data for the node info
            task.node.driver_internal_info['irmc_bios_config'] = 'data'
            irmc_management._restore_bios_config(task)

            self.assertEqual(1, mock_elcm.restore_bios_config.call_count)

    def test__restore_bios_config_failed(self, mock_get_power,
                                         mock_power_action,
                                         mock_elcm):
        mock_elcm.restore_bios_config.side_effect = Exception

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            # Set bios data for the node info
            task.node.driver_internal_info['irmc_bios_config'] = 'data'

            self.assertRaises(exception.IRMCOperationError,
                              irmc_management._restore_bios_config,
                              task)
            # Backed up BIOS config is still in the node object
            self.assertEqual('data', task.node.driver_internal_info[
                'irmc_bios_config'])
            self.assertTrue(mock_elcm.restore_bios_config.called)

    def test__restore_bios_config_corrupted(self, mock_get_power,
                                            mock_power_action,
                                            mock_elcm):
        mock_elcm.restore_bios_config.side_effect = \
            irmc_management.irmc.scci.SCCIInvalidInputError

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            # Set bios data for the node info
            task.node.driver_internal_info['irmc_bios_config'] = 'data'

            self.assertRaises(exception.IRMCOperationError,
                              irmc_management._restore_bios_config,
                              task)
            # Backed up BIOS config is removed from the node object
            self.assertNotIn('irmc_bios_config',
                             task.node.driver_internal_info)
            self.assertTrue(mock_elcm.restore_bios_config.called)


class IRMCManagementTestCase(test_common.BaseIRMCTest):
    def setUp(self):
        super(IRMCManagementTestCase, self).setUp()
        self.info = irmc_common.parse_driver_info(self.node)

    def test_get_properties(self):
        expected = irmc_common.COMMON_PROPERTIES
        expected.update(ipmitool.COMMON_PROPERTIES)
        expected.update(ipmitool.CONSOLE_PROPERTIES)
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            # Remove the boot and deploy interfaces properties
            task.driver.boot = fake.FakeBoot()
            task.driver.deploy = fake.FakeDeploy()
            self.assertEqual(expected, task.driver.get_properties())

    @mock.patch.object(redfish_util, 'parse_driver_info', autospec=True)
    @mock.patch.object(irmc_common, 'parse_driver_info', spec_set=True,
                       autospec=True)
    def test_validate_ipmi_success(self, mock_drvinfo, redfish_parsedr_mock):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            task.driver.management.validate(task)
            mock_drvinfo.assert_called_once_with(task.node)
            redfish_parsedr_mock.assert_not_called()

    @mock.patch.object(redfish_util, 'parse_driver_info', autospec=True)
    @mock.patch.object(irmc_common, 'parse_driver_info', spec_set=True,
                       autospec=True)
    def test_validate_ipmi_fail(self, mock_drvinfo, redfish_parsedr_mock):
        side_effect = exception.InvalidParameterValue("Invalid Input")
        mock_drvinfo.side_effect = side_effect
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            self.assertRaises(exception.InvalidParameterValue,
                              task.driver.management.validate,
                              task)
            mock_drvinfo.assert_called_once_with(task.node)
            redfish_parsedr_mock.assert_not_called()

    @mock.patch.object(redfish_util, 'parse_driver_info', autospec=True)
    @mock.patch.object(irmc_common, 'parse_driver_info', spec_set=True,
                       autospec=True)
    def test_validate_redfish_success(
            self, mock_drvinfo, redfish_parsedr_mock):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', False)
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            task.driver.management.validate(task)
            redfish_parsedr_mock.assert_called_once_with(task.node)
            mock_drvinfo.assert_called_once_with(task.node)

    @mock.patch.object(redfish_util, 'parse_driver_info', autospec=True)
    @mock.patch.object(irmc_common, 'parse_driver_info', spec_set=True,
                       autospec=True)
    def test_validate_redfish_fail(self, mock_drvinfo, redfish_parsedr_mock):
        side_effect = exception.InvalidParameterValue("Invalid Input")
        redfish_parsedr_mock.side_effect = side_effect
        self.node.set_driver_internal_info('irmc_ipmi_succeed', False)
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            mock_drvinfo.assert_not_called()
            self.assertRaises(exception.InvalidParameterValue,
                              task.driver.management.validate,
                              task)
            redfish_parsedr_mock.assert_called_once_with(task.node)

    def test_management_interface_get_supported_boot_devices_ipmi(self):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            expected = [boot_devices.PXE, boot_devices.DISK,
                        boot_devices.CDROM, boot_devices.BIOS,
                        boot_devices.SAFE]
            self.assertEqual(sorted(expected), sorted(task.driver.management.
                             get_supported_boot_devices(task)))

    def test_management_interface_get_supported_boot_devices_redfish(self):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', False)
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            expected = list(redfish_management.BOOT_DEVICE_MAP_REV)
            self.assertEqual(sorted(expected), sorted(task.driver.management.
                             get_supported_boot_devices(task)))

    @mock.patch.object(irmc_management.ipmitool, "send_raw", spec_set=True,
                       autospec=True)
    def _test_management_interface_set_boot_device_ok(
            self, boot_mode, params, expected_raw_code, send_raw_mock):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()
        send_raw_mock.return_value = [None, None]

        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.node.properties['capabilities'] = ''
            if boot_mode:
                driver_utils.add_node_capability(task, 'boot_mode', boot_mode)
            irmc_management.IRMCManagement().set_boot_device(task, **params)
            send_raw_mock.assert_has_calls([
                mock.call(task, "0x00 0x08 0x03 0x08"),
                mock.call(task, expected_raw_code)])

    def test_management_interface_set_boot_device_ok_pxe_ipmi(self):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()

        params = {'device': boot_devices.PXE, 'persistent': False}
        self._test_management_interface_set_boot_device_ok(
            None,
            params,
            "0x00 0x08 0x05 0xa0 0x04 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'bios',
            params,
            "0x00 0x08 0x05 0x80 0x04 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'uefi',
            params,
            "0x00 0x08 0x05 0xa0 0x04 0x00 0x00 0x00")

        params['persistent'] = True
        self._test_management_interface_set_boot_device_ok(
            None,
            params,
            "0x00 0x08 0x05 0xe0 0x04 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'bios',
            params,
            "0x00 0x08 0x05 0xc0 0x04 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'uefi',
            params,
            "0x00 0x08 0x05 0xe0 0x04 0x00 0x00 0x00")

    def test_management_interface_set_boot_device_ok_disk_ipmi(self):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()

        params = {'device': boot_devices.DISK, 'persistent': False}
        self._test_management_interface_set_boot_device_ok(
            None,
            params,
            "0x00 0x08 0x05 0xa0 0x08 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'bios',
            params,
            "0x00 0x08 0x05 0x80 0x08 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'uefi',
            params,
            "0x00 0x08 0x05 0xa0 0x08 0x00 0x00 0x00")

        params['persistent'] = True
        self._test_management_interface_set_boot_device_ok(
            None,
            params,
            "0x00 0x08 0x05 0xe0 0x08 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'bios',
            params,
            "0x00 0x08 0x05 0xc0 0x08 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'uefi',
            params,
            "0x00 0x08 0x05 0xe0 0x08 0x00 0x00 0x00")

    def test_management_interface_set_boot_device_ok_cdrom_ipmi(self):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()

        params = {'device': boot_devices.CDROM, 'persistent': False}
        self._test_management_interface_set_boot_device_ok(
            None,
            params,
            "0x00 0x08 0x05 0xa0 0x20 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'bios',
            params,
            "0x00 0x08 0x05 0x80 0x20 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'uefi',
            params,
            "0x00 0x08 0x05 0xa0 0x20 0x00 0x00 0x00")

        params['persistent'] = True
        self._test_management_interface_set_boot_device_ok(
            None,
            params,
            "0x00 0x08 0x05 0xe0 0x20 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'bios',
            params,
            "0x00 0x08 0x05 0xc0 0x20 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'uefi',
            params,
            "0x00 0x08 0x05 0xe0 0x20 0x00 0x00 0x00")

    def test_management_interface_set_boot_device_ok_bios_ipmi(self):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()

        params = {'device': boot_devices.BIOS, 'persistent': False}
        self._test_management_interface_set_boot_device_ok(
            None,
            params,
            "0x00 0x08 0x05 0xa0 0x18 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'bios',
            params,
            "0x00 0x08 0x05 0x80 0x18 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'uefi',
            params,
            "0x00 0x08 0x05 0xa0 0x18 0x00 0x00 0x00")

        params['persistent'] = True
        self._test_management_interface_set_boot_device_ok(
            None,
            params,
            "0x00 0x08 0x05 0xe0 0x18 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'bios',
            params,
            "0x00 0x08 0x05 0xc0 0x18 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'uefi',
            params,
            "0x00 0x08 0x05 0xe0 0x18 0x00 0x00 0x00")

    def test_management_interface_set_boot_device_ok_safe_ipmi(self):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()

        params = {'device': boot_devices.SAFE, 'persistent': False}
        self._test_management_interface_set_boot_device_ok(
            None,
            params,
            "0x00 0x08 0x05 0xa0 0x0c 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'bios',
            params,
            "0x00 0x08 0x05 0x80 0x0c 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'uefi',
            params,
            "0x00 0x08 0x05 0xa0 0x0c 0x00 0x00 0x00")

        params['persistent'] = True
        self._test_management_interface_set_boot_device_ok(
            None,
            params,
            "0x00 0x08 0x05 0xe0 0x0c 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'bios',
            params,
            "0x00 0x08 0x05 0xc0 0x0c 0x00 0x00 0x00")
        self._test_management_interface_set_boot_device_ok(
            'uefi',
            params,
            "0x00 0x08 0x05 0xe0 0x0c 0x00 0x00 0x00")

    @mock.patch.object(irmc_management.ipmitool, "send_raw", spec_set=True,
                       autospec=True)
    def test_management_interface_set_boot_device_ng_ipmi(self, send_raw_mock):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()

        """uefi mode, next boot only, unknown device."""
        send_raw_mock.return_value = [None, None]

        with task_manager.acquire(self.context, self.node.uuid) as task:
            driver_utils.add_node_capability(task, 'boot_mode', 'uefi')
            self.assertRaises(exception.InvalidParameterValue,
                              irmc_management.IRMCManagement().set_boot_device,
                              task,
                              "unknown")

    @mock.patch.object(irmc_management.ipmitool, 'send_raw', autospec=True)
    @mock.patch.object(redfish_management.RedfishManagement, 'set_boot_device',
                       autospec=True)
    def test_management_interfase_set_boot_device_success_redfish(
            self, redfish_set_boot_dev_mock, ipmi_raw_mock):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', False)
        self.node.save()
        ipmi_raw_mock.side_effect = exception.IPMIFailure
        management_inst = irmc_management.IRMCManagement()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            params = ['pxe', True]
            management_inst.set_boot_device(task, *params)
            redfish_set_boot_dev_mock.assert_called_once_with(
                management_inst, task, *params)

    @mock.patch.object(redfish_management.RedfishManagement, 'set_boot_device',
                       autospec=True)
    def test_management_interfase_set_boot_device_fail_redfish(
            self, redfish_set_boot_dev_mock):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', False)
        self.node.save()
        management_inst = irmc_management.IRMCManagement()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            params = [task, 'safe', True]
            self.assertRaises(exception.InvalidParameterValue,
                              management_inst.set_boot_device, *params)
            redfish_set_boot_dev_mock.assert_not_called()

    @mock.patch.object(irmc_management.irmc, 'scci',
                       spec_set=mock_specs.SCCICLIENT_IRMC_SCCI_SPEC)
    @mock.patch.object(irmc_common, 'get_irmc_report', spec_set=True,
                       autospec=True)
    def test_management_interface_get_sensors_data_scci_ok_ipmi(
            self, mock_get_irmc_report, mock_scci):
        """'irmc_sensor_method' = 'scci' specified and OK data."""
        with open(os.path.join(os.path.dirname(__file__),
                               'fake_sensors_data_ok.xml'), "r") as report:
            fake_txt = report.read()
        fake_xml = ET.fromstring(fake_txt)

        mock_get_irmc_report.return_value = fake_xml
        mock_scci.get_sensor_data.return_value = fake_xml.find(
            "./System/SensorDataRecords")

        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.node.driver_info['irmc_sensor_method'] = 'scci'
            sensor_dict = irmc_management.IRMCManagement().get_sensors_data(
                task)

        expected = {
            'Fan (4)': {
                'FAN1 SYS (29)': {
                    'Units': 'RPM',
                    'Sensor ID': 'FAN1 SYS (29)',
                    'Sensor Reading': '600 RPM'
                },
                'FAN2 SYS (29)': {
                    'Units': 'None',
                    'Sensor ID': 'FAN2 SYS (29)',
                    'Sensor Reading': 'None None'
                }
            },
            'Temperature (1)': {
                'Systemboard 1 (7)': {
                    'Units': 'degree C',
                    'Sensor ID': 'Systemboard 1 (7)',
                    'Sensor Reading': '80 degree C'
                },
                'Ambient (55)': {
                    'Units': 'degree C',
                    'Sensor ID': 'Ambient (55)',
                    'Sensor Reading': '42 degree C'
                }
            }
        }
        self.assertEqual(expected, sensor_dict)

    @mock.patch.object(irmc_management.irmc, 'scci',
                       spec_set=mock_specs.SCCICLIENT_IRMC_SCCI_SPEC)
    @mock.patch.object(irmc_common, 'get_irmc_report', spec_set=True,
                       autospec=True)
    def test_management_interface_get_sensors_data_scci_ok_redfish(
            self, mock_get_irmc_report, mock_scci):
        """'irmc_sensor_method' = 'scci' specified and OK data."""
        with open(os.path.join(os.path.dirname(__file__),
                               'fake_sensors_data_ok.xml'), "r") as report:
            fake_txt = report.read()
        fake_xml = ET.fromstring(fake_txt)

        mock_get_irmc_report.return_value = fake_xml
        mock_scci.get_sensor_data.return_value = fake_xml.find(
            "./System/SensorDataRecords")

        self.node.set_driver_internal_info('irmc_ipmi_succeed', False)
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.node.driver_info['irmc_sensor_method'] = 'scci'
            sensor_dict = irmc_management.IRMCManagement().get_sensors_data(
                task)

        expected = {
            'Fan (4)': {
                'FAN1 SYS (29)': {
                    'Units': 'RPM',
                    'Sensor ID': 'FAN1 SYS (29)',
                    'Sensor Reading': '600 RPM'
                },
                'FAN2 SYS (29)': {
                    'Units': 'None',
                    'Sensor ID': 'FAN2 SYS (29)',
                    'Sensor Reading': 'None None'
                }
            },
            'Temperature (1)': {
                'Systemboard 1 (7)': {
                    'Units': 'degree C',
                    'Sensor ID': 'Systemboard 1 (7)',
                    'Sensor Reading': '80 degree C'
                },
                'Ambient (55)': {
                    'Units': 'degree C',
                    'Sensor ID': 'Ambient (55)',
                    'Sensor Reading': '42 degree C'
                }
            }
        }
        self.assertEqual(expected, sensor_dict)

    @mock.patch.object(irmc_management.irmc, 'scci',
                       spec_set=mock_specs.SCCICLIENT_IRMC_SCCI_SPEC)
    @mock.patch.object(irmc_common, 'get_irmc_report', spec_set=True,
                       autospec=True)
    def test_management_interface_get_sensors_data_scci_ng_ipmi(
            self, mock_get_irmc_report, mock_scci):
        """'irmc_sensor_method' = 'scci' specified and NG data."""
        with open(os.path.join(os.path.dirname(__file__),
                               'fake_sensors_data_ng.xml'), "r") as report:
            fake_txt = report.read()
        fake_xml = ET.fromstring(fake_txt)

        mock_get_irmc_report.return_value = fake_xml
        mock_scci.get_sensor_data.return_value = fake_xml.find(
            "./System/SensorDataRecords")

        self.node.set_driver_internal_info('irmc_fw_version', 'iRMC S5/2.00S')
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.node.driver_info['irmc_sensor_method'] = 'scci'
            sensor_dict = irmc_management.IRMCManagement().get_sensors_data(
                task)

        self.assertEqual(len(sensor_dict), 0)

    @mock.patch.object(irmc_management.irmc, 'scci',
                       spec_set=mock_specs.SCCICLIENT_IRMC_SCCI_SPEC)
    @mock.patch.object(irmc_common, 'get_irmc_report', spec_set=True,
                       autospec=True)
    def test_management_interface_get_sensors_data_scci_ng_redfish(
            self, mock_get_irmc_report, mock_scci):
        """'irmc_sensor_method' = 'scci' specified and NG data."""
        with open(os.path.join(os.path.dirname(__file__),
                               'fake_sensors_data_ng.xml'), "r") as report:
            fake_txt = report.read()
        fake_xml = ET.fromstring(fake_txt)

        mock_get_irmc_report.return_value = fake_xml
        mock_scci.get_sensor_data.return_value = fake_xml.find(
            "./System/SensorDataRecords")

        self.node.set_driver_internal_info('irmc_ipmi_succeed', False)
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.node.driver_info['irmc_sensor_method'] = 'scci'
            sensor_dict = irmc_management.IRMCManagement().get_sensors_data(
                task)

        self.assertEqual(len(sensor_dict), 0)

    @mock.patch.object(ipmitool.IPMIManagement, 'get_sensors_data',
                       spec_set=True, autospec=True)
    def test_management_interface_get_sensors_data_ipmitool_ok_ipmi(
            self,
            get_sensors_data_mock):
        """'irmc_sensor_method' = 'ipmitool' specified."""
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.node.driver_info['irmc_sensor_method'] = 'ipmitool'
            task.driver.management.get_sensors_data(task)
            get_sensors_data_mock.assert_called_once_with(
                task.driver.management, task)

    @mock.patch.object(ipmitool.IPMIManagement, 'get_sensors_data',
                       spec_set=True, autospec=True)
    def test_management_interface_get_sensors_data_ipmitool_ng_redfish(
            self,
            get_sensors_data_mock):
        """'irmc_sensor_method' = 'ipmitool' specified."""
        self.node.set_driver_internal_info('irmc_ipmi_succeed', False)
        self.node.save()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.node.driver_info['irmc_sensor_method'] = 'ipmitool'
            self.assertRaises(exception.InvalidParameterValue,
                              task.driver.management.get_sensors_data, task)

    @mock.patch.object(irmc_common, 'get_irmc_report', spec_set=True,
                       autospec=True)
    def test_management_interface_get_sensors_data_exception(
            self,
            get_irmc_report_mock):
        """'FailedToGetSensorData Exception."""

        get_irmc_report_mock.side_effect = exception.InvalidParameterValue(
            "Fake Error")
        irmc_management.irmc.scci.SCCIInvalidInputError = Exception
        irmc_management.irmc.scci.SCCIClientError = Exception

        with task_manager.acquire(self.context, self.node.uuid) as task:
            task.node.driver_info['irmc_sensor_method'] = 'scci'
            e = self.assertRaises(
                exception.FailedToGetSensorData,
                irmc_management.IRMCManagement().get_sensors_data, task)
        self.assertEqual("Failed to get sensor data for node %s. "
                         "Error: Fake Error" % self.node.uuid, str(e))

    @mock.patch.object(redfish_management.RedfishManagement, 'detect_vendor',
                       spec_set=True, autospec=True)
    @mock.patch.object(ipmitool.IPMIManagement, 'detect_vendor',
                       spec_set=True, autospec=True)
    def test_management_interface_detect_vendor_ipmi(self,
                                                     ipmimgmt_detectv_mock,
                                                     redfishmgmt_detectv_mock):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', True)
        self.node.save()
        irmc_mgmt_inst = irmc_management.IRMCManagement()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            irmc_mgmt_inst.detect_vendor(task)
            ipmimgmt_detectv_mock.assert_called_once_with(irmc_mgmt_inst, task)
            redfishmgmt_detectv_mock.assert_not_called()

    @mock.patch.object(redfish_management.RedfishManagement, 'detect_vendor',
                       spec_set=True, autospec=True)
    @mock.patch.object(ipmitool.IPMIManagement, 'detect_vendor',
                       spec_set=True, autospec=True)
    def test_management_interface_detect_vendor_redfish(
            self, ipmimgmt_detectv_mock, redfishmgmt_detectv_mock):
        self.node.set_driver_internal_info('irmc_ipmi_succeed', False)
        self.node.save()
        ipmimgmt_detectv_mock.side_effect = exception.IPMIFailure
        irmc_mgmt_inst = irmc_management.IRMCManagement()
        with task_manager.acquire(self.context, self.node.uuid) as task:
            irmc_mgmt_inst.detect_vendor(task)
            redfishmgmt_detectv_mock.assert_called_once_with(
                irmc_mgmt_inst, task)

    @mock.patch.object(irmc_management.LOG, 'error', spec_set=True,
                       autospec=True)
    @mock.patch.object(irmc_common, 'get_irmc_client', spec_set=True,
                       autospec=True)
    def test_management_interface_inject_nmi_ok(self, mock_get_irmc_client,
                                                mock_log):
        irmc_client = mock_get_irmc_client.return_value
        with task_manager.acquire(self.context, self.node.uuid) as task:
            irmc_management.IRMCManagement().inject_nmi(task)

            irmc_client.assert_called_once_with(
                irmc_management.irmc.scci.POWER_RAISE_NMI)
            self.assertFalse(mock_log.called)

    @mock.patch.object(irmc_management.LOG, 'error', spec_set=True,
                       autospec=True)
    @mock.patch.object(irmc_common, 'get_irmc_client', spec_set=True,
                       autospec=True)
    def test_management_interface_inject_nmi_fail(self, mock_get_irmc_client,
                                                  mock_log):
        irmc_client = mock_get_irmc_client.return_value
        irmc_client.side_effect = Exception()
        irmc_management.irmc.scci.SCCIClientError = Exception

        with task_manager.acquire(self.context, self.node.uuid) as task:
            self.assertRaises(exception.IRMCOperationError,
                              irmc_management.IRMCManagement().inject_nmi,
                              task)

            irmc_client.assert_called_once_with(
                irmc_management.irmc.scci.POWER_RAISE_NMI)
            self.assertTrue(mock_log.called)

    @mock.patch.object(irmc_management, '_restore_bios_config',
                       spec_set=True, autospec=True)
    def test_management_interface_restore_irmc_bios_config(self,
                                                           mock_restore_bios):
        with task_manager.acquire(self.context, self.node.uuid) as task:
            result = task.driver.management.restore_irmc_bios_config(task)
            self.assertIsNone(result)
            mock_restore_bios.assert_called_once_with(task)