def test_upload_from_ova(tmpdir, srv, fmt, compressed): offset = CLUSTER_SIZE data = b"I can eat glass and it doesn't hurt me." # Create raw disk with some data. tmp = str(tmpdir.join("tmp")) with open(tmp, "wb") as f: f.truncate(IMAGE_SIZE) f.seek(offset) f.write(data) # Create source disk. src = str(tmpdir.join("src")) qemu_img.convert(tmp, src, "raw", fmt, compressed=compressed) # Create OVA package. ova = str(tmpdir.join("src.ova")) with tarfile.open(ova, "w") as tar: tar.add(src, arcname=os.path.basename(src)) # Prepare destination file. dst = str(tmpdir.join("dst")) with open(dst, "wb") as f: f.truncate(IMAGE_SIZE) # Test uploading src from ova. url = prepare_transfer(srv, "file://" + dst) client.upload(ova, url, srv.config.tls.ca_file, member=os.path.basename(src)) qemu_img.compare(src, dst)
def test_ova_compressed_qcow2(tmpdir): size = 1024**2 offset = 64 * 1024 data = b"I can eat glass and it doesn't hurt me." tmp = str(tmpdir.join("disk.raw")) with open(tmp, "wb") as f: f.truncate(size) f.seek(offset) f.write(data) disk = str(tmpdir.join("disk.qcow2")) qemu_img.convert(tmp, disk, "raw", "qcow2", compressed=True) ova = str(tmpdir.join("vm.ova")) # Create tar file with compressed qcow2 disk. with tarfile.open(ova, "w") as tar: tar.add(disk, arcname=os.path.basename(disk)) # Read disk contents from the tar file. with tarfile.open(ova) as tar: member = tar.getmember(os.path.basename(disk)) with qemu_nbd.open( ova, fmt="qcow2", read_only=True, offset=member.offset_data, size=member.size) as d: assert d.export_size == size assert d.read(offset, len(data)) == data
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_measure_to_qcow2(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) # Created test image from temporary file. img = str(tmpdir.join("img")) qemu_img.convert(tmp, img, "raw", fmt, compressed=compressed) measure = client.measure(img, "qcow2") assert measure["required"] == 393216
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_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_info(tmpdir, fmt, compressed): # Created 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) # Created test image from temporary file. img = str(tmpdir.join("img")) qemu_img.convert(tmp, img, "raw", fmt, compressed=compressed) img_info = client.info(img) # Check image info. assert img_info["format"] == fmt assert img_info["virtual-size"] == size # We don't add member info if member was not specified. assert "member-offset" not in img_info assert "member-size" not in img_info # Create ova with test image. member = os.path.basename(img) ova = str(tmpdir.join("ova")) with tarfile.open(ova, "w") as tar: tar.add(img, arcname=member) # Get info for the member from the ova. ova_info = client.info(ova, member=member) # Image info from ova should be the same. assert ova_info["format"] == fmt assert ova_info["virtual-size"] == size # If member was specified, we report also the offset and size. with tarfile.open(ova) as tar: member_info = tar.getmember(member) assert ova_info["member-offset"] == member_info.offset_data assert ova_info["member-size"] == member_info.size
def test_measure_from_ova(tmpdir, compressed, fmt): # 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) # Created test image from temporary file. img = str(tmpdir.join("img")) qemu_img.convert(tmp, img, "raw", "qcow2", compressed=compressed) # Measure the image. img_measure = client.measure(img, fmt) # We don't add member info if member was not specified. assert "member-offset" not in img_measure assert "member-size" not in img_measure # 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) # Measure the image from the ova. ova_measure = client.measure(ova, fmt, member=member) # Measurement from ova should be same. assert ova_measure["required"] == img_measure["required"] assert ova_measure["fully-allocated"] == img_measure["fully-allocated"] # If member was specified, we report also the offset and size. with tarfile.open(ova) as tar: member_info = tar.getmember(member) assert ova_measure["member-offset"] == member_info.offset_data assert ova_measure["member-size"] == member_info.size
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)