Skip to content

Commit

Permalink
utils_test/libvirt: Reorg disk related APIs into utils_disk
Browse files Browse the repository at this point in the history
Reorganise disk related method to utils_disk and break circular
imports caused with respect to disk related APIs in utils_misc and
gluster, move some of storage apis to utils_disk and utils_storage
for better work flow.

Signed-off-by: Balamuruhan S <[email protected]>
  • Loading branch information
balamuruhans committed Aug 19, 2019
1 parent 5c83fe5 commit 86817e1
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 207 deletions.
94 changes: 0 additions & 94 deletions virttest/gluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

from avocado.utils import process

from virttest import data_dir
from virttest import utils_misc
from virttest import utils_net
from virttest import error_context
from virttest.compat_52lts import decode_to_text
Expand Down Expand Up @@ -236,48 +234,6 @@ def gluster_vol_create(vol_name, hostname, brick_path, force=False, session=None
return is_gluster_vol_avail(vol_name, session)


def glusterfs_mount(g_uri, mount_point):
"""
Mount gluster volume to mountpoint.
:param g_uri: stripped gluster uri from create_gluster_uri(.., True)
:type g_uri: str
"""
utils_misc.mount(g_uri, mount_point, "glusterfs", None,
False, "fuse.glusterfs")


@error_context.context_aware
def create_gluster_vol(params):
vol_name = params.get("gluster_volume_name")
force = params.get('force_recreate_gluster') == "yes"

brick_path = params.get("gluster_brick")
if not os.path.isabs(brick_path): # do nothing when path is absolute
base_dir = params.get("images_base_dir", data_dir.get_data_dir())
brick_path = os.path.join(base_dir, brick_path)

error_context.context("Host name lookup failed")
hostname = socket.gethostname()
if not hostname or hostname == "(none)":
if_up = utils_net.get_net_if(state="UP")
for i in if_up:
ipv4_value = utils_net.get_net_if_addrs(i)["ipv4"]
logging.debug("ipv4_value is %s", ipv4_value)
if ipv4_value != []:
ip_addr = ipv4_value[0]
break
hostname = ip_addr

# Start the gluster dameon, if not started
glusterd_start()
# Check for the volume is already present, if not create one.
if not is_gluster_vol_avail(vol_name) or force:
return gluster_vol_create(vol_name, hostname, brick_path, force)
else:
return True


@error_context.context_aware
def create_gluster_uri(params, stripped=False):
"""
Expand Down Expand Up @@ -307,56 +263,6 @@ def create_gluster_uri(params, stripped=False):
return gluster_uri


def file_exists(params, filename_path):
sg_uri = create_gluster_uri(params, stripped=True)
g_uri = create_gluster_uri(params, stripped=False)
# Using directly /tmp dir because directory should be really temporary and
# should be deleted immediately when no longer needed and
# created directory don't file tmp dir by any data.
tmpdir = "gmount-%s" % (utils_misc.generate_random_string(6))
tmpdir_path = os.path.join(data_dir.get_tmp_dir(), tmpdir)
while os.path.exists(tmpdir_path):
tmpdir = "gmount-%s" % (utils_misc.generate_random_string(6))
tmpdir_path = os.path.join(data_dir.get_tmp_dir(), tmpdir)
ret = False
try:
try:
os.mkdir(tmpdir_path)
glusterfs_mount(sg_uri, tmpdir_path)
mount_filename_path = os.path.join(tmpdir_path,
filename_path[len(g_uri):])
if os.path.exists(mount_filename_path):
ret = True
except Exception as e:
logging.error("Failed to mount gluster volume %s to"
" mount dir %s: %s" % (sg_uri, tmpdir_path, e))
finally:
if utils_misc.umount(sg_uri, tmpdir_path, "glusterfs", False,
"fuse.glusterfs"):
try:
os.rmdir(tmpdir_path)
except OSError:
pass
else:
logging.warning("Unable to unmount tmp directory %s with glusterfs"
" mount.", tmpdir_path)
return ret


def get_image_filename(params, image_name, image_format):
"""
Form the image file name using gluster uri
"""

img_name = image_name.split('/')[-1]
gluster_uri = create_gluster_uri(params)
if params.get("image_raw_device") == "yes":
image_filename = "%s%s" % (gluster_uri, img_name)
else:
image_filename = "%s%s.%s" % (gluster_uri, img_name, image_format)
return image_filename


@error_context.context_aware
def gluster_allow_insecure(vol_name, session=None):
"""
Expand Down
62 changes: 4 additions & 58 deletions virttest/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

from virttest import iscsi
from virttest import utils_misc
from virttest import utils_storage
from virttest import virt_vm
from virttest import gluster
from virttest import lvm
from virttest import ceph
from virttest import data_dir
Expand All @@ -41,7 +41,7 @@ def preprocess_image_backend(bindir, params, env):
enable_gluster = params.get("enable_gluster")
gluster_image = params.get("gluster_brick")
if enable_gluster and gluster_image:
return gluster.create_gluster_vol(params)
return utils_storage.create_gluster_vol(params)

return True

Expand All @@ -54,61 +54,6 @@ def postprocess_images(bindir, params):
image_obj.rm_cloned_image(vm_params, vm, image, bindir)


def file_exists(params, filename_path):
"""
Check if image_filename exists.
:param params: Dictionary containing the test parameters.
:param filename_path: path to file
:type filename_path: str
:param root_dir: Base directory for relative filenames.
:type root_dir: str
:return: True if image file exists else False
"""
gluster_image = params.get("gluster_brick")
if gluster_image:
return gluster.file_exists(params, filename_path)

if params.get("enable_ceph") == "yes":
image_name = params.get("image_name")
image_format = params.get("image_format", "qcow2")
ceph_monitor = params["ceph_monitor"]
rbd_pool_name = params["rbd_pool_name"]
rbd_image_name = "%s.%s" % (image_name.split("/")[-1], image_format)
return ceph.rbd_image_exist(ceph_monitor, rbd_pool_name,
rbd_image_name)

return os.path.exists(filename_path)


def file_remove(params, filename_path):
"""
Remove the image
:param params: Dictionary containing the test parameters.
:param filename_path: path to file
"""
if params.get("enable_ceph") == "yes":
image_name = params.get("image_name")
image_format = params.get("image_format", "qcow2")
ceph_monitor = params["ceph_monitor"]
rbd_pool_name = params["rbd_pool_name"]
rbd_image_name = "%s.%s" % (image_name.split("/")[-1], image_format)
return ceph.rbd_image_rm(ceph_monitor, rbd_pool_name, rbd_image_name)

if params.get("gluster_brick"):
# TODO: Add implementation for gluster_brick
return

if params.get('storage_type') in ('iscsi', 'lvm'):
# TODO: Add implementation for iscsi/lvm
return

if os.path.exists(filename_path):
os.unlink(filename_path)
return


def get_image_blkdebug_filename(params, root_dir):
"""
Generate an blkdebug file path from params and root_dir.
Expand Down Expand Up @@ -149,7 +94,8 @@ def get_image_filename(params, root_dir, basename=False):
if enable_gluster:
image_name = params.get("image_name", "image")
image_format = params.get("image_format", "qcow2")
return gluster.get_image_filename(params, image_name, image_format)
return utils_storage.get_gluster_image_filename(params, image_name,
image_format)
if enable_ceph:
image_format = params.get("image_format", "qcow2")
ceph_monitor = params["ceph_monitor"]
Expand Down
133 changes: 130 additions & 3 deletions virttest/utils_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

from virttest import error_context
from virttest import utils_numeric
from virttest import utils_misc
from virttest import data_dir
from virttest import gluster
from virttest import ceph
from virttest.libvirt_xml import pool_xml
from virttest.compat_52lts import decode_to_text
from virttest.compat_52lts import results_stdout_52lts

Expand Down Expand Up @@ -66,8 +71,19 @@ def copytree(src, dst, overwrite=True, ignore=''):
shutil.copy(src_file, dst_dir)


def glusterfs_mount(g_uri, mount_point):
"""
Mount gluster volume to mountpoint.
:param g_uri: stripped gluster uri from create_gluster_uri(.., True)
:type g_uri: str
"""
mount(g_uri, mount_point, "glusterfs", None,
False, "fuse.glusterfs")


def is_mount(src, dst=None, fstype=None, options=None, verbose=False,
session=None):
session=None, fstype_mtab=None):
"""
Check is src or dst mounted.
Expand Down Expand Up @@ -109,7 +125,8 @@ def is_mount(src, dst=None, fstype=None, options=None, verbose=False,
return False


def mount(src, dst, fstype=None, options=None, verbose=False, session=None):
def mount(src, dst, fstype=None, options=None, verbose=False, session=None,
fstype_mtab=None):
"""
Mount src under dst if it's really mounted, then remout with options.
Expand Down Expand Up @@ -137,7 +154,8 @@ def mount(src, dst, fstype=None, options=None, verbose=False, session=None):
return process.system(cmd, verbose=verbose) == 0


def umount(src, dst, fstype=None, verbose=False, session=None):
def umount(src, dst, fstype=None, verbose=False, session=None,
fstype_mtab=None):
"""
Umount src from dst, if src really mounted under dst.
Expand Down Expand Up @@ -1026,6 +1044,115 @@ def get_disk_by_serial(serial_str, session=None):
return disk


def new_disk_vol_name(pool_name):
"""
According to BZ#1138523, the new volume name must be the next
created partition(sdb1, etc.), so we need to inspect the original
partitions of the disk then count the new partition number.
:param pool_name: Disk pool name
:return: New volume name or none
"""
poolxml = pool_xml.PoolXML.new_from_dumpxml(pool_name)
if poolxml.get_type(pool_name) != "disk":
logging.error("This is not a disk pool")
return None
disk = poolxml.get_source().device_path[5:]
part_num = len(list(filter(lambda s: s.startswith(disk),
get_parts_list())))
return disk + str(part_num)


def gluster_file_exists(params, filename_path):
sg_uri = gluster.create_gluster_uri(params, stripped=True)
g_uri = gluster.create_gluster_uri(params, stripped=False)
# Using directly /tmp dir because directory should be really temporary and
# should be deleted immediately when no longer needed and
# created directory don't file tmp dir by any data.
tmpdir = "gmount-%s" % (utils_misc.generate_random_string(6))
tmpdir_path = os.path.join(data_dir.get_tmp_dir(), tmpdir)
while os.path.exists(tmpdir_path):
tmpdir = "gmount-%s" % (utils_misc.generate_random_string(6))
tmpdir_path = os.path.join(data_dir.get_tmp_dir(), tmpdir)
ret = False
try:
try:
os.mkdir(tmpdir_path)
glusterfs_mount(sg_uri, tmpdir_path)
mount_filename_path = os.path.join(tmpdir_path,
filename_path[len(g_uri):])
if os.path.exists(mount_filename_path):
ret = True
except Exception as e:
logging.error("Failed to mount gluster volume %s to"
" mount dir %s: %s" % (sg_uri, tmpdir_path, e))
finally:
if umount(sg_uri, tmpdir_path, "glusterfs", False, "fuse.glusterfs"):
try:
os.rmdir(tmpdir_path)
except OSError:
pass
else:
logging.warning("Unable to unmount tmp directory %s with glusterfs"
" mount.", tmpdir_path)
return ret


def file_remove(params, filename_path):
"""
Remove the image
:param params: Dictionary containing the test parameters.
:param filename_path: path to file
"""
if params.get("enable_ceph") == "yes":
image_name = params.get("image_name")
image_format = params.get("image_format", "qcow2")
ceph_monitor = params["ceph_monitor"]
rbd_pool_name = params["rbd_pool_name"]
rbd_image_name = "%s.%s" % (image_name.split("/")[-1], image_format)
return ceph.rbd_image_rm(ceph_monitor, rbd_pool_name, rbd_image_name)

if params.get("gluster_brick"):
# TODO: Add implementation for gluster_brick
return

if params.get('storage_type') in ('iscsi', 'lvm'):
# TODO: Add implementation for iscsi/lvm
return

if os.path.exists(filename_path):
os.unlink(filename_path)
return


def file_exists(params, filename_path):
"""
Check if image_filename exists.
:param params: Dictionary containing the test parameters.
:param filename_path: path to file
:type filename_path: str
:param root_dir: Base directory for relative filenames.
:type root_dir: str
:return: True if image file exists else False
"""
gluster_image = params.get("gluster_brick")
if gluster_image:
return gluster_file_exists(params, filename_path)

if params.get("enable_ceph") == "yes":
image_name = params.get("image_name")
image_format = params.get("image_format", "qcow2")
ceph_monitor = params["ceph_monitor"]
rbd_pool_name = params["rbd_pool_name"]
rbd_image_name = "%s.%s" % (image_name.split("/")[-1], image_format)
return ceph.rbd_image_exist(ceph_monitor, rbd_pool_name,
rbd_image_name)

return os.path.exists(filename_path)


class Disk(object):

"""
Expand Down
Loading

0 comments on commit 86817e1

Please sign in to comment.