def test_update_bot_session_and_wait_for_expiry(bot_session, context, instance, sleep_duration): bots_interface = instance._get_instance("") bot_session_keepalive_timeout = bots_interface._bot_session_keepalive_timeout with mock.patch.object(bots_interface, '_close_bot_session', autospec=True) as close_botsession_fn: request = bots_pb2.CreateBotSessionRequest(bot_session=bot_session) bot = instance.CreateBotSession(request, context) request_time = datetime.utcnow() request = bots_pb2.UpdateBotSessionRequest(name=bot.name, bot_session=bot) response = instance.UpdateBotSession(request, context) check_bot_session_request_response_and_assigned_expiry( instance, bot_session, response, request_time) if bot_session_keepalive_timeout: assert bots_interface._next_expire_time_occurs_in() >= 0 time.sleep(sleep_duration) bots_interface._reap_next_expired_session() # Call this manually since the asyncio event loop isn't running if bot_session_keepalive_timeout and bot_session_keepalive_timeout <= sleep_duration: # the BotSession should have expired after sleeping `sleep_duration` assert close_botsession_fn.call_count == 1 else: # no timeout, or timeout > 1, shouldn't see any expiries yet. assert close_botsession_fn.call_count == 0
def create_bot_session(self, parent, bot_session): """ Create bot session request Returns BotSession if correct else a grpc StatusCode """ request = bots_pb2.CreateBotSessionRequest(parent=parent, bot_session=bot_session) return self._bot_call(self._stub.CreateBotSession, request)
def test_create_bot_session_bot_id_fail(context, instance): bot = bots_pb2.BotSession() request = bots_pb2.CreateBotSessionRequest(parent='', bot_session=bot) instance.CreateBotSession(request, context) context.set_code.assert_called_once_with(grpc.StatusCode.INVALID_ARGUMENT)
def test_create_bot_session(bot_session, context, instance): request_time = datetime.utcnow() request = bots_pb2.CreateBotSessionRequest(bot_session=bot_session) response = instance.CreateBotSession(request, context) check_bot_session_request_response_and_assigned_expiry( instance, bot_session, response, request_time)
def test_number_of_leases(number_of_jobs, bot_session, context, instance): for _ in range(0, number_of_jobs): _inject_work(instance._instances[""]._scheduler) request = bots_pb2.CreateBotSessionRequest(bot_session=bot_session) response = instance.CreateBotSession(request, context) assert len(response.leases) == min(number_of_jobs, 1)
def test_update_bot_session_zombie(bot_session, context, instance): request = bots_pb2.CreateBotSessionRequest(bot_session=bot_session) bot = instance.CreateBotSession(request, context) # Update server with incorrect UUID by rotating it bot.name = bot.name[len(bot.name):0] request = bots_pb2.UpdateBotSessionRequest(name=bot.name, bot_session=bot) instance.UpdateBotSession(request, context) context.set_code.assert_called_once_with(grpc.StatusCode.INVALID_ARGUMENT)
def test_unmet_platform_requirements(bot_session, context, instance): request = bots_pb2.CreateBotSessionRequest(parent='', bot_session=bot_session) action_digest = remote_execution_pb2.Digest(hash='gaff') _inject_work(instance._instances[""]._scheduler, action_digest=action_digest, platform_requirements={'OSFamily': set('wonderful-os')}) response = instance.CreateBotSession(request, context) assert len(response.leases) == 0
def test_unhealthy_bot(bot_session, context, instance): # set botstatus to unhealthy bot_session.status = BotStatus.UNHEALTHY.value request = bots_pb2.CreateBotSessionRequest(parent='', bot_session=bot_session) action_digest = remote_execution_pb2.Digest(hash='gaff') _inject_work(instance._instances[""]._scheduler, action_digest=action_digest) response = instance.CreateBotSession(request, context) # No leases should be given assert len(response.leases) == 0
def test_update_leases_with_work(bot_session, context, instance): request = bots_pb2.CreateBotSessionRequest(parent='', bot_session=bot_session) action_digest = remote_execution_pb2.Digest(hash='gaff') _inject_work(instance._instances[""]._scheduler, action_digest=action_digest) response = instance.CreateBotSession(request, context) assert len(response.leases) == 1 response_action = remote_execution_pb2.Digest() response.leases[0].payload.Unpack(response_action) assert isinstance(response, bots_pb2.BotSession) assert response.leases[0].state == LeaseState.PENDING.value assert response_action == action_digest
def __test_create_server(queue, remote): # Open a channel to the remote server: channel = grpc.insecure_channel(remote) try: stub = remote_execution_pb2_grpc.ExecutionStub(channel) request = remote_execution_pb2.ExecuteRequest(instance_name='main') response = next(stub.Execute(request)) assert response.DESCRIPTOR is operations_pb2.Operation.DESCRIPTOR except grpc.RpcError as e: if e.code() == grpc.StatusCode.UNIMPLEMENTED: queue.put(False) except AssertionError: queue.put(False) try: stub = remote_execution_pb2_grpc.ActionCacheStub(channel) request = remote_execution_pb2.GetActionResultRequest(instance_name='main') response = stub.GetActionResult(request) assert response.DESCRIPTOR is remote_execution_pb2.ActionResult.DESCRIPTOR except grpc.RpcError as e: if e.code() == grpc.StatusCode.UNIMPLEMENTED: queue.put(False) except AssertionError: queue.put(False) try: stub = remote_execution_pb2_grpc.ContentAddressableStorageStub(channel) request = remote_execution_pb2.BatchUpdateBlobsRequest(instance_name='main') response = stub.BatchUpdateBlobs(request) assert response.DESCRIPTOR is remote_execution_pb2.BatchUpdateBlobsResponse.DESCRIPTOR except grpc.RpcError as e: if e.code() == grpc.StatusCode.UNIMPLEMENTED: queue.put(False) except AssertionError: queue.put(False) try: stub = buildstream_pb2_grpc.ReferenceStorageStub(channel) request = buildstream_pb2.GetReferenceRequest(instance_name='main') response = stub.GetReference(request) assert response.DESCRIPTOR is buildstream_pb2.GetReferenceResponse.DESCRIPTOR except grpc.RpcError as e: if e.code() == grpc.StatusCode.UNIMPLEMENTED: queue.put(False) except AssertionError: queue.put(False) try: stub = bytestream_pb2_grpc.ByteStreamStub(channel) request = bytestream_pb2.ReadRequest() response = stub.Read(request) assert next(response).DESCRIPTOR is bytestream_pb2.ReadResponse.DESCRIPTOR except grpc.RpcError as e: if e.code() == grpc.StatusCode.UNIMPLEMENTED: queue.put(False) except AssertionError: queue.put(False) try: stub = operations_pb2_grpc.OperationsStub(channel) request = operations_pb2.ListOperationsRequest(name='main') response = stub.ListOperations(request) assert response.DESCRIPTOR is operations_pb2.ListOperationsResponse.DESCRIPTOR except grpc.RpcError as e: if e.code() == grpc.StatusCode.UNIMPLEMENTED: queue.put(False) except AssertionError: queue.put(False) try: session = bots_pb2.BotSession(leases=[], bot_id="test-bot", name="test-bot", status=BotStatus.OK.value) stub = bots_pb2_grpc.BotsStub(channel) request = bots_pb2.CreateBotSessionRequest(parent='main', bot_session=session) response = stub.CreateBotSession(request) assert response.DESCRIPTOR is bots_pb2.BotSession.DESCRIPTOR except grpc.RpcError as e: if e.code() == grpc.StatusCode.UNIMPLEMENTED: queue.put(False) except AssertionError: queue.put(False) # Sleep for 5 seconds to allow a useful number of metrics to get reported time.sleep(5) queue.put(True)