def test_vm_and_vm_id_property_methods() -> None: """Unit test for Address.vm and Address.vm_id methods""" # Test getter vm = SpecificLocation(id=UID()) address_with_vm = Address( network=SpecificLocation(id=UID()), domain=SpecificLocation(id=UID()), device=SpecificLocation(id=UID()), vm=vm, ) # Test device getter assert address_with_vm.vm == vm # Test device setter an_id = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) new_vm = SpecificLocation(id=an_id) address_with_vm.vm = new_vm assert address_with_vm.vm == new_vm # Test domain_id getter address_without_vm = Address( network=SpecificLocation(id=UID()), domain=SpecificLocation(id=UID()), device=SpecificLocation(id=UID()), ) assert address_with_vm.vm_id == an_id assert address_without_vm.vm_id is None
def test_domain_and_domain_id_property_methods() -> None: """Unit test for Address.domain and Address.domain_id methods""" # Test getter domain = SpecificLocation(id=UID()) address_with_domain = Address( network=SpecificLocation(id=UID()), domain=domain, device=SpecificLocation(id=UID()), vm=SpecificLocation(id=UID()), ) # Test domain getter assert address_with_domain.domain == domain # Test domain setter an_id = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) new_domain = SpecificLocation(id=an_id) address_with_domain.domain = new_domain assert address_with_domain.domain == new_domain # Test domain_id getter address_without_domain = Address( network=SpecificLocation(id=UID()), device=SpecificLocation(id=UID()), vm=SpecificLocation(id=UID()), ) assert address_with_domain.domain_id == an_id assert address_without_domain.domain_id is None
def test_proto_deserialization() -> None: """Tests that default Address deserialization works as expected - from Protobuf""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) loc = SpecificLocation(id=uid, name="Test Location") obj = Address( network=loc, domain=loc, device=loc, vm=loc, ) blob = Address.get_protobuf_schema()( has_network=True, has_domain=True, has_device=True, has_vm=True, network=loc.serialize(), domain=loc.serialize(), device=loc.serialize(), vm=loc.serialize(), ) obj2 = sy.deserialize(blob=blob, from_proto=True) assert obj == obj2
def test_proto_serialization() -> None: """Tests that default Address serialization works as expected - to Protobuf""" uid = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) loc = SpecificLocation(id=uid, name="Test Location") obj = Address( name="Test Address", network=loc, domain=loc, device=loc, vm=loc, ) blob = Address.get_protobuf_schema()( name="Test Address", has_network=True, has_domain=True, has_device=True, has_vm=True, network=sy.serialize(loc), domain=sy.serialize(loc), device=sy.serialize(loc), vm=sy.serialize(loc), ) assert sy.serialize(obj, to_proto=True) == blob assert sy.serialize(obj, to_proto=True) == blob assert sy.serialize(obj, to_proto=True) == blob
def test_pprint_property_method(address_kwargs: dict, expected_icon: str) -> None: """Unit tests for Address.pprint property method""" named_address = Address(name="Sneaky Nahua", **address_kwargs) assert named_address.pprint == expected_icon + " Sneaky Nahua (Address)" unnamed_address = Address(**address_kwargs) assert expected_icon in unnamed_address.pprint assert "(Address)" in unnamed_address.pprint
def test_target_emoji_method() -> None: """Unit test for Address.target_emoji method""" an_id = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) address = Address( network=SpecificLocation(id=an_id), domain=SpecificLocation(id=an_id), device=SpecificLocation(id=an_id), vm=SpecificLocation(id=an_id), ) assert address.target_emoji() == "@<UID:🙍🛖>"
def test_get_object_action_serde() -> None: msg = GetObjectAction(id_at_location=UID(), address=Address(), reply_to=Address(), msg_id=UID()) blob = serialize(msg) msg2 = sy.deserialize(blob=blob) assert msg.id == msg2.id assert msg.id_at_location == msg2.id_at_location assert msg.address == msg2.address assert msg.reply_to == msg2.reply_to
def test_get_all_tensors_response_serde() -> None: target = Address(name="Alice") request_content = { "workers": { "626sadaf631": { "tensor": [1, 2, 3, 4, 5, 6], "description": "Tensor description", "tags": ["#x", "#data-sample"], "pointable": True, }, "a84ew64wq6e": { "tensor": [9, 8, 2, 3, 5, 6], "description": "Tensor sample description", "tags": ["#y", "#label-sample"], "pointable": True, }, } } msg = GetTensorsResponse( address=target, status_code=200, content=request_content, ) blob = serialize(msg) msg2 = sy.deserialize(blob=blob) assert msg.id == msg2.id assert msg.address == target assert msg.content == msg2.content assert msg == msg2
def test_update_role_message_serde(node: sy.VirtualMachine) -> None: target = Address(name="Alice") content = { "role_id": "9a4f9dasd6", "name": "Role Sample", "can_triage_results": True, "can_edit_settings": False, "can_create_users": False, "can_edit_roles": False, "can_manage_infrastructure": True, } msg = UpdateRoleMessage( address=target, content=content, reply_to=node.address, ) blob = serialize(msg) msg2 = sy.deserialize(blob=blob) assert msg.id == msg2.id assert msg.address == target assert msg.content == msg2.content assert msg == msg2
def test_get_all_data_requests_response_serde() -> None: target = Address(name="Alice") request_content = { "workers": { "626sadaf631": { "dataset-id": "68a465aer3adf", "user-id": "user-id7", "request-type": "read", }, "a84ew64wq6e": { "dataset-id": "98w4e54a6d", "user-id": "user-id9", "request-type": "write", }, } } msg = GetRequestsResponse( address=target, status_code=200, content=request_content, ) blob = sy.serialize(msg) msg2 = sy.deserialize(blob=blob) assert msg.id == msg2.id assert msg.address == target assert msg.content == msg2.content assert msg == msg2
def test_delete_worker_response_serde() -> None: target = Address(name="Alice") content = { "settings": { "cloud-admin-token": "d84we35ad3a1d59a84sd9", "cloud-credentials": "<cloud-credentials.pem>", "infra": { "autoscaling": True, "triggers": { "memory": "50", "vCPU": "80" } }, } } msg = GetSetUpResponse( status_code=200, address=target, content=content, ) blob = serialize(msg) msg2 = sy.deserialize(blob=blob) assert msg.id == msg2.id assert msg.address == target assert msg.content == msg2.content assert msg == msg2
def test_create_initial_setup_message_serde(node: sy.VirtualMachine) -> None: target = Address(name="Alice") request_content = { "settings": { "cloud-admin-token": "d84we35ad3a1d59a84sd9", "cloud-credentials": "<cloud-credentials.pem>", "infra": { "autoscaling": True, "triggers": { "memory": "50", "vCPU": "80" } }, } } msg = CreateInitialSetUpMessage( address=target, content=request_content, reply_to=node.address, ) blob = serialize(msg) msg2 = sy.deserialize(blob=blob) assert msg.id == msg2.id assert msg.address == target assert msg.content == msg2.content assert msg == msg2
def test_get_role_response_serde() -> None: target = Address(name="Alice") content = { "name": "Role Sample", "can_triage_results": True, "can_edit_settings": False, "can_create_users": False, "can_edit_roles": False, "can_manage_infrastructure": True, } msg = GetRoleResponse( address=target, status_code=200, content=content, ) blob = serialize(msg) msg2 = sy.deserialize(blob=blob) assert msg.id == msg2.id assert msg.address == target assert msg.content == msg2.content assert msg == msg2
def test_get_all_workers_response_serde() -> None: target = Address(name="Alice") request_content = { "workers": { "626sadaf631": { "environment-name": "Heart Diseases Environment", "owner": "user-id7", "deployment-date": "05/12/2021", }, "a84ew64wq6e": { "worker-id": "eqw9e4a5d846", "environment-name": "Brain Diseases Environment", "owner": "user-id8", "deployment-date": "15/12/2021", }, } } msg = GetWorkersResponse( address=target, status_code=200, content=request_content, ) blob = serialize(msg) msg2 = sy.deserialize(blob=blob) assert msg.id == msg2.id assert msg.address == target assert msg.content == msg2.content assert msg == msg2
def test_get_dataset_response_serde() -> None: target = Address(name="Alice") content = { "dataset": ["<tensor_id>", "<tensor_id>", "<tensor_id>", "<tensor_id>"], "description": "Dataset Description", "tags": ["#x", "#data-sample"], "pointable": True, "read-permission": ["user-id1", "user-id2", "user-id3"], "write-permission": ["user-id1", "user-id5", "user-id9"], } msg = GetDatasetResponse( address=target, status_code=200, content=content, ) blob = serialize(msg) msg2 = sy.deserialize(blob=blob) assert msg.id == msg2.id assert msg.address == target assert msg.content == msg2.content assert msg == msg2
def test_create_dataset_message_serde(node: sy.VirtualMachine) -> None: target = Address(name="Alice") request_content = { "dataset": ["<tensor_id>", "<tensor_id>", "<tensor_id>", "<tensor_id>"], "description": "Dataset Description", "tags": ["#x", "#data-sample"], "pointable": True, "read-permission": ["user-id1", "user-id2", "user-id3"], "write-permission": ["user-id1", "user-id5", "user-id9"], } msg = CreateDatasetMessage( address=target, content=request_content, reply_to=node.address, ) blob = serialize(msg) msg2 = sy.deserialize(blob=blob) assert msg.id == msg2.id assert msg.address == target assert msg.content == msg2.content assert msg == msg2
def test_signaling_answer_message_serde() -> None: bob_vm = sy.VirtualMachine(name="Bob") target = Address(name="Alice") target_id = secrets.token_hex(nbytes=16) host_id = secrets.token_hex(nbytes=16) msg = SignalingAnswerMessage( address=target, payload="SDP", host_metadata=bob_vm.get_metadata_for_client(), target_peer=target_id, host_peer=host_id, ) msg_metadata = bob_vm.get_metadata_for_client() blob = msg.serialize() msg2 = sy.deserialize(blob=blob) msg2_metadata = msg2.host_metadata assert msg.id == msg2.id assert msg.address == target assert msg.payload == msg2.payload assert msg2.payload == "SDP" assert msg2.host_peer == host_id assert msg2.target_peer == target_id assert msg == msg2 assert msg_metadata.name == msg2_metadata.name assert msg_metadata.node == msg2_metadata.node assert msg_metadata.id == msg2_metadata.id
def test_default_serialization_and_deserialization() -> None: """Tests that default Address serde works as expected - to Protobuf""" an_id = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) obj = Address( network=SpecificLocation(id=an_id), domain=SpecificLocation(id=an_id), device=SpecificLocation(id=an_id), vm=SpecificLocation(id=an_id), ) blob = obj.to_proto() assert obj.serialize() == blob assert obj == sy.deserialize(blob=blob)
def test_update_worker_message_serde(node: sy.VirtualMachine) -> None: target = Address(name="Alice") content = { "worker-id": "eqw9e4a5d846", "settings": { "instance-size": "t4g.large", "vCPU": "2", "network-bandwith": "5Gbps", "vGPU": True, }, } msg = UpdateWorkerMessage( address=target, content=content, reply_to=node.address, ) blob = serialize(msg) msg2 = sy.deserialize(blob=blob) assert msg.id == msg2.id assert msg.address == target assert msg.content == msg2.content assert msg == msg2
def test_network_getter_and_setter() -> None: """Unit tests for Address.network getter and setter""" an_id = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) # Test getter network = SpecificLocation(id=an_id) address = Address( network=network, domain=SpecificLocation(id=an_id), device=SpecificLocation(id=an_id), vm=SpecificLocation(id=an_id), ) assert address.network == network # Test setter new_network = SpecificLocation(id=an_id) address.network = new_network assert address.network == new_network
def _construct_address() -> Address: """Helper method to construct an Address""" return Address( network=SpecificLocation(id=UID()), domain=SpecificLocation(id=UID()), device=SpecificLocation(id=UID()), vm=SpecificLocation(id=UID()), )
def test_init_with_specific_id(address_kwargs: dict, expected_values: dict) -> None: """Test that Address will use the SpecificLocation you pass into the constructor""" address = Address(**address_kwargs) assert address.network is expected_values["network"] assert address.domain is expected_values["domain"] assert address.device is expected_values["device"] assert address.vm is expected_values["vm"]
def test_network_id_property_method() -> None: """Unit test for Address.network_id method""" an_id = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) # Test getter address_with_network = Address( network=SpecificLocation(id=an_id), domain=SpecificLocation(id=an_id), device=SpecificLocation(id=an_id), vm=SpecificLocation(id=an_id), ) address_without_network = Address( domain=SpecificLocation(id=an_id), device=SpecificLocation(id=an_id), vm=SpecificLocation(id=an_id), ) assert address_with_network.network_id == an_id assert address_without_network.network_id is None
def test_to_string() -> None: """Tests that SpecificLocation generates an intuitive string.""" an_id = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) obj = Address( network=SpecificLocation(id=an_id), domain=SpecificLocation(id=an_id), device=SpecificLocation(id=an_id), vm=SpecificLocation(id=an_id), ) str_out = ( "<Address - Network:<SpecificLocation:..a1514>, Domain:<Specific" "Location:..a1514> Device:<SpecificLocation:..a1514>, VM:<Specific" "Location:..a1514>") assert str(obj) == str_out assert obj.__repr__() == str_out
def test_target_id_property_method_with_a_return() -> None: """Unit test for Address.target_id method""" network = SpecificLocation(id=UID()) domain = SpecificLocation(id=UID()) device = SpecificLocation(id=UID()) vm = SpecificLocation(id=UID()) address = Address( network=network, domain=domain, device=device, vm=vm, ) assert address.target_id == vm address.vm = None assert address.target_id == device address.device = None assert address.target_id == domain address.domain = None assert address.target_id == network
def test_plan_execution(client: sy.VirtualMachineClient) -> None: tensor_pointer1 = th.tensor([1, 2, 3]).send(client) tensor_pointer2 = th.tensor([4, 5, 6]).send(client) tensor_pointer3 = th.tensor([7, 8, 9]).send(client) result_tensor_pointer1 = th.tensor([0, 0, 0]).send(client) result_tensor_pointer2 = th.tensor([0, 0, 0]).send(client) result1_uid = result_tensor_pointer1.id_at_location result2_uid = result_tensor_pointer2.id_at_location a1 = RunClassMethodAction( path="torch.Tensor.add", _self=tensor_pointer1, args=[tensor_pointer2], kwargs={}, id_at_location=result1_uid, address=Address(), msg_id=UID(), ) a2 = RunClassMethodAction( path="torch.Tensor.add", _self=result_tensor_pointer1, args=[tensor_pointer3], kwargs={}, id_at_location=result2_uid, address=Address(), msg_id=UID(), ) plan = Plan([a1, a2]) plan_pointer = plan.send(client) plan_pointer() expected_tensor1 = th.tensor([5, 7, 9]) expected_tensor2 = th.tensor([12, 15, 18]) assert all(expected_tensor1 == result_tensor_pointer1.get()) assert all(expected_tensor2 == result_tensor_pointer2.get())
def test_partial_serialization_and_deserialization() -> None: """Test that addresses with only some attributes serialize and deserialize correctly/""" an_id = UID(value=uuid.UUID(int=333779996850170035686993356951732753684)) obj = Address( # network=SpecificLocation(id=an_id), domain=SpecificLocation(id=an_id), device=SpecificLocation(id=an_id), vm=SpecificLocation(id=an_id), ) assert obj == sy.deserialize(blob=sy.serialize(obj)) obj = Address( network=SpecificLocation(id=an_id), # domain=SpecificLocation(id=an_id), device=SpecificLocation(id=an_id), vm=SpecificLocation(id=an_id), ) blob = sy.serialize(obj, to_proto=True) assert obj == sy.deserialize(blob=blob) obj = Address( network=SpecificLocation(id=an_id), domain=SpecificLocation(id=an_id), # device=SpecificLocation(id=an_id), vm=SpecificLocation(id=an_id), ) blob = sy.serialize(obj, to_proto=True) assert obj == sy.deserialize(blob=blob) obj = Address( network=SpecificLocation(id=an_id), domain=SpecificLocation(id=an_id), device=SpecificLocation(id=an_id), # vm=SpecificLocation(id=an_id) ) blob = sy.serialize(obj, to_proto=True) assert obj == sy.deserialize(blob=blob)
def test_fail_process_request_service() -> None: addr = Address() request = RequestMessage( object_id=UID(), address=addr, requester_verify_key=get_verify_key(), owner_address=addr, ) with raises(Exception, match="No way to dispatch Deny Message."): request.deny()
def test_fail_accept_request_message() -> None: addr = Address() request = RequestMessage( object_id=UID(), address=addr, requester_verify_key=get_verify_key(), owner_address=addr, ) with raises(Exception, match="No way to dispatch Accept Message."): request.accept()
def test_request_answer_message() -> None: addr = Address() msg = RequestAnswerMessage(request_id=UID(), address=addr, reply_to=addr) serialized = serialize(obj=msg) new_msg = deserialize(blob=serialized) assert msg.request_id == new_msg.request_id assert msg.address == new_msg.address assert msg.reply_to == new_msg.reply_to