def __init__(self, hypervisor, image_datastore, image_path=None): super(FakeImageManager, self).__init__() self._hypervisor = hypervisor self._logger = logging.getLogger(__name__) self.image_path = image_path or self.IMAGE_PATH self._disk_manager = FakeDiskManager(hypervisor, self.image_path) self._image_datastore = image_datastore self._setup_datastores()
def __init__(self, agent_config): self.logger = logging.getLogger(__name__) prefix = socket.gethostname() suffix = str(agent_config.host_port) self._uuid = str(uuid.uuid5(uuid.NAMESPACE_DNS, prefix + suffix)) tempdir = mkdtemp(prefix='disk', delete=True) self.disk_manager = FakeDiskManager(self, os.path.join(tempdir, 'disk')) self.vm_manager = FakeVmManager(self) self.network_manager = FakeNetworkManager(self, agent_config.networks) self.system = FakeSystem(self) datastores = agent_config.datastores # For fake hypervisor, we assume there is always one image datastore. if agent_config.image_datastores: image_datastore = list(agent_config.image_datastores)[0]["name"] else: image_datastore = None self.datastore_manager = FakeDatastoreManager(self.system, datastores, image_datastore) self.image_manager = FakeImageManager(self, image_datastore) self.image_manager.copy_to_datastores( "ttylinux", self.datastore_manager.get_datastore_ids())
class FakeImageManager(ImageManager): IMAGE_PATH = '/tmp/images' def __init__(self, hypervisor, image_datastore, image_path=None): super(FakeImageManager, self).__init__() self._hypervisor = hypervisor self._logger = logging.getLogger(__name__) self.image_path = image_path or self.IMAGE_PATH self._disk_manager = FakeDiskManager(hypervisor, self.image_path) self._image_datastore = image_datastore self._setup_datastores() def copy_to_datastores(self, image_id, datastores): """Demoware method to deploy the ttylinux image.""" for datastore in datastores: self.deploy_image(image_id, image_id, datastore) def deploy_image(self, src_id, dst_id, datastore_id): self._logger.info("creating fake image for %s in datastore %s" % (src_id, datastore_id)) self._disk_manager.create_disk(datastore_id, dst_id, 0, force=True) def check_image(self, image_id, datastore_id): return self._disk_manager._check_disk(image_id, datastore_id) def check_and_validate_image(self, image_id, datastore_id): return self.check_image(image_id, datastore_id) def copy_image(self, src_datastore, src_id, dst_datastore, dst_id): self._logger.info("copying fake image from %s in datastore %s to %s in" " datastore %s" % (src_id, src_datastore, dst_id, dst_datastore)) self._disk_manager.copy_disk(src_datastore, src_id, dst_datastore, dst_id) def get_image_metadata(self, image_id, datastore): return None def image_file(self, datastore_id, image_id): return self.get_image_path(datastore_id, image_id) def get_image_directory_path(self, datastore_id, image_id): # for the fake host, the image id is not a part of the image directory # path. return os.path.join(self.image_path, datastore_id) def get_image_path(self, datastore_id, image_id): image_dir = self.get_image_directory_path(datastore_id, image_id) return os.path.join(image_dir, "%s.vmdk" % image_id) def get_images(self, datastore): return list(self._disk_manager.get_resource_ids(datastore)) def get_inactive_images(self, datastore_id): return list() def _setup_datastores(self): datastores = self._hypervisor.datastore_manager.get_datastore_ids() self._logger.info("setup datastore (%s)" % ",".join(datastores)) for i, datastore_id in enumerate(datastores): self._logger.info("create dir for %s" % datastore_id) datastore_id_path = os.path.join(self.image_path, datastore_id) mkdir_p(datastore_id_path) datastore_name = \ self._hypervisor.datastore_manager.datastore_name(datastore_id) self._logger.info("create symlink for %s" % datastore_name) datastore_name_path = os.path.join(self.image_path, datastore_name) if not os.path.exists(datastore_name_path): os.symlink(datastore_id_path, datastore_name_path) def get_datastore_id_from_path(self, image_path): # The image path looks like: /tmp/tmprWGCzm/datastore1/ttylinux.vmdk directory = os.path.dirname(image_path) return os.path.basename(directory) def get_image_id_from_path(self, image_path): # The image path looks like: /tmp/tmprWGCzm/datastore1/ttylinux.vmdk basename = os.path.basename(image_path) return os.path.splitext(basename)[0] def image_size(self, image_id): return self._disk_manager.disk_size(self._image_datastore, image_id) def touch_image_timestamp(self, dsid, image_id): return def get_timestamp_mod_time_from_dir(self, dirname, renamed=False): return True, 0 def create_image(self, image_id, datastore_id): return def finalize_image(self, datastore_id, tmp_dir, image_id): return def delete_tmp_dir(self, datastore_id, tmp_dir): return def create_image_with_vm_disk(self, datastore_id, tmp_dir, image_id, vm_disk_os_path): return
def test_disk_size(self): dm = Fdm(MagicMock(), capacity_map={'datastore1': 1024}) dm.create_disk('datastore1', 'name1', 512) assert_that(dm.disk_size('datastore1', 'name1'), is_(512))
def test_capacity_check(self): dm = Fdm(MagicMock(), capacity_map={'datastore1': 1024}) dm.create_disk('datastore1', 'name1', 1023) assert_that(dm.used_storage('datastore1'), is_(1023)) dm.create_disk('datastore1', 'name2', 2)
def test_used_storage(self): dm = Fdm(MagicMock()) dm.create_disk('datastore1', 'name1', 1024) assert_that(dm.used_storage('datastore1'), is_(1024)) dm.create_disk('datastore1', 'name2', 10) assert_that(dm.used_storage('datastore1'), is_(1034))