"Fossies" - the Fresh Open Source Software Archive

Member "ironic-16.0.3/ironic/tests/unit/drivers/modules/drac/test_periodic_task.py" (18 Jan 2021, 19209 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_periodic_task.py": 16.0.2_vs_16.0.3.

    1 #
    2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    3 # not use this file except in compliance with the License. You may obtain
    4 # a copy of the License at
    5 #
    6 #      http://www.apache.org/licenses/LICENSE-2.0
    7 #
    8 # Unless required by applicable law or agreed to in writing, software
    9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   11 # License for the specific language governing permissions and limitations
   12 # under the License.
   13 
   14 """
   15 Test class for DRAC periodic tasks
   16 """
   17 
   18 from unittest import mock
   19 
   20 from ironic.conductor import task_manager
   21 from ironic.conductor import utils as manager_utils
   22 from ironic.drivers.modules.drac import common as drac_common
   23 from ironic.drivers.modules.drac import raid as drac_raid
   24 from ironic.tests.unit.db import base as db_base
   25 from ironic.tests.unit.drivers.modules.drac import utils as test_utils
   26 from ironic.tests.unit.objects import utils as obj_utils
   27 
   28 INFO_DICT = test_utils.INFO_DICT
   29 
   30 
   31 class DracPeriodicTaskTestCase(db_base.DbTestCase):
   32 
   33     def setUp(self):
   34         super(DracPeriodicTaskTestCase, self).setUp()
   35         self.node = obj_utils.create_test_node(self.context,
   36                                                driver='idrac',
   37                                                driver_info=INFO_DICT)
   38         self.raid = drac_raid.DracRAID()
   39         self.raid_wsman = drac_raid.DracWSManRAID()
   40         self.job = {
   41             'id': 'JID_001436912645',
   42             'name': 'ConfigBIOS:BIOS.Setup.1-1',
   43             'start_time': '00000101000000',
   44             'until_time': 'TIME_NA',
   45             'message': 'Job in progress',
   46             'status': 'Running',
   47             'percent_complete': 34}
   48         self.virtual_disk = {
   49             'id': 'Disk.Virtual.0:RAID.Integrated.1-1',
   50             'name': 'disk 0',
   51             'description': 'Virtual Disk 0 on Integrated RAID Controller 1',
   52             'controller': 'RAID.Integrated.1-1',
   53             'raid_level': '1',
   54             'size_mb': 571776,
   55             'status': 'ok',
   56             'raid_status': 'online',
   57             'span_depth': 1,
   58             'span_length': 2,
   59             'pending_operations': None
   60         }
   61 
   62     def test__query_raid_config_job_status_drac(self):
   63         self._test__query_raid_config_job_status(self.raid)
   64 
   65     def test__query_raid_config_job_status_drac_wsman(self):
   66         self._test__query_raid_config_job_status(self.raid_wsman)
   67 
   68     @mock.patch.object(task_manager, 'acquire', autospec=True)
   69     def _test__query_raid_config_job_status(self, raid, mock_acquire):
   70         # mock node.driver_internal_info
   71         driver_internal_info = {'raid_config_job_ids': ['42']}
   72         self.node.driver_internal_info = driver_internal_info
   73         self.node.save()
   74         # mock manager
   75         mock_manager = mock.Mock()
   76         node_list = [(self.node.uuid, 'idrac', '',
   77                       {'raid_config_job_ids': ['42']})]
   78         mock_manager.iter_nodes.return_value = node_list
   79         # mock task_manager.acquire
   80         task = mock.Mock(node=self.node, driver=mock.Mock(raid=raid))
   81         mock_acquire.return_value = mock.MagicMock(
   82             __enter__=mock.MagicMock(return_value=task))
   83         # mock _check_node_raid_jobs
   84         raid._check_node_raid_jobs = mock.Mock()
   85 
   86         raid._query_raid_config_job_status(mock_manager,
   87                                            self.context)
   88 
   89         raid._check_node_raid_jobs.assert_called_once_with(task)
   90 
   91     def test__query_raid_config_job_status_no_config_jobs_drac(self):
   92         self._test__query_raid_config_job_status_no_config_jobs(self.raid)
   93 
   94     def test__query_raid_config_job_status_no_config_jobs_drac_wsman(self):
   95         self._test__query_raid_config_job_status_no_config_jobs(
   96             self.raid_wsman)
   97 
   98     @mock.patch.object(task_manager, 'acquire', autospec=True)
   99     def _test__query_raid_config_job_status_no_config_jobs(self, raid,
  100                                                            mock_acquire):
  101         # mock manager
  102         mock_manager = mock.Mock()
  103         node_list = [(self.node.uuid, 'idrac', '', {})]
  104         mock_manager.iter_nodes.return_value = node_list
  105         # mock task_manager.acquire
  106         task = mock.Mock(node=self.node, driver=mock.Mock(raid=raid))
  107         mock_acquire.return_value = mock.MagicMock(
  108             __enter__=mock.MagicMock(return_value=task))
  109         # mock _check_node_raid_jobs
  110         raid._check_node_raid_jobs = mock.Mock()
  111 
  112         raid._query_raid_config_job_status(mock_manager, None)
  113 
  114         self.assertEqual(0, raid._check_node_raid_jobs.call_count)
  115 
  116     def test__query_raid_config_job_status_no_nodes(self):
  117         # mock manager
  118         mock_manager = mock.Mock()
  119         node_list = []
  120         mock_manager.iter_nodes.return_value = node_list
  121         # mock _check_node_raid_jobs
  122         self.raid._check_node_raid_jobs = mock.Mock()
  123 
  124         self.raid._query_raid_config_job_status(mock_manager, None)
  125 
  126         self.assertEqual(0, self.raid._check_node_raid_jobs.call_count)
  127 
  128     @mock.patch.object(drac_common, 'get_drac_client', spec_set=True,
  129                        autospec=True)
  130     def test__check_node_raid_jobs_without_update(self, mock_get_drac_client):
  131         # mock node.driver_internal_info
  132         driver_internal_info = {'raid_config_job_ids': ['42']}
  133         self.node.driver_internal_info = driver_internal_info
  134         self.node.save()
  135         # mock task
  136         task = mock.Mock(node=self.node)
  137         # mock dracclient.get_job
  138         mock_client = mock.Mock()
  139         mock_get_drac_client.return_value = mock_client
  140         mock_client.get_job.return_value = test_utils.dict_to_namedtuple(
  141             values=self.job)
  142 
  143         self.raid._check_node_raid_jobs(task)
  144 
  145         mock_client.get_job.assert_called_once_with('42')
  146         self.assertEqual(0, mock_client.list_virtual_disks.call_count)
  147         self.node.refresh()
  148         self.assertEqual(['42'],
  149                          self.node.driver_internal_info['raid_config_job_ids'])
  150         self.assertEqual({}, self.node.raid_config)
  151         self.assertIs(False, self.node.maintenance)
  152 
  153     @mock.patch.object(drac_common, 'get_drac_client', spec_set=True,
  154                        autospec=True)
  155     @mock.patch.object(drac_raid.DracRAID, 'get_logical_disks',
  156                        spec_set=True, autospec=True)
  157     def _test__check_node_raid_jobs_with_completed_job(
  158             self, mock_notify_conductor_resume,
  159             mock_get_logical_disks, mock_get_drac_client):
  160         expected_logical_disk = {'size_gb': 558,
  161                                  'raid_level': '1',
  162                                  'name': 'disk 0'}
  163         # mock node.driver_internal_info
  164         driver_internal_info = {'raid_config_job_ids': ['42']}
  165         self.node.driver_internal_info = driver_internal_info
  166         self.node.save()
  167         # mock task
  168         task = mock.Mock(node=self.node, context=self.context)
  169         # mock dracclient.get_job
  170         self.job['status'] = 'Completed'
  171         mock_client = mock.Mock()
  172         mock_get_drac_client.return_value = mock_client
  173         mock_client.get_job.return_value = test_utils.dict_to_namedtuple(
  174             values=self.job)
  175         # mock driver.raid.get_logical_disks
  176         mock_get_logical_disks.return_value = {
  177             'logical_disks': [expected_logical_disk]
  178         }
  179 
  180         self.raid._check_node_raid_jobs(task)
  181 
  182         mock_client.get_job.assert_called_once_with('42')
  183         self.node.refresh()
  184         self.assertEqual([],
  185                          self.node.driver_internal_info['raid_config_job_ids'])
  186         self.assertEqual([expected_logical_disk],
  187                          self.node.raid_config['logical_disks'])
  188         mock_notify_conductor_resume.assert_called_once_with(task)
  189 
  190     @mock.patch.object(manager_utils, 'notify_conductor_resume_clean',
  191                        autospec=True)
  192     def test__check_node_raid_jobs_with_completed_job_in_clean(
  193             self, mock_notify_conductor_resume):
  194         self.node.clean_step = {'foo': 'bar'}
  195         self.node.save()
  196         self._test__check_node_raid_jobs_with_completed_job(
  197             mock_notify_conductor_resume)
  198 
  199     @mock.patch.object(manager_utils, 'notify_conductor_resume_deploy',
  200                        autospec=True)
  201     def test__check_node_raid_jobs_with_completed_job_in_deploy(
  202             self, mock_notify_conductor_resume):
  203         self._test__check_node_raid_jobs_with_completed_job(
  204             mock_notify_conductor_resume)
  205 
  206     @mock.patch.object(manager_utils, 'cleaning_error_handler', autospec=True)
  207     @mock.patch.object(drac_common, 'get_drac_client', spec_set=True,
  208                        autospec=True)
  209     def test__check_node_raid_jobs_with_failed_job(
  210             self, mock_get_drac_client, mock_cleaning_error_handler):
  211         # mock node.driver_internal_info and node.clean_step
  212         driver_internal_info = {'raid_config_job_ids': ['42']}
  213         self.node.driver_internal_info = driver_internal_info
  214         self.node.clean_step = {'foo': 'bar'}
  215         self.node.save()
  216         # mock task
  217         task = mock.Mock(node=self.node, context=self.context)
  218         # mock dracclient.get_job
  219         self.job['status'] = 'Failed'
  220         self.job['message'] = 'boom'
  221         mock_client = mock.Mock()
  222         mock_get_drac_client.return_value = mock_client
  223         mock_client.get_job.return_value = test_utils.dict_to_namedtuple(
  224             values=self.job)
  225         # mock dracclient.list_virtual_disks
  226         mock_client.list_virtual_disks.return_value = [
  227             test_utils.dict_to_namedtuple(values=self.virtual_disk)]
  228 
  229         self.raid._check_node_raid_jobs(task)
  230 
  231         mock_client.get_job.assert_called_once_with('42')
  232         self.assertEqual(0, mock_client.list_virtual_disks.call_count)
  233         self.node.refresh()
  234         self.assertEqual([],
  235                          self.node.driver_internal_info['raid_config_job_ids'])
  236         self.assertEqual({}, self.node.raid_config)
  237         mock_cleaning_error_handler.assert_called_once_with(task, mock.ANY)
  238 
  239     @mock.patch.object(manager_utils, 'deploying_error_handler', autospec=True)
  240     @mock.patch.object(manager_utils, 'cleaning_error_handler', autospec=True)
  241     @mock.patch.object(drac_common, 'get_drac_client', spec_set=True,
  242                        autospec=True)
  243     @mock.patch.object(drac_raid.DracRAID, 'get_logical_disks',
  244                        spec_set=True, autospec=True)
  245     def _test__check_node_raid_jobs_with_completed_job_already_failed(
  246             self, mock_notify_conductor_resume,
  247             mock_get_logical_disks, mock_get_drac_client,
  248             mock_cleaning_error_handler, mock_deploying_error_handler):
  249         expected_logical_disk = {'size_gb': 558,
  250                                  'raid_level': '1',
  251                                  'name': 'disk 0'}
  252         # mock node.driver_internal_info
  253         driver_internal_info = {'raid_config_job_ids': ['42'],
  254                                 'raid_config_job_failure': True}
  255         self.node.driver_internal_info = driver_internal_info
  256         self.node.save()
  257         # mock task
  258         task = mock.Mock(node=self.node, context=self.context)
  259         # mock dracclient.get_job
  260         self.job['status'] = 'Completed'
  261         mock_client = mock.Mock()
  262         mock_get_drac_client.return_value = mock_client
  263         mock_client.get_job.return_value = test_utils.dict_to_namedtuple(
  264             values=self.job)
  265         # mock driver.raid.get_logical_disks
  266         mock_get_logical_disks.return_value = {
  267             'logical_disks': [expected_logical_disk]
  268         }
  269 
  270         self.raid._check_node_raid_jobs(task)
  271 
  272         mock_client.get_job.assert_called_once_with('42')
  273         self.node.refresh()
  274         self.assertEqual([],
  275                          self.node.driver_internal_info['raid_config_job_ids'])
  276         self.assertNotIn('raid_config_job_failure',
  277                          self.node.driver_internal_info)
  278         self.assertNotIn('logical_disks', self.node.raid_config)
  279         if self.node.clean_step:
  280             mock_cleaning_error_handler.assert_called_once_with(task, mock.ANY)
  281         else:
  282             mock_deploying_error_handler.assert_called_once_with(task,
  283                                                                  mock.ANY,
  284                                                                  mock.ANY)
  285         self.assertFalse(mock_notify_conductor_resume.called)
  286 
  287     @mock.patch.object(manager_utils, 'notify_conductor_resume_clean',
  288                        autospec=True)
  289     def test__check_node_raid_jobs_with_completed_job_already_failed_in_clean(
  290             self, mock_notify_conductor_resume):
  291         self.node.clean_step = {'foo': 'bar'}
  292         self.node.save()
  293         self._test__check_node_raid_jobs_with_completed_job_already_failed(
  294             mock_notify_conductor_resume)
  295 
  296     @mock.patch.object(manager_utils, 'notify_conductor_resume_deploy',
  297                        autospec=True)
  298     def test__check_node_raid_jobs_with_completed_job_already_failed_in_deploy(
  299             self, mock_notify_conductor_resume):
  300         self._test__check_node_raid_jobs_with_completed_job_already_failed(
  301             mock_notify_conductor_resume)
  302 
  303     @mock.patch.object(drac_common, 'get_drac_client', spec_set=True,
  304                        autospec=True)
  305     @mock.patch.object(drac_raid.DracRAID, 'get_logical_disks',
  306                        spec_set=True, autospec=True)
  307     def _test__check_node_raid_jobs_with_multiple_jobs_completed(
  308             self, mock_notify_conductor_resume,
  309             mock_get_logical_disks, mock_get_drac_client):
  310         expected_logical_disk = {'size_gb': 558,
  311                                  'raid_level': '1',
  312                                  'name': 'disk 0'}
  313         # mock node.driver_internal_info
  314         driver_internal_info = {'raid_config_job_ids': ['42', '36']}
  315         self.node.driver_internal_info = driver_internal_info
  316         self.node.save()
  317         # mock task
  318         task = mock.Mock(node=self.node, context=self.context)
  319         # mock dracclient.get_job
  320         self.job['status'] = 'Completed'
  321         mock_client = mock.Mock()
  322         mock_get_drac_client.return_value = mock_client
  323         mock_client.get_job.return_value = test_utils.dict_to_namedtuple(
  324             values=self.job)
  325         # mock driver.raid.get_logical_disks
  326         mock_get_logical_disks.return_value = {
  327             'logical_disks': [expected_logical_disk]
  328         }
  329 
  330         self.raid._check_node_raid_jobs(task)
  331 
  332         mock_client.get_job.assert_has_calls([mock.call('42'),
  333                                               mock.call('36')])
  334         self.node.refresh()
  335         self.assertEqual([],
  336                          self.node.driver_internal_info['raid_config_job_ids'])
  337         self.assertNotIn('raid_config_job_failure',
  338                          self.node.driver_internal_info)
  339         self.assertEqual([expected_logical_disk],
  340                          self.node.raid_config['logical_disks'])
  341         mock_notify_conductor_resume.assert_called_once_with(task)
  342 
  343     @mock.patch.object(manager_utils, 'notify_conductor_resume_clean',
  344                        autospec=True)
  345     def test__check_node_raid_jobs_with_multiple_jobs_completed_in_clean(
  346             self, mock_notify_conductor_resume):
  347         self.node.clean_step = {'foo': 'bar'}
  348         self.node.save()
  349         self._test__check_node_raid_jobs_with_multiple_jobs_completed(
  350             mock_notify_conductor_resume)
  351 
  352     @mock.patch.object(manager_utils, 'notify_conductor_resume_deploy',
  353                        autospec=True)
  354     def test__check_node_raid_jobs_with_multiple_jobs_completed_in_deploy(
  355             self, mock_notify_conductor_resume):
  356         self._test__check_node_raid_jobs_with_multiple_jobs_completed(
  357             mock_notify_conductor_resume)
  358 
  359     @mock.patch.object(manager_utils, 'deploying_error_handler', autospec=True)
  360     @mock.patch.object(manager_utils, 'cleaning_error_handler', autospec=True)
  361     @mock.patch.object(drac_common, 'get_drac_client', spec_set=True,
  362                        autospec=True)
  363     @mock.patch.object(drac_raid.DracRAID, 'get_logical_disks',
  364                        spec_set=True, autospec=True)
  365     def _test__check_node_raid_jobs_with_multiple_jobs_failed(
  366             self, mock_notify_conductor_resume,
  367             mock_get_logical_disks, mock_get_drac_client,
  368             mock_cleaning_error_handler, mock_deploying_error_handler):
  369         expected_logical_disk = {'size_gb': 558,
  370                                  'raid_level': '1',
  371                                  'name': 'disk 0'}
  372         # mock node.driver_internal_info
  373         driver_internal_info = {'raid_config_job_ids': ['42', '36']}
  374         self.node.driver_internal_info = driver_internal_info
  375         self.node.save()
  376         # mock task
  377         task = mock.Mock(node=self.node, context=self.context)
  378         # mock dracclient.get_job
  379         self.job['status'] = 'Completed'
  380         failed_job = self.job.copy()
  381         failed_job['status'] = 'Failed'
  382         failed_job['message'] = 'boom'
  383         mock_client = mock.Mock()
  384         mock_get_drac_client.return_value = mock_client
  385         mock_client.get_job.side_effect = [
  386             test_utils.dict_to_namedtuple(values=failed_job),
  387             test_utils.dict_to_namedtuple(values=self.job)]
  388         # mock driver.raid.get_logical_disks
  389         mock_get_logical_disks.return_value = {
  390             'logical_disks': [expected_logical_disk]
  391         }
  392 
  393         self.raid._check_node_raid_jobs(task)
  394 
  395         mock_client.get_job.assert_has_calls([mock.call('42'),
  396                                               mock.call('36')])
  397         self.node.refresh()
  398         self.assertEqual([],
  399                          self.node.driver_internal_info['raid_config_job_ids'])
  400         self.assertNotIn('raid_config_job_failure',
  401                          self.node.driver_internal_info)
  402         self.assertNotIn('logical_disks', self.node.raid_config)
  403         if self.node.clean_step:
  404             mock_cleaning_error_handler.assert_called_once_with(task, mock.ANY)
  405         else:
  406             mock_deploying_error_handler.assert_called_once_with(task,
  407                                                                  mock.ANY,
  408                                                                  mock.ANY)
  409         self.assertFalse(mock_notify_conductor_resume.called)
  410 
  411     @mock.patch.object(manager_utils, 'notify_conductor_resume_clean',
  412                        autospec=True)
  413     def test__check_node_raid_jobs_with_multiple_jobs_failed_in_clean(
  414             self, mock_notify_conductor_resume):
  415         self.node.clean_step = {'foo': 'bar'}
  416         self.node.save()
  417         self._test__check_node_raid_jobs_with_multiple_jobs_failed(
  418             mock_notify_conductor_resume)
  419 
  420     @mock.patch.object(manager_utils, 'notify_conductor_resume_deploy',
  421                        autospec=True)
  422     def test__check_node_raid_jobs_with_multiple_jobs_failed_in_deploy(
  423             self, mock_notify_conductor_resume):
  424         self._test__check_node_raid_jobs_with_multiple_jobs_failed(
  425             mock_notify_conductor_resume)