Skip to content

Commit

Permalink
utils_misc: 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 to utils_disk for better work flow.

Signed-off-by: Srikanth Aithal <[email protected]>
Signed-off-by: Balamuruhan S <[email protected]>
  • Loading branch information
balamuruhans committed Aug 21, 2019
1 parent 5c83fe5 commit 3b60e46
Show file tree
Hide file tree
Showing 8 changed files with 380 additions and 420 deletions.
20 changes: 10 additions & 10 deletions selftests/unit/test_nfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from virttest.unittest_utils import mock
from virttest import nfs
from virttest import utils_misc
from virttest import utils_disk
from virttest.staging import service


Expand Down Expand Up @@ -58,16 +58,16 @@ def setup_stubs_setup(self, nfs_obj):
os.makedirs.expect_call(nfs_obj.export_dir)
nfs_obj.exportfs.export.expect_call()
os.makedirs.expect_call(nfs_obj.mount_dir)
utils_misc.mount.expect_call(nfs_obj.mount_src, nfs_obj.mount_dir,
"nfs", perm=nfs_obj.mount_options)
utils_disk.mount.expect_call(nfs_obj.mount_src, nfs_obj.mount_dir,
"nfs", nfs_obj.mount_options)

def setup_stubs_is_mounted(self, nfs_obj):
utils_misc.is_mounted.expect_call(nfs_obj.mount_src,
nfs_obj.mount_dir,
"nfs").and_return(True)
utils_disk.is_mount.expect_call(nfs_obj.mount_src,
nfs_obj.mount_dir,
"nfs").and_return(True)

def setup_stubs_cleanup(self, nfs_obj):
utils_misc.umount.expect_call(nfs_obj.mount_src,
utils_disk.umount.expect_call(nfs_obj.mount_src,
nfs_obj.mount_dir,
"nfs")
nfs_obj.exportfs.reset_export.expect_call()
Expand All @@ -84,9 +84,9 @@ def setUp(self):
self.god.stub_function(process, "system_output")
self.god.stub_function(os.path, "isfile")
self.god.stub_function(os, "makedirs")
self.god.stub_function(utils_misc, "is_mounted")
self.god.stub_function(utils_misc, "mount")
self.god.stub_function(utils_misc, "umount")
self.god.stub_function(utils_disk, "is_mount")
self.god.stub_function(utils_disk, "mount")
self.god.stub_function(utils_disk, "umount")
self.god.stub_function(service.Factory, "create_service")
attr = getattr(nfs, "Exportfs")
setattr(attr, "already_exported", False)
Expand Down
113 changes: 0 additions & 113 deletions virttest/gluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
"""

import logging
import os
import re
import socket

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 @@ -204,80 +201,6 @@ def gluster_brick_delete(brick_path, session=None):
logging.error("Not able to delete brick folder %s", details)


@error_context.context_aware
def gluster_vol_create(vol_name, hostname, brick_path, force=False, session=None):
"""
Gluster Volume Creation
:param vol_name: Name of gluster volume
:param hostname: hostname to create gluster volume
:param force: Boolean for adding force option or not
"""
# Create a brick
if is_gluster_vol_avail(vol_name, session):
gluster_vol_stop(vol_name, True, session)
gluster_vol_delete(vol_name, session)
gluster_brick_delete(brick_path, session)

gluster_brick_create(brick_path, session=session)

if force:
force_opt = "force"
else:
force_opt = ""

cmd = "gluster volume create %s %s:/%s %s" % (vol_name, hostname,
brick_path, force_opt)
error_context.context("Volume creation failed")
if session:
session.cmd(cmd)
else:
process.system(cmd)
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,42 +230,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
Expand Down
9 changes: 5 additions & 4 deletions virttest/nfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from virttest import utils_misc
from virttest import test_setup
from virttest import utils_disk
from virttest.utils_iptables import Iptables
from virttest.utils_conn import SSHConnection
from virttest.compat_52lts import results_stdout_52lts, results_stderr_52lts
Expand Down Expand Up @@ -212,20 +213,20 @@ def is_mounted(self):
:return: If the src is mounted as expect
:rtype: Boolean
"""
return utils_misc.is_mounted(self.mount_src, self.mount_dir, "nfs")
return utils_disk.is_mount(self.mount_src, self.mount_dir, "nfs")

def mount(self):
"""
Mount source into given mount point.
"""
return utils_misc.mount(self.mount_src, self.mount_dir, "nfs",
perm=self.mount_options)
return utils_disk.mount(self.mount_src, self.mount_dir, "nfs",
self.mount_options)

def umount(self):
"""
Umount the given mount point.
"""
return utils_misc.umount(self.mount_src, self.mount_dir, "nfs")
return utils_disk.umount(self.mount_src, self.mount_dir, "nfs")

def setup(self):
"""
Expand Down
3 changes: 2 additions & 1 deletion virttest/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from avocado.core import exceptions
from avocado.utils import process
from avocado.utils import data_factory

from virttest import iscsi
from virttest import utils_misc
Expand Down Expand Up @@ -395,7 +396,7 @@ def get_backup_set(filename, backup_dir, action, good):
# of the good image
src_bad = filename
src_good = os.path.join(backup_dir, "%s.backup" % basename)
hsh = utils_misc.generate_random_string(4)
hsh = data_factory.generate_random_string(4)
dst_bad = (os.path.join(backup_dir, "%s.bad.%s" %
(basename, hsh)))
dst_good = (os.path.join(backup_dir, "%s.good.%s" %
Expand Down
7 changes: 4 additions & 3 deletions virttest/tests/unattended_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from avocado.utils import process
from avocado.utils import crypto
from avocado.utils import download
from avocado.utils import wait

from virttest import virt_vm
from virttest import asset
Expand Down Expand Up @@ -1129,10 +1130,10 @@ def terminate_syslog_server_thread():
def copy_file_from_nfs(src, dst, mount_point, image_name):
logging.info("Test failed before the install process start."
" So just copy a good image from nfs for following tests.")
utils_misc.mount(src, mount_point, "nfs", perm="ro")
utils_disk.mount(src, mount_point, "nfs", "ro")
image_src = utils_misc.get_path(mount_point, image_name)
shutil.copy(image_src, dst)
utils_misc.umount(src, mount_point, "nfs")
utils_disk.umount(src, mount_point, "nfs")


def string_in_serial_log(serial_log_file_path, string):
Expand Down Expand Up @@ -1478,7 +1479,7 @@ def copy_images():
if params.get("medium", "cdrom") == "import":
vm.shutdown()
try:
if utils_misc.wait_for(vm.is_dead, shutdown_cleanly_timeout, 1, 1):
if wait.wait_for(vm.is_dead, shutdown_cleanly_timeout, 1, 1):
logging.info("Guest managed to shutdown cleanly")
except qemu_monitor.MonitorError as e:
logging.warning("Guest apparently shut down, but got a "
Expand Down
Loading

0 comments on commit 3b60e46

Please sign in to comment.