def test_write_read(self, run): benji_obj = self.benji_open() store = BenjiStore(benji_obj) version = store.find_versions(version_uid=self.version_uid)[0] store.open(version) cow_version = store.create_cow_version(version) image_2_filename = os.path.join(self.testpath.path, 'image') image_2 = bytearray(self.image) block_size = random.randint(512, 2 * 65536) for pos in range(0, self.size, block_size): if pos + block_size > self.size: write_length = self.size - pos else: write_length = block_size if random.randint(1, 100) <= 25: if random.randint(0, 1): image_2[pos:pos + write_length] = self.random_bytes(write_length) store.write(cow_version, pos, image_2[pos:pos + write_length]) else: image_2[pos:pos + write_length] = b'\0' * write_length store.write(cow_version, pos, b'\0' * write_length) with open(image_2_filename, 'wb') as f: f.write(image_2) for block_size in (512, 1024, 4096, 65536, 1861): image = bytearray() for pos in range(0, self.size, block_size): if pos + block_size > self.size: read_length = self.size - pos else: read_length = block_size image = image + store.read(version, cow_version, pos, read_length) self.assertEqual(self.size, len(image)) for pos in range(0, self.size): if image_2[pos] != image[pos]: self.fail( 'Written image different at offset {} (block size {}).' .format(pos, block_size)) break store.fixate(cow_version) benji_obj.deep_scrub(cow_version.uid, 'file:{}'.format(image_2_filename)) store.close(version) benji_obj.close()
def test_create_cow_version(self): benji_obj = self.benji_open() store = BenjiStore(benji_obj) version = store.find_versions(version_uid=self.version_uid)[0] store.open(version) cow_version = store.create_cow_version(version) self.assertEqual(version.volume, cow_version.volume) self.assertEqual(version.size, cow_version.size) self.assertEqual(version.block_size, cow_version.block_size) self.assertEqual(version.storage_id, cow_version.storage_id) self.assertNotEqual(version.snapshot, cow_version.snapshot) store.fixate(cow_version) store.close(version) benji_obj.close()
def test_read(self, block_size): benji_obj = self.benji_open() store = BenjiStore(benji_obj) version = store.find_versions(version_uid=self.version_uid)[0] store.open(version) image = bytearray() for pos in range(0, self.size, block_size): if pos + block_size > self.size: read_length = self.size - pos else: read_length = block_size image = image + store.read(version, None, pos, read_length) self.assertEqual(self.size, len(image)) self.assertEqual(self.image, image) store.close(version) benji_obj.close()