def test_device_plus_parent(mocker): share = mocker.patch( "lava_dispatcher_host.share_device_with_container_lxc") add_device_container_mapping( "1", { "serial_number": "1234567890", "vendor_id": None, "product_id": None, "fs_label": None, }, "mycontainer1", ) add_device_container_mapping( "1", { "serial_number": "", "vendor_id": "1234", "product_id": "3456", "fs_label": None, }, "mycontainer2", ) share_device_with_container( Namespace(device="/dev/foo/bar", serial_number="1234567890")) share.assert_called_once_with("mycontainer1", "/dev/foo/bar") share.reset_mock() share_device_with_container( Namespace(device="/dev/foo/bar", vendor_id="1234", product_id="3456")) share.assert_called_once_with("mycontainer2", "/dev/foo/bar") share.reset_mock()
def test_only_adds_slash_dev_if_needed(mocker): share = mocker.patch("lava_dispatcher_host.share_device_with_container_lxc") add_device_container_mapping("1", {"serial_number": "1234567890"}, "mycontainer") share_device_with_container( Namespace(device="/dev/foo/bar", serial_number="1234567890") ) share.assert_called_once_with("mycontainer", "/dev/foo/bar")
def test_device_missing(mocker): mocker.patch("os.path.exists", return_value=False) check_call = mocker.patch("subprocess.check_call") add_device_container_mapping("1", {"serial_number": "1234567890"}, "mycontainer") share_device_with_container( Namespace(device="foo/bar", serial_number="1234567890")) check_call.assert_not_called()
def test_share_device_setup_logger(mocker): check_call = mocker.patch("subprocess.check_call") add_device_container_mapping("1", {"serial_number": "1234567890"}, "mycontainer") setup_logger = mocker.Mock() share_device_with_container( Namespace(device="foo", serial_number="1234567890"), setup_logger) setup_logger.assert_called_once_with(mocker.ANY)
def test_simple_share_device_with_container(mocker, pass_device_lxc, device_links): add_device_container_mapping("1", {"serial_number": "1234567890"}, "mycontainer") share_device_with_container( Namespace(device="foo/bar", serial_number="1234567890")) pass_device_lxc.assert_called_once_with("mycontainer", "/dev/foo/bar", device_links, "1")
def test_second_mapping_does_not_invalidate_first(mocker): share = mocker.patch("lava_dispatcher_host.share_device_with_container_lxc") add_device_container_mapping("1", {"serial_number": "1234567890"}, "mycontainer1") add_device_container_mapping("1", {"serial_number": "badbeeb00c"}, "mycontainer1") share_device_with_container( Namespace(device="/dev/foo/bar", serial_number="1234567890") ) share.assert_called_once_with("mycontainer1", "/dev/foo/bar")
def test_simple_share_device_with_container(mocker): check_call = mocker.patch("subprocess.check_call") add_device_container_mapping("1", {"serial_number": "1234567890"}, "mycontainer") share_device_with_container( Namespace(device="foo/bar", serial_number="1234567890")) check_call.assert_called_once_with( ["lxc-device", "-n", "mycontainer", "add", "/dev/foo/bar"])
def test_unknown_container_type(mocker): add_device_container_mapping( "1", {"serial_number": "1234567890"}, "mycontainer", container_type="unsupported", ) with pytest.raises(InfrastructureError): share_device_with_container( Namespace(device="foo/bar", serial_number="1234567890"))
def test_two_concurrent_jobs(mocker, pass_device_lxc, device_links): add_device_container_mapping("1", {"serial_number": "1234567890"}, "container1") add_device_container_mapping("2", {"serial_number": "9876543210"}, "container2") share_device_with_container( Namespace(device="baz/qux", serial_number="9876543210")) pass_device_lxc.assert_called_once_with("container2", "/dev/baz/qux", device_links, "2")
def test_two_concurrent_jobs(mocker): check_call = mocker.patch("subprocess.check_call") add_device_container_mapping("1", {"serial_number": "1234567890"}, "container1") add_device_container_mapping("2", {"serial_number": "9876543210"}, "container2") share_device_with_container( Namespace(device="baz/qux", serial_number="9876543210")) check_call.assert_called_once_with( ["lxc-device", "-n", "container2", "add", "/dev/baz/qux"])
def test_share_device_setup_logger(mocker): check_call = mocker.patch("subprocess.check_call") add_device_container_mapping( "1", {"serial_number": "1234567890"}, "mycontainer", logging_info={"logging_url": "proto://host"}, ) setup_logger = mocker.Mock() share_device_with_container( Namespace(device="foo", serial_number="1234567890"), setup_logger) setup_logger.assert_called()
def test_two_devices_two_containers(mocker): share = mocker.patch("lava_dispatcher_host.share_device_with_container_lxc") add_device_container_mapping("1", {"serial_number": "1234567890"}, "mycontainer1") add_device_container_mapping("1", {"serial_number": "badbeeb00c"}, "mycontainer2") share_device_with_container( Namespace(device="/dev/foo/bar", serial_number="1234567890") ) share.assert_called_once_with("mycontainer1", "/dev/foo/bar") share.reset_mock() share_device_with_container( Namespace(device="/dev/foo/bar", serial_number="badbeeb00c") ) share.assert_called_once_with("mycontainer2", "/dev/foo/bar")
def test_map_by_vendor_id_and_product_id(mocker, pass_device_lxc, device_links): add_device_container_mapping( "1", {"vendor_id": "aaaa", "product_id": "xxxx"}, "container1" ) share_device_with_container( Namespace( device="bus/usb/001/099", serial_number="9876543210", vendor_id="aaaa", product_id="xxxx", ) ) pass_device_lxc.assert_called_once_with( "container1", "/dev/bus/usb/001/099", device_links )
def test_map_by_vendor_id_and_product_id(mocker): check_call = mocker.patch("subprocess.check_call") add_device_container_mapping("1", { "vendor_id": "aaaa", "product_id": "xxxx" }, "container1") share_device_with_container( Namespace( device="bus/usb/001/099", serial_number="9876543210", vendor_id="aaaa", product_id="xxxx", )) check_call.assert_called_once_with( ["lxc-device", "-n", "container1", "add", "/dev/bus/usb/001/099"])
def handle_devices_share(options): if options.remote: client = Client() data = vars(options) request = {} for f in [ "device", "serial_number", "usb_vendor_id", "usb_product_id", "fs_label", ]: if f in data: request[f] = data[f] client.send_request(request) else: share_device_with_container(options)
def test_mapping_with_serial_number_but_called_with_vendor_product_id_too( mocker, pass_device_lxc, device_links): add_device_container_mapping( "1", { "serial_number": "1234567890", "vendor_id": None, "product_id": None, "fs_label": None, }, "mycontainer", ) share_device_with_container( Namespace( device="foo/bar", serial_number="1234567890", vendor_id="0123", product_id="4567", )) pass_device_lxc.assert_called_once_with("mycontainer", "/dev/foo/bar", device_links, "1")
def test_mapping_with_serial_number_but_called_with_vendor_product_id_too( mocker): check_call = mocker.patch("subprocess.check_call") add_device_container_mapping( "1", { "serial_number": "1234567890", "vendor_id": None, "product_id": None, "fs_label": None, }, "mycontainer", ) share_device_with_container( Namespace( device="foo/bar", serial_number="1234567890", vendor_id="0123", product_id="4567", )) check_call.assert_called_once_with( ["lxc-device", "-n", "mycontainer", "add", "/dev/foo/bar"])
def handle_devices_share(options): share_device_with_container(options)
def test_no_device_found(mocker): check_call = mocker.patch("subprocess.check_call") share_device_with_container( Namespace(device="bus/usb/001/099", serial_number="9876543210")) check_call.assert_not_called()
def handle_devices_share(options): share_device_with_container(options, setup_logger) finish_logger()
def handle(self, command: ShareCommand): share_device_with_container(command.options)