def test_configure_hardware_fail(requirements): hardware_interface = HardwareInterface(Worker()) worker_requirements = Worker(configs=requirements) with pytest.raises(FailedPreconditionError): hardware_interface.configure_hardware(worker_requirements.get_pb2())
def cli(context, parent, update_period, executing_update_period, remote, auth_token, client_key, client_cert, server_cert, remote_cas, cas_client_key, cas_client_cert, cas_server_cert, worker_property, verbose): setup_logging(verbosity=verbose) # Setup the remote execution server channel: try: context.channel, details = setup_channel(remote, auth_token=auth_token, server_cert=server_cert, client_key=client_key, client_cert=client_cert) if remote_cas is not None and remote_cas != remote: context.cas_channel, details = setup_channel( remote_cas, server_cert=cas_server_cert, client_key=cas_client_key, client_cert=cas_client_cert) context.remote_cas_url = remote_cas else: context.cas_channel = context.channel context.remote_cas_url = remote context.cas_client_key, context.cas_client_cert, context.cas_server_cert = details except InvalidArgumentError as e: click.echo("Error: {}.".format(e), err=True) sys.exit(-1) context.parent = parent bot_interface = interface.BotInterface(context.channel, update_period, executing_update_period) worker_properties_dict = {} for property_name, property_value in worker_property: if property_name not in worker_properties_dict: worker_properties_dict[property_name] = set() worker_properties_dict[property_name].add(property_value) worker = Worker() worker.add_device(Device(properties=worker_properties_dict)) hardware_interface = HardwareInterface(worker) context.bot_interface = bot_interface context.hardware_interface = hardware_interface
def test_cancel_leases(instance): def __work(lease, context, cancel_event): # while not cancel_event.is_set(): return lease with serve_bots_interface([instance]) as server: interface = ServerInterface(server.remote) hardware_interface = HardwareInterface(Worker()) session = BotSession(instance, interface, hardware_interface, __work) lease = bots_pb2.Lease() lease.state = LeaseState.PENDING.value lease.id = 'foo' server.inject_work(lease) session.create_bot_session() leases_pb2 = session.get_pb2().leases assert len(leases_pb2) == 1 assert leases_pb2[0].state == LeaseState.ACTIVE.value server.cancel_lease(leases_pb2[0].id) session.update_bot_session() assert len(session.get_pb2().leases) == 1 loop = asyncio.get_event_loop() for task in asyncio.Task.all_tasks(): try: loop.run_until_complete(task) except asyncio.CancelledError: pass assert session.get_pb2().leases[0].state == LeaseState.CANCELLED.value
def test_worker_property(prop): worker = Worker(properties=prop) protobuf_worker = worker.get_pb2() proto_prop = {} for p in protobuf_worker.properties: k = p.key v = p.value proto_prop_values = proto_prop.get(k) if not proto_prop_values: proto_prop[k] = [v] else: proto_prop_values.append(v) assert prop == proto_prop assert worker.properties == prop
def test_worker_config(config): worker = Worker(configs=config) protobuf_worker = worker.get_pb2() proto_cfg = {} for cfg in protobuf_worker.configs: k = cfg.key v = cfg.value proto_cfg_values = proto_cfg.get(k) if not proto_cfg_values: proto_cfg[k] = [v] else: proto_cfg_values.append(v) assert config == proto_cfg assert worker.configs == config
def test_create_bot_session(instance): with serve_bots_interface([instance]) as server: interface = ServerInterface(server.remote) hardware_interface = HardwareInterface(Worker()) session = BotSession(instance, interface, hardware_interface, None) session.create_bot_session() assert session.get_pb2() == server.get_bot_session()
def test_create_bot_session_with_work(instance): def __work(lease, context, event): return lease with serve_bots_interface([instance]) as server: interface = ServerInterface(server.remote) hardware_interface = HardwareInterface(Worker()) session = BotSession(instance, interface, hardware_interface, __work) server.inject_work() session.create_bot_session() assert len(session.get_pb2().leases) == 1 loop = asyncio.get_event_loop() for task in asyncio.Task.all_tasks(): loop.run_until_complete(task) assert session.get_pb2().leases[0].state == LeaseState.COMPLETED.value
def test_create_hardware(): worker = Worker() interface = HardwareInterface(worker) device0 = Device() worker.add_device(device0) protobuf_worker = interface.get_worker_pb2() assert len(protobuf_worker.devices) == 1 worker.add_device(Device()) protobuf_worker = interface.get_worker_pb2() assert len(protobuf_worker.devices) == 2 assert protobuf_worker.devices[0].handle != protobuf_worker.devices[1].handle assert device0.name == protobuf_worker.devices[0].handle
def test_configure_hardware(requirements): hardware_interface = HardwareInterface(Worker(configs=requirements)) worker_requirements = Worker(configs=requirements) hardware_interface.configure_hardware(worker_requirements.get_pb2())
def test_worker_property_fail(prop): with pytest.raises(KeyError): Worker(properties=prop)
def test_worker_config_fail(config): with pytest.raises(KeyError): Worker(configs=config)