def test_handle_request_preemptible_request(self, reaper_action): mock_action = mock.Mock() reaper_action.return_value = mock_action reaper_request = fakes.make_reaper_request() with mock.patch.object(self.reaper, 'handle_reaper_request') as m: m.side_effect = exception.PreemptibleRequest() self.reaper.handle_request(reaper_request) self.assertEqual(ra.ActionState.CANCELED, mock_action.state)
def test_handle_request_unexpected_error(self, reaper_action): mock_action = mock.Mock() reaper_action.return_value = mock_action reaper_request = fakes.make_reaper_request() with mock.patch.object(self.reaper, 'handle_reaper_request') as m: reason = "unexpected error" m.side_effect = Exception(reason) self.reaper.handle_request(reaper_request) self.assertEqual(ra.ActionState.FAILED, mock_action.state)
def test_handle_state_calculation_request(self, system_mock): state_mock = mock.Mock(usage=mock.Mock(return_value=90)) mocked_system = mock.Mock(system_state=mock.Mock( return_value=state_mock)) system_mock.return_value = mocked_system CONF.aardvark.watermark = 80 request = fakes.make_reaper_request() with mock.patch.object(self.reaper, 'free_resources') as mocked: self.reaper.handle_state_calculation_request(request) self.assertTrue(mocked.called)
def test_handle_request(self, reaper_action): reaper_request = fakes.make_reaper_request() calculation_request = fakes.make_calculation_request() with mock.patch.object(self.reaper, 'handle_reaper_request') as m: self.reaper.handle_request(reaper_request) m.assert_called_once_with(reaper_request) with mock.patch.object(self.reaper, 'handle_state_calculation_request') as m: self.reaper.handle_request(calculation_request) m.assert_called_once_with(calculation_request)
def test_handle_reaper_request(self, mock_reset, mock_rebuild): # create a request with no aggregates uuids = ['instance1', 'instance2'] image = 'fake_image' request = fakes.make_reaper_request(uuids=uuids, image=image) with mock.patch.object(self.reaper, '_do_handle_reaper_request'): self.reaper.handle_reaper_request(request) mock_rebuild.assert_has_calls( [mock.call('instance1', image), mock.call('instance2', image)], any_order=True) self.assertTrue(not mock_reset.called)
def test_do_handle_reaper_request(self, system_mock): project_id = 'preemptible1' project = [mock.Mock(_id=project_id)] self.reaper.aggregates = ['aggregate_1'] mocked_system = mock.Mock(preemptible_projects=project) system_mock.return_value = mocked_system request = fakes.make_reaper_request(project="project1") self.assertNotEqual(self.reaper.aggregates, request.aggregates) with mock.patch.object(self.reaper, 'free_resources') as mocked: self.reaper._do_handle_reaper_request(request) # Make sure that the request has the aggregates of the reaper # instance. self.assertEqual(self.reaper.aggregates, request.aggregates) mocked.assert_called_once()
def test_handle_reaper_request_error(self, mock_reset, mock_rebuild): uuids = ['instance1', 'instance2'] request = fakes.make_reaper_request(uuids=uuids) self.reaper.aggregates = ['aggregate_1'] self.assertNotEqual(self.reaper.aggregates, request.aggregates) with mock.patch.object(self.reaper, '_do_handle_reaper_request') as m: m.side_effect = exception.PreemptibleRequest() self.assertRaises(exception.PreemptibleRequest, self.reaper.handle_reaper_request, request) mock_reset.assert_has_calls( [mock.call('instance1'), mock.call('instance2')], any_order=True) self.assertTrue(not mock_rebuild.called)
def test_free_resources_not_found_server(self, mock_delete): mock_projects = [mock.Mock(id_=1), mock.Mock(id_=2)] system = mock.Mock(preemptible_projects=mock_projects) request = fakes.make_reaper_request() mock_delete.side_effect = n_exc.NotFound("") hosts = ['host1'] servers = [mock.Mock(uuid='server1'), mock.Mock(uuid='server2')] # Hack for mock's limitation with the name attribute for server in servers: server.name = server.uuid mocked_return = mock.Mock(return_value=(hosts, servers)) mock_strategy = mock.Mock(get_preemptible_servers=mocked_return) with mock.patch.object(self.reaper, '_load_configured_strategy') as m: m.return_value = mock_strategy self.assertRaises(exception.RetriesExceeded, self.reaper.free_resources, request, system)
def test_free_resources(self, mock_allocs, mock_delete): mock_projects = [mock.Mock(id_=1), mock.Mock(id_=2)] system = mock.Mock(preemptible_projects=mock_projects) request = fakes.make_reaper_request() hosts = ['host1'] servers = [mock.Mock(uuid='server1'), mock.Mock(uuid='server2')] # Hack for mock's limitation with the name attribute for server in servers: server.name = server.uuid mocked_return = mock.Mock(return_value=(hosts, servers)) not_found = obj_fakes.make_resources() mock_allocs.return_value = not_found mock_strategy = mock.Mock(get_preemptible_servers=mocked_return) with mock.patch.object(self.reaper, '_load_configured_strategy') as m: m.return_value = mock_strategy self.reaper.free_resources(request, system) mock_delete.assert_has_calls( [mock.call('server1'), mock.call('server2')], any_order=True)
def _make_request(self): return fakes.make_reaper_request()