def test_nbd_backend(srv, client, tmpdir, nbd_server, fmt, compressed): size = 2 * 1024**2 # Create temporary file with some data. tmp = tmpdir.join("tmp") with open(tmp, "wb") as f: f.truncate(size) # Add zero allocated cluster. f.write(b"\0" * 64 * 1024) # Add cluster with data. f.seek(1 * 1024**2) f.write(b"some data") # NBD backend checksums guest visible data. checksum = blkhash.checksum(tmp) # Create test image. qemu_img.convert(tmp, nbd_server.image, "raw", fmt, compressed=compressed) nbd_server.fmt = fmt nbd_server.start() ticket = testutil.create_ticket(url=nbd_server.sock.url(), size=size) srv.auth.add(ticket) res = client.request("GET", "/images/{}/checksum".format(ticket["uuid"])) data = res.read() assert res.status == 200 res = json.loads(data) assert res == checksum
def test_checksum_algorithm(tmpdir, algorithm, digest_size): img = str(tmpdir.join("img")) qemu_img.create(img, "raw", size="2m") expected = blkhash.checksum( img, block_size=1024**2, algorithm=algorithm, digest_size=digest_size) actual = client.checksum(img, block_size=1024**2, algorithm=algorithm) assert actual == expected
def test_checksum(tmpdir, fmt, compressed): # Create temporary file with some data. size = 2 * 1024**2 tmp = str(tmpdir.join("tmp")) with open(tmp, "wb") as f: f.truncate(size) f.write(b"x" * CLUSTER_SIZE) # Create test image from temporary file. img = str(tmpdir.join("img")) qemu_img.convert(tmp, img, "raw", fmt, compressed=compressed) expected = blkhash.checksum(tmp, block_size=1024**2) actual = client.checksum(img, block_size=1024**2) assert actual == expected
def test_checksum(tmpdir, size, algorithm, digest_size, checksum): path = str(tmpdir.join("file")) with open(path, "wb") as f: f.write(b"data") f.truncate(size) actual = blkhash.checksum(path, block_size=blkhash.BLOCK_SIZE, algorithm=algorithm, digest_size=digest_size) assert actual == { "algorithm": algorithm, "block_size": blkhash.BLOCK_SIZE, "checksum": checksum, }
def test_user_block_size(srv, client, tmpdir, block_size): size = 2 * 1024**2 img = str(tmpdir.join("file")) qemu_img.create(img, "raw", size=size) # File backend operate on host data, not guest data. checksum = blkhash.checksum(img, block_size=block_size) ticket = testutil.create_ticket(url="file://" + img, size=size) srv.auth.add(ticket) res = client.request("GET", "/images/{}/checksum?block_size={}" .format(ticket["uuid"], block_size)) data = res.read() assert res.status == 200 res = json.loads(data) assert res == checksum
def test_checksum_from_ova(tmpdir, fmt, compressed): # Create temporary file with some data. size = 2 * 1024**2 tmp = str(tmpdir.join("tmp")) with open(tmp, "wb") as f: f.truncate(size) f.write(b"x" * CLUSTER_SIZE) # Create test image from temporary file. img = str(tmpdir.join("img")) qemu_img.convert(tmp, img, "raw", fmt, compressed=compressed) # Add test image to ova. member = os.path.basename(img) ova = str(tmpdir.join("ova")) with tarfile.open(ova, "w") as tar: tar.add(img, arcname=member) expected = blkhash.checksum(tmp, block_size=1024**2) actual = client.checksum(ova, member=member, block_size=1024**2) assert actual == expected
def test_algorithms(srv, client, tmpdir, algorithm, digest_size): size = 2 * 1024**2 image = str(tmpdir.join("image")) qemu_img.create(image, "raw", size=size) ticket = testutil.create_ticket(url="file://" + image, size=size) srv.auth.add(ticket) # File backend operate on host data, not guest data. checksum = blkhash.checksum( image, algorithm=algorithm, digest_size=digest_size) path = "/images/{}/checksum?algorithm={}".format( ticket["uuid"], algorithm) res = client.request("GET", path) data = res.read() assert res.status == 200 res = json.loads(data) assert res == checksum
def test_file_backend(srv, client, user_file, fmt): qemu_img.create(user_file.path, fmt, size="2m") with qemu_nbd.open(user_file.path, fmt) as c: # Add cluster with data. c.write(1 * 1024**2, b"some data") c.flush() # File backend operate on host data, not guest data. checksum = blkhash.checksum(user_file.path) # File backend uses actual size, not vitual size. size = os.path.getsize(user_file.path) ticket = testutil.create_ticket(url="file://" + user_file.path, size=size) srv.auth.add(ticket) res = client.request("GET", "/images/{}/checksum".format(ticket["uuid"])) data = res.read() assert res.status == 200 res = json.loads(data) assert res == checksum