Skip to content

Commit

Permalink
virttest: use avocado.utils APIs instead of utils_misc
Browse files Browse the repository at this point in the history
remove API dependency on utils_misc and use avocado utils
instead

Signed-off-by: Balamuruhan S <[email protected]>
  • Loading branch information
balamuruhans committed Aug 21, 2019
1 parent 3b60e46 commit a4058a7
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 88 deletions.
11 changes: 6 additions & 5 deletions selftests/unit/test_libvirt_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import six

from avocado.utils import process
from avocado.utils import data_factory


try:
Expand All @@ -22,7 +23,7 @@
if os.path.isdir(os.path.join(basedir, 'virttest')):
sys.path.append(basedir)

from virttest import xml_utils, utils_misc, data_dir
from virttest import xml_utils, data_dir
from virttest.libvirt_xml import accessors, vm_xml, xcepts, network_xml, base
from virttest.libvirt_xml import nodedev_xml
from virttest.libvirt_xml.devices import librarian
Expand Down Expand Up @@ -814,10 +815,10 @@ def test_arbitrart_attributes(self):
channel = librarian.get('channel')(virsh_instance=self.dummy_virsh)
console = librarian.get('console')(virsh_instance=self.dummy_virsh)
for chardev in (parallel, serial, channel, console):
attribute1 = utils_misc.generate_random_string(10)
value1 = utils_misc.generate_random_string(10)
attribute2 = utils_misc.generate_random_string(10)
value2 = utils_misc.generate_random_string(10)
attribute1 = data_factory.generate_random_string(10)
value1 = data_factory.generate_random_string(10)
attribute2 = data_factory.generate_random_string(10)
value2 = data_factory.generate_random_string(10)
chardev.add_source(**{attribute1: value1, attribute2: value2})
chardev.add_target(**{attribute1: value1, attribute2: value2})
self.assertEqual(chardev.sources, chardev.targets)
Expand Down
11 changes: 6 additions & 5 deletions selftests/unit/test_utils_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
if os.path.isdir(os.path.join(basedir, 'virttest')):
sys.path.append(basedir)

from avocado.utils import data_factory

from virttest import utils_env
from virttest import utils_params
from virttest import utils_misc


class FakeVm(object):
Expand All @@ -25,7 +26,7 @@ def __init__(self, vm_name, params):
self.driver_type = self.params.get('driver_type')
self.instance = ("%s-%s" % (
time.strftime("%Y%m%d-%H%M%S"),
utils_misc.generate_random_string(16)))
data_factory.generate_random_string(16)))

def get_params(self):
return self.params
Expand All @@ -39,7 +40,7 @@ class FakeSyncListenServer(object):
def __init__(self, address='', port=123, tmpdir=None):
self.instance = ("%s-%s" % (
time.strftime("%Y%m%d-%H%M%S"),
utils_misc.generate_random_string(16)))
data_factory.generate_random_string(16)))
self.port = port

def close(self):
Expand Down Expand Up @@ -203,8 +204,8 @@ def _update_env(env, key, value):
if "changing_dict" not in env:
env["changing_dict"] = {}
while True:
key = "%s" % utils_misc.generate_random_string(length=10)
value = "%s" % utils_misc.generate_random_string(length=10)
key = "%s" % data_factory.generate_random_string(length=10)
value = "%s" % data_factory.generate_random_string(length=10)
_update_env(env, key, value)
if termination_event.isSet():
break
Expand Down
20 changes: 10 additions & 10 deletions selftests/unit/test_utils_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import shelve

from avocado.utils import process
from avocado.utils import data_factory

from six.moves import xrange

Expand All @@ -21,7 +22,6 @@

from virttest.unittest_utils import mock
from virttest import utils_net
from virttest import utils_misc
from virttest import cartesian_config
from virttest import utils_params
from virttest import propcan
Expand All @@ -40,7 +40,7 @@ def __init__(self, vm_name, params):
self.driver_type = self.params.get('driver_type')
self.instance = ("%s-%s" % (
time.strftime("%Y%m%d-%H%M%S"),
utils_misc.generate_random_string(16)))
data_factory.generate_random_string(16)))

def get_params(self):
return self.params
Expand Down Expand Up @@ -152,7 +152,7 @@ def what_func(propertea):
half_prop_end = (len(self.VirtIface.__all_slots__) // 2) + 1
props = {}
for propertea in self.VirtIface.__all_slots__[0:half_prop_end]:
props[propertea] = utils_misc.generate_random_string(16)
props[propertea] = data_factory.generate_random_string(16)
virtiface = self.VirtIface(props)
self.loop_assert(virtiface, list(props.keys()), what_func)

Expand All @@ -161,7 +161,7 @@ def what_func(propertea):
return props[propertea]
props = {}
for propertea in self.VirtIface.__all_slots__:
props[propertea] = utils_misc.generate_random_string(16)
props[propertea] = data_factory.generate_random_string(16)
virtiface = self.VirtIface(props)
self.loop_assert(virtiface, list(props.keys()), what_func)

Expand All @@ -173,11 +173,11 @@ def what_func(propertea):
return props[propertea]
props = {}
for propertea in self.VirtIface.__all_slots__:
props[propertea] = utils_misc.generate_random_string(16)
props[propertea] = data_factory.generate_random_string(16)
more_props = {}
for _ in xrange(0, 16):
key = utils_misc.generate_random_string(16)
value = utils_misc.generate_random_string(16)
key = data_factory.generate_random_string(16)
value = data_factory.generate_random_string(16)
more_props[key] = value
# Keep separated for testing
apendex_set = {}
Expand Down Expand Up @@ -225,15 +225,15 @@ def get_style(self, vm_type, driver_type):
return utils_net.VMNetStyle.get_style(vm_type, driver_type)

def test_default_default(self):
style = self.get_style(utils_misc.generate_random_string(16),
utils_misc.generate_random_string(16))
style = self.get_style(data_factory.generate_random_string(16),
data_factory.generate_random_string(16))
self.assertEqual(style['mac_prefix'], '9a')
self.assertEqual(style['container_class'], utils_net.QemuIface)
self.assert_(issubclass(style['container_class'], utils_net.VirtIface))

def test_libvirt(self):
style = self.get_style('libvirt',
utils_misc.generate_random_string(16))
data_factory.generate_random_string(16))
self.assertEqual(style['container_class'], utils_net.LibvirtIface)
self.assert_(issubclass(style['container_class'], utils_net.VirtIface))

Expand Down
5 changes: 3 additions & 2 deletions virttest/env_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from avocado.utils import path
from avocado.utils import distro
from avocado.utils import cpu as cpu_utils
from avocado.utils import data_factory
from avocado.core import exceptions

import six
Expand Down Expand Up @@ -354,7 +355,7 @@ def check_image(test, params, image_name, vm_process_status=None):
# It should before image check.
if params.get("save_image", "no") == "yes":
if vm_process_status == "dead":
hsh = utils_misc.generate_random_string(4)
hsh = data_factory.generate_random_string(4)
name = ("JOB-%s-TEST-%s-%s-%s.%s" % (
test.job.unique_id[:7], str(test.name.uid),
image_name, hsh, image.image_format))
Expand Down Expand Up @@ -1526,7 +1527,7 @@ def _take_screendumps(test, params, env):
os.makedirs(temp_dir)
except OSError:
pass
random_id = utils_misc.generate_random_string(6)
random_id = data_factory.generate_random_string(6)
temp_filename = "scrdump-%s-iter%s.ppm" % (random_id, test.iteration)
temp_filename = os.path.join(temp_dir, temp_filename)
delay = float(params.get("screendump_delay", 5))
Expand Down
28 changes: 15 additions & 13 deletions virttest/libvirt_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import aexpect
from avocado.utils import process
from avocado.utils import crypto
from avocado.utils import data_factory
from avocado.utils import wait
from avocado.core import exceptions

from virttest import error_context
Expand Down Expand Up @@ -1361,7 +1363,7 @@ def get_serial_console_filename(self, name):
:param name: The serial port name.
"""
return "serial-%s-%s-%s.log" % (name, self.name,
utils_misc.generate_random_string(4))
data_factory.generate_random_string(4))

def get_serial_console_filenames(self):
"""
Expand Down Expand Up @@ -2016,9 +2018,9 @@ def create(self, name=None, params=None, root_dir=None, timeout=5.0,
# some other problem happened, raise normally
raise
# Wait for the domain to be created
utils_misc.wait_for(func=self.is_alive, timeout=60,
text=("waiting for domain %s to start" %
self.name))
wait.wait_for(func=self.is_alive, timeout=60,
text=("waiting for domain %s to start" %
self.name))
result = virsh.domuuid(self.name, uri=self.connect_uri)
self.uuid = results_stdout_52lts(result).strip()
# Create isa serial ports.
Expand Down Expand Up @@ -2159,7 +2161,7 @@ def destroy(self, gracefully=True, free_mac_addresses=True):
self.params.get("shutdown_command"))
logging.debug("Shutdown command sent; waiting for VM "
"to go down...")
if utils_misc.wait_for(self.is_dead, 60, 1, 1):
if wait.wait_for(self.is_dead, 60, 1, 1):
logging.debug("VM is down")
return
finally:
Expand Down Expand Up @@ -2391,9 +2393,9 @@ def reboot(self, session=None, method="shell", nic_index=0, timeout=240,
raise virt_vm.VMRebootError("Unknown reboot method: %s" % method)

error_context.context("waiting for guest to go down", logging.info)
if not utils_misc.wait_for(lambda: not
session.is_responsive(timeout=30),
120, 0, 1):
if not wait.wait_for(lambda: not
session.is_responsive(timeout=30),
120, 0, 1):
raise virt_vm.VMRebootError("Guest refuses to go down")
session.close()

Expand All @@ -2418,9 +2420,9 @@ def start(self, autoconsole=True):
result = virsh.start(self.name, uri=self.connect_uri)
if not result.exit_status:
# Wait for the domain to be created
has_started = utils_misc.wait_for(func=self.is_alive, timeout=60,
text=("waiting for domain %s "
"to start" % self.name))
has_started = wait.wait_for(func=self.is_alive, timeout=60,
text=("waiting for domain %s "
"to start" % self.name))
if has_started is None:
raise virt_vm.VMStartError(self.name, "libvirt domain not "
"active after start")
Expand Down Expand Up @@ -2962,10 +2964,10 @@ def _stop_ga():
_stop_ga()
# Check qemu-guest-agent status
if start:
if not utils_misc.wait_for(_is_ga_running, timeout=60):
if not wait.wait_for(_is_ga_running, timeout=60):
raise virt_vm.VMError("qemu-guest-agent is not running.")
else:
if not utils_misc.wait_for(_is_ga_finished, timeout=60):
if not wait.wait_for(_is_ga_finished, timeout=60):
raise virt_vm.VMError("qemu-guest-agent is running")
finally:
session.close()
Expand Down
4 changes: 3 additions & 1 deletion virttest/qemu_devices/qdevices.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from collections import OrderedDict
from functools import reduce

from avocado.utils import data_factory

from virttest import qemu_monitor
from virttest import utils_misc
from virttest.qemu_devices.utils import DeviceError
Expand Down Expand Up @@ -43,7 +45,7 @@ def _build_cmd(cmd, args=None, q_id=None):
if args is not None:
obj["arguments"] = args
if q_id is True:
obj["id"] = utils_misc.generate_random_string(8)
obj["id"] = data_factory.generate_random_string(8)
elif q_id is not None:
obj["id"] = q_id
return obj
Expand Down
3 changes: 2 additions & 1 deletion virttest/qemu_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import aexpect
from avocado.utils import process
from avocado.utils import data_factory

from virttest import utils_misc
from virttest import error_context
Expand All @@ -27,7 +28,7 @@ def __init__(self, test, params, image_name, blkdebug_cfg="",
log_func=None):
self.type = ""
if log_filename:
log_filename += "-" + utils_misc.generate_random_string(4)
log_filename += "-" + data_factory.generate_random_string(4)
self.output_func = utils_misc.log_line
self.output_params = (log_filename,)
else:
Expand Down
13 changes: 8 additions & 5 deletions virttest/qemu_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
logging.warning("Could not import json module. "
"QMP monitor functionality disabled.")

from avocado.utils import wait
from avocado.utils import data_factory

from . import passfd_setup
from . import utils_misc
from . import cartesian_config
Expand Down Expand Up @@ -1914,7 +1917,7 @@ def cmd(self, cmd, args=None, timeout=CMD_TIMEOUT, debug=True, fd=None):
# Read any data that might be available
self._read_objects()
# Send command
q_id = utils_misc.generate_random_string(8)
q_id = data_factory.generate_random_string(8)
cmdobj = self._build_cmd(cmd, args, q_id)
if debug:
logging.debug("Send command: %s" % cmdobj)
Expand Down Expand Up @@ -2247,7 +2250,7 @@ def system_reset(self):
self.verify_supported_cmd(cmd)
self.clear_event(event)
ret = self.cmd(cmd=cmd)
if not utils_misc.wait_for(lambda: self.get_event(event), timeout=120):
if not wait.wait_for(lambda: self.get_event(event), timeout=120):
raise QMPEventError(cmd, event, self.vm.name, self.name)
return ret

Expand Down Expand Up @@ -2593,7 +2596,7 @@ def system_wakeup(self):
# Send a system_wakeup monitor command
self.cmd(cmd)
# Look for WAKEUP QMP event
if not utils_misc.wait_for(lambda: self.get_event(qmp_event), 120):
if not wait.wait_for(lambda: self.get_event(qmp_event), 120):
raise QMPEventError(cmd, qmp_event, self.vm.name, self.name)
logging.info("%s QMP event received" % qmp_event)

Expand Down Expand Up @@ -2835,7 +2838,7 @@ def balloon(self, size):
# Send a balloon monitor command
self.send_args_cmd("%s value=%s" % (cmd, size))
# Look for BALLOON QMP events
if not utils_misc.wait_for(lambda: self.get_event(qmp_event), 120):
if not wait.wait_for(lambda: self.get_event(qmp_event), 120):
raise QMPEventError(cmd, qmp_event, self.vm.name, self.name)
logging.info("%s QMP event received" % qmp_event)

Expand Down Expand Up @@ -3014,7 +3017,7 @@ def system_powerdown(self):
# Send a powerdown monitor command
self.cmd(cmd)
# Look for POWERDOWN QMP events
if not utils_misc.wait_for(lambda: self.get_event(qmp_event), 120):
if not wait.wait_for(lambda: self.get_event(qmp_event), 120):
raise QMPEventError(cmd, qmp_event, self.vm.name, self.name)
logging.info("%s QMP event received" % qmp_event)

Expand Down
Loading

0 comments on commit a4058a7

Please sign in to comment.