diff --git a/virttest/virt_storage/backend/base.py b/virttest/virt_storage/backend/base.py index 154c8c2d323..aba950c6383 100644 --- a/virttest/virt_storage/backend/base.py +++ b/virttest/virt_storage/backend/base.py @@ -127,7 +127,16 @@ def acquire_volume(self, volume): self.refresh() def info(self): - return utils.get_instance_info(self) + out = dict() + out["name"] = self.name + out["uuid"] = str(self.uuid) + out["source"] = str(self.source) + out["target"] = str(self.target) + out["capacity"] = str(self.capacity) + out["available"] = str(self.available) + out["helper"] = str(self.helper) + out["volumes"] = list(map(str, self._volumes)) + return out def __str__(self): return "%s:%s" % (self.__class__.__name__, self.name) diff --git a/virttest/virt_storage/backend/direct_iscsi.py b/virttest/virt_storage/backend/direct_iscsi.py index 91eaa01339e..19eae3317ef 100644 --- a/virttest/virt_storage/backend/direct_iscsi.py +++ b/virttest/virt_storage/backend/direct_iscsi.py @@ -63,4 +63,5 @@ def __find_appropriate_lun(self, vol): @property def available(self): - return sum(filter(lambda x: x.url and x.name is None, self._volumes)) + free_voluems = filter(lambda x: x.is_allocated and x.name is None, self._volumes) + return sum(map(lambda x: x.capacity, free_voluems)) diff --git a/virttest/virt_storage/backend/directory.py b/virttest/virt_storage/backend/directory.py index 9fec8fd0a7c..a6367c74ae6 100644 --- a/virttest/virt_storage/backend/directory.py +++ b/virttest/virt_storage/backend/directory.py @@ -40,8 +40,6 @@ def create_volume_from_local(self, path): return volume def create_volume(self, volume): - if volume.path is None: - volume.path = self.helper.get_path_by_name(volume.name) storage_util.create_volume(volume) volume.is_allocated = True return volume diff --git a/virttest/virt_storage/backend/gluster.py b/virttest/virt_storage/backend/gluster.py index 6854fbac032..7c73a033e85 100644 --- a/virttest/virt_storage/backend/gluster.py +++ b/virttest/virt_storage/backend/gluster.py @@ -24,9 +24,6 @@ def remove_volume(self, volume): self._volumes.remove(volume) def create_volume(self, volume): - if volume.url is None: - url = self.helper.get_url_by_name(volume.name) - volume.url = volume.path = url storage_util.create_volume(volume) volume.is_allocated = True return volume diff --git a/virttest/virt_storage/backend/nfs.py b/virttest/virt_storage/backend/nfs.py index 4271f85dbba..b10f752e7d4 100644 --- a/virttest/virt_storage/backend/nfs.py +++ b/virttest/virt_storage/backend/nfs.py @@ -27,19 +27,16 @@ def refresh(self): return map(self.create_volume_from_remote, urls) def create_volume_from_remote(self, url): + path = self.helper.url_to_path(url) + capacity = self.helper.get_size(path) volume = storage_volume.StorageVolume(self) volume.url = url - volume.path = self.helper.url_to_path(url) - volume.capacity = self.helper.get_size(volume.path) + volume.path = path + volume.capacity = capacity volume.is_allocated = True return volume def create_volume(self, volume): - if volume.url is None: - url = self.helper.get_url_by_name(volume.name) - volume.url = url - path = self.helper.url_to_path(volume.url) - volume.path = path storage_util.create_volume(volume) volume.is_allocated = True return volume diff --git a/virttest/virt_storage/backend/rbd.py b/virttest/virt_storage/backend/rbd.py index a663de8272c..747e410f431 100755 --- a/virttest/virt_storage/backend/rbd.py +++ b/virttest/virt_storage/backend/rbd.py @@ -28,9 +28,6 @@ def create_volume_from_remote(self, url): return volume def create_volume(self, volume): - if volume.url is None: - url = self.helper.get_url_by_name(volume.name) - volume.url = volume.path = url if volume.is_allocated: self.helper.remove_image(volume.url) storage_util.create_volume(volume) diff --git a/virttest/virt_storage/helper/fscli.py b/virttest/virt_storage/helper/fscli.py index 3da9a5b8fc2..6b3909ae354 100755 --- a/virttest/virt_storage/helper/fscli.py +++ b/virttest/virt_storage/helper/fscli.py @@ -1,4 +1,3 @@ -import logging import os import shutil @@ -14,12 +13,11 @@ class FsCli(object): def __init__(self, dir_path): self.dir_path = dir_path self._is_export = None + self._protocol = r"file://" def create(self): if not self.is_exists: os.makedirs(self.dir_path) - else: - logging.warn("Dir '%s' is exists!" % self.dir_path) self._is_export = True def remove(self): @@ -32,7 +30,12 @@ def remove_file(path): return process.system("rm -f %s" % path, shell=True) def get_path_by_name(self, name): - return os.path.join(self.dir_path, name) + path = os.path.join(self.dir_path, name) + return os.path.realpath(path) + + def get_url_by_name(self, name): + path = self.get_path_by_name(name) + return self.path_to_url(path) def list_files(self, _root=None): """List all files in top directory""" @@ -40,7 +43,8 @@ def list_files(self, _root=None): def _list_files(_dir): for root, dirs, files in os.walk(_dir): for f in files: - yield os.path.join(root, f) + path = os.path.join(root, f) + yield os.path.realpath(path) for d in dirs: _d = os.path.join(root, d) _list_files(_d) @@ -49,22 +53,19 @@ def _list_files(_dir): return _list_files(root_dir) @staticmethod - def get_size(f): + def get_size(path): """Get file size""" try: - return os.path.getsize(f) + return os.path.getsize(path) except OSError: return 0 - @staticmethod - def url_to_path(f): - """Get file real-path""" - return os.path.realpath(f) - - @staticmethod - def path_to_url(f): + def path_to_url(self, path): """Get url schema path""" - return "file://%s" % os.path.realpath(f) + return "%s%s" % (self._protocol, os.path.realpath(path)) + + def url_to_path(self, url): + return url[len(self._protocol):] @property def is_exists(self): diff --git a/virttest/virt_storage/helper/glustercli.py b/virttest/virt_storage/helper/glustercli.py index 41a1f574132..77b097a4a58 100755 --- a/virttest/virt_storage/helper/glustercli.py +++ b/virttest/virt_storage/helper/glustercli.py @@ -51,12 +51,6 @@ def get_size(self, url): except OSError: return 0 - def refresh(self): - if self.volume.mounted: - self.volume.umount() - self.volume.mount() - self.volume.umount() - def mount(self): if not self.is_mounted: self.volume.mount() diff --git a/virttest/virt_storage/helper/iscsicli.py b/virttest/virt_storage/helper/iscsicli.py index 12d24cc4e4c..b0bd77fbd23 100755 --- a/virttest/virt_storage/helper/iscsicli.py +++ b/virttest/virt_storage/helper/iscsicli.py @@ -15,7 +15,7 @@ def get_pool_helper(pool): class IscsiCli(object): - dev_root = '/dev/disk/by-path/' + dev_root = '/dev/disk/by-path' initiator_file = "/etc/iscsi/initiatorname.iscsi" iscsi_service = service.SpecificServiceManager("iscsi") @@ -108,6 +108,16 @@ def path_to_url(self, path): return None + def url_to_path(self, url): + match = re.search(r"iscsi://(?P.*)@(?P.*)/(?P.*)/(?P\d+)", url) + if match: + portal = match.groupdict()['portal'] + target = match.groupdict()['target'] + lun = match.groupdict()['lun'] + name = "ip-%s-iscsi-%s-lun-%s" % (portal, target, lun) + return "%s/%s" % (self.dev_root, name) + return None + @property def is_logged(self): if self._is_logged_in is None: diff --git a/virttest/virt_storage/helper/nfscli.py b/virttest/virt_storage/helper/nfscli.py index bb5681f751e..f76f9503cff 100755 --- a/virttest/virt_storage/helper/nfscli.py +++ b/virttest/virt_storage/helper/nfscli.py @@ -21,6 +21,7 @@ def __init__(self, host, dir_path, target=None): self._target = target self._is_mounted = None self._is_export = None + self._protocol = r"nfs://" super(NfsCli, self).__init__(self.target) @property @@ -66,13 +67,13 @@ def mount(self): self._is_mounted = True def path_to_url(self, f): - return f.replace(self.target, "nfs://%s" % self.src_path) + return f.replace(self.target, "%s%s" % (self._protocol, self.src_path)) def url_to_path(self, url): - return url.replace("nfs://%s" % self.src_path, self.target) + return url.replace("%s%s" % (self._protocol, self.src_path), self.target) def get_url_by_name(self, name): - return "nfs://%s/%s" % (self.src_path, name) + return "%s%s/%s" % (self._protocol, self.src_path, name) @property def is_export(self): diff --git a/virttest/virt_storage/helper/rbdcli.py b/virttest/virt_storage/helper/rbdcli.py index 554d4b43491..a872001c527 100755 --- a/virttest/virt_storage/helper/rbdcli.py +++ b/virttest/virt_storage/helper/rbdcli.py @@ -31,7 +31,8 @@ def __init__(self, conffile, conf, pool): self.cluster = rados.Rados(conffile=conffile, conf=conf) self._pool = pool self._is_connect = False - self._base_url = "rbd:%s/" % self._pool + self._protocol = "rbd:" + self._base_url = "%s%s/" % (self._protocol, self._pool) def list_images(self): self.connect() @@ -43,6 +44,11 @@ def list_images(self): def get_url_by_name(self, image): return "%s%s" % (self._base_url, image) + def url_to_path(self, url): + if url.startswith(self._protocol): + return url[len(self._protocol):] + return url + def get_size(self, image): if not self.is_image_exists(image): return 0 diff --git a/virttest/virt_storage/storage_volume.py b/virttest/virt_storage/storage_volume.py index 761172e75f4..4e177e633ad 100755 --- a/virttest/virt_storage/storage_volume.py +++ b/virttest/virt_storage/storage_volume.py @@ -1,5 +1,4 @@ from virttest import utils_misc -from virttest.virt_storage import utils class StorageVolume(object): @@ -7,19 +6,43 @@ class StorageVolume(object): def __init__(self, pool, name=None, _format="raw"): self.name = name self.pool = pool - self.url = None - self.path = None - self.format = _format + self._url = None + self._path = None self._capacity = None self._backing_store = None self._key = None self._auth = None + self.format = _format self.is_allocated = None self.encryption = None self.preallocation = None self.used_by = [] self.pool.add_volume(self) + @property + def url(self): + if self._url is None: + if self.name and hasattr(self.pool.helper, "get_url_by_name"): + url = self.pool.helper.get_url_by_name(self.name) + self._url = url + return self._url + + @url.setter + def url(self, url): + self._url = url + + @property + def path(self): + if self._path is None: + if self.url and hasattr(self.pool.helper, "url_to_path"): + path = self.pool.helper.url_to_path(self.url) + self._path = path + return self._path + + @path.setter + def path(self, path): + self._path = path + @property def key(self): if self._key is None: @@ -36,9 +59,8 @@ def key(self, key): @property def capacity(self): if self._capacity is None: - if self.key: - driver = self.pool.helper - self._capacity = driver.get_size(self.key) + if self.key and hasattr(self.pool.helper, "get_size"): + self._capacity = self.pool.get_size(self.key) return int(self._capacity) @capacity.setter @@ -61,11 +83,24 @@ def backing_store(self, backing): @property def auth(self): if self._auth is None: - self._auth = self.pool.source.auth + if self.pool.source: + self._auth = self.pool.source.auth return self._auth def info(self): - return utils.get_instance_info(self) + out = dict() + out["name"] = self.name + out["pool"] = str(self.pool) + out["url"] = self.url + out["path"] = self.path + out["key"] = self.key + out["format"] = self.format + out["auth"] = str(self.auth) + out["capacity"] = self.capacity + out["backing_store"] = str(self.backing_store) + out["encryption"] = str(self.encryption) + out["preallocation"] = self.preallocation + return out def generate_qemu_img_options(self): options = " -f %s" % self.format diff --git a/virttest/virt_storage/unit_test.py b/virttest/virt_storage/unit_test.py index b7705ba8cf4..5a7e92be8aa 100755 --- a/virttest/virt_storage/unit_test.py +++ b/virttest/virt_storage/unit_test.py @@ -128,7 +128,23 @@ def test_05_acqurie_volume(self): unittest.TestCase.assertTrue(self, volume.is_allocated, "volume(%s) is_allocated value mismatch!" % volume.name) - def test_06_find_pool_by_volume(self): + def test_06_compare_volume_info_img1(self): + img1 = sp_admin.get_volume_by_name("img1") + info1 = {'capacity': 104857600, 'name': 'img1', 'format': 'qcow2', 'url': 'file:///tmp/avocado/img1', + 'encryption': 'None', 'auth': 'None', 'backing_store': 'StorageVolume: img4, /tmp/avocado/img4', + 'preallocation': 'off', 'key': '/tmp/avocado/img1', 'path': '/tmp/avocado/img1', + 'pool': 'DirectoryPool:sp1'} + unittest.TestCase.assertEqual(self, info1, img1.info(), "Volume info of 'img1' mismatch!") + + def test_06_compare_volume_info_img4(self): + img = sp_admin.get_volume_by_name("img4") + info = {'capacity': 104857600, 'name': 'img4', 'format': 'qcow2', 'url': 'file:///tmp/avocado/img4', + 'encryption': 'VolumeEncryption: luks', 'auth': 'None', + 'backing_store': 'StorageVolume: img5, /tmp/avocado/img5', 'preallocation': 'off', + 'key': '/tmp/avocado/img4', 'path': '/tmp/avocado/img4', 'pool': 'DirectoryPool:sp1'} + unittest.TestCase.assertEqual(self, info, img.info(), "Volume info of 'img4' mismatch!") + + def test_07_find_pool_by_volume(self): for volume in sp_admin.list_volumes(): if volume.name: pool = sp_admin.find_pool_by_volume(volume) @@ -137,7 +153,7 @@ def test_06_find_pool_by_volume(self): unittest.TestCase.assertEqual( self, pool.name, pool_name, "pool name mismatch!") - def test_07_find_volume_by_key(self): + def test_08_find_volume_by_key(self): path_img1 = "/tmp/avocado/img1" unittest.TestCase.assertIsNotNone( self, @@ -151,20 +167,20 @@ def test_07_find_volume_by_key(self): sp_admin.get_volume_by_name("img1"), "volumes are not same!") - def test_08_find_pool_by_path(self): + def test_09_find_pool_by_path(self): path = "/tmp/avocado" pool = sp_admin.find_pool_by_path(path) unittest.TestCase.assertEqual( self, "sp1", pool.name, "can not found pool by path!") - def test_09_remove_volume(self): + def test_10_remove_volume(self): for volume in sp_admin.list_volumes(): if volume.is_allocated: sp_admin.remove_volume(volume) unittest.TestCase.assertNotIn(self, volume, sp_admin.list_volumes(), "Volume (%s) is exists!" % volume.name) - def test_10_stop_pool(self): + def test_11_stop_pool(self): for pool in sp_admin.list_pools(): sp_admin.stop_pool(pool) unittest.TestCase.assertEqual( @@ -174,7 +190,7 @@ def test_10_stop_pool(self): "pool (%s) state mismatch" % pool.name) - def test_11_destory_pool(self): + def test_12_destory_pool(self): for pool in sp_admin.list_pools(): sp_admin.destroy_pool(pool) unittest.TestCase.assertEqual( diff --git a/virttest/virt_storage/utils/__init__.py b/virttest/virt_storage/utils/__init__.py index 556e29f438e..319d357a427 100755 --- a/virttest/virt_storage/utils/__init__.py +++ b/virttest/virt_storage/utils/__init__.py @@ -21,30 +21,3 @@ def get_pool_helper(pool): func = getattr(driver, func_name) return func(pool) - -def get_instance_info(inst): - _info = dict() - for key, val in vars(inst).items(): - # skip method - if callable(val): - continue - # skip private attributes - if key.startswith("_"): - key = key.lstrip("_") - if hasattr(inst, key): - _info[key] = getattr(inst, key) - continue - - if isinstance(val, list): - val = list(map(str, val)) - _info[key] = val - continue - elif isinstance(val, dict): - for k, v in val.items(): - val[k] = str(v) - _info[key] = val - continue - else: - _info[key] = str(val) - continue - return _info diff --git a/virttest/virt_storage/virt_encryption.py b/virttest/virt_storage/virt_encryption.py index e49c27ef8b2..50d4c1e870e 100755 --- a/virttest/virt_storage/virt_encryption.py +++ b/virttest/virt_storage/virt_encryption.py @@ -7,6 +7,9 @@ def __init__(self, encrypt_format=None, secret=None): self.format = encrypt_format self.secret = secret + def __str__(self): + return "%s: %s" % (self.__class__.__name__, self.format) + @classmethod def encryption_define_by_params(cls, params): instance = cls()