summaryrefslogtreecommitdiff
path: root/ironic/tests/unit/drivers/modules/redfish/test_bios.py
blob: f9146f3b6734d3a6801315a788f9bf118a4a4ff5 (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
# Copyright 2018 DMTF. 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.

from unittest import mock

from oslo_utils import importutils

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 deploy_utils
from ironic.drivers.modules.redfish import bios as redfish_bios
from ironic.drivers.modules.redfish import boot as redfish_boot
from ironic.drivers.modules.redfish import utils as redfish_utils
from ironic import objects
from ironic.tests.unit.db import base as db_base
from ironic.tests.unit.db import utils as db_utils
from ironic.tests.unit.objects import utils as obj_utils

sushy = importutils.try_import('sushy')

INFO_DICT = db_utils.get_test_redfish_info()


class NoBiosSystem(object):
    identity = '/redfish/v1/Systems/1234'

    @property
    def bios(self):
        raise sushy.exceptions.MissingAttributeError(attribute='Bios',
                                                     resource=self)


@mock.patch('oslo_utils.eventletutils.EventletEvent.wait',
            lambda *args, **kwargs: None)
class RedfishBiosTestCase(db_base.DbTestCase):

    def setUp(self):
        super(RedfishBiosTestCase, self).setUp()
        self.config(enabled_bios_interfaces=['redfish'],
                    enabled_hardware_types=['redfish'],
                    enabled_power_interfaces=['redfish'],
                    enabled_boot_interfaces=['redfish-virtual-media'],
                    enabled_management_interfaces=['redfish'])
        self.node = obj_utils.create_test_node(
            self.context, driver='redfish', driver_info=INFO_DICT)

    @mock.patch.object(redfish_bios, 'sushy', None)
    def test_loading_error(self):
        self.assertRaisesRegex(
            exception.DriverLoadError,
            'Unable to import the sushy library',
            redfish_bios.RedfishBIOS)

    def test_get_properties(self):
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            properties = task.driver.get_properties()
            for prop in redfish_utils.COMMON_PROPERTIES:
                self.assertIn(prop, properties)

    @mock.patch.object(redfish_utils, 'parse_driver_info', autospec=True)
    def test_validate(self, mock_parse_driver_info):
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            task.driver.bios.validate(task)
            mock_parse_driver_info.assert_called_once_with(task.node)

    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    @mock.patch.object(objects, 'BIOSSettingList', autospec=True)
    def test_cache_bios_settings_noop(self, mock_setting_list,
                                      mock_get_system):
        create_list = []
        update_list = []
        delete_list = []
        nochange_list = [{'name': 'EmbeddedSata', 'value': 'Raid'},
                         {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        mock_setting_list.sync_node_setting.return_value = (
            create_list, update_list, delete_list, nochange_list
        )

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            settings = {'foo': 'bar'}
            mock_get_system.return_value.bios.attributes = settings

            task.driver.bios.cache_bios_settings(task)
            mock_get_system.assert_called_once_with(task.node)
            mock_setting_list.sync_node_setting.assert_called_once_with(
                task.context, task.node.id,
                [{'name': 'foo', 'value': 'bar'}])

            mock_setting_list.create.assert_not_called()
            mock_setting_list.save.assert_not_called()
            mock_setting_list.delete.assert_not_called()

    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    @mock.patch.object(objects, 'BIOSSettingList', autospec=True)
    def test_cache_bios_settings_no_bios(self, mock_setting_list,
                                         mock_get_system):
        create_list = []
        update_list = []
        delete_list = []
        nochange_list = [{'name': 'EmbeddedSata', 'value': 'Raid'},
                         {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        mock_setting_list.sync_node_setting.return_value = (
            create_list, update_list, delete_list, nochange_list
        )
        mock_get_system.return_value = NoBiosSystem()

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            self.assertRaisesRegex(exception.UnsupportedDriverExtension,
                                   'BIOS settings are not supported',
                                   task.driver.bios.cache_bios_settings, task)
            mock_get_system.assert_called_once_with(task.node)
            mock_setting_list.sync_node_setting.assert_not_called()
            mock_setting_list.create.assert_not_called()
            mock_setting_list.save.assert_not_called()
            mock_setting_list.delete.assert_not_called()

    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    @mock.patch.object(objects, 'BIOSSettingList', autospec=True)
    def test_cache_bios_settings(self, mock_setting_list, mock_get_system):
        create_list = [{'name': 'DebugMode', 'value': 'enabled'}]
        update_list = [{'name': 'BootMode', 'value': 'Uefi'},
                       {'name': 'NicBoot2', 'value': 'NetworkBoot'}]
        delete_list = [{'name': 'AdminPhone', 'value': '555-867-5309'}]
        nochange_list = [{'name': 'EmbeddedSata', 'value': 'Raid'},
                         {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        delete_names = []
        for setting in delete_list:
            delete_names.append(setting.get('name'))
        mock_setting_list.sync_node_setting.return_value = (
            create_list, update_list, delete_list, nochange_list
        )

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            settings = {'foo': 'bar'}
            mock_get_system.return_value.bios.attributes = settings

            task.driver.bios.cache_bios_settings(task)
            mock_get_system.assert_called_once_with(task.node)
            mock_setting_list.sync_node_setting.assert_called_once_with(
                task.context, task.node.id,
                [{'name': 'foo', 'value': 'bar'}])

            mock_setting_list.create.assert_called_once_with(
                task.context, task.node.id, create_list)
            mock_setting_list.save.assert_called_once_with(
                task.context, task.node.id, update_list)
            mock_setting_list.delete.assert_called_once_with(
                task.context, task.node.id, delete_names)

    @mock.patch.object(manager_utils, 'is_fast_track', autospec=True)
    @mock.patch.object(redfish_boot.RedfishVirtualMediaBoot, 'prepare_ramdisk',
                       spec_set=True, autospec=True)
    @mock.patch.object(deploy_utils, 'build_agent_options', autospec=True)
    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    @mock.patch.object(manager_utils, 'node_power_action', autospec=True)
    def _test_step_pre_reboot(self, mock_power_action, mock_get_system,
                              mock_build_agent_options, mock_prepare,
                              mock_fast_track, fast_track=False):
        mock_fast_track.return_value = fast_track
        if self.node.clean_step:
            step_data = self.node.clean_step
            check_fields = ['cleaning_reboot', 'skip_current_clean_step']
            expected_ret = states.CLEANWAIT
        else:
            step_data = self.node.deploy_step
            check_fields = ['deployment_reboot', 'skip_current_deploy_step']
            expected_ret = states.DEPLOYWAIT
        data = step_data['argsinfo'].get('settings', None)
        step = step_data['step']
        if step == 'factory_reset':
            check_fields.append('post_factory_reset_reboot_requested')
        elif step == 'apply_configuration':
            check_fields.append('post_config_reboot_requested')
            attributes = {s['name']: s['value'] for s in data}
        mock_build_agent_options.return_value = {'a': 'b'}
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            bios = mock_get_system(task.node).bios
            if step == 'factory_reset':
                ret = task.driver.bios.factory_reset(task)
            if step == 'apply_configuration':
                bios.supported_apply_times = []
                ret = task.driver.bios.apply_configuration(task, data)
            mock_get_system.assert_called_with(task.node)
            if fast_track:
                mock_power_action.assert_has_calls([
                    mock.call(task, states.POWER_OFF),
                    mock.call(task, states.REBOOT),
                ])
            else:
                mock_power_action.assert_called_once_with(task, states.REBOOT)
            if step == 'factory_reset':
                bios.reset_bios.assert_called_once()
            if step == 'apply_configuration':
                bios.set_attributes.assert_called_once_with(
                    attributes, apply_time=None)
            mock_build_agent_options.assert_called_once_with(task.node)
            mock_prepare.assert_called_once_with(mock.ANY, task, {'a': 'b'})
            for field in check_fields:
                self.assertIn(field, task.node.driver_internal_info)
            self.assertEqual(expected_ret, ret)

    def test_factory_reset_step_pre_reboot_cleaning(self):
        self.node.clean_step = {'priority': 100, 'interface': 'bios',
                                'step': 'factory_reset', 'argsinfo': {}}
        self.node.save()
        self._test_step_pre_reboot()

    def test_factory_reset_step_pre_reboot_deploying(self):
        self.node.deploy_step = {'priority': 100, 'interface': 'bios',
                                 'step': 'factory_reset', 'argsinfo': {}}
        self.node.save()
        self._test_step_pre_reboot()

    def test_factory_reset_step_pre_reboot_fast_track(self):
        self.node.clean_step = {'priority': 100, 'interface': 'bios',
                                'step': 'factory_reset', 'argsinfo': {}}
        self.node.save()
        self._test_step_pre_reboot(fast_track=True)

    def test_apply_conf_step_pre_reboot_cleaning(self):
        data = [{'name': 'ProcTurboMode', 'value': 'Disabled'},
                {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        self.node.clean_step = {'priority': 100, 'interface': 'bios',
                                'step': 'apply_configuration',
                                'argsinfo': {'settings': data}}
        self.node.save()
        self._test_step_pre_reboot()

    def test_apply_conf_step_pre_reboot_deploying(self):
        data = [{'name': 'ProcTurboMode', 'value': 'Disabled'},
                {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        self.node.deploy_step = {'priority': 100, 'interface': 'bios',
                                 'step': 'apply_configuration',
                                 'argsinfo': {'settings': data}}
        self.node.save()
        self._test_step_pre_reboot()

    def test_apply_conf_step_pre_reboot_fast_track(self):
        data = [{'name': 'ProcTurboMode', 'value': 'Disabled'},
                {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        self.node.clean_step = {'priority': 100, 'interface': 'bios',
                                'step': 'apply_configuration',
                                'argsinfo': {'settings': data}}
        self.node.save()
        self._test_step_pre_reboot(fast_track=True)

    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    def _test_step_post_reboot(self, mock_get_system,
                               attributes_after_reboot=None):
        if self.node.deploy_step:
            step_data = self.node.deploy_step
        else:
            step_data = self.node.clean_step
        data = step_data['argsinfo'].get('settings', None)
        step = step_data['step']
        if step == 'factory_reset':
            check_fields = ['post_factory_reset_reboot_requested']
        if step == 'apply_configuration':
            mock_bios = mock.Mock()
            # if attributes after reboot not provided then mimic success
            # by returning the same as requested
            mock_bios.attributes = attributes_after_reboot \
                or self.node.driver_internal_info['requested_bios_attrs']
            mock_bios.get_attribute_registry = []
            mock_system = mock.Mock()
            mock_system.bios = mock_bios
            mock_get_system.return_value = mock_system
            check_fields = ['post_config_reboot_requested',
                            'requested_bios_attrs']
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            if step == 'factory_reset':
                task.driver.bios.factory_reset(task)
            if step == 'apply_configuration':
                task.driver.bios.apply_configuration(task, data)
            mock_get_system.assert_called_with(task.node)
            info = task.node.driver_internal_info
            for field in check_fields:
                self.assertNotIn(field, info)

    def test_factory_reset_post_reboot_cleaning(self):
        self.node.clean_step = {'priority': 100, 'interface': 'bios',
                                'step': 'factory_reset', 'argsinfo': {}}
        node = self.node
        driver_internal_info = node.driver_internal_info
        driver_internal_info['post_factory_reset_reboot_requested'] = True
        node.driver_internal_info = driver_internal_info
        node.save()
        self._test_step_post_reboot()

    def test_factory_reset_post_reboot_deploying(self):
        self.node.deploy_step = {'priority': 100, 'interface': 'bios',
                                 'step': 'factory_reset', 'argsinfo': {}}
        node = self.node
        driver_internal_info = node.driver_internal_info
        driver_internal_info['post_factory_reset_reboot_requested'] = True
        node.driver_internal_info = driver_internal_info
        node.save()
        self._test_step_post_reboot()

    @mock.patch.object(manager_utils, 'cleaning_error_handler', autospec=True)
    @mock.patch.object(manager_utils, 'deploying_error_handler', autospec=True)
    def test_apply_conf_post_reboot_cleaning(self,
                                             mock_deploying_error_handler,
                                             mock_cleaning_error_handler):
        data = [{'name': 'ProcTurboMode', 'value': 'Enabled'},
                {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        self.node.clean_step = {'priority': 100, 'interface': 'bios',
                                'step': 'apply_configuration',
                                'argsinfo': {'settings': data}}
        requested_attrs = {'ProcTurboMode': 'Enabled',
                           'NicBoot1': 'NetworkBoot'}
        node = self.node
        driver_internal_info = node.driver_internal_info
        driver_internal_info['post_config_reboot_requested'] = True
        driver_internal_info['requested_bios_attrs'] = requested_attrs
        self.node.driver_internal_info = driver_internal_info
        self.node.save()
        self._test_step_post_reboot()
        mock_cleaning_error_handler.assert_not_called()
        mock_deploying_error_handler.assert_not_called()

    @mock.patch.object(manager_utils, 'cleaning_error_handler', autospec=True)
    def test_apply_conf_post_reboot_cleaning_failed(
            self, mock_cleaning_error_handler):
        data = [{'name': 'ProcTurboMode', 'value': 'Enabled',
                 'registry': {'description': 'Turbo mode'}}]
        self.node.clean_step = {'priority': 100, 'interface': 'bios',
                                'step': 'apply_configuration',
                                'argsinfo': {'settings': data}}
        requested_attrs = {'ProcTurboMode': 'Enabled'}
        attributes_after_reboot = {'ProcTurboMode': 'Disabled'}
        node = self.node
        driver_internal_info = node.driver_internal_info
        driver_internal_info['post_config_reboot_requested'] = True
        driver_internal_info['requested_bios_attrs'] = requested_attrs
        self.node.driver_internal_info = driver_internal_info
        self.node.provision_state = states.CLEANING
        self.node.save()
        self._test_step_post_reboot(
            attributes_after_reboot=attributes_after_reboot)
        mock_cleaning_error_handler.assert_called_once()

    def test_apply_conf_post_reboot_deploying(self):
        data = [{'name': 'ProcTurboMode', 'value': 'Enabled',
                 'registry': {'description': 'Turbo mode'}},
                {'name': 'NicBoot1', 'value': 'NetworkBoot',
                 'registry': {'description': 'Boot off network'}}]
        self.node.deploy_step = {'priority': 100, 'interface': 'bios',
                                 'step': 'apply_configuration',
                                 'argsinfo': {'settings': data}}
        requested_attrs = {'ProcTurboMode': 'Enabled',
                           'NicBoot1': 'NetworkBoot'}
        node = self.node
        driver_internal_info = node.driver_internal_info
        driver_internal_info['post_config_reboot_requested'] = True
        driver_internal_info['requested_bios_attrs'] = requested_attrs
        self.node.driver_internal_info = driver_internal_info
        self.node.save()
        self._test_step_post_reboot()

    @mock.patch.object(manager_utils, 'deploying_error_handler', autospec=True)
    def test_apply_conf_post_reboot_deploying_failed(
            self, mock_deploying_error_handler):
        data = [{'name': 'ProcTurboMode', 'value': 'Enabled',
                 'registry': {'description': 'Turbo mode'}}]
        self.node.deploy_step = {'priority': 100, 'interface': 'bios',
                                 'step': 'apply_configuration',
                                 'argsinfo': {'settings': data}}
        requested_attrs = {'ProcTurboMode': 'Enabled'}
        attributes_after_reboot = {'ProcTurboMode': 'Disabled'}
        node = self.node
        driver_internal_info = node.driver_internal_info
        driver_internal_info['post_config_reboot_requested'] = True
        driver_internal_info['requested_bios_attrs'] = requested_attrs
        self.node.driver_internal_info = driver_internal_info
        self.node.provision_state = states.DEPLOYWAIT
        self.node.save()
        self._test_step_post_reboot(
            attributes_after_reboot=attributes_after_reboot)
        mock_deploying_error_handler.assert_called_once()

    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    def test_factory_reset_fail(self, mock_get_system):
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            bios = mock_get_system(task.node).bios
            bios.reset_bios.side_effect = sushy.exceptions.SushyError
            self.assertRaisesRegex(
                exception.RedfishError, 'BIOS factory reset failed',
                task.driver.bios.factory_reset, task)

    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    def test_factory_reset_not_supported(self, mock_get_system):
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            mock_get_system.return_value = NoBiosSystem()
            self.assertRaisesRegex(
                exception.RedfishError, 'BIOS factory reset failed',
                task.driver.bios.factory_reset, task)

    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    def test_apply_configuration_not_supported(self, mock_get_system):
        settings = [{'name': 'ProcTurboMode', 'value': 'Disabled'},
                    {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            mock_get_system.return_value = NoBiosSystem()
            self.assertRaisesRegex(exception.RedfishError,
                                   'BIOS settings are not supported',
                                   task.driver.bios.apply_configuration,
                                   task, settings)
            mock_get_system.assert_called_once_with(task.node)

    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    def test_check_bios_attrs(self, mock_get_system):
        settings = [{'name': 'ProcTurboMode', 'value': 'Disabled'},
                    {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        requested_attrs = {'ProcTurboMode': 'Enabled',
                           'NicBoot1': 'NetworkBoot'}
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            attributes = mock_get_system(task.node).bios.attributes
            task.node.driver_internal_info[
                'post_config_reboot_requested'] = True
            task.node.driver_internal_info[
                'requested_bios_attrs'] = requested_attrs
            task.driver.bios._check_bios_attrs = mock.MagicMock()
            task.driver.bios.apply_configuration(task, settings)
            task.driver.bios._check_bios_attrs \
                .assert_called_once_with(task, attributes, requested_attrs)

    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    def test_apply_configuration_fail(self, mock_get_system):
        settings = [{'name': 'ProcTurboMode', 'value': 'Disabled'},
                    {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            bios = mock_get_system(task.node).bios
            bios.set_attributes.side_effect = sushy.exceptions.SushyError
            self.assertRaisesRegex(
                exception.RedfishError, 'BIOS apply configuration failed',
                task.driver.bios.apply_configuration, task, settings)

    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    def test_post_configuration(self, mock_get_system):
        settings = [{'name': 'ProcTurboMode', 'value': 'Disabled'},
                    {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            task.driver.bios.post_configuration = mock.MagicMock()
            task.driver.bios.apply_configuration(task, settings)
            task.driver.bios.post_configuration\
                .assert_called_once_with(task, settings)

    @mock.patch.object(redfish_boot.RedfishVirtualMediaBoot, 'prepare_ramdisk',
                       spec_set=True, autospec=True)
    @mock.patch.object(deploy_utils, 'build_agent_options', autospec=True)
    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    @mock.patch.object(manager_utils, 'node_power_action', autospec=True)
    def test_apply_configuration_apply_time_immediate(self, mock_power_action,
                                                      mock_get_system,
                                                      mock_build_agent_options,
                                                      mock_prepare):
        settings = [{'name': 'ProcTurboMode', 'value': 'Disabled'},
                    {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            bios = mock_get_system(task.node).bios
            bios.supported_apply_times = ['immediate']

            task.driver.bios.apply_configuration(task, settings)

            bios.set_attributes.assert_called_once_with(
                {s['name']: s['value'] for s in settings},
                apply_time=None)

    @mock.patch.object(redfish_boot.RedfishVirtualMediaBoot, 'prepare_ramdisk',
                       spec_set=True, autospec=True)
    @mock.patch.object(deploy_utils, 'build_agent_options', autospec=True)
    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    @mock.patch.object(manager_utils, 'node_power_action', autospec=True)
    def test_apply_configuration_apply_time_on_reset(self, mock_power_action,
                                                     mock_get_system,
                                                     mock_build_agent_options,
                                                     mock_prepare):
        settings = [{'name': 'ProcTurboMode', 'value': 'Disabled'},
                    {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            bios = mock_get_system(task.node).bios
            bios.supported_apply_times = [sushy.APPLY_TIME_ON_RESET]

            task.driver.bios.apply_configuration(task, settings)

            bios.set_attributes.assert_called_once_with(
                {s['name']: s['value'] for s in settings},
                apply_time=sushy.APPLY_TIME_ON_RESET)

    @mock.patch.object(redfish_boot.RedfishVirtualMediaBoot, 'prepare_ramdisk',
                       spec_set=True, autospec=True)
    @mock.patch.object(deploy_utils, 'build_agent_options', autospec=True)
    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    @mock.patch.object(manager_utils, 'node_power_action', autospec=True)
    def test_apply_configuration_no_supported_apply_times(
            self, mock_power_action, mock_get_system, mock_build_agent_options,
            mock_prepare):
        settings = [{'name': 'ProcTurboMode', 'value': 'Disabled'},
                    {'name': 'NicBoot1', 'value': 'NetworkBoot'}]
        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=False) as task:
            bios = mock_get_system(task.node).bios
            bios.supported_apply_times = None

            task.driver.bios.apply_configuration(task, settings)

            bios.set_attributes.assert_called_once_with(
                {s['name']: s['value'] for s in settings},
                apply_time=None)


@mock.patch('oslo_utils.eventletutils.EventletEvent.wait',
            lambda *args, **kwargs: None)
class RedfishBiosRegistryTestCase(db_base.DbTestCase):

    def setUp(self):
        super(RedfishBiosRegistryTestCase, self).setUp()
        self.config(enabled_bios_interfaces=['redfish'],
                    enabled_hardware_types=['redfish'],
                    enabled_power_interfaces=['redfish'],
                    enabled_boot_interfaces=['redfish-virtual-media'],
                    enabled_management_interfaces=['redfish'])
        self.node = obj_utils.create_test_node(
            self.context, driver='redfish', driver_info=INFO_DICT)

        self.settings = {'SystemModelName': 'UltraSumma',
                         'DcuStreamPrefetcher': 'Enabled',
                         'BootDelay': 10}

        class AttributeField():

            def __init__(self):
                self.name = None
                self.allowable_values = None
                self.attribute_type = None
                self.lower_bound = None
                self.max_length = None
                self.min_length = None
                self.read_only = None
                self.reset_required = None
                self.type = None
                self.unique = None
                self.upper_bound = None

        class AttributeRegistryEntryField():

            def __init__(self, num_entries):
                self.attributes = []
                for _ in range(num_entries):
                    self.attributes.append(AttributeField())

        class AttributeRegistryTest():

            def __init__(self, num_entries):
                self.registry_entries = AttributeRegistryEntryField(
                    num_entries)

        self.registry = AttributeRegistryTest(4)
        self.registry.registry_entries.attributes[0].name = "SystemModelName"
        self.registry.registry_entries.attributes[0].attribute_type = "String"
        self.registry.registry_entries.attributes[0].max_length = 32
        self.registry.registry_entries.attributes[0].read_only = True
        self.registry.registry_entries.attributes[0].unique = True
        self.registry.registry_entries.attributes[1].name =\
            "DcuStreamPrefetcher"
        self.registry.registry_entries.attributes[1].attribute_type =\
            "Enumeration"
        self.registry.registry_entries.attributes[1].read_only = False
        self.registry.registry_entries.attributes[1].allowable_values =\
            [{'ValueName': 'Enabled', 'ValueDisplayName': 'Enabled'},
             {'ValueName': 'Disabled', 'ValueDisplayName': 'Disabled'}]
        self.registry.registry_entries.attributes[2].name = "BootDelay"
        self.registry.registry_entries.attributes[2].attribute_type = "Integer"
        self.registry.registry_entries.attributes[2].lower_bound = 5
        self.registry.registry_entries.attributes[2].upper_bound = 30
        self.registry.registry_entries.attributes[2].reset_required = True
        self.registry.registry_entries.attributes[3].name = "SomeAttribute"
        self.registry.registry_entries.attributes[3].attribute_type = "String"
        self.registry.registry_entries.attributes[3].max_length = 32
        self.registry.registry_entries.attributes[3].read_only = True

        self.expected_no_registry = [
            {'name': 'SystemModelName', 'value': 'UltraSumma'},
            {'name': 'DcuStreamPrefetcher', 'value': 'Enabled'},
            {'name': 'BootDelay', 'value': 10}]

    @mock.patch.object(objects, 'BIOSSettingList', autospec=True)
    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    def test_cache_bios_registry_save(self, mock_get_system,
                                      mock_setting_list):
        create_list = []
        update_list = []
        delete_list = []
        nochange_list = []
        mock_setting_list.sync_node_setting.return_value = (
            create_list, update_list, delete_list, nochange_list
        )

        expected = [{'name': 'SystemModelName', 'value': 'UltraSumma',
                     'allowable_values': None, 'lower_bound': None,
                     'max_length': 32, 'min_length': None, 'read_only': True,
                     'reset_required': None, 'attribute_type': 'String',
                     'unique': True, 'upper_bound': None},
                    {'name': 'DcuStreamPrefetcher', 'value': 'Enabled',
                     'allowable_values': ['Enabled', 'Disabled'],
                     'lower_bound': None, 'max_length': None,
                     'min_length': None, 'read_only': False,
                     'reset_required': None, 'attribute_type':
                     'Enumeration', 'unique': None, 'upper_bound': None},
                    {'name': 'BootDelay', 'value': 10,
                     'allowable_values': None, 'lower_bound': 5,
                     'max_length': None, 'min_length': None, 'read_only': None,
                     'reset_required': True, 'attribute_type': 'Integer',
                     'unique': None, 'upper_bound': 30}]

        mock_get_system.return_value.bios.attributes = self.settings
        mock_get_system.return_value.bios.get_attribute_registry.\
            return_value = self.registry

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            task.driver.bios.cache_bios_settings(task)
            mock_get_system.assert_called_once_with(task.node)
            mock_setting_list.sync_node_setting.assert_called_once_with(
                task.context, task.node.id, expected)

    @mock.patch.object(objects, 'BIOSSettingList', autospec=True)
    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    def test_cache_empty_bios_registry(self, mock_get_system,
                                       mock_setting_list):
        create_list = []
        update_list = []
        delete_list = []
        nochange_list = []
        mock_setting_list.sync_node_setting.return_value = (
            create_list, update_list, delete_list, nochange_list
        )

        mock_get_system.return_value.bios.attributes = self.settings
        mock_get_system.return_value.bios.get_attribute_registry.\
            return_value = {}

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            task.driver.bios.cache_bios_settings(task)
            mock_setting_list.sync_node_setting.assert_called_once_with(
                task.context, task.node.id, self.expected_no_registry)

    @mock.patch.object(objects, 'BIOSSettingList', autospec=True)
    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    def test_cache_no_bios_registry(self, mock_get_system,
                                    mock_setting_list):
        create_list = []
        update_list = []
        delete_list = []
        nochange_list = []
        mock_setting_list.sync_node_setting.return_value = (
            create_list, update_list, delete_list, nochange_list
        )

        mock_get_system.return_value.bios.attributes = self.settings
        mock_get_system.return_value.bios.get_attribute_registry.\
            return_value = None

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            task.driver.bios.cache_bios_settings(task)
            mock_setting_list.sync_node_setting.assert_called_once_with(
                task.context, task.node.id, self.expected_no_registry)

    @mock.patch.object(objects, 'BIOSSettingList', autospec=True)
    @mock.patch.object(redfish_utils, 'get_system', autospec=True)
    def test_cache_exception_bios_registry(self, mock_get_system,
                                           mock_setting_list):
        create_list = []
        update_list = []
        delete_list = []
        nochange_list = []
        mock_setting_list.sync_node_setting.return_value = (
            create_list, update_list, delete_list, nochange_list
        )

        mock_get_system.return_value.bios.attributes = self.settings

        with task_manager.acquire(self.context, self.node.uuid,
                                  shared=True) as task:
            bios = mock_get_system(task.node).bios
            bios.reset_bios.side_effect = sushy.exceptions.SushyError

            task.driver.bios.cache_bios_settings(task)
            mock_setting_list.sync_node_setting.assert_called_once_with(
                task.context, task.node.id, self.expected_no_registry)