def test_transient_error(_, client): mock_scheduler_client = mock.create_autospec( spec=scheduler_client.SchedulerClient, spec_set=False, instance=True) mock_thrift_client = mock.create_autospec(spec=AuroraAdmin.Client, instance=True) mock_thrift_client.killTasks.side_effect = [ Response(responseCode=ResponseCode.ERROR_TRANSIENT, details=[ ResponseDetail(message="message1"), ResponseDetail(message="message2") ], serverInfo=DEFAULT_RESPONSE.serverInfo), Response(responseCode=ResponseCode.ERROR_TRANSIENT, serverInfo=DEFAULT_RESPONSE.serverInfo), Response(responseCode=ResponseCode.OK, serverInfo=DEFAULT_RESPONSE.serverInfo) ] mock_scheduler_client.get_thrift_client.return_value = mock_thrift_client client.get.return_value = mock_scheduler_client proxy = TestSchedulerProxy(Cluster(name='local')) proxy.killTasks(TaskQuery(), None) assert mock_thrift_client.killTasks.call_count == 3
def test_combine_messages(self): resp = Response(responseCode=ResponseCode.ERROR) assert base.combine_messages(resp) == '' resp = Response(responseCode=ResponseCode.ERROR, details=[]) assert base.combine_messages(resp) == '' resp = Response(responseCode=ResponseCode.ERROR, details=[ResponseDetail(message='Error')]) assert base.combine_messages(resp) == 'Error' resp = Response( responseCode=ResponseCode.ERROR, details=[ResponseDetail(message='Error1'), ResponseDetail(message='Error2')]) assert base.combine_messages(resp) == 'Error1, Error2'
def test_start_update_command_line_succeeds_noop_update(self): mock_context = FakeAuroraCommandContext() resp = self.create_simple_success_response() resp.details = [ResponseDetail(message="Noop update.")] with contextlib.nested( patch('apache.aurora.client.cli.update.Update.create_context', return_value=mock_context), patch('apache.aurora.client.factory.CLUSTERS', new=self.TEST_CLUSTERS)): mock_api = mock_context.get_api('west') mock_api.start_job_update.return_value = resp with temporary_file() as fp: fp.write(self.get_valid_config()) fp.flush() cmd = AuroraCommandLine() result = cmd.execute( ['beta-update', 'start', self.TEST_JOBSPEC, fp.name]) assert result == EXIT_OK assert mock_api.start_job_update.call_count == 1 args, kwargs = mock_api.start_job_update.call_args assert isinstance(args[0], AuroraConfig) assert args[1] is None assert mock_context.get_out() == ["Noop update."] assert mock_context.get_err() == []
def create_response(cls, quota, prod, non_prod, response_code=ResponseCode.OK): return Response( responseCode=response_code, details=[ResponseDetail(message='test')], result=Result(getQuotaResult=GetQuotaResult( quota=quota, prodSharedConsumption=prod, nonProdSharedConsumption=non_prod)) )
def create_response(cls, quota, prod, non_prod, response_code=None): response_code = ResponseCode.OK if response_code is None else response_code resp = Response(responseCode=response_code, details=[ResponseDetail(message='test')]) resp.result = Result(getQuotaResult=GetQuotaResult( quota=quota, prodConsumption=prod, nonProdConsumption=non_prod)) return resp
def mock_get_tasks(self, tasks, response_code=None): response_code = ResponseCode.OK if response_code is None else response_code resp = Response(responseCode=response_code, details=[ResponseDetail(message='test')]) resp.result = Result(scheduleStatusResult=ScheduleStatusResult( tasks=tasks)) self._scheduler.getTasksWithoutConfigs.return_value = resp
def create_blank_response(cls, code, msg): # TODO(wfarner): Don't use a mock here. response = create_autospec(spec=Response, instance=True) response.responseCode = code response.result = create_autospec(spec=Result, instance=True) response.details = [ResponseDetail(message=msg)] return response
def mock_get_quota(self, allocated, consumed, response_code=None): response_code = ResponseCode.OK if response_code is None else response_code resp = Response(responseCode=response_code, details=[ResponseDetail(message='test')]) resp.result = Result( getQuotaResult=GetQuotaResult( quota=deepcopy(allocated), prodSharedConsumption=deepcopy(consumed))) self._scheduler.getQuota.return_value = resp
def validate_quota_from_requested(self, job_key, production, released, acquired): """Validates requested change will not exceed the available quota. Arguments: job_key -- job key. production -- production flag. released -- production CapacityRequest to be released (in case of job update). acquired -- production CapacityRequest to be acquired. Returns: ResponseCode.OK if check is successful. """ # TODO(wfarner): Avoid synthesizing scheduler responses. resp_ok = Response( responseCode=ResponseCode.OK, details=[ResponseDetail(message='Quota check successful.')]) if not production: return resp_ok resp = self._scheduler.getQuota(job_key.role) if resp.responseCode != ResponseCode.OK: log.error('Failed to get quota from scheduler: %s' % combine_messages(resp)) return resp allocated = CapacityRequest(resp.result.getQuotaResult.quota) consumed = CapacityRequest( resp.result.getQuotaResult.prodSharedConsumption) requested = acquired - released effective = allocated - consumed - requested if not effective.valid(): log.info('Not enough quota to create/update job.') print_quota(allocated.quota(), 'Total allocated quota', job_key.role) print_quota(consumed.quota(), 'Consumed quota', job_key.role) print_quota(requested.quota(), 'Requested', job_key.name) print_quota(effective.invert_or_reset().quota(), 'Additional quota required', job_key.role) # TODO(wfarner): Avoid synthesizing scheduler responses. return Response( responseCode=ResponseCode.INVALID_REQUEST, details=[ResponseDetail(message='Failed quota check.')]) return resp_ok
def mock_scheduler(cls, response_code=None): scheduler = create_autospec(spec=SchedulerThriftApiSpec, instance=True) response_code = ResponseCode.OK if response_code is None else response_code resp = Response(responseCode=response_code, details=[ResponseDetail(message='test')]) resp.result = Result(scheduleStatusResult=ScheduleStatusResult( tasks=cls.create_tasks())) scheduler.getTasksWithoutConfigs.return_value = resp return scheduler
def expect_get_statuses(self, instance_ids=WATCH_INSTANCES, num_calls=EXPECTED_CYCLES): tasks = [self.create_task(instance_id) for instance_id in instance_ids] response = Response( responseCode=ResponseCode.OK, details=[ResponseDetail(message='test')], result=Result(scheduleStatusResult=ScheduleStatusResult( tasks=tasks))) query = self.get_tasks_status_query(instance_ids) for _ in range(int(num_calls)): self._scheduler.getTasksWithoutConfigs(query).AndReturn(response)
def test_start_update_command_line_succeeds_noop_update(self): resp = self.create_simple_success_response() resp.details = [ResponseDetail(message="Noop update.")] 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) result = self._command.execute(self._fake_context) assert result == EXIT_OK assert self._mock_api.start_job_update.mock_calls == [ call(ANY, None, None) ] assert self._fake_context.get_out() == ["Noop update."] assert self._fake_context.get_err() == []
def test_start_update_command_line_succeeds_noop_update(self): resp = self.create_simple_success_response() resp.details = [ResponseDetail(message="Noop update.")] 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) 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, None, None, ANY)] assert self._fake_context.get_out() == [ "Noop update." ] assert self._fake_context.get_err() == []
def create_response(cls, locks, response_code=None): response_code = ResponseCode.OK if response_code is None else response_code resp = Response(responseCode=response_code, details=[ResponseDetail(message='test')]) resp.result = Result(getLocksResult=GetLocksResult(locks=locks)) return resp
def test_format_response_with_details(self): resp = Response(responseCode=ResponseCode.ERROR, details=[ResponseDetail(message='Error')]) formatted = base.format_response(resp) assert formatted == 'Response from scheduler: ERROR (message: Error)'
def create_response(cls, tasks, response_code=ResponseCode.OK): return Response( responseCode=response_code, details=[ResponseDetail(message='test')], result=Result(scheduleStatusResult=ScheduleStatusResult( tasks=tasks)))
def make_response(code=ResponseCode.OK, message='test', result=None): return Response( responseCode=code, details=[ResponseDetail(message=message)], result=result, serverInfo=ServerInfo(clusterName='test', thriftAPIVersion=THRIFT_API_VERSION))
def create_blank_response(cls, code, msg): return Response(responseCode=code, details=[ResponseDetail(message=msg)])
def create_blank_response(cls, code, msg): return Response(responseCode=code, details=[ResponseDetail(message=msg)], result=create_autospec(spec=Result, spec_set=True, instance=True))
def create_blank_response(cls, code, msg): return Response( responseCode=code, details=[ResponseDetail(message=msg)], serverInfo=ServerInfo(thriftAPIVersion=CURRENT_API_VERSION.major))
def _failed_response(self, message): # TODO(wfarner): Avoid synthesizing scheduler responses, consider using an exception instead. return Response(responseCode=ResponseCode.ERROR, details=[ResponseDetail(message=message)])
def make_response(code, msg='test'): return Response(responseCode=code, serverInfo=SERVER_INFO, details=[ResponseDetail(message=msg)])
def make_response(code=ResponseCode.OK, message='test', result=None): return Response( responseCode=code, details=[ResponseDetail(message=message)], result=result, serverInfo=ServerInfo(clusterName='test'))