def test_kill_batched_passes_config(self): """Verify that the batched code path correctly passes job config to the api.""" command = KillCommand() config = self.get_valid_config() mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [1]) mock_options.no_batching = False mock_options.config = config fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) fake_context.add_config(config) fake_context.add_expected_query_result( AuroraClientCommandTest.create_query_call_result( AuroraClientCommandTest.create_scheduled_task( 1, ScheduleStatus.RUNNING))) mock_api = fake_context.get_api('test') mock_api.kill_job.return_value = self.create_simple_success_response() command.execute(fake_context) assert mock_api.kill_job.mock_calls == [ call(self.TEST_JOBKEY, mock_options.instance_spec.instance, config=config, message=None) ]
def test_kill_updating_error_batches(self): """Verify that the batch kill path short circuits and includes the lock error message.""" command = KillCommand() mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [1]) mock_options.no_batching = False fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) fake_context.add_expected_query_result(self.create_query_call_result(), job_key=self.TEST_JOBKEY) fake_context.add_expected_query_result( AuroraClientCommandTest.create_query_call_result( AuroraClientCommandTest.create_scheduled_task( 1, ScheduleStatus.RUNNING))) mock_api = fake_context.get_api('test') mock_api.kill_job.return_value = AuroraClientCommandTest.create_blank_response( ResponseCode.JOB_UPDATING_ERROR, "Error.") with pytest.raises(Context.CommandError): command.execute(fake_context) mock_api.kill_job.assert_called_once_with( self.TEST_JOBKEY, mock_options.instance_spec.instance, config=None, message=None) self.assert_lock_message(fake_context)
def test_kill_lock_error_batches(self): """Verify that the batch kill path short circuits and includes the lock error message.""" command = KillCommand() jobkey = AuroraJobKey("cluster", "role", "env", "job") mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(jobkey, [1]) mock_options.no_batching = False fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) fake_context.add_expected_query_result( AuroraClientCommandTest.create_query_call_result( AuroraClientCommandTest.create_scheduled_task( 1, ScheduleStatus.RUNNING))) mock_api = fake_context.get_api('test') mock_api.kill_job.return_value = AuroraClientCommandTest.create_blank_response( ResponseCode.LOCK_ERROR, "Error.") with pytest.raises(Context.CommandError): command.execute(fake_context) mock_api.kill_job.assert_called_once_with( jobkey, mock_options.instance_spec.instance) self.assert_lock_message(fake_context)
def test_restart_with_lock(self): command = RestartCommand() jobkey = AuroraJobKey("cluster", "role", "env", "job") mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(jobkey, []) fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) mock_api = fake_context.get_api("test") mock_api.restart.return_value = AuroraClientCommandTest.create_blank_response( ResponseCode.LOCK_ERROR, "Error.") with pytest.raises(Context.CommandError): command.execute(fake_context) updater_config = UpdaterConfig(mock_options.batch_size, mock_options.restart_threshold, mock_options.watch_secs, mock_options.max_per_instance_failures, mock_options.max_total_failures) mock_api.restart.assert_called_once_with( jobkey, mock_options.instance_spec.instance, updater_config, mock_options.healthcheck_interval_seconds, config=None) self.assert_lock_message(fake_context)
def test_kill_nobatch_passes_config(self): """Verify that the no batch code path correctly passes job config to the api.""" command = KillCommand() config = self.get_valid_config() mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, []) mock_options.no_batching = True mock_options.config = config fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) fake_context.add_config(config) mock_api = fake_context.get_api('test') fake_context.add_expected_query_result( AuroraClientCommandTest.create_empty_task_result()) mock_api.kill_job.return_value = self.create_simple_success_response() command.execute(fake_context) assert mock_api.kill_job.mock_calls == [ call(self.TEST_JOBKEY, mock_options.instance_spec.instance, config=config) ]
def setUp(self): self._fake_context = FakeAuroraCommandContext() self._mock_options = mock_verb_options(DiffCommand()) self._mock_options.instance_spec = TaskInstanceKey( self.TEST_JOBKEY, [0, 1]) self._fake_context.set_options(self._mock_options) self._mock_api = self._fake_context.get_api("west")
def test_start_update_command_line_succeeds(self): resp = self.create_simple_success_response() resp.result = Result(startJobUpdateResult=StartJobUpdateResult( key=JobUpdateKey(job=JobKey( role="role", environment="env", name="name"), id="id"))) self._mock_api.start_job_update.return_value = resp mock_config = self.create_mock_config() self._fake_context.get_job_config = Mock(return_value=mock_config) self._mock_options.instance_spec = TaskInstanceKey(self._job_key, None) self._mock_options.message = 'hello' with patch( 'apache.aurora.client.cli.update.DiffFormatter') as formatter: formatter.return_value = self._formatter assert self._command.execute(self._fake_context) == EXIT_OK assert self._formatter.show_job_update_diff.mock_calls == [ call(self._mock_options.instance_spec.instance) ] assert self._mock_api.start_job_update.mock_calls == [ call(ANY, 'hello', None, ANY) ] assert self._fake_context.get_out() == [ StartUpdate.UPDATE_MSG_TEMPLATE % ('http://something_or_other/scheduler/role/env/name/update/id'), ] assert self._fake_context.get_err() == []
def setUp(self): self._command = AddCommand() self._mock_options = mock_verb_options(self._command) self._mock_options.task_instance = TaskInstanceKey(self.TEST_JOBKEY, 1) self._mock_options.instance_count = 3 self._fake_context = FakeAuroraCommandContext() self._fake_context.set_options(self._mock_options) self._mock_api = self._fake_context.get_api('test')
def setUp(self): self._command = StartUpdate() self._job_key = AuroraJobKey("cluster", "role", "env", "job") self._mock_options = mock_verb_options(self._command) self._mock_options.instance_spec = TaskInstanceKey(self._job_key, []) self._fake_context = FakeAuroraCommandContext() self._fake_context.set_options(self._mock_options) self._mock_api = self._fake_context.get_api('UNUSED')
def setUp(self): self._command = UpdateCommand() self._mock_options = mock_verb_options(self._command) self._mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, []) self._mock_options.force = True self._fake_context = FakeAuroraCommandContext() self._fake_context.set_options(self._mock_options) self._mock_api = self._fake_context.get_api("test")
def setUp(self): self._command = StartUpdate() self._job_key = AuroraJobKey.from_thrift("cluster", UPDATE_KEY.job) self._mock_options = mock_verb_options(self._command) self._mock_options.instance_spec = TaskInstanceKey(self._job_key, None) self._fake_context = FakeAuroraCommandContext() self._fake_context.set_options(self._mock_options) self._mock_api = self._fake_context.get_api('UNUSED')
def setUp(self): self._command = DiffCommand() self._mock_options = mock_verb_options(self._command) self._mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [0, 1]) self._fake_context = FakeAuroraCommandContext() self._fake_context.set_options(self._mock_options) self._mock_api = self._fake_context.get_api("test") self._formatter = Mock(spec=DiffFormatter)
def test_update_no_active_instance_check(self): self._mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [1]) self._mock_options.strict = True mock_config = self.create_mock_config() self._fake_context.get_job_config = Mock(return_value=mock_config) self._mock_api.start_job_update.return_value = self.create_simple_success_response() self._command.execute(self._fake_context) self._mock_api.start_job_update.assert_called_once_with( mock_config, self._mock_options.instance_spec.instance)
def test_update_no_active_instance_check(self): self._mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [1]) self._mock_options.strict = True config = self.get_job_config() self._fake_context.get_job_config = Mock(return_value=config) self._mock_api.update_job.return_value = self.create_simple_success_response() self._command.execute(self._fake_context) assert self._mock_api.update_job.mock_calls == [ call( config, self._mock_options.healthcheck_interval_seconds, self._mock_options.instance_spec.instance)]
def test_kill_batched_queries_active_instances(self): """Verify that the batch kill operates on active instances only.""" command = KillCommand() mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [1]) mock_options.no_batching = False fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) fake_context.add_expected_query_result(AuroraClientCommandTest.create_empty_task_result()) command.execute(fake_context) message = "No tasks to kill found for job %s" % self.TEST_JOBKEY.to_path() assert fake_context.get_err()[0] == message
def test_kill_inactive_instance_spec(self): """Verify the instance spec is validated in a batched kill.""" command = KillCommand() mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [1]) mock_options.no_batching = False mock_options.strict = True fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) fake_context.add_expected_query_result(AuroraClientCommandTest.create_empty_task_result()) with pytest.raises(Context.CommandError) as e: command.execute(fake_context) assert e.value.message == "Invalid instance parameter: [1]"
def test_restart_inactive_instance_spec(self): command = RestartCommand() jobkey = AuroraJobKey("cluster", "role", "env", "job") mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(jobkey, [1]) mock_options.strict = True fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) fake_context.add_expected_query_result(AuroraClientCommandTest.create_empty_task_result()) with pytest.raises(Context.CommandError) as e: command.execute(fake_context) assert e.message == "Invalid instance parameter: [1]"
def test_kill_opens_url(self): """Verify the kill commands opens the job page if requested""" command = KillCommand() mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [1]) mock_options.open_browser = True fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) fake_context.add_expected_query_result(AuroraClientCommandTest.create_empty_task_result()) command.execute(fake_context) assert self.mock_webbrowser.mock_calls == [ call("http://something_or_other/scheduler/bozo/test/hello") ]
def test_update_no_active_instance_check(self): self._mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, [1]) self._mock_options.strict = True mock_config = self.create_mock_config() self._fake_context.get_job_config = Mock(return_value=mock_config) self._mock_api.start_job_update.return_value = self.create_simple_success_response() with patch('apache.aurora.client.cli.update.DiffFormatter') as formatter: formatter.return_value = self._formatter self._command.execute(self._fake_context) assert self._formatter.show_job_update_diff.mock_calls == [ call(self._mock_options.instance_spec.instance) ] assert self._mock_api.start_job_update.mock_calls == [ call(mock_config, None, self._mock_options.instance_spec.instance, ANY) ]
def test_kill_batched_queries_active_instances(self): """Verify that the batch kill operates on active instances only.""" command = KillCommand() jobkey = AuroraJobKey("cluster", "role", "env", "job") mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(jobkey, [1]) mock_options.no_batching = False fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) fake_context.add_expected_query_result( AuroraClientCommandTest.create_empty_task_result()) command.execute(fake_context) assert fake_context.get_err( )[0] == "No tasks to kill found for job cluster/role/env/job"
def test_restart_opens_url(self): command = RestartCommand() jobkey = AuroraJobKey("cluster", "role", "env", "job") mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(jobkey, None) mock_options.strict = True mock_options.open_browser = True fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) fake_api = fake_context.fake_api fake_api.restart.return_value = AuroraClientCommandTest.create_simple_success_response() command.execute(fake_context) assert self.mock_webbrowser.mock_calls == [ call("http://something_or_other/scheduler/role/env/job") ]
def test_kill_lock_error_nobatch(self): """Verify that the no batch code path correctly includes the lock error message.""" command = KillCommand() mock_options = mock_verb_options(command) mock_options.instance_spec = TaskInstanceKey(self.TEST_JOBKEY, []) mock_options.no_batching = True fake_context = FakeAuroraCommandContext() fake_context.set_options(mock_options) mock_api = fake_context.get_api('test') mock_api.kill_job.return_value = AuroraClientCommandTest.create_blank_response( ResponseCode.LOCK_ERROR, "Error.") with pytest.raises(Context.CommandError): command.execute(fake_context) mock_api.kill_job.assert_called_once_with( self.TEST_JOBKEY, mock_options.instance_spec.instance, config=None) self.assert_lock_message(fake_context)
def test_start_update_command_line_succeeds(self): resp = self.create_simple_success_response() resp.result = Result(startJobUpdateResult=StartJobUpdateResult( key=JobUpdateKey(job=JobKey( role="role", environment="env", name="name"), id="id"))) self._mock_api.start_job_update.return_value = resp mock_config = self.create_mock_config() self._fake_context.get_job_config = Mock(return_value=mock_config) self._mock_options.instance_spec = TaskInstanceKey(self._job_key, None) self._mock_options.message = 'hello' assert self._command.execute(self._fake_context) == EXIT_OK update_url_msg = StartUpdate.UPDATE_MSG_TEMPLATE % ( 'http://something_or_other/scheduler/role/env/name/id') assert self._mock_api.start_job_update.mock_calls == [ call(ANY, 'hello', None) ] assert self._fake_context.get_out() == [update_url_msg] assert self._fake_context.get_err() == []