def test_open_tcp(tmpdir, url_template, host, export): image = str(tmpdir.join("image")) with open(image, "wb") as f: f.truncate(1024**3) sock = nbd.TCPAddress(host, testutil.random_tcp_port()) url = url_template.format(port=sock.port) log.debug("Trying url=%r export=%r", url, export) with qemu_nbd.run(image, "raw", sock, export_name=export): with nbd.open(urlparse(url)) as c: assert c.export_size == 1024**3
def test_run_tcp(tmpfile): with io.open(tmpfile, "r+b") as f: f.truncate(1024**2) addr = nbd.TCPAddress("localhost", testutil.random_tcp_port()) with qemu_nbd.run(tmpfile, "raw", addr): # The helper already waited for the NBD socket, not wait needed. assert sockutil.wait_for_socket(addr, 0.0) # The socket must be closed, no wait needed. assert not sockutil.wait_for_socket(addr, 0.0)
def test_get_nbd_backend(tmpdir, cfg, nbd_server, transport): if transport == "unix": nbd_server.sock = nbd.UnixAddress(tmpdir.join("sock")) else: nbd_server.sock = nbd.TCPAddress("localhost", testutil.random_tcp_port()) nbd_server.start() ticket = auth.Ticket( testutil.create_ticket(url=urlunparse(nbd_server.url))) req = Request() b = backends.get(req, ticket, cfg).backend assert b.name == "nbd"
def test_full_backup(tmpdir, fmt, transport): disk_size = 1024**2 disk_part = disk_size // 4 disk = str(tmpdir.join("disk." + fmt)) backup_disk = str(tmpdir.join("backup.raw")) # Create disk qemu_img.create(disk, fmt, size=disk_size) # Pupulate disk with data. with qemu_nbd.open(disk, fmt) as d: for i in range(0, disk_size, disk_part): data = b"%d\n" % i d.write(i, data.ljust(512)) d.flush() if transport == "unix": nbd_sock = nbd.UnixAddress(tmpdir.join("nbd.sock")) else: nbd_sock = nbd.TCPAddress("localhost", testutil.random_tcp_port()) # Backup using qemu-img convert. with backup.full_backup(tmpdir, disk, fmt, nbd_sock): log.debug("Backing up image with qemu-img") qemu_img.convert(nbd_sock.url("sda"), backup_disk, src_fmt="raw", dst_fmt="raw", progress=True) # Compare source and backup disks. with qemu_nbd.open(disk, fmt, read_only=True) as d, \ io.open(backup_disk, "rb") as b: for i in range(0, disk_size, disk_part): b.seek(i) assert d.read(i, 512) == b.read(512)
from ovirt_imageio._internal import nbd from ovirt_imageio._internal import nbdutil from ovirt_imageio._internal import qemu_img from ovirt_imageio._internal import qemu_nbd from ovirt_imageio._internal import sockutil from . import testutil from . marks import flaky_in_ovirt_ci @pytest.mark.parametrize("addr,export,url", [ (nbd.UnixAddress("/sock"), None, "nbd:unix:/sock"), (nbd.UnixAddress("/sock"), "", "nbd:unix:/sock"), (nbd.UnixAddress("/sock"), "sda", "nbd:unix:/sock:exportname=sda"), (nbd.TCPAddress("host", 10900), None, "nbd:host:10900"), (nbd.TCPAddress("host", 10900), "", "nbd:host:10900"), (nbd.TCPAddress("host", 10900), "sdb", "nbd:host:10900:exportname=sdb"), ]) def test_server_url(addr, export, url): srv = qemu_nbd.Server("image", "raw", addr, export_name=export) assert srv.url == urllib.parse.urlparse(url) @pytest.mark.parametrize("fmt", ["raw", "qcow2"]) def test_open(tmpdir, fmt): disk = str(tmpdir.join("disk." + fmt)) qemu_img.create(disk, fmt, size=1024**2) offset = 64 * 1024 data = b"it works"
def test_tcp_address_invalid(host, port): with pytest.raises(ValueError): nbd.TCPAddress(host, port)
def nbd_sock(request, tmpdir): if request.param == "unix": return nbd.UnixAddress(tmpdir.join("sock")) else: return nbd.TCPAddress("localhost", testutil.random_tcp_port())