def test_find_versions(self): benji_obj = self.benji_open() store = BenjiStore(benji_obj) versions = store.find_versions() self.assertEqual(1, len(versions)) self.assertEqual(self.version_uid, versions[0].uid) self.assertEqual(self.size, versions[0].size) 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 nbd(self, bind_address: str, bind_port: str, read_only: bool) -> None: with Benji(self.config) as benji_obj: store = BenjiStore(benji_obj) addr = (bind_address, bind_port) server = NbdServer(addr, store, read_only) logger.info("Starting to serve NBD on %s:%s" % (addr[0], addr[1])) server.serve_forever()
def nbd(self, bind_address, bind_port, read_only): benji_obj = None try: benji_obj = Benji(self.config) store = BenjiStore(benji_obj) addr = (bind_address, bind_port) server = NbdServer(addr, store, read_only) logger.info("Starting to serve nbd on %s:%s" % (addr[0], addr[1])) server.serve_forever() finally: if benji_obj: 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()
def test(self): benji_obj = self.benjiOpen() store = BenjiStore(benji_obj) addr = ('127.0.0.1', self.SERVER_PORT) read_only = False self.nbd_server = NbdServer(addr, store, read_only) logger.info("Starting to serve NBD on %s:%s" % (addr[0], addr[1])) self.subprocess_run(args=['sudo', 'modprobe', 'nbd']) self.nbd_client_thread = threading.Thread(target=self.nbd_client, daemon=True, args=(self.version_uid,)) self.nbd_client_thread.start() self.nbd_server.serve_forever() self.nbd_client_thread.join() self.assertEqual({self.version_uid[0], VersionUid(2)}, set([version.uid for version in benji_obj.ls()])) benji_obj.close()
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()