"Fossies" - the Fresh Open Source Software Archive

Member "ironic-16.0.3/ironic/tests/unit/drivers/modules/redfish/test_boot.py" (18 Jan 2021, 39264 Bytes) of package /linux/misc/openstack/ironic-16.0.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_boot.py": 16.0.2_vs_16.0.3.

    1 # Copyright 2019 Red Hat, Inc.
    2 # All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 
   16 from unittest import mock
   17 
   18 from oslo_utils import importutils
   19 
   20 from ironic.common import boot_devices
   21 from ironic.common import exception
   22 from ironic.common import states
   23 from ironic.conductor import task_manager
   24 from ironic.drivers.modules import boot_mode_utils
   25 from ironic.drivers.modules import deploy_utils
   26 from ironic.drivers.modules import image_utils
   27 from ironic.drivers.modules.redfish import boot as redfish_boot
   28 from ironic.drivers.modules.redfish import utils as redfish_utils
   29 from ironic.tests.unit.db import base as db_base
   30 from ironic.tests.unit.db import utils as db_utils
   31 from ironic.tests.unit.objects import utils as obj_utils
   32 
   33 sushy = importutils.try_import('sushy')
   34 
   35 INFO_DICT = db_utils.get_test_redfish_info()
   36 
   37 
   38 @mock.patch('oslo_utils.eventletutils.EventletEvent.wait',
   39             lambda *args, **kwargs: None)
   40 class RedfishVirtualMediaBootTestCase(db_base.DbTestCase):
   41 
   42     def setUp(self):
   43         super(RedfishVirtualMediaBootTestCase, self).setUp()
   44         self.config(enabled_hardware_types=['redfish'],
   45                     enabled_power_interfaces=['redfish'],
   46                     enabled_boot_interfaces=['redfish-virtual-media'],
   47                     enabled_management_interfaces=['redfish'],
   48                     enabled_inspect_interfaces=['redfish'],
   49                     enabled_bios_interfaces=['redfish'])
   50         self.node = obj_utils.create_test_node(
   51             self.context, driver='redfish', driver_info=INFO_DICT)
   52 
   53     @mock.patch.object(redfish_boot, 'sushy', None)
   54     def test_loading_error(self):
   55         self.assertRaisesRegex(
   56             exception.DriverLoadError,
   57             'Unable to import the sushy library',
   58             redfish_boot.RedfishVirtualMediaBoot)
   59 
   60     def test_parse_driver_info_deploy(self):
   61         with task_manager.acquire(self.context, self.node.uuid,
   62                                   shared=True) as task:
   63             task.node.driver_info.update(
   64                 {'deploy_kernel': 'kernel',
   65                  'deploy_ramdisk': 'ramdisk',
   66                  'bootloader': 'bootloader'}
   67             )
   68 
   69             actual_driver_info = redfish_boot._parse_driver_info(task.node)
   70 
   71             self.assertIn('kernel', actual_driver_info['deploy_kernel'])
   72             self.assertIn('ramdisk', actual_driver_info['deploy_ramdisk'])
   73             self.assertIn('bootloader', actual_driver_info['bootloader'])
   74 
   75     def test_parse_driver_info_rescue(self):
   76         with task_manager.acquire(self.context, self.node.uuid,
   77                                   shared=True) as task:
   78             task.node.provision_state = states.RESCUING
   79             task.node.driver_info.update(
   80                 {'rescue_kernel': 'kernel',
   81                  'rescue_ramdisk': 'ramdisk',
   82                  'bootloader': 'bootloader'}
   83             )
   84 
   85             actual_driver_info = redfish_boot._parse_driver_info(task.node)
   86 
   87             self.assertIn('kernel', actual_driver_info['rescue_kernel'])
   88             self.assertIn('ramdisk', actual_driver_info['rescue_ramdisk'])
   89             self.assertIn('bootloader', actual_driver_info['bootloader'])
   90 
   91     def test_parse_driver_info_exc(self):
   92         with task_manager.acquire(self.context, self.node.uuid,
   93                                   shared=True) as task:
   94             self.assertRaises(exception.MissingParameterValue,
   95                               redfish_boot._parse_driver_info,
   96                               task.node)
   97 
   98     def _test_parse_driver_info_from_conf(self, mode='deploy'):
   99         with task_manager.acquire(self.context, self.node.uuid,
  100                                   shared=True) as task:
  101             if mode == 'rescue':
  102                 task.node.provision_state = states.RESCUING
  103 
  104             expected = {
  105                 '%s_ramdisk' % mode: 'glance://%s_ramdisk_uuid' % mode,
  106                 '%s_kernel' % mode: 'glance://%s_kernel_uuid' % mode
  107             }
  108 
  109             self.config(group='conductor', **expected)
  110 
  111             image_info = redfish_boot._parse_driver_info(task.node)
  112 
  113             for key, value in expected.items():
  114                 self.assertEqual(value, image_info[key])
  115 
  116     def test_parse_driver_info_from_conf_deploy(self):
  117         self._test_parse_driver_info_from_conf()
  118 
  119     def test_parse_driver_info_from_conf_rescue(self):
  120         self._test_parse_driver_info_from_conf(mode='rescue')
  121 
  122     def _test_parse_driver_info_mixed_source(self, mode='deploy'):
  123         with task_manager.acquire(self.context, self.node.uuid,
  124                                   shared=True) as task:
  125             if mode == 'rescue':
  126                 task.node.provision_state = states.RESCUING
  127 
  128             kernel_config = {
  129                 '%s_kernel' % mode: 'glance://%s_kernel_uuid' % mode
  130             }
  131 
  132             ramdisk_config = {
  133                 '%s_ramdisk' % mode: 'glance://%s_ramdisk_uuid' % mode,
  134             }
  135 
  136             self.config(group='conductor', **kernel_config)
  137 
  138             task.node.driver_info.update(ramdisk_config)
  139 
  140             self.assertRaises(exception.MissingParameterValue,
  141                               redfish_boot._parse_driver_info, task.node)
  142 
  143     def test_parse_driver_info_mixed_source_deploy(self):
  144         self._test_parse_driver_info_mixed_source()
  145 
  146     def test_parse_driver_info_mixed_source_rescue(self):
  147         self._test_parse_driver_info_mixed_source(mode='rescue')
  148 
  149     def test_parse_deploy_info(self):
  150         with task_manager.acquire(self.context, self.node.uuid,
  151                                   shared=True) as task:
  152             task.node.driver_info.update(
  153                 {'deploy_kernel': 'kernel',
  154                  'deploy_ramdisk': 'ramdisk',
  155                  'bootloader': 'bootloader'}
  156             )
  157 
  158             task.node.instance_info.update(
  159                 {'image_source': 'http://boot/iso',
  160                  'kernel': 'http://kernel/img',
  161                  'ramdisk': 'http://ramdisk/img'})
  162 
  163             actual_instance_info = redfish_boot._parse_deploy_info(task.node)
  164 
  165             self.assertEqual(
  166                 'http://boot/iso', actual_instance_info['image_source'])
  167             self.assertEqual(
  168                 'http://kernel/img', actual_instance_info['kernel'])
  169             self.assertEqual(
  170                 'http://ramdisk/img', actual_instance_info['ramdisk'])
  171 
  172     def test_parse_deploy_info_exc(self):
  173         with task_manager.acquire(self.context, self.node.uuid,
  174                                   shared=True) as task:
  175             self.assertRaises(exception.MissingParameterValue,
  176                               redfish_boot._parse_deploy_info,
  177                               task.node)
  178 
  179     @mock.patch.object(redfish_utils, 'parse_driver_info', autospec=True)
  180     @mock.patch.object(deploy_utils, 'validate_image_properties',
  181                        autospec=True)
  182     @mock.patch.object(boot_mode_utils, 'get_boot_mode_for_deploy',
  183                        autospec=True)
  184     def test_validate_uefi_boot(self, mock_get_boot_mode,
  185                                 mock_validate_image_properties,
  186                                 mock_parse_driver_info):
  187         with task_manager.acquire(self.context, self.node.uuid,
  188                                   shared=True) as task:
  189             task.node.instance_info.update(
  190                 {'kernel': 'kernel',
  191                  'ramdisk': 'ramdisk',
  192                  'image_source': 'http://image/source'}
  193             )
  194 
  195             task.node.driver_info.update(
  196                 {'deploy_kernel': 'kernel',
  197                  'deploy_ramdisk': 'ramdisk',
  198                  'bootloader': 'bootloader'}
  199             )
  200 
  201             mock_get_boot_mode.return_value = 'uefi'
  202 
  203             task.driver.boot.validate(task)
  204 
  205             mock_validate_image_properties.assert_called_once_with(
  206                 mock.ANY, mock.ANY, mock.ANY)
  207 
  208     @mock.patch.object(redfish_utils, 'parse_driver_info', autospec=True)
  209     @mock.patch.object(deploy_utils, 'validate_image_properties',
  210                        autospec=True)
  211     @mock.patch.object(boot_mode_utils, 'get_boot_mode_for_deploy',
  212                        autospec=True)
  213     def test_validate_bios_boot(self, mock_get_boot_mode,
  214                                 mock_validate_image_properties,
  215                                 mock_parse_driver_info):
  216         with task_manager.acquire(self.context, self.node.uuid,
  217                                   shared=True) as task:
  218             task.node.instance_info.update(
  219                 {'kernel': 'kernel',
  220                  'ramdisk': 'ramdisk',
  221                  'image_source': 'http://image/source'}
  222             )
  223 
  224             task.node.driver_info.update(
  225                 {'deploy_kernel': 'kernel',
  226                  'deploy_ramdisk': 'ramdisk',
  227                  'bootloader': 'bootloader'}
  228             )
  229 
  230             mock_get_boot_mode.return_value = 'bios'
  231 
  232             task.driver.boot.validate(task)
  233 
  234             mock_validate_image_properties.assert_called_once_with(
  235                 mock.ANY, mock.ANY, mock.ANY)
  236 
  237     @mock.patch.object(redfish_utils, 'parse_driver_info', autospec=True)
  238     @mock.patch.object(deploy_utils, 'validate_image_properties',
  239                        autospec=True)
  240     @mock.patch.object(boot_mode_utils, 'get_boot_mode_for_deploy',
  241                        autospec=True)
  242     def test_validate_bios_boot_iso(self, mock_get_boot_mode,
  243                                     mock_validate_image_properties,
  244                                     mock_parse_driver_info):
  245         with task_manager.acquire(self.context, self.node.uuid,
  246                                   shared=True) as task:
  247             task.node.instance_info.update(
  248                 {'boot_iso': 'http://localhost/file.iso'}
  249             )
  250 
  251             task.node.driver_info.update(
  252                 {'deploy_kernel': 'kernel',
  253                  'deploy_ramdisk': 'ramdisk',
  254                  'bootloader': 'bootloader'}
  255             )
  256             # NOTE(TheJulia): Boot mode doesn't matter for this
  257             # test scenario.
  258             mock_get_boot_mode.return_value = 'bios'
  259 
  260             task.driver.boot.validate(task)
  261 
  262             mock_validate_image_properties.assert_called_once_with(
  263                 mock.ANY, mock.ANY, mock.ANY)
  264 
  265     @mock.patch.object(redfish_utils, 'parse_driver_info', autospec=True)
  266     @mock.patch.object(deploy_utils, 'validate_image_properties',
  267                        autospec=True)
  268     @mock.patch.object(boot_mode_utils, 'get_boot_mode_for_deploy',
  269                        autospec=True)
  270     def test_validate_bios_boot_iso_conflicting_image_source(
  271             self, mock_get_boot_mode,
  272             mock_validate_image_properties,
  273             mock_parse_driver_info):
  274         with task_manager.acquire(self.context, self.node.uuid,
  275                                   shared=True) as task:
  276             task.node.instance_info.update(
  277                 {'boot_iso': 'http://localhost/file.iso',
  278                  'image_source': 'http://localhost/file.img'}
  279             )
  280 
  281             task.node.driver_info.update(
  282                 {'deploy_kernel': 'kernel',
  283                  'deploy_ramdisk': 'ramdisk',
  284                  'bootloader': 'bootloader'}
  285             )
  286             # NOTE(TheJulia): Boot mode doesn't matter for this
  287             # test scenario.
  288             mock_get_boot_mode.return_value = 'bios'
  289 
  290             task.driver.boot.validate(task)
  291 
  292             mock_validate_image_properties.assert_called_once_with(
  293                 mock.ANY, mock.ANY, mock.ANY)
  294 
  295     @mock.patch.object(redfish_utils, 'parse_driver_info', autospec=True)
  296     @mock.patch.object(deploy_utils, 'validate_image_properties',
  297                        autospec=True)
  298     def test_validate_missing(self, mock_validate_image_properties,
  299                               mock_parse_driver_info):
  300         with task_manager.acquire(self.context, self.node.uuid,
  301                                   shared=True) as task:
  302             self.assertRaises(exception.MissingParameterValue,
  303                               task.driver.boot.validate, task)
  304 
  305     @mock.patch.object(redfish_utils, 'parse_driver_info', autospec=True)
  306     def test_validate_inspection(self, mock_parse_driver_info):
  307         with task_manager.acquire(self.context, self.node.uuid,
  308                                   shared=True) as task:
  309             task.node.driver_info.update(
  310                 {'deploy_kernel': 'kernel',
  311                  'deploy_ramdisk': 'ramdisk',
  312                  'bootloader': 'bootloader'}
  313             )
  314 
  315             task.driver.boot.validate_inspection(task)
  316 
  317             mock_parse_driver_info.assert_called_once_with(task.node)
  318 
  319     @mock.patch.object(redfish_utils, 'parse_driver_info', autospec=True)
  320     def test_validate_inspection_missing(self, mock_parse_driver_info):
  321         with task_manager.acquire(self.context, self.node.uuid,
  322                                   shared=True) as task:
  323             self.assertRaises(exception.UnsupportedDriverExtension,
  324                               task.driver.boot.validate_inspection, task)
  325 
  326     @mock.patch.object(redfish_boot.manager_utils, 'node_set_boot_device',
  327                        autospec=True)
  328     @mock.patch.object(image_utils, 'prepare_deploy_iso', autospec=True)
  329     @mock.patch.object(redfish_boot, '_eject_vmedia', autospec=True)
  330     @mock.patch.object(redfish_boot, '_insert_vmedia', autospec=True)
  331     @mock.patch.object(redfish_boot, '_parse_driver_info', autospec=True)
  332     @mock.patch.object(redfish_boot.manager_utils, 'node_power_action',
  333                        autospec=True)
  334     @mock.patch.object(redfish_boot, 'boot_mode_utils', autospec=True)
  335     def test_prepare_ramdisk_with_params(
  336             self, mock_boot_mode_utils, mock_node_power_action,
  337             mock__parse_driver_info, mock__insert_vmedia, mock__eject_vmedia,
  338             mock_prepare_deploy_iso, mock_node_set_boot_device):
  339 
  340         with task_manager.acquire(self.context, self.node.uuid,
  341                                   shared=False) as task:
  342             task.node.provision_state = states.DEPLOYING
  343 
  344             mock__parse_driver_info.return_value = {}
  345             mock_prepare_deploy_iso.return_value = 'image-url'
  346 
  347             task.driver.boot.prepare_ramdisk(task, {})
  348 
  349             mock_node_power_action.assert_called_once_with(
  350                 task, states.POWER_OFF)
  351 
  352             mock__eject_vmedia.assert_called_once_with(
  353                 task, sushy.VIRTUAL_MEDIA_CD)
  354 
  355             mock__insert_vmedia.assert_called_once_with(
  356                 task, 'image-url', sushy.VIRTUAL_MEDIA_CD)
  357 
  358             expected_params = {
  359                 'ipa-agent-token': mock.ANY,
  360                 'ipa-debug': '1',
  361             }
  362 
  363             mock_prepare_deploy_iso.assert_called_once_with(
  364                 task, expected_params, 'deploy', {})
  365 
  366             mock_node_set_boot_device.assert_called_once_with(
  367                 task, boot_devices.CDROM, False)
  368 
  369             mock_boot_mode_utils.sync_boot_mode.assert_called_once_with(task)
  370 
  371     @mock.patch.object(redfish_boot.manager_utils, 'node_set_boot_device',
  372                        autospec=True)
  373     @mock.patch.object(image_utils, 'prepare_deploy_iso', autospec=True)
  374     @mock.patch.object(redfish_boot, '_eject_vmedia', autospec=True)
  375     @mock.patch.object(redfish_boot, '_insert_vmedia', autospec=True)
  376     @mock.patch.object(redfish_boot, '_parse_driver_info', autospec=True)
  377     @mock.patch.object(redfish_boot.manager_utils, 'node_power_action',
  378                        autospec=True)
  379     @mock.patch.object(redfish_boot, 'boot_mode_utils', autospec=True)
  380     def test_prepare_ramdisk_no_debug(
  381             self, mock_boot_mode_utils, mock_node_power_action,
  382             mock__parse_driver_info, mock__insert_vmedia, mock__eject_vmedia,
  383             mock_prepare_deploy_iso, mock_node_set_boot_device):
  384         self.config(debug=False)
  385         with task_manager.acquire(self.context, self.node.uuid,
  386                                   shared=True) as task:
  387             task.node.provision_state = states.DEPLOYING
  388 
  389             mock__parse_driver_info.return_value = {}
  390             mock_prepare_deploy_iso.return_value = 'image-url'
  391 
  392             task.driver.boot.prepare_ramdisk(task, {})
  393 
  394             mock_node_power_action.assert_called_once_with(
  395                 task, states.POWER_OFF)
  396 
  397             mock__eject_vmedia.assert_called_once_with(
  398                 task, sushy.VIRTUAL_MEDIA_CD)
  399 
  400             mock__insert_vmedia.assert_called_once_with(
  401                 task, 'image-url', sushy.VIRTUAL_MEDIA_CD)
  402 
  403             expected_params = {
  404                 'ipa-agent-token': mock.ANY,
  405             }
  406 
  407             mock_prepare_deploy_iso.assert_called_once_with(
  408                 task, expected_params, 'deploy', {})
  409 
  410             mock_node_set_boot_device.assert_called_once_with(
  411                 task, boot_devices.CDROM, False)
  412 
  413             mock_boot_mode_utils.sync_boot_mode.assert_called_once_with(task)
  414 
  415     @mock.patch.object(redfish_boot.manager_utils, 'node_set_boot_device',
  416                        autospec=True)
  417     @mock.patch.object(image_utils, 'prepare_floppy_image', autospec=True)
  418     @mock.patch.object(image_utils, 'prepare_deploy_iso', autospec=True)
  419     @mock.patch.object(redfish_boot, '_has_vmedia_device', autospec=True)
  420     @mock.patch.object(redfish_boot, '_eject_vmedia', autospec=True)
  421     @mock.patch.object(redfish_boot, '_insert_vmedia', autospec=True)
  422     @mock.patch.object(redfish_boot, '_parse_driver_info', autospec=True)
  423     @mock.patch.object(redfish_boot.manager_utils, 'node_power_action',
  424                        autospec=True)
  425     @mock.patch.object(redfish_boot, 'boot_mode_utils', autospec=True)
  426     def test_prepare_ramdisk_with_floppy(
  427             self, mock_boot_mode_utils, mock_node_power_action,
  428             mock__parse_driver_info, mock__insert_vmedia, mock__eject_vmedia,
  429             mock__has_vmedia_device, mock_prepare_deploy_iso,
  430             mock_prepare_floppy_image, mock_node_set_boot_device):
  431 
  432         with task_manager.acquire(self.context, self.node.uuid,
  433                                   shared=False) as task:
  434             task.node.provision_state = states.DEPLOYING
  435 
  436             d_info = {
  437                 'config_via_floppy': True
  438             }
  439 
  440             mock__parse_driver_info.return_value = d_info
  441 
  442             mock__has_vmedia_device.return_value = True
  443             mock_prepare_floppy_image.return_value = 'floppy-image-url'
  444             mock_prepare_deploy_iso.return_value = 'cd-image-url'
  445 
  446             task.driver.boot.prepare_ramdisk(task, {})
  447 
  448             mock_node_power_action.assert_called_once_with(
  449                 task, states.POWER_OFF)
  450 
  451             mock__has_vmedia_device.assert_called_once_with(
  452                 task, sushy.VIRTUAL_MEDIA_FLOPPY)
  453 
  454             eject_calls = [
  455                 mock.call(task, sushy.VIRTUAL_MEDIA_FLOPPY),
  456                 mock.call(task, sushy.VIRTUAL_MEDIA_CD)
  457             ]
  458 
  459             mock__eject_vmedia.assert_has_calls(eject_calls)
  460 
  461             insert_calls = [
  462                 mock.call(task, 'floppy-image-url',
  463                           sushy.VIRTUAL_MEDIA_FLOPPY),
  464                 mock.call(task, 'cd-image-url',
  465                           sushy.VIRTUAL_MEDIA_CD),
  466             ]
  467 
  468             mock__insert_vmedia.assert_has_calls(insert_calls)
  469 
  470             expected_params = {
  471                 'boot_method': 'vmedia',
  472                 'ipa-debug': '1',
  473                 'ipa-agent-token': mock.ANY,
  474             }
  475 
  476             mock_prepare_deploy_iso.assert_called_once_with(
  477                 task, expected_params, 'deploy', d_info)
  478 
  479             mock_node_set_boot_device.assert_called_once_with(
  480                 task, boot_devices.CDROM, False)
  481 
  482             mock_boot_mode_utils.sync_boot_mode.assert_called_once_with(task)
  483 
  484     @mock.patch.object(redfish_boot, '_has_vmedia_device', autospec=True)
  485     @mock.patch.object(redfish_boot, '_eject_vmedia', autospec=True)
  486     @mock.patch.object(image_utils, 'cleanup_iso_image', autospec=True)
  487     @mock.patch.object(image_utils, 'cleanup_floppy_image', autospec=True)
  488     @mock.patch.object(redfish_boot, '_parse_driver_info', autospec=True)
  489     def test_clean_up_ramdisk(
  490             self, mock__parse_driver_info, mock_cleanup_floppy_image,
  491             mock_cleanup_iso_image, mock__eject_vmedia,
  492             mock__has_vmedia_device):
  493 
  494         with task_manager.acquire(self.context, self.node.uuid,
  495                                   shared=True) as task:
  496             task.node.provision_state = states.DEPLOYING
  497 
  498             mock__parse_driver_info.return_value = {'config_via_floppy': True}
  499             mock__has_vmedia_device.return_value = True
  500 
  501             task.driver.boot.clean_up_ramdisk(task)
  502 
  503             mock_cleanup_iso_image.assert_called_once_with(task)
  504 
  505             mock_cleanup_floppy_image.assert_called_once_with(task)
  506 
  507             mock__has_vmedia_device.assert_called_once_with(
  508                 task, sushy.VIRTUAL_MEDIA_FLOPPY)
  509 
  510             eject_calls = [
  511                 mock.call(task, sushy.VIRTUAL_MEDIA_CD),
  512                 mock.call(task, sushy.VIRTUAL_MEDIA_FLOPPY)
  513             ]
  514 
  515             mock__eject_vmedia.assert_has_calls(eject_calls)
  516 
  517     @mock.patch.object(redfish_boot.RedfishVirtualMediaBoot,
  518                        'clean_up_instance', autospec=True)
  519     @mock.patch.object(image_utils, 'prepare_boot_iso', autospec=True)
  520     @mock.patch.object(redfish_boot, '_eject_vmedia', autospec=True)
  521     @mock.patch.object(redfish_boot, '_insert_vmedia', autospec=True)
  522     @mock.patch.object(redfish_boot, '_parse_deploy_info', autospec=True)
  523     @mock.patch.object(redfish_boot, 'manager_utils', autospec=True)
  524     @mock.patch.object(redfish_boot, 'deploy_utils', autospec=True)
  525     @mock.patch.object(redfish_boot, 'boot_mode_utils', autospec=True)
  526     def test_prepare_instance_normal_boot(
  527             self, mock_boot_mode_utils, mock_deploy_utils, mock_manager_utils,
  528             mock__parse_deploy_info, mock__insert_vmedia, mock__eject_vmedia,
  529             mock_prepare_boot_iso, mock_clean_up_instance):
  530 
  531         with task_manager.acquire(self.context, self.node.uuid,
  532                                   shared=True) as task:
  533             task.node.provision_state = states.DEPLOYING
  534             task.node.driver_internal_info[
  535                 'root_uuid_or_disk_id'] = self.node.uuid
  536 
  537             mock_deploy_utils.get_boot_option.return_value = 'net'
  538 
  539             d_info = {
  540                 'deploy_kernel': 'kernel',
  541                 'deploy_ramdisk': 'ramdisk',
  542                 'bootloader': 'bootloader'
  543             }
  544 
  545             mock__parse_deploy_info.return_value = d_info
  546 
  547             mock_prepare_boot_iso.return_value = 'image-url'
  548 
  549             task.driver.boot.prepare_instance(task)
  550 
  551             expected_params = {
  552                 'root_uuid': self.node.uuid
  553             }
  554 
  555             mock_prepare_boot_iso.assert_called_once_with(
  556                 task, d_info, **expected_params)
  557 
  558             mock__eject_vmedia.assert_called_once_with(
  559                 task, sushy.VIRTUAL_MEDIA_CD)
  560 
  561             mock__insert_vmedia.assert_called_once_with(
  562                 task, 'image-url', sushy.VIRTUAL_MEDIA_CD)
  563 
  564             mock_manager_utils.node_set_boot_device.assert_called_once_with(
  565                 task, boot_devices.CDROM, persistent=True)
  566 
  567             mock_boot_mode_utils.sync_boot_mode.assert_called_once_with(task)
  568 
  569     @mock.patch.object(redfish_boot.RedfishVirtualMediaBoot,
  570                        'clean_up_instance', autospec=True)
  571     @mock.patch.object(image_utils, 'prepare_boot_iso', autospec=True)
  572     @mock.patch.object(redfish_boot, '_eject_vmedia', autospec=True)
  573     @mock.patch.object(redfish_boot, '_insert_vmedia', autospec=True)
  574     @mock.patch.object(redfish_boot, '_parse_deploy_info', autospec=True)
  575     @mock.patch.object(redfish_boot, 'manager_utils', autospec=True)
  576     @mock.patch.object(redfish_boot, 'deploy_utils', autospec=True)
  577     @mock.patch.object(redfish_boot, 'boot_mode_utils', autospec=True)
  578     def test_prepare_instance_ramdisk_boot(
  579             self, mock_boot_mode_utils, mock_deploy_utils, mock_manager_utils,
  580             mock__parse_deploy_info, mock__insert_vmedia, mock__eject_vmedia,
  581             mock_prepare_boot_iso, mock_clean_up_instance):
  582 
  583         with task_manager.acquire(self.context, self.node.uuid,
  584                                   shared=True) as task:
  585             task.node.provision_state = states.DEPLOYING
  586             task.node.driver_internal_info[
  587                 'root_uuid_or_disk_id'] = self.node.uuid
  588 
  589             mock_deploy_utils.get_boot_option.return_value = 'ramdisk'
  590 
  591             d_info = {
  592                 'deploy_kernel': 'kernel',
  593                 'deploy_ramdisk': 'ramdisk',
  594                 'bootloader': 'bootloader'
  595             }
  596             mock__parse_deploy_info.return_value = d_info
  597 
  598             mock_prepare_boot_iso.return_value = 'image-url'
  599 
  600             task.driver.boot.prepare_instance(task)
  601 
  602             mock_clean_up_instance.assert_called_once_with(mock.ANY, task)
  603 
  604             mock_prepare_boot_iso.assert_called_once_with(task, d_info)
  605 
  606             mock__eject_vmedia.assert_called_once_with(
  607                 task, sushy.VIRTUAL_MEDIA_CD)
  608 
  609             mock__insert_vmedia.assert_called_once_with(
  610                 task, 'image-url', sushy.VIRTUAL_MEDIA_CD)
  611 
  612             mock_manager_utils.node_set_boot_device.assert_called_once_with(
  613                 task, boot_devices.CDROM, persistent=True)
  614 
  615             mock_boot_mode_utils.sync_boot_mode.assert_called_once_with(task)
  616 
  617     @mock.patch.object(redfish_boot.RedfishVirtualMediaBoot,
  618                        'clean_up_instance', autospec=True)
  619     @mock.patch.object(image_utils, 'prepare_boot_iso', autospec=True)
  620     @mock.patch.object(redfish_boot, '_eject_vmedia', autospec=True)
  621     @mock.patch.object(redfish_boot, '_insert_vmedia', autospec=True)
  622     @mock.patch.object(redfish_boot, '_parse_deploy_info', autospec=True)
  623     @mock.patch.object(redfish_boot, 'manager_utils', autospec=True)
  624     @mock.patch.object(redfish_boot, 'deploy_utils', autospec=True)
  625     @mock.patch.object(redfish_boot, 'boot_mode_utils', autospec=True)
  626     def test_prepare_instance_ramdisk_boot_iso(
  627             self, mock_boot_mode_utils, mock_deploy_utils, mock_manager_utils,
  628             mock__parse_deploy_info, mock__insert_vmedia, mock__eject_vmedia,
  629             mock_prepare_boot_iso, mock_clean_up_instance):
  630 
  631         with task_manager.acquire(self.context, self.node.uuid,
  632                                   shared=True) as task:
  633             task.node.provision_state = states.DEPLOYING
  634             task.node.driver_internal_info[
  635                 'root_uuid_or_disk_id'] = self.node.uuid
  636 
  637             mock_deploy_utils.get_boot_option.return_value = 'ramdisk'
  638 
  639             d_info = {
  640                 'deploy_kernel': 'kernel',
  641                 'deploy_ramdisk': 'ramdisk',
  642                 'bootloader': 'bootloader'
  643             }
  644 
  645             mock__parse_deploy_info.return_value = d_info
  646             mock_prepare_boot_iso.return_value = 'image-url'
  647 
  648             task.driver.boot.prepare_instance(task)
  649 
  650             mock_prepare_boot_iso.assert_called_once_with(task, d_info)
  651 
  652             mock__eject_vmedia.assert_called_once_with(
  653                 task, sushy.VIRTUAL_MEDIA_CD)
  654 
  655             mock__insert_vmedia.assert_called_once_with(
  656                 task, 'image-url', sushy.VIRTUAL_MEDIA_CD)
  657 
  658             mock_manager_utils.node_set_boot_device.assert_called_once_with(
  659                 task, boot_devices.CDROM, persistent=True)
  660 
  661             mock_boot_mode_utils.sync_boot_mode.assert_called_once_with(task)
  662 
  663     @mock.patch.object(redfish_boot.RedfishVirtualMediaBoot,
  664                        'clean_up_instance', autospec=True)
  665     @mock.patch.object(image_utils, 'prepare_boot_iso', autospec=True)
  666     @mock.patch.object(redfish_boot, '_eject_vmedia', autospec=True)
  667     @mock.patch.object(redfish_boot, '_insert_vmedia', autospec=True)
  668     @mock.patch.object(redfish_boot, '_parse_deploy_info', autospec=True)
  669     @mock.patch.object(redfish_boot, 'manager_utils', autospec=True)
  670     @mock.patch.object(redfish_boot, 'deploy_utils', autospec=True)
  671     @mock.patch.object(redfish_boot, 'boot_mode_utils', autospec=True)
  672     def test_prepare_instance_ramdisk_boot_iso_boot(
  673             self, mock_boot_mode_utils, mock_deploy_utils, mock_manager_utils,
  674             mock__parse_deploy_info, mock__insert_vmedia, mock__eject_vmedia,
  675             mock_prepare_boot_iso, mock_clean_up_instance):
  676 
  677         with task_manager.acquire(self.context, self.node.uuid,
  678                                   shared=True) as task:
  679             task.node.provision_state = states.DEPLOYING
  680             i_info = task.node.instance_info
  681             i_info['boot_iso'] = "super-magic"
  682             task.node.instance_info = i_info
  683             mock_deploy_utils.get_boot_option.return_value = 'ramdisk'
  684             mock__parse_deploy_info.return_value = {}
  685 
  686             mock_prepare_boot_iso.return_value = 'image-url'
  687 
  688             task.driver.boot.prepare_instance(task)
  689 
  690             mock_prepare_boot_iso.assert_called_once_with(task, {})
  691 
  692             mock__eject_vmedia.assert_called_once_with(
  693                 task, sushy.VIRTUAL_MEDIA_CD)
  694 
  695             mock__insert_vmedia.assert_called_once_with(
  696                 task, 'image-url', sushy.VIRTUAL_MEDIA_CD)
  697 
  698             mock_manager_utils.node_set_boot_device.assert_called_once_with(
  699                 task, boot_devices.CDROM, persistent=True)
  700 
  701             mock_boot_mode_utils.sync_boot_mode.assert_called_once_with(task)
  702 
  703     @mock.patch.object(boot_mode_utils, 'sync_boot_mode', autospec=True)
  704     @mock.patch.object(redfish_boot, '_eject_vmedia', autospec=True)
  705     @mock.patch.object(image_utils, 'cleanup_iso_image', autospec=True)
  706     @mock.patch.object(redfish_boot, 'manager_utils', autospec=True)
  707     def _test_prepare_instance_local_boot(
  708             self, mock_manager_utils,
  709             mock_cleanup_iso_image, mock__eject_vmedia, mock_sync_boot_mode):
  710 
  711         with task_manager.acquire(self.context, self.node.uuid,
  712                                   shared=True) as task:
  713             task.node.provision_state = states.DEPLOYING
  714             task.node.driver_internal_info[
  715                 'root_uuid_or_disk_id'] = self.node.uuid
  716 
  717             task.driver.boot.prepare_instance(task)
  718 
  719             mock_manager_utils.node_set_boot_device.assert_called_once_with(
  720                 task, boot_devices.DISK, persistent=True)
  721             mock_cleanup_iso_image.assert_called_once_with(task)
  722             mock__eject_vmedia.assert_called_once_with(
  723                 task, sushy.VIRTUAL_MEDIA_CD)
  724             mock_sync_boot_mode.assert_called_once_with(task)
  725 
  726     def test_prepare_instance_local_whole_disk_image(self):
  727         self.node.driver_internal_info = {'is_whole_disk_image': True}
  728         self.node.save()
  729         self._test_prepare_instance_local_boot()
  730 
  731     def test_prepare_instance_local_boot_option(self):
  732         instance_info = self.node.instance_info
  733         instance_info['capabilities'] = '{"boot_option": "local"}'
  734         self.node.instance_info = instance_info
  735         self.node.save()
  736         self._test_prepare_instance_local_boot()
  737 
  738     @mock.patch.object(redfish_boot, '_eject_vmedia', autospec=True)
  739     @mock.patch.object(image_utils, 'cleanup_iso_image', autospec=True)
  740     def _test_clean_up_instance(self, mock_cleanup_iso_image,
  741                                 mock__eject_vmedia):
  742 
  743         with task_manager.acquire(self.context, self.node.uuid,
  744                                   shared=True) as task:
  745 
  746             task.driver.boot.clean_up_instance(task)
  747 
  748             mock_cleanup_iso_image.assert_called_once_with(task)
  749             eject_calls = [mock.call(task, sushy.VIRTUAL_MEDIA_CD)]
  750             if task.node.driver_info.get('config_via_floppy'):
  751                 eject_calls.append(mock.call(task, sushy.VIRTUAL_MEDIA_FLOPPY))
  752 
  753             mock__eject_vmedia.assert_has_calls(eject_calls)
  754 
  755     def test_clean_up_instance_only_cdrom(self):
  756         self._test_clean_up_instance()
  757 
  758     def test_clean_up_instance_cdrom_and_floppy(self):
  759         driver_info = self.node.driver_info
  760         driver_info['config_via_floppy'] = True
  761         self.node.driver_info = driver_info
  762         self.node.save()
  763         self._test_clean_up_instance()
  764 
  765     @mock.patch.object(redfish_boot, 'redfish_utils', autospec=True)
  766     def test__insert_vmedia_anew(self, mock_redfish_utils):
  767 
  768         with task_manager.acquire(self.context, self.node.uuid,
  769                                   shared=True) as task:
  770             mock_vmedia_cd = mock.MagicMock(
  771                 inserted=False,
  772                 media_types=[sushy.VIRTUAL_MEDIA_CD])
  773             mock_vmedia_floppy = mock.MagicMock(
  774                 inserted=False,
  775                 media_types=[sushy.VIRTUAL_MEDIA_FLOPPY])
  776 
  777             mock_manager = mock.MagicMock()
  778 
  779             mock_manager.virtual_media.get_members.return_value = [
  780                 mock_vmedia_cd, mock_vmedia_floppy]
  781 
  782             mock_redfish_utils.get_system.return_value.managers = [
  783                 mock_manager]
  784 
  785             redfish_boot._insert_vmedia(
  786                 task, 'img-url', sushy.VIRTUAL_MEDIA_CD)
  787 
  788             mock_vmedia_cd.insert_media.assert_called_once_with(
  789                 'img-url', inserted=True, write_protected=True)
  790 
  791             self.assertFalse(mock_vmedia_floppy.insert_media.call_count)
  792 
  793     @mock.patch.object(redfish_boot, 'redfish_utils', autospec=True)
  794     def test__insert_vmedia_already_inserted(self, mock_redfish_utils):
  795 
  796         with task_manager.acquire(self.context, self.node.uuid,
  797                                   shared=True) as task:
  798             mock_vmedia_cd = mock.MagicMock(
  799                 inserted=True,
  800                 image='img-url',
  801                 media_types=[sushy.VIRTUAL_MEDIA_CD])
  802             mock_manager = mock.MagicMock()
  803 
  804             mock_manager.virtual_media.get_members.return_value = [
  805                 mock_vmedia_cd]
  806 
  807             mock_redfish_utils.get_system.return_value.managers = [
  808                 mock_manager]
  809 
  810             redfish_boot._insert_vmedia(
  811                 task, 'img-url', sushy.VIRTUAL_MEDIA_CD)
  812 
  813             self.assertFalse(mock_vmedia_cd.insert_media.call_count)
  814 
  815     @mock.patch.object(redfish_boot, 'redfish_utils', autospec=True)
  816     def test__insert_vmedia_bad_device(self, mock_redfish_utils):
  817 
  818         with task_manager.acquire(self.context, self.node.uuid,
  819                                   shared=True) as task:
  820             mock_vmedia_floppy = mock.MagicMock(
  821                 inserted=False,
  822                 media_types=[sushy.VIRTUAL_MEDIA_FLOPPY])
  823             mock_manager = mock.MagicMock()
  824 
  825             mock_manager.virtual_media.get_members.return_value = [
  826                 mock_vmedia_floppy]
  827 
  828             mock_redfish_utils.get_system.return_value.managers = [
  829                 mock_manager]
  830 
  831             self.assertRaises(
  832                 exception.InvalidParameterValue,
  833                 redfish_boot._insert_vmedia,
  834                 task, 'img-url', sushy.VIRTUAL_MEDIA_CD)
  835 
  836     @mock.patch.object(redfish_boot, 'redfish_utils', autospec=True)
  837     def test__eject_vmedia_everything(self, mock_redfish_utils):
  838 
  839         with task_manager.acquire(self.context, self.node.uuid,
  840                                   shared=True) as task:
  841             mock_vmedia_cd = mock.MagicMock(
  842                 inserted=True,
  843                 media_types=[sushy.VIRTUAL_MEDIA_CD])
  844             mock_vmedia_floppy = mock.MagicMock(
  845                 inserted=True,
  846                 media_types=[sushy.VIRTUAL_MEDIA_FLOPPY])
  847 
  848             mock_manager = mock.MagicMock()
  849 
  850             mock_manager.virtual_media.get_members.return_value = [
  851                 mock_vmedia_cd, mock_vmedia_floppy]
  852 
  853             mock_redfish_utils.get_system.return_value.managers = [
  854                 mock_manager]
  855 
  856             redfish_boot._eject_vmedia(task)
  857 
  858             mock_vmedia_cd.eject_media.assert_called_once_with()
  859             mock_vmedia_floppy.eject_media.assert_called_once_with()
  860 
  861     @mock.patch.object(redfish_boot, 'redfish_utils', autospec=True)
  862     def test__eject_vmedia_specific(self, mock_redfish_utils):
  863 
  864         with task_manager.acquire(self.context, self.node.uuid,
  865                                   shared=True) as task:
  866             mock_vmedia_cd = mock.MagicMock(
  867                 inserted=True,
  868                 media_types=[sushy.VIRTUAL_MEDIA_CD])
  869             mock_vmedia_floppy = mock.MagicMock(
  870                 inserted=True,
  871                 media_types=[sushy.VIRTUAL_MEDIA_FLOPPY])
  872 
  873             mock_manager = mock.MagicMock()
  874 
  875             mock_manager.virtual_media.get_members.return_value = [
  876                 mock_vmedia_cd, mock_vmedia_floppy]
  877 
  878             mock_redfish_utils.get_system.return_value.managers = [
  879                 mock_manager]
  880 
  881             redfish_boot._eject_vmedia(task, sushy.VIRTUAL_MEDIA_CD)
  882 
  883             mock_vmedia_cd.eject_media.assert_called_once_with()
  884             self.assertFalse(mock_vmedia_floppy.eject_media.call_count)
  885 
  886     @mock.patch.object(redfish_boot, 'redfish_utils', autospec=True)
  887     def test__eject_vmedia_not_inserted(self, mock_redfish_utils):
  888 
  889         with task_manager.acquire(self.context, self.node.uuid,
  890                                   shared=True) as task:
  891             mock_vmedia_cd = mock.MagicMock(
  892                 inserted=False,
  893                 media_types=[sushy.VIRTUAL_MEDIA_CD])
  894             mock_vmedia_floppy = mock.MagicMock(
  895                 inserted=False,
  896                 media_types=[sushy.VIRTUAL_MEDIA_FLOPPY])
  897 
  898             mock_manager = mock.MagicMock()
  899 
  900             mock_manager.virtual_media.get_members.return_value = [
  901                 mock_vmedia_cd, mock_vmedia_floppy]
  902 
  903             mock_redfish_utils.get_system.return_value.managers = [
  904                 mock_manager]
  905 
  906             redfish_boot._eject_vmedia(task)
  907 
  908             self.assertFalse(mock_vmedia_cd.eject_media.call_count)
  909             self.assertFalse(mock_vmedia_floppy.eject_media.call_count)
  910 
  911     @mock.patch.object(redfish_boot, 'redfish_utils', autospec=True)
  912     def test__eject_vmedia_unknown(self, mock_redfish_utils):
  913 
  914         with task_manager.acquire(self.context, self.node.uuid,
  915                                   shared=True) as task:
  916             mock_vmedia_cd = mock.MagicMock(
  917                 inserted=False,
  918                 media_types=[sushy.VIRTUAL_MEDIA_CD])
  919 
  920             mock_manager = mock.MagicMock()
  921 
  922             mock_manager.virtual_media.get_members.return_value = [
  923                 mock_vmedia_cd]
  924 
  925             mock_redfish_utils.get_system.return_value.managers = [
  926                 mock_manager]
  927 
  928             redfish_boot._eject_vmedia(task)
  929 
  930             self.assertFalse(mock_vmedia_cd.eject_media.call_count)