Skip to content

Commit

Permalink
remove useless refresh function
Browse files Browse the repository at this point in the history
Signed-off-by: Xu Tian <[email protected]>
  • Loading branch information
Xu Tian committed Nov 12, 2019
1 parent cb483a9 commit 978a6ac
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 85 deletions.
11 changes: 10 additions & 1 deletion virttest/virt_storage/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion virttest/virt_storage/backend/direct_iscsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
2 changes: 0 additions & 2 deletions virttest/virt_storage/backend/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions virttest/virt_storage/backend/gluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 4 additions & 7 deletions virttest/virt_storage/backend/nfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 0 additions & 3 deletions virttest/virt_storage/backend/rbd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 16 additions & 15 deletions virttest/virt_storage/helper/fscli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
import os
import shutil

Expand All @@ -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):
Expand All @@ -32,15 +30,21 @@ 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"""

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)
Expand All @@ -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):
Expand Down
6 changes: 0 additions & 6 deletions virttest/virt_storage/helper/glustercli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion virttest/virt_storage/helper/iscsicli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -108,6 +108,16 @@ def path_to_url(self, path):

return None

def url_to_path(self, url):
match = re.search(r"iscsi://(?P<secret>.*)@(?P<portoal>.*)/(?P<target>.*)/(?P<lun>\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:
Expand Down
7 changes: 4 additions & 3 deletions virttest/virt_storage/helper/nfscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 7 additions & 1 deletion virttest/virt_storage/helper/rbdcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
53 changes: 44 additions & 9 deletions virttest/virt_storage/storage_volume.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
from virttest import utils_misc
from virttest.virt_storage import utils


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:
Expand All @@ -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
Expand All @@ -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
Expand Down
28 changes: 22 additions & 6 deletions virttest/virt_storage/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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(
Expand Down
Loading

0 comments on commit 978a6ac

Please sign in to comment.