def test_successful_batched_killall_job(self): """Run a test of the "kill" command against a mocked-out API: Verifies that the kill command sends the right API RPCs, and performs the correct tests on the result.""" mock_options = self.setup_mock_options() mock_options.batch_size = 5 mock_config = Mock() (mock_api, mock_scheduler_proxy) = self.create_mock_api() mock_api.kill_job.return_value = self.get_kill_job_response() mock_api.check_status.return_value = self.create_status_call_result() with contextlib.nested( patch('apache.aurora.client.commands.core.make_client', return_value=mock_api), patch('twitter.common.app.get_options', return_value=mock_options), patch('apache.aurora.client.commands.core.get_job_config', return_value=mock_config), patch('apache.aurora.client.commands.core.JobMonitor')) as ( mock_make_client, options, mock_get_job_config, mock_monitor): with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() killall(['west/mchucarroll/test/hello', fp.name], mock_options) # Now check that the right API calls got made. assert mock_api.kill_job.call_count == 4 mock_api.kill_job.assert_called_with( AuroraJobKey(cluster=self.TEST_CLUSTER, role=self.TEST_ROLE, env=self.TEST_ENV, name=self.TEST_JOB), [15, 16, 17, 18, 19])
def test_simple_successful_killall_job(self): """Run a test of the "kill" command against a mocked-out API: Verifies that the kill command sends the right API RPCs, and performs the correct tests on the result.""" mock_options = self.setup_mock_options() mock_config = Mock() (mock_api, mock_scheduler_proxy) = self.create_mock_api() mock_api.kill_job.return_value = self.get_kill_job_response() mock_scheduler_proxy.killTasks.return_value = self.get_kill_job_response() mock_query_results = [ self.create_mock_status_query_result(ScheduleStatus.RUNNING), self.create_mock_status_query_result(ScheduleStatus.KILLING), self.create_mock_status_query_result(ScheduleStatus.KILLED), ] mock_scheduler_proxy.getTasksWithoutConfigs.side_effect = mock_query_results mock_event = create_autospec(spec=threading.Event) with contextlib.nested( patch('time.sleep'), patch('apache.aurora.client.commands.core.make_client', return_value=mock_api), patch('twitter.common.app.get_options', return_value=mock_options), patch('apache.aurora.client.commands.core.get_job_config', return_value=mock_config), patch('threading._Event.wait', return_value=mock_event)): with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() killall(['west/mchucarroll/test/hello', fp.name], mock_options) self.assert_kill_job_called(mock_api) mock_api.kill_job.assert_called_with( AuroraJobKey(cluster=self.TEST_CLUSTER, role=self.TEST_ROLE, env=self.TEST_ENV, name=self.TEST_JOB), None, config=mock_config) self.assert_scheduler_called(mock_api, self.get_expected_task_query(), 3)
def test_kill_job_api_level(self): """Test kill client-side API logic.""" mock_options = self.setup_mock_options() mock_config = Mock() mock_config.hooks = [] mock_config.raw.return_value.enable_hooks.return_value.get.return_value = False (mock_api, mock_scheduler_proxy) = self.create_mock_api() mock_scheduler_proxy.killTasks.return_value = self.get_kill_job_response() mock_query_results = [ self.create_mock_status_query_result(ScheduleStatus.RUNNING), self.create_mock_status_query_result(ScheduleStatus.KILLING), self.create_mock_status_query_result(ScheduleStatus.KILLED), ] mock_scheduler_proxy.getTasksWithoutConfigs.side_effect = mock_query_results with contextlib.nested( patch('threading._Event.wait'), patch('apache.aurora.client.factory.make_client', return_value=mock_api), patch('apache.aurora.client.api.SchedulerProxy', return_value=mock_scheduler_proxy), patch('apache.aurora.client.factory.CLUSTERS', new=self.TEST_CLUSTERS), patch('twitter.common.app.get_options', return_value=mock_options), patch('apache.aurora.client.commands.core.get_job_config', return_value=mock_config)): with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() killall(['west/mchucarroll/test/hello', fp.name], mock_options) # Now check that the right API calls got made. assert mock_scheduler_proxy.killTasks.call_count == 1 query = self.get_expected_task_query() mock_scheduler_proxy.killTasks.assert_called_with(query, None) self.assert_scheduler_called(mock_api, query, 3)
def test_successful_batched_killall_job(self): """Run a test of the "kill" command against a mocked-out API: Verifies that the kill command sends the right API RPCs, and performs the correct tests on the result.""" mock_options = self.setup_mock_options() mock_options.batch_size = 5 mock_config = Mock() (mock_api, mock_scheduler_proxy) = self.create_mock_api() mock_api.kill_job.return_value = self.get_kill_job_response() mock_api.check_status.return_value = self.create_status_call_result() with contextlib.nested( patch('apache.aurora.client.commands.core.make_client', return_value=mock_api), patch('twitter.common.app.get_options', return_value=mock_options), patch('apache.aurora.client.commands.core.get_job_config', return_value=mock_config), patch('apache.aurora.client.commands.core.JobMonitor')): with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() killall(['west/mchucarroll/test/hello', fp.name], mock_options) # Now check that the right API calls got made. assert mock_api.kill_job.call_count == 4 mock_api.kill_job.assert_called_with( AuroraJobKey(cluster=self.TEST_CLUSTER, role=self.TEST_ROLE, env=self.TEST_ENV, name=self.TEST_JOB), [15, 16, 17, 18, 19])
def test_simple_successful_killall_job(self): """Run a test of the "kill" command against a mocked-out API: Verifies that the kill command sends the right API RPCs, and performs the correct tests on the result.""" mock_options = self.setup_mock_options() mock_config = Mock() (mock_api, mock_scheduler_proxy) = self.create_mock_api() mock_api.kill_job.return_value = self.get_kill_job_response() mock_scheduler_proxy.killTasks.return_value = self.get_kill_job_response() mock_query_results = [ self.create_mock_status_query_result(ScheduleStatus.RUNNING), self.create_mock_status_query_result(ScheduleStatus.KILLING), self.create_mock_status_query_result(ScheduleStatus.KILLED), ] mock_scheduler_proxy.getTasksWithoutConfigs.side_effect = mock_query_results with contextlib.nested( patch('time.sleep'), patch('apache.aurora.client.commands.core.make_client', return_value=mock_api), patch('twitter.common.app.get_options', return_value=mock_options), patch('apache.aurora.client.commands.core.get_job_config', return_value=mock_config)): with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() killall(['west/mchucarroll/test/hello', fp.name], mock_options) # Now check that the right API calls got made. self.assert_kill_job_called(mock_api) mock_api.kill_job.assert_called_with( AuroraJobKey(cluster=self.TEST_CLUSTER, role=self.TEST_ROLE, env=self.TEST_ENV, name=self.TEST_JOB), None, config=mock_config) self.assert_scheduler_called(mock_api, self.get_expected_task_query(), 3)
def test_kill_job_api_level(self): """Test kill client-side API logic.""" mock_options = self.setup_mock_options() mock_config = Mock() mock_config.hooks = [] mock_config.raw.return_value.enable_hooks.return_value.get.return_value = False (mock_api, mock_scheduler_proxy) = self.create_mock_api() mock_scheduler_proxy.killTasks.return_value = self.get_kill_job_response() with contextlib.nested( patch('apache.aurora.client.factory.make_client', return_value=mock_api), patch('apache.aurora.client.api.SchedulerProxy', return_value=mock_scheduler_proxy), patch('apache.aurora.client.factory.CLUSTERS', new=self.TEST_CLUSTERS), patch('twitter.common.app.get_options', return_value=mock_options), patch('apache.aurora.client.commands.core.get_job_config', return_value=mock_config)) as ( mock_api_patch, mock_scheduler_proxy_class, mock_clusters, options, mock_get_job_config): with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() killall(['west/mchucarroll/test/hello', fp.name], mock_options) # Now check that the right API calls got made. assert mock_scheduler_proxy.killTasks.call_count == 1 mock_scheduler_proxy.killTasks.assert_called_with(self.get_expected_task_query(), None)
def test_happy_hook(self): """Test that hooks that return 0 don't block command execution""" class HappyHook(CoreCommandHook): @property def name(self): return "I'm so happy" def execute(self, cmd, options, *args, **kwargs): return 0 CoreCommandHook.register_hook(HappyHook()) mock_options = self.setup_mock_options() mock_config = Mock() (mock_api, mock_scheduler_proxy) = self.create_mock_api() mock_api.kill_job.return_value = self.get_kill_job_response() mock_scheduler_proxy.killTasks.return_value = self.get_kill_job_response() mock_query_results = [ self.create_mock_status_query_result(ScheduleStatus.RUNNING), self.create_mock_status_query_result(ScheduleStatus.KILLING), self.create_mock_status_query_result(ScheduleStatus.KILLED), ] mock_scheduler_proxy.getTasksWithoutConfigs.side_effect = mock_query_results with contextlib.nested( patch('threading._Event.wait'), patch('apache.aurora.client.commands.core.make_client', return_value=mock_api), patch('twitter.common.app.get_options', return_value=mock_options), patch('apache.aurora.client.commands.core.get_job_config', return_value=mock_config)) as ( sleep, mock_make_client, options, mock_get_job_config): with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() killall(['west/mchucarroll/test/hello', fp.name], mock_options) # Now check that the right API calls got made. self.assert_kill_job_called(mock_api) mock_api.kill_job.assert_called_with( AuroraJobKey(cluster=self.TEST_CLUSTER, role=self.TEST_ROLE, env=self.TEST_ENV, name=self.TEST_JOB), None, config=mock_config) self.assert_scheduler_called(mock_api, self.get_expected_task_query(), 3) CoreCommandHook.clear_hooks()