def test_get_nova_adapter(self, mock_adapter, mock_nova_session): nova._NOVA_ADAPTER = None mock_session_obj = mock.Mock() expected = {'session': mock_session_obj, 'auth': None, 'version': "2.1"} mock_nova_session.return_value = mock_session_obj nova._get_nova_adapter() mock_nova_session.assert_called_once_with('nova') mock_adapter.assert_called_once_with(group='nova', **expected) """Check if existing adapter is used.""" mock_nova_session.reset_mock() nova._get_nova_adapter() mock_nova_session.assert_not_called()
def test_power_update_failed(self, mock_log): nova_adapter = nova._get_nova_adapter() event = [{'name': 'power-update', 'server_uuid': 'server-id-1', 'tag': 'POWER_OFF'}] nova_result = requests.Response() with mock.patch.object(nova_adapter, 'post', autospec=True) as mock_post_event: for stat_code in (500, 404, 400): mock_log.reset_mock() nova_result.status_code = stat_code type(nova_result).text = mock.PropertyMock(return_value="blah") mock_post_event.return_value = nova_result result = self.api.power_update( self.ctx, 'server-id-1', 'power off') self.assertFalse(result) expected = ("Failed to notify nova on event: %s. %s.", event[0], "blah") mock_log.warning.assert_called_once_with(*expected) mock_post_event.assert_has_calls([ mock.call('/os-server-external-events', json={'events': event}, microversion='2.76', global_request_id=self.ctx.global_id, raise_exc=False) ])
def test_power_update_failed(self, mock_log): nova_adapter = nova._get_nova_adapter() event = [{'name': 'power-update', 'server_uuid': 'server-id-1', 'tag': 'POWER_OFF'}] nova_result = requests.Response() with mock.patch.object(nova_adapter, 'post') as mock_post_event: for stat_code in (500, 404, 207): mock_log.reset_mock() nova_result.status_code = stat_code type(nova_result).text = mock.PropertyMock(return_value="blah") if stat_code == 207: def json_func(): return {'events': [{}]} nova_result.json = json_func mock_post_event.return_value = nova_result result = self.api.power_update( self.ctx, 'server-id-1', 'power off') self.assertFalse(result) if stat_code == 207: expected = ('Invalid response %s returned from nova for ' 'power-update event %s. %s.') self.assertIn(expected, mock_log.error.call_args[0][0]) else: expected = ("Failed to notify nova on event: %s. %s.", event[0], "blah") mock_log.warning.assert_called_once_with(*expected) mock_post_event.assert_has_calls([ mock.call('/os-server-external-events', json={'events': event}, microversion='2.76', global_request_id=self.ctx.global_id, raise_exc=False) ])