def test_options(tmpdir, fmt, cache, aio, discard): size = 4 * 1024**2 chunk_size = 128 * 1024 src = str(tmpdir.join("src." + fmt)) qemu_img.create(src, fmt, size=size) with qemu_nbd.open(src, fmt) as c: for offset in range(0, size, chunk_size): c.write(offset, struct.pack(">Q", offset)) c.flush() dst = str(tmpdir.join("dst." + fmt)) qemu_img.create(dst, fmt, size=size) src_addr = nbd.UnixAddress(str(tmpdir.join("src.sock"))) dst_addr = nbd.UnixAddress(str(tmpdir.join("dst.sock"))) with qemu_nbd.run( src, fmt, src_addr, read_only=True, cache=cache, aio=aio, discard=discard), \ qemu_nbd.run( dst, fmt, dst_addr, cache=cache, aio=aio, discard=discard), \ nbd.Client(src_addr) as src_client, \ nbd.Client(dst_addr) as dst_client: nbdutil.copy(src_client, dst_client) qemu_img.compare(src, dst)
def test_copy_nbd_to_nbd(tmpdir, src_fmt, dst_fmt, zero): # Make sure we have zero extents larger than MAX_ZERO_SIZE (1 GiB). It # would be nice to have also data extents larger than MAX_COPY_SIZE (128 # MiB), but this is too slow for automated tests. size = 2 * io.MAX_ZERO_SIZE # Default cluser size with qcow2 format. cluster_size = 64 * 1024 src = str(tmpdir.join("src." + src_fmt)) qemu_img.create(src, src_fmt, size=size) with qemu_nbd.open(src, src_fmt) as c: # Create first data extent. c.write(0, b"data extent 1\n") # Between the data extents we have a zero extent bigger than # io.MAX_ZERO_SIZE. # Create data extent larger than io.BUFFER_SIZE. data = b"data extent 2\n" + b"x" * io.BUFFER_SIZE c.write(io.MAX_ZERO_SIZE + 2 * cluster_size, data) # Between the data extents we have a zero extent smaller than # io.MAX_ZERO_SIZE. # Create last data extent at the end of the image. c.write(size - 4096, b"data extent 3\n") c.flush() src_sock = UnixAddress(tmpdir.join("src.sock")) src_url = urlparse(src_sock.url()) dst = str(tmpdir.join("dst." + dst_fmt)) qemu_img.create(dst, dst_fmt, size=size) dst_sock = UnixAddress(tmpdir.join("dst.sock")) dst_url = urlparse(dst_sock.url()) with qemu_nbd.run(src, src_fmt, src_sock, read_only=True), \ qemu_nbd.run(dst, dst_fmt, dst_sock), \ nbd.open(src_url, "r") as src_backend, \ nbd.open(dst_url, "r+") as dst_backend: # Because we copy to new image, we can alays use zero=False, but we # test both to verify that the result is the same. io.copy(src_backend, dst_backend, zero=zero) qemu_img.compare(src, dst)
def test_shared(tmpdir, fmt): size = 1024**2 chunk_size = size // 2 src = str(tmpdir.join("src." + fmt)) qemu_img.create(src, fmt, size=size) with qemu_nbd.open(src, fmt) as c: c.write(0, b"a" * chunk_size) c.write(0, b"b" * chunk_size) c.flush() dst = str(tmpdir.join("dst." + fmt)) qemu_img.create(dst, fmt, size=size) src_addr = nbd.UnixAddress(str(tmpdir.join("src.sock"))) dst_addr = nbd.UnixAddress(str(tmpdir.join("dst.sock"))) # Start 2 qemu-nbd servers, each with 2 clients that can read and write in # parallel for higher throughput. with qemu_nbd.run(src, fmt, src_addr, read_only=True, shared=2), \ qemu_nbd.run(dst, fmt, dst_addr, shared=2), \ nbd.Client(src_addr) as src_client1, \ nbd.Client(src_addr) as src_client2, \ nbd.Client(dst_addr) as dst_client1, \ nbd.Client(dst_addr) as dst_client2: # Copy first half of the image with src_client1 and dst_client2 and # second half with src_client2 and dst_client2. In a real application # we would have more clients, running in multiple threads. chunk1 = src_client1.read(0, chunk_size) dst_client1.write(0, chunk1) chunk2 = src_client2.read(chunk_size, chunk_size) dst_client2.write(chunk_size, chunk2) dst_client1.flush() dst_client2.flush() qemu_img.compare(src, dst)
def test_open_tcp(tmpdir, url_template, export): image = str(tmpdir.join("image")) with open(image, "wb") as f: f.truncate(1024**3) sock = nbd.TCPAddress("localhost", 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_open_unix(tmpdir, url, export): image = str(tmpdir.join("image")) with open(image, "wb") as f: f.truncate(1024**3) sock = nbd.UnixAddress(tmpdir.join("sock")) url = url.replace("/path", sock) 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 nbdutil.wait_for_socket(addr, 0.0) # The socket must be closed, no wait needed. assert not nbdutil.wait_for_socket(addr, 0.0)
def test_qcow2_write_read(tmpdir): image = str(tmpdir.join("image")) sock = nbd.UnixAddress(tmpdir.join("sock")) offset = 1024**2 data = b"can read and write qcow2" create_image(image, "qcow2", 1024**3) with qemu_nbd.run(image, "qcow2", sock): with nbd.Client(sock) as c: c.write(offset, data) c.flush() with nbd.Client(sock) as c: assert c.read(offset, len(data)) == data
def test_raw_read(tmpdir): image = str(tmpdir.join("image")) sock = nbd.UnixAddress(tmpdir.join("sock")) offset = 1024**2 data = b"can read from raw" with open(image, "wb") as f: f.truncate(1024**3) f.seek(offset) f.write(data) with qemu_nbd.run(image, "raw", sock): with nbd.Client(sock) as c: assert c.read(offset, len(data)) == data
def test_run_unix(tmpdir): image = str(tmpdir.join("image")) sock = str(tmpdir.join("sock")) with io.open(image, "wb") as f: f.truncate(1024**2) addr = nbd.UnixAddress(sock) with qemu_nbd.run(image, "raw", addr): # The helper already waited for the NBD socket, not wait needed. assert nbdutil.wait_for_socket(addr, 0.0) # The socket must be closed, no wait needed. assert not nbdutil.wait_for_socket(addr, 0.0)
def test_raw_write(tmpdir): image = str(tmpdir.join("image")) with open(image, "wb") as f: f.truncate(1024**3) sock = nbd.UnixAddress(tmpdir.join("sock")) offset = 1024**2 data = b"can write to raw" with qemu_nbd.run(image, "raw", sock): with nbd.Client(sock) as c: c.write(offset, data) c.flush() with open(image, "rb") as f: f.seek(offset) assert f.read(len(data)) == data
def test_handshake(tmpdir, export, fmt): image = str(tmpdir.join("image")) create_image(image, fmt, 1024**3) sock = nbd.UnixAddress(tmpdir.join("sock")) with qemu_nbd.run(image, fmt, sock, export_name=export): if export: c = nbd.Client(sock, export) else: c = nbd.Client(sock) with c: # TODO: test transmission_flags? assert c.export_size == 1024**3 assert c.minimum_block_size == 1 assert c.preferred_block_size == 4096 assert c.maximum_block_size == 32 * 1024**2 assert c.base_allocation
def test_zero_min_block_size(tmpdir, fmt): offset = 1024**2 image = str(tmpdir.join("image")) sock = nbd.UnixAddress(tmpdir.join("sock")) create_image(image, fmt, 1024**3) with qemu_nbd.run(image, fmt, sock): # Fill range with data with nbd.Client(sock) as c: size = c.minimum_block_size c.write(offset, b"x" * size) c.flush() # Zero range using minimum block size with nbd.Client(sock) as c: c.zero(offset, size) c.flush() with nbd.Client(sock) as c: assert c.read(offset, size) == b"\0" * size
def test_zero(tmpdir, fmt): size = 2 * 1024**2 offset = 1024**2 image = str(tmpdir.join("image")) sock = nbd.UnixAddress(tmpdir.join("sock")) create_image(image, fmt, size) with qemu_nbd.run(image, fmt, sock): # Fill image with data with nbd.Client(sock) as c: c.write(0, b"x" * size) c.flush() # Zero a range with nbd.Client(sock) as c: c.zero(offset, 4096) c.flush() with nbd.Client(sock) as c: assert c.read(offset, 4096) == b"\0" * 4096