"Fossies" - the Fresh Open Source Software Archive

Member "masakari-9.0.0/masakari/tests/unit/engine/drivers/taskflow/test_instance_failure_flow.py" (13 May 2020, 12277 Bytes) of package /linux/misc/openstack/masakari-9.0.0.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_instance_failure_flow.py": 8.0.0_vs_9.0.0.

    1 # Copyright 2016 NTT DATA
    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 """
   17 Unit Tests for instance failure TaskFlow
   18 """
   19 
   20 from unittest import mock
   21 
   22 from masakari.compute import nova
   23 from masakari import context
   24 from masakari.engine.drivers.taskflow import instance_failure
   25 from masakari import exception
   26 from masakari import test
   27 from masakari.tests.unit import fakes
   28 
   29 
   30 class InstanceFailureTestCase(test.TestCase):
   31 
   32     def setUp(self):
   33         super(InstanceFailureTestCase, self).setUp()
   34         self.ctxt = context.get_admin_context()
   35         self.novaclient = nova.API()
   36         self.fake_client = fakes.FakeNovaClient()
   37         self.instance_id = "1"
   38         # overriding 'wait_period_after_power_off' and
   39         # 'wait_period_after_power_on' to 2 seconds to
   40         # reduce the wait period.
   41         self.override_config('wait_period_after_power_off', 2)
   42         self.override_config('wait_period_after_power_on', 2)
   43         self.override_config("process_all_instances",
   44                              False, "instance_failure")
   45 
   46     def _test_stop_instance(self):
   47         task = instance_failure.StopInstanceTask(self.ctxt, self.novaclient)
   48         task.execute(self.instance_id)
   49         # verify instance is stopped
   50         instance = self.novaclient.get_server(self.ctxt, self.instance_id)
   51         self.assertEqual('stopped',
   52                          getattr(instance, 'OS-EXT-STS:vm_state'))
   53 
   54     def _test_confirm_instance_is_active(self):
   55         task = instance_failure.ConfirmInstanceActiveTask(self.ctxt,
   56                                                           self.novaclient)
   57         task.execute(self.instance_id)
   58         # verify instance is in active state
   59         instance = self.novaclient.get_server(self.ctxt, self.instance_id)
   60         self.assertEqual('active',
   61                          getattr(instance, 'OS-EXT-STS:vm_state'))
   62 
   63     @mock.patch('masakari.compute.nova.novaclient')
   64     @mock.patch('masakari.engine.drivers.taskflow.base.MasakariTask.'
   65                 'update_details')
   66     def test_instance_failure_flow(self, _mock_notify, _mock_novaclient):
   67         _mock_novaclient.return_value = self.fake_client
   68 
   69         # create test data
   70         self.fake_client.servers.create(self.instance_id,
   71                                         host="fake-host",
   72                                         ha_enabled=True)
   73 
   74         # test StopInstanceTask
   75         self._test_stop_instance()
   76 
   77         # test StartInstanceTask
   78         task = instance_failure.StartInstanceTask(self.ctxt, self.novaclient)
   79         task.execute(self.instance_id)
   80 
   81         # test ConfirmInstanceActiveTask
   82         self._test_confirm_instance_is_active()
   83 
   84         # verify progress details
   85         _mock_notify.assert_has_calls([
   86             mock.call('Stopping instance: ' + self.instance_id),
   87             mock.call("Stopped instance: '" + self.instance_id + "'", 1.0),
   88             mock.call("Starting instance: '" + self.instance_id + "'"),
   89             mock.call("Instance started: '" + self.instance_id + "'", 1.0),
   90             mock.call("Confirming instance '" + self.instance_id +
   91                       "' vm_state is ACTIVE"),
   92             mock.call("Confirmed instance '" + self.instance_id +
   93                       "' vm_state is ACTIVE", 1.0)
   94         ])
   95 
   96     @mock.patch('masakari.compute.nova.novaclient')
   97     @mock.patch('masakari.engine.drivers.taskflow.base.MasakariTask.'
   98                 'update_details')
   99     def test_instance_failure_flow_resized_instance(self, _mock_notify,
  100                                                     _mock_novaclient):
  101         _mock_novaclient.return_value = self.fake_client
  102 
  103         # create test data
  104         self.fake_client.servers.create(self.instance_id,
  105                                         host="fake-host",
  106                                         ha_enabled=True, vm_state="resized")
  107 
  108         # test StopInstanceTask
  109         self._test_stop_instance()
  110 
  111         # test StartInstanceTask
  112         task = instance_failure.StartInstanceTask(self.ctxt, self.novaclient)
  113         task.execute(self.instance_id)
  114 
  115         # test ConfirmInstanceActiveTask
  116         self._test_confirm_instance_is_active()
  117 
  118         # verify progress details
  119         _mock_notify.assert_has_calls([
  120             mock.call('Stopping instance: ' + self.instance_id),
  121             mock.call("Stopped instance: '" + self.instance_id + "'", 1.0),
  122             mock.call("Starting instance: '" + self.instance_id + "'"),
  123             mock.call("Instance started: '" + self.instance_id + "'", 1.0),
  124             mock.call("Confirming instance '" + self.instance_id +
  125                       "' vm_state is ACTIVE"),
  126             mock.call("Confirmed instance '" + self.instance_id +
  127                       "' vm_state is ACTIVE", 1.0)
  128         ])
  129 
  130     @mock.patch('masakari.compute.nova.novaclient')
  131     @mock.patch('masakari.engine.drivers.taskflow.base.MasakariTask.'
  132                 'update_details')
  133     def test_instance_failure_flow_stop_failed(self, _mock_notify,
  134                                                _mock_novaclient):
  135         _mock_novaclient.return_value = self.fake_client
  136 
  137         # create test data
  138         server = self.fake_client.servers.create(self.instance_id,
  139                                                  host="fake-host",
  140                                                  ha_enabled=True)
  141 
  142         def fake_stop_server(context, uuid):
  143             # assume that while stopping instance goes into error state
  144             setattr(server, 'OS-EXT-STS:vm_state', "error")
  145             return server
  146 
  147         # test StopInstanceTask
  148         task = instance_failure.StopInstanceTask(self.ctxt, self.novaclient)
  149         with mock.patch.object(self.novaclient, 'stop_server',
  150                                fake_stop_server):
  151             self.assertRaises(
  152                 exception.InstanceRecoveryFailureException, task.execute,
  153                 self.instance_id)
  154 
  155         # verify progress details
  156         _mock_notify.assert_has_calls([
  157             mock.call('Stopping instance: ' + self.instance_id),
  158             mock.call('Failed to stop instance ' + self.instance_id, 1.0)
  159         ])
  160 
  161     @mock.patch('masakari.compute.nova.novaclient')
  162     @mock.patch('masakari.engine.drivers.taskflow.base.MasakariTask.'
  163                 'update_details')
  164     def test_instance_failure_flow_not_ha_enabled(self, _mock_notify,
  165                                                   _mock_novaclient):
  166         _mock_novaclient.return_value = self.fake_client
  167 
  168         # create test data
  169         self.fake_client.servers.create(self.instance_id, host="fake-host")
  170 
  171         # test StopInstanceTask
  172         task = instance_failure.StopInstanceTask(self.ctxt, self.novaclient)
  173         self.assertRaises(
  174             exception.SkipInstanceRecoveryException, task.execute,
  175             self.instance_id)
  176 
  177         # verify progress details
  178         _mock_notify.assert_has_calls([
  179             mock.call('Skipping recovery for instance: ' + self.instance_id +
  180                       ' as it is not Ha_Enabled', 1.0)
  181         ])
  182 
  183     @mock.patch('masakari.compute.nova.novaclient')
  184     @mock.patch('masakari.engine.drivers.taskflow.base.MasakariTask.'
  185                 'update_details')
  186     def test_instance_failure_flow_vm_in_paused_state(self, _mock_notify,
  187                                                   _mock_novaclient):
  188         _mock_novaclient.return_value = self.fake_client
  189 
  190         # create test data
  191         self.fake_client.servers.create(self.instance_id,
  192                                         host="fake-host", ha_enabled=True,
  193                                         vm_state="paused")
  194 
  195         # test StopInstanceTask
  196         task = instance_failure.StopInstanceTask(self.ctxt, self.novaclient)
  197         self.assertRaises(
  198             exception.IgnoreInstanceRecoveryException, task.execute,
  199             self.instance_id)
  200 
  201         # verify progress details
  202         _mock_notify.assert_has_calls([
  203             mock.call("Recovery of instance '" + self.instance_id +
  204                       "' is ignored as it is in 'paused' state.", 1.0)
  205         ])
  206 
  207     @mock.patch('masakari.compute.nova.novaclient')
  208     @mock.patch('masakari.engine.drivers.taskflow.base.MasakariTask.'
  209                 'update_details')
  210     def test_instance_failure_flow_not_ha_enabled_but_conf_option_is_set(
  211             self, _mock_notify, _mock_novaclient):
  212         # Setting this config option to True indicates masakari has to recover
  213         # the instance irrespective of whether it is HA_Enabled or not.
  214         self.override_config("process_all_instances",
  215                              True, "instance_failure")
  216         _mock_novaclient.return_value = self.fake_client
  217 
  218         # create test data
  219         self.fake_client.servers.create(self.instance_id,
  220                                         host="fake-host", vm_state="resized")
  221 
  222         # test StopInstanceTask
  223         self._test_stop_instance()
  224 
  225         # test StartInstanceTask
  226         task = instance_failure.StartInstanceTask(self.ctxt, self.novaclient)
  227         task.execute(self.instance_id)
  228 
  229         # test ConfirmInstanceActiveTask
  230         self._test_confirm_instance_is_active()
  231 
  232         # verify progress details
  233         _mock_notify.assert_has_calls([
  234             mock.call('Stopping instance: ' + self.instance_id),
  235             mock.call("Stopped instance: '" + self.instance_id + "'", 1.0),
  236             mock.call("Starting instance: '" + self.instance_id + "'"),
  237             mock.call("Instance started: '" + self.instance_id + "'", 1.0),
  238             mock.call("Confirming instance '" + self.instance_id +
  239                       "' vm_state is ACTIVE"),
  240             mock.call("Confirmed instance '" + self.instance_id +
  241                       "' vm_state is ACTIVE", 1.0)
  242         ])
  243 
  244     @mock.patch('masakari.compute.nova.novaclient')
  245     @mock.patch('masakari.engine.drivers.taskflow.base.MasakariTask.'
  246                 'update_details')
  247     def test_instance_failure_flow_start_failed(self, _mock_notify,
  248                                                 _mock_novaclient):
  249         _mock_novaclient.return_value = self.fake_client
  250 
  251         # create test data
  252         server = self.fake_client.servers.create(self.instance_id,
  253                                                  host="fake-host",
  254                                                  ha_enabled=True)
  255 
  256         # test StopInstanceTask
  257         self._test_stop_instance()
  258 
  259         def fake_start_server(context, uuid):
  260             # assume that while starting instance goes into error state
  261             setattr(server, 'OS-EXT-STS:vm_state', "error")
  262             return server
  263 
  264         # test StartInstanceTask
  265         task = instance_failure.StartInstanceTask(self.ctxt, self.novaclient)
  266         with mock.patch.object(self.novaclient, 'start_server',
  267                                fake_start_server):
  268             task.execute(self.instance_id)
  269 
  270         # test ConfirmInstanceActiveTask
  271         task = instance_failure.ConfirmInstanceActiveTask(self.ctxt,
  272                                                           self.novaclient)
  273         self.assertRaises(
  274             exception.InstanceRecoveryFailureException, task.execute,
  275             self.instance_id)
  276 
  277         # verify progress details
  278         _mock_notify.assert_has_calls([
  279             mock.call('Stopping instance: ' + self.instance_id),
  280             mock.call("Stopped instance: '" + self.instance_id + "'", 1.0),
  281             mock.call("Starting instance: '" + self.instance_id + "'"),
  282             mock.call("Instance started: '" + self.instance_id + "'", 1.0),
  283             mock.call("Confirming instance '" + self.instance_id +
  284                       "' vm_state is ACTIVE"),
  285             mock.call('Failed to start instance 1', 1.0)
  286         ])