def test_extents_zero(nbd_server, user_file, fmt): size = 6 * 1024**3 qemu_img.create(user_file.path, fmt, size=size) nbd_server.image = user_file.path nbd_server.fmt = fmt nbd_server.start() with nbd.open(nbd_server.url, "r+") as b: # qcow2 extents resolution is cluster size. data = b"x" * 64 * 1024 b.write(data) # The second extent length is bigger than NBD maximum length, testing # that our extent length is not limited by NBD limits. The backend # sends multiple block status commands and merge the returned extents. b.seek(5 * 1024**3) b.write(data) # Holes can be reported only for qcow2 images. hole = fmt == "qcow2" assert list(b.extents()) == [ extent.ZeroExtent(0, len(data), False, False), extent.ZeroExtent(len(data), 5 * 1024**3 - len(data), True, hole), extent.ZeroExtent(5 * 1024**3, len(data), False, False), extent.ZeroExtent(5 * 1024**3 + len(data), 1024**3 - len(data), True, hole), ]
def test_daemon_extents_zero(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server) chunk_size = len(handler.image) // 2 handler.extents["zero"] = [ { "start": 0, "length": chunk_size, "zero": False, "hole": False, }, { "start": chunk_size, "length": chunk_size, "zero": True, "hole": False, }, ] with Backend(http_server.url, http_server.cafile) as b: # Zero extents are available. assert list(b.extents()) == [ extent.ZeroExtent(0, chunk_size, False, False), extent.ZeroExtent(chunk_size, chunk_size, True, False), ] # Dirty extents are not available. with pytest.raises(errors.UnsupportedOperation): list(b.extents("dirty"))
def create_zero_extents(fmt): """ Create zero extents from format string. "A0-" -> [ ZeroExtent(0 * CHUNK_SIZE, CHUNK_SIZE, False, False), ZeroExtent(1 * CHUNK_SIZE, CHUNK_SIZE, True, False), ZeroExtent(2 * CHUNK_SIZE, CHUNK_SIZE, True, True), ] """ extents = [] offset = 0 for c in fmt: if c == "0": extents.append( extent.ZeroExtent(offset, CHUNK_SIZE, zero=True, hole=False)) elif c == "-": extents.append( extent.ZeroExtent(offset, CHUNK_SIZE, zero=True, hole=True)) else: extents.append( extent.ZeroExtent(offset, CHUNK_SIZE, zero=False, hole=False)) offset += CHUNK_SIZE return extents
def test_daemon_extents_dirty(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server) chunk_size = len(handler.image) // 2 handler.extents["dirty"] = [ { "start": 0, "length": chunk_size, "dirty": True, "hole": False, }, { "start": chunk_size, "length": chunk_size, "dirty": False, "hole": False, }, ] with Backend(http_server.url, http_server.cafile) as b: # Both "zero" and "dirty" extents are available. assert list(b.extents("zero")) == [ extent.ZeroExtent(0, b.size(), True, False), ] assert list(b.extents("dirty")) == [ extent.DirtyExtent(0, chunk_size, True, False), extent.DirtyExtent(chunk_size, chunk_size, False, False), ]
def test_user_extents(): extents = { "zero": [ extent.ZeroExtent(0, 32, False, False), extent.ZeroExtent(0, 32, True, False), ], "dirty": [ extent.DirtyExtent(0, 16, True, False), extent.DirtyExtent(0, 48, False, False), ] } data = b"a" * 32 + b"\0" * 32 m = memory.Backend(data=data, extents=extents) assert list(m.extents()) == extents["zero"] assert list(m.extents("zero")) == extents["zero"] assert list(m.extents("dirty")) == extents["dirty"]
def test_extents(user_file): size = user_file.sector_size * 2 with io.open(user_file.path, "wb") as f: f.truncate(size) with file.open(user_file.url, "r+", sparse=True) as f: # We support detecting extents now; empty file reports one data # extents. assert list(f.extents()) == [extent.ZeroExtent(0, size, False, False)]
def test_daemon_open(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server) with Backend(http_server.url, http_server.cafile) as b: assert b.server_address == uhttp_server.server_address assert b.tell() == 0 assert b.size() == len(handler.image) # Client reports server extents (all zero). assert list(b.extents()) == [ extent.ZeroExtent(0, b.size(), True, False) ]
def test_daemon_no_extents_open(http_server, uhttp_server): handler = Daemon(http_server, uhttp_server, extents=False) with Backend(http_server.url, http_server.cafile) as b: assert b.server_address == uhttp_server.server_address assert b.tell() == 0 assert b.size() == len(handler.image) # Client emulates extents (all non-zero). assert list(b.extents()) == [ extent.ZeroExtent(0, b.size(), False, False) ]
def test_old_proxy_open(http_server): handler = OldProxy(http_server) with Backend(http_server.url, http_server.cafile) as b: assert b.server_address == http_server.server_address assert b.tell() == 0 assert b.size() == len(handler.image) # Client emulates extents (all non-zero). assert list(b.extents()) == [ extent.ZeroExtent(0, b.size(), False, False) ]
def extents(self, ctx="zero"): return [extent.ZeroExtent(0, self.size(), False, False)]
def test_extents(): m = memory.Backend(data=bytearray(b"data")) assert list(m.extents()) == [extent.ZeroExtent(0, 4, False, False)]