diff --git a/CODING_STYLE b/CODING_STYLE index b6a04bd2b9..cb1028c927 100644 --- a/CODING_STYLE +++ b/CODING_STYLE @@ -13,24 +13,9 @@ maintainable. (Obviously using C or whatever for writing tests is fine). Base coding style -When writing python code, unless otherwise stated, stick to the python style -guide (http://www.python.org/dev/peps/pep-0008/). We now ship a script, -run_pep8.py, that can verify non compliances to the PEP8, with some of -the few exceptions we allow people to do. If you see that the script -finds problems with your code, we expect you to fix them before you -send pull requests. In order for the script to be useful you'll have to -use the tools provided by your distro to install the programs 'pep8' and -'autopep8'. - - -Indentation & whitespace - -Format your code for an 80 character wide screen. - -Indentation is now 4 spaces, as opposed to hard tabs (which it used to be). -This is the Python standard. - -Don't leave trailing whitespace, or put whitespace on blank lines. +The coding style for all Python code is the one enforced by black (see +https://black.readthedocs.io/en/stable/the_black_code_style/). The +selftests/style.sh script can be used to verify the code style matches. Variable names and UpPeR cAsE diff --git a/Makefile b/Makefile index 2e5f11c34a..84e1b5438d 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,8 @@ requirements: pip - $(PYTHON) -m pip install -r requirements.txt check: - inspekt checkall --disable-lint W,R,C,E0203,E0601,E1002,E1101,E1102,E1103,E1120,F0401,I0011,E1003,W605,I1101 --disable-style W605,W606,E501,E265,W601,E402,E722,E741 --exclude avocado-libs,scripts/github --no-license-check + ./selftests/style.sh + inspekt lint --disable W,R,C,E0203,E0601,E1002,E1101,E1102,E1103,E1120,F0401,I0011,E1003,W605,I1101 --exclude avocado-libs,scripts/github pylint --errors-only --disable=all --enable=spelling --spelling-dict=en_US --spelling-private-dict-file=spell.ignore * clean: diff --git a/avocado_vt/discovery.py b/avocado_vt/discovery.py index 06348ace6f..60ffa1acb0 100644 --- a/avocado_vt/discovery.py +++ b/avocado_vt/discovery.py @@ -22,19 +22,19 @@ class DiscoveryMixIn: - def _get_parser(self): options_processor = VirtTestOptionsProcess(self.config) return options_processor.get_parser() def _save_parser_cartesian_config(self, parser): - path = get_opt(self.config, 'vt.save_config') + path = get_opt(self.config, "vt.save_config") if path is None: return - with open(path, 'w') as cartesian_config: + with open(path, "w") as cartesian_config: cartesian_config.write("include %s\n" % parser.filename) - for statement in (parser.only_filters + parser.no_filters + - parser.assignments): + for statement in ( + parser.only_filters + parser.no_filters + parser.assignments + ): cartesian_config.write("%s\n" % statement) def convert_parameters(self, params): @@ -46,19 +46,18 @@ def convert_parameters(self, params): :return: dict with test name and vt parameters """ test_name = params.get("_short_name_map_file")["subtests.cfg"] - if (get_opt(self.config, 'vt.config') - and get_opt(self.config, 'vt.short_names_when_config')): + if get_opt(self.config, "vt.config") and get_opt( + self.config, "vt.short_names_when_config" + ): test_name = params.get("shortname") - elif get_opt(self.config, 'vt.type') == "spice": + elif get_opt(self.config, "vt.type") == "spice": short_name_map_file = params.get("_short_name_map_file") if "tests-variants.cfg" in short_name_map_file: - test_name = short_name_map_file.get("tests-variants.cfg", - test_name) + test_name = short_name_map_file.get("tests-variants.cfg", test_name) # We want avocado to inject params coming from its multiplexer into # the test params. This will allow users to access avocado params # from inside virt tests. This feature would only work if the virt # test in question is executed from inside avocado. - params['id'] = test_name - test_parameters = {'name': test_name, - 'vt_params': params} + params["id"] = test_name + test_parameters = {"name": test_name, "vt_params": params} return test_parameters diff --git a/avocado_vt/loader.py b/avocado_vt/loader.py index 47761547bb..32bb2ec87f 100644 --- a/avocado_vt/loader.py +++ b/avocado_vt/loader.py @@ -33,6 +33,7 @@ try: from avocado.core import loader + AVOCADO_LOADER_AVAILABLE = True except ImportError: AVOCADO_LOADER_AVAILABLE = False @@ -45,23 +46,24 @@ def guest_listing(config, guest_name_parser=None): """ List available guest operating systems and info about image availability """ - if get_opt(config, 'vt.type') == 'lvsb': + if get_opt(config, "vt.type") == "lvsb": raise ValueError("No guest types available for lvsb testing") - LOG.debug("Using %s for guest images\n", - os.path.join(data_dir.get_data_dir(), 'images')) + LOG.debug( + "Using %s for guest images\n", os.path.join(data_dir.get_data_dir(), "images") + ) LOG.info("Available guests in config:") if guest_name_parser is None: guest_name_parser = standalone_test.get_guest_name_parser(config) for params in guest_name_parser.get_dicts(): base_dir = params.get("images_base_dir", data_dir.get_data_dir()) image_name = storage.get_image_filename(params, base_dir) - machine_type = get_opt(config, 'vt.common.machine_type') - name = params['name'].replace('.%s' % machine_type, '') + machine_type = get_opt(config, "vt.common.machine_type") + name = params["name"].replace(".%s" % machine_type, "") if os.path.isfile(image_name): out = name else: missing = "(missing %s)" % os.path.basename(image_name) - out = (name + " " + output.TERM_SUPPORT.warn_header_str(missing)) + out = name + " " + output.TERM_SUPPORT.warn_header_str(missing) LOG.debug(out) LOG.debug("") @@ -70,17 +72,17 @@ def arch_listing(config, guest_name_parser=None): """ List available machine/archs for given guest operating systems """ - guest_os = get_opt(config, 'vt.guest_os') + guest_os = get_opt(config, "vt.guest_os") if guest_os is not None: - extra = " for guest os \"%s\"" % guest_os + extra = ' for guest os "%s"' % guest_os else: extra = "" LOG.info("Available arch profiles%s", extra) if guest_name_parser is None: guest_name_parser = standalone_test.get_guest_name_parser(config) - machine_type = get_opt(config, 'vt.common.machine_type') + machine_type = get_opt(config, "vt.common.machine_type") for params in guest_name_parser.get_dicts(): - LOG.debug(params['name'].replace('.%s' % machine_type, '')) + LOG.debug(params["name"].replace(".%s" % machine_type, "")) LOG.debug("") @@ -92,13 +94,14 @@ class NotAvocadoVTTest(object): if AVOCADO_LOADER_AVAILABLE: + class VirtTestLoader(loader.TestLoader, DiscoveryMixIn): """ Avocado loader plugin to load avocado-vt tests """ - name = 'vt' + name = "vt" def __init__(self, config, extra_params): """ @@ -111,32 +114,32 @@ def __init__(self, config, extra_params): # Avocado has renamed "args" to "config" in 84ae9a5d61, lets # keep making the old name available for compatibility with # new and old releases - if hasattr(self, 'config'): - self.args = self.config # pylint: disable=E0203 + if hasattr(self, "config"): + self.args = self.config # pylint: disable=E0203 # And in case an older Avocado is used, the Loader class will # contain an "args" attribute instead else: - self.config = self.args # pylint: disable=E0203 + self.config = self.args # pylint: disable=E0203 if vt_extra_params: # We don't want to override the original config self.config = copy.deepcopy(self.config) - extra = get_opt(self.config, 'vt.extra_params') + extra = get_opt(self.config, "vt.extra_params") if extra is not None: extra += vt_extra_params else: extra = vt_extra_params - set_opt(self.config, 'vt.extra_params', extra) + set_opt(self.config, "vt.extra_params", extra) def get_extra_listing(self): - if get_opt(self.config, 'vt.list_guests'): + if get_opt(self.config, "vt.list_guests"): config = copy.copy(self.config) - set_opt(config, 'vt.config', None) - set_opt(config, 'vt.guest_os', None) + set_opt(config, "vt.config", None) + set_opt(config, "vt.guest_os", None) guest_listing(config) - if get_opt(self.config, 'vt.list_archs'): + if get_opt(self.config, "vt.list_archs"): config = copy.copy(self.config) - set_opt(config, 'vt.common.machine_type', None) - set_opt(config, 'vt.common.arch', None) + set_opt(config, "vt.common.machine_type", None) + set_opt(config, "vt.common.arch", None) arch_listing(config) @staticmethod @@ -147,7 +150,7 @@ def get_type_label_mapping(): :returns: a dictionary with the test class as key and description as value. """ - return {VirtTest: 'VT', NotAvocadoVTTest: "!VT"} + return {VirtTest: "VT", NotAvocadoVTTest: "!VT"} @staticmethod def get_decorator_mapping(): @@ -158,8 +161,10 @@ def get_decorator_mapping(): function as value. """ term_support = output.TermSupport() - return {VirtTest: term_support.healthy_str, - NotAvocadoVTTest: term_support.fail_header_str} + return { + VirtTest: term_support.healthy_str, + NotAvocadoVTTest: term_support.fail_header_str, + } @staticmethod def _report_bad_discovery(name, reason, which_tests): @@ -184,8 +189,9 @@ def discover(self, url, which_tests=loader.DiscoverMode.DEFAULT): # the other test plugins to handle the URL. except cartesian_config.ParserError as details: return self._report_bad_discovery(url, details, which_tests) - elif (which_tests is loader.DiscoverMode.DEFAULT and - not get_opt(self.config, 'vt.config')): + elif which_tests is loader.DiscoverMode.DEFAULT and not get_opt( + self.config, "vt.config" + ): # By default don't run anything unless vt.config provided return [] # Create test_suite @@ -193,6 +199,5 @@ def discover(self, url, which_tests=loader.DiscoverMode.DEFAULT): for params in (_ for _ in cartesian_parser.get_dicts()): test_suite.append((VirtTest, self.convert_parameters(params))) if which_tests is loader.DiscoverMode.ALL and not test_suite: - return self._report_bad_discovery(url, "No matching tests", - which_tests) + return self._report_bad_discovery(url, "No matching tests", which_tests) return test_suite diff --git a/avocado_vt/options.py b/avocado_vt/options.py index 9c8810de4c..6412f798b0 100644 --- a/avocado_vt/options.py +++ b/avocado_vt/options.py @@ -34,7 +34,7 @@ from virttest.standalone_test import SUPPORTED_TEST_TYPES -LOG = logging.getLogger('avocado.vt.options') +LOG = logging.getLogger("avocado.vt.options") class VirtTestOptionsProcess(object): @@ -52,70 +52,54 @@ def __init__(self, config): # Doing this makes things configurable yet the number of options # is not overwhelming. # setup section - set_opt_from_settings(self.config, - 'vt.setup', 'backup_image_before_test', - key_type=bool, default=True) - set_opt_from_settings(self.config, - 'vt.setup', 'restore_image_after_test', - key_type=bool, default=True) - set_opt_from_settings(self.config, - 'vt.setup', 'keep_guest_running', - key_type=bool, default=False) + set_opt_from_settings( + self.config, + "vt.setup", + "backup_image_before_test", + key_type=bool, + default=True, + ) + set_opt_from_settings( + self.config, + "vt.setup", + "restore_image_after_test", + key_type=bool, + default=True, + ) + set_opt_from_settings( + self.config, "vt.setup", "keep_guest_running", key_type=bool, default=False + ) # common section - set_opt_from_settings(self.config, - 'vt.common', 'data_dir', - default=None) - set_opt_from_settings(self.config, - 'vt.common', 'tmp_dir', - default='') - set_opt_from_settings(self.config, - 'vt.common', 'type_specific', - key_type=bool, default=False) - set_opt_from_settings(self.config, - 'vt.common', 'mem', - default=None) - set_opt_from_settings(self.config, - 'vt.common', 'nettype', - default=None) - set_opt_from_settings(self.config, - 'vt.common', 'netdst', - default='virbr0') + set_opt_from_settings(self.config, "vt.common", "data_dir", default=None) + set_opt_from_settings(self.config, "vt.common", "tmp_dir", default="") + set_opt_from_settings( + self.config, "vt.common", "type_specific", key_type=bool, default=False + ) + set_opt_from_settings(self.config, "vt.common", "mem", default=None) + set_opt_from_settings(self.config, "vt.common", "nettype", default=None) + set_opt_from_settings(self.config, "vt.common", "netdst", default="virbr0") # qemu section - set_opt_from_settings(self.config, - 'vt.qemu', 'accel', - default='kvm') - set_opt_from_settings(self.config, - 'vt.qemu', 'vhost', - default='off') - set_opt_from_settings(self.config, - 'vt.qemu', 'monitor', - default=None) - set_opt_from_settings(self.config, - 'vt.qemu', 'smp', - default='2') - set_opt_from_settings(self.config, - 'vt.qemu', 'image_type', - default=SUPPORTED_IMAGE_TYPES[0]) - set_opt_from_settings(self.config, - 'vt.qemu', 'nic_model', - default=SUPPORTED_NIC_MODELS[0]) - set_opt_from_settings(self.config, - 'vt.qemu', 'disk_bus', - default=SUPPORTED_DISK_BUSES[0]) - set_opt_from_settings(self.config, - 'vt.qemu', 'sandbox', - default='on') - set_opt_from_settings(self.config, - 'vt.qemu', 'defconfig', - default='yes') - set_opt_from_settings(self.config, - 'vt.qemu', 'malloc_perturb', - default='yes') + set_opt_from_settings(self.config, "vt.qemu", "accel", default="kvm") + set_opt_from_settings(self.config, "vt.qemu", "vhost", default="off") + set_opt_from_settings(self.config, "vt.qemu", "monitor", default=None) + set_opt_from_settings(self.config, "vt.qemu", "smp", default="2") + set_opt_from_settings( + self.config, "vt.qemu", "image_type", default=SUPPORTED_IMAGE_TYPES[0] + ) + set_opt_from_settings( + self.config, "vt.qemu", "nic_model", default=SUPPORTED_NIC_MODELS[0] + ) + set_opt_from_settings( + self.config, "vt.qemu", "disk_bus", default=SUPPORTED_DISK_BUSES[0] + ) + set_opt_from_settings(self.config, "vt.qemu", "sandbox", default="on") + set_opt_from_settings(self.config, "vt.qemu", "defconfig", default="yes") + set_opt_from_settings(self.config, "vt.qemu", "malloc_perturb", default="yes") # debug section - set_opt_from_settings(self.config, - 'vt.debug', 'no_cleanup', - key_type=bool, default=False) + set_opt_from_settings( + self.config, "vt.debug", "no_cleanup", key_type=bool, default=False + ) self.cartesian_parser = None @@ -123,213 +107,259 @@ def _process_qemu_bin(self): """ Puts the value of the qemu bin option in the cartesian parser command. """ - qemu_bin_setting = ('option --vt-qemu-bin or ' - 'config vt.qemu.qemu_bin') - if (get_opt(self.config, 'vt.config') and - get_opt(self.config, 'vt.qemu.qemu_bin') is None): - LOG.info("Config provided and no %s set. Not trying " - "to automatically set qemu bin.", qemu_bin_setting) + qemu_bin_setting = "option --vt-qemu-bin or " "config vt.qemu.qemu_bin" + if ( + get_opt(self.config, "vt.config") + and get_opt(self.config, "vt.qemu.qemu_bin") is None + ): + LOG.info( + "Config provided and no %s set. Not trying " + "to automatically set qemu bin.", + qemu_bin_setting, + ) else: - (qemu_bin_path, qemu_img_path, qemu_io_path, - qemu_dst_bin_path) = standalone_test.find_default_qemu_paths( - get_opt(self.config, 'vt.qemu.qemu_bin'), - get_opt(self.config, 'vt.qemu.qemu_dst_bin')) + ( + qemu_bin_path, + qemu_img_path, + qemu_io_path, + qemu_dst_bin_path, + ) = standalone_test.find_default_qemu_paths( + get_opt(self.config, "vt.qemu.qemu_bin"), + get_opt(self.config, "vt.qemu.qemu_dst_bin"), + ) self.cartesian_parser.assign("qemu_binary", qemu_bin_path) self.cartesian_parser.assign("qemu_img_binary", qemu_img_path) self.cartesian_parser.assign("qemu_io_binary", qemu_io_path) if qemu_dst_bin_path is not None: - self.cartesian_parser.assign("qemu_dst_binary", - qemu_dst_bin_path) + self.cartesian_parser.assign("qemu_dst_binary", qemu_dst_bin_path) def _process_qemu_img(self): """ Puts the value of the qemu bin option in the cartesian parser command. """ - qemu_img_setting = ('option --vt-qemu-img or ' - 'config vt.qemu.qemu_img') - if (get_opt(self.config, 'vt.config') and - get_opt(self.config, 'vt.qemu.bin') is None): - LOG.info("Config provided and no %s set. Not trying " - "to automatically set qemu bin", qemu_img_setting) + qemu_img_setting = "option --vt-qemu-img or " "config vt.qemu.qemu_img" + if ( + get_opt(self.config, "vt.config") + and get_opt(self.config, "vt.qemu.bin") is None + ): + LOG.info( + "Config provided and no %s set. Not trying " + "to automatically set qemu bin", + qemu_img_setting, + ) else: - (_, qemu_img_path, - _, _) = standalone_test.find_default_qemu_paths( - get_opt(self.config, 'vt.qemu.qemu_bin'), - get_opt(self.config, 'vt.qemu.qemu_dst_bin')) + (_, qemu_img_path, _, _) = standalone_test.find_default_qemu_paths( + get_opt(self.config, "vt.qemu.qemu_bin"), + get_opt(self.config, "vt.qemu.qemu_dst_bin"), + ) self.cartesian_parser.assign("qemu_img_binary", qemu_img_path) def _process_qemu_accel(self): """ Puts the value of the qemu bin option in the cartesian parser command. """ - if get_opt(self.config, 'vt.qemu.accel') == 'tcg': + if get_opt(self.config, "vt.qemu.accel") == "tcg": self.cartesian_parser.assign("disable_kvm", "yes") def _process_bridge_mode(self): - nettype_setting = 'config vt.qemu.nettype' - if not get_opt(self.config, 'vt.config'): + nettype_setting = "config vt.qemu.nettype" + if not get_opt(self.config, "vt.config"): # Let's select reasonable defaults depending on vt.type - if not get_opt(self.config, 'vt.common.nettype'): - if get_opt(self.config, 'vt.type') == 'qemu': - set_opt(self.config, 'vt.common.nettype', - ("bridge" if os.getuid() == 0 else "user")) - elif get_opt(self.config, 'vt.type') == 'spice': - set_opt(self.config, 'vt.common.nettype', "none") + if not get_opt(self.config, "vt.common.nettype"): + if get_opt(self.config, "vt.type") == "qemu": + set_opt( + self.config, + "vt.common.nettype", + ("bridge" if os.getuid() == 0 else "user"), + ) + elif get_opt(self.config, "vt.type") == "spice": + set_opt(self.config, "vt.common.nettype", "none") else: - set_opt(self.config, 'vt.common.nettype', "bridge") - - if get_opt(self.config, 'vt.common.nettype') not in SUPPORTED_NET_TYPES: - raise ValueError("Invalid %s '%s'. " - "Valid values: (%s)" % - (nettype_setting, - get_opt(self.config, 'vt.common.nettype'), - ", ".join(SUPPORTED_NET_TYPES))) - if get_opt(self.config, 'vt.common.nettype') == 'bridge': + set_opt(self.config, "vt.common.nettype", "bridge") + + if get_opt(self.config, "vt.common.nettype") not in SUPPORTED_NET_TYPES: + raise ValueError( + "Invalid %s '%s'. " + "Valid values: (%s)" + % ( + nettype_setting, + get_opt(self.config, "vt.common.nettype"), + ", ".join(SUPPORTED_NET_TYPES), + ) + ) + if get_opt(self.config, "vt.common.nettype") == "bridge": if os.getuid() != 0: - raise ValueError("In order to use %s '%s' you " - "need to be root" % (nettype_setting, - get_opt(self.config, 'vt.common.nettype'))) + raise ValueError( + "In order to use %s '%s' you " + "need to be root" + % (nettype_setting, get_opt(self.config, "vt.common.nettype")) + ) self.cartesian_parser.assign("nettype", "bridge") - self.cartesian_parser.assign("netdst", get_opt(self.config, 'vt.common.netdst')) - elif get_opt(self.config, 'vt.common.nettype') == 'user': + self.cartesian_parser.assign( + "netdst", get_opt(self.config, "vt.common.netdst") + ) + elif get_opt(self.config, "vt.common.nettype") == "user": self.cartesian_parser.assign("nettype", "user") else: LOG.info("Config provided, ignoring %s", nettype_setting) def _process_monitor(self): - if not get_opt(self.config, 'vt.config'): - if not get_opt(self.config, 'vt.qemu.monitor'): + if not get_opt(self.config, "vt.config"): + if not get_opt(self.config, "vt.qemu.monitor"): pass - elif get_opt(self.config, 'vt.qemu.monitor') == 'qmp': + elif get_opt(self.config, "vt.qemu.monitor") == "qmp": self.cartesian_parser.assign("monitor_type", "qmp") - elif get_opt(self.config, 'vt.qemu.monitor') == 'human': + elif get_opt(self.config, "vt.qemu.monitor") == "human": self.cartesian_parser.assign("monitor_type", "human") else: LOG.info("Config provided, ignoring monitor setting") def _process_smp(self): - smp_setting = 'config vt.qemu.smp' - if not get_opt(self.config, 'vt.config'): - if get_opt(self.config, 'vt.qemu.smp') == '1': + smp_setting = "config vt.qemu.smp" + if not get_opt(self.config, "vt.config"): + if get_opt(self.config, "vt.qemu.smp") == "1": self.cartesian_parser.only_filter("up") - elif get_opt(self.config, 'vt.qemu.smp') == '2': + elif get_opt(self.config, "vt.qemu.smp") == "2": self.cartesian_parser.only_filter("smp2") else: try: self.cartesian_parser.only_filter("up") self.cartesian_parser.assign( - "smp", int(get_opt(self.config, 'vt.qemu.smp'))) + "smp", int(get_opt(self.config, "vt.qemu.smp")) + ) except ValueError: - raise ValueError("Invalid %s '%s'. Valid value: (1, 2, " - "or integer)" % get_opt(self.config, 'vt.qemu.smp')) + raise ValueError( + "Invalid %s '%s'. Valid value: (1, 2, " + "or integer)" % get_opt(self.config, "vt.qemu.smp") + ) else: LOG.info("Config provided, ignoring %s", smp_setting) def _process_arch(self): - if get_opt(self.config, 'vt.config'): + if get_opt(self.config, "vt.config"): arch_setting = "option --vt-arch or config vt.common.arch" LOG.info("Config provided, ignoring %s", arch_setting) return - arch = get_opt(self.config, 'vt.common.arch') + arch = get_opt(self.config, "vt.common.arch") if arch: self.cartesian_parser.only_filter(arch) def _process_machine_type(self): - machine_type_setting = ("option --vt-machine-type or config " - "vt.common.machine_type") - if not get_opt(self.config, 'vt.config'): - if get_opt(self.config, 'vt.common.machine_type') is None: + machine_type_setting = ( + "option --vt-machine-type or config " "vt.common.machine_type" + ) + if not get_opt(self.config, "vt.config"): + if get_opt(self.config, "vt.common.machine_type") is None: # TODO: this is x86-specific, instead we can get the default # arch from qemu binary and run on all supported machine types - if ((get_opt(self.config, 'vt.common.arch') is None) and - (get_opt(self.config, 'vt.guest_os') is None)): - self.cartesian_parser.only_filter( - defaults.DEFAULT_MACHINE_TYPE) + if (get_opt(self.config, "vt.common.arch") is None) and ( + get_opt(self.config, "vt.guest_os") is None + ): + self.cartesian_parser.only_filter(defaults.DEFAULT_MACHINE_TYPE) else: - self.cartesian_parser.only_filter(get_opt(self.config, - 'vt.common.machine_type')) + self.cartesian_parser.only_filter( + get_opt(self.config, "vt.common.machine_type") + ) else: LOG.info("Config provided, ignoring %s", machine_type_setting) def _process_image_type(self): - image_type_setting = 'config vt.qemu.image_type' - if not get_opt(self.config, 'vt.config'): - if get_opt(self.config, 'vt.qemu.image_type') in SUPPORTED_IMAGE_TYPES: - self.cartesian_parser.only_filter(get_opt(self.config, 'vt.qemu.image_type')) + image_type_setting = "config vt.qemu.image_type" + if not get_opt(self.config, "vt.config"): + if get_opt(self.config, "vt.qemu.image_type") in SUPPORTED_IMAGE_TYPES: + self.cartesian_parser.only_filter( + get_opt(self.config, "vt.qemu.image_type") + ) else: self.cartesian_parser.only_filter("raw") # The actual param name is image_format. - self.cartesian_parser.assign("image_format", - get_opt(self.config, 'vt.qemu.image_type')) + self.cartesian_parser.assign( + "image_format", get_opt(self.config, "vt.qemu.image_type") + ) else: LOG.info("Config provided, ignoring %s", image_type_setting) def _process_nic_model(self): - nic_model_setting = 'config vt.qemu.nic_model' - if not get_opt(self.config, 'vt.config'): - if get_opt(self.config, 'vt.qemu.nic_model') in SUPPORTED_NIC_MODELS: - self.cartesian_parser.only_filter(get_opt(self.config, 'vt.qemu.nic_model')) + nic_model_setting = "config vt.qemu.nic_model" + if not get_opt(self.config, "vt.config"): + if get_opt(self.config, "vt.qemu.nic_model") in SUPPORTED_NIC_MODELS: + self.cartesian_parser.only_filter( + get_opt(self.config, "vt.qemu.nic_model") + ) else: self.cartesian_parser.only_filter("nic_custom") self.cartesian_parser.assign( - "nic_model", get_opt(self.config, 'vt.qemu.nic_model')) + "nic_model", get_opt(self.config, "vt.qemu.nic_model") + ) else: LOG.info("Config provided, ignoring %s", nic_model_setting) def _process_disk_buses(self): - disk_bus_setting = 'config vt.qemu.disk_bus' - if not get_opt(self.config, 'vt.config'): - if get_opt(self.config, 'vt.qemu.disk_bus') in SUPPORTED_DISK_BUSES: - self.cartesian_parser.only_filter(get_opt(self.config, 'vt.qemu.disk_bus')) + disk_bus_setting = "config vt.qemu.disk_bus" + if not get_opt(self.config, "vt.config"): + if get_opt(self.config, "vt.qemu.disk_bus") in SUPPORTED_DISK_BUSES: + self.cartesian_parser.only_filter( + get_opt(self.config, "vt.qemu.disk_bus") + ) else: - raise ValueError("Invalid %s '%s'. Valid values: %s" % - (disk_bus_setting, - get_opt(self.config, 'vt.qemu.disk_bus'), - SUPPORTED_DISK_BUSES)) + raise ValueError( + "Invalid %s '%s'. Valid values: %s" + % ( + disk_bus_setting, + get_opt(self.config, "vt.qemu.disk_bus"), + SUPPORTED_DISK_BUSES, + ) + ) else: LOG.info("Config provided, ignoring %s", disk_bus_setting) def _process_vhost(self): - nettype_setting = 'config vt.qemu.nettype' - vhost_setting = 'config vt.qemu.vhost' - if not get_opt(self.config, 'vt.config'): - if get_opt(self.config, 'vt.common.nettype') == "bridge": - if get_opt(self.config, 'vt.qemu.vhost') == "on": + nettype_setting = "config vt.qemu.nettype" + vhost_setting = "config vt.qemu.vhost" + if not get_opt(self.config, "vt.config"): + if get_opt(self.config, "vt.common.nettype") == "bridge": + if get_opt(self.config, "vt.qemu.vhost") == "on": self.cartesian_parser.assign("vhost", "on") - elif get_opt(self.config, 'vt.qemu.vhost') == "force": - self.cartesian_parser.assign("netdev_extra_params", - '",vhostforce=on"') + elif get_opt(self.config, "vt.qemu.vhost") == "force": + self.cartesian_parser.assign( + "netdev_extra_params", '",vhostforce=on"' + ) self.cartesian_parser.assign("vhost", "on") else: - if get_opt(self.config, 'vt.qemu.vhost') in ["on", "force"]: - raise ValueError("%s '%s' is incompatible with %s '%s'" - % (nettype_setting, - get_opt(self.config, 'vt.common.nettype'), - vhost_setting, - get_opt(self.config, 'vt.qemu.vhost'))) + if get_opt(self.config, "vt.qemu.vhost") in ["on", "force"]: + raise ValueError( + "%s '%s' is incompatible with %s '%s'" + % ( + nettype_setting, + get_opt(self.config, "vt.common.nettype"), + vhost_setting, + get_opt(self.config, "vt.qemu.vhost"), + ) + ) else: LOG.info("Config provided, ignoring %s", vhost_setting) def _process_qemu_sandbox(self): - sandbox_setting = 'config vt.qemu.sandbox' - if not get_opt(self.config, 'vt.config'): - if get_opt(self.config, 'vt.qemu.sandbox') == "off": + sandbox_setting = "config vt.qemu.sandbox" + if not get_opt(self.config, "vt.config"): + if get_opt(self.config, "vt.qemu.sandbox") == "off": self.cartesian_parser.assign("qemu_sandbox", "off") else: LOG.info("Config provided, ignoring %s", sandbox_setting) def _process_qemu_defconfig(self): - defconfig_setting = 'config vt.qemu.sandbox' - if not get_opt(self.config, 'vt.config'): - if get_opt(self.config, 'vt.qemu.defconfig') == "no": + defconfig_setting = "config vt.qemu.sandbox" + if not get_opt(self.config, "vt.config"): + if get_opt(self.config, "vt.qemu.defconfig") == "no": self.cartesian_parser.assign("defconfig", "no") else: LOG.info("Config provided, ignoring %s", defconfig_setting) def _process_malloc_perturb(self): - self.cartesian_parser.assign("malloc_perturb", - get_opt(self.config, 'vt.qemu.malloc_perturb')) + self.cartesian_parser.assign( + "malloc_perturb", get_opt(self.config, "vt.qemu.malloc_perturb") + ) def _process_qemu_specific_options(self): """ @@ -353,65 +383,68 @@ def _process_lvsb_specific_options(self): """ Calls for processing all options specific to LVSB test """ - set_opt(self.config, 'no_downloads', True) + set_opt(self.config, "no_downloads", True) def _process_libvirt_specific_options(self): """ Calls for processing all options specific to libvirt test. """ - uri_setting = 'config vt.libvirt.connect_uri' - if get_opt(self.config, 'vt.libvirt.connect_uri'): + uri_setting = "config vt.libvirt.connect_uri" + if get_opt(self.config, "vt.libvirt.connect_uri"): driver_found = False for driver in SUPPORTED_LIBVIRT_DRIVERS: - if get_opt(self.config, 'vt.libvirt.connect_uri').count(driver): + if get_opt(self.config, "vt.libvirt.connect_uri").count(driver): driver_found = True self.cartesian_parser.only_filter(driver) if not driver_found: - raise ValueError("Unsupported %s '%s'" - % (uri_setting, - get_opt(self.config, - 'vt.libvbirt.connect_uri'))) + raise ValueError( + "Unsupported %s '%s'" + % (uri_setting, get_opt(self.config, "vt.libvbirt.connect_uri")) + ) else: self.cartesian_parser.only_filter("qemu") def _process_guest_os(self): - guest_os_setting = 'option --vt-guest-os' + guest_os_setting = "option --vt-guest-os" - if get_opt(self.config, 'vt.type') == 'spice': + if get_opt(self.config, "vt.type") == "spice": LOG.info("Ignoring predefined OS: %s", guest_os_setting) return - if not get_opt(self.config, 'vt.config'): + if not get_opt(self.config, "vt.config"): if len(standalone_test.get_guest_name_list(self.config)) == 0: - raise ValueError("%s '%s' is not on the known guest os for " - "arch '%s' and machine type '%s'. (see " - "--vt-list-guests)" - % (guest_os_setting, - get_opt(self.config, 'vt.guest_os'), - get_opt(self.config, 'vt.common.arch'), - get_opt(self.config, 'vt.common.machine_type'))) + raise ValueError( + "%s '%s' is not on the known guest os for " + "arch '%s' and machine type '%s'. (see " + "--vt-list-guests)" + % ( + guest_os_setting, + get_opt(self.config, "vt.guest_os"), + get_opt(self.config, "vt.common.arch"), + get_opt(self.config, "vt.common.machine_type"), + ) + ) self.cartesian_parser.only_filter( - get_opt(self.config, 'vt.guest_os') or defaults.DEFAULT_GUEST_OS) + get_opt(self.config, "vt.guest_os") or defaults.DEFAULT_GUEST_OS + ) else: LOG.info("Config provided, ignoring %s", guest_os_setting) def _process_restart_vm(self): - if not get_opt(self.config, 'vt.config'): - if not get_opt(self.config, 'vt.setup.keep_guest_running'): + if not get_opt(self.config, "vt.config"): + if not get_opt(self.config, "vt.setup.keep_guest_running"): self.cartesian_parser.assign("kill_vm", "yes") def _process_restore_image(self): - if not get_opt(self.config, 'vt.config'): - if get_opt(self.config, 'vt.setup.backup_image_before_test'): - self.cartesian_parser.assign("backup_image_before_testing", - "yes") - if get_opt(self.config, 'vt.setup.restore_image_after_test'): - self.cartesian_parser.assign("restore_image_after_testing", - "yes") + if not get_opt(self.config, "vt.config"): + if get_opt(self.config, "vt.setup.backup_image_before_test"): + self.cartesian_parser.assign("backup_image_before_testing", "yes") + if get_opt(self.config, "vt.setup.restore_image_after_test"): + self.cartesian_parser.assign("restore_image_after_testing", "yes") def _process_mem(self): - if not get_opt(self.config, 'vt.config'): - mem = get_opt(self.config, 'vt.common.mem') + if not get_opt(self.config, "vt.config"): + mem = get_opt(self.config, "vt.common.mem") if mem is not None: self.cartesian_parser.assign("mem", mem) @@ -420,7 +453,7 @@ def _process_tcpdump(self): Verify whether we can run tcpdump. If we can't, turn it off. """ try: - tcpdump_path = utils_path.find_command('tcpdump') + tcpdump_path = utils_path.find_command("tcpdump") except utils_path.CmdNotFoundError: tcpdump_path = None @@ -430,24 +463,24 @@ def _process_tcpdump(self): self.cartesian_parser.assign("run_tcpdump", "no") def _process_no_filter(self): - if get_opt(self.config, 'vt.no_filter'): - for item in get_opt(self.config, 'vt.no_filter').split(' '): + if get_opt(self.config, "vt.no_filter"): + for item in get_opt(self.config, "vt.no_filter").split(" "): self.cartesian_parser.no_filter(item) def _process_only_filter(self): - if get_opt(self.config, 'vt.only_filter'): - for item in get_opt(self.config, 'vt.only_filter').split(' '): + if get_opt(self.config, "vt.only_filter"): + for item in get_opt(self.config, "vt.only_filter").split(" "): self.cartesian_parser.only_filter(item) def _process_extra_params(self): if get_opt(self.config, "vt.extra_params"): for param in get_opt(self.config, "vt.extra_params"): - key, value = param.split('=', 1) + key, value = param.split("=", 1) self.cartesian_parser.assign(key, value) def _process_only_type_specific(self): - if not get_opt(self.config, 'vt.config'): - if get_opt(self.config, 'vt.type_specific'): + if not get_opt(self.config, "vt.config"): + if get_opt(self.config, "vt.type_specific"): self.cartesian_parser.only_filter("(subtest=type_specific)") def _process_general_options(self): @@ -474,12 +507,12 @@ def _process_backend_specific_options(self, vt_type): Calls for processing of backend specific options """ backend_specific_options = { - 'qemu': self._process_qemu_specific_options, - 'lvsb': self._process_lvsb_specific_options, - 'openvswitch': self._process_qemu_specific_options, - 'libvirt': self._process_libvirt_specific_options, - 'spice': self._process_spice_specific_options, - } + "qemu": self._process_qemu_specific_options, + "lvsb": self._process_lvsb_specific_options, + "openvswitch": self._process_qemu_specific_options, + "libvirt": self._process_libvirt_specific_options, + "spice": self._process_spice_specific_options, + } specific_opts = backend_specific_options.get(vt_type, None) if specific_opts is not None: specific_opts() @@ -490,53 +523,58 @@ def _process_spice_specific_options(self): """ # We can call here for self._process_qemu_specific_options() # to process some --options, but let SpiceQA tests will be independent - set_opt(self.config, 'no_downloads', True) + set_opt(self.config, "no_downloads", True) def _process_options(self): """ Process the options given in the command line. """ cfg = None - vt_type_setting = 'option --vt-type' - vt_config_setting = 'option --vt-config' + vt_type_setting = "option --vt-type" + vt_config_setting = "option --vt-config" - vt_type = get_opt(self.config, 'vt.type') - vt_config = get_opt(self.config, 'vt.config') + vt_type = get_opt(self.config, "vt.type") + vt_config = get_opt(self.config, "vt.config") if (not vt_type) and (not vt_config): - raise ValueError("No %s or %s specified" % - (vt_type_setting, vt_config_setting)) + raise ValueError( + "No %s or %s specified" % (vt_type_setting, vt_config_setting) + ) if vt_type: if vt_type not in SUPPORTED_TEST_TYPES: - raise ValueError("Invalid %s %s. Valid values: %s. " - % (vt_type_setting, - vt_type, - " ".join(SUPPORTED_TEST_TYPES))) + raise ValueError( + "Invalid %s %s. Valid values: %s. " + % (vt_type_setting, vt_type, " ".join(SUPPORTED_TEST_TYPES)) + ) self.cartesian_parser = cartesian_config.Parser(debug=False) if vt_config: cfg = os.path.abspath(vt_config) self.cartesian_parser.parse_file(cfg) - elif get_opt(self.config, 'vt.filter.default_filters'): - cfg = data_dir.get_backend_cfg_path(vt_type, - 'tests-shared.cfg') + elif get_opt(self.config, "vt.filter.default_filters"): + cfg = data_dir.get_backend_cfg_path(vt_type, "tests-shared.cfg") self.cartesian_parser.parse_file(cfg) - for arg in ('no_9p_export', 'no_virtio_rng', 'no_pci_assignable', - 'smallpages', 'default_bios', 'bridge'): - if arg not in get_opt(self.config, 'vt.filter.default_filters'): + for arg in ( + "no_9p_export", + "no_virtio_rng", + "no_pci_assignable", + "smallpages", + "default_bios", + "bridge", + ): + if arg not in get_opt(self.config, "vt.filter.default_filters"): self.cartesian_parser.only_filter(arg) - if 'image_backend' not in get_opt(self.config, 'vt.filter.default_filters'): - self.cartesian_parser.only_filter('(image_backend=' - 'filesystem)') - if 'multihost' not in get_opt(self.config, 'vt.filter.default_filters'): - self.cartesian_parser.no_filter('multihost') + if "image_backend" not in get_opt(self.config, "vt.filter.default_filters"): + self.cartesian_parser.only_filter("(image_backend=" "filesystem)") + if "multihost" not in get_opt(self.config, "vt.filter.default_filters"): + self.cartesian_parser.no_filter("multihost") else: - cfg = data_dir.get_backend_cfg_path(vt_type, 'tests.cfg') + cfg = data_dir.get_backend_cfg_path(vt_type, "tests.cfg") self.cartesian_parser.parse_file(cfg) - if vt_type != 'lvsb': + if vt_type != "lvsb": self._process_general_options() self._process_backend_specific_options(vt_type) diff --git a/avocado_vt/plugins/vt.py b/avocado_vt/plugins/vt.py index 6fc12b6ca0..06bffd99d2 100644 --- a/avocado_vt/plugins/vt.py +++ b/avocado_vt/plugins/vt.py @@ -23,26 +23,27 @@ from virttest import data_dir, defaults, standalone_test from virttest.compat import get_settings_value, add_option -from virttest.standalone_test import (SUPPORTED_LIBVIRT_URIS, - SUPPORTED_TEST_TYPES) +from virttest.standalone_test import SUPPORTED_LIBVIRT_URIS, SUPPORTED_TEST_TYPES try: from avocado.core.loader import loader from ..loader import VirtTestLoader + AVOCADO_LOADER_AVAILABLE = True except ImportError: AVOCADO_LOADER_AVAILABLE = False -_PROVIDERS_DOWNLOAD_DIR = os.path.join(data_dir.get_test_providers_dir(), - 'downloads') +_PROVIDERS_DOWNLOAD_DIR = os.path.join(data_dir.get_test_providers_dir(), "downloads") try: assert len(os.listdir(_PROVIDERS_DOWNLOAD_DIR)) != 0 except (OSError, AssertionError): - raise ImportError("Bootstrap missing. " - "Execute 'avocado vt-bootstrap' or disable this " - "plugin to get rid of this message") + raise ImportError( + "Bootstrap missing. " + "Execute 'avocado vt-bootstrap' or disable this " + "plugin to get rid of this message" + ) def add_basic_vt_options(parser): @@ -50,87 +51,97 @@ def add_basic_vt_options(parser): Add basic vt options to parser """ - help_msg = ("Explicitly choose a cartesian config. When choosing this, " - "some options will be ignored (see options below)") - add_option(parser, - dest='vt.config', - arg='--vt-config', - help=help_msg) + help_msg = ( + "Explicitly choose a cartesian config. When choosing this, " + "some options will be ignored (see options below)" + ) + add_option(parser, dest="vt.config", arg="--vt-config", help=help_msg) help_msg = "Save the resulting cartesian config to a file" - add_option(parser, - dest='vt.save_config', - arg='--vt-save-config', - help=help_msg) + add_option(parser, dest="vt.save_config", arg="--vt-save-config", help=help_msg) help_msg = "Enable short names as test names when using a config file" - add_option(parser, - dest='vt.short_names_when_config', - arg='--vt-short-names-when-config', - help=help_msg) - - help_msg = ("Choose test type (%s). Default: %%(default)s" % - ", ".join(SUPPORTED_TEST_TYPES)) - add_option(parser, - dest='vt.type', - arg='--vt-type', - default=SUPPORTED_TEST_TYPES[0], - help=help_msg) - - arch = get_settings_value('vt.common', 'arch', default=None) + add_option( + parser, + dest="vt.short_names_when_config", + arg="--vt-short-names-when-config", + help=help_msg, + ) + + help_msg = "Choose test type (%s). Default: %%(default)s" % ", ".join( + SUPPORTED_TEST_TYPES + ) + add_option( + parser, + dest="vt.type", + arg="--vt-type", + default=SUPPORTED_TEST_TYPES[0], + help=help_msg, + ) + + arch = get_settings_value("vt.common", "arch", default=None) help_msg = "Choose the VM architecture. Default: %(default)s" - add_option(parser, - dest='vt.common.arch', - arg='--vt-arch', - default=arch, - help=help_msg) - - machine = get_settings_value('vt.common', 'machine_type', - default=defaults.DEFAULT_MACHINE_TYPE) + add_option( + parser, dest="vt.common.arch", arg="--vt-arch", default=arch, help=help_msg + ) + + machine = get_settings_value( + "vt.common", "machine_type", default=defaults.DEFAULT_MACHINE_TYPE + ) help_msg = "Choose the VM machine type. Default: %(default)s" - add_option(parser, - dest='vt.common.machine_type', - arg='--vt-machine-type', - default=machine, - help=help_msg) - - help_msg = ("Select the guest OS to be used. If --vt-config is provided, " - "this will be ignored. Default: %(default)s") - add_option(parser, - dest='vt.guest_os', - arg='--vt-guest-os', - default=defaults.DEFAULT_GUEST_OS, - help=help_msg) - - help_msg = ("List of space separated 'no' filters to be passed to the " - "config parser. Default: '%(default)s'") - add_option(parser, - dest='vt.no_filter', - arg='--vt-no-filter', - default="", - help=help_msg) - - help_msg = ("List of space separated 'only' filters to be passed to the " - "config parser. Default: '%(default)s'") - add_option(parser, - dest='vt.only_filter', - arg='--vt-only-filter', - default="", - help=help_msg) - - help_msg = ("Allows to selectively skip certain default filters. This uses " - "directly 'tests-shared.cfg' and instead of " - "'$provider/tests.cfg' and applies following lists of default " - "filters, unless they are specified as arguments: " - "no_9p_export,no_virtio_rng,no_pci_assignable,smallpages," - "default_bios,bridge,image_backend,multihost. This can be used" - " to eg. run hugepages tests by filtering 'smallpages' via " - "this option.") - add_option(parser, - dest='vt.filter.default_filters', - arg='--vt-filter-default-filters', - nargs='+', - help=help_msg) + add_option( + parser, + dest="vt.common.machine_type", + arg="--vt-machine-type", + default=machine, + help=help_msg, + ) + + help_msg = ( + "Select the guest OS to be used. If --vt-config is provided, " + "this will be ignored. Default: %(default)s" + ) + add_option( + parser, + dest="vt.guest_os", + arg="--vt-guest-os", + default=defaults.DEFAULT_GUEST_OS, + help=help_msg, + ) + + help_msg = ( + "List of space separated 'no' filters to be passed to the " + "config parser. Default: '%(default)s'" + ) + add_option( + parser, dest="vt.no_filter", arg="--vt-no-filter", default="", help=help_msg + ) + + help_msg = ( + "List of space separated 'only' filters to be passed to the " + "config parser. Default: '%(default)s'" + ) + add_option( + parser, dest="vt.only_filter", arg="--vt-only-filter", default="", help=help_msg + ) + + help_msg = ( + "Allows to selectively skip certain default filters. This uses " + "directly 'tests-shared.cfg' and instead of " + "'$provider/tests.cfg' and applies following lists of default " + "filters, unless they are specified as arguments: " + "no_9p_export,no_virtio_rng,no_pci_assignable,smallpages," + "default_bios,bridge,image_backend,multihost. This can be used" + " to eg. run hugepages tests by filtering 'smallpages' via " + "this option." + ) + add_option( + parser, + dest="vt.filter.default_filters", + arg="--vt-filter-default-filters", + nargs="+", + help=help_msg, + ) def add_qemu_bin_vt_option(parser): @@ -148,35 +159,40 @@ def _str_or_none(arg): qemu_bin_path = standalone_test.find_default_qemu_paths()[0] except (RuntimeError, utils_path.CmdNotFoundError): qemu_bin_path = None - qemu_bin = get_settings_value('vt.qemu', 'qemu_bin', - default=None) + qemu_bin = get_settings_value("vt.qemu", "qemu_bin", default=None) if qemu_bin is None: # Allow default to be None when not set in setting default_qemu_bin = None qemu_bin = qemu_bin_path else: default_qemu_bin = qemu_bin - help_msg = ("Path to a custom qemu binary to be tested. If --vt-config is " - "provided and this flag is omitted, no attempt to set the " - "qemu binaries will be made. Current: %s" % - _str_or_none(qemu_bin)) - add_option(parser, - dest='vt.qemu.qemu_bin', - arg='--vt-qemu-bin', - default=default_qemu_bin, - help=help_msg) - - qemu_dst = get_settings_value('vt.qemu', 'qemu_dst_bin', - default=qemu_bin_path) - help_msg = ("Path to a custom qemu binary to be tested for the destination" - " of a migration, overrides --vt-qemu-bin. If --vt-config is " - "provided and this flag is omitted, no attempt to set the qemu" - " binaries will be made. Current: %s" % _str_or_none(qemu_dst)) - add_option(parser, - dest='vt.qemu.qemu_dst_bin', - arg='--vt-qemu-dst-bin', - default=qemu_dst, - help=help_msg) + help_msg = ( + "Path to a custom qemu binary to be tested. If --vt-config is " + "provided and this flag is omitted, no attempt to set the " + "qemu binaries will be made. Current: %s" % _str_or_none(qemu_bin) + ) + add_option( + parser, + dest="vt.qemu.qemu_bin", + arg="--vt-qemu-bin", + default=default_qemu_bin, + help=help_msg, + ) + + qemu_dst = get_settings_value("vt.qemu", "qemu_dst_bin", default=qemu_bin_path) + help_msg = ( + "Path to a custom qemu binary to be tested for the destination" + " of a migration, overrides --vt-qemu-bin. If --vt-config is " + "provided and this flag is omitted, no attempt to set the qemu" + " binaries will be made. Current: %s" % _str_or_none(qemu_dst) + ) + add_option( + parser, + dest="vt.qemu.qemu_dst_bin", + arg="--vt-qemu-dst-bin", + default=qemu_dst, + help=help_msg, + ) class VTRun(CLI): @@ -185,7 +201,7 @@ class VTRun(CLI): Avocado VT - legacy virt-test support """ - name = 'vt' + name = "vt" description = "Avocado VT/virt-test support to 'run' command" def configure(self, parser): @@ -194,37 +210,45 @@ def configure(self, parser): :param parser: Main test runner parser. """ - run_subcommand_parser = parser.subcommands.choices.get('run', None) + run_subcommand_parser = parser.subcommands.choices.get("run", None) if run_subcommand_parser is None: return vt_compat_group_common = run_subcommand_parser.add_argument_group( - 'Virt-Test compat layer - Common options') + "Virt-Test compat layer - Common options" + ) vt_compat_group_qemu = run_subcommand_parser.add_argument_group( - 'Virt-Test compat layer - QEMU options') + "Virt-Test compat layer - QEMU options" + ) vt_compat_group_libvirt = run_subcommand_parser.add_argument_group( - 'Virt-Test compat layer - Libvirt options') + "Virt-Test compat layer - Libvirt options" + ) add_basic_vt_options(vt_compat_group_common) add_qemu_bin_vt_option(vt_compat_group_qemu) help_msg = "List of 'key=value' pairs passed to cartesian parser." - add_option(parser=vt_compat_group_qemu, - dest='vt.extra_params', - arg='--vt-extra-params', - action='append', - help=help_msg) + add_option( + parser=vt_compat_group_qemu, + dest="vt.extra_params", + arg="--vt-extra-params", + action="append", + help=help_msg, + ) supported_uris = ", ".join(SUPPORTED_LIBVIRT_URIS) - help_msg = ("Choose test connect uri for libvirt (E.g: %s). Current: " - "%%(default)s" % supported_uris) - uri_current = get_settings_value('vt.libvirt', 'connect_uri', - default=None) - add_option(parser=vt_compat_group_libvirt, - dest='vt.libvirt.connect_uri', - arg='--vt-connect-uri', - default=uri_current, - help=help_msg) + help_msg = ( + "Choose test connect uri for libvirt (E.g: %s). Current: " + "%%(default)s" % supported_uris + ) + uri_current = get_settings_value("vt.libvirt", "connect_uri", default=None) + add_option( + parser=vt_compat_group_libvirt, + dest="vt.libvirt.connect_uri", + arg="--vt-connect-uri", + default=uri_current, + help=help_msg, + ) def run(self, config): """ diff --git a/avocado_vt/plugins/vt_bootstrap.py b/avocado_vt/plugins/vt_bootstrap.py index 30aa03f373..6f034e265d 100644 --- a/avocado_vt/plugins/vt_bootstrap.py +++ b/avocado_vt/plugins/vt_bootstrap.py @@ -31,66 +31,118 @@ class VTBootstrap(CLICmd): Avocado VT - implements the 'vt-bootstrap' subcommand """ - name = 'vt-bootstrap' + name = "vt-bootstrap" description = "Avocado VT - implements the 'vt-bootstrap' subcommand" def configure(self, parser): parser = super(VTBootstrap, self).configure(parser) - parser.add_argument("--vt-type", action="store", - help=("Choose test type (%s)" % - ", ".join(SUPPORTED_TEST_TYPES)), - default='qemu', dest='vt.type') - parser.add_argument("--vt-guest-os", action="store", - default="%s.%s" % (defaults.DEFAULT_GUEST_OS, - defaults.ARCH), - help=("Select the guest OS to be used " - "optionally followed by guest arch. " - "If -c is provided, this will be " - "ignored. Default: %(default)s"), - dest='vt.guest_os') - parser.add_argument("--vt-selinux-setup", action="store_true", - default=False, - help="Define default contexts of directory.") - parser.add_argument("--vt-no-downloads", action="store_true", - default=False, - help="Do not attempt any download") - parser.add_argument("--vt-skip-verify-download-assets", - action='store_true', default=False, - help=("Skip the bootstrap phase that verifies " - "and possibly downloads assets files " - "(usually a JeOS image)")) - parser.add_argument("--vt-update-config", action="store_true", - default=False, help=("Forces configuration " - "updates (all manual " - "config file editing " - "will be lost). " - "Requires --vt-type " - "to be set")) - parser.add_argument("--vt-update-providers", action="store_true", - default=False, help=("Forces test " - "providers to be " - "updated (git repos " - "will be pulled)")) - parser.add_argument("--yes-to-all", action="store_true", - default=False, help=("All interactive " - "questions will be " - "answered with yes (y)")) - parser.add_argument("--vt-host-distro-name", action="store", - metavar="HOST_DISTRO_NAME", - help=("The name of the distro to be used when " - "generating the host configuration entry")) - parser.add_argument("--vt-host-distro-version", action="store", - metavar="HOST_DISTRO_VERSION", - help=("The version of the distro to be used when " - "generating the host configuration entry")) - parser.add_argument("--vt-host-distro-release", action="store", - metavar="HOST_DISTRO_RELEASE", - help=("The release of the distro to be used when " - "generating the host configuration entry.")) - parser.add_argument("--vt-host-distro-arch", action="store", - metavar="HOST_DISTRO_ARCH", - help=("The architecture of the distro to be used when " - "generating the host configuration entry.")) + parser.add_argument( + "--vt-type", + action="store", + help=("Choose test type (%s)" % ", ".join(SUPPORTED_TEST_TYPES)), + default="qemu", + dest="vt.type", + ) + parser.add_argument( + "--vt-guest-os", + action="store", + default="%s.%s" % (defaults.DEFAULT_GUEST_OS, defaults.ARCH), + help=( + "Select the guest OS to be used " + "optionally followed by guest arch. " + "If -c is provided, this will be " + "ignored. Default: %(default)s" + ), + dest="vt.guest_os", + ) + parser.add_argument( + "--vt-selinux-setup", + action="store_true", + default=False, + help="Define default contexts of directory.", + ) + parser.add_argument( + "--vt-no-downloads", + action="store_true", + default=False, + help="Do not attempt any download", + ) + parser.add_argument( + "--vt-skip-verify-download-assets", + action="store_true", + default=False, + help=( + "Skip the bootstrap phase that verifies " + "and possibly downloads assets files " + "(usually a JeOS image)" + ), + ) + parser.add_argument( + "--vt-update-config", + action="store_true", + default=False, + help=( + "Forces configuration " + "updates (all manual " + "config file editing " + "will be lost). " + "Requires --vt-type " + "to be set" + ), + ) + parser.add_argument( + "--vt-update-providers", + action="store_true", + default=False, + help=( + "Forces test " + "providers to be " + "updated (git repos " + "will be pulled)" + ), + ) + parser.add_argument( + "--yes-to-all", + action="store_true", + default=False, + help=("All interactive " "questions will be " "answered with yes (y)"), + ) + parser.add_argument( + "--vt-host-distro-name", + action="store", + metavar="HOST_DISTRO_NAME", + help=( + "The name of the distro to be used when " + "generating the host configuration entry" + ), + ) + parser.add_argument( + "--vt-host-distro-version", + action="store", + metavar="HOST_DISTRO_VERSION", + help=( + "The version of the distro to be used when " + "generating the host configuration entry" + ), + ) + parser.add_argument( + "--vt-host-distro-release", + action="store", + metavar="HOST_DISTRO_RELEASE", + help=( + "The release of the distro to be used when " + "generating the host configuration entry." + ), + ) + parser.add_argument( + "--vt-host-distro-arch", + action="store", + metavar="HOST_DISTRO_ARCH", + help=( + "The architecture of the distro to be used when " + "generating the host configuration entry." + ), + ) def run(self, config): try: @@ -98,18 +150,18 @@ def run(self, config): sys.exit(0) except process.CmdError as ce: if ce.result.interrupted: - LOG.info('Bootstrap command interrupted by user') - LOG.info('Command: %s', ce.command) + LOG.info("Bootstrap command interrupted by user") + LOG.info("Command: %s", ce.command) else: - LOG.error('Bootstrap command failed') - LOG.error('Command: %s', ce.command) + LOG.error("Bootstrap command failed") + LOG.error("Command: %s", ce.command) if ce.result.stderr_text: - LOG.error('stderr output:') + LOG.error("stderr output:") LOG.error(ce.result.stderr_text) if ce.result.stdout_text: - LOG.error('stdout output:') + LOG.error("stdout output:") LOG.error(ce.result.stdout_text) sys.exit(1) except KeyboardInterrupt: - LOG.info('Bootstrap interrupted by user') + LOG.info("Bootstrap interrupted by user") sys.exit(1) diff --git a/avocado_vt/plugins/vt_init.py b/avocado_vt/plugins/vt_init.py index 34d5ca8e72..4c13d81b88 100644 --- a/avocado_vt/plugins/vt_init.py +++ b/avocado_vt/plugins/vt_init.py @@ -4,27 +4,29 @@ from avocado.core.settings import settings from avocado.utils import path as utils_path -from virttest.compat import (get_settings_value, - is_registering_settings_required) -from virttest.defaults import (DEFAULT_GUEST_OS, - DEFAULT_MACHINE_TYPE) -from virttest.standalone_test import (SUPPORTED_DISK_BUSES, - SUPPORTED_IMAGE_TYPES, - SUPPORTED_NIC_MODELS, - SUPPORTED_TEST_TYPES, - find_default_qemu_paths) +from virttest.compat import get_settings_value, is_registering_settings_required +from virttest.defaults import DEFAULT_GUEST_OS, DEFAULT_MACHINE_TYPE +from virttest.standalone_test import ( + SUPPORTED_DISK_BUSES, + SUPPORTED_IMAGE_TYPES, + SUPPORTED_NIC_MODELS, + SUPPORTED_TEST_TYPES, + find_default_qemu_paths, +) try: from avocado.core.loader import loader + AVOCADO_LOADER_AVAILABLE = True except ImportError: AVOCADO_LOADER_AVAILABLE = False -if hasattr(plugin_interfaces, 'Init'): +if hasattr(plugin_interfaces, "Init"): + class VtInit(plugin_interfaces.Init): - name = 'vt-init' + name = "vt-init" description = "VT plugin initilization" def initialize(self): @@ -32,254 +34,321 @@ def initialize(self): return # [vt] section - section = 'vt' - - help_msg = ('Explicitly choose a cartesian config. When choosing ' - 'this, some options will be ignored (see options ' - 'below)') - settings.register_option(section, key='config', default=None, - help_msg=help_msg) - - help_msg = 'Save the resulting cartesian config to a file' - settings.register_option(section, key='save_config', default=None, - help_msg=help_msg) - - help_msg = ("Enable short names as test names when using a config " - "file") - settings.register_option(section, key='short_names_when_config', - key_type=bool, default=False, - help_msg=help_msg) - - help_msg = ("Choose test type (%s). Default: %%(default)s" % - ", ".join(SUPPORTED_TEST_TYPES)) - settings.register_option(section, key='type', - default=SUPPORTED_TEST_TYPES[0], - help_msg=help_msg) - - help_msg = ("Select the guest OS to be used. If --vt-config is " - "provided, this will be ignored. Default: %s" % - DEFAULT_GUEST_OS) - settings.register_option(section, key='guest_os', - default=DEFAULT_GUEST_OS, - help_msg=help_msg) - - help_msg = ("List of space separated 'no' filters to be passed to " - "the config parser.") - settings.register_option(section, key='no_filter', default='', - help_msg=help_msg) - - help_msg = ("List of space separated 'only' filters to be passed " - "to the config parser.") - settings.register_option(section, key='only_filter', default='', - help_msg=help_msg) + section = "vt" + + help_msg = ( + "Explicitly choose a cartesian config. When choosing " + "this, some options will be ignored (see options " + "below)" + ) + settings.register_option( + section, key="config", default=None, help_msg=help_msg + ) + + help_msg = "Save the resulting cartesian config to a file" + settings.register_option( + section, key="save_config", default=None, help_msg=help_msg + ) + + help_msg = "Enable short names as test names when using a config " "file" + settings.register_option( + section, + key="short_names_when_config", + key_type=bool, + default=False, + help_msg=help_msg, + ) + + help_msg = "Choose test type (%s). Default: %%(default)s" % ", ".join( + SUPPORTED_TEST_TYPES + ) + settings.register_option( + section, key="type", default=SUPPORTED_TEST_TYPES[0], help_msg=help_msg + ) + + help_msg = ( + "Select the guest OS to be used. If --vt-config is " + "provided, this will be ignored. Default: %s" % DEFAULT_GUEST_OS + ) + settings.register_option( + section, key="guest_os", default=DEFAULT_GUEST_OS, help_msg=help_msg + ) + + help_msg = ( + "List of space separated 'no' filters to be passed to " + "the config parser." + ) + settings.register_option( + section, key="no_filter", default="", help_msg=help_msg + ) + + help_msg = ( + "List of space separated 'only' filters to be passed " + "to the config parser." + ) + settings.register_option( + section, key="only_filter", default="", help_msg=help_msg + ) help_msg = "List of 'key=value' pairs passed to cartesian parser." - settings.register_option(section, key='extra_params', nargs='+', - key_type=list, default=[], - help_msg=help_msg) - - help_msg = ("Also list the available guests (this option ignores " - "the --vt-config and --vt-guest-os)") - settings.register_option(section, key='list_guests', key_type=bool, - default=False, help_msg=help_msg) - help_msg = ("Also list the available arch/machines for the given " - "guest OS. (Use \"--vt-guest-os ''\" to see all " - "combinations; --vt-config --vt-machine-type and " - "--vt-arch args are ignored)") - settings.register_option(section, key='list_archs', key_type=bool, - default=False, help_msg=help_msg) + settings.register_option( + section, + key="extra_params", + nargs="+", + key_type=list, + default=[], + help_msg=help_msg, + ) + + help_msg = ( + "Also list the available guests (this option ignores " + "the --vt-config and --vt-guest-os)" + ) + settings.register_option( + section, + key="list_guests", + key_type=bool, + default=False, + help_msg=help_msg, + ) + help_msg = ( + "Also list the available arch/machines for the given " + "guest OS. (Use \"--vt-guest-os ''\" to see all " + "combinations; --vt-config --vt-machine-type and " + "--vt-arch args are ignored)" + ) + settings.register_option( + section, + key="list_archs", + key_type=bool, + default=False, + help_msg=help_msg, + ) # [vt.setup] section - section = 'vt.setup' - - help_msg = 'Backup image before testing (if not already backed up)' - settings.register_option(section, 'backup_image_before_test', - help_msg=help_msg, key_type=bool, - default=True) - - help_msg = 'Restore image after testing (if backup present)' - settings.register_option(section, 'restore_image_after_test', - help_msg=help_msg, key_type=bool, - default=True) - - help_msg = 'Keep guest running between tests (faster, but unsafe)' - settings.register_option(section, 'keep_guest_running', - help_msg=help_msg, key_type=bool, - default=False) + section = "vt.setup" + + help_msg = "Backup image before testing (if not already backed up)" + settings.register_option( + section, + "backup_image_before_test", + help_msg=help_msg, + key_type=bool, + default=True, + ) + + help_msg = "Restore image after testing (if backup present)" + settings.register_option( + section, + "restore_image_after_test", + help_msg=help_msg, + key_type=bool, + default=True, + ) + + help_msg = "Keep guest running between tests (faster, but unsafe)" + settings.register_option( + section, + "keep_guest_running", + help_msg=help_msg, + key_type=bool, + default=False, + ) # [vt.common] section - section = 'vt.common' - - help_msg = ('Data dir path. If none specified, the default ' - 'virt-test data dir will be used') - settings.register_option(section, 'data_dir', - help_msg=help_msg, - default='') - - help_msg = ('Make the temporary dir path persistent across jobs if' - ' needed. By default the data in the temporary ' - 'directory will be wiped after each test in some cases' - ' and after each job in others.') - settings.register_option(section, 'tmp_dir', - help_msg=help_msg, - default='') - - help_msg = ('Enable only type specific tests. Shared tests will ' - 'not be tested') - settings.register_option(section, 'type_specific_only', - help_msg=help_msg, key_type=bool, - default=False) - - help_msg = ('RAM dedicated to the main VM. Usually defaults to ' - '1024, as set in "base.cfg", but can be a different ' - 'value depending on the various other configuration ' - 'files such as configuration files under "guest-os" ' - 'and test provider specific files') - settings.register_option(section, 'mem', - help_msg=help_msg, - default=None) - - help_msg = 'Architecture under test' - settings.register_option(section, 'arch', - help_msg=help_msg, - default=None) - - help_msg = 'Machine type under test' - settings.register_option(section, 'machine_type', - help_msg=help_msg, - default=DEFAULT_MACHINE_TYPE) - - help_msg = 'Nettype (bridge, user, none)' - settings.register_option(section, 'nettype', - help_msg=help_msg, - default='') - - help_msg = 'Bridge name to be used if you select bridge as a nettype' - settings.register_option(section, 'netdst', - help_msg=help_msg, - default='virbr0') + section = "vt.common" + + help_msg = ( + "Data dir path. If none specified, the default " + "virt-test data dir will be used" + ) + settings.register_option(section, "data_dir", help_msg=help_msg, default="") + + help_msg = ( + "Make the temporary dir path persistent across jobs if" + " needed. By default the data in the temporary " + "directory will be wiped after each test in some cases" + " and after each job in others." + ) + settings.register_option(section, "tmp_dir", help_msg=help_msg, default="") + + help_msg = ( + "Enable only type specific tests. Shared tests will " "not be tested" + ) + settings.register_option( + section, + "type_specific_only", + help_msg=help_msg, + key_type=bool, + default=False, + ) + + help_msg = ( + "RAM dedicated to the main VM. Usually defaults to " + '1024, as set in "base.cfg", but can be a different ' + "value depending on the various other configuration " + 'files such as configuration files under "guest-os" ' + "and test provider specific files" + ) + settings.register_option(section, "mem", help_msg=help_msg, default=None) + + help_msg = "Architecture under test" + settings.register_option(section, "arch", help_msg=help_msg, default=None) + + help_msg = "Machine type under test" + settings.register_option( + section, "machine_type", help_msg=help_msg, default=DEFAULT_MACHINE_TYPE + ) + + help_msg = "Nettype (bridge, user, none)" + settings.register_option(section, "nettype", help_msg=help_msg, default="") + + help_msg = "Bridge name to be used if you select bridge as a nettype" + settings.register_option( + section, "netdst", help_msg=help_msg, default="virbr0" + ) # [vt.qemu] section - section = 'vt.qemu' + section = "vt.qemu" try: default_qemu_bin_path = find_default_qemu_paths()[0] except (RuntimeError, utils_path.CmdNotFoundError): default_qemu_bin_path = None - qemu_bin = get_settings_value(section, 'qemu_bin', default=None) + qemu_bin = get_settings_value(section, "qemu_bin", default=None) if qemu_bin is None: # Allow default to be None when not set in setting default_qemu_bin = None else: default_qemu_bin = qemu_bin - help_msg = 'Path to a custom qemu binary to be tested' - settings.register_option(section, 'qemu_bin', - help_msg=help_msg, - default=default_qemu_bin) - - help_msg = ('Path to a custom qemu binary to be tested for the ' - 'destination of a migration, overrides qemu_bin for ' - 'that particular purpose') - settings.register_option(section, 'qemu_dst_bin', - help_msg=help_msg, - default=default_qemu_bin_path) - - help_msg = 'Accelerator used to run qemu (kvm or tcg)' - settings.register_option(section, 'accel', - help_msg=help_msg, - default='kvm') - - help_msg = ('Whether to enable vhost for qemu (on/off/force). ' - 'Depends on nettype=bridge') - settings.register_option(section, 'vhost', - help_msg=help_msg, - default='off') - - help_msg = 'Monitor type (human or qmp)' - settings.register_option(section, 'monitor', - help_msg=help_msg, - default='') - - help_msg = 'Number of virtual cpus to use (1 or 2)' - settings.register_option(section, 'smp', - help_msg=help_msg, - default='2') - - help_msg = 'Image format type to use (any valid qemu format)' - settings.register_option(section, 'image_type', - help_msg=help_msg, - default=SUPPORTED_IMAGE_TYPES[0]) - - help_msg = 'Guest network card model (any valid qemu card)' - settings.register_option(section, 'nic_model', - help_msg=help_msg, - default=SUPPORTED_NIC_MODELS[0]) - - help_msg = ('Guest disk bus for main image. One of ide, scsi, ' - 'virtio_blk, virtio_scsi, lsi_scsi, ahci, usb2 ' - 'or xenblk. Note: Older qemu versions and/or ' - 'operating systems (such as WinXP) might not support ' - 'virtio_scsi. Please use virtio_blk or ide instead.') - settings.register_option(section, 'disk_bus', - help_msg=help_msg, - default=SUPPORTED_DISK_BUSES[0]) - - help_msg = 'Enable qemu sandboxing (on/off)' - settings.register_option(section, 'sandbox', - help_msg=help_msg, - default='on') - - help_msg = ('Prevent qemu from loading sysconfdir/qemu.conf ' - 'and sysconfdir/target-ARCH.conf at startup (yes/no)') - settings.register_option(section, 'defconfig', - help_msg=help_msg, - default='yes') - - help_msg = ('Use MALLOC_PERTURB_ env variable set to 1 to help ' - 'catch memory allocation problems on qemu (yes/no)') - settings.register_option(section, 'malloc_perturb', - help_msg=help_msg, - default='yes') + help_msg = "Path to a custom qemu binary to be tested" + settings.register_option( + section, "qemu_bin", help_msg=help_msg, default=default_qemu_bin + ) + + help_msg = ( + "Path to a custom qemu binary to be tested for the " + "destination of a migration, overrides qemu_bin for " + "that particular purpose" + ) + settings.register_option( + section, + "qemu_dst_bin", + help_msg=help_msg, + default=default_qemu_bin_path, + ) + + help_msg = "Accelerator used to run qemu (kvm or tcg)" + settings.register_option(section, "accel", help_msg=help_msg, default="kvm") + + help_msg = ( + "Whether to enable vhost for qemu (on/off/force). " + "Depends on nettype=bridge" + ) + settings.register_option(section, "vhost", help_msg=help_msg, default="off") + + help_msg = "Monitor type (human or qmp)" + settings.register_option(section, "monitor", help_msg=help_msg, default="") + + help_msg = "Number of virtual cpus to use (1 or 2)" + settings.register_option(section, "smp", help_msg=help_msg, default="2") + + help_msg = "Image format type to use (any valid qemu format)" + settings.register_option( + section, + "image_type", + help_msg=help_msg, + default=SUPPORTED_IMAGE_TYPES[0], + ) + + help_msg = "Guest network card model (any valid qemu card)" + settings.register_option( + section, "nic_model", help_msg=help_msg, default=SUPPORTED_NIC_MODELS[0] + ) + + help_msg = ( + "Guest disk bus for main image. One of ide, scsi, " + "virtio_blk, virtio_scsi, lsi_scsi, ahci, usb2 " + "or xenblk. Note: Older qemu versions and/or " + "operating systems (such as WinXP) might not support " + "virtio_scsi. Please use virtio_blk or ide instead." + ) + settings.register_option( + section, "disk_bus", help_msg=help_msg, default=SUPPORTED_DISK_BUSES[0] + ) + + help_msg = "Enable qemu sandboxing (on/off)" + settings.register_option( + section, "sandbox", help_msg=help_msg, default="on" + ) + + help_msg = ( + "Prevent qemu from loading sysconfdir/qemu.conf " + "and sysconfdir/target-ARCH.conf at startup (yes/no)" + ) + settings.register_option( + section, "defconfig", help_msg=help_msg, default="yes" + ) + + help_msg = ( + "Use MALLOC_PERTURB_ env variable set to 1 to help " + "catch memory allocation problems on qemu (yes/no)" + ) + settings.register_option( + section, "malloc_perturb", help_msg=help_msg, default="yes" + ) # [vt.libvirt] section - section = 'vt.libvirt' + section = "vt.libvirt" - uri_current = get_settings_value(section, 'connect_uri', - default=None) - help_msg = ('Test connect URI for libvirt (qemu:///system, ' - 'lxc:///)') - settings.register_option(section, 'connect_uri', - help_msg=help_msg, default=uri_current) + uri_current = get_settings_value(section, "connect_uri", default=None) + help_msg = "Test connect URI for libvirt (qemu:///system, " "lxc:///)" + settings.register_option( + section, "connect_uri", help_msg=help_msg, default=uri_current + ) # [vt.debug] section - help_msg = ('Do not clean up tmp files or VM processes at the end ' - 'of a virt-test execution') - settings.register_option('vt.debug', 'no_cleanup', - help_msg=help_msg, key_type=bool, - default=False) + help_msg = ( + "Do not clean up tmp files or VM processes at the end " + "of a virt-test execution" + ) + settings.register_option( + "vt.debug", + "no_cleanup", + help_msg=help_msg, + key_type=bool, + default=False, + ) # [vt.filter] section - help_msg = ("Allows to selectively skip certain default filters. " - "This uses directly 'tests-shared.cfg' and instead of " - "'$provider/tests.cfg' and applies following lists of " - "default filters, unless they are specified as " - "arguments: no_9p_export,no_virtio_rng," - "no_pci_assignable,smallpages,default_bios,ridge," - "image_backend,multihost. This can be used to eg. run " - "hugepages tests by filtering 'smallpages' via this " - "option.") - settings.register_option('vt.filter', key='default_filters', - nargs='+', default=None, - help_msg=help_msg) + help_msg = ( + "Allows to selectively skip certain default filters. " + "This uses directly 'tests-shared.cfg' and instead of " + "'$provider/tests.cfg' and applies following lists of " + "default filters, unless they are specified as " + "arguments: no_9p_export,no_virtio_rng," + "no_pci_assignable,smallpages,default_bios,ridge," + "image_backend,multihost. This can be used to eg. run " + "hugepages tests by filtering 'smallpages' via this " + "option." + ) + settings.register_option( + "vt.filter", + key="default_filters", + nargs="+", + default=None, + help_msg=help_msg, + ) # [plugins.vtjoblock] section - help_msg = 'Directory in which to write the lock file' - settings.register_option('plugins.vtjoblock', 'dir', - help_msg=help_msg, - default='/tmp') + help_msg = "Directory in which to write the lock file" + settings.register_option( + "plugins.vtjoblock", "dir", help_msg=help_msg, default="/tmp" + ) settings.merge_with_configs() if AVOCADO_LOADER_AVAILABLE: - virt_loader = getattr(importlib.import_module('avocado_vt.loader'), - 'VirtTestLoader') + virt_loader = getattr( + importlib.import_module("avocado_vt.loader"), "VirtTestLoader" + ) loader.register_plugin(virt_loader) diff --git a/avocado_vt/plugins/vt_joblock.py b/avocado_vt/plugins/vt_joblock.py index 7334d44663..eeab55f7f9 100644 --- a/avocado_vt/plugins/vt_joblock.py +++ b/avocado_vt/plugins/vt_joblock.py @@ -25,6 +25,7 @@ class LockCreationError(Exception): """ Represents any error situation when attempting to create a lock file """ + pass @@ -37,13 +38,14 @@ class OtherProcessHoldsLockError(Exception): class VTJobLock(Pre, Post): - name = 'vt-joblock' - description = 'Avocado-VT Job Lock/Unlock' + name = "vt-joblock" + description = "Avocado-VT Job Lock/Unlock" def __init__(self, **kwargs): self.log = logging.getLogger("avocado.app") - lock_dir = get_settings_value("plugins.vtjoblock", "dir", - key_type=str, default='/tmp') + lock_dir = get_settings_value( + "plugins.vtjoblock", "dir", key_type=str, default="/tmp" + ) self.lock_dir = os.path.expanduser(lock_dir) self.lock_file = None @@ -57,17 +59,16 @@ def _create_self_lock_file(self, job): :returns: the full path for the lock file created :rtype: str """ - pattern = 'avocado-vt-joblock-%(jobid)s-%(uid)s-%(random)s.pid' + pattern = "avocado-vt-joblock-%(jobid)s-%(uid)s-%(random)s.pid" # the job unique id is already random, but, let's add yet another one - rand = ''.join([random.choice(string.ascii_lowercase + string.digits) - for i in xrange(8)]) - path = pattern % {'jobid': job.unique_id, - 'uid': os.getuid(), - 'random': rand} + rand = "".join( + [random.choice(string.ascii_lowercase + string.digits) for i in xrange(8)] + ) + path = pattern % {"jobid": job.unique_id, "uid": os.getuid(), "random": rand} path = os.path.join(self.lock_dir, path) try: - with open(path, 'w') as lockfile: + with open(path, "w") as lockfile: lockfile.write("%u" % os.getpid()) return path except Exception as e: @@ -83,10 +84,10 @@ def _get_lock_files(self): """ try: files = os.listdir(self.lock_dir) - pattern = re.compile(r'avocado-vt-joblock-[0-9a-f]{40}-[0-9]+' - '-[0-9a-z]{8}\.pid') - return [os.path.join(self.lock_dir, _) for _ in files - if pattern.match(_)] + pattern = re.compile( + r"avocado-vt-joblock-[0-9a-f]{40}-[0-9]+" "-[0-9a-z]{8}\.pid" + ) + return [os.path.join(self.lock_dir, _) for _ in files if pattern.match(_)] except OSError as e: if e.errno == errno.ENOENT: return [] @@ -97,7 +98,7 @@ def _lock(self, job): lock_files.remove(self.lock_file) for path in lock_files: try: - lock_pid = int(open(path, 'r').read()) + lock_pid = int(open(path, "r").read()) except Exception: msg = 'Cannot read PID from "%s".' % path raise LockCreationError(msg) @@ -121,12 +122,14 @@ def _get_klass_or_none(test_factory): def pre_tests(self, job): try: if job.test_suite is not None: - if hasattr(job.test_suite, 'tests'): + if hasattr(job.test_suite, "tests"): tests = job.test_suite.tests else: tests = job.test_suite - if any(self._get_klass_or_none(test_factory) is VirtTest - for test_factory in tests): + if any( + self._get_klass_or_none(test_factory) is VirtTest + for test_factory in tests + ): self._lock(job) except Exception as detail: msg = "Failure trying to set Avocado-VT job lock: %s" % detail diff --git a/avocado_vt/plugins/vt_list.py b/avocado_vt/plugins/vt_list.py index 067a12afb3..a24d434f8d 100644 --- a/avocado_vt/plugins/vt_list.py +++ b/avocado_vt/plugins/vt_list.py @@ -28,6 +28,7 @@ try: from avocado.core.loader import loader from ..loader import VirtTestLoader + AVOCADO_LOADER_AVAILABLE = True except ImportError: AVOCADO_LOADER_AVAILABLE = False @@ -39,16 +40,17 @@ # the autotest library available in the system. AUTOTEST_PATH = None -if 'AUTOTEST_PATH' in os.environ: - AUTOTEST_PATH = os.path.expanduser(os.environ['AUTOTEST_PATH']) - CLIENT_DIR = os.path.join(os.path.abspath(AUTOTEST_PATH), 'client') - SETUP_MODULES_PATH = os.path.join(CLIENT_DIR, 'setup_modules.py') +if "AUTOTEST_PATH" in os.environ: + AUTOTEST_PATH = os.path.expanduser(os.environ["AUTOTEST_PATH"]) + CLIENT_DIR = os.path.join(os.path.abspath(AUTOTEST_PATH), "client") + SETUP_MODULES_PATH = os.path.join(CLIENT_DIR, "setup_modules.py") if not os.path.exists(SETUP_MODULES_PATH): - raise EnvironmentError("Although AUTOTEST_PATH has been declared, " - "%s missing." % SETUP_MODULES_PATH) - SETUP_MODULES = load_source('autotest_setup_modules', SETUP_MODULES_PATH) - SETUP_MODULES.setup(base_path=CLIENT_DIR, - root_module_name="autotest.client") + raise EnvironmentError( + "Although AUTOTEST_PATH has been declared, " + "%s missing." % SETUP_MODULES_PATH + ) + SETUP_MODULES = load_source("autotest_setup_modules", SETUP_MODULES_PATH) + SETUP_MODULES.setup(base_path=CLIENT_DIR, root_module_name="autotest.client") # The code below is used by this plugin to find the virt test directory, # so that it can load the virttest python lib, used by the plugin code. @@ -56,27 +58,29 @@ # fail to load. VIRT_TEST_PATH = None -if 'VIRT_TEST_PATH' in os.environ: - VIRT_TEST_PATH = os.environ['VIRT_TEST_PATH'] +if "VIRT_TEST_PATH" in os.environ: + VIRT_TEST_PATH = os.environ["VIRT_TEST_PATH"] else: - VIRT_TEST_PATH = get_settings_value(section='virt_test', - key='virt_test_path', default=None) + VIRT_TEST_PATH = get_settings_value( + section="virt_test", key="virt_test_path", default=None + ) if VIRT_TEST_PATH is not None: sys.path.append(os.path.expanduser(VIRT_TEST_PATH)) -from virttest import data_dir # pylint: disable=C0413 +from virttest import data_dir # pylint: disable=C0413 -_PROVIDERS_DOWNLOAD_DIR = os.path.join(data_dir.get_test_providers_dir(), - 'downloads') +_PROVIDERS_DOWNLOAD_DIR = os.path.join(data_dir.get_test_providers_dir(), "downloads") try: assert len(os.listdir(_PROVIDERS_DOWNLOAD_DIR)) != 0 except (OSError, AssertionError): - raise EnvironmentError("Bootstrap missing. " - "Execute 'avocado vt-bootstrap' or disable this " - "plugin to get rid of this message") + raise EnvironmentError( + "Bootstrap missing. " + "Execute 'avocado vt-bootstrap' or disable this " + "plugin to get rid of this message" + ) class VTLister(CLI): @@ -85,7 +89,7 @@ class VTLister(CLI): Avocado VT - implements legacy virt-test listing """ - name = 'vt-list' + name = "vt-list" description = "Avocado-VT/virt-test support for 'list' command" def configure(self, parser): @@ -94,32 +98,41 @@ def configure(self, parser): :param parser: Main test runner parser. """ - list_subcommand_parser = parser.subcommands.choices.get('list', None) + list_subcommand_parser = parser.subcommands.choices.get("list", None) if list_subcommand_parser is None: return vt_compat_group_lister = list_subcommand_parser.add_argument_group( - 'Virt-Test compat layer - Lister options') - - help_msg = ("Also list the available guests (this option ignores the " - "--vt-config and --vt-guest-os)") - add_option(parser=vt_compat_group_lister, - dest='vt.list_guests', - arg='--vt-list-guests', - action='store_true', - default=False, - help=help_msg) - - help_msg = ("Also list the available arch/machines for the given guest" - " OS. (Use \"--vt-guest-os ''\" to see all combinations; " - "--vt-config --vt-machine-type and --vt-arch args are " - "ignored)") - add_option(parser=vt_compat_group_lister, - dest='vt.list_archs', - arg='--vt-list-archs', - action='store_true', - default=False, - help=help_msg) + "Virt-Test compat layer - Lister options" + ) + + help_msg = ( + "Also list the available guests (this option ignores the " + "--vt-config and --vt-guest-os)" + ) + add_option( + parser=vt_compat_group_lister, + dest="vt.list_guests", + arg="--vt-list-guests", + action="store_true", + default=False, + help=help_msg, + ) + + help_msg = ( + "Also list the available arch/machines for the given guest" + " OS. (Use \"--vt-guest-os ''\" to see all combinations; " + "--vt-config --vt-machine-type and --vt-arch args are " + "ignored)" + ) + add_option( + parser=vt_compat_group_lister, + dest="vt.list_archs", + arg="--vt-list-archs", + action="store_true", + default=False, + help=help_msg, + ) add_basic_vt_options(vt_compat_group_lister) add_qemu_bin_vt_option(vt_compat_group_lister) diff --git a/avocado_vt/plugins/vt_list_archs.py b/avocado_vt/plugins/vt_list_archs.py index ed05efdb86..1ee21ca8a8 100644 --- a/avocado_vt/plugins/vt_list_archs.py +++ b/avocado_vt/plugins/vt_list_archs.py @@ -11,16 +11,14 @@ class VTListArchs(CLICmd): Avocado VT - implements vt-list-archs command """ - name = 'vt-list-archs' + name = "vt-list-archs" description = "Avocado-VT 'vt-list-archs' command" def configure(self, parser): parser = super(VTListArchs, self).configure(parser) # Expose the --vt-type option, as the archs definitions depend on it - add_option(parser=parser, dest='vt.type', arg='--vt-type') + add_option(parser=parser, dest="vt.type", arg="--vt-type") def run(self, config): - guest_name_parser = get_guest_name_parser(config, - arch=None, - machine=None) + guest_name_parser = get_guest_name_parser(config, arch=None, machine=None) arch_listing(config, guest_name_parser) diff --git a/avocado_vt/plugins/vt_list_guests.py b/avocado_vt/plugins/vt_list_guests.py index ff1c7d9769..9194ae0a9b 100644 --- a/avocado_vt/plugins/vt_list_guests.py +++ b/avocado_vt/plugins/vt_list_guests.py @@ -12,28 +12,29 @@ class VTListGuests(CLICmd): Avocado VT - implements vt-list-guests command """ - name = 'vt-list-guests' + name = "vt-list-guests" description = "Avocado-VT 'vt-list-guests' command" def configure(self, parser): parser = super(VTListGuests, self).configure(parser) # [vt.list_guests] section - section = 'vt.list_guests' + section = "vt.list_guests" - key = 'guest_os' + key = "guest_os" if is_registering_settings_required(): - settings.register_option(section, key=key, default=None, - help_msg='List only specific guests') + settings.register_option( + section, key=key, default=None, help_msg="List only specific guests" + ) namespace = "%s.%s" % (section, key) - add_option(parser=parser, dest=namespace, arg='--guest-os') + add_option(parser=parser, dest=namespace, arg="--guest-os") # Also expose the --vt-type option, as the guests depend on it - add_option(parser=parser, dest='vt.type', arg='--vt-type') + add_option(parser=parser, dest="vt.type", arg="--vt-type") def run(self, config): guest_name_parser = get_guest_name_parser( - config, - guest_os='vt.list_guests.guest_os') + config, guest_os="vt.list_guests.guest_os" + ) guest_listing(config, guest_name_parser) diff --git a/avocado_vt/plugins/vt_resolver.py b/avocado_vt/plugins/vt_resolver.py index 72dcd3810b..49366e53cf 100644 --- a/avocado_vt/plugins/vt_resolver.py +++ b/avocado_vt/plugins/vt_resolver.py @@ -1,8 +1,7 @@ import warnings from avocado.core.plugin_interfaces import Discoverer, Resolver -from avocado.core.resolver import (ReferenceResolution, - ReferenceResolutionResult) +from avocado.core.resolver import ReferenceResolution, ReferenceResolutionResult from avocado.core.settings import settings from ..discovery import DiscoveryMixIn @@ -14,54 +13,62 @@ class VTResolverUtils(DiscoveryMixIn): - def __init__(self, config): self.config = config or settings.as_dict() def _parameters_to_runnable(self, params): params = self.convert_parameters(params) - uri = params.get('name') - vt_params = params.get('vt_params') + uri = params.get("name") + vt_params = params.get("vt_params") # Flatten the vt_params, discarding the attributes that are not # scalars, and will not be used in the context of nrunner - for key in ('_name_map_file', '_short_name_map_file', 'dep'): + for key in ("_name_map_file", "_short_name_map_file", "dep"): if key in vt_params: - del (vt_params[key]) + del vt_params[key] - return Runnable('avocado-vt', uri, **vt_params) + return Runnable("avocado-vt", uri, **vt_params) def _get_reference_resolution(self, reference): cartesian_parser = self._get_parser() self._save_parser_cartesian_config(cartesian_parser) - if reference != '': + if reference != "": cartesian_parser.only_filter(reference) - runnables = [self._parameters_to_runnable(d) for d in - cartesian_parser.get_dicts()] + runnables = [ + self._parameters_to_runnable(d) for d in cartesian_parser.get_dicts() + ] if runnables: - if self.config.get( - "run.max_parallel_tasks", self.config.get( - "nrunner.max_parallel_tasks", 1)) != 1: - if self.config.get( - "run.spawner", self.config.get( - "nrunner.spawner", "process")) != "lxc": - warnings.warn("The VT NextRunner can be run only " - "with max-parallel-tasks set to 1 with a process " - "spawner, did you forget to use an LXC spawner?") - return ReferenceResolution(reference, - ReferenceResolutionResult.SUCCESS, - runnables) + if ( + self.config.get( + "run.max_parallel_tasks", + self.config.get("nrunner.max_parallel_tasks", 1), + ) + != 1 + ): + if ( + self.config.get( + "run.spawner", self.config.get("nrunner.spawner", "process") + ) + != "lxc" + ): + warnings.warn( + "The VT NextRunner can be run only " + "with max-parallel-tasks set to 1 with a process " + "spawner, did you forget to use an LXC spawner?" + ) + return ReferenceResolution( + reference, ReferenceResolutionResult.SUCCESS, runnables + ) else: - return ReferenceResolution(reference, - ReferenceResolutionResult.NOTFOUND) + return ReferenceResolution(reference, ReferenceResolutionResult.NOTFOUND) class VTResolver(VTResolverUtils, Resolver): - name = 'vt' - description = 'Test resolver for Avocado-VT tests' + name = "vt" + description = "Test resolver for Avocado-VT tests" def resolve(self, reference): """ @@ -75,11 +82,11 @@ def resolve(self, reference): class VTDiscoverer(Discoverer, VTResolverUtils): - name = 'vt-discoverer' - description = 'Test discoverer for Avocado-VT tests' + name = "vt-discoverer" + description = "Test discoverer for Avocado-VT tests" def discover(self): """It will discover vt test resolutions from cartesian config.""" self.config = self.config or settings.as_dict() - return [self._get_reference_resolution('')] + return [self._get_reference_resolution("")] diff --git a/avocado_vt/plugins/vt_runner.py b/avocado_vt/plugins/vt_runner.py index 14e94c0cff..49b68f047a 100644 --- a/avocado_vt/plugins/vt_runner.py +++ b/avocado_vt/plugins/vt_runner.py @@ -12,31 +12,35 @@ # Compatibility with avocado 92.0 LTS version, this can be removed when # the 92.0 support will be dropped. try: - from avocado.core.nrunner import (BaseRunner, - BaseRunnerApp, - RUNNER_RUN_CHECK_INTERVAL) + from avocado.core.nrunner import ( + BaseRunner, + BaseRunnerApp, + RUNNER_RUN_CHECK_INTERVAL, + ) from avocado.core.nrunner import main as nrunner_main from avocado.core.runners.utils import messages + LTS = True except ImportError: from avocado.core.nrunner.app import BaseRunnerApp - from avocado.core.nrunner.runner import (BaseRunner, - RUNNER_RUN_CHECK_INTERVAL) + from avocado.core.nrunner.runner import BaseRunner, RUNNER_RUN_CHECK_INTERVAL from avocado.core.utils import messages + LTS = False class VirtTest(test.VirtTest): - def __init__(self, queue, runnable): self.queue = queue - base_logdir = getattr(runnable, 'output_dir', None) + base_logdir = getattr(runnable, "output_dir", None) vt_params = runnable.kwargs - vt_params['job_env_cleanup'] = 'no' - kwargs = {'name': TestID(1, runnable.uri), - 'config': runnable.config, - 'base_logdir': base_logdir, - 'vt_params': vt_params} + vt_params["job_env_cleanup"] = "no" + kwargs = { + "name": TestID(1, runnable.uri), + "config": runnable.config, + "base_logdir": base_logdir, + "vt_params": vt_params, + } super().__init__(**kwargs) def _save_log_dir(self): @@ -47,7 +51,7 @@ def _save_log_dir(self): basedir = os.path.relpath(root, start=self.logdir) for file in files: file_path = os.path.join(root, file) - with open(file_path, 'rb') as f: + with open(file_path, "rb") as f: base_path = os.path.join(basedir, file) while True: # Read data in manageable chunks rather than @@ -55,8 +59,7 @@ def _save_log_dir(self): in_data = f.read(200000) if not in_data: break - self.queue.put(messages.FileMessage.get(in_data, - base_path)) + self.queue.put(messages.FileMessage.get(in_data, base_path)) def runTest(self): status = "PASS" @@ -71,7 +74,7 @@ def runTest(self): raise self.__status # pylint: disable-msg=E0702 except exceptions.TestBaseException as detail: status = detail.status - fail_reason = (astring.to_text(detail)) + fail_reason = astring.to_text(detail) fail_class = detail.__class__.__name__ traceback_log = traceback.format_exc() if status == "ERROR" or status not in teststatus.STATUSES: @@ -82,23 +85,29 @@ def runTest(self): except Exception as detail: status = "ERROR" try: - fail_reason = (astring.to_text(detail)) + fail_reason = astring.to_text(detail) fail_class = detail.__class__.__name__ traceback_log = traceback.format_exc() except TypeError: - fail_reason = ("Unable to get exception, check the traceback " - "in `debug.log` for details.") + fail_reason = ( + "Unable to get exception, check the traceback " + "in `debug.log` for details." + ) self.queue.put(messages.StderrMessage.get(traceback_log)) finally: self.queue.put(messages.WhiteboardMessage.get(self.whiteboard)) - if 'avocado_test_' in self.logdir: + if "avocado_test_" in self.logdir: self._save_log_dir() try: - self.queue.put(messages.FinishedMessage.get(status, - fail_reason=fail_reason, - class_name="VirtTest", - fail_class=fail_class, - traceback=traceback_log)) + self.queue.put( + messages.FinishedMessage.get( + status, + fail_reason=fail_reason, + class_name="VirtTest", + fail_class=fail_class, + traceback=traceback_log, + ) + ) except TypeError: self.queue.put(messages.FinishedMessage.get(status, fail_reason)) @@ -115,13 +124,16 @@ class VTTestRunner(BaseRunner): * kwargs: all the VT specific parameters """ - name = 'avocado-vt' - description = 'nrunner application for Avocado-VT tests' - CONFIGURATION_USED = ['datadir.paths.cache_dirs', - 'core.show', - 'job.output.loglevel', - 'job.run.store_logging_stream'] + name = "avocado-vt" + description = "nrunner application for Avocado-VT tests" + + CONFIGURATION_USED = [ + "datadir.paths.cache_dirs", + "core.show", + "job.output.loglevel", + "job.run.store_logging_stream", + ] DEFAULT_TIMEOUT = 86400 @@ -130,12 +142,16 @@ def run(self, runnable=None): self.runnable = runnable yield messages.StartedMessage.get() - if self.runnable.config.get( - "run.max_parallel_tasks", self.runnable.config.get( - "nrunner.max_parallel_tasks", 1)) != 1: - yield messages.FinishedMessage.get('cancel', - fail_reason="parallel run is not" - " allowed for vt tests") + if ( + self.runnable.config.get( + "run.max_parallel_tasks", + self.runnable.config.get("nrunner.max_parallel_tasks", 1), + ) + != 1 + ): + yield messages.FinishedMessage.get( + "cancel", fail_reason="parallel run is not" " allowed for vt tests" + ) else: try: queue = multiprocessing.SimpleQueue() @@ -149,22 +165,20 @@ def run(self, runnable=None): else: message = queue.get() yield message - if message.get('status') == 'finished': + if message.get("status") == "finished": break except Exception: yield messages.StderrMessage.get(traceback.format_exc()) - yield messages.FinishedMessage.get('error') + yield messages.FinishedMessage.get("error") class RunnerApp(BaseRunnerApp): - PROG_NAME = 'avocado-runner-avocado-vt' - PROG_DESCRIPTION = 'nrunner application for Avocado-VT tests' + PROG_NAME = "avocado-runner-avocado-vt" + PROG_DESCRIPTION = "nrunner application for Avocado-VT tests" if LTS: - RUNNABLE_KINDS_CAPABLE = { - 'avocado-vt': VTTestRunner - } + RUNNABLE_KINDS_CAPABLE = {"avocado-vt": VTTestRunner} else: - RUNNABLE_KINDS_CAPABLE = ['avocado-vt'] + RUNNABLE_KINDS_CAPABLE = ["avocado-vt"] def main(): @@ -175,5 +189,5 @@ def main(): app.run() -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/avocado_vt/plugins/vt_settings.py b/avocado_vt/plugins/vt_settings.py index 1d12014369..070c957523 100644 --- a/avocado_vt/plugins/vt_settings.py +++ b/avocado_vt/plugins/vt_settings.py @@ -24,10 +24,11 @@ class VTSettings(Settings): - def adjust_settings_paths(self, paths): - base = resource_filename('avocado_vt', 'conf.d') - for path in [os.path.join(base, conf) - for conf in resource_listdir('avocado_vt', 'conf.d') - if conf.endswith('.conf')]: + base = resource_filename("avocado_vt", "conf.d") + for path in [ + os.path.join(base, conf) + for conf in resource_listdir("avocado_vt", "conf.d") + if conf.endswith(".conf") + ]: paths.insert(0, path) diff --git a/avocado_vt/test.py b/avocado_vt/test.py index b285f29247..5cd145761b 100644 --- a/avocado_vt/test.py +++ b/avocado_vt/test.py @@ -48,16 +48,17 @@ # users to specify their autotest from a git clone location. AUTOTEST_PATH = None -if 'AUTOTEST_PATH' in os.environ: - AUTOTEST_PATH = os.path.expanduser(os.environ['AUTOTEST_PATH']) - CLIENT_DIR = os.path.join(os.path.abspath(AUTOTEST_PATH), 'client') - SETUP_MODULES_PATH = os.path.join(CLIENT_DIR, 'setup_modules.py') +if "AUTOTEST_PATH" in os.environ: + AUTOTEST_PATH = os.path.expanduser(os.environ["AUTOTEST_PATH"]) + CLIENT_DIR = os.path.join(os.path.abspath(AUTOTEST_PATH), "client") + SETUP_MODULES_PATH = os.path.join(CLIENT_DIR, "setup_modules.py") if not os.path.exists(SETUP_MODULES_PATH): - raise EnvironmentError("Although AUTOTEST_PATH has been declared, " - "%s missing." % SETUP_MODULES_PATH) - SETUP_MODULES = load_source('autotest_setup_modules', SETUP_MODULES_PATH) - SETUP_MODULES.setup(base_path=CLIENT_DIR, - root_module_name="autotest.client") + raise EnvironmentError( + "Although AUTOTEST_PATH has been declared, " + "%s missing." % SETUP_MODULES_PATH + ) + SETUP_MODULES = load_source("autotest_setup_modules", SETUP_MODULES_PATH) + SETUP_MODULES.setup(base_path=CLIENT_DIR, root_module_name="autotest.client") def cleanup_env(env_filename, env_version): @@ -89,7 +90,7 @@ def __init__(self, **kwargs): """ self.__vt_params = None self.bindir = data_dir.get_root_dir() - self.virtdir = os.path.join(self.bindir, 'shared') + self.virtdir = os.path.join(self.bindir, "shared") # self.__vt_params must be initialized after super vt_params = utils_params.Params(kwargs.pop("vt_params", None)) # for timeout use Avocado-vt timeout as default but allow @@ -103,11 +104,12 @@ def __init__(self, **kwargs): self.background_errors.clear() if "methodName" not in kwargs: - kwargs["methodName"] = 'runTest' + kwargs["methodName"] = "runTest" super(VirtTest, self).__init__(**kwargs) - self.builddir = os.path.join(self.workdir, 'backends', - vt_params.get("vm_type", "")) + self.builddir = os.path.join( + self.workdir, "backends", vt_params.get("vm_type", "") + ) self.tmpdir = os.path.dirname(self.workdir) self.__vt_params = vt_params self.debugdir = self.logdir @@ -140,8 +142,8 @@ def setUp(self): Avocado to allow skips let's say our tests run during setUp phase and report the status in test. """ - env_lang = os.environ.get('LANG') - os.environ['LANG'] = 'C' + env_lang = os.environ.get("LANG") + os.environ["LANG"] = "C" try: self._runTest() self.__status = "PASS" @@ -149,18 +151,21 @@ def setUp(self): # into avocado (skips, warns and errors will display correctly) except exceptions.TestSkipError: self.__exc_info = sys.exc_info() - raise # This one has to be raised in setUp + raise # This one has to be raised in setUp except: # nopep8 Old-style exceptions are not inherited from Exception() self.__exc_info = sys.exc_info() self.__status = self.__exc_info[1] finally: # Clean libvirtd debug logs if the test is not fail or error if self.params.get("libvirtd_log_cleanup", "no") == "yes": - if (self.params.get("vm_type") == 'libvirt' and - self.params.get("enable_libvirtd_debug_log", "yes") == "yes"): + if ( + self.params.get("vm_type") == "libvirt" + and self.params.get("enable_libvirtd_debug_log", "yes") == "yes" + ): libvirtd_log = self.params["libvirtd_debug_file"] - if ("TestFail" not in str(self.__exc_info) and - "TestError" not in str(self.__exc_info)): + if "TestFail" not in str( + self.__exc_info + ) and "TestError" not in str(self.__exc_info): if libvirtd_log and os.path.isfile(libvirtd_log): self.log.info("cleaning libvirtd logs...") os.remove(libvirtd_log) @@ -168,26 +173,31 @@ def setUp(self): # tar the libvirtd log and archive self.log.info("archiving libvirtd debug logs") from virttest import utils_package + if utils_package.package_install("tar"): if os.path.isfile(libvirtd_log): - archive = os.path.join(os.path.dirname( - libvirtd_log), "libvirtd.tar.gz") - cmd = ("tar -zcf %s -P %s" - % (shlex.quote(archive), - shlex.quote(libvirtd_log))) + archive = os.path.join( + os.path.dirname(libvirtd_log), "libvirtd.tar.gz" + ) + cmd = "tar -zcf %s -P %s" % ( + shlex.quote(archive), + shlex.quote(libvirtd_log), + ) if process.system(cmd) == 0: os.remove(libvirtd_log) else: - self.log.error("Unable to find log file: %s", - libvirtd_log) + self.log.error( + "Unable to find log file: %s", libvirtd_log + ) else: - self.log.error("Unable to find tar to compress libvirtd " - "logs") + self.log.error( + "Unable to find tar to compress libvirtd " "logs" + ) if env_lang: - os.environ['LANG'] = env_lang + os.environ["LANG"] = env_lang else: - del os.environ['LANG'] + del os.environ["LANG"] def runTest(self): """ @@ -211,21 +221,25 @@ def _runTest(self): self._log_parameters() # Warn of this special condition in related location in output & logs - if os.getuid() == 0 and params.get('nettype', 'user') == 'user': + if os.getuid() == 0 and params.get("nettype", "user") == "user": self.log.warning("") - self.log.warning("Testing with nettype='user' while running " - "as root may produce unexpected results!!!") + self.log.warning( + "Testing with nettype='user' while running " + "as root may produce unexpected results!!!" + ) self.log.warning("") subtest_dirs = self._get_subtest_dirs() # Get the test routine corresponding to the specified # test type - self.log.debug("Searching for test modules that match " - "'type = %s' and 'provider = %s' " - "on this cartesian dict", - params.get("type"), - params.get("provider", None)) + self.log.debug( + "Searching for test modules that match " + "'type = %s' and 'provider = %s' " + "on this cartesian dict", + params.get("type"), + params.get("provider", None), + ) t_types = params.get("type").split() @@ -234,13 +248,16 @@ def _runTest(self): test_modules = utils.find_test_modules(t_types, subtest_dirs) # Open the environment file - env_filename = os.path.join(data_dir.get_tmp_dir(), - params.get("env", "env")) + env_filename = os.path.join(data_dir.get_tmp_dir(), params.get("env", "env")) env = utils_env.Env(env_filename, self.env_version) if params.get_boolean("job_env_cleanup", "yes"): - self.runner_queue.put({"func_at_exit": cleanup_env, - "args": (env_filename, self.env_version), - "once": True}) + self.runner_queue.put( + { + "func_at_exit": cleanup_env, + "args": (env_filename, self.env_version), + "once": True, + } + ) test_passed = False t_type = None @@ -258,7 +275,8 @@ def _runTest(self): for t_type in t_types: test_module = test_modules[t_type] run_func = utils_misc.get_test_entrypoint_func( - t_type, test_module) + t_type, test_module + ) try: # pylint: disable-next=E1102 run_func(self, params, env) @@ -268,11 +286,12 @@ def _runTest(self): test_passed = True error_message = funcatexit.run_exitfuncs(env, t_type) if error_message: - raise exceptions.TestWarn("funcatexit failed with: %s" % - error_message) + raise exceptions.TestWarn( + "funcatexit failed with: %s" % error_message + ) except: # nopep8 Old-style exceptions are not inherited from Exception() - stacktrace.log_exc_info(sys.exc_info(), 'avocado.test') + stacktrace.log_exc_info(sys.exc_info(), "avocado.test") if t_type is not None: error_message = funcatexit.run_exitfuncs(env, t_type) if error_message: @@ -287,20 +306,23 @@ def _runTest(self): # Post-process try: try: - params['test_passed'] = str(test_passed) + params["test_passed"] = str(test_passed) env_process.postprocess(self, params, env) except: # nopep8 Old-style exceptions are not inherited from Exception() - stacktrace.log_exc_info(sys.exc_info(), - 'avocado.test') + stacktrace.log_exc_info(sys.exc_info(), "avocado.test") if test_passed: raise - self.log.error("Exception raised during " - "postprocessing: %s", - sys.exc_info()[1]) + self.log.error( + "Exception raised during " "postprocessing: %s", + sys.exc_info()[1], + ) finally: - if self._safe_env_save(env) or params.get("env_cleanup", "no") == "yes": - env.destroy() # Force-clean as it can't be stored + if ( + self._safe_env_save(env) + or params.get("env_cleanup", "no") == "yes" + ): + env.destroy() # Force-clean as it can't be stored except Exception as e: if params.get("abort_on_error") != "yes": @@ -313,10 +335,15 @@ def _runTest(self): continue self.log.info("VM '%s' is alive.", vm.name) for m in vm.monitors: - self.log.info("It has a %s monitor unix socket at: %s", - m.protocol, m.filename) - self.log.info("The command line used to start it was:\n%s", - vm.make_create_command()) + self.log.info( + "It has a %s monitor unix socket at: %s", + m.protocol, + m.filename, + ) + self.log.info( + "The command line used to start it was:\n%s", + vm.make_create_command(), + ) raise exceptions.JobError("Abort requested (%s)" % e) return test_passed diff --git a/avocado_vt/utils.py b/avocado_vt/utils.py index 7e37ba479b..14ad79bfd9 100644 --- a/avocado_vt/utils.py +++ b/avocado_vt/utils.py @@ -57,10 +57,8 @@ def find_subtest_dirs(other_subtests_dirs, bindir, ignore_files=None): # else the relative path will be searched in the bin_dir subtestdir = os.path.join(bindir, d, "tests") if not os.path.isdir(subtestdir): - raise exceptions.TestError("Directory %s does not " - "exist" % subtestdir) - subtest_dirs += data_dir.SubdirList(subtestdir, - ignore_files) + raise exceptions.TestError("Directory %s does not " "exist" % subtestdir) + subtest_dirs += data_dir.SubdirList(subtestdir, ignore_files) return subtest_dirs @@ -75,14 +73,12 @@ def find_generic_specific_subtest_dirs(vm_type, ignore_files=None): :type ignore_files: list or None """ subtest_dirs = [] - generic_subdirs = asset.get_test_provider_subdirs('generic') + generic_subdirs = asset.get_test_provider_subdirs("generic") for generic_subdir in generic_subdirs: - subtest_dirs += data_dir.SubdirList(generic_subdir, - ignore_files) + subtest_dirs += data_dir.SubdirList(generic_subdir, ignore_files) specific_subdirs = asset.get_test_provider_subdirs(vm_type) for specific_subdir in specific_subdirs: - subtest_dirs += data_dir.SubdirList(specific_subdir, - ignore_files) + subtest_dirs += data_dir.SubdirList(specific_subdir, ignore_files) return subtest_dirs @@ -96,10 +92,10 @@ def find_provider_subtest_dirs(provider, ignore_files=None): """ subtests_dirs = [] provider_info = asset.get_test_provider_info(provider) - for key in provider_info['backends']: + for key in provider_info["backends"]: subtests_dirs += data_dir.SubdirList( - provider_info['backends'][key]['path'], - ignore_files) + provider_info["backends"][key]["path"], ignore_files + ) return subtests_dirs @@ -116,13 +112,14 @@ def find_test_modules(test_types, subtest_dirs): for d in subtest_dirs: module_path = os.path.join(d, "%s.py" % test_type) if os.path.isfile(module_path): - logging.debug("Found subtest module %s", - module_path) + logging.debug("Found subtest module %s", module_path) subtest_dir = d break else: - msg = ("Could not find test file %s.py on test" - "dirs %s" % (test_type, subtest_dirs)) + msg = "Could not find test file %s.py on test" "dirs %s" % ( + test_type, + subtest_dirs, + ) raise exceptions.TestError(msg) # Load the test module test_modules[test_type] = import_module(test_type, subtest_dir) @@ -146,11 +143,12 @@ def _safe_env_save(self, env): try: pickle.dumps(env.data) except Exception: - self.log.warn("Unable to save environment: %s", - stacktrace.str_unpickable_object(env.data)) + self.log.warn( + "Unable to save environment: %s", + stacktrace.str_unpickable_object(env.data), + ) else: - self.log.warn("Unable to save environment: %s (%s)", details, - env.data) + self.log.warn("Unable to save environment: %s (%s)", details, env.data) return True return False @@ -169,15 +167,15 @@ def _get_subtest_dirs(self): Get list of directories containing subtests. """ test_filter = bootstrap.test_filter - subtest_dirs = find_subtest_dirs(self.params.get("other_tests_dirs", - ""), - self.bindir, - test_filter) + subtest_dirs = find_subtest_dirs( + self.params.get("other_tests_dirs", ""), self.bindir, test_filter + ) provider = self.params.get("provider", None) if provider is None: subtest_dirs += find_generic_specific_subtest_dirs( - self.params.get("vm_type"), test_filter) + self.params.get("vm_type"), test_filter + ) else: subtest_dirs += find_provider_subtest_dirs(provider, test_filter) return subtest_dirs @@ -196,18 +194,20 @@ def verify_background_errors(self): error_messages = [] for index, error in enumerate(bg_errors): error_messages.append( - "- ERROR #%d -\n%s" % (index, "".join( - traceback.format_exception(*error) - ))) + "- ERROR #%d -\n%s" + % (index, "".join(traceback.format_exception(*error))) + ) if error_messages: error_messages.insert(0, "BACKGROUND ERROR LIST:") - genio.write_file(err_file_path, '\n'.join(error_messages)) + genio.write_file(err_file_path, "\n".join(error_messages)) if bg_errors: msg = ["Background error"] msg.append("s are" if len(bg_errors) > 1 else " is") - msg.append((" detected, please refer to file: " - "'%s' for more details.") % BG_ERR_FILE) - self.error(''.join(msg)) + msg.append( + (" detected, please refer to file: " "'%s' for more details.") + % BG_ERR_FILE + ) + self.error("".join(msg)) @property def datadir(self): diff --git a/docs/source/conf.py b/docs/source/conf.py index 2d0a02bf7a..54fa8d7846 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -20,12 +20,12 @@ def getstatusoutput(cmd): """Return (status, output) of executing cmd in a shell.""" - pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r') + pipe = os.popen("{ " + cmd + "; } 2>&1", "r") text = pipe.read() sts = pipe.close() if sts is None: sts = 0 - if text[-1:] == '\n': + if text[-1:] == "\n": text = text[:-1] return sts, text @@ -39,62 +39,83 @@ class DocBuildError(Exception): # documentation root, use os.path.abspath to make it absolute, like shown here. root_path = os.path.abspath(os.path.join("..", "..")) sys.path.insert(0, root_path) -_sphinx_apidoc = getstatusoutput('which sphinx-apidoc')[1].strip() -_output_dir = os.path.join(root_path, 'docs', 'source', 'api') -_api_dir = os.path.join(root_path, 'virttest') -_status, _output = getstatusoutput("%s -o %s %s" % (_sphinx_apidoc, _output_dir, _api_dir)) +_sphinx_apidoc = getstatusoutput("which sphinx-apidoc")[1].strip() +_output_dir = os.path.join(root_path, "docs", "source", "api") +_api_dir = os.path.join(root_path, "virttest") +_status, _output = getstatusoutput( + "%s -o %s %s" % (_sphinx_apidoc, _output_dir, _api_dir) +) if _status: raise DocBuildError("API rst auto generation failed: %s" % _output) -extensions = ['sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.coverage'] +extensions = [ + "sphinx.ext.autodoc", + "sphinx.ext.intersphinx", + "sphinx.ext.todo", + "sphinx.ext.coverage", +] -master_doc = 'index' -project = u'Avocado VT' -copyright = u'2014, Red Hat' +master_doc = "index" +project = "Avocado VT" +copyright = "2014, Red Hat" -version_file = os.path.join(root_path, 'VERSION') -VERSION = open(version_file, 'r').read().strip() +version_file = os.path.join(root_path, "VERSION") +VERSION = open(version_file, "r").read().strip() version = VERSION release = VERSION # on_rtd is whether we are on readthedocs.org, this line of code grabbed from # docs.readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' +on_rtd = os.environ.get("READTHEDOCS", None) == "True" if not on_rtd: # only import and set the theme if we're building docs locally try: import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' + + html_theme = "sphinx_rtd_theme" html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] except ImportError: - html_theme = 'default' + html_theme = "default" -htmlhelp_basename = 'avocadodoc' +htmlhelp_basename = "avocadodoc" latex_documents = [ - ('index', 'avocado.tex', - u'Avocado Virt Test Compatibility Layer Documentation', - u'Lucas Meneghel Rodrigues', 'manual'), + ( + "index", + "avocado.tex", + "Avocado Virt Test Compatibility Layer Documentation", + "Lucas Meneghel Rodrigues", + "manual", + ), ] man_pages = [ - ('index', 'avocado', u'Avocado Virt Test Compatibility Layer Documentation', - [u'Lucas Meneghel Rodrigues'], 1) + ( + "index", + "avocado", + "Avocado Virt Test Compatibility Layer Documentation", + ["Lucas Meneghel Rodrigues"], + 1, + ) ] texinfo_documents = [ - ('index', 'avocado', u'Avocado Virt Test Compatibility Layer', - u'Lucas Meneghel Rodrigues', 'avocado', - 'Avocado Virtualization Testing (VT) plugin', - 'Testing'), + ( + "index", + "avocado", + "Avocado Virt Test Compatibility Layer", + "Lucas Meneghel Rodrigues", + "avocado", + "Avocado Virtualization Testing (VT) plugin", + "Testing", + ), ] # Example configuration for intersphinx: refer to the Python standard library. -intersphinx_mapping = {'http://docs.python.org/': None, - 'http://avocado-framework.readthedocs.org/en/latest/': None} +intersphinx_mapping = { + "http://docs.python.org/": None, + "http://avocado-framework.readthedocs.org/en/latest/": None, +} -autoclass_content = 'both' -highlight_language = 'none' +autoclass_content = "both" +highlight_language = "none" diff --git a/docs/source/contributing/Guidelines.rst b/docs/source/contributing/Guidelines.rst index 995455764b..7d6e25606b 100644 --- a/docs/source/contributing/Guidelines.rst +++ b/docs/source/contributing/Guidelines.rst @@ -77,7 +77,7 @@ Rules for Maintainers Rules for Contributors ====================== -1. [Must] Coding style compliance, for example, align with inspekt tool, pep8 +1. [Must] Coding style should conform to what's enforced by black (see ``selftests/style.sh``) 2. [Must] PR commit message is meaningful. Refer to the link on how to write a good commit message 3. [Must] Travis CI pass and no conflict 4. [Must] Provide test results. If no, provide justification. Apply to any PR diff --git a/examples/tests/guest_hostname.py b/examples/tests/guest_hostname.py index fb5ebaf0db..b50b74acba 100644 --- a/examples/tests/guest_hostname.py +++ b/examples/tests/guest_hostname.py @@ -7,7 +7,7 @@ import logging -LOG = logging.getLogger('avocado.vt.examples.guest_hostname') +LOG = logging.getLogger("avocado.vt.examples.guest_hostname") def run(test, params, env): diff --git a/examples/tests/hostname.py b/examples/tests/hostname.py index 82531a071f..a987a6b1be 100644 --- a/examples/tests/hostname.py +++ b/examples/tests/hostname.py @@ -11,7 +11,7 @@ from avocado.utils import process -LOG = logging.getLogger('avocado.vt.examples.hostname') +LOG = logging.getLogger("avocado.vt.examples.hostname") def run(test, params, env): @@ -23,5 +23,4 @@ def run(test, params, env): :param env: Dictionary with test environment. """ result = process.run("hostname") - LOG.info("Output of 'hostname' cmd is '%s'", - result.stdout_text) + LOG.info("Output of 'hostname' cmd is '%s'", result.stdout_text) diff --git a/examples/tests/ls_disk.py b/examples/tests/ls_disk.py index 0022964ec0..116d38a4d5 100644 --- a/examples/tests/ls_disk.py +++ b/examples/tests/ls_disk.py @@ -15,7 +15,7 @@ import logging -LOG = logging.getLogger('avocado.vt.examples.lsdisk') +LOG = logging.getLogger("avocado.vt.examples.lsdisk") def run(test, params, env): diff --git a/examples/tests/service.py b/examples/tests/service.py index b9489156c7..0142a04d40 100644 --- a/examples/tests/service.py +++ b/examples/tests/service.py @@ -17,7 +17,7 @@ from virttest import error_context -LOG = logging.getLogger('avocado.vt.examples.service') +LOG = logging.getLogger("avocado.vt.examples.service") # error_context.context_aware decorator initializes context, which provides additional @@ -37,7 +37,7 @@ def run(test, params, env): :param params: Dictionary with the test parameters :param env: Dictionary with test environment. """ - if params.get('test_on_guest') == "yes": + if params.get("test_on_guest") == "yes": # error_context.context() is common method to log test steps used to verify # what exactly was tested. error_context.context("Using guest.", LOG.info) @@ -53,8 +53,7 @@ def run(test, params, env): error_context.context("Initialize service manager", LOG.info) service = SpecificServiceManager(params["test_service"], runner) - error_context.context("Testing service %s" % - params["test_service"], LOG.info) + error_context.context("Testing service %s" % params["test_service"], LOG.info) original_status = service.status() LOG.info("Original status=%s", original_status) @@ -76,5 +75,7 @@ def run(test, params, env): service.start() time.sleep(5) if not service.status() is original_status: - raise exceptions.TestFail("Fail to restore original status of the %s " - "service" % params["test_service"]) + raise exceptions.TestFail( + "Fail to restore original status of the %s " + "service" % params["test_service"] + ) diff --git a/examples/tests/template.py b/examples/tests/template.py index 7d4738938f..be2b0f94bf 100644 --- a/examples/tests/template.py +++ b/examples/tests/template.py @@ -4,7 +4,7 @@ import logging -LOG = logging.getLogger('avocado.test') +LOG = logging.getLogger("avocado.test") def run(test, params, env): diff --git a/requirements-travis.txt b/requirements-travis.txt index 77a9b68fd0..8b8c89c0e9 100644 --- a/requirements-travis.txt +++ b/requirements-travis.txt @@ -5,3 +5,4 @@ inspektor==0.5.3 aexpect>=1.6.4 netifaces==0.11.0 pyenchant==3.2.2 +black==22.3.0 diff --git a/scripts/cd_hash.py b/scripts/cd_hash.py index 77d84522f4..ca9b05aa9f 100755 --- a/scripts/cd_hash.py +++ b/scripts/cd_hash.py @@ -40,7 +40,15 @@ continue logging.info("Hash values for file %s", os.path.basename(filename)) - logging.info(" md5 (1m): %s", crypto.hash_file(filename, 1024 * 1024, algorithm="md5")) - logging.info(" sha1 (1m): %s", crypto.hash_file(filename, 1024 * 1024, algorithm="sha1")) + logging.info( + " md5 (1m): %s", + crypto.hash_file(filename, 1024 * 1024, algorithm="md5"), + ) + logging.info( + " sha1 (1m): %s", + crypto.hash_file(filename, 1024 * 1024, algorithm="sha1"), + ) logging.info(" md5 (full): %s", crypto.hash_file(filename, algorithm="md5")) - logging.info(" sha1 (full): %s", crypto.hash_file(filename, algorithm="sha1")) + logging.info( + " sha1 (full): %s", crypto.hash_file(filename, algorithm="sha1") + ) diff --git a/scripts/download_manager.py b/scripts/download_manager.py index 166cca7822..654afd3eed 100755 --- a/scripts/download_manager.py +++ b/scripts/download_manager.py @@ -21,9 +21,10 @@ import sys import os import logging + # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import asset @@ -42,21 +43,24 @@ def download_assets(): all_assets_sorted = [] if all_assets: logging.info("Available download assets:") - title_list = [a['title'] for a in all_assets] + title_list = [a["title"] for a in all_assets] for index, title in enumerate(sorted(title_list)): - asset_info = [a for a in all_assets if a['title'] == title][0] + asset_info = [a for a in all_assets if a["title"] == title][0] all_assets_sorted.append(asset_info) - asset_present_str = TERM_SUPPORT.partial_str('Missing') - if asset_info['asset_exists']: - asset_present_str = TERM_SUPPORT.healthy_str('Present') - asset_msg = ("%s - [%s] %s (%s)" % - (index + 1, - asset_present_str, - TERM_SUPPORT.header_str(asset_info['title']), - asset_info['destination'])) + asset_present_str = TERM_SUPPORT.partial_str("Missing") + if asset_info["asset_exists"]: + asset_present_str = TERM_SUPPORT.healthy_str("Present") + asset_msg = "%s - [%s] %s (%s)" % ( + index + 1, + asset_present_str, + TERM_SUPPORT.header_str(asset_info["title"]), + asset_info["destination"], + ) logging.info(asset_msg) - indexes = input("Type the index for the assets you want to " - "download (comma separated, leave empty to abort): ") + indexes = input( + "Type the index for the assets you want to " + "download (comma separated, leave empty to abort): " + ) index_list = [] @@ -75,10 +79,8 @@ def download_assets(): try: asset.download_file(asset_info, interactive=True) except urllib.error.HTTPError as http_error: - logging.error("HTTP Error %s: URL %s", - http_error.code, - asset_info['url']) - os.remove(asset_info['destination']) + logging.error("HTTP Error %s: URL %s", http_error.code, asset_info["url"]) + os.remove(asset_info["destination"]) if __name__ == "__main__": diff --git a/scripts/github/cache_populate.py b/scripts/github/cache_populate.py index 3a301a576d..8c78bef8d8 100755 --- a/scripts/github/cache_populate.py +++ b/scripts/github/cache_populate.py @@ -10,21 +10,22 @@ from six.moves import input -gh = Github(login_or_token=input("Enter github username: "), - password=getpass.getpass('Enter github password: '), - user_agent='PyGithub/Python') +gh = Github( + login_or_token=input("Enter github username: "), + password=getpass.getpass("Enter github password: "), + user_agent="PyGithub/Python", +) -print("Enter location (/)", end=' ') -repo_full_name = 'avocado-framework/avocado-vt' -repo_full_name = input("or blank for '%s': " - % repo_full_name).strip() or repo_full_name +print("Enter location (/)", end=" ") +repo_full_name = "avocado-framework/avocado-vt" +repo_full_name = input("or blank for '%s': " % repo_full_name).strip() or repo_full_name print() issues = GithubIssues(gh, repo_full_name) for issue in issues: - sys.stdout.write(str(issue['number']) + '\n') + sys.stdout.write(str(issue["number"]) + "\n") sys.stdout.flush() # make sure cache is cleaned and saved up diff --git a/scripts/github/example.py b/scripts/github/example.py index 830c4db668..e7e4bc3de5 100755 --- a/scripts/github/example.py +++ b/scripts/github/example.py @@ -14,19 +14,18 @@ print("Enter github username:") username = sys.stdin.readline().strip() print() -password = getpass.getpass('Enter github password: ') +password = getpass.getpass("Enter github password: ") -gh = Github(login_or_token=username, - password=password, user_agent='PyGithub/Python') +gh = Github(login_or_token=username, password=password, user_agent="PyGithub/Python") # needed to fetch fresh rate_limiting data -repo = gh.get_repo('avocado-framework/avocado-vt') +repo = gh.get_repo("avocado-framework/avocado-vt") # Requests for logged in users are limited to 5000 per hour # or about 1 request every 0.7 seconds start = gh.rate_limiting # Open up cache and repository -issues = GithubIssues(gh, 'avocado-framework/avocado-vt') -print("Issue #125: ", end=' ') +issues = GithubIssues(gh, "avocado-framework/avocado-vt") +print("Issue #125: ", end=" ") # Any issue can be referenced by number print(issues[125]) end = gh.rate_limiting @@ -34,9 +33,9 @@ print("Cache hits: %s misses: %s" % (issues.cache_hits, issues.cache_misses)) # Pull requests are treated as issues -issues = GithubIssues(gh, 'avocado-framework/avocado-vt') +issues = GithubIssues(gh, "avocado-framework/avocado-vt") start = end -print("Pull #526: ", end=' ') +print("Pull #526: ", end=" ") print(issues[526]) end = gh.rate_limiting print("Requests used: ", start[0] - end[0]) @@ -44,7 +43,7 @@ # Listing issues requires finding the last issue # this takes a while when the cache is empty -issues = GithubIssues(gh, 'avocado-framework/avocado-vt') +issues = GithubIssues(gh, "avocado-framework/avocado-vt") start = end print("Total number of issues (this could take a while):") # This len() is used to force the slower binary-search @@ -54,7 +53,7 @@ print("Cache hits: %s misses: %s" % (issues.cache_hits, issues.cache_misses)) # Searches are supported and return lists of issue-numbers -issues = GithubIssues(gh, 'avocado-framework/avocado-vt') +issues = GithubIssues(gh, "avocado-framework/avocado-vt") start = end print("Open issues last few days without any label (could take 2-10 minutes):") two_days = datetime.timedelta(days=2) @@ -67,20 +66,22 @@ # sort - str - 'created', 'updated', 'comments' # direction - str - 'asc', 'desc' # since - datetime.datetime -criteria = {'state': 'open', 'since': last_week} +criteria = {"state": "open", "since": last_week} # Search results are cached for 10-minutes, otherwise searches can be slow for number in issues.search(criteria): issue = issues[number] # some items must be searched/compared manually - if len(issue['labels']) < 1: - print('https://github.com/avocado-framework/avocado-vt/issues/%s\t"%s"' - % (issue['number'], issue['summary'])) + if len(issue["labels"]) < 1: + print( + 'https://github.com/avocado-framework/avocado-vt/issues/%s\t"%s"' + % (issue["number"], issue["summary"]) + ) print() print("Requests used: ", start[0] - end[0]) print("Cache hits: %s misses: %s" % (issues.cache_hits, issues.cache_misses)) # Now that cache is populated, this will be very fast -issues = GithubIssues(gh, 'avocado-framework/avocado-vt') +issues = GithubIssues(gh, "avocado-framework/avocado-vt") start = end print("Total number of issues (this should be a lot faster):") # This length uses a cached issue count plus a 'since' criteria search diff --git a/scripts/github/github_issues.py b/scripts/github/github_issues.py index 81e1e2b924..d65dcb4f67 100644 --- a/scripts/github/github_issues.py +++ b/scripts/github/github_issues.py @@ -20,7 +20,6 @@ class Nothing(object): # Needed to signal list cache, not github object class SearchResults(object): - def __init__(self, *stuff): self.raw_data = stuff @@ -35,22 +34,29 @@ class GithubCache(object): cache_misses = 0 cache_lifetimes = { - 'default': datetime.timedelta(hours=2), + "default": datetime.timedelta(hours=2), github.GitCommit.GitCommit: datetime.timedelta(days=30), github.NamedUser.NamedUser: datetime.timedelta(days=30), github.Commit.Commit: datetime.timedelta(days=30), github.Issue.Issue: datetime.timedelta(minutes=30), github.PullRequest.PullRequest: datetime.timedelta(hours=1), # Special case for github.Issue.Issue - 'closed': datetime.timedelta(days=30), + "closed": datetime.timedelta(days=30), SearchResults: datetime.timedelta(minutes=10), github.NamedUser.NamedUser: datetime.timedelta(hours=2), github.GitAuthor.GitAuthor: datetime.timedelta(days=9999), - 'total_issues': datetime.timedelta(days=9999) + "total_issues": datetime.timedelta(days=9999), } - def __init__(self, github_obj, cache_get_partial, cache_set_partial, - cache_del_partial, pre_fetch_partial, fetch_partial): + def __init__( + self, + github_obj, + cache_get_partial, + cache_set_partial, + cache_del_partial, + pre_fetch_partial, + fetch_partial, + ): self.github = github_obj self.cache_get = cache_get_partial # Returns native dict self.cache_set = cache_set_partial # called with value=dict @@ -64,12 +70,18 @@ def __call__(self): """ # microseconds aren't useful when fetch takes ~1 second now = datetime.datetime.utcnow() - now = datetime.datetime(year=now.year, month=now.month, - day=now.day, hour=now.hour, - minute=now.minute, second=0, microsecond=0) + now = datetime.datetime( + year=now.year, + month=now.month, + day=now.day, + hour=now.hour, + minute=now.minute, + second=0, + microsecond=0, + ) try: data = self.cached_data() - if data['expires'] < now: + if data["expires"] < now: raise KeyError # refresh cache self.cache_hits += 1 except KeyError: @@ -79,21 +91,18 @@ def __call__(self): # Any exceptions thrown during conversion should purge cache entry try: # Format data for consumption - if data['klass'] == github.PaginatedList.PaginatedList: - inside_klass = data['inside_klass'] + if data["klass"] == github.PaginatedList.PaginatedList: + inside_klass = data["inside_klass"] result = [] - for item in data['raw_data']: - result.append( - self.github.create_from_raw_data(inside_klass, - item)) + for item in data["raw_data"]: + result.append(self.github.create_from_raw_data(inside_klass, item)) return result - elif data['klass'] == Nothing: + elif data["klass"] == Nothing: return None # it's a None object - elif data['klass'] == SearchResults: - return data['raw_data'] # just the contents + elif data["klass"] == SearchResults: + return data["raw_data"] # just the contents else: - return self.github.create_from_raw_data(data['klass'], - data['raw_data']) + return self.github.create_from_raw_data(data["klass"], data["raw_data"]) except Exception: try: self.cache_del() @@ -107,16 +116,20 @@ def format_data(klass, expires, raw_data, inside_klass=None): Enforce uniform data format for fetched data """ if inside_klass is None: - return {'klass': klass, - 'fetched': datetime.datetime.utcnow(), - 'expires': expires, - 'raw_data': raw_data} + return { + "klass": klass, + "fetched": datetime.datetime.utcnow(), + "expires": expires, + "raw_data": raw_data, + } else: - return {'klass': klass, - 'inside_klass': inside_klass, - 'fetched': datetime.datetime.utcnow(), - 'expires': expires, - 'raw_data': raw_data} + return { + "klass": klass, + "inside_klass": inside_klass, + "fetched": datetime.datetime.utcnow(), + "expires": expires, + "raw_data": raw_data, + } def fetched_data(self, now): """ @@ -128,10 +141,11 @@ def fetched_data(self, now): fetched_obj = self.fetch() except github.GithubException as detail: if detail.status == 404: - raise KeyError('Github item not-found error while calling %s ' - 'with args=%s and dargs=%s' % (self.fetch.func, - self.fetch.args, - self.fetch.keywords)) + raise KeyError( + "Github item not-found error while calling %s " + "with args=%s and dargs=%s" + % (self.fetch.func, self.fetch.args, self.fetch.keywords) + ) else: raise if fetched_obj is None: @@ -141,24 +155,29 @@ def fetched_data(self, now): if isinstance(fetched_obj, github.PaginatedList.PaginatedList): raw_data = [item.raw_data for item in fetched_obj] inside_klass = fetched_obj[0].__class__ - expires = now + self.cache_lifetimes.get(inside_klass, - self.cache_lifetimes['default']) - return self.__class__.format_data(klass, - now + self.cache_lifetimes.get( - inside_klass, - self.cache_lifetimes[ - 'default']), - raw_data, inside_klass) + expires = now + self.cache_lifetimes.get( + inside_klass, self.cache_lifetimes["default"] + ) + return self.__class__.format_data( + klass, + now + + self.cache_lifetimes.get( + inside_klass, self.cache_lifetimes["default"] + ), + raw_data, + inside_klass, + ) else: - expires = now + self.cache_lifetimes.get(klass, - # else default - self.cache_lifetimes['default']) + expires = now + self.cache_lifetimes.get( + klass, + # else default + self.cache_lifetimes["default"], + ) # closed issues/pull requests don't change much - if hasattr(fetched_obj, 'closed_at'): + if hasattr(fetched_obj, "closed_at"): if fetched_obj.closed_at is not None: - expires = now + self.cache_lifetimes['closed'] - return self.__class__.format_data(klass, expires, - fetched_obj.raw_data) + expires = now + self.cache_lifetimes["closed"] + return self.__class__.format_data(klass, expires, fetched_obj.raw_data) def cached_data(self): """ @@ -193,9 +212,9 @@ def __init__(self, github_obj, repo_full_name, cache_filename): self.github = github_obj self.repo_full_name = repo_full_name - self.shelf = shelve.open(filename=cache_filename, - protocol=self.protocol, - writeback=True) + self.shelf = shelve.open( + filename=cache_filename, protocol=self.protocol, writeback=True + ) # Avoid exceeding rate-limit per hour requests = self.github.rate_limiting[1] # requests per hour @@ -204,20 +223,21 @@ def __init__(self, github_obj, repo_full_name, cache_filename): self.pre_fetch_partial = Partial(time.sleep, sleeptime) # self.pre_fetch_partial = None # cheat-mode enable (no delays) - repo_cache_key = 'repo_%s' % self.repo_full_name + repo_cache_key = "repo_%s" % self.repo_full_name # get_repo called same way throughout instance life cache_get_partial = Partial(self.shelf.__getitem__, repo_cache_key) cache_set_partial = Partial(self.shelf.__setitem__, repo_cache_key) cache_del_partial = Partial(self.shelf.__delitem__, repo_cache_key) - fetch_partial = Partial(self.github.get_repo, - self.repo_full_name) + fetch_partial = Partial(self.github.get_repo, self.repo_full_name) # Callable instance retrieves cached or fetched value for key - self.get_repo = self.cache_class(self.github, - cache_get_partial, - cache_set_partial, - cache_del_partial, - self.pre_fetch_partial, - fetch_partial) + self.get_repo = self.cache_class( + self.github, + cache_get_partial, + cache_set_partial, + cache_del_partial, + self.pre_fetch_partial, + fetch_partial, + ) super(GithubIssuesBase, self).__init__() def __del__(self): @@ -259,12 +279,12 @@ def __iter__(self): yield self[key] def __setitem__(self, key, value): - raise KeyError("Read only mapping while trying to set %s to %s" - % (str(key), str(value))) + raise KeyError( + "Read only mapping while trying to set %s to %s" % (str(key), str(value)) + ) def __delitem__(self, key): - raise KeyError( - "Read only mapping while trying to delete %s" % str(key)) + raise KeyError("Read only mapping while trying to delete %s" % str(key)) def __getitem__(self, key): """ @@ -280,7 +300,7 @@ def __getitem__(self, key): return item def get_issue_cache_key(self, number): - return 'repo_%s_issue_%s' % (self.repo_full_name, str(int(number))) + return "repo_%s_issue_%s" % (self.repo_full_name, str(int(number))) def has_key(self, key): return self.__contains__(key) @@ -306,39 +326,37 @@ class GithubIssues(GithubIssuesBase, object): # Marshal callables for key to github.Issue.Issue value marshal_map = { - 'number': lambda gh_obj: getattr(gh_obj, 'number'), - 'summary': lambda gh_obj: getattr(gh_obj, 'title'), - 'description': lambda gh_obj: getattr(gh_obj, 'body'), - 'modified': lambda gh_obj: getattr(gh_obj, 'updated_at'), - 'commits': NotImplementedError, # setup in __init__ - 'opened': lambda gh_obj: getattr(gh_obj, 'created_at'), - 'closed': lambda gh_obj: getattr(gh_obj, 'closed_at'), - 'assigned': lambda gh_obj: getattr(gh_obj, 'assignee'), - 'author': lambda gh_obj: getattr(gh_obj, 'user').login, - 'commit_authors': NotImplementedError, # setup in __init__ - 'comments': lambda gh_obj: getattr(gh_obj, 'comments'), - 'comment_authors': NotImplementedError, # setup in __init__ - 'labels': lambda gh_obj: [label.name for label in gh_obj.labels], - 'url': lambda gh_obj: getattr(gh_obj, 'html_url'), - 'github_issue': lambda gh_obj: gh_obj + "number": lambda gh_obj: getattr(gh_obj, "number"), + "summary": lambda gh_obj: getattr(gh_obj, "title"), + "description": lambda gh_obj: getattr(gh_obj, "body"), + "modified": lambda gh_obj: getattr(gh_obj, "updated_at"), + "commits": NotImplementedError, # setup in __init__ + "opened": lambda gh_obj: getattr(gh_obj, "created_at"), + "closed": lambda gh_obj: getattr(gh_obj, "closed_at"), + "assigned": lambda gh_obj: getattr(gh_obj, "assignee"), + "author": lambda gh_obj: getattr(gh_obj, "user").login, + "commit_authors": NotImplementedError, # setup in __init__ + "comments": lambda gh_obj: getattr(gh_obj, "comments"), + "comment_authors": NotImplementedError, # setup in __init__ + "labels": lambda gh_obj: [label.name for label in gh_obj.labels], + "url": lambda gh_obj: getattr(gh_obj, "html_url"), + "github_issue": lambda gh_obj: gh_obj, } # Storage for property values - _cache_hits = 0 # Tracks temporary cache instances + _cache_hits = 0 # Tracks temporary cache instances _cache_misses = 0 # Tracks temporary cache instances def __init__(self, github_obj, repo_full_name): """ Initialize cache and reference github repository issues """ - cache_filename = self.__class__.__name__ + '.cache' - super(GithubIssues, self).__init__(github_obj, - repo_full_name, - cache_filename) + cache_filename = self.__class__.__name__ + ".cache" + super(GithubIssues, self).__init__(github_obj, repo_full_name, cache_filename) # These marshal functions require state - self.marshal_map['commits'] = self.gh_pr_commits - self.marshal_map['commit_authors'] = self.gh_pr_commit_authors - self.marshal_map['comment_authors'] = self.gh_issue_comment_authors + self.marshal_map["commits"] = self.gh_pr_commits + self.marshal_map["commit_authors"] = self.gh_pr_commit_authors + self.marshal_map["comment_authors"] = self.gh_issue_comment_authors def __del__(self): self.vacuum() @@ -351,7 +369,7 @@ def vacuum(self): now = datetime.datetime.utcnow() for key, value in list(self.shelf.items()): # no need to be precise - if value['expires'] <= now: + if value["expires"] <= now: keys_to_del.append(key) for key in keys_to_del: del self.shelf[key] @@ -376,52 +394,55 @@ def __len__(self): """ Return cached number of issues """ - cache_key = 'repo_%s_total_issues' % self.repo_full_name + cache_key = "repo_%s_total_issues" % self.repo_full_name # seconds aren't useful when fetch takes > 1 minute now = datetime.datetime.utcnow() - now = datetime.datetime(year=now.year, month=now.month, - day=now.day, hour=now.hour, - minute=now.minute, second=0, microsecond=0) + now = datetime.datetime( + year=now.year, + month=now.month, + day=now.day, + hour=now.hour, + minute=now.minute, + second=0, + microsecond=0, + ) # Easier to do custom caching behavior here than fuss with GithubCache try: cache_data = self.shelf.__getitem__(cache_key) - if cache_data['expires'] < now: + if cache_data["expires"] < now: raise KeyError # Bypass search_result caching used in self.search() - searchresult = self.make_search_results( - {'since': cache_data['since']}) + searchresult = self.make_search_results({"since": cache_data["since"]}) # about to change the number - cache_data['since'] = now + cache_data["since"] = now # total equal to old count plus new count since then - cache_data['raw_data'] += len(searchresult.raw_data) + cache_data["raw_data"] += len(searchresult.raw_data) except KeyError: cache_data = {} # doesn't expire ever - cache_data['expires'] = now + GithubCache.cache_lifetimes[ - 'total_issues'] - cache_data['since'] = now + cache_data["expires"] = now + GithubCache.cache_lifetimes["total_issues"] + cache_data["since"] = now # This will take a while if issue cache is stale - cache_data['raw_data'] = super(GithubIssues, self).__len__() + cache_data["raw_data"] = super(GithubIssues, self).__len__() self.shelf.__setitem__(cache_key, cache_data) - return cache_data['raw_data'] + return cache_data["raw_data"] def get_gh_obj(self, cache_key, fetch_partial): """ Helper to get object possibly from cache and update counters """ - cache_get_partial = Partial(self.shelf.__getitem__, - cache_key) - cache_set_partial = Partial(self.shelf.__setitem__, - cache_key) - cache_del_partial = Partial(self.shelf.__delitem__, - cache_key) + cache_get_partial = Partial(self.shelf.__getitem__, cache_key) + cache_set_partial = Partial(self.shelf.__setitem__, cache_key) + cache_del_partial = Partial(self.shelf.__delitem__, cache_key) # Callable instance could change every time - get_obj = GithubCache(self.github, - cache_get_partial, - cache_set_partial, - cache_del_partial, - self.pre_fetch_partial, - fetch_partial) + get_obj = GithubCache( + self.github, + cache_get_partial, + cache_set_partial, + cache_del_partial, + self.pre_fetch_partial, + fetch_partial, + ) result = get_obj() self._cache_hits += get_obj.cache_hits self._cache_misses += get_obj.cache_misses @@ -442,72 +463,74 @@ def search(self, criteria): """ valid_criteria = {} # use search dictionary to form hash for cached results - search_cache_key = 'issue_search' + search_cache_key = "issue_search" # Validate & transform criteria - if 'state' in criteria: - state = str(criteria['state']) - if state not in ('open', 'closed'): + if "state" in criteria: + state = str(criteria["state"]) + if state not in ("open", "closed"): raise ValueError("'state' criteria must be 'open' or 'closed'") - valid_criteria['state'] = state - search_cache_key = '%s_%s' % (search_cache_key, state) - - if 'assignee' in criteria: - assignee = str(criteria['assignee']) - search_cache_key = '%s_%s' % (search_cache_key, assignee) - if assignee in ('none', '*'): - valid_criteria['assignee'] = assignee + valid_criteria["state"] = state + search_cache_key = "%s_%s" % (search_cache_key, state) + + if "assignee" in criteria: + assignee = str(criteria["assignee"]) + search_cache_key = "%s_%s" % (search_cache_key, assignee) + if assignee in ("none", "*"): + valid_criteria["assignee"] = assignee else: # returns github.NamedUser.NamedUser - valid_criteria['assignee'] = self.get_gh_user(assignee) + valid_criteria["assignee"] = self.get_gh_user(assignee) - if 'mentioned' in criteria: - mentioned = str(criteria['assignee']) - search_cache_key = '%s_%s' % (search_cache_key, mentioned) - if mentioned in ('none', '*'): - valid_criteria['mentioned'] = mentioned + if "mentioned" in criteria: + mentioned = str(criteria["assignee"]) + search_cache_key = "%s_%s" % (search_cache_key, mentioned) + if mentioned in ("none", "*"): + valid_criteria["mentioned"] = mentioned else: # returns github.NamedUser.NamedUser - valid_criteria['mentioned'] = self.get_gh_user(mentioned) + valid_criteria["mentioned"] = self.get_gh_user(mentioned) - if 'labels' in criteria: - labels = criteria['labels'] + if "labels" in criteria: + labels = criteria["labels"] if not isinstance(labels, list): raise ValueError("'lables' criteria must be a list") - valid_criteria['labels'] = [] + valid_criteria["labels"] = [] for name in labels: - search_cache_key = '%s_%s' % (search_cache_key, labels) - valid_criteria['labels'].append(self.get_gh_label(str(name))) - - if 'sort' in criteria: - sort = str(criteria['sort']) - if sort not in ('created', 'updated', 'comments'): - raise ValueError("'sort' criteria must be 'created', 'updated'" - ", 'comments'") - valid_criteria['sort'] = sort - search_cache_key = '%s_%s' % (search_cache_key, sort) - - if 'direction' in criteria: - direction = str(criteria['direction']) - if direction not in ('asc', 'desc'): + search_cache_key = "%s_%s" % (search_cache_key, labels) + valid_criteria["labels"].append(self.get_gh_label(str(name))) + + if "sort" in criteria: + sort = str(criteria["sort"]) + if sort not in ("created", "updated", "comments"): + raise ValueError( + "'sort' criteria must be 'created', 'updated'" ", 'comments'" + ) + valid_criteria["sort"] = sort + search_cache_key = "%s_%s" % (search_cache_key, sort) + + if "direction" in criteria: + direction = str(criteria["direction"]) + if direction not in ("asc", "desc"): raise ValueError("'direction' criteria must be 'asc', 'desc'") - valid_criteria['direction'] = direction - search_cache_key = '%s_%s' % (search_cache_key, direction) + valid_criteria["direction"] = direction + search_cache_key = "%s_%s" % (search_cache_key, direction) - if 'since' in criteria: - since = criteria['since'] + if "since" in criteria: + since = criteria["since"] if not isinstance(since, datetime.datetime): - raise ValueError("'since' criteria must be a " - "datetime.datetime") + raise ValueError("'since' criteria must be a " "datetime.datetime") # second and milisecond not useful to search or cache - since = datetime.datetime(year=since.year, - month=since.month, - day=since.day, - hour=since.hour, - minute=since.minute, - second=0, - microsecond=0) - search_cache_key = '%s_%s' % (search_cache_key, since.isoformat()) - valid_criteria['since'] = since + since = datetime.datetime( + year=since.year, + month=since.month, + day=since.day, + hour=since.hour, + minute=since.minute, + second=0, + microsecond=0, + ) + search_cache_key = "%s_%s" % (search_cache_key, since.isoformat()) + valid_criteria["since"] = since # Do not perform search operation unless no cached results # or cached results have expired @@ -533,22 +556,23 @@ def clean_cache_entry(self, key): pass def get_gh_user(self, login): - cache_key = 'github_user_%s' % login + cache_key = "github_user_%s" % login fetch_partial = Partial(self.github.get_user, login) try: return self.get_gh_obj(cache_key, fetch_partial) except KeyError: - raise ValueError('login %s is not a valid github user' % login) + raise ValueError("login %s is not a valid github user" % login) def get_gh_label(self, name): repo = self.get_repo() - cache_key = str('repo_%s_label_%s' % (self.repo_full_name, name)) + cache_key = str("repo_%s_label_%s" % (self.repo_full_name, name)) fetch_partial = Partial(repo.get_label, name) try: return self.get_gh_obj(cache_key, fetch_partial) except KeyError: - raise ValueError('label %s is not valid for repo %s' % (name, - self.repo_full_name)) + raise ValueError( + "label %s is not valid for repo %s" % (name, self.repo_full_name) + ) def marshal_gh_obj(self, gh_issue): """ @@ -564,9 +588,11 @@ def gh_issue_is_pull(gh_issue): """ pullreq = gh_issue.pull_request if pullreq is not None: - if (pullreq.diff_url is None and - pullreq.html_url is None and - pullreq.patch_url is None): + if ( + pullreq.diff_url is None + and pullreq.html_url is None + and pullreq.patch_url is None + ): return False else: return False @@ -580,14 +606,13 @@ def gh_issue_comment_authors(self, gh_issue): """ if gh_issue.comments > 0: num = gh_issue.number - cache_key = ('repo_%s_issue_%s_comments' - % (self.repo_full_name, num)) + cache_key = "repo_%s_issue_%s_comments" % (self.repo_full_name, num) fetch_partial = Partial(gh_issue.get_comments) authors = set() for comment in self.get_gh_obj(cache_key, fetch_partial): # Referencing user attribute requires a request, so cache it - user_cache_key = cache_key + '_%s_user' % comment.id - user_fetch_partial = Partial(getattr, comment, 'user') + user_cache_key = cache_key + "_%s_user" % comment.id + user_fetch_partial = Partial(getattr, comment, "user") try: user = self.get_gh_obj(user_cache_key, user_fetch_partial) except Exception: @@ -608,23 +633,21 @@ def gh_pr_commit_authors(self, gh_issue): if GithubIssues.gh_issue_is_pull(gh_issue): num = gh_issue.number repo = self.get_repo() - cache_key = 'repo_%s_pull_%s' % (self.repo_full_name, str(num)) + cache_key = "repo_%s_pull_%s" % (self.repo_full_name, str(num)) fetch_partial = Partial(repo.get_pull, num) pull = self.get_gh_obj(cache_key, fetch_partial) if pull.commits is None or pull.commits < 1: return None # No commits == no commit authors - cache_key = 'repo_%s_pull_%s_commits' % (self.repo_full_name, - str(num)) + cache_key = "repo_%s_pull_%s_commits" % (self.repo_full_name, str(num)) fetch_partial = Partial(pull.get_commits) authors = set() for commit in self.get_gh_obj(cache_key, fetch_partial): # Referencing commit author requires a request, cache it. - author_cache_key = cache_key + '_%s_author' % str(commit.sha) - author_fetch_partial = Partial(getattr, commit, 'author') + author_cache_key = cache_key + "_%s_author" % str(commit.sha) + author_fetch_partial = Partial(getattr, commit, "author") try: - author_obj = self.get_gh_obj(author_cache_key, - author_fetch_partial) + author_obj = self.get_gh_obj(author_cache_key, author_fetch_partial) except Exception: # clean up commit list cache entry also self.clean_cache_entry(cache_key) @@ -632,13 +655,14 @@ def gh_pr_commit_authors(self, gh_issue): # Retrieve e-mail from git commit object if author_obj is None: # Referencing git commit requires a request, cache it - gitcommit_cache_key = (cache_key + '_%s_gitcommit' - % str(commit.sha)) - gitcommit_fetch_partial = Partial(getattr, commit, - 'commit') # git commit + gitcommit_cache_key = cache_key + "_%s_gitcommit" % str(commit.sha) + gitcommit_fetch_partial = Partial( + getattr, commit, "commit" + ) # git commit try: - gitcommit = self.get_gh_obj(gitcommit_cache_key, - gitcommit_fetch_partial) + gitcommit = self.get_gh_obj( + gitcommit_cache_key, gitcommit_fetch_partial + ) except Exception: # Need to clean commit and gitcommit entries self.clean_cache_entry(cache_key) @@ -658,7 +682,7 @@ def gh_pr_commits(self, gh_issue): if GithubIssues.gh_issue_is_pull(gh_issue): num = gh_issue.number repo = self.get_repo() - cache_key = 'repo_%s_pull_%s' % (self.repo_full_name, str(num)) + cache_key = "repo_%s_pull_%s" % (self.repo_full_name, str(num)) fetch_partial = Partial(repo.get_pull, num) pull = self.get_gh_obj(cache_key, fetch_partial) return pull.commits @@ -666,10 +690,10 @@ def gh_pr_commits(self, gh_issue): class MutateError(KeyError): - def __init__(self, key, number): - super(MutateError, self).__init__("Unable to modify %s on issue %d" - % (str(key), number)) + super(MutateError, self).__init__( + "Unable to modify %s on issue %d" % (str(key), number) + ) class MutableIssue(dict): @@ -678,8 +702,10 @@ class MutableIssue(dict): def __init__(self, github_issues, issue_number): if not isinstance(github_issues, GithubIssues): - raise ValueError("github_issues %s is not a GithubIssues, it's a %s" - % (str(github_issues), str(type(github_issues)))) + raise ValueError( + "github_issues %s is not a GithubIssues, it's a %s" + % (str(github_issues), str(type(github_issues))) + ) # make sure issue_number is valid and cached junk = github_issues[issue_number] del junk @@ -699,10 +725,10 @@ def _issue_cache_key(self): def _setdelitem(self, opr, key, value): if key not in list(self._github_issues.marshal_map.keys()): raise MutateError(key, self._issue_number) - methodname = '%s_%s' % (opr, key) + methodname = "%s_%s" % (opr, key) if callable(getattr(self, methodname)): method = getattr(self, methodname) - if opr == 'set': + if opr == "set": method(value) else: method() @@ -714,23 +740,23 @@ def __getitem__(self, key): return self._github_issue[key] def __setitem__(self, key, value): - self._setdelitem('set', key, value) + self._setdelitem("set", key, value) def __delitem__(self, key): - self._setdelitem('del', key, None) + self._setdelitem("del", key, None) def set_labels(self, value): """ Merge list of new lables into existing label set """ new_labels = set(value) - old_labels = set(self._github_issue['labels']) + old_labels = set(self._github_issue["labels"]) change_list = list(new_labels | old_labels) get_gh_label = self._github_issues.get_gh_label # save typing # Raise exception if any label name is bad gh_labels = [get_gh_label(label) for label in change_list] # Access PyGithub object to change labels - self._github_issue['github_issue'].set_labels(*gh_labels) + self._github_issue["github_issue"].set_labels(*gh_labels) # Force retrieval of changed item self._github_issues.clean_cache_entry(self._issue_cache_key) @@ -738,7 +764,7 @@ def del_labels(self): """ Remove all labels from an issue """ - self._github_issue['github_issue'].delete_labels() + self._github_issue["github_issue"].delete_labels() # Force retrieval of changed item self._github_issues.clean_cache_entry(self._issue_cache_key) diff --git a/scripts/github/label_issues.py b/scripts/github/label_issues.py index 195555d091..4681b7adda 100755 --- a/scripts/github/label_issues.py +++ b/scripts/github/label_issues.py @@ -27,26 +27,30 @@ def set_labels(mutable_issue): break print() if len(labels) > 0: - mutable_issue['labels'] = labels + mutable_issue["labels"] = labels -gh = Github(login_or_token=input("Enter github username: "), - password=getpass.getpass('Enter github password: '), - user_agent='PyGithub/Python') +gh = Github( + login_or_token=input("Enter github username: "), + password=getpass.getpass("Enter github password: "), + user_agent="PyGithub/Python", +) -print("Enter location (/)", end=' ') -repo_full_name = 'avocado-framework/avocado-vt' -repo_full_name = input("or blank for '%s': " - % repo_full_name).strip() or repo_full_name +print("Enter location (/)", end=" ") +repo_full_name = "avocado-framework/avocado-vt" +repo_full_name = input("or blank for '%s': " % repo_full_name).strip() or repo_full_name print() issues = GithubIssues(gh, repo_full_name) # Can't directly search for no labels -criteria = {'state': 'open', - 'sort': 'updated', 'direction': 'asc'} # updated-asc == oldest first +criteria = { + "state": "open", + "sort": "updated", + "direction": "asc", +} # updated-asc == oldest first -heading = ("Open, unlabeled issues from %s, oldest-first" % repo_full_name) +heading = "Open, unlabeled issues from %s, oldest-first" % repo_full_name print(heading) print("-" * len(heading)) print() @@ -55,11 +59,11 @@ def set_labels(mutable_issue): labels = ", ".join([label.name for label in repo.get_labels()]) for number in issues.search(criteria): - if len(issues[number]['labels']) > 0: + if len(issues[number]["labels"]) > 0: continue - print('#%d:' % number, end=' ') - print(issues[number]['summary'] + ':') - print(issues[number]['description']) + print("#%d:" % number, end=" ") + print(issues[number]["summary"] + ":") + print(issues[number]["description"]) print("Available Labels:", labels) set_labels(MutableIssue(issues, number)) diff --git a/scripts/github/pulls_applied.py b/scripts/github/pulls_applied.py index b491834912..820acefaa9 100755 --- a/scripts/github/pulls_applied.py +++ b/scripts/github/pulls_applied.py @@ -10,14 +10,15 @@ from six.moves import input -gh = Github(login_or_token=input("Enter github username: "), - password=getpass.getpass('Enter github password: '), - user_agent='PyGithub/Python') +gh = Github( + login_or_token=input("Enter github username: "), + password=getpass.getpass("Enter github password: "), + user_agent="PyGithub/Python", +) -print("Enter location (/)", end=' ') -repo_full_name = 'avocado-framework/avocado-vt' -repo_full_name = input("or blank for '%s': " - % repo_full_name).strip() or repo_full_name +print("Enter location (/)", end=" ") +repo_full_name = "avocado-framework/avocado-vt" +repo_full_name = input("or blank for '%s': " % repo_full_name).strip() or repo_full_name print() @@ -29,7 +30,7 @@ while True: date_string = "20" + input("Enter date (YY-MM-DD): ") + " 00:00:00.0" date_string = date_string.strip() - fmt = '%Y-%m-%d %H:%M:%S.%f' + fmt = "%Y-%m-%d %H:%M:%S.%f" try: since = datetime.datetime.strptime(date_string, fmt) break @@ -61,11 +62,13 @@ # sort - str - 'created', 'updated', 'comments' # direction - str - 'asc', 'desc' # since - datetime.datetime -criteria = {'state': 'closed', 'labels': labels, - 'sort': 'updated', 'since': since} +criteria = {"state": "closed", "labels": labels, "sort": "updated", "since": since} -heading = ("Applied %s pull-requests from %s since %s by author" - % (",".join(labels), repo_full_name, since.isoformat())) +heading = "Applied %s pull-requests from %s since %s by author" % ( + ",".join(labels), + repo_full_name, + since.isoformat(), +) print(heading) print("-" * len(heading)) print() @@ -74,16 +77,17 @@ for number in issues.search(criteria): issue = issues[number] # Issues don't have commits - if issue['commits'] is not None: - author_issues[issue['author']] = issue + if issue["commits"] is not None: + author_issues[issue["author"]] = issue authors = list(author_issues.keys()) authors.sort() for author in authors: issue = author_issues[author] - print("Pull #%d: '%s'" % (issue['number'], issue['summary'])) - print(" %d commit(s) by %s" % (issue['commits'], - ",".join(issue['commit_authors']))) + print("Pull #%d: '%s'" % (issue["number"], issue["summary"])) + print( + " %d commit(s) by %s" % (issue["commits"], ",".join(issue["commit_authors"])) + ) print() # make sure cache is cleaned and saved up diff --git a/scripts/github/stale_pulls.py b/scripts/github/stale_pulls.py index 734f23b77d..2d8f830f2e 100755 --- a/scripts/github/stale_pulls.py +++ b/scripts/github/stale_pulls.py @@ -10,14 +10,15 @@ from six.moves import input -gh = Github(login_or_token=input("Enter github username: "), - password=getpass.getpass('Enter github password: '), - user_agent='PyGithub/Python') +gh = Github( + login_or_token=input("Enter github username: "), + password=getpass.getpass("Enter github password: "), + user_agent="PyGithub/Python", +) -print("Enter location (/)", end=' ') -repo_full_name = 'avocado-framework/avocado-vt' -repo_full_name = input("or blank for '%s': " - % repo_full_name).strip() or repo_full_name +print("Enter location (/)", end=" ") +repo_full_name = "avocado-framework/avocado-vt" +repo_full_name = input("or blank for '%s': " % repo_full_name).strip() or repo_full_name print() @@ -39,25 +40,25 @@ break print() -criteria = {'state': 'open', 'labels': labels, - 'sort': 'updated', 'direction': 'asc'} +criteria = {"state": "open", "labels": labels, "sort": "updated", "direction": "asc"} -heading = ("Oldest updates for Open %s pull requests from %s, past 1 day old:" - % (",".join(labels), repo_full_name)) +heading = "Oldest updates for Open %s pull requests from %s, past 1 day old:" % ( + ",".join(labels), + repo_full_name, +) print(heading) print("-" * len(heading)) print() for number in issues.search(criteria): - if issues[number]['commits'] and issues[number]['commits'] > 0: - age = datetime.datetime.now() - issues[number]['modified'] + if issues[number]["commits"] and issues[number]["commits"] > 0: + age = datetime.datetime.now() - issues[number]["modified"] hours = age.seconds // (60 * 60) days = age.days - url = issues[number]['url'] - summary = issues[number]['summary'] + url = issues[number]["url"] + summary = issues[number]["summary"] if days > 0: - print("%s - %d days %d hours old - %s" % ( - url, days, hours, summary[0:30])) + print("%s - %d days %d hours old - %s" % (url, days, hours, summary[0:30])) else: # Results sorted by decreasing update age # don't care about issues updated today diff --git a/scripts/github/unassigned_issues.py b/scripts/github/unassigned_issues.py index ca814a2f22..6245a65b9f 100755 --- a/scripts/github/unassigned_issues.py +++ b/scripts/github/unassigned_issues.py @@ -9,14 +9,15 @@ from six.moves import input -gh = Github(login_or_token=input("Enter github username: "), - password=getpass.getpass('Enter github password: '), - user_agent='PyGithub/Python') +gh = Github( + login_or_token=input("Enter github username: "), + password=getpass.getpass("Enter github password: "), + user_agent="PyGithub/Python", +) -print("Enter location (/)", end=' ') -repo_full_name = 'avocado-framework/avocado-vt' -repo_full_name = input("or blank for '%s': " - % repo_full_name).strip() or repo_full_name +print("Enter location (/)", end=" ") +repo_full_name = "avocado-framework/avocado-vt" +repo_full_name = input("or blank for '%s': " % repo_full_name).strip() or repo_full_name print() @@ -38,17 +39,24 @@ break print() -criteria = {'state': 'open', 'assignee': 'none', 'labels': labels, - 'sort': 'updated', 'direction': 'asc'} # asc-updated == oldest first +criteria = { + "state": "open", + "assignee": "none", + "labels": labels, + "sort": "updated", + "direction": "asc", +} # asc-updated == oldest first -heading = ("Unassigned %s issues from %s, oldest-first" - % (",".join(labels), repo_full_name)) +heading = "Unassigned %s issues from %s, oldest-first" % ( + ",".join(labels), + repo_full_name, +) print(heading) print("-" * len(heading)) print() for number in issues.search(criteria): - print(issues[number]['url'], issues[number]['summary'][:30]) + print(issues[number]["url"], issues[number]["summary"][:30]) # make sure cache is cleaned and saved up del issues diff --git a/scripts/koji_pkgspec.py b/scripts/koji_pkgspec.py index c5cf39bae3..626d77b350 100755 --- a/scripts/koji_pkgspec.py +++ b/scripts/koji_pkgspec.py @@ -14,7 +14,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import cartesian_config @@ -28,21 +28,31 @@ class OptionParser(optparse.OptionParser): """ def __init__(self): - optparse.OptionParser.__init__(self, - usage=('Usage: %prog [options] ' - '[koji-pkg-spec]')) - - general = optparse.OptionGroup(self, 'GENERAL OPTIONS') - general.add_option('-a', '--arch', dest='arch', default='x86_64', - help=('architecture of packages to list, together ' - 'with "noarch". defaults to "x86_64"')) - general.add_option('-t', '--tag', dest='tag', help='default koji tag') + optparse.OptionParser.__init__( + self, usage=("Usage: %prog [options] " "[koji-pkg-spec]") + ) + + general = optparse.OptionGroup(self, "GENERAL OPTIONS") + general.add_option( + "-a", + "--arch", + dest="arch", + default="x86_64", + help=( + "architecture of packages to list, together " + 'with "noarch". defaults to "x86_64"' + ), + ) + general.add_option("-t", "--tag", dest="tag", help="default koji tag") self.add_option_group(general) - cartesian_config = optparse.OptionGroup(self, 'CARTESIAN CONFIG') - cartesian_config.add_option('-c', '--config', dest='config', - help=('use a cartesian configuration file ' - 'for fetching package values')) + cartesian_config = optparse.OptionGroup(self, "CARTESIAN CONFIG") + cartesian_config.add_option( + "-c", + "--config", + dest="config", + help=("use a cartesian configuration file " "for fetching package values"), + ) self.add_option_group(cartesian_config) @@ -76,17 +86,17 @@ def get_koji_qemu_kvm_tag_pkgs(self, config_file): if tag is not None and pkgs is not None: break - if 'koji_qemu_kvm_tag' in d: + if "koji_qemu_kvm_tag" in d: if tag is None: - tag = d.get('koji_qemu_kvm_tag') - if 'koji_qemu_kvm_pkgs' in d: + tag = d.get("koji_qemu_kvm_tag") + if "koji_qemu_kvm_pkgs" in d: if pkgs is None: - pkgs = d.get('koji_qemu_kvm_pkgs') + pkgs = d.get("koji_qemu_kvm_pkgs") return (tag, pkgs) def check_koji_pkg_spec(self, koji_pkg_spec): if not koji_pkg_spec.is_valid(): - print('ERROR:', koji_pkg_spec.describe_invalid()) + print("ERROR:", koji_pkg_spec.describe_invalid()) sys.exit(-1) def print_koji_pkg_spec_info(self, koji_pkg_spec): @@ -95,13 +105,14 @@ def print_koji_pkg_spec_info(self, koji_pkg_spec): print('ERROR: could not find info about "%s"' % koji_pkg_spec.to_text()) return - name = info.get('name', 'unknown') - pkgs = self.koji_client.get_pkg_rpm_file_names(koji_pkg_spec, - arch=self.options.arch) - print('Package name: %s' % name) - print('Package files:') + name = info.get("name", "unknown") + pkgs = self.koji_client.get_pkg_rpm_file_names( + koji_pkg_spec, arch=self.options.arch + ) + print("Package name: %s" % name) + print("Package files:") for p in pkgs: - print('\t* %s' % p) + print("\t* %s" % p) print() def main(self): @@ -128,6 +139,6 @@ def main(self): self.print_koji_pkg_spec_info(koji_pkg_spec) -if __name__ == '__main__': +if __name__ == "__main__": app = App() app.main() diff --git a/scripts/logging_config.py b/scripts/logging_config.py index be1f9927d1..b05f77e7cb 100644 --- a/scripts/logging_config.py +++ b/scripts/logging_config.py @@ -23,13 +23,14 @@ class LoggingConfig(object): stderr_level = logging.ERROR file_formatter = logging.Formatter( - fmt='%(asctime)s %(levelname)-5.5s|%(module)10.10s:%(lineno)4.4d| ' - '%(message)s', - datefmt='%m/%d %H:%M:%S') + fmt="%(asctime)s %(levelname)-5.5s|%(module)10.10s:%(lineno)4.4d| " + "%(message)s", + datefmt="%m/%d %H:%M:%S", + ) console_formatter = logging.Formatter( - fmt='%(asctime)s %(levelname)-5.5s| %(message)s', - datefmt='%H:%M:%S') + fmt="%(asctime)s %(levelname)-5.5s| %(message)s", datefmt="%H:%M:%S" + ) def __init__(self, use_console=True, set_fmt=True): self.logger = logging.getLogger() @@ -46,8 +47,7 @@ def add_stream_handler(self, stream, level=logging.DEBUG): return handler def add_console_handlers(self): - stdout_handler = self.add_stream_handler(sys.stdout, - level=self.stdout_level) + stdout_handler = self.add_stream_handler(sys.stdout, level=self.stdout_level) # only pass records *below* STDERR_LEVEL to stdout, to avoid # duplication stdout_handler.addFilter(AllowBelowSeverity(self.stderr_level)) @@ -65,9 +65,8 @@ def add_file_handler(self, file_path, level=logging.DEBUG, log_dir=None): return handler def _add_file_handlers_for_all_levels(self, log_dir, log_name): - for level in (logging.DEBUG, logging.INFO, logging.WARNING, - logging.ERROR): - file_name = '%s.%s' % (log_name, logging.getLevelName(level)) + for level in (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR): + file_name = "%s.%s" % (log_name, logging.getLevelName(level)) self.add_file_handler(file_name, level=level, log_dir=log_dir) def add_debug_file_handlers(self, log_dir, log_name=None): diff --git a/scripts/package_jeos.py b/scripts/package_jeos.py index d2d6cb1d93..476e054555 100755 --- a/scripts/package_jeos.py +++ b/scripts/package_jeos.py @@ -10,7 +10,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import utils_misc @@ -28,24 +28,24 @@ def package_jeos(img): :param img: Path to a qcow2 image """ basedir = os.path.dirname(img) - backup = img + '.backup' - qemu_img = utils_misc.find_command('qemu-img') + backup = img + ".backup" + qemu_img = utils_misc.find_command("qemu-img") shutil.move(img, backup) logging.info("Backup %s saved", backup) - process.system("%s convert -f qcow2 -O qcow2 -o compat=0.10 %s %s" % (qemu_img, backup, img)) + process.system( + "%s convert -f qcow2 -O qcow2 -o compat=0.10 %s %s" % (qemu_img, backup, img) + ) logging.info("Sparse file %s created successfully", img) compressed_img = img + ".xz" - archiver = utils_misc.find_command('xz') + archiver = utils_misc.find_command("xz") process.system("%s -9 -e %s" % (archiver, img)) - logging.info("JeOS compressed file %s created successfuly", - compressed_img) + logging.info("JeOS compressed file %s created successfuly", compressed_img) if __name__ == "__main__": if len(sys.argv) <= 1: - print("Usage: %s [path to freshly installed JeOS qcow2 image]" % - sys.argv[0]) + print("Usage: %s [path to freshly installed JeOS qcow2 image]" % sys.argv[0]) sys.exit(1) path = sys.argv[1] diff --git a/scripts/parallel.py b/scripts/parallel.py index 228ef61173..febe5cffe2 100644 --- a/scripts/parallel.py +++ b/scripts/parallel.py @@ -6,7 +6,6 @@ class ParallelError(Exception): - def __init__(self, out, errors): self.out = out self.errors = errors @@ -14,7 +13,6 @@ def __init__(self, out, errors): class ParallelExecute(object): - def __init__(self, functions, max_simultaneous_procs=20): """ This takes in a dictionary of functions which map to a set of @@ -87,13 +85,13 @@ def run_until_completion(self): if len(errors) > 0: msg = "Errors occurred during execution:" - msg = '\n'.join([msg] + errors) + msg = "\n".join([msg] + errors) raise ParallelError(msg, errors) -def redirect_io(log_file='/dev/null'): +def redirect_io(log_file="/dev/null"): # Always redirect stdin. - in_fd = os.open('/dev/null', os.O_RDONLY) + in_fd = os.open("/dev/null", os.O_RDONLY) try: os.dup2(in_fd, 0) finally: @@ -106,6 +104,6 @@ def redirect_io(log_file='/dev/null'): finally: os.close(out_fd) - sys.stdin = os.fdopen(0, 'r') - sys.stdout = os.fdopen(1, 'w') - sys.stderr = os.fdopen(2, 'w') + sys.stdin = os.fdopen(0, "r") + sys.stdout = os.fdopen(1, "w") + sys.stderr = os.fdopen(2, "w") diff --git a/scripts/regression.py b/scripts/regression.py index 446995963e..fd64154c30 100755 --- a/scripts/regression.py +++ b/scripts/regression.py @@ -11,6 +11,7 @@ import sys import re import warnings + try: import configparser as ConfigParser except ImportError: @@ -21,10 +22,10 @@ def getoutput(cmd): """Return output of executing cmd in a shell.""" - pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r') + pipe = os.popen("{ " + cmd + "; } 2>&1", "r") text = pipe.read() pipe.close() - if text[-1:] == '\n': + if text[-1:] == "\n": text = text[:-1] return text @@ -36,12 +37,11 @@ def exec_sql(cmd, conf="../../global_config.ini"): passwd = config.get("AUTOTEST_WEB", "password") db = config.get("AUTOTEST_WEB", "database") db_type = config.get("AUTOTEST_WEB", "db_type") - if db_type != 'mysql': + if db_type != "mysql": print("regression.py: only support mysql database!") sys.exit(1) - conn = MySQLdb.connect(host="localhost", user=user, - passwd=passwd, db=db) + conn = MySQLdb.connect(host="localhost", user=user, passwd=passwd, db=db) cursor = conn.cursor() cursor.execute(cmd) rows = cursor.fetchall() @@ -57,22 +57,21 @@ def exec_sql(cmd, conf="../../global_config.ini"): return lines -def get_test_keyval(jobid, keyname, default=''): - idx = exec_sql("select job_idx from tko_jobs where afe_job_id=%s" - % jobid)[-1] - test_idx = exec_sql('select test_idx from tko_tests where job_idx=%s' - % idx)[3] +def get_test_keyval(jobid, keyname, default=""): + idx = exec_sql("select job_idx from tko_jobs where afe_job_id=%s" % jobid)[-1] + test_idx = exec_sql("select test_idx from tko_tests where job_idx=%s" % idx)[3] try: - return exec_sql('select value from tko_test_attributes' - ' where test_idx=%s and attribute="%s"' - % (test_idx, keyname))[-1] + return exec_sql( + "select value from tko_test_attributes" + ' where test_idx=%s and attribute="%s"' % (test_idx, keyname) + )[-1] except Exception: return default class Sample(object): - """ Collect test results in same environment to a sample """ + """Collect test results in same environment to a sample""" def __init__(self, sample_type, arg): def generate_raw_table(test_dict): @@ -80,30 +79,30 @@ def generate_raw_table(test_dict): tmp = [] sample_type = category = None for i in test_dict: - line = i.split('|')[1:] + line = i.split("|")[1:] if not sample_type: sample_type = line[0:2] if sample_type != line[0:2]: - ret_dict.append('|'.join(sample_type + tmp)) + ret_dict.append("|".join(sample_type + tmp)) sample_type = line[0:2] tmp = [] if "e+" in line[-1]: tmp.append("%f" % float(line[-1])) - elif 'e-' in line[-1]: + elif "e-" in line[-1]: tmp.append("%f" % float(line[-1])) elif not (re.findall("[a-zA-Z]", line[-1]) or is_int(line[-1])): tmp.append("%f" % float(line[-1])) else: tmp.append(line[-1]) - if category != i.split('|')[0]: - category = i.split('|')[0] + if category != i.split("|")[0]: + category = i.split("|")[0] ret_dict.append("Category:" + category.strip()) ret_dict.append(self.categories) - ret_dict.append('|'.join(sample_type + tmp)) + ret_dict.append("|".join(sample_type + tmp)) return ret_dict - if sample_type == 'filepath': + if sample_type == "filepath": files = arg.split() self.files_dict = [] for i in range(len(files)): @@ -113,13 +112,13 @@ def generate_raw_table(test_dict): line = line.strip() if re.findall("^### ", line): if "kvm-userspace-ver" in line: - self.kvmver = line.split(':')[-1] + self.kvmver = line.split(":")[-1] elif "kvm_version" in line: - self.hostkernel = line.split(':')[-1] + self.hostkernel = line.split(":")[-1] elif "guest-kernel-ver" in line: - self.guestkernel = line.split(':')[-1] + self.guestkernel = line.split(":")[-1] elif "session-length" in line: - self.len = line.split(':')[-1] + self.len = line.split(":")[-1] else: f.append(line.strip()) self.files_dict.append(f) @@ -139,14 +138,18 @@ def generate_raw_table(test_dict): cpunum = len(re.findall("processor\s+: \d", cpuinfo)) cpumodel = re.findall("Model name:\s+(.*)", lscpu) socketnum = int(re.findall("Socket\(s\):\s+(\d+)", lscpu)[0]) - corenum = int(re.findall("Core\(s\) per socket:\s+(\d+)", lscpu)[0]) * socketnum - threadnum = int(re.findall("Thread\(s\) per core:\s+(\d+)", lscpu)[0]) * corenum + corenum = ( + int(re.findall("Core\(s\) per socket:\s+(\d+)", lscpu)[0]) * socketnum + ) + threadnum = ( + int(re.findall("Thread\(s\) per core:\s+(\d+)", lscpu)[0]) * corenum + ) numanodenum = int(re.findall("NUMA node\(s\):\s+(\d+)", lscpu)[0]) memnum = float(re.findall("MemTotal:\s+(\d+)", meminfo)[0]) / 1024 / 1024 nicnum = len(re.findall("\d+:\d+\.0 Ethernet", lspci)) disknum = re.findall("sd\w+\S", partitions) fdiskinfo = re.findall("Disk\s+(/dev/sd.*\s+GiB),", fdisk) - elif sample_type == 'database': + elif sample_type == "database": jobid = arg self.kvmver = get_test_keyval(jobid, "kvm-userspace-ver") self.hostkernel = get_test_keyval(jobid, "kvm_version") @@ -154,10 +157,13 @@ def generate_raw_table(test_dict): self.len = get_test_keyval(jobid, "session-length") self.categories = get_test_keyval(jobid, "category") - idx = exec_sql("select job_idx from tko_jobs where afe_job_id=%s" - % jobid)[-1] - data = exec_sql("select test_idx,iteration_key,iteration_value" - " from tko_perf_view where job_idx=%s" % idx) + idx = exec_sql("select job_idx from tko_jobs where afe_job_id=%s" % jobid)[ + -1 + ] + data = exec_sql( + "select test_idx,iteration_key,iteration_value" + " from tko_perf_view where job_idx=%s" % idx + ) testidx = None job_dict = [] test_dict = [] @@ -169,13 +175,16 @@ def generate_raw_table(test_dict): job_dict.append(generate_raw_table(test_dict)) test_dict = [] testidx = s[0] - test_dict.append(' | '.join(s[1].split('--')[0:] + s[-1:])) + test_dict.append(" | ".join(s[1].split("--")[0:] + s[-1:])) job_dict.append(generate_raw_table(test_dict)) self.files_dict = job_dict self.version = " userspace: %s\n host kernel: %s\n guest kernel: %s" % ( - self.kvmver, self.hostkernel, self.guestkernel) + self.kvmver, + self.hostkernel, + self.guestkernel, + ) nrepeat = len(self.files_dict) if nrepeat < 2: print("`nrepeat' should be larger than 1!") @@ -188,7 +197,18 @@ def generate_raw_table(test_dict): Please check sysinfo directory in autotest result to get more details. (eg: http://autotest-server.com/results/5057-autotest/host1/sysinfo/) -
""" % (cpunum, cpumodel, corenum, threadnum, socketnum, numanodenum, memnum, nicnum, fdiskinfo, disknum) +
""" % ( + cpunum, + cpumodel, + corenum, + threadnum, + socketnum, + numanodenum, + memnum, + nicnum, + fdiskinfo, + disknum, + ) self.desc += """ - Every Avg line represents the average value based on *%d* repetitions of the same test, and the following SD line represents the Standard Deviation between the *%d* repetitions. @@ -197,11 +217,15 @@ def generate_raw_table(test_dict): takes into account the SD of the averages. - The paired t-test is computed for the averages of same category. -""" % (nrepeat, nrepeat) +""" % ( + nrepeat, + nrepeat, + ) def getAvg(self, avg_update=None): - return self._process_files(self.files_dict, self._get_list_avg, - avg_update=avg_update) + return self._process_files( + self.files_dict, self._get_list_avg, avg_update=avg_update + ) def getAvgPercent(self, avgs_dict): return self._process_files(avgs_dict, self._get_augment_rate) @@ -243,8 +267,8 @@ def getTtestPvalue(self, fs_dict1, fs_dict2, paired=None, ratio=None): sample1 = np.array(s1[line][col]) sample2 = np.array(s2[line][col]) warnings.simplefilter("ignore", RuntimeWarning) - if (paired): - if (ratio): + if paired: + if ratio: (_, p) = stats.ttest_rel(np.log(sample1), np.log(sample2)) else: (_, p) = stats.ttest_rel(sample1, sample2) @@ -259,7 +283,7 @@ def getTtestPvalue(self, fs_dict1, fs_dict2, paired=None, ratio=None): return ret def _get_rate(self, data): - """ num2 / num1 * 100 """ + """num2 / num1 * 100""" result = "0.0" if len(data) == 2 and float(data[0]) != 0: result = float(data[1]) / float(data[0]) * 100 @@ -270,7 +294,7 @@ def _get_rate(self, data): return result def _get_augment_rate(self, data): - """ (num2 - num1) / num1 * 100 """ + """(num2 - num1) / num1 * 100""" result = "+0.0" if len(data) == 2 and float(data[0]) != 0: result = (float(data[1]) - float(data[0])) / float(data[0]) * 100 @@ -293,23 +317,23 @@ def _get_list_sd(self, data): o_sum += float(i) sqsum += float(i) ** 2 avg = o_sum / n - if avg == 0 or n == 1 or sqsum - (n * avg ** 2) <= 0: + if avg == 0 or n == 1 or sqsum - (n * avg**2) <= 0: return "0.0" - return "%f" % (((sqsum - (n * avg ** 2)) / (n - 1)) ** 0.5) + return "%f" % (((sqsum - (n * avg**2)) / (n - 1)) ** 0.5) def _get_list_avg(self, data): - """ Compute the average of list entries """ + """Compute the average of list entries""" o_sum = 0.0 for i in data: o_sum += float(i) return "%f" % (o_sum / len(data)) def _get_list_self(self, data): - """ Use this to convert sample dicts """ + """Use this to convert sample dicts""" return data def _process_lines(self, files_dict, row, func, avg_update, merge): - """ Use unified function to process same lines of different samples """ + """Use unified function to process same lines of different samples""" lines = [] ret = [] @@ -327,10 +351,11 @@ def _process_lines(self, files_dict, row, func, avg_update, merge): ret.append(func(data_list)) if avg_update: - for row in avg_update.split('|'): - items = row.split(',') - ret[int(items[0])] = "%f" % (float(ret[int(items[1])]) / - float(ret[int(items[2])])) + for row in avg_update.split("|"): + items = row.split(",") + ret[int(items[0])] = "%f" % ( + float(ret[int(items[1])]) / float(ret[int(items[2])]) + ) if merge: return "|".join(ret) return ret @@ -345,14 +370,23 @@ def _process_files(self, files_dict, func, avg_update=None, merge=True): if re.findall("[a-zA-Z]", files_dict[0][i]): ret_lines.append(files_dict[0][i].strip()) else: - line = self._process_lines(files_dict, i, func, avg_update, - merge) + line = self._process_lines(files_dict, i, func, avg_update, merge) ret_lines.append(line) return ret_lines -def display(lists, rates, allpvalues, f, ignore_col, o_sum="Augment Rate", - prefix0=None, prefix1=None, prefix2=None, prefix3=None): +def display( + lists, + rates, + allpvalues, + f, + ignore_col, + o_sum="Augment Rate", + prefix0=None, + prefix1=None, + prefix2=None, + prefix3=None, +): """ Display lists data to standard format @@ -366,6 +400,7 @@ def display(lists, rates, allpvalues, f, ignore_col, o_sum="Augment Rate", param prefix2: output prefix in Diff Avg/P-value lines param prefix3: output prefix in total Sign line """ + def str_ignore(out, split=False): out = out.split("|") for i in range(ignore_col): @@ -402,8 +437,7 @@ def tee_line(content, filepath, n=None): for l in range(len(lists[0])): if not re.findall("[a-zA-Z]", lists[0][l]): break - tee("", - f) + tee("
", f) tee("

== %s " % o_sum + "==

", f) category = 0 for i in range(len(lists[0])): @@ -424,24 +458,29 @@ def tee_line(content, filepath, n=None): else: tee_line(pfix + str_ignore(lists[n][i], True), f) if not is_diff and n == 0: - if '|' in lists[n][i]: + if "|" in lists[n][i]: tee_line(prefix0 + lists[n][i], f) elif "Category:" in lists[n][i]: if category != 0 and prefix3: if len(allpvalues[category - 1]) > 0: - tee_line(prefix3 + str_ignore( - allpvalues[category - 1][0]), f) + tee_line( + prefix3 + str_ignore(allpvalues[category - 1][0]), f + ) tee("
", f) tee("
", f) - tee("", f) + tee( + "
", + f, + ) category += 1 tee("" % lists[n][i], f) else: tee("" % lists[n][i], f) for n in range(len(rates)): - if lists[0][i] != rates[n][i] and (not re.findall("[a-zA-Z]", - rates[n][i]) or "nan" in rates[n][i]): + if lists[0][i] != rates[n][i] and ( + not re.findall("[a-zA-Z]", rates[n][i]) or "nan" in rates[n][i] + ): tee_line(prefix2[n] + str_ignore(rates[n][i], True), f) if prefix3 and len(allpvalues[-1]) > 0: tee_line(prefix3 + str_ignore(allpvalues[category - 1][0]), f) @@ -449,7 +488,7 @@ def tee_line(content, filepath, n=None): def analyze(test, sample_type, arg1, arg2, configfile): - """ Compute averages/p-vales of two samples, print results nicely """ + """Compute averages/p-vales of two samples, print results nicely""" config = ConfigParser.ConfigParser() config.read(configfile) ignore_col = int(config.get(test, "ignore_col")) @@ -462,7 +501,7 @@ def get_list(directory): print(cmd) return getoutput(cmd) - if sample_type == 'filepath': + if sample_type == "filepath": arg1 = get_list(arg1) arg2 = get_list(arg2) @@ -488,7 +527,7 @@ def get_list(directory): if not re.findall("[a-zA-Z]", avg1[i]): tmp1.append([avg1[i]]) tmp2.append([avg2[i]]) - elif 'Category' in avg1[i] and i != 0: + elif "Category" in avg1[i] and i != 0: navg1.append(tmp1) navg2.append(tmp2) tmp1 = [] @@ -507,28 +546,57 @@ def get_list(directory): desc = desc % s1.len - tee("
####1. Description of setup#1\n%s\n test data:  %s
" - % (s1.version, s1.testdata), "%s.html" % test) - tee("
####2. Description of setup#2\n%s\n test data:  %s
" - % (s2.version, s2.testdata), "%s.html" % test) - tee("
" + '\n'.join(desc.split('\\n')) + "
", test + ".html") + tee( + "
####1. Description of setup#1\n%s\n test data:  %s
" + % (s1.version, s1.testdata), + "%s.html" % test, + ) + tee( + "
####2. Description of setup#2\n%s\n test data:  %s
" + % (s2.version, s2.testdata), + "%s.html" % test, + ) + tee("
" + "\n".join(desc.split("\\n")) + "
", test + ".html") tee("
" + s1.desc + "
", test + ".html") - display([avg1, sd1, avg2, sd2], rlist, allpvalues, test + ".html", - ignore_col, o_sum="Regression Testing: %s" % test, prefix0="#|Tile|", - prefix1=["1|Avg|", " |%SD|", "2|Avg|", " |%SD|"], - prefix2=["-|%Diff between Avg|", "-|Significance|"], - prefix3="-|Total Significance|") - - display(s1.files_dict, [avg1], [], test + ".avg.html", ignore_col, - o_sum="Raw data of sample 1", prefix0="#|Tile|", - prefix1=[" | |"], - prefix2=["-|Avg |"], prefix3="") - - display(s2.files_dict, [avg2], [], test + ".avg.html", ignore_col, - o_sum="Raw data of sample 2", prefix0="#|Tile|", - prefix1=[" | |"], - prefix2=["-|Avg |"], prefix3="") + display( + [avg1, sd1, avg2, sd2], + rlist, + allpvalues, + test + ".html", + ignore_col, + o_sum="Regression Testing: %s" % test, + prefix0="#|Tile|", + prefix1=["1|Avg|", " |%SD|", "2|Avg|", " |%SD|"], + prefix2=["-|%Diff between Avg|", "-|Significance|"], + prefix3="-|Total Significance|", + ) + + display( + s1.files_dict, + [avg1], + [], + test + ".avg.html", + ignore_col, + o_sum="Raw data of sample 1", + prefix0="#|Tile|", + prefix1=[" | |"], + prefix2=["-|Avg |"], + prefix3="", + ) + + display( + s2.files_dict, + [avg2], + [], + test + ".avg.html", + ignore_col, + o_sum="Raw data of sample 2", + prefix0="#|Tile|", + prefix1=[" | |"], + prefix2=["-|Avg |"], + prefix3="", + ) def is_int(n): @@ -548,7 +616,7 @@ def is_float(n): def tee(content, filepath): - """ Write content to standard output and filepath """ + """Write content to standard output and filepath""" fd = open(filepath, "a") fd.write(content + "\n") fd.close() @@ -558,7 +626,7 @@ def tee(content, filepath): if __name__ == "__main__": if len(sys.argv) != 5: this = os.path.basename(sys.argv[0]) - print('Usage: %s filepath ' % this) - print(' or %s db ' % this) + print("Usage: %s filepath " % this) + print(" or %s db " % this) sys.exit(1) - analyze(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], 'perf.conf') + analyze(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], "perf.conf") diff --git a/scripts/scan_results.py b/scripts/scan_results.py index 025109ac48..e8f08b5859 100755 --- a/scripts/scan_results.py +++ b/scripts/scan_results.py @@ -19,9 +19,11 @@ def colour_result(result): """Colour result in the test status info""" - colours_map = {"PASS": "\033[92mPASS\033[00m", - "ERROR": "\033[93mERROR\033[00m", - "FAIL": "\033[91mFAIL\033[00m"} + colours_map = { + "PASS": "\033[92mPASS\033[00m", + "ERROR": "\033[93mERROR\033[00m", + "FAIL": "\033[91mFAIL\033[00m", + } return colours_map.get(result) or result @@ -33,20 +35,21 @@ def summarise_records(records): for rows in records: records_summary[rows[1]] = records_summary.get(rows[1], 0) + 1 records_summary[rows[4]] = records_summary.get(rows[4], 0) + 1 - res = ", ".join("%s=%r" % ( - key, val) for (key, val) in six.iteritems(records_summary)) + res = ", ".join( + "%s=%r" % (key, val) for (key, val) in six.iteritems(records_summary) + ) print("\033[96mSummary: \n" + res + "\033[00m") def get_total_seconds(td): - """ Alias for get total_seconds in python2.6 """ - if hasattr(td, 'total_seconds'): + """Alias for get total_seconds in python2.6""" + if hasattr(td, "total_seconds"): return td.total_seconds() return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 1e6) // 1e6 def fetch_data(db_file=".journal.sqlite"): - """ Fetch tests status info from journal database""" + """Fetch tests status info from journal database""" records = [] con = sqlite3.connect(db_file) try: @@ -81,49 +84,49 @@ def fetch_data(db_file=".journal.sqlite"): def print_data(records, skip_timestamp=False): - """ Print formatted tests status info""" + """Print formatted tests status info""" if not records: return if not skip_timestamp: - print("%-40s %-15s %-15s %-15s %-10s %-10s" % ( - "CaseName", "Status", "StartTime", - "EndTime", "Result", "TimeElapsed")) + print( + "%-40s %-15s %-15s %-15s %-10s %-10s" + % ("CaseName", "Status", "StartTime", "EndTime", "Result", "TimeElapsed") + ) else: print("%-40s %-15s %-10s" % ("CaseName", "Status", "Result")) for row in records: if not skip_timestamp: - print("%s %s %s %s %s %s" % ( - row[0], row[1], row[2], row[3], colour_result(row[4]), row[5])) + print( + "%s %s %s %s %s %s" + % (row[0], row[1], row[2], row[3], colour_result(row[4]), row[5]) + ) else: print("%s %s %s" % (row[0], row[1], colour_result(row[4]))) summarise_records(records) if __name__ == "__main__": - default_results_dir = os.path.join(data_dir.get_logs_dir(), 'latest') + default_results_dir = os.path.join(data_dir.get_logs_dir(), "latest") parser = argparse.ArgumentParser(description="Avocado journal dump tool") parser.add_argument( - '-d', - '--test-results-dir', - action='store', + "-d", + "--test-results-dir", + action="store", default=default_results_dir, - dest='results_dir', - help="avocado test results dir, Default: %s" % - default_results_dir) + dest="results_dir", + help="avocado test results dir, Default: %s" % default_results_dir, + ) parser.add_argument( - '-s', - '--skip-timestamp', - action='store_true', + "-s", + "--skip-timestamp", + action="store_true", default=False, - dest='skip_timestamp', - help="skip timestamp output (leaving status and result enabled)") - parser.add_argument( - '-v', - '--version', - action='version', - version='%(prog)s 1.0') + dest="skip_timestamp", + help="skip timestamp output (leaving status and result enabled)", + ) + parser.add_argument("-v", "--version", action="version", version="%(prog)s 1.0") arguments = parser.parse_args() - db_file = os.path.join(arguments.results_dir, '.journal.sqlite') + db_file = os.path.join(arguments.results_dir, ".journal.sqlite") if not os.path.isfile(db_file): print("`.journal.sqlite` DB not found in results directory, ") print("Please start avocado with option '--journal'.") diff --git a/scripts/tapfd_helper.py b/scripts/tapfd_helper.py index aa684c2376..b640b087bc 100755 --- a/scripts/tapfd_helper.py +++ b/scripts/tapfd_helper.py @@ -6,7 +6,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import utils_net @@ -27,7 +27,7 @@ def destroy_tap(tapfd_list): sys.exit(255) brname = sys.argv[1] - cmd_line = ' '.join(sys.argv[2:]) + cmd_line = " ".join(sys.argv[2:]) if re.findall("-netdev\s", cmd_line): # so we get the new qemu cli with netdev parameter. diff --git a/scripts/virt_disk.py b/scripts/virt_disk.py index 886f220f36..a5fdcd5f3a 100755 --- a/scripts/virt_disk.py +++ b/scripts/virt_disk.py @@ -14,7 +14,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import utils_disk @@ -27,32 +27,57 @@ class OptionParser(optparse.OptionParser): """ def __init__(self): - optparse.OptionParser.__init__(self, - usage=('Usage: %prog [options] ' - ' ' - '[file 1][file 2]..[file N]')) - - media = optparse.OptionGroup(self, 'MEDIA SELECTION') - media.set_description('Choose only one of the media formats supported') - media.add_option('-c', '--cdrom', dest='cdrom', default=False, - action='store_true', - help=('create a basic cdrom image')) - media.add_option('-f', '--floppy', dest='floppy', default=False, - action='store_true', - help=('create a basic floppy image')) - media.add_option('--floppy-size', dest='vfd_size', - action='store_true', default="1440k", - help=('Floppy size (1440k or 2880k). ' - 'defaults to %default')) + optparse.OptionParser.__init__( + self, + usage=( + "Usage: %prog [options] " + " " + "[file 1][file 2]..[file N]" + ), + ) + + media = optparse.OptionGroup(self, "MEDIA SELECTION") + media.set_description("Choose only one of the media formats supported") + media.add_option( + "-c", + "--cdrom", + dest="cdrom", + default=False, + action="store_true", + help=("create a basic cdrom image"), + ) + media.add_option( + "-f", + "--floppy", + dest="floppy", + default=False, + action="store_true", + help=("create a basic floppy image"), + ) + media.add_option( + "--floppy-size", + dest="vfd_size", + action="store_true", + default="1440k", + help=("Floppy size (1440k or 2880k). " "defaults to %default"), + ) self.add_option_group(media) - path = optparse.OptionGroup(self, 'PATH SELECTION') - path.add_option('-q', '--qemu-img', dest='qemu_img', - default='/usr/bin/qemu-img', - help=('qemu-img binary path. defaults to ' - '%default')) - path.add_option('-t', '--temp', dest='temp', default='/tmp', - help='Path to hold temporary files. defaults to %default') + path = optparse.OptionGroup(self, "PATH SELECTION") + path.add_option( + "-q", + "--qemu-img", + dest="qemu_img", + default="/usr/bin/qemu-img", + help=("qemu-img binary path. defaults to " "%default"), + ) + path.add_option( + "-t", + "--temp", + dest="temp", + default="/tmp", + help="Path to hold temporary files. defaults to %default", + ) self.add_option_group(path) @@ -73,7 +98,7 @@ def parse_cmdline(self): self.options, self.args = self.opt_parser.parse_args() if not (self.options.cdrom or self.options.floppy): self.usage() - if (self.options.cdrom and self.options.floppy): + if self.options.cdrom and self.options.floppy: self.usage() if not len(self.args) >= 1: @@ -85,19 +110,20 @@ def parse_cmdline(self): def main(self): self.parse_cmdline() if self.options.floppy: - self.disk = utils_disk.FloppyDisk(self.image, - self.options.qemu_img, - self.options.temp, - self.options.vfd_size) + self.disk = utils_disk.FloppyDisk( + self.image, + self.options.qemu_img, + self.options.temp, + self.options.vfd_size, + ) elif self.options.cdrom: - self.disk = utils_disk.CdromDisk(self.image, - self.options.temp) + self.disk = utils_disk.CdromDisk(self.image, self.options.temp) for f in self.files: self.disk.copy_to(f) self.disk.close() -if __name__ == '__main__': +if __name__ == "__main__": app = App() app.main() diff --git a/selftests/doc/test_doc_build.py b/selftests/doc/test_doc_build.py index 2dc2af78ac..d390d4e16e 100644 --- a/selftests/doc/test_doc_build.py +++ b/selftests/doc/test_doc_build.py @@ -9,7 +9,7 @@ from avocado.utils import process -basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..') +basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..") basedir = os.path.abspath(basedir) @@ -18,20 +18,21 @@ class DocBuildError(Exception): class DocBuildTest(unittest.TestCase): - def test_build_docs(self): """ Build avocado VT HTML docs, reporting failures """ - ignore_list = ['No python imaging library installed', - 'ovirtsdk module not present', - 'Virsh executable not set or found on path', - 'virt-admin executable not set or found on path', - "failed to import module u'virttest.step_editor'"] + ignore_list = [ + "No python imaging library installed", + "ovirtsdk module not present", + "Virsh executable not set or found on path", + "virt-admin executable not set or found on path", + "failed to import module u'virttest.step_editor'", + ] failure_lines = [] - doc_dir = os.path.join(basedir, 'docs') - process.run('make -C %s clean' % doc_dir) - result = process.run('make -C %s html' % doc_dir) + doc_dir = os.path.join(basedir, "docs") + process.run("make -C %s clean" % doc_dir) + result = process.run("make -C %s html" % doc_dir) stdout = result.stdout_text.splitlines() stderr = result.stderr_text.splitlines() output_lines = stdout + stderr @@ -39,23 +40,25 @@ def test_build_docs(self): ignore_msg = False for ignore in ignore_list: if ignore in line: - print('Expected warning ignored: %s' % line) + print("Expected warning ignored: %s" % line) ignore_msg = True if ignore_msg: continue - if 'ERROR' in line: + if "ERROR" in line: failure_lines.append(line) - if 'WARNING' in line: + if "WARNING" in line: failure_lines.append(line) if failure_lines: - e_msg = ('%s ERRORS and/or WARNINGS detected while building the html docs:\n' % - len(failure_lines)) + e_msg = ( + "%s ERRORS and/or WARNINGS detected while building the html docs:\n" + % len(failure_lines) + ) for (index, failure_line) in enumerate(failure_lines): e_msg += "%s) %s\n" % (index + 1, failure_line) - e_msg += ('Full output: %s\n' % '\n'.join(output_lines)) - e_msg += 'Please check the output and fix your docstrings/.rst docs' + e_msg += "Full output: %s\n" % "\n".join(output_lines) + e_msg += "Please check the output and fix your docstrings/.rst docs" raise DocBuildError(e_msg) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/functional/test_basic.py b/selftests/functional/test_basic.py index 5caeedf173..499222eb49 100644 --- a/selftests/functional/test_basic.py +++ b/selftests/functional/test_basic.py @@ -9,7 +9,7 @@ from virttest import data_dir -BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..') +BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..") BASE_DIR = os.path.abspath(BASE_DIR) TEST_STATUSES_PY = """from avocado.core import exceptions @@ -74,42 +74,66 @@ def run(test, params, env): class BasicTests(unittest.TestCase): - def setUp(self): - self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__) + self.tmpdir = tempfile.mkdtemp(prefix="avocado_" + __name__) self.rm_files = [] def test_statuses(self): os.chdir(BASE_DIR) - test_path = os.path.join(data_dir.get_test_providers_dir(), - "downloads", "io-github-autotest-qemu", - "generic", "tests", "test_statuses.py") - self.assertTrue(os.path.exists(os.path.dirname(test_path)), - "The qemu providers dir does not exists, Avocado-vt " - "is probably not configured properly.") + test_path = os.path.join( + data_dir.get_test_providers_dir(), + "downloads", + "io-github-autotest-qemu", + "generic", + "tests", + "test_statuses.py", + ) + self.assertTrue( + os.path.exists(os.path.dirname(test_path)), + "The qemu providers dir does not exists, Avocado-vt " + "is probably not configured properly.", + ) self.rm_files.append(test_path) script.make_script(test_path, TEST_STATUSES_PY) - cfg = script.make_script(os.path.join(self.tmpdir, - "test_statuses.cfg"), - TEST_STATUSES_CFG) - result = process.run("avocado --show all run --vt-config %s " - "--job-results-dir %s" - % (cfg, self.tmpdir), ignore_status=True) - self.assertEqual(result.exit_status, 1, "Exit status is not 1:\n%s" - % result) - status = json.load(open(os.path.join(self.tmpdir, "latest", - "results.json"))) + cfg = script.make_script( + os.path.join(self.tmpdir, "test_statuses.cfg"), TEST_STATUSES_CFG + ) + result = process.run( + "avocado --show all run --vt-config %s " + "--job-results-dir %s" % (cfg, self.tmpdir), + ignore_status=True, + ) + self.assertEqual(result.exit_status, 1, "Exit status is not 1:\n%s" % result) + status = json.load(open(os.path.join(self.tmpdir, "latest", "results.json"))) act_statuses = [_["status"] for _ in status["tests"]] - statuses_master = ["SKIP", "PASS", "FAIL", "ERROR", "CANCEL", "PASS", - "FAIL", "ERROR", "ERROR"] - statuses_36lts = ["SKIP", "PASS", "FAIL", "ERROR", "SKIP", "PASS", - "FAIL", "ERROR", "ERROR"] - if not (act_statuses == statuses_master or - act_statuses == statuses_36lts): - self.fail("Test statuses does not match any of expected results:" - "\nmaster: %s\n36lts: %s\nactual: %s\n\noutput:\n%s" - % (statuses_master, statuses_36lts, act_statuses, - result)) + statuses_master = [ + "SKIP", + "PASS", + "FAIL", + "ERROR", + "CANCEL", + "PASS", + "FAIL", + "ERROR", + "ERROR", + ] + statuses_36lts = [ + "SKIP", + "PASS", + "FAIL", + "ERROR", + "SKIP", + "PASS", + "FAIL", + "ERROR", + "ERROR", + ] + if not (act_statuses == statuses_master or act_statuses == statuses_36lts): + self.fail( + "Test statuses does not match any of expected results:" + "\nmaster: %s\n36lts: %s\nactual: %s\n\noutput:\n%s" + % (statuses_master, statuses_36lts, act_statuses, result) + ) def tearDown(self): for path in self.rm_files: @@ -120,5 +144,5 @@ def tearDown(self): shutil.rmtree(self.tmpdir) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/style.sh b/selftests/style.sh new file mode 100755 index 0000000000..18ad91b79f --- /dev/null +++ b/selftests/style.sh @@ -0,0 +1,4 @@ +#!/bin/sh -e +echo "** Running black..." + +black --check --diff --color . diff --git a/selftests/unit/test_cartesian_config.py b/selftests/unit/test_cartesian_config.py index 574e4e2502..c962dcd978 100644 --- a/selftests/unit/test_cartesian_config.py +++ b/selftests/unit/test_cartesian_config.py @@ -7,25 +7,24 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import cartesian_config mydir = os.path.dirname(__file__) -testdatadir = os.path.join(mydir, 'unittest_data') +testdatadir = os.path.join(mydir, "unittest_data") class CartesianConfigTest(unittest.TestCase): - def _checkDictionaries(self, parser, reference): result = list(parser.get_dicts()) # as the dictionary list is very large, test each item individually: self.assertEquals(len(result), len(reference)) for resdict, refdict in list(zip(result, reference)): # checking the dict name first should make some errors more visible - self.assertEquals(resdict.get('name'), refdict.get('name')) + self.assertEquals(resdict.get("name"), refdict.get("name")) self.assertEquals(resdict, refdict) def _checkConfigDump(self, config, dump): @@ -33,10 +32,10 @@ def _checkConfigDump(self, config, dump): configpath = os.path.join(testdatadir, config) dumppath = os.path.join(testdatadir, dump) - if dumppath.endswith('.gz'): - df = gzip.GzipFile(dumppath, 'r') + if dumppath.endswith(".gz"): + df = gzip.GzipFile(dumppath, "r") else: - df = open(dumppath, 'r') + df = open(dumppath, "r") # we could have used pickle, but repr()-based dumps are easier to # generate, debug, and edit dumpdata = eval(df.read()) @@ -56,7 +55,8 @@ def _checkStringDump(self, string, dump, defaults=False): self._checkDictionaries(p, dump) def testSimpleVariant(self): - self._checkStringConfig(""" + self._checkStringConfig( + """ c = abc variants: - a: @@ -64,25 +64,31 @@ def testSimpleVariant(self): - b: x = vb """, - [ - {'_name_map_file': {'': 'a'}, - '_short_name_map_file': {'': 'a'}, - 'c': 'abc', - 'dep': [], - 'name': 'a', - 'shortname': 'a', - 'x': 'va'}, - {'_name_map_file': {'': 'b'}, - '_short_name_map_file': {'': 'b'}, - 'c': 'abc', - 'dep': [], - 'name': 'b', - 'shortname': 'b', - 'x': 'vb'}, - ]) + [ + { + "_name_map_file": {"": "a"}, + "_short_name_map_file": {"": "a"}, + "c": "abc", + "dep": [], + "name": "a", + "shortname": "a", + "x": "va", + }, + { + "_name_map_file": {"": "b"}, + "_short_name_map_file": {"": "b"}, + "c": "abc", + "dep": [], + "name": "b", + "shortname": "b", + "x": "vb", + }, + ], + ) def testFilterMixing(self): - self._checkStringDump(""" + self._checkStringDump( + """ variants: - unknown_qemu: - rhel64: @@ -96,26 +102,34 @@ def testFilterMixing(self): no unknown_qemu - testB: """, - [ - {'_name_map_file': {'': 'testA.kvm.unknown_qemu'}, - '_short_name_map_file': {'': 'testA.kvm.unknown_qemu'}, - 'dep': [], - 'name': 'testA.kvm.unknown_qemu', - 'shortname': 'testA.kvm.unknown_qemu'}, - {'_name_map_file': {'': 'testB.kvm.unknown_qemu'}, - '_short_name_map_file': {'': 'testB.kvm.unknown_qemu'}, - 'dep': [], - 'name': 'testB.kvm.unknown_qemu', - 'shortname': 'testB.kvm.unknown_qemu'}, - {'_name_map_file': {'': 'testB.nokvm.unknown_qemu'}, - '_short_name_map_file': {'': 'testB.nokvm.unknown_qemu'}, - 'dep': [], - 'name': 'testB.nokvm.unknown_qemu', - 'shortname': 'testB.nokvm.unknown_qemu'}, - ]) + [ + { + "_name_map_file": {"": "testA.kvm.unknown_qemu"}, + "_short_name_map_file": {"": "testA.kvm.unknown_qemu"}, + "dep": [], + "name": "testA.kvm.unknown_qemu", + "shortname": "testA.kvm.unknown_qemu", + }, + { + "_name_map_file": {"": "testB.kvm.unknown_qemu"}, + "_short_name_map_file": {"": "testB.kvm.unknown_qemu"}, + "dep": [], + "name": "testB.kvm.unknown_qemu", + "shortname": "testB.kvm.unknown_qemu", + }, + { + "_name_map_file": {"": "testB.nokvm.unknown_qemu"}, + "_short_name_map_file": {"": "testB.nokvm.unknown_qemu"}, + "dep": [], + "name": "testB.nokvm.unknown_qemu", + "shortname": "testB.nokvm.unknown_qemu", + }, + ], + ) def testNameVariant(self): - self._checkStringDump(""" + self._checkStringDump( + """ variants tests: # All tests in configuration - wait: run = "wait" @@ -139,76 +153,101 @@ def testNameVariant(self): only (host_os=linux) """, - [ - {'_name_map_file': {'': '(host_os=linux).(virt_system=linux).(tests=wait).long'}, - '_short_name_map_file': {'': 'linux.linux.wait.long'}, - 'dep': [], - 'host_os': 'linux', - 'image': 'linux', - 'name': '(host_os=linux).(virt_system=linux).(tests=wait).long', - 'run': 'wait', - 'shortname': 'linux.wait.long', - 'tests': 'wait', - 'time': 'short_time', - 'virt_system': 'linux'}, - {'_name_map_file': {'': '(host_os=linux).(virt_system=linux).(tests=wait).short'}, - '_short_name_map_file': {'': 'linux.linux.wait.short'}, - 'dep': ['(host_os=linux).(virt_system=linux).(tests=wait).long'], - 'host_os': 'linux', - 'image': 'linux', - 'name': '(host_os=linux).(virt_system=linux).(tests=wait).short', - 'run': 'wait', - 'shortname': 'linux.wait.short', - 'tests': 'wait', - 'time': 'logn_time', - 'virt_system': 'linux'}, - {'_name_map_file': {'': '(host_os=linux).(virt_system=linux).(tests=test2)'}, - '_short_name_map_file': {'': 'linux.linux.test2'}, - 'dep': [], - 'host_os': 'linux', - 'image': 'linux', - 'name': '(host_os=linux).(virt_system=linux).(tests=test2)', - 'run': 'test1', - 'shortname': 'linux.test2', - 'tests': 'test2', - 'virt_system': 'linux'}, - {'_name_map_file': {'': '(host_os=linux).(virt_system=windows).(tests=wait).long'}, - '_short_name_map_file': {'': 'linux.windows.wait.long'}, - 'dep': [], - 'host_os': 'linux', - 'image': 'linux', - 'name': '(host_os=linux).(virt_system=windows).(tests=wait).long', - 'run': 'wait', - 'shortname': 'linux.windows.wait.long', - 'tests': 'wait', - 'time': 'short_time', - 'virt_system': 'windows'}, - {'_name_map_file': {'': '(host_os=linux).(virt_system=windows).(tests=wait).short'}, - '_short_name_map_file': {'': 'linux.windows.wait.short'}, - 'dep': ['(host_os=linux).(virt_system=windows).(tests=wait).long'], - 'host_os': 'linux', - 'image': 'linux', - 'name': '(host_os=linux).(virt_system=windows).(tests=wait).short', - 'run': 'wait', - 'shortname': 'linux.windows.wait.short', - 'tests': 'wait', - 'time': 'logn_time', - 'virt_system': 'windows'}, - {'_name_map_file': {'': '(host_os=linux).(virt_system=windows).(tests=test2)'}, - '_short_name_map_file': {'': 'linux.windows.test2'}, - 'dep': [], - 'host_os': 'linux', - 'image': 'linux', - 'name': '(host_os=linux).(virt_system=windows).(tests=test2)', - 'run': 'test1', - 'shortname': 'linux.windows.test2', - 'tests': 'test2', - 'virt_system': 'windows'}, - ] - ) + [ + { + "_name_map_file": { + "": "(host_os=linux).(virt_system=linux).(tests=wait).long" + }, + "_short_name_map_file": {"": "linux.linux.wait.long"}, + "dep": [], + "host_os": "linux", + "image": "linux", + "name": "(host_os=linux).(virt_system=linux).(tests=wait).long", + "run": "wait", + "shortname": "linux.wait.long", + "tests": "wait", + "time": "short_time", + "virt_system": "linux", + }, + { + "_name_map_file": { + "": "(host_os=linux).(virt_system=linux).(tests=wait).short" + }, + "_short_name_map_file": {"": "linux.linux.wait.short"}, + "dep": ["(host_os=linux).(virt_system=linux).(tests=wait).long"], + "host_os": "linux", + "image": "linux", + "name": "(host_os=linux).(virt_system=linux).(tests=wait).short", + "run": "wait", + "shortname": "linux.wait.short", + "tests": "wait", + "time": "logn_time", + "virt_system": "linux", + }, + { + "_name_map_file": { + "": "(host_os=linux).(virt_system=linux).(tests=test2)" + }, + "_short_name_map_file": {"": "linux.linux.test2"}, + "dep": [], + "host_os": "linux", + "image": "linux", + "name": "(host_os=linux).(virt_system=linux).(tests=test2)", + "run": "test1", + "shortname": "linux.test2", + "tests": "test2", + "virt_system": "linux", + }, + { + "_name_map_file": { + "": "(host_os=linux).(virt_system=windows).(tests=wait).long" + }, + "_short_name_map_file": {"": "linux.windows.wait.long"}, + "dep": [], + "host_os": "linux", + "image": "linux", + "name": "(host_os=linux).(virt_system=windows).(tests=wait).long", + "run": "wait", + "shortname": "linux.windows.wait.long", + "tests": "wait", + "time": "short_time", + "virt_system": "windows", + }, + { + "_name_map_file": { + "": "(host_os=linux).(virt_system=windows).(tests=wait).short" + }, + "_short_name_map_file": {"": "linux.windows.wait.short"}, + "dep": ["(host_os=linux).(virt_system=windows).(tests=wait).long"], + "host_os": "linux", + "image": "linux", + "name": "(host_os=linux).(virt_system=windows).(tests=wait).short", + "run": "wait", + "shortname": "linux.windows.wait.short", + "tests": "wait", + "time": "logn_time", + "virt_system": "windows", + }, + { + "_name_map_file": { + "": "(host_os=linux).(virt_system=windows).(tests=test2)" + }, + "_short_name_map_file": {"": "linux.windows.test2"}, + "dep": [], + "host_os": "linux", + "image": "linux", + "name": "(host_os=linux).(virt_system=windows).(tests=test2)", + "run": "test1", + "shortname": "linux.windows.test2", + "tests": "test2", + "virt_system": "windows", + }, + ], + ) def testDefaults(self): - self._checkStringDump(""" + self._checkStringDump( + """ variants tests: - wait: run = "wait" @@ -230,52 +269,69 @@ def testDefaults(self): - @windows: image = windows """, - [ - {'_name_map_file': {'': '(host_os=windows).(virt_system=linux).(tests=wait).long'}, - '_short_name_map_file': {'': 'windows.linux.wait.long'}, - 'dep': [], - 'host_os': 'windows', - 'image': 'windows', - 'name': '(host_os=windows).(virt_system=linux).(tests=wait).long', - 'run': 'wait', - 'shortname': 'wait.long', - 'tests': 'wait', - 'time': 'short_time', - 'virt_system': 'linux'}, - {'_name_map_file': {'': '(host_os=windows).(virt_system=linux).(tests=wait).short'}, - '_short_name_map_file': {'': 'windows.linux.wait.short'}, - 'dep': ['(host_os=windows).(virt_system=linux).(tests=wait).long'], - 'host_os': 'windows', - 'image': 'windows', - 'name': '(host_os=windows).(virt_system=linux).(tests=wait).short', - 'run': 'wait', - 'shortname': 'wait.short', - 'tests': 'wait', - 'time': 'logn_time', - 'virt_system': 'linux'}, - {'_name_map_file': {'': '(host_os=windows).(virt_system=linux).(tests=test2)'}, - '_short_name_map_file': {'': 'windows.linux.test2'}, - 'dep': [], - 'host_os': 'windows', - 'image': 'windows', - 'name': '(host_os=windows).(virt_system=linux).(tests=test2)', - 'run': 'test1', - 'shortname': 'test2', - 'tests': 'test2', - 'virt_system': 'linux'}, - ], - True) - - self.assertRaises(cartesian_config.ParserError, - self._checkStringDump, """ + [ + { + "_name_map_file": { + "": "(host_os=windows).(virt_system=linux).(tests=wait).long" + }, + "_short_name_map_file": {"": "windows.linux.wait.long"}, + "dep": [], + "host_os": "windows", + "image": "windows", + "name": "(host_os=windows).(virt_system=linux).(tests=wait).long", + "run": "wait", + "shortname": "wait.long", + "tests": "wait", + "time": "short_time", + "virt_system": "linux", + }, + { + "_name_map_file": { + "": "(host_os=windows).(virt_system=linux).(tests=wait).short" + }, + "_short_name_map_file": {"": "windows.linux.wait.short"}, + "dep": ["(host_os=windows).(virt_system=linux).(tests=wait).long"], + "host_os": "windows", + "image": "windows", + "name": "(host_os=windows).(virt_system=linux).(tests=wait).short", + "run": "wait", + "shortname": "wait.short", + "tests": "wait", + "time": "logn_time", + "virt_system": "linux", + }, + { + "_name_map_file": { + "": "(host_os=windows).(virt_system=linux).(tests=test2)" + }, + "_short_name_map_file": {"": "windows.linux.test2"}, + "dep": [], + "host_os": "windows", + "image": "windows", + "name": "(host_os=windows).(virt_system=linux).(tests=test2)", + "run": "test1", + "shortname": "test2", + "tests": "test2", + "virt_system": "linux", + }, + ], + True, + ) + + self.assertRaises( + cartesian_config.ParserError, + self._checkStringDump, + """ variants tests [default=system2]: - system1: """, - [], - True) + [], + True, + ) def testDel(self): - self._checkStringDump(""" + self._checkStringDump( + """ variants tests: - wait: run = "wait" @@ -287,34 +343,42 @@ def testDel(self): - test2: run = "test1" """, - [ - {'_name_map_file': {'': '(tests=wait).long'}, - '_short_name_map_file': {'': 'wait.long'}, - 'dep': [], - 'name': '(tests=wait).long', - 'run': 'wait', - 'shortname': 'wait.long', - 'tests': 'wait', - 'time': 'short_time'}, - {'_name_map_file': {'': '(tests=wait).short'}, - '_short_name_map_file': {'': 'wait.short'}, - 'dep': ['(tests=wait).long'], - 'name': '(tests=wait).short', - 'run': 'wait', - 'shortname': 'wait.short', - 'tests': 'wait', - 'time': 'logn_time'}, - {'_name_map_file': {'': '(tests=test2)'}, - '_short_name_map_file': {'': 'test2'}, - 'dep': [], - 'name': '(tests=test2)', - 'run': 'test1', - 'shortname': 'test2', - 'tests': 'test2'}, - ], - True) - - self._checkStringDump(""" + [ + { + "_name_map_file": {"": "(tests=wait).long"}, + "_short_name_map_file": {"": "wait.long"}, + "dep": [], + "name": "(tests=wait).long", + "run": "wait", + "shortname": "wait.long", + "tests": "wait", + "time": "short_time", + }, + { + "_name_map_file": {"": "(tests=wait).short"}, + "_short_name_map_file": {"": "wait.short"}, + "dep": ["(tests=wait).long"], + "name": "(tests=wait).short", + "run": "wait", + "shortname": "wait.short", + "tests": "wait", + "time": "logn_time", + }, + { + "_name_map_file": {"": "(tests=test2)"}, + "_short_name_map_file": {"": "test2"}, + "dep": [], + "name": "(tests=test2)", + "run": "test1", + "shortname": "test2", + "tests": "test2", + }, + ], + True, + ) + + self._checkStringDump( + """ variants tests: - wait: run = "wait" @@ -328,33 +392,41 @@ def testDel(self): del time """, - [ - {'_name_map_file': {'': '(tests=wait).long'}, - '_short_name_map_file': {'': 'wait.long'}, - 'dep': [], - 'name': '(tests=wait).long', - 'run': 'wait', - 'shortname': 'wait.long', - 'tests': 'wait'}, - {'_name_map_file': {'': '(tests=wait).short'}, - '_short_name_map_file': {'': 'wait.short'}, - 'dep': ['(tests=wait).long'], - 'name': '(tests=wait).short', - 'run': 'wait', - 'shortname': 'wait.short', - 'tests': 'wait'}, - {'_name_map_file': {'': '(tests=test2)'}, - '_short_name_map_file': {'': 'test2'}, - 'dep': [], - 'name': '(tests=test2)', - 'run': 'test1', - 'shortname': 'test2', - 'tests': 'test2'}, - ], - True) + [ + { + "_name_map_file": {"": "(tests=wait).long"}, + "_short_name_map_file": {"": "wait.long"}, + "dep": [], + "name": "(tests=wait).long", + "run": "wait", + "shortname": "wait.long", + "tests": "wait", + }, + { + "_name_map_file": {"": "(tests=wait).short"}, + "_short_name_map_file": {"": "wait.short"}, + "dep": ["(tests=wait).long"], + "name": "(tests=wait).short", + "run": "wait", + "shortname": "wait.short", + "tests": "wait", + }, + { + "_name_map_file": {"": "(tests=test2)"}, + "_short_name_map_file": {"": "test2"}, + "dep": [], + "name": "(tests=test2)", + "run": "test1", + "shortname": "test2", + "tests": "test2", + }, + ], + True, + ) def testSuffixJoinDel(self): - self._checkStringDump(""" + self._checkStringDump( + """ variants: - x: foo = x @@ -390,102 +462,134 @@ def testSuffixJoinDel(self): join x z del foo_x """, - [ - {'_name_map_file': {'': 'control_group.x'}, - '_short_name_map_file': {'': 'control_group.x'}, - 'dep': [], - 'name': 'control_group.x', - 'shortname': 'control_group.x', - 'foo': 'x'}, - {'_name_map_file': {'': 'control_group.y'}, - '_short_name_map_file': {'': 'control_group.y'}, - 'dep': [], - 'name': 'control_group.y', - 'shortname': 'control_group.y', - 'foo': 'y'}, - {'_name_map_file': {'': 'control_group.z'}, - '_short_name_map_file': {'': 'control_group.z'}, - 'dep': [], - 'name': 'control_group.z', - 'shortname': 'control_group.z', - 'foo': 'z'}, - {'_name_map_file': {'': 'del_raw.x'}, - '_short_name_map_file': {'': 'del_raw.x'}, - 'dep': [], - 'name': 'del_raw.x', - 'shortname': 'del_raw.x', - 'foo': 'x'}, - {'_name_map_file': {'': 'del_raw.y'}, - '_short_name_map_file': {'': 'del_raw.y'}, - 'dep': [], - 'name': 'del_raw.y', - 'shortname': 'del_raw.y', - 'foo': 'y'}, - {'_name_map_file': {'': 'del_raw.z'}, - '_short_name_map_file': {'': 'del_raw.z'}, - 'dep': [], - 'name': 'del_raw.z', - 'shortname': 'del_raw.z'}, - {'_name_map_file': {'': 'del_suffix.x'}, - '_short_name_map_file': {'': 'del_suffix.x'}, - 'dep': [], - 'name': 'del_suffix.x', - 'shortname': 'del_suffix.x'}, - {'_name_map_file': {'': 'del_suffix.y'}, - '_short_name_map_file': {'': 'del_suffix.y'}, - 'dep': [], - 'name': 'del_suffix.y', - 'shortname': 'del_suffix.y', - 'foo': 'y'}, - {'_name_map_file': {'': 'del_suffix.z'}, - '_short_name_map_file': {'': 'del_suffix.z'}, - 'dep': [], - 'name': 'del_suffix.z', - 'shortname': 'del_suffix.z', - 'foo': 'z'}, - {'_name_map_file': {'': 'control_group_xy.y'}, - '_short_name_map_file': {'': 'control_group_xy.y'}, - 'dep': [], - 'name': 'control_group_xy.x.y', - 'shortname': 'control_group_xy.x.y', - 'foo_x': 'x', - 'foo_y': 'y'}, - {'_name_map_file': {'': 'del_raw_xy.y'}, - '_short_name_map_file': {'': 'del_raw_xy.y'}, - 'dep': [], - 'name': 'del_raw_xy.x.y', - 'shortname': 'del_raw_xy.x.y', - 'foo_x': 'x', - 'foo_y': 'y'}, - {'_name_map_file': {'': 'del_suffix_xy.y'}, - '_short_name_map_file': {'': 'del_suffix_xy.y'}, - 'dep': [], - 'name': 'del_suffix_xy.x.y', - 'shortname': 'del_suffix_xy.x.y', - 'foo': 'y'}, - {'_name_map_file': {'': 'control_group_xz.z'}, - '_short_name_map_file': {'': 'control_group_xz.z'}, - 'dep': [], - 'name': 'control_group_xz.x.z', - 'shortname': 'control_group_xz.x.z', - 'foo': 'z', - 'foo_x': 'x'}, - {'_name_map_file': {'': 'del_raw_xz.z'}, - '_short_name_map_file': {'': 'del_raw_xz.z'}, - 'dep': [], - 'name': 'del_raw_xz.x.z', - 'shortname': 'del_raw_xz.x.z', - 'foo': 'x'}, - {'_name_map_file': {'': 'del_suffix_xz.z'}, - '_short_name_map_file': {'': 'del_suffix_xz.z'}, - 'dep': [], - 'name': 'del_suffix_xz.x.z', - 'shortname': 'del_suffix_xz.x.z', - 'foo': 'z'}, - ], - True) - - self._checkStringDump(""" + [ + { + "_name_map_file": {"": "control_group.x"}, + "_short_name_map_file": {"": "control_group.x"}, + "dep": [], + "name": "control_group.x", + "shortname": "control_group.x", + "foo": "x", + }, + { + "_name_map_file": {"": "control_group.y"}, + "_short_name_map_file": {"": "control_group.y"}, + "dep": [], + "name": "control_group.y", + "shortname": "control_group.y", + "foo": "y", + }, + { + "_name_map_file": {"": "control_group.z"}, + "_short_name_map_file": {"": "control_group.z"}, + "dep": [], + "name": "control_group.z", + "shortname": "control_group.z", + "foo": "z", + }, + { + "_name_map_file": {"": "del_raw.x"}, + "_short_name_map_file": {"": "del_raw.x"}, + "dep": [], + "name": "del_raw.x", + "shortname": "del_raw.x", + "foo": "x", + }, + { + "_name_map_file": {"": "del_raw.y"}, + "_short_name_map_file": {"": "del_raw.y"}, + "dep": [], + "name": "del_raw.y", + "shortname": "del_raw.y", + "foo": "y", + }, + { + "_name_map_file": {"": "del_raw.z"}, + "_short_name_map_file": {"": "del_raw.z"}, + "dep": [], + "name": "del_raw.z", + "shortname": "del_raw.z", + }, + { + "_name_map_file": {"": "del_suffix.x"}, + "_short_name_map_file": {"": "del_suffix.x"}, + "dep": [], + "name": "del_suffix.x", + "shortname": "del_suffix.x", + }, + { + "_name_map_file": {"": "del_suffix.y"}, + "_short_name_map_file": {"": "del_suffix.y"}, + "dep": [], + "name": "del_suffix.y", + "shortname": "del_suffix.y", + "foo": "y", + }, + { + "_name_map_file": {"": "del_suffix.z"}, + "_short_name_map_file": {"": "del_suffix.z"}, + "dep": [], + "name": "del_suffix.z", + "shortname": "del_suffix.z", + "foo": "z", + }, + { + "_name_map_file": {"": "control_group_xy.y"}, + "_short_name_map_file": {"": "control_group_xy.y"}, + "dep": [], + "name": "control_group_xy.x.y", + "shortname": "control_group_xy.x.y", + "foo_x": "x", + "foo_y": "y", + }, + { + "_name_map_file": {"": "del_raw_xy.y"}, + "_short_name_map_file": {"": "del_raw_xy.y"}, + "dep": [], + "name": "del_raw_xy.x.y", + "shortname": "del_raw_xy.x.y", + "foo_x": "x", + "foo_y": "y", + }, + { + "_name_map_file": {"": "del_suffix_xy.y"}, + "_short_name_map_file": {"": "del_suffix_xy.y"}, + "dep": [], + "name": "del_suffix_xy.x.y", + "shortname": "del_suffix_xy.x.y", + "foo": "y", + }, + { + "_name_map_file": {"": "control_group_xz.z"}, + "_short_name_map_file": {"": "control_group_xz.z"}, + "dep": [], + "name": "control_group_xz.x.z", + "shortname": "control_group_xz.x.z", + "foo": "z", + "foo_x": "x", + }, + { + "_name_map_file": {"": "del_raw_xz.z"}, + "_short_name_map_file": {"": "del_raw_xz.z"}, + "dep": [], + "name": "del_raw_xz.x.z", + "shortname": "del_raw_xz.x.z", + "foo": "x", + }, + { + "_name_map_file": {"": "del_suffix_xz.z"}, + "_short_name_map_file": {"": "del_suffix_xz.z"}, + "dep": [], + "name": "del_suffix_xz.x.z", + "shortname": "del_suffix_xz.x.z", + "foo": "z", + }, + ], + True, + ) + + self._checkStringDump( + """ variants tests: - wait: run = "wait" @@ -499,34 +603,43 @@ def testSuffixJoinDel(self): del time """, - [ - {'_name_map_file': {'': '(tests=wait).long'}, - '_short_name_map_file': {'': 'wait.long'}, - 'dep': [], - 'name': '(tests=wait).long', - 'run': 'wait', - 'shortname': 'wait.long', - 'tests': 'wait'}, - {'_name_map_file': {'': '(tests=wait).short'}, - '_short_name_map_file': {'': 'wait.short'}, - 'dep': ['(tests=wait).long'], - 'name': '(tests=wait).short', - 'run': 'wait', - 'shortname': 'wait.short', - 'tests': 'wait'}, - {'_name_map_file': {'': '(tests=test2)'}, - '_short_name_map_file': {'': 'test2'}, - 'dep': [], - 'name': '(tests=test2)', - 'run': 'test1', - 'shortname': 'test2', - 'tests': 'test2'}, - ], - True) + [ + { + "_name_map_file": {"": "(tests=wait).long"}, + "_short_name_map_file": {"": "wait.long"}, + "dep": [], + "name": "(tests=wait).long", + "run": "wait", + "shortname": "wait.long", + "tests": "wait", + }, + { + "_name_map_file": {"": "(tests=wait).short"}, + "_short_name_map_file": {"": "wait.short"}, + "dep": ["(tests=wait).long"], + "name": "(tests=wait).short", + "run": "wait", + "shortname": "wait.short", + "tests": "wait", + }, + { + "_name_map_file": {"": "(tests=test2)"}, + "_short_name_map_file": {"": "test2"}, + "dep": [], + "name": "(tests=test2)", + "run": "test1", + "shortname": "test2", + "tests": "test2", + }, + ], + True, + ) def testError1(self): - self.assertRaises(cartesian_config.ParserError, - self._checkStringDump, """ + self.assertRaises( + cartesian_config.ParserError, + self._checkStringDump, + """ variants tests: wait: run = "wait" @@ -538,19 +651,24 @@ def testError1(self): - test2: run = "test1" """, - [], - True) + [], + True, + ) def testMissingInclude(self): - self.assertRaises(cartesian_config.MissingIncludeError, - self._checkStringDump, """ + self.assertRaises( + cartesian_config.MissingIncludeError, + self._checkStringDump, + """ include xxxxxxxxx/xxxxxxxxxxx """, - [], - True) + [], + True, + ) def testVariableAssignment(self): - self._checkStringDump(""" + self._checkStringDump( + """ variants tests: -system1: var = 1 @@ -571,28 +689,31 @@ def testVariableAssignment(self): starts_with_number = index ${1st} not_a_substitution = ${} """, - [ - {'_name_map_file': {'': '(tests=system1)'}, - '_short_name_map_file': {'': 'system1'}, - 'variable-name-with-dashes': 'sampletext', - 'ddd': 'tests variant is system1', - 'dashes': 'show sampletext', - 'dep': [], - 'error': '${tests + str(int(system) + 3)}4', - 'name': '(tests=system1)', - 'shortname': 'system1', - 'system': 'dsystem1ahoj4c4', - 'tests': 'system1', - 'var': 'b2atest', - '1st': '1', - 'starts_with_number': 'index 1', - 'not_a_substitution': '${}', - }, - ], - True) + [ + { + "_name_map_file": {"": "(tests=system1)"}, + "_short_name_map_file": {"": "system1"}, + "variable-name-with-dashes": "sampletext", + "ddd": "tests variant is system1", + "dashes": "show sampletext", + "dep": [], + "error": "${tests + str(int(system) + 3)}4", + "name": "(tests=system1)", + "shortname": "system1", + "system": "dsystem1ahoj4c4", + "tests": "system1", + "var": "b2atest", + "1st": "1", + "starts_with_number": "index 1", + "not_a_substitution": "${}", + }, + ], + True, + ) def testVariableLazyAssignment(self): - self._checkStringDump(""" + self._checkStringDump( + """ arg1 = ~balabala variants: - base_content: @@ -608,68 +729,102 @@ def testVariableLazyAssignment(self): - dummy_set: foo ~= qux """, - [ - {'_name_map_file': {'': 'lazy_set.base_content'}, - '_short_name_map_file': {'': 'lazy_set.base_content'}, - 'arg1': '~balabala', - 'dep': [], - 'foo': 'bar', - 'name': 'lazy_set.base_content', - 'shortname': 'lazy_set.base_content'}, - {'_name_map_file': {'': 'lazy_set.empty_content'}, - '_short_name_map_file': {'': 'lazy_set.empty_content'}, - 'arg1': '~balabala', - 'dep': [], - 'foo': 'baz', - 'name': 'lazy_set.empty_content', - 'shortname': 'lazy_set.empty_content'}, - {'_name_map_file': {'': 'lazy_set_with_substitution.base_content'}, - '_short_name_map_file': {'': 'lazy_set_with_substitution.base_content'}, - 'arg1': '~balabala', - 'dep': [], - 'foo': 'bar', - 'name': 'lazy_set_with_substitution.base_content', - 'shortname': 'lazy_set_with_substitution.base_content'}, - {'_name_map_file': {'': 'lazy_set_with_substitution.empty_content'}, - '_short_name_map_file': {'': 'lazy_set_with_substitution.empty_content'}, - 'arg1': '~balabala', - 'dep': [], - 'foo': '~balabala', - 'name': 'lazy_set_with_substitution.empty_content', - 'shortname': 'lazy_set_with_substitution.empty_content'}, - {'_name_map_file': {'': 'lazy_set_with_double_token.base_content'}, - '_short_name_map_file': {'': 'lazy_set_with_double_token.base_content'}, - 'arg1': '~balabala', - 'dep': [], - 'foo': 'bar', - 'name': 'lazy_set_with_double_token.base_content', - 'shortname': 'lazy_set_with_double_token.base_content'}, - {'_name_map_file': {'': 'lazy_set_with_double_token.empty_content'}, - '_short_name_map_file': {'': 'lazy_set_with_double_token.empty_content'}, - 'arg1': '~balabala', - 'dep': [], - 'foo': '~= foo', - 'name': 'lazy_set_with_double_token.empty_content', - 'shortname': 'lazy_set_with_double_token.empty_content'}, - {'_name_map_file': {'': 'dummy_set.base_content'}, - '_short_name_map_file': {'': 'dummy_set.base_content'}, - 'arg1': '~balabala', - 'dep': [], - 'foo': 'bar', - 'name': 'dummy_set.base_content', - 'shortname': 'dummy_set.base_content'}, - {'_name_map_file': {'': 'dummy_set.empty_content'}, - '_short_name_map_file': {'': 'dummy_set.empty_content'}, - 'arg1': '~balabala', - 'dep': [], - 'foo': 'qux', - 'name': 'dummy_set.empty_content', - 'shortname': 'dummy_set.empty_content'}, - ], - True) + [ + { + "_name_map_file": {"": "lazy_set.base_content"}, + "_short_name_map_file": {"": "lazy_set.base_content"}, + "arg1": "~balabala", + "dep": [], + "foo": "bar", + "name": "lazy_set.base_content", + "shortname": "lazy_set.base_content", + }, + { + "_name_map_file": {"": "lazy_set.empty_content"}, + "_short_name_map_file": {"": "lazy_set.empty_content"}, + "arg1": "~balabala", + "dep": [], + "foo": "baz", + "name": "lazy_set.empty_content", + "shortname": "lazy_set.empty_content", + }, + { + "_name_map_file": { + "": "lazy_set_with_substitution.base_content" + }, + "_short_name_map_file": { + "": "lazy_set_with_substitution.base_content" + }, + "arg1": "~balabala", + "dep": [], + "foo": "bar", + "name": "lazy_set_with_substitution.base_content", + "shortname": "lazy_set_with_substitution.base_content", + }, + { + "_name_map_file": { + "": "lazy_set_with_substitution.empty_content" + }, + "_short_name_map_file": { + "": "lazy_set_with_substitution.empty_content" + }, + "arg1": "~balabala", + "dep": [], + "foo": "~balabala", + "name": "lazy_set_with_substitution.empty_content", + "shortname": "lazy_set_with_substitution.empty_content", + }, + { + "_name_map_file": { + "": "lazy_set_with_double_token.base_content" + }, + "_short_name_map_file": { + "": "lazy_set_with_double_token.base_content" + }, + "arg1": "~balabala", + "dep": [], + "foo": "bar", + "name": "lazy_set_with_double_token.base_content", + "shortname": "lazy_set_with_double_token.base_content", + }, + { + "_name_map_file": { + "": "lazy_set_with_double_token.empty_content" + }, + "_short_name_map_file": { + "": "lazy_set_with_double_token.empty_content" + }, + "arg1": "~balabala", + "dep": [], + "foo": "~= foo", + "name": "lazy_set_with_double_token.empty_content", + "shortname": "lazy_set_with_double_token.empty_content", + }, + { + "_name_map_file": {"": "dummy_set.base_content"}, + "_short_name_map_file": {"": "dummy_set.base_content"}, + "arg1": "~balabala", + "dep": [], + "foo": "bar", + "name": "dummy_set.base_content", + "shortname": "dummy_set.base_content", + }, + { + "_name_map_file": {"": "dummy_set.empty_content"}, + "_short_name_map_file": {"": "dummy_set.empty_content"}, + "arg1": "~balabala", + "dep": [], + "foo": "qux", + "name": "dummy_set.empty_content", + "shortname": "dummy_set.empty_content", + }, + ], + True, + ) def testCondition(self): - self._checkStringDump(""" + self._checkStringDump( + """ variants tests [meta1]: - wait: run = "wait" @@ -684,35 +839,43 @@ def testCondition(self): test2: bbb = aaaa aaa = 1 """, - [ - {'_name_map_file': {'': '(tests=wait).long'}, - '_short_name_map_file': {'': 'wait.long'}, - 'dep': [], - 'name': '(tests=wait).long', - 'run': 'wait', - 'shortname': 'wait.long', - 'tests': 'wait', - 'time': 'short_time'}, - {'_name_map_file': {'': '(tests=wait).short'}, - '_short_name_map_file': {'': 'wait.short'}, - 'dep': ['(tests=wait).long'], - 'name': '(tests=wait).short', - 'run': 'wait', - 'shortname': 'wait.short', - 'tests': 'wait', - 'time': 'logn_time'}, - {'_name_map_file': {'': '(tests=test2)'}, - '_short_name_map_file': {'': 'test2'}, - 'aaa': '1', - 'bbb': 'aaaa', - 'dep': [], - 'name': '(tests=test2)', - 'run': 'test1', - 'shortname': 'test2', - 'tests': 'test2'}, - ], - True) - self._checkStringDump(""" + [ + { + "_name_map_file": {"": "(tests=wait).long"}, + "_short_name_map_file": {"": "wait.long"}, + "dep": [], + "name": "(tests=wait).long", + "run": "wait", + "shortname": "wait.long", + "tests": "wait", + "time": "short_time", + }, + { + "_name_map_file": {"": "(tests=wait).short"}, + "_short_name_map_file": {"": "wait.short"}, + "dep": ["(tests=wait).long"], + "name": "(tests=wait).short", + "run": "wait", + "shortname": "wait.short", + "tests": "wait", + "time": "logn_time", + }, + { + "_name_map_file": {"": "(tests=test2)"}, + "_short_name_map_file": {"": "test2"}, + "aaa": "1", + "bbb": "aaaa", + "dep": [], + "name": "(tests=test2)", + "run": "test1", + "shortname": "test2", + "tests": "test2", + }, + ], + True, + ) + self._checkStringDump( + """ variants: - a: foo = foo @@ -727,38 +890,48 @@ def testCondition(self): bala = balabala - d: """, - [ - {'_name_map_file': {'': 'c.a'}, - '_short_name_map_file': {'': 'c.a'}, - 'bala': 'balabala', - 'dep': [], - 'foo': 'bar', - 'name': 'c.a', - 'shortname': 'c.a'}, - {'_name_map_file': {'': 'c.b'}, - '_short_name_map_file': {'': 'c.b'}, - 'bala': 'lalalala', - 'dep': [], - 'foo': 'foob', - 'name': 'c.b', - 'shortname': 'c.b'}, - {'_name_map_file': {'': 'd.a'}, - '_short_name_map_file': {'': 'd.a'}, - 'dep': [], - 'foo': 'foo', - 'name': 'd.a', - 'shortname': 'd.a'}, - {'_name_map_file': {'': 'd.b'}, - '_short_name_map_file': {'': 'd.b'}, - 'dep': [], - 'foo': 'foob', - 'name': 'd.b', - 'shortname': 'd.b'}, - ], - True) + [ + { + "_name_map_file": {"": "c.a"}, + "_short_name_map_file": {"": "c.a"}, + "bala": "balabala", + "dep": [], + "foo": "bar", + "name": "c.a", + "shortname": "c.a", + }, + { + "_name_map_file": {"": "c.b"}, + "_short_name_map_file": {"": "c.b"}, + "bala": "lalalala", + "dep": [], + "foo": "foob", + "name": "c.b", + "shortname": "c.b", + }, + { + "_name_map_file": {"": "d.a"}, + "_short_name_map_file": {"": "d.a"}, + "dep": [], + "foo": "foo", + "name": "d.a", + "shortname": "d.a", + }, + { + "_name_map_file": {"": "d.b"}, + "_short_name_map_file": {"": "d.b"}, + "dep": [], + "foo": "foob", + "name": "d.b", + "shortname": "d.b", + }, + ], + True, + ) def testNegativeCondition(self): - self._checkStringDump(""" + self._checkStringDump( + """ variants tests [meta1]: - wait: run = "wait" @@ -773,40 +946,49 @@ def testNegativeCondition(self): !test2: bbb = aaaa aaa = 1 """, - [ - {'_name_map_file': {'': '(tests=wait).long'}, - '_short_name_map_file': {'': 'wait.long'}, - 'aaa': '1', - 'bbb': 'aaaa', - 'dep': [], - 'name': '(tests=wait).long', - 'run': 'wait', - 'shortname': 'wait.long', - 'tests': 'wait', - 'time': 'short_time'}, - {'_name_map_file': {'': '(tests=wait).short'}, - '_short_name_map_file': {'': 'wait.short'}, - 'aaa': '1', - 'bbb': 'aaaa', - 'dep': ['(tests=wait).long'], - 'name': '(tests=wait).short', - 'run': 'wait', - 'shortname': 'wait.short', - 'tests': 'wait', - 'time': 'logn_time'}, - {'_name_map_file': {'': '(tests=test2)'}, - '_short_name_map_file': {'': 'test2'}, - 'dep': [], - 'name': '(tests=test2)', - 'run': 'test1', - 'shortname': 'test2', - 'tests': 'test2'}, - ], - True) + [ + { + "_name_map_file": {"": "(tests=wait).long"}, + "_short_name_map_file": {"": "wait.long"}, + "aaa": "1", + "bbb": "aaaa", + "dep": [], + "name": "(tests=wait).long", + "run": "wait", + "shortname": "wait.long", + "tests": "wait", + "time": "short_time", + }, + { + "_name_map_file": {"": "(tests=wait).short"}, + "_short_name_map_file": {"": "wait.short"}, + "aaa": "1", + "bbb": "aaaa", + "dep": ["(tests=wait).long"], + "name": "(tests=wait).short", + "run": "wait", + "shortname": "wait.short", + "tests": "wait", + "time": "logn_time", + }, + { + "_name_map_file": {"": "(tests=test2)"}, + "_short_name_map_file": {"": "test2"}, + "dep": [], + "name": "(tests=test2)", + "run": "test1", + "shortname": "test2", + "tests": "test2", + }, + ], + True, + ) def testSyntaxErrors(self): - self.assertRaises(cartesian_config.LexerError, - self._checkStringDump, """ + self.assertRaises( + cartesian_config.LexerError, + self._checkStringDump, + """ variants tests$: - system1: var = 1 @@ -819,88 +1001,120 @@ def testSyntaxErrors(self): s.* ?<= d system += 4 """, - [], - True) - - self.assertRaises(cartesian_config.LexerError, - self._checkStringDump, """ + [], + True, + ) + + self.assertRaises( + cartesian_config.LexerError, + self._checkStringDump, + """ variants tests [defaul$$$$t=system1]: - system1: """, - [], - True) - - self.assertRaises(cartesian_config.ParserError, - self._checkStringDump, """ + [], + True, + ) + + self.assertRaises( + cartesian_config.ParserError, + self._checkStringDump, + """ variants tests [default=system1] wrong: - system1: """, - [], - True) - - self.assertRaises(cartesian_config.ParserError, - self._checkStringDump, """ + [], + True, + ) + + self.assertRaises( + cartesian_config.ParserError, + self._checkStringDump, + """ only xxx...yyy """, - [], - True) - - self.assertRaises(cartesian_config.ParserError, - self._checkStringDump, """ + [], + True, + ) + + self.assertRaises( + cartesian_config.ParserError, + self._checkStringDump, + """ only xxx..,yyy """, - [], - True) - - self.assertRaises(cartesian_config.ParserError, - self._checkStringDump, """ + [], + True, + ) + + self.assertRaises( + cartesian_config.ParserError, + self._checkStringDump, + """ aaabbbb.ddd """, - [], - True) - - self.assertRaises(cartesian_config.ParserError, - self._checkStringDump, """ + [], + True, + ) + + self.assertRaises( + cartesian_config.ParserError, + self._checkStringDump, + """ aaa.bbb: variants test: -sss: """, - [], - True) - - self.assertRaises(cartesian_config.ParserError, - self._checkStringDump, """ + [], + True, + ) + + self.assertRaises( + cartesian_config.ParserError, + self._checkStringDump, + """ variants test [sss = bbb: -sss: """, - [], - True) - - self.assertRaises(cartesian_config.ParserError, - self._checkStringDump, """ + [], + True, + ) + + self.assertRaises( + cartesian_config.ParserError, + self._checkStringDump, + """ variants test [default]: -sss: """, - [], - True) - - self.assertRaises(cartesian_config.ParserError, - self._checkStringDump, """ + [], + True, + ) + + self.assertRaises( + cartesian_config.ParserError, + self._checkStringDump, + """ variants test [default] ddd: -sss: """, - [], - True) - - self.assertRaises(cartesian_config.ParserError, - self._checkStringDump, """ + [], + True, + ) + + self.assertRaises( + cartesian_config.ParserError, + self._checkStringDump, + """ variants test [default] ddd """, - [], - True) + [], + True, + ) def testComplicatedFilter(self): - self._checkStringDump(""" + self._checkStringDump( + """ variants tests: - wait: run = "wait" @@ -928,65 +1142,86 @@ def testComplicatedFilter(self): start = windows only test2 """, - [ - {'_name_map_file': {'': '(host_os=linux).(guest_os=linux).(tests=wait).long'}, - '_short_name_map_file': {'': 'linux.linux.wait.long'}, - 'dep': [], - 'guest_os': 'linux', - 'host_os': 'linux', - 'install': 'linux', - 'name': '(host_os=linux).(guest_os=linux).(tests=wait).long', - 'run': 'wait', - 'shortname': 'linux.linux.wait.long', - 'start': 'linux', - 'tests': 'wait', - 'time': 'short_time'}, - {'_name_map_file': {'': '(host_os=linux).(guest_os=linux).(tests=test2)'}, - '_short_name_map_file': {'': 'linux.linux.test2'}, - 'dep': [], - 'guest_os': 'linux', - 'host_os': 'linux', - 'install': 'linux', - 'name': '(host_os=linux).(guest_os=linux).(tests=test2)', - 'run': 'test1', - 'shortname': 'linux.linux.test2', - 'start': 'linux', - 'tests': 'test2'}, - {'_name_map_file': {'': '(host_os=linux).(guest_os=windows).(tests=test2)'}, - '_short_name_map_file': {'': 'linux.windows.test2'}, - 'dep': [], - 'guest_os': 'windows', - 'host_os': 'linux', - 'install': 'windows', - 'name': '(host_os=linux).(guest_os=windows).(tests=test2)', - 'run': 'test1', - 'shortname': 'linux.windows.test2', - 'start': 'linux', - 'tests': 'test2'}, - {'_name_map_file': {'': '(host_os=windows).(guest_os=linux).(tests=test2)'}, - '_short_name_map_file': {'': 'windows.linux.test2'}, - 'dep': [], - 'guest_os': 'linux', - 'host_os': 'windows', - 'install': 'linux', - 'name': '(host_os=windows).(guest_os=linux).(tests=test2)', - 'run': 'test1', - 'shortname': 'windows.linux.test2', - 'start': 'windows', - 'tests': 'test2'}, - {'_name_map_file': {'': '(host_os=windows).(guest_os=windows).(tests=test2)'}, - '_short_name_map_file': {'': 'windows.windows.test2'}, - 'dep': [], - 'guest_os': 'windows', - 'host_os': 'windows', - 'install': 'windows', - 'name': '(host_os=windows).(guest_os=windows).(tests=test2)', - 'run': 'test1', - 'shortname': 'windows.windows.test2', - 'start': 'windows', - 'tests': 'test2'}, - ], - True) + [ + { + "_name_map_file": { + "": "(host_os=linux).(guest_os=linux).(tests=wait).long" + }, + "_short_name_map_file": {"": "linux.linux.wait.long"}, + "dep": [], + "guest_os": "linux", + "host_os": "linux", + "install": "linux", + "name": "(host_os=linux).(guest_os=linux).(tests=wait).long", + "run": "wait", + "shortname": "linux.linux.wait.long", + "start": "linux", + "tests": "wait", + "time": "short_time", + }, + { + "_name_map_file": { + "": "(host_os=linux).(guest_os=linux).(tests=test2)" + }, + "_short_name_map_file": {"": "linux.linux.test2"}, + "dep": [], + "guest_os": "linux", + "host_os": "linux", + "install": "linux", + "name": "(host_os=linux).(guest_os=linux).(tests=test2)", + "run": "test1", + "shortname": "linux.linux.test2", + "start": "linux", + "tests": "test2", + }, + { + "_name_map_file": { + "": "(host_os=linux).(guest_os=windows).(tests=test2)" + }, + "_short_name_map_file": {"": "linux.windows.test2"}, + "dep": [], + "guest_os": "windows", + "host_os": "linux", + "install": "windows", + "name": "(host_os=linux).(guest_os=windows).(tests=test2)", + "run": "test1", + "shortname": "linux.windows.test2", + "start": "linux", + "tests": "test2", + }, + { + "_name_map_file": { + "": "(host_os=windows).(guest_os=linux).(tests=test2)" + }, + "_short_name_map_file": {"": "windows.linux.test2"}, + "dep": [], + "guest_os": "linux", + "host_os": "windows", + "install": "linux", + "name": "(host_os=windows).(guest_os=linux).(tests=test2)", + "run": "test1", + "shortname": "windows.linux.test2", + "start": "windows", + "tests": "test2", + }, + { + "_name_map_file": { + "": "(host_os=windows).(guest_os=windows).(tests=test2)" + }, + "_short_name_map_file": {"": "windows.windows.test2"}, + "dep": [], + "guest_os": "windows", + "host_os": "windows", + "install": "windows", + "name": "(host_os=windows).(guest_os=windows).(tests=test2)", + "run": "test1", + "shortname": "windows.windows.test2", + "start": "windows", + "tests": "test2", + }, + ], + True, + ) f = "only xxx.yyy..(xxx=333).aaa, ddd (eeee) rrr.aaa" @@ -997,19 +1232,26 @@ def testComplicatedFilter(self): lexer.get_next_check([cartesian_config.LIndent]) lexer.get_next_check([cartesian_config.LOnly]) p_filter = cartesian_config.parse_filter(lexer, lexer.rest_line()) - self.assertEquals(p_filter, - [[[cartesian_config.Label("xxx"), - cartesian_config.Label("yyy")], - [cartesian_config.Label("xxx", "333"), - cartesian_config.Label("aaa")]], - [[cartesian_config.Label("ddd")]], - [[cartesian_config.Label("eeee")]], - [[cartesian_config.Label("rrr"), - cartesian_config.Label("aaa")]]], - "Failed to parse filter.") + self.assertEquals( + p_filter, + [ + [ + [cartesian_config.Label("xxx"), cartesian_config.Label("yyy")], + [ + cartesian_config.Label("xxx", "333"), + cartesian_config.Label("aaa"), + ], + ], + [[cartesian_config.Label("ddd")]], + [[cartesian_config.Label("eeee")]], + [[cartesian_config.Label("rrr"), cartesian_config.Label("aaa")]], + ], + "Failed to parse filter.", + ) def testJoinSubstitution(self): - self._checkStringDump(""" + self._checkStringDump( + """ key0 = "Baz" variants: - one: @@ -1063,70 +1305,76 @@ def testJoinSubstitution(self): #test21 = "${key2}" #test22 = "${key3}" """, - [ - {'_name_map_file': {'': 'alpha.two'}, - '_short_name_map_file': {'': 'alpha.two'}, - 'dep': [], - 'key0': 'Baz', - 'key1': 'Alpha', - 'key1_v1': 'Hello', - 'key1_v2': 'Bye', - 'key2': 'Foo', - 'key3': 'Bar', - 'name': 'alpha.one.two', - 'shortname': 'alpha.one.two', - 'test01': 'Hello', - #'test02': '${key1_v1}', - #'test03': '${key1_v2}', - 'test04': 'Bye', - #'test05': '${key1_v1}', - #'test06': '${key1_v2}', - #'test07': 'Bye', - #'test08': '${key1_v1}', - #'test09': '${key1_v2}', - #'test10': '${key2}', - #'test11': 'Bar', - 'test12': 'Alpha', - #'test18': 'Alpha', - #'test19': '${key1_v1}', - #'test20': 'Bye', - #'test21': '${key2}', - #'test22': 'Bar', - 'test100': 'Baz'}, - {'_name_map_file': {'': 'beta.two'}, - '_short_name_map_file': {'': 'beta.two'}, - 'dep': [], - 'key0': 'Baz', - 'key1_v1': 'Hello', - 'key1_v2': 'Bye', - 'key2': 'Foo', - 'key3': 'Bar', - 'name': 'beta.one.two', - 'shortname': 'beta.one.two', - 'test01': 'Hello', - #'test02': '${key1_v1}', - #'test03': '${key1_v2}', - 'test04': 'Bye', - #'test05': '${key1_v1}', - #'test06': '${key1_v2}', - #'test13': 'Bye', - #'test14': '${key1_v1}', - #'test15': '${key1_v2}', - #'test16': '${key2}', - #'test17': 'Bar', - #'test18': 'Bye', - #'test19': '${key1_v1}', - #'test20': '${key1_v2}', - #'test21': '${key2}', - #'test22': 'Bar', - 'test100': 'Baz'}, - ], - True) + [ + { + "_name_map_file": {"": "alpha.two"}, + "_short_name_map_file": {"": "alpha.two"}, + "dep": [], + "key0": "Baz", + "key1": "Alpha", + "key1_v1": "Hello", + "key1_v2": "Bye", + "key2": "Foo", + "key3": "Bar", + "name": "alpha.one.two", + "shortname": "alpha.one.two", + "test01": "Hello", + #'test02': '${key1_v1}', + #'test03': '${key1_v2}', + "test04": "Bye", + #'test05': '${key1_v1}', + #'test06': '${key1_v2}', + #'test07': 'Bye', + #'test08': '${key1_v1}', + #'test09': '${key1_v2}', + #'test10': '${key2}', + #'test11': 'Bar', + "test12": "Alpha", + #'test18': 'Alpha', + #'test19': '${key1_v1}', + #'test20': 'Bye', + #'test21': '${key2}', + #'test22': 'Bar', + "test100": "Baz", + }, + { + "_name_map_file": {"": "beta.two"}, + "_short_name_map_file": {"": "beta.two"}, + "dep": [], + "key0": "Baz", + "key1_v1": "Hello", + "key1_v2": "Bye", + "key2": "Foo", + "key3": "Bar", + "name": "beta.one.two", + "shortname": "beta.one.two", + "test01": "Hello", + #'test02': '${key1_v1}', + #'test03': '${key1_v2}', + "test04": "Bye", + #'test05': '${key1_v1}', + #'test06': '${key1_v2}', + #'test13': 'Bye', + #'test14': '${key1_v1}', + #'test15': '${key1_v2}', + #'test16': '${key2}', + #'test17': 'Bar', + #'test18': 'Bye', + #'test19': '${key1_v1}', + #'test20': '${key1_v2}', + #'test21': '${key2}', + #'test22': 'Bar', + "test100": "Baz", + }, + ], + True, + ) def testHugeTest1(self): - self._checkConfigDump('testcfg.huge/test1.cfg', - 'testcfg.huge/test1.cfg.repr.gz') + self._checkConfigDump( + "testcfg.huge/test1.cfg", "testcfg.huge/test1.cfg.repr.gz" + ) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_cartesian_config_lint.py b/selftests/unit/test_cartesian_config_lint.py index 480d2aaf1f..7a63a1cefc 100644 --- a/selftests/unit/test_cartesian_config_lint.py +++ b/selftests/unit/test_cartesian_config_lint.py @@ -12,12 +12,11 @@ BASEDIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -RHELDIR = os.path.join(BASEDIR, 'shared', 'cfg', 'guest-os', 'Linux', 'RHEL') -UNATTENDEDDIR = os.path.join(BASEDIR, 'shared', 'unattended') +RHELDIR = os.path.join(BASEDIR, "shared", "cfg", "guest-os", "Linux", "RHEL") +UNATTENDEDDIR = os.path.join(BASEDIR, "shared", "unattended") class CartesianCfgLint(unittest.TestCase): - @staticmethod def get_cfg_as_dict(path, drop_only=True, drop_conditional_assigment=True): """ @@ -36,68 +35,85 @@ def get_cfg_as_dict(path, drop_only=True, drop_conditional_assigment=True): """ lines = open(path).readlines() if drop_only: - lines = [l for l in lines - if not re.match('^\s*only\s+', l)] + lines = [l for l in lines if not re.match("^\s*only\s+", l)] if drop_conditional_assigment: - lines = [l for l in lines - if not re.match('^\s*[a-zA-Z0-9_]+([\s,])?.*\:$', l)] + lines = [ + l for l in lines if not re.match("^\s*[a-zA-Z0-9_]+([\s,])?.*\:$", l) + ] lines.insert(0, "variants:") content = "\n".join(lines) parser = cartesian_config.Parser() parser.parse_string(content) dicts = [d for d in parser.get_dicts()] - len_dicts = (len(dicts)) + len_dicts = len(dicts) assert len_dicts == 1 return dicts[0] - @unittest.skipIf(not os.path.isdir(RHELDIR), - "Could not find RHEL configuration dir") + @unittest.skipIf( + not os.path.isdir(RHELDIR), "Could not find RHEL configuration dir" + ) def test_rhel_iso_names(self): - arch_map = {'i386': '32', - 'x86_64': '64', - 'ppc64': 'ppc64', - 'ppc64le': 'ppc64le', - 'aarch64': 'aarch64'} + arch_map = { + "i386": "32", + "x86_64": "64", + "ppc64": "ppc64", + "ppc64le": "ppc64le", + "aarch64": "aarch64", + } for major in (5, 6, 7): - minors = set([ver.split(".")[1] for ver in - glob.glob(os.path.join(RHELDIR, "%s.*" % major))]) + minors = set( + [ + ver.split(".")[1] + for ver in glob.glob(os.path.join(RHELDIR, "%s.*" % major)) + ] + ) for minor in minors: - if minor == 'devel': + if minor == "devel": continue generic_cfg = "%s.%s.cfg" % (major, minor) generic_cfg_path = os.path.join(RHELDIR, generic_cfg) config_dict = self.get_cfg_as_dict(generic_cfg_path) - self.assertEqual(config_dict['shortname'], - "%s.%s" % (major, minor)) - self.assertEqual(config_dict['image_name'], - 'images/rhel%s%s' % (major, minor)) + self.assertEqual(config_dict["shortname"], "%s.%s" % (major, minor)) + self.assertEqual( + config_dict["image_name"], "images/rhel%s%s" % (major, minor) + ) for arch, alt in list(arch_map.items()): arch_cfg = "%s.%s/%s.cfg" % (major, minor, arch) arch_cfg_path = os.path.join(RHELDIR, arch_cfg) if not os.path.exists(arch_cfg_path): continue config_dict = self.get_cfg_as_dict(arch_cfg_path) - if 'cdrom_unattended' in config_dict: - self.assertEqual(config_dict['cdrom_unattended'], - 'images/rhel%s%s-%s/ks.iso' % (major, minor, alt)) - if 'kernel' in config_dict: - self.assertEqual(config_dict['kernel'], - 'images/rhel%s%s-%s/vmlinuz' % (major, minor, alt)) - if 'initrd' in config_dict: - self.assertEqual(config_dict['initrd'], - 'images/rhel%s%s-%s/initrd.img' % (major, minor, alt)) - if 'cdrom_cd1' in config_dict: - self.assertEqual(config_dict['cdrom_cd1'], - 'isos/linux/RHEL-%s.%s-%s-DVD.iso' % (major, minor, arch)) + if "cdrom_unattended" in config_dict: + self.assertEqual( + config_dict["cdrom_unattended"], + "images/rhel%s%s-%s/ks.iso" % (major, minor, alt), + ) + if "kernel" in config_dict: + self.assertEqual( + config_dict["kernel"], + "images/rhel%s%s-%s/vmlinuz" % (major, minor, alt), + ) + if "initrd" in config_dict: + self.assertEqual( + config_dict["initrd"], + "images/rhel%s%s-%s/initrd.img" % (major, minor, alt), + ) + if "cdrom_cd1" in config_dict: + self.assertEqual( + config_dict["cdrom_cd1"], + "isos/linux/RHEL-%s.%s-%s-DVD.iso" % (major, minor, arch), + ) - @unittest.skipIf(not os.path.isdir(UNATTENDEDDIR), - "Could not find unattended configuration dir") + @unittest.skipIf( + not os.path.isdir(UNATTENDEDDIR), "Could not find unattended configuration dir" + ) def test_unattended_kickstart_password_123456(self): """ Tests if passwords in unattended installs are set to 123456 """ - rootpw_regex = re.compile(r'^rootpw\s+(--plaintext\s+)?123456\s?$', - re.MULTILINE) - for ks in glob.glob(os.path.join(UNATTENDEDDIR, '*.ks')): + rootpw_regex = re.compile( + r"^rootpw\s+(--plaintext\s+)?123456\s?$", re.MULTILINE + ) + for ks in glob.glob(os.path.join(UNATTENDEDDIR, "*.ks")): self.assertIsNotNone(rootpw_regex.search(open(ks).read())) diff --git a/selftests/unit/test_env_process.py b/selftests/unit/test_env_process.py index d6af7a6c0b..063335d3ce 100644 --- a/selftests/unit/test_env_process.py +++ b/selftests/unit/test_env_process.py @@ -10,19 +10,26 @@ class QEMUVersion(unittest.TestCase): - def test_regex(self): versions_expected = { - 'QEMU emulator version 2.9.0(qemu-kvm-rhev-2.9.0-16.el7_4.8)': ( - '2.9.0', 'qemu-kvm-rhev-2.9.0-16.el7_4.8'), - 'QEMU emulator version 2.10.50 (v2.10.0-594-gf75637badd)': ( - '2.10.50', 'v2.10.0-594-gf75637badd'), - 'QEMU emulator version 2.7.1(qemu-2.7.1-7.fc25), Copyright (c) ' - '2003-2016 Fabrice Bellard and the QEMU Project developers': ( - '2.7.1', 'qemu-2.7.1-7.fc25'), - 'QEMU PC emulator version 0.12.1 (qemu-kvm-0.12.1.2-2.503.el6_9.3),' - ' Copyright (c) 2003-2008 Fabrice Bellard': ( - '0.12.1', 'qemu-kvm-0.12.1.2-2.503.el6_9.3') + "QEMU emulator version 2.9.0(qemu-kvm-rhev-2.9.0-16.el7_4.8)": ( + "2.9.0", + "qemu-kvm-rhev-2.9.0-16.el7_4.8", + ), + "QEMU emulator version 2.10.50 (v2.10.0-594-gf75637badd)": ( + "2.10.50", + "v2.10.0-594-gf75637badd", + ), + "QEMU emulator version 2.7.1(qemu-2.7.1-7.fc25), Copyright (c) " + "2003-2016 Fabrice Bellard and the QEMU Project developers": ( + "2.7.1", + "qemu-2.7.1-7.fc25", + ), + "QEMU PC emulator version 0.12.1 (qemu-kvm-0.12.1.2-2.503.el6_9.3)," + " Copyright (c) 2003-2008 Fabrice Bellard": ( + "0.12.1", + "qemu-kvm-0.12.1.2-2.503.el6_9.3", + ), } for version, expected in list(versions_expected.items()): match = re.match(QEMU_VERSION_RE, version) diff --git a/selftests/unit/test_installer.py b/selftests/unit/test_installer.py index 63ad22a9c0..71309313f6 100644 --- a/selftests/unit/test_installer.py +++ b/selftests/unit/test_installer.py @@ -6,7 +6,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import installer @@ -14,13 +14,12 @@ class installer_test(unittest.TestCase): - def setUp(self): self.registry = installer.InstallerRegistry() def test_register_get_installer(self): - install_mode = 'custom_install_mode' - virt_type = 'custom_virt_type' + install_mode = "custom_install_mode" + virt_type = "custom_virt_type" class CustomVirtInstaller: pass @@ -30,19 +29,18 @@ class CustomVirtInstaller: self.assertTrue(klass is CustomVirtInstaller) def test_register_get_installer_default(self): - install_mode = 'base_install_mode' + install_mode = "base_install_mode" class BaseVirtInstaller: pass self.registry.register(install_mode, BaseVirtInstaller) - klass = self.registry.get_installer(install_mode, - get_default_virt=True) + klass = self.registry.get_installer(install_mode, get_default_virt=True) self.assertTrue(klass is BaseVirtInstaller) - klass = self.registry.get_installer(install_mode, - virt=None, - get_default_virt=True) + klass = self.registry.get_installer( + install_mode, virt=None, get_default_virt=True + ) self.assertTrue(klass is BaseVirtInstaller) def test_make_installer(self): @@ -50,13 +48,10 @@ def test_make_installer(self): vm_type = test""" class Installer: - def __init__(self, mode, name, test, params): pass - installer.INSTALLER_REGISTRY.register('test_install_mode', - Installer, - 'test') + installer.INSTALLER_REGISTRY.register("test_install_mode", Installer, "test") config_parser = cartesian_config.Parser() config_parser.parse_string(config) @@ -66,5 +61,5 @@ def __init__(self, mode, name, test, params): self.assertTrue(isinstance(instance, Installer)) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_iscsi.py b/selftests/unit/test_iscsi.py index eda5f695bf..9d65dd278f 100644 --- a/selftests/unit/test_iscsi.py +++ b/selftests/unit/test_iscsi.py @@ -9,7 +9,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest.unittest_utils import mock @@ -18,7 +18,6 @@ class iscsi_test(unittest.TestCase): - def setup_stubs_init(self): path.find_command.expect_call("iscsiadm") path.find_command.expect_call("targetcli") @@ -32,41 +31,40 @@ def setup_stubs_login(self, iscsi_obj): os.path.isfile.expect_call(iscsi_obj.emulated_image).and_return(False) process.system.expect_call(c_cmd) self.setup_stubs_export_target(iscsi_obj) - self.setup_stubs_portal_visible(iscsi_obj, "127.0.0.1:3260,1 %s" - % iscsi_obj.target) + self.setup_stubs_portal_visible( + iscsi_obj, "127.0.0.1:3260,1 %s" % iscsi_obj.target + ) lg_msg = "successful" process.system_output.expect_call(lg_cmd).and_return(lg_msg) def setup_stubs_get_device_name(self, iscsi_obj): s_msg = "tcp: [15] 127.0.0.1:3260,1 %s" % iscsi_obj.target - process.system_output.expect_call("iscsiadm --mode session", - ignore_status=True - ).and_return(s_msg) + process.system_output.expect_call( + "iscsiadm --mode session", ignore_status=True + ).and_return(s_msg) detail = "Target: %s\n Attached scsi disk " % iscsi_obj.target detail += "sdb State running" - process.system_output.expect_call("iscsiadm -m session -P 3" - ).and_return(detail) + process.system_output.expect_call("iscsiadm -m session -P 3").and_return(detail) def setup_stubs_cleanup(self, iscsi_obj, fname=""): s_msg = "tcp [15] 127.0.0.1:3260,1 %s" % iscsi_obj.target - process.system_output.expect_call("iscsiadm --mode session", - ignore_status=True - ).and_return(s_msg) + process.system_output.expect_call( + "iscsiadm --mode session", ignore_status=True + ).and_return(s_msg) out_cmd = "iscsiadm --mode node --logout -T %s" % iscsi_obj.target process.system_output.expect_call(out_cmd).and_return("successful") out_cmd = "iscsiadm --mode node" ret_str = "127.0.0.1:3260,1 %s" % iscsi_obj.target - process.system_output.expect_call(out_cmd, - ignore_status=True - ).and_return(ret_str) + process.system_output.expect_call(out_cmd, ignore_status=True).and_return( + ret_str + ) out_cmd = "iscsiadm -m node -o delete -T %s " % iscsi_obj.target out_cmd += "--portal 127.0.0.1" process.system.expect_call(out_cmd, ignore_status=True).and_return("") os.path.isfile.expect_call(fname).and_return(False) s_cmd = "targetcli /backstores/fileio ls" - process.system_output.expect_call(s_cmd - ).and_return("Target 1: iqn.iscsitest") + process.system_output.expect_call(s_cmd).and_return("Target 1: iqn.iscsitest") cmd = "targetcli ls /iscsi 1" process.system_output.expect_call(cmd).and_return(iscsi_obj.target) d_cmd = "targetcli /iscsi delete %s" % iscsi_obj.target @@ -75,22 +73,23 @@ def setup_stubs_cleanup(self, iscsi_obj, fname=""): process.system.expect_call(cmd) def setup_stubs_logged_in(self, result=""): - process.system_output.expect_call("iscsiadm --mode session", - ignore_status=True - ).and_return(result) + process.system_output.expect_call( + "iscsiadm --mode session", ignore_status=True + ).and_return(result) def setup_stubs_portal_visible(self, iscsi_obj, result=""): host_name = iscsi_obj.portal_ip v_cmd = "iscsiadm -m discovery -t sendtargets -p %s" % host_name - process.system_output.expect_call(v_cmd, - ignore_status=True).and_return(result) + process.system_output.expect_call(v_cmd, ignore_status=True).and_return(result) def setup_stubs_export_target(self, iscsi_obj): cmd = "targetcli ls /iscsi 1" process.system_output.expect_call(cmd).and_return("") utils_selinux.is_enforcing.expect_call().and_return(False) - cmd = "targetcli /backstores/fileio/ create %s %s" % (iscsi_obj.device, - iscsi_obj.emulated_image) + cmd = "targetcli /backstores/fileio/ create %s %s" % ( + iscsi_obj.device, + iscsi_obj.emulated_image, + ) process.system_output.expect_call(cmd).and_return("Created fileio") cmd = "targetcli /iscsi/ create %s" % iscsi_obj.target process.system_output.expect_call(cmd).and_return("Created target") @@ -106,8 +105,9 @@ def setup_stubs_export_target(self, iscsi_obj): cmd = "firewall-cmd --reload" process.system.expect_call(cmd) self.setup_stubs_set_chap_auth_target(iscsi_obj) - self.setup_stubs_portal_visible(iscsi_obj, "127.0.0.1:3260,1 %s" - % iscsi_obj.target) + self.setup_stubs_portal_visible( + iscsi_obj, "127.0.0.1:3260,1 %s" % iscsi_obj.target + ) self.setup_stubs_set_chap_auth_initiator(iscsi_obj) cmd = "targetcli / saveconfig" process.system.expect_call(cmd) @@ -147,9 +147,9 @@ def setup_stubs_set_chap_auth_target(self, iscsi_obj): process.system.expect_call(cmd) def setup_stubs_set_chap_auth_initiator(self, iscsi_obj): - u_name = {'node.session.auth.authmethod': 'CHAP'} - u_name['node.session.auth.username'] = iscsi_obj.chap_user - u_name['node.session.auth.password'] = iscsi_obj.chap_passwd + u_name = {"node.session.auth.authmethod": "CHAP"} + u_name["node.session.auth.username"] = iscsi_obj.chap_user + u_name["node.session.auth.password"] = iscsi_obj.chap_passwd for name in list(u_name.keys()): u_cmd = "iscsiadm --mode node --targetname %s " % iscsi_obj.target u_cmd += "--op update --name %s --value %s" % (name, u_name[name]) @@ -163,11 +163,13 @@ def setUp(self): # "portal_ip": "", # "initiator": ""} - self.iscsi_emulated_params = {"emulated_image": "/tmp/iscsitest", - "target": "iqn.2003-01.org.linux:iscsitest", - "image_size": "1024K", - "chap_user": "tester", - "chap_passwd": "123456"} + self.iscsi_emulated_params = { + "emulated_image": "/tmp/iscsitest", + "target": "iqn.2003-01.org.linux:iscsitest", + "image_size": "1024K", + "chap_user": "tester", + "chap_passwd": "123456", + } self.god = mock.mock_god() self.god.stub_function(path, "find_command") self.god.stub_function(process, "system") @@ -204,8 +206,9 @@ def test_iscsi_visible(self): iscsi_emulated = iscsi.Iscsi.create_iSCSI(self.iscsi_emulated_params) self.setup_stubs_portal_visible(iscsi_emulated) self.assertFalse(iscsi_emulated.portal_visible()) - self.setup_stubs_portal_visible(iscsi_emulated, "127.0.0.1:3260,1 %s" - % iscsi_emulated.target) + self.setup_stubs_portal_visible( + iscsi_emulated, "127.0.0.1:3260,1 %s" % iscsi_emulated.target + ) def test_iscsi_target_id(self): self.setup_stubs_init() diff --git a/selftests/unit/test_libvirt_network.py b/selftests/unit/test_libvirt_network.py index efc9339c26..957fc0c1a6 100644 --- a/selftests/unit/test_libvirt_network.py +++ b/selftests/unit/test_libvirt_network.py @@ -12,22 +12,22 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from test_virsh import FakeVirshFactory from virttest.libvirt_xml.network_xml import NetworkXML # The output of virsh.net_list with only default net -_DEFAULT_NET = (' Name State Autostart Persistent\n' - '----------------------------------------------------------\n' - ' default active yes yes\n') +_DEFAULT_NET = ( + " Name State Autostart Persistent\n" + "----------------------------------------------------------\n" + " default active yes yes\n" +) # Set initial state of test net global _net_state -_net_state = {'active': False, - 'autostart': False, - 'persistent': False} +_net_state = {"active": False, "autostart": False, "persistent": False} class NetworkTestBase(unittest.TestCase): @@ -37,73 +37,77 @@ class NetworkTestBase(unittest.TestCase): """ @staticmethod - def _net_list(option='--all', **dargs): + def _net_list(option="--all", **dargs): """Bogus net_list command""" - cmd = 'virsh net-list --all' - if not _net_state['active'] and not _net_state['persistent']: - test_net = '' + cmd = "virsh net-list --all" + if not _net_state["active"] and not _net_state["persistent"]: + test_net = "" else: - if _net_state['active']: - active = 'active' + if _net_state["active"]: + active = "active" else: - active = 'inactive' + active = "inactive" - if _net_state['persistent']: - persistent = 'yes' + if _net_state["persistent"]: + persistent = "yes" else: - persistent = 'no' + persistent = "no" - if _net_state['autostart']: - autostart = 'yes' + if _net_state["autostart"]: + autostart = "yes" else: - autostart = 'no' - - test_net = ' %-21s%-11s%-14s%-11s\n' % ( - 'unittest', active, autostart, persistent) + autostart = "no" + + test_net = " %-21s%-11s%-14s%-11s\n" % ( + "unittest", + active, + autostart, + persistent, + ) output = _DEFAULT_NET + test_net return CmdResult(cmd, output) @staticmethod - def _net_define(xmlfile='unittest.xml', **dargs): + def _net_define(xmlfile="unittest.xml", **dargs): """Bogus net_define command""" - _net_state['persistent'] = True + _net_state["persistent"] = True @staticmethod - def _net_undefine(name='unittest', **dargs): + def _net_undefine(name="unittest", **dargs): """Bogus net_undefine command""" - _net_state['persistent'] = False - _net_state['autostart'] = False + _net_state["persistent"] = False + _net_state["autostart"] = False @staticmethod - def _net_start(name='unittest', **dargs): + def _net_start(name="unittest", **dargs): """Bogus net_start command""" - _net_state['active'] = True + _net_state["active"] = True @staticmethod - def _net_destroy(name='unittest', **dargs): + def _net_destroy(name="unittest", **dargs): """Bogus net_destroy command""" - _net_state['active'] = False + _net_state["active"] = False @staticmethod - def _net_autostart(name='unittest', extra='', **dargs): + def _net_autostart(name="unittest", extra="", **dargs): """Bogus net_autostart command""" - if _net_state['persistent']: - if extra == '--disable': - _net_state['autostart'] = False + if _net_state["persistent"]: + if extra == "--disable": + _net_state["autostart"] = False else: - _net_state['autostart'] = True + _net_state["autostart"] = True else: - _net_state['autostart'] = False + _net_state["autostart"] = False def setUp(self): # Use defined virsh methods below - self.bogus_virsh = FakeVirshFactory(preserve=['net_state_dict']) - self.bogus_virsh.__super_set__('net_list', self._net_list) - self.bogus_virsh.__super_set__('net_define', self._net_define) - self.bogus_virsh.__super_set__('net_undefine', self._net_undefine) - self.bogus_virsh.__super_set__('net_start', self._net_start) - self.bogus_virsh.__super_set__('net_destroy', self._net_destroy) - self.bogus_virsh.__super_set__('net_autostart', self._net_autostart) + self.bogus_virsh = FakeVirshFactory(preserve=["net_state_dict"]) + self.bogus_virsh.__super_set__("net_list", self._net_list) + self.bogus_virsh.__super_set__("net_define", self._net_define) + self.bogus_virsh.__super_set__("net_undefine", self._net_undefine) + self.bogus_virsh.__super_set__("net_start", self._net_start) + self.bogus_virsh.__super_set__("net_destroy", self._net_destroy) + self.bogus_virsh.__super_set__("net_autostart", self._net_autostart) class NetworkXMLTest(NetworkTestBase): @@ -120,36 +124,29 @@ def test_sync_and_state_dict(self): """ # Test sync without state option - test_xml = NetworkXML(network_name='unittest', - virsh_instance=self.bogus_virsh) + test_xml = NetworkXML(network_name="unittest", virsh_instance=self.bogus_virsh) test_xml.sync() new_state = test_xml.state_dict() - state = {'active': True, - 'persistent': True, - 'autostart': True} + state = {"active": True, "persistent": True, "autostart": True} self.assertEqual(state, new_state) for values in itertools.product([True, False], repeat=3): # Change network to all possible states. - keys = ['active', 'persistent', 'autostart'] + keys = ["active", "persistent", "autostart"] state = dict(zip(keys, values)) test_xml.sync(state=state) # Check result's validity. new_state = test_xml.state_dict() # Transient network can't set autostart - if state == {'active': True, - 'persistent': False, - 'autostart': True}: - state = {'active': True, - 'persistent': False, - 'autostart': False} + if state == {"active": True, "persistent": False, "autostart": True}: + state = {"active": True, "persistent": False, "autostart": False} # Non-exist network should return None when retieving state. - if not state['active'] and not state['persistent']: + if not state["active"] and not state["persistent"]: assert new_state is None else: self.assertEqual(state, new_state) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_libvirt_storage.py b/selftests/unit/test_libvirt_storage.py index 392f300ba0..edcfb0aad1 100644 --- a/selftests/unit/test_libvirt_storage.py +++ b/selftests/unit/test_libvirt_storage.py @@ -8,16 +8,18 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import libvirt_storage from test_virsh import FakeVirshFactory # The output of virsh.pool_list with only default pool -_DEFAULT_POOL = ("Name State Autostart\n" - "-----------------------------------------\n" - "default active yes \n") +_DEFAULT_POOL = ( + "Name State Autostart\n" + "-----------------------------------------\n" + "default active yes \n" +) # Set output of virsh.pool_list global _pools_output @@ -25,7 +27,6 @@ class PoolTestBase(unittest.TestCase): - @staticmethod def _pool_list(option="--all", **dargs): # Bogus output of virsh commands @@ -44,15 +45,17 @@ def _pool_info(name="default", **dargs): "Autostart: yes\n" "Capacity: 47.93 GiB\n" "Allocation: 36.74 GiB\n" - "Available: 11.20 GiB\n") + "Available: 11.20 GiB\n" + ) if name == "default": return CmdResult(cmd, default_output) else: return CmdResult(cmd) @staticmethod - def _pool_define_as(name="unittest", pool_type="dir", - target="/var/tmp", extra="", **dargs): + def _pool_define_as( + name="unittest", pool_type="dir", target="/var/tmp", extra="", **dargs + ): unittest_pool = "unittest inactive no\n" global _pools_output _pools_output = _DEFAULT_POOL + unittest_pool @@ -93,31 +96,28 @@ def setUp(self): # To avoid not installed libvirt packages self.bogus_virsh = FakeVirshFactory() # Use defined virsh methods needed in this unittest - self.bogus_virsh.__super_set__('pool_list', self._pool_list) - self.bogus_virsh.__super_set__('pool_info', self._pool_info) - self.bogus_virsh.__super_set__('pool_define_as', self._pool_define_as) - self.bogus_virsh.__super_set__('pool_build', self._pool_build) - self.bogus_virsh.__super_set__('pool_start', self._pool_start) - self.bogus_virsh.__super_set__('pool_destroy', self._pool_destroy) - self.bogus_virsh.__super_set__('pool_undefine', self._pool_undefine) - self.bogus_virsh.__super_set__('pool_autostart', self._pool_autostart) + self.bogus_virsh.__super_set__("pool_list", self._pool_list) + self.bogus_virsh.__super_set__("pool_info", self._pool_info) + self.bogus_virsh.__super_set__("pool_define_as", self._pool_define_as) + self.bogus_virsh.__super_set__("pool_build", self._pool_build) + self.bogus_virsh.__super_set__("pool_start", self._pool_start) + self.bogus_virsh.__super_set__("pool_destroy", self._pool_destroy) + self.bogus_virsh.__super_set__("pool_undefine", self._pool_undefine) + self.bogus_virsh.__super_set__("pool_autostart", self._pool_autostart) self.sp = libvirt_storage.StoragePool(virsh_instance=self.bogus_virsh) class ExistPoolTest(PoolTestBase): - def test_exist_pool(self): pools = self.sp.list_pools() assert isinstance(pools, dict) # Test pool_state - self.assertTrue( - self.sp.pool_state("default") in ['active', 'inactive']) + self.assertTrue(self.sp.pool_state("default") in ["active", "inactive"]) # Test pool_info self.assertNotEqual(self.sp.pool_info("default"), {}) class NewPoolTest(PoolTestBase): - def test_dir_pool(self): # Used for auto cleanup self.pool_name = "unittest" @@ -134,7 +134,6 @@ def tearDown(self): class NotExpectedPoolTest(PoolTestBase): - def test_not_exist_pool(self): self.assertFalse(self.sp.pool_exists("NOTEXISTPOOL")) assert self.sp.pool_state("NOTEXISTPOOL") is None diff --git a/selftests/unit/test_libvirt_xml.py b/selftests/unit/test_libvirt_xml.py index 59cf2e0bd5..ab897978c0 100644 --- a/selftests/unit/test_libvirt_xml.py +++ b/selftests/unit/test_libvirt_xml.py @@ -19,7 +19,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import xml_utils, utils_misc, data_dir @@ -57,77 +57,72 @@ class LibvirtXMLTestBase(unittest.TestCase): # domain_xml # usage: # xml = __domain_xml__ % (name, uuid) - __domain_xml__ = ('' - ' %s' - ' %s' - ' ' - ' ' - ' ' - ' ' - ' ' - ' ' - ' 2048' - ' 1000000' - ' -1' - ' 1000000' - ' -1' - ' ' - ' ' # Tests below depend on device order - - ' ' - ' ' - ' ' - ' ' - ' ' - ' ' - ' ' - ' ' - ' ' + " %s" + " %s" + " " + ' ' + ' ' + ' ' + ' ' + ' ' + " 2048" + " 1000000" + " -1" + " 1000000" + " -1" + " " + " " # Tests below depend on device order + ' ' + ' ' + " " + ' ' + ' ' + ' ' + " " + ' ' + ' ' - ' ' - ' ' - ' ' - ' ' - ' ' + ' ' + " " + ' ' + ' ' - ' ' - ' ' - ' ' - - ' ' - ' ' - ' ' - ' ' - ' ' - ' ' - ' ' - ' ' - - ' ' - - ' ' - ' ' - ' ' - ' ' - '
' - ' ' - - ' ' - - ' ' - ' ' - ' sec_baselabel' - ' sec_imagelabel' - ' ' - - '') + ' ' + " " + ' ' + ' ' + ' ' + " " + ' ' + ' ' + ' ' + " " + ' ' + ' ' + ' ' + ' ' + ' ' + '
' + " " + " " + ' ' + " " + " sec_baselabel" + " sec_imagelabel" + " " + "" + ) __doms_dir__ = None @staticmethod - def _capabilities(option='', **dargs): + def _capabilities(option="", **dargs): # Compacted to save space return CAPABILITIES @@ -138,9 +133,8 @@ def _domuuid(name, **dargs): @staticmethod def _define(file_path, **dargs): vmxml = xml_utils.XMLTreeFile(file_path) - dom_name = vmxml.find('name').text - xml_path = os.path.join(LibvirtXMLTestBase.__doms_dir__, - '%s.xml' % dom_name) + dom_name = vmxml.find("name").text + xml_path = os.path.join(LibvirtXMLTestBase.__doms_dir__, "%s.xml" % dom_name) shutil.copy(file_path, xml_path) @staticmethod @@ -154,18 +148,20 @@ def _dumpxml(name, to_file="", **dargs): stderr = stdout exit_status = 1 result = process.CmdResult(cmd, stdout, stderr, exit_status) - raise process.CmdError(cmd, result, - "Virsh Command returned non-zero exit status") + raise process.CmdError( + cmd, result, "Virsh Command returned non-zero exit status" + ) - file_path = os.path.join(LibvirtXMLTestBase.__doms_dir__, - '%s.xml' % name) + file_path = os.path.join(LibvirtXMLTestBase.__doms_dir__, "%s.xml" % name) if os.path.exists(file_path): - xml_file = open(file_path, 'r') + xml_file = open(file_path, "r") domain_xml = xml_file.read() else: - xml_file = open(file_path, 'w') - domain_xml = LibvirtXMLTestBase.__domain_xml__ % (name, - LibvirtXMLTestBase._domuuid(None)) + xml_file = open(file_path, "w") + domain_xml = LibvirtXMLTestBase.__domain_xml__ % ( + name, + LibvirtXMLTestBase._domuuid(None), + ) xml_file.write(domain_xml) xml_file.close() @@ -179,45 +175,49 @@ def _dumpxml(name, to_file="", **dargs): def _nodedev_dumpxml(name, options="", to_file=None, **dargs): # Must mirror virsh.nodedev_dumpxml() API but can't test this option if options != "": - raise ValueError('Dummy virsh for testing does not support options' - ' parameter') + raise ValueError( + "Dummy virsh for testing does not support options" " parameter" + ) if to_file is not None: - raise ValueError('Dummy virsh for testing does not support to_file' - ' parameter') - if name is not 'pci_0000_00_00_0': - raise ValueError('Dummy virsh for testing only support ' - ' device name pci_0000_00_00_0') - xml = ("" - "pci_0000_00_00_0" - "/sys/devices/pci0000:00/0000:00:00.0" - "computer" - "" - "0" - "0" - "0" - "0" - "5000X Chipset Memory Controller Hub" - "Intel Corporation" - "" - "") - return process.CmdResult('virsh nodedev-dumpxml pci_0000_00_00_0', - xml, '', 0) + raise ValueError( + "Dummy virsh for testing does not support to_file" " parameter" + ) + if name is not "pci_0000_00_00_0": + raise ValueError( + "Dummy virsh for testing only support " " device name pci_0000_00_00_0" + ) + xml = ( + "" + "pci_0000_00_00_0" + "/sys/devices/pci0000:00/0000:00:00.0" + "computer" + "" + "0" + "0" + "0" + "0" + "5000X Chipset Memory Controller Hub" + "Intel Corporation" + "" + "" + ) + return process.CmdResult("virsh nodedev-dumpxml pci_0000_00_00_0", xml, "", 0) def setUp(self): self.dummy_virsh = FakeVirshFactory() # make a tmp_dir to store information. - LibvirtXMLTestBase.__doms_dir__ = os.path.join(data_dir.get_tmp_dir(), - 'domains') + LibvirtXMLTestBase.__doms_dir__ = os.path.join( + data_dir.get_tmp_dir(), "domains" + ) if not os.path.isdir(LibvirtXMLTestBase.__doms_dir__): os.makedirs(LibvirtXMLTestBase.__doms_dir__) # Normally not kosher to call super_set, but required here for testing - self.dummy_virsh.__super_set__('capabilities', self._capabilities) - self.dummy_virsh.__super_set__('dumpxml', self._dumpxml) - self.dummy_virsh.__super_set__('domuuid', self._domuuid) - self.dummy_virsh.__super_set__('define', self._define) - self.dummy_virsh.__super_set__( - 'nodedev_dumpxml', self._nodedev_dumpxml) + self.dummy_virsh.__super_set__("capabilities", self._capabilities) + self.dummy_virsh.__super_set__("dumpxml", self._dumpxml) + self.dummy_virsh.__super_set__("domuuid", self._domuuid) + self.dummy_virsh.__super_set__("define", self._define) + self.dummy_virsh.__super_set__("nodedev_dumpxml", self._nodedev_dumpxml) def tearDown(self): librarian.DEVICE_TYPES = list(ORIGINAL_DEVICE_TYPES) @@ -227,11 +227,10 @@ def tearDown(self): # AccessorsTest.test_XMLElementNest is namespace sensitive class Baz(base.LibvirtXMLBase): - __slots__ = ('foobar',) + __slots__ = ("foobar",) def __init__(self, parent, virsh_instance): - accessors.XMLElementText('foobar', self, ['set', 'del'], - '/', 'baz') + accessors.XMLElementText("foobar", self, ["set", "del"], "/", "baz") super(Baz, self).__init__(virsh_instance=virsh_instance) # must setup some basic XML inside for this element self.xml = """""" @@ -239,48 +238,44 @@ def __init__(self, parent, virsh_instance): parent.assertTrue(isinstance(parent, AccessorsTest)) parent.assertTrue(self.foobar is None) # Forbidden should still catch these - parent.assertRaises(xcepts.LibvirtXMLForbiddenError, - self.__setattr__, 'foobar', None) - parent.assertRaises(xcepts.LibvirtXMLForbiddenError, - self.__setitem__, 'foobar', None) - parent.assertRaises(xcepts.LibvirtXMLForbiddenError, - self.__delattr__, 'foobar') - parent.assertRaises(xcepts.LibvirtXMLForbiddenError, - self.__delitem__, 'foobar') + parent.assertRaises( + xcepts.LibvirtXMLForbiddenError, self.__setattr__, "foobar", None + ) + parent.assertRaises( + xcepts.LibvirtXMLForbiddenError, self.__setitem__, "foobar", None + ) + parent.assertRaises(xcepts.LibvirtXMLForbiddenError, self.__delattr__, "foobar") + parent.assertRaises(xcepts.LibvirtXMLForbiddenError, self.__delitem__, "foobar") # AccessorsTest.test_XMLElementNest is namespace sensitive class Bar(base.LibvirtXMLBase): - __slots__ = ('baz',) + __slots__ = ("baz",) def __init__(self, parent, virsh_instance): - subclass_dargs = {'parent': parent, - 'virsh_instance': virsh_instance} - accessors.XMLElementNest('baz', self, None, - '/', 'baz', Baz, subclass_dargs) + subclass_dargs = {"parent": parent, "virsh_instance": virsh_instance} + accessors.XMLElementNest("baz", self, None, "/", "baz", Baz, subclass_dargs) super(Bar, self).__init__(virsh_instance=virsh_instance) # must setup some basic XML inside for this element self.xml = """""" parent.assertTrue(isinstance(parent, AccessorsTest)) - parent.assertRaises(xcepts.LibvirtXMLNotFoundError, - self.__getattr__, 'baz') - parent.assertRaises(xcepts.LibvirtXMLNotFoundError, - self.__getitem__, 'baz') + parent.assertRaises(xcepts.LibvirtXMLNotFoundError, self.__getattr__, "baz") + parent.assertRaises(xcepts.LibvirtXMLNotFoundError, self.__getitem__, "baz") # Built-in type-checking - parent.assertRaises(ValueError, self.__setattr__, - 'baz', True) # bool() is not a Bar() - parent.assertRaises(ValueError, self.__setitem__, - 'baz', None) + parent.assertRaises( + ValueError, self.__setattr__, "baz", True + ) # bool() is not a Bar() + parent.assertRaises(ValueError, self.__setitem__, "baz", None) class AccessorsTest(LibvirtXMLTestBase): - def test_type_check(self): class bar(object): pass class foo(bar): pass + # Save some typing type_check = accessors.type_check foobar = foo() @@ -293,134 +288,133 @@ class foo(bar): def test_required_slots(self): class Foo(accessors.AccessorGeneratorBase): - class Getter(accessors.AccessorBase): - __slots__ = accessors.add_to_slots('foo', 'bar') + __slots__ = accessors.add_to_slots("foo", "bar") pass + lvx = base.LibvirtXMLBase(self.dummy_virsh) - forbidden = ['set', 'del'] - self.assertRaises(ValueError, Foo, 'foobar', lvx, forbidden) - self.assertRaises(ValueError, Foo, 'foobar', lvx, forbidden, foo='') - self.assertRaises(ValueError, Foo, 'foobar', lvx, forbidden, bar='') + forbidden = ["set", "del"] + self.assertRaises(ValueError, Foo, "foobar", lvx, forbidden) + self.assertRaises(ValueError, Foo, "foobar", lvx, forbidden, foo="") + self.assertRaises(ValueError, Foo, "foobar", lvx, forbidden, bar="") def test_accessor_base(self): class ABSubclass(accessors.AccessorBase): pass + lvx = base.LibvirtXMLBase(self.dummy_virsh) # operation attribute check should fail - self.assertRaises(ValueError, accessors.AccessorBase, - 'foobar', lvx, lvx) - abinst = ABSubclass('Getter', 'foobar', lvx) - self.assertEqual(abinst.property_name, 'foobar') + self.assertRaises(ValueError, accessors.AccessorBase, "foobar", lvx, lvx) + abinst = ABSubclass("Getter", "foobar", lvx) + self.assertEqual(abinst.property_name, "foobar") # test call to get_libvirtxml() accessor self.assertEqual(abinst.libvirtxml, lvx) def test_XMLElementInt(self): class FooBar(base.LibvirtXMLBase): - __slots__ = ('auto_test', - 'bin_test', - 'oct_test', - 'dec_test', - 'hex_test') + __slots__ = ("auto_test", "bin_test", "oct_test", "dec_test", "hex_test") + lvx = FooBar(self.dummy_virsh) - lvx.xml = ('' - ' 00' - ' 10' - ' 10' - ' 10' - ' 10' - '') - - name_radix = {'auto': 0, 'bin': 2, 'oct': 8, 'dec': 10, 'hex': 16} + lvx.xml = ( + "" + " 00" + " 10" + " 10" + " 10" + " 10" + "" + ) + + name_radix = {"auto": 0, "bin": 2, "oct": 8, "dec": 10, "hex": 16} for name, radix in list(name_radix.items()): - accessors.XMLElementInt(name + '_test', lvx, - parent_xpath='/', - tag_name=name, - radix=radix) - self.assertEqual(lvx[name + '_test'], radix) + accessors.XMLElementInt( + name + "_test", lvx, parent_xpath="/", tag_name=name, radix=radix + ) + self.assertEqual(lvx[name + "_test"], radix) - self.assertRaises(ValueError, - lvx.__setitem__, 'bin_test', 'text') + self.assertRaises(ValueError, lvx.__setitem__, "bin_test", "text") def test_AllForbidden(self): class FooBar(base.LibvirtXMLBase): - __slots__ = ('test',) + __slots__ = ("test",) + lvx = FooBar(self.dummy_virsh) - accessors.AllForbidden('test', lvx) - self.assertRaises(xcepts.LibvirtXMLForbiddenError, - lvx.__getitem__, 'test') - self.assertRaises(xcepts.LibvirtXMLForbiddenError, - lvx.__setitem__, 'test', 'foobar') - self.assertRaises(xcepts.LibvirtXMLForbiddenError, - lvx.__delitem__, 'test') + accessors.AllForbidden("test", lvx) + self.assertRaises(xcepts.LibvirtXMLForbiddenError, lvx.__getitem__, "test") + self.assertRaises( + xcepts.LibvirtXMLForbiddenError, lvx.__setitem__, "test", "foobar" + ) + self.assertRaises(xcepts.LibvirtXMLForbiddenError, lvx.__delitem__, "test") def test_not_enuf_dargs(self): class FooBar(base.LibvirtXMLBase): - __slots__ = ('test',) + __slots__ = ("test",) + foobar = FooBar(self.dummy_virsh) - self.assertRaises(ValueError, - accessors.XMLElementText, 'test', - foobar, '/') - self.assertRaises(TypeError, - accessors.XMLElementText) - self.assertRaises(TypeError, - accessors.XMLElementText, 'test') + self.assertRaises(ValueError, accessors.XMLElementText, "test", foobar, "/") + self.assertRaises(TypeError, accessors.XMLElementText) + self.assertRaises(TypeError, accessors.XMLElementText, "test") def test_too_many_dargs(self): class FooBar(base.LibvirtXMLBase): - __slots__ = ('test',) + __slots__ = ("test",) + foobar = FooBar(self.dummy_virsh) - self.assertRaises(ValueError, - accessors.XMLElementText, 'test', - foobar, '/', 'foobar') - self.assertRaises(ValueError, - accessors.XMLElementText, 'test', - None, None, None, None) + self.assertRaises( + ValueError, accessors.XMLElementText, "test", foobar, "/", "foobar" + ) + self.assertRaises( + ValueError, accessors.XMLElementText, "test", None, None, None, None + ) def test_create_by_xpath(self): class FooBar(base.LibvirtXMLBase): - __slots__ = ('test',) + __slots__ = ("test",) def __init__(self, virsh_instance): # pylint: disable=E1003 super(FooBar, self).__init__(virsh_instance) - accessors.XMLElementDict('test', self, None, 'foo/bar', 'baz') + accessors.XMLElementDict("test", self, None, "foo/bar", "baz") + foobar = FooBar(self.dummy_virsh) - foobar.xml = '' - test_dict = {'test1': '1', 'test2': '2'} + foobar.xml = "" + test_dict = {"test1": "1", "test2": "2"} foobar.test = test_dict self.assertEqual(foobar.test, test_dict) - element = foobar.xmltreefile.find('foo/bar/baz') + element = foobar.xmltreefile.find("foo/bar/baz") self.assertTrue(element is not None) element_dict = dict(list(element.items())) self.assertEqual(test_dict, element_dict) def test_XMLElementNest(self): class Foo(base.LibvirtXMLBase): - __slots__ = ('bar',) + __slots__ = ("bar",) def __init__(self, parent, virsh_instance): - subclass_dargs = {'parent': parent, - 'virsh_instance': virsh_instance} - accessors.XMLElementNest('bar', self, None, - '/', 'bar', Bar, subclass_dargs) + subclass_dargs = {"parent": parent, "virsh_instance": virsh_instance} + accessors.XMLElementNest( + "bar", self, None, "/", "bar", Bar, subclass_dargs + ) # pylint: disable=E1003 super(Foo, self).__init__(virsh_instance=virsh_instance) parent.assertTrue(isinstance(parent, AccessorsTest)) - self.set_xml(""" + self.set_xml( + """ foobar - """) + """ + ) parent.assertTrue(isinstance(self.bar, Bar)) parent.assertTrue(isinstance(self.bar.baz, Baz)) - parent.assertEqual(self.bar.baz.foobar, 'foobar') - parent.assertRaises(ValueError, self.bar.__setattr__, - 'baz', Bar) # Baz is not a Bar() + parent.assertEqual(self.bar.baz.foobar, "foobar") + parent.assertRaises( + ValueError, self.bar.__setattr__, "baz", Bar + ) # Baz is not a Bar() foo = Foo(parent=self, virsh_instance=self.dummy_virsh) - self.assertEqual(foo.bar.baz.foobar, 'foobar') + self.assertEqual(foo.bar.baz.foobar, "foobar") baz = Baz(parent=self, virsh_instance=self.dummy_virsh) # setting foobar is forbidden, have to go the long way around baz.xml = """test value""" @@ -428,51 +422,54 @@ def __init__(self, parent, virsh_instance): bar.baz = baz foo.bar = bar # Point at new Bar instance # TODO: Make 'foo.bar.baz = baz' work - self.assertEqual(foo.bar.baz.foobar, 'test value') + self.assertEqual(foo.bar.baz.foobar, "test value") self.assertTrue(isinstance(foo.bar.baz, Baz)) def test_XMLElementBool_simple(self): class Foo(base.LibvirtXMLBase): - __slots__ = ('bar', 'baz') + __slots__ = ("bar", "baz") def __init__(self, virsh_instance): - accessors.XMLElementBool('bar', self, - parent_xpath='/', tag_name='bar') - accessors.XMLElementBool('baz', self, - parent_xpath='/', tag_name='bar') + accessors.XMLElementBool("bar", self, parent_xpath="/", tag_name="bar") + accessors.XMLElementBool("baz", self, parent_xpath="/", tag_name="bar") # pylint: disable=E1003 super(Foo, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" + foo = Foo(self.dummy_virsh) self.assertFalse(foo.bar) - self.assertFalse(foo['bar']) + self.assertFalse(foo["bar"]) self.assertFalse(foo.baz) - self.assertFalse(foo['baz']) + self.assertFalse(foo["baz"]) foo.bar = True self.assertTrue(foo.bar) - self.assertTrue(foo['bar']) + self.assertTrue(foo["bar"]) self.assertTrue(foo.baz) - self.assertTrue(foo['baz']) + self.assertTrue(foo["baz"]) del foo.baz self.assertFalse(foo.bar) - self.assertFalse(foo['bar']) + self.assertFalse(foo["bar"]) self.assertFalse(foo.baz) - self.assertFalse(foo['baz']) + self.assertFalse(foo["baz"]) def test_XMLElementBool_deep(self): class Foo(base.LibvirtXMLBase): - __slots__ = ('bar', 'baz', 'foo') + __slots__ = ("bar", "baz", "foo") def __init__(self, virsh_instance): - accessors.XMLElementBool('foo', self, - parent_xpath='/l1', tag_name='foo') - accessors.XMLElementBool('bar', self, - parent_xpath='/l1/l2/l3', tag_name='bar') - accessors.XMLElementBool('baz', self, - parent_xpath='/l1/l2', tag_name='baz') + accessors.XMLElementBool( + "foo", self, parent_xpath="/l1", tag_name="foo" + ) + accessors.XMLElementBool( + "bar", self, parent_xpath="/l1/l2/l3", tag_name="bar" + ) + accessors.XMLElementBool( + "baz", self, parent_xpath="/l1/l2", tag_name="baz" + ) # pylint: disable=E1003 super(Foo, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" + foo = Foo(self.dummy_virsh) for attr in "foo bar baz".split(): self.assertFalse(getattr(foo, attr)) @@ -494,7 +491,6 @@ def __init__(self, virsh_instance): def test_XMLElementList(self): class Whatchamacallit(object): - def __init__(self, secret_sauce): self.secret_sauce = str(secret_sauce) @@ -502,23 +498,26 @@ def __init__(self, secret_sauce): def from_it(item, index, lvxml): if not isinstance(item, Whatchamacallit): raise ValueError - return ('whatchamacallit', - {'secret_sauce': str(item.secret_sauce)}) + return ("whatchamacallit", {"secret_sauce": str(item.secret_sauce)}) @staticmethod def to_it(tag, attrs, index, lvxml): - if not tag.startswith('whatchamacallit'): + if not tag.startswith("whatchamacallit"): return None else: - return Whatchamacallit(attrs.get('secret_sauce')) + return Whatchamacallit(attrs.get("secret_sauce")) class Foo(base.LibvirtXMLBase): - __slots__ = ('bar',) + __slots__ = ("bar",) def __init__(self, virsh_instance): - accessors.XMLElementList('bar', self, parent_xpath='/bar', - marshal_from=Whatchamacallit.from_it, - marshal_to=Whatchamacallit.to_it) + accessors.XMLElementList( + "bar", + self, + parent_xpath="/bar", + marshal_from=Whatchamacallit.from_it, + marshal_to=Whatchamacallit.to_it, + ) # pylint: disable=E1003 super(Foo, self).__init__(virsh_instance=virsh_instance) self.xml = """ @@ -526,43 +525,48 @@ def __init__(self, virsh_instance): """ + foo = Foo(virsh_instance=self.dummy_virsh) existing = foo.bar self.assertEqual(len(existing), 2) - self.assertEqual(existing[0].secret_sauce, 'foobar') - self.assertEqual(existing[1].secret_sauce, '5') + self.assertEqual(existing[0].secret_sauce, "foobar") + self.assertEqual(existing[1].secret_sauce, "5") foo.bar = existing existing[0].secret_sauce = existing[1].secret_sauce = None # No value change - self.assertEqual(foo.bar[0].secret_sauce, 'foobar') - self.assertEqual(foo.bar[1].secret_sauce, '5') - existing.append(Whatchamacallit('None')) + self.assertEqual(foo.bar[0].secret_sauce, "foobar") + self.assertEqual(foo.bar[1].secret_sauce, "5") + existing.append(Whatchamacallit("None")) foo.bar = existing # values changed test = foo.bar - self.assertEqual(test[0].secret_sauce, 'None') - self.assertEqual(test[1].secret_sauce, 'None') - self.assertEqual(test[2].secret_sauce, 'None') + self.assertEqual(test[0].secret_sauce, "None") + self.assertEqual(test[1].secret_sauce, "None") + self.assertEqual(test[2].secret_sauce, "None") def test_XMLElementList_Text(self): class Whatchamacallit(base.LibvirtXMLBase): - __slots__ = ('text',) + __slots__ = ("text",) def __init__(self, virsh_instance, text=None): - accessors.XMLElementText('text', self, parent_xpath='/', - tag_name='whatchamacallit') + accessors.XMLElementText( + "text", self, parent_xpath="/", tag_name="whatchamacallit" + ) # pylint: disable=E1003 - super(Whatchamacallit, self).__init__( - virsh_instance=virsh_instance) + super(Whatchamacallit, self).__init__(virsh_instance=virsh_instance) self.xml = "" self.text = str(text) class Foo(base.LibvirtXMLBase): - __slots__ = ('bar',) + __slots__ = ("bar",) def __init__(self, virsh_instance): - accessors.XMLElementList('bar', self, parent_xpath='/bar', - marshal_from=self.from_it, - marshal_to=self.to_it) + accessors.XMLElementList( + "bar", + self, + parent_xpath="/bar", + marshal_from=self.from_it, + marshal_to=self.to_it, + ) # pylint: disable=E1003 super(Foo, self).__init__(virsh_instance=virsh_instance) self.xml = """ @@ -575,12 +579,11 @@ def __init__(self, virsh_instance): def from_it(item, index, lvxml): if not isinstance(item, Whatchamacallit): raise ValueError - return ('whatchamacallit', - {}, item.text) + return ("whatchamacallit", {}, item.text) @staticmethod def to_it(tag, attrs, index, lvxml, text): - if not tag.startswith('whatchamacallit'): + if not tag.startswith("whatchamacallit"): return None else: return Whatchamacallit(lvxml.virsh, text) @@ -588,23 +591,22 @@ def to_it(tag, attrs, index, lvxml, text): foo = Foo(virsh_instance=self.dummy_virsh) existing = foo.bar self.assertEqual(len(existing), 2) - self.assertEqual(existing[0].text, 'test_text1') - self.assertEqual(existing[1].text, 'test_text2') + self.assertEqual(existing[0].text, "test_text1") + self.assertEqual(existing[1].text, "test_text2") foo.bar = existing existing[0].text = existing[1].text = None # No value change - self.assertEqual(foo.bar[0].text, 'test_text1') - self.assertEqual(foo.bar[1].text, 'test_text2') - existing.append(Whatchamacallit(self.dummy_virsh, 'None')) + self.assertEqual(foo.bar[0].text, "test_text1") + self.assertEqual(foo.bar[1].text, "test_text2") + existing.append(Whatchamacallit(self.dummy_virsh, "None")) foo.bar = existing # values changed test = foo.bar - self.assertEqual(test[0].text, 'None') - self.assertEqual(test[1].text, 'None') - self.assertEqual(test[2].text, 'None') + self.assertEqual(test[0].text, "None") + self.assertEqual(test[1].text, "None") + self.assertEqual(test[2].text, "None") class TestLibvirtXML(LibvirtXMLTestBase): - def _from_scratch(self): return capability_xml.CapabilityXML(virsh_instance=self.dummy_virsh) @@ -612,80 +614,80 @@ def test_uuid(self): lvxml = self._from_scratch() test_uuid = lvxml.uuid self.assertEqual(test_uuid, UUID) - test_uuid = lvxml['uuid'] + test_uuid = lvxml["uuid"] self.assertEqual(test_uuid, UUID) - self.assertRaises(xcepts.LibvirtXMLForbiddenError, - lvxml.__setattr__, - 'uuid', 'foobar') - self.assertRaises(xcepts.LibvirtXMLForbiddenError, - lvxml.__delitem__, - 'uuid') + self.assertRaises( + xcepts.LibvirtXMLForbiddenError, lvxml.__setattr__, "uuid", "foobar" + ) + self.assertRaises(xcepts.LibvirtXMLForbiddenError, lvxml.__delitem__, "uuid") def test_guest_capabilities(self): lvxml = self._from_scratch() - expected_os = 'hvm' - expected_arch = 'x86_64' - expected_guest = {'wordsize': '64', - 'emulator': '/usr/libexec/qemu-kvm', - 'machine': ['rhel6.3.0', 'pc'], - 'maxcpus': '255', - 'domain_qemu': {}, - 'domain_kvm': {'emulator': '/usr/libexec/qemu-kvm'}} + expected_os = "hvm" + expected_arch = "x86_64" + expected_guest = { + "wordsize": "64", + "emulator": "/usr/libexec/qemu-kvm", + "machine": ["rhel6.3.0", "pc"], + "maxcpus": "255", + "domain_qemu": {}, + "domain_kvm": {"emulator": "/usr/libexec/qemu-kvm"}, + } expected = {expected_os: {expected_arch: expected_guest}} test_guest_capa = lvxml.guest_capabilities self.assertEqual(test_guest_capa, expected) - test_guest_capa = lvxml['guest_capabilities'] + test_guest_capa = lvxml["guest_capabilities"] self.assertEqual(test_guest_capa, expected) - self.assertRaises(xcepts.LibvirtXMLForbiddenError, - lvxml.__setattr__, - 'guest_capabilities', 'foobar') - self.assertRaises(xcepts.LibvirtXMLForbiddenError, - lvxml.__delitem__, - 'guest_capabilities') + self.assertRaises( + xcepts.LibvirtXMLForbiddenError, + lvxml.__setattr__, + "guest_capabilities", + "foobar", + ) + self.assertRaises( + xcepts.LibvirtXMLForbiddenError, lvxml.__delitem__, "guest_capabilities" + ) class TestVMXML(LibvirtXMLTestBase): - def _from_scratch(self): - vmxml = vm_xml.VMXML('test1', virsh_instance=self.dummy_virsh) - vmxml.vm_name = 'test2' - vmxml.uuid = 'test3' + vmxml = vm_xml.VMXML("test1", virsh_instance=self.dummy_virsh) + vmxml.vm_name = "test2" + vmxml.uuid = "test3" vmxml.vcpu = 4 return vmxml def test_getters(self): vmxml = self._from_scratch() - self.assertEqual(vmxml.hypervisor_type, 'test1') - self.assertEqual(vmxml.vm_name, 'test2') - self.assertEqual(vmxml.uuid, 'test3') + self.assertEqual(vmxml.hypervisor_type, "test1") + self.assertEqual(vmxml.vm_name, "test2") + self.assertEqual(vmxml.uuid, "test3") self.assertEqual(vmxml.vcpu, 4) def test_valid_xml(self): vmxml = self._from_scratch() test_xtf = xml_utils.XMLTreeFile(vmxml.xml) # re-parse from filename - self.assertEqual(test_xtf.getroot().get('type'), 'test1') - self.assertEqual(test_xtf.find('name').text, 'test2') - self.assertEqual(test_xtf.find('uuid').text, 'test3') - self.assertEqual(test_xtf.find('vcpu').text, '4') + self.assertEqual(test_xtf.getroot().get("type"), "test1") + self.assertEqual(test_xtf.find("name").text, "test2") + self.assertEqual(test_xtf.find("uuid").text, "test3") + self.assertEqual(test_xtf.find("vcpu").text, "4") def test_new_from_dumpxml(self): - vmxml = vm_xml.VMXML.new_from_dumpxml('foobar', - virsh_instance=self.dummy_virsh) - self.assertEqual(vmxml.vm_name, 'foobar') + vmxml = vm_xml.VMXML.new_from_dumpxml("foobar", virsh_instance=self.dummy_virsh) + self.assertEqual(vmxml.vm_name, "foobar") self.assertEqual(vmxml.uuid, self._domuuid(None)) - self.assertEqual(vmxml.hypervisor_type, 'kvm') + self.assertEqual(vmxml.hypervisor_type, "kvm") def test_restore(self): - vmxml = vm_xml.VMXML.new_from_dumpxml('foobar', - virsh_instance=self.dummy_virsh) - vmxml.vm_name = 'test name' - vmxml.uuid = 'test uuid' - vmxml.hypervisor_type = 'atari' + vmxml = vm_xml.VMXML.new_from_dumpxml("foobar", virsh_instance=self.dummy_virsh) + vmxml.vm_name = "test name" + vmxml.uuid = "test uuid" + vmxml.hypervisor_type = "atari" # Changes verified in test_new_from_dumpxml() above vmxml.restore() - self.assertEqual(vmxml.vm_name, 'foobar') + self.assertEqual(vmxml.vm_name, "foobar") self.assertEqual(vmxml.uuid, self._domuuid(None)) - self.assertEqual(vmxml.hypervisor_type, 'kvm') + self.assertEqual(vmxml.hypervisor_type, "kvm") def test_seclabel(self): vmxml = self._from_scratch() @@ -693,17 +695,20 @@ def test_seclabel(self): # should not raise an exception del vmxml.seclabel - self.assertRaises(xcepts.LibvirtXMLError, - getattr, vmxml, 'seclabel') + self.assertRaises(xcepts.LibvirtXMLError, getattr, vmxml, "seclabel") - vmxml.set_seclabel([{'type': "dynamic"}]) - self.assertEqual(vmxml.seclabel[0]['type'], 'dynamic') + vmxml.set_seclabel([{"type": "dynamic"}]) + self.assertEqual(vmxml.seclabel[0]["type"], "dynamic") self.assertEqual(len(vmxml.seclabel[0]), 1) - seclabel_dict = {'type': 'test_type', 'model': 'test_model', - 'relabel': 'test_relabel', 'label': 'test_label', - 'baselabel': 'test_baselabel', - 'imagelabel': 'test_imagelabel'} + seclabel_dict = { + "type": "test_type", + "model": "test_model", + "relabel": "test_relabel", + "label": "test_label", + "baselabel": "test_baselabel", + "imagelabel": "test_imagelabel", + } vmxml.set_seclabel([seclabel_dict]) seclabel = vmxml.get_seclabel()[0] @@ -717,72 +722,68 @@ def test_seclabel(self): class testNetworkXML(LibvirtXMLTestBase): - def _from_scratch(self): - netxml = network_xml.NetworkXML(network_name='test0', - virsh_instance=self.dummy_virsh) - self.assertEqual(netxml.name, 'test0') - netxml.name = 'test1' - netxml.uuid = 'test2' - netxml.bridge = {'test3': 'test4'} + netxml = network_xml.NetworkXML( + network_name="test0", virsh_instance=self.dummy_virsh + ) + self.assertEqual(netxml.name, "test0") + netxml.name = "test1" + netxml.uuid = "test2" + netxml.bridge = {"test3": "test4"} ipxml = network_xml.IPXML() - ipxml.address = ('address_test') - ipxml.netmask = ('netmask_test') + ipxml.address = "address_test" + ipxml.netmask = "netmask_test" netxml.ips = [ipxml] return netxml def test_getters(self): netxml = self._from_scratch() - self.assertEqual(netxml.name, 'test1') - self.assertEqual(netxml.uuid, 'test2') - self.assertEqual(netxml.bridge, {'test3': 'test4'}) + self.assertEqual(netxml.name, "test1") + self.assertEqual(netxml.uuid, "test2") + self.assertEqual(netxml.bridge, {"test3": "test4"}) def test_valid_xml(self): netxml = self._from_scratch() test_xtf = xml_utils.XMLTreeFile(netxml.xml) # re-parse from filename - self.assertEqual(test_xtf.find('name').text, 'test1') - self.assertEqual(test_xtf.find('uuid').text, 'test2') - self.assertEqual(test_xtf.find('bridge').get('test3'), 'test4') + self.assertEqual(test_xtf.find("name").text, "test1") + self.assertEqual(test_xtf.find("uuid").text, "test2") + self.assertEqual(test_xtf.find("bridge").get("test3"), "test4") def test_ip_getter(self): netxml = self._from_scratch() ipxml = netxml.ips[0] - self.assertEqual(ipxml.address, 'address_test') - self.assertEqual(ipxml.netmask, 'netmask_test') + self.assertEqual(ipxml.address, "address_test") + self.assertEqual(ipxml.netmask, "netmask_test") class testLibrarian(LibvirtXMLTestBase): - def test_bad_names(self): - for badname in ('__init__', 'librarian', '__doc__', '/dev/null', '', - None): + for badname in ("__init__", "librarian", "__doc__", "/dev/null", "", None): self.assertRaises(xcepts.LibvirtXMLError, librarian.get, badname) def test_no_module(self): # Bypass type-check to induse module load failure original_device_types = librarian.DEVICE_TYPES - for badname in ('DoesNotExist', '/dev/null', '', None): + for badname in ("DoesNotExist", "/dev/null", "", None): librarian.DEVICE_TYPES.append(badname) - self.assertRaises(xcepts.LibvirtXMLError, librarian.get, - badname) + self.assertRaises(xcepts.LibvirtXMLError, librarian.get, badname) def test_serial_class(self): - Serial = librarian.get('serial') + Serial = librarian.get("serial") self.assertTrue(issubclass(Serial, devices_base.UntypedDeviceBase)) self.assertTrue(issubclass(Serial, devices_base.TypedDeviceBase)) class testStubXML(LibvirtXMLTestBase): - @six.add_metaclass(devices_base.StubDeviceMeta) class UntypedFoobar(devices_base.UntypedDeviceBase): - _device_tag = 'foobar' + _device_tag = "foobar" @six.add_metaclass(devices_base.StubDeviceMeta) class TypedFoobar(devices_base.TypedDeviceBase): - _device_tag = 'foo' - _def_type_name = 'bar' + _device_tag = "foo" + _def_type_name = "bar" def setUp(self): logging.disable(logging.WARNING) @@ -790,29 +791,33 @@ def setUp(self): def test_untyped_device_stub(self): foobar = self.UntypedFoobar(virsh_instance=self.dummy_virsh) - self.assertEqual(foobar.virsh.domuuid(None), - "ddb0cf86-5ba8-4f83-480a-d96f54339219") - self.assertEqual(foobar.device_tag, 'foobar') - self.assertEqual(unicode(foobar), - u"\n") + self.assertEqual( + foobar.virsh.domuuid(None), "ddb0cf86-5ba8-4f83-480a-d96f54339219" + ) + self.assertEqual(foobar.device_tag, "foobar") + self.assertEqual( + unicode(foobar), "\n" + ) def test_typed_device_stub(self): foobar = self.TypedFoobar(virsh_instance=self.dummy_virsh) - self.assertEqual(foobar.virsh.domuuid(None), - "ddb0cf86-5ba8-4f83-480a-d96f54339219") - self.assertEqual(foobar.device_tag, 'foo') - self.assertEqual(foobar.type_name, 'bar') - self.assertEqual(unicode(foobar), - u'\n') + self.assertEqual( + foobar.virsh.domuuid(None), "ddb0cf86-5ba8-4f83-480a-d96f54339219" + ) + self.assertEqual(foobar.device_tag, "foo") + self.assertEqual(foobar.type_name, "bar") + self.assertEqual( + unicode(foobar), + "\n", + ) class testCharacterXML(LibvirtXMLTestBase): - def test_arbitrart_attributes(self): - parallel = librarian.get('parallel')(virsh_instance=self.dummy_virsh) - serial = librarian.get('serial')(virsh_instance=self.dummy_virsh) - channel = librarian.get('channel')(virsh_instance=self.dummy_virsh) - console = librarian.get('console')(virsh_instance=self.dummy_virsh) + parallel = librarian.get("parallel")(virsh_instance=self.dummy_virsh) + serial = librarian.get("serial")(virsh_instance=self.dummy_virsh) + 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) @@ -825,52 +830,50 @@ def test_arbitrart_attributes(self): class testSerialXML(LibvirtXMLTestBase): - XML = u"\ + XML = "\ " def _from_scratch(self): - serial = librarian.get('Serial')(virsh_instance=self.dummy_virsh) - self.assertEqual(serial.device_tag, 'serial') - self.assertEqual(serial.type_name, 'pty') + serial = librarian.get("Serial")(virsh_instance=self.dummy_virsh) + self.assertEqual(serial.device_tag, "serial") + self.assertEqual(serial.type_name, "pty") self.assertEqual(serial.virsh, self.dummy_virsh) - serial.add_source(path='/dev/null') + serial.add_source(path="/dev/null") serial.add_target(port="-1") return serial def test_getters(self): serial = self._from_scratch() - self.assertEqual(serial.sources[0]['path'], '/dev/null') - self.assertEqual(serial.targets[0]['port'], '-1') + self.assertEqual(serial.sources[0]["path"], "/dev/null") + self.assertEqual(serial.targets[0]["port"], "-1") def test_from_element(self): element = xml_utils.ElementTree.fromstring(self.XML) serial1 = self._from_scratch() - serial2 = librarian.get('Serial').new_from_element(element) + serial2 = librarian.get("Serial").new_from_element(element) self.assertEqual(serial1, serial2) # Can't in-place modify the dictionary since it's virtual serial2.update_target(0, port="0") self.assertTrue(serial1 != serial2) - serial1.targets = [{'port': '0'}] + serial1.targets = [{"port": "0"}] self.assertEqual(serial1, serial2) def test_vm_get_by_class(self): - vmxml = vm_xml.VMXML.new_from_dumpxml('foobar', - virsh_instance=self.dummy_virsh) - serial_devices = vmxml.get_devices(device_type='serial') + vmxml = vm_xml.VMXML.new_from_dumpxml("foobar", virsh_instance=self.dummy_virsh) + serial_devices = vmxml.get_devices(device_type="serial") self.assertEqual(len(serial_devices), 4) def test_vm_get_modify(self): - vmxml = vm_xml.VMXML.new_from_dumpxml('foobar', - virsh_instance=self.dummy_virsh) - devices = vmxml['devices'] + vmxml = vm_xml.VMXML.new_from_dumpxml("foobar", virsh_instance=self.dummy_virsh) + devices = vmxml["devices"] serial1 = devices[0] serial2 = devices[1] serial3 = devices[2] serial4 = devices[3] - self.assertEqual(serial1.device_tag, 'serial') - self.assertEqual(serial2.device_tag, 'serial') - self.assertEqual(serial1.type_name, 'pty') - self.assertEqual(serial2.type_name, 'pty') + self.assertEqual(serial1.device_tag, "serial") + self.assertEqual(serial2.device_tag, "serial") + self.assertEqual(serial1.type_name, "pty") + self.assertEqual(serial2.type_name, "pty") self.assertFalse(serial1 == serial2) self.assertEqual(serial1.sources, []) # mix up access style @@ -879,145 +882,146 @@ def test_vm_get_modify(self): serial1.update_target(0, port="1") self.assertEqual(serial1, serial2) # Exercise bind mode - self.assertEqual(serial3.type_name, 'tcp') + self.assertEqual(serial3.type_name, "tcp") source_connect = serial3.sources[0] - self.assertEqual(source_connect, {'mode': "connect", 'host': '1.2.3.4', - 'service': '2445'}) - self.assertEqual(serial3.protocol_type, 'raw') - self.assertEqual(serial3.targets[0]['port'], '2') + self.assertEqual( + source_connect, {"mode": "connect", "host": "1.2.3.4", "service": "2445"} + ) + self.assertEqual(serial3.protocol_type, "raw") + self.assertEqual(serial3.targets[0]["port"], "2") # Exercise udp type - self.assertEqual(serial4.type_name, 'udp') + self.assertEqual(serial4.type_name, "udp") source_bind = serial4.sources[0] source_connect = serial4.sources[1] - self.assertEqual(source_bind['host'], "1.2.3.4") - self.assertEqual(source_connect['host'], "4.3.2.1") - self.assertEqual(source_bind['service'], '2445') - self.assertEqual(source_connect['service'], '5442') + self.assertEqual(source_bind["host"], "1.2.3.4") + self.assertEqual(source_connect["host"], "4.3.2.1") + self.assertEqual(source_bind["service"], "2445") + self.assertEqual(source_connect["service"], "5442") class testVMCPUTuneXML(LibvirtXMLTestBase): - def test_get_set_del(self): - vmxml = vm_xml.VMXML.new_from_dumpxml('foobar', - virsh_instance=self.dummy_virsh) + vmxml = vm_xml.VMXML.new_from_dumpxml("foobar", virsh_instance=self.dummy_virsh) cputune = vmxml.cputune # Get, modify, set and delete integer and attribute elements self.assertEqual(cputune.shares, 2048) self.assertEqual(cputune.period, 1000000) self.assertEqual(cputune.quota, -1) - self.assertEqual(cputune.emulatorpin, '1-3') + self.assertEqual(cputune.emulatorpin, "1-3") self.assertEqual(cputune.emulator_period, 1000000) self.assertEqual(cputune.emulator_quota, -1) cputune.shares = 0 self.assertEqual(cputune.shares, 0) - cputune.emulatorpin = '2-4' - self.assertEqual(cputune.emulatorpin, '2-4') + cputune.emulatorpin = "2-4" + self.assertEqual(cputune.emulatorpin, "2-4") del cputune.shares - self.assertRaises(xcepts.LibvirtXMLNotFoundError, - cputune.__getitem__, 'shares') + self.assertRaises(xcepts.LibvirtXMLNotFoundError, cputune.__getitem__, "shares") del cputune.emulatorpin - self.assertRaises(xcepts.LibvirtXMLNotFoundError, - cputune.__getitem__, 'emulatorpin') + self.assertRaises( + xcepts.LibvirtXMLNotFoundError, cputune.__getitem__, "emulatorpin" + ) # Get, modify, set and delete vcpupins vcpupins = cputune.vcpupins self.assertEqual(len(vcpupins), 4) - self.assertEqual(vcpupins[3], {'vcpu': '3', 'cpuset': '0,4'}) - vcpupins.append({'vcpu': '4', 'cpuset': '2-4,^3'}) + self.assertEqual(vcpupins[3], {"vcpu": "3", "cpuset": "0,4"}) + vcpupins.append({"vcpu": "4", "cpuset": "2-4,^3"}) cputune.vcpupins = vcpupins self.assertEqual(len(cputune.vcpupins), 5) - self.assertEqual(cputune.vcpupins[4], - {'vcpu': '4', 'cpuset': '2-4,^3'}) + self.assertEqual(cputune.vcpupins[4], {"vcpu": "4", "cpuset": "2-4,^3"}) del cputune.vcpupins self.assertEqual(cputune.vcpupins, []) class testDiskXML(LibvirtXMLTestBase): - def _check_disk(self, disk): self.assertEqual(disk.type_name, "file") self.assertEqual(disk.device, "disk") - for propname in ('rawio', 'sgio', 'snapshot', 'iotune'): - self.assertRaises(xcepts.LibvirtXMLNotFoundError, - disk.__getitem__, propname) - self.assertRaises(xcepts.LibvirtXMLNotFoundError, - disk.__getattr__, propname) - self.assertRaises(xcepts.LibvirtXMLNotFoundError, - getattr(disk, 'get_' + propname)) - self.assertEqual(disk.driver, {'name': 'qemu', 'type': 'qcow2'}) - self.assertEqual(disk.target['dev'], 'vda') - self.assertEqual(disk.target['bus'], 'virtio') + for propname in ("rawio", "sgio", "snapshot", "iotune"): + self.assertRaises( + xcepts.LibvirtXMLNotFoundError, disk.__getitem__, propname + ) + self.assertRaises( + xcepts.LibvirtXMLNotFoundError, disk.__getattr__, propname + ) + self.assertRaises( + xcepts.LibvirtXMLNotFoundError, getattr(disk, "get_" + propname) + ) + self.assertEqual(disk.driver, {"name": "qemu", "type": "qcow2"}) + self.assertEqual(disk.target["dev"], "vda") + self.assertEqual(disk.target["bus"], "virtio") source = disk.source - self.assertEqual(source.attrs, {'file': '/foo/bar/baz.qcow2'}) + self.assertEqual(source.attrs, {"file": "/foo/bar/baz.qcow2"}) self.assertEqual(source.seclabels, []) self.assertEqual(source.hosts, []) - self.assertEqual(disk.address.attrs, {"domain": "0x0000", - "bus": "0x00", "slot": "0x04", - "function": "0x0", "type": "pci"}) + self.assertEqual( + disk.address.attrs, + { + "domain": "0x0000", + "bus": "0x00", + "slot": "0x04", + "function": "0x0", + "type": "pci", + }, + ) def test_vm_get(self): - vmxml = vm_xml.VMXML.new_from_dumpxml('foobar', - virsh_instance=self.dummy_virsh) + vmxml = vm_xml.VMXML.new_from_dumpxml("foobar", virsh_instance=self.dummy_virsh) for device in vmxml.devices: - if device.device_tag is 'disk': + if device.device_tag is "disk": self._check_disk(device) else: continue def test_vm_get_by_class(self): - vmxml = vm_xml.VMXML.new_from_dumpxml('foobar', - virsh_instance=self.dummy_virsh) - disk_devices = vmxml.get_devices(device_type='disk') + vmxml = vm_xml.VMXML.new_from_dumpxml("foobar", virsh_instance=self.dummy_virsh) + disk_devices = vmxml.get_devices(device_type="disk") self.assertEqual(len(disk_devices), 1) self._check_disk(disk_devices[0]) class testAddressXML(LibvirtXMLTestBase): - def test_required(self): - address = librarian.get('address') - self.assertRaises(xcepts.LibvirtXMLError, - address.new_from_dict, - {}, self.dummy_virsh) + address = librarian.get("address") + self.assertRaises( + xcepts.LibvirtXMLError, address.new_from_dict, {}, self.dummy_virsh + ) # no type_name attribute - element = xml_utils.ElementTree.Element('address', {'foo': 'bar'}) - self.assertRaises(xcepts.LibvirtXMLError, - address.new_from_element, - element, self.dummy_virsh) - element.set('type', 'foobar') + element = xml_utils.ElementTree.Element("address", {"foo": "bar"}) + self.assertRaises( + xcepts.LibvirtXMLError, address.new_from_element, element, self.dummy_virsh + ) + element.set("type", "foobar") new_address = address.new_from_element(element, self.dummy_virsh) - the_dict = {'type_name': 'foobar', 'foo': 'bar'} + the_dict = {"type_name": "foobar", "foo": "bar"} another_address = address.new_from_dict(the_dict, self.dummy_virsh) self.assertEqual(str(new_address), str(another_address)) class testVMXMLDevices(LibvirtXMLTestBase): - def test_channels(self): logging.disable(logging.WARNING) - vmxml = vm_xml.VMXML.new_from_dumpxml('foobar', - virsh_instance=self.dummy_virsh) - channels = vmxml.devices.by_device_tag('channel') + vmxml = vm_xml.VMXML.new_from_dumpxml("foobar", virsh_instance=self.dummy_virsh) + channels = vmxml.devices.by_device_tag("channel") self.assertEqual(len(channels), 2) self.assertTrue(isinstance(channels, vm_xml.VMXMLDevices)) - self.assertEqual(channels[0].type_name, 'foo1') - self.assertEqual(channels[1].type_name, 'bar1') + self.assertEqual(channels[0].type_name, "foo1") + self.assertEqual(channels[1].type_name, "bar1") one = channels.pop() two = channels.pop() self.assertEqual(len(channels), 0) def test_graphics(self): logging.disable(logging.WARNING) - vmxml = vm_xml.VMXML.new_from_dumpxml('foobar', - virsh_instance=self.dummy_virsh) + vmxml = vm_xml.VMXML.new_from_dumpxml("foobar", virsh_instance=self.dummy_virsh) devices = vmxml.devices # Assume only one graphics device, take first in list - graphics_index = devices.index(devices.by_device_tag('graphics')[0]) + graphics_index = devices.index(devices.by_device_tag("graphics")[0]) # Make copy of existing graphics device graphics = devices[graphics_index] # Modify copy - graphics.passwd = 'foobar' + graphics.passwd = "foobar" # Remove existing graphics device del devices[graphics_index] # Add modified copy (another copy) @@ -1029,90 +1033,85 @@ def test_graphics(self): # clean up devices temp files del devices # Check result - self.assertEqual(vmxml.devices[-1].passwd, 'foobar') + self.assertEqual(vmxml.devices[-1].passwd, "foobar") class testCAPXML(LibvirtXMLTestBase): - def test_capxmlbase(self): capxmlbase = nodedev_xml.CAPXML() - self.assertRaises(NotImplementedError, - capxmlbase.get_sysfs_sub_path) - self.assertRaises(NotImplementedError, - capxmlbase.get_key2filename_dict) - self.assertRaises(NotImplementedError, - capxmlbase.get_key2value_dict) + self.assertRaises(NotImplementedError, capxmlbase.get_sysfs_sub_path) + self.assertRaises(NotImplementedError, capxmlbase.get_key2filename_dict) + self.assertRaises(NotImplementedError, capxmlbase.get_key2value_dict) class testNodedevXMLBase(LibvirtXMLTestBase): - def _from_scratch(self): - nodedevxml = nodedev_xml.NodedevXMLBase( - virsh_instance=self.dummy_virsh) - nodedevxml.name = 'name_test' - nodedevxml.parent = 'parent_test' + nodedevxml = nodedev_xml.NodedevXMLBase(virsh_instance=self.dummy_virsh) + nodedevxml.name = "name_test" + nodedevxml.parent = "parent_test" return nodedevxml def test_getter(self): nodedevxml = self._from_scratch() - self.assertEqual(nodedevxml.name, 'name_test') - self.assertEqual(nodedevxml.parent, 'parent_test') + self.assertEqual(nodedevxml.name, "name_test") + self.assertEqual(nodedevxml.parent, "parent_test") def test_static(self): base = nodedev_xml.NodedevXMLBase - cap_list = ['system', 'pci'] + cap_list = ["system", "pci"] for cap_type in cap_list: result = base.get_cap_by_type(cap_type) self.assertTrue(isinstance(result, nodedev_xml.CAPXML)) class testNodedevXML(LibvirtXMLTestBase): - def test_new_from_dumpxml(self): NodedevXML = nodedev_xml.NodedevXML - nodedevxml = NodedevXML.new_from_dumpxml('pci_0000_00_00_0', - virsh_instance=self.dummy_virsh) + nodedevxml = NodedevXML.new_from_dumpxml( + "pci_0000_00_00_0", virsh_instance=self.dummy_virsh + ) self.assertTrue(isinstance(nodedevxml, NodedevXML)) def test_get_key2value_dict(self): NodedevXML = nodedev_xml.NodedevXML - xml = NodedevXML.new_from_dumpxml('pci_0000_00_00_0', - virsh_instance=self.dummy_virsh) + xml = NodedevXML.new_from_dumpxml( + "pci_0000_00_00_0", virsh_instance=self.dummy_virsh + ) result = xml.get_key2value_dict() self.assertTrue(isinstance(result, dict)) def test_get_key2syspath_dict(self): NodedevXML = nodedev_xml.NodedevXML - xml = NodedevXML.new_from_dumpxml('pci_0000_00_00_0', - virsh_instance=self.dummy_virsh) + xml = NodedevXML.new_from_dumpxml( + "pci_0000_00_00_0", virsh_instance=self.dummy_virsh + ) result = xml.get_key2syspath_dict() self.assertTrue(isinstance(result, dict)) class testPCIXML(LibvirtXMLTestBase): - def _from_scratch(self): pcixml = nodedev_xml.PCIXML() pcixml.domain = 0x10 pcixml.bus = 0x20 pcixml.slot = 0x30 pcixml.function = 0x1 - pcixml.vendor_id = '0x123' - pcixml.product_id = '0x123' + pcixml.vendor_id = "0x123" + pcixml.product_id = "0x123" return pcixml def test_static(self): PCIXML = nodedev_xml.PCIXML result = PCIXML.make_sysfs_sub_path(0x10, 0x20, 0x30, 0x1) - self.assertEqual(result, 'pci_bus/0010:20/device/0010:20:30.1') + self.assertEqual(result, "pci_bus/0010:20/device/0010:20:30.1") def test_get_path(self): pcixml = self._from_scratch() result = pcixml.get_sysfs_sub_path() - self.assertEqual(result, 'pci_bus/0010:20/device/0010:20:30.1') + self.assertEqual(result, "pci_bus/0010:20/device/0010:20:30.1") def test_get_key2filename_dict(self): PCIXML = nodedev_xml.PCIXML diff --git a/selftests/unit/test_nfs.py b/selftests/unit/test_nfs.py index 77d0720d6a..c3e3492bbe 100644 --- a/selftests/unit/test_nfs.py +++ b/selftests/unit/test_nfs.py @@ -9,7 +9,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest.unittest_utils import mock @@ -19,15 +19,16 @@ class FakeService(object): - def __init__(self, service_name): - self.fake_cmds = [{"cmd": "status", "stdout": True}, - {"cmd": "restart", "stdout": ""}] + self.fake_cmds = [ + {"cmd": "status", "stdout": True}, + {"cmd": "restart", "stdout": ""}, + ] def get_stdout(self, cmd): for fake_cmd in self.fake_cmds: - if fake_cmd['cmd'] == cmd: - return fake_cmd['stdout'] + if fake_cmd["cmd"] == cmd: + return fake_cmd["stdout"] raise ValueError("Could not locate locate '%s' on fake cmd db" % cmd) def status(self): @@ -38,18 +39,16 @@ def restart(self): class nfs_test(unittest.TestCase): - def setup_stubs_init(self): path.find_command.expect_call("mount") path.find_command.expect_call("service") path.find_command.expect_call("exportfs") - service.Factory.create_service.expect_call("nfs").and_return( - FakeService("nfs")) + service.Factory.create_service.expect_call("nfs").and_return(FakeService("nfs")) service.Factory.create_service.expect_call("rpcbind").and_return( - FakeService("rpcbind")) + FakeService("rpcbind") + ) mount_src = self.nfs_params.get("nfs_mount_src") - export_dir = (self.nfs_params.get("export_dir") or - mount_src.split(":")[-1]) + export_dir = self.nfs_params.get("export_dir") or mount_src.split(":")[-1] export_ip = self.nfs_params.get("export_ip", "*") export_options = self.nfs_params.get("export_options", "").strip() nfs.Exportfs.expect_new(export_dir, export_ip, export_options) @@ -58,26 +57,27 @@ 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_misc.mount.expect_call( + nfs_obj.mount_src, nfs_obj.mount_dir, "nfs", perm=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_misc.is_mounted.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, - nfs_obj.mount_dir, - "nfs") + utils_misc.umount.expect_call(nfs_obj.mount_src, nfs_obj.mount_dir, "nfs") nfs_obj.exportfs.reset_export.expect_call() def setUp(self): - self.nfs_params = {"nfs_mount_dir": "/mnt/nfstest", - "nfs_mount_options": "rw", - "nfs_mount_src": "127.0.0.1:/mnt/nfssrc", - "setup_local_nfs": "yes", - "export_options": "rw,no_root_squash"} + self.nfs_params = { + "nfs_mount_dir": "/mnt/nfstest", + "nfs_mount_options": "rw", + "nfs_mount_src": "127.0.0.1:/mnt/nfssrc", + "setup_local_nfs": "yes", + "export_options": "rw,no_root_squash", + } self.god = mock.mock_god() self.god.stub_function(path, "find_command") self.god.stub_function(process, "system") diff --git a/selftests/unit/test_propcan.py b/selftests/unit/test_propcan.py index 93aea4ea65..32bdd003f4 100644 --- a/selftests/unit/test_propcan.py +++ b/selftests/unit/test_propcan.py @@ -6,129 +6,133 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import propcan class TestPropCanBase(unittest.TestCase): - def test_empty_init(self): self.assertRaises(NotImplementedError, propcan.PropCanBase) def test_empty_params_init(self): - self.assertRaises(NotImplementedError, - propcan.PropCanBase, - {'foo': 'bar'}) + self.assertRaises(NotImplementedError, propcan.PropCanBase, {"foo": "bar"}) def test_single_init(self): class FooBar(propcan.PropCanBase): - __slots__ = ('foo',) - testcan = FooBar(foo='bar') + __slots__ = ("foo",) + + testcan = FooBar(foo="bar") self.assertEqual(len(testcan), 1) - self.assertEqual(testcan['foo'], 'bar') - self.assertEqual(testcan.foo, 'bar') + self.assertEqual(testcan["foo"], "bar") + self.assertEqual(testcan.foo, "bar") def test_double_init(self): class FooBar(propcan.PropCanBase): - __slots__ = ('foo', 'bar') - testcan = FooBar(foo='bar', bar='foo') + __slots__ = ("foo", "bar") + + testcan = FooBar(foo="bar", bar="foo") self.assertEqual(len(testcan), 2) - self.assertEqual(testcan['foo'], 'bar') - self.assertEqual(testcan['bar'], 'foo') + self.assertEqual(testcan["foo"], "bar") + self.assertEqual(testcan["bar"], "foo") self.assertEqual(len(testcan), 2) - self.assertEqual(testcan.foo, 'bar') - self.assertEqual(testcan.bar, 'foo') + self.assertEqual(testcan.foo, "bar") + self.assertEqual(testcan.bar, "foo") def test_slots_restrict(self): class FooBar(propcan.PropCanBase): - __slots__ = ('foo',) - testcan = FooBar(foo='bar') + __slots__ = ("foo",) + + testcan = FooBar(foo="bar") self.assertEqual(len(testcan), 1) - self.assertEqual(testcan['foo'], 'bar') - self.assertEqual(testcan.foo, 'bar') - self.assertRaises(AttributeError, setattr, testcan, 'bar', 'foo') - self.assertRaises(KeyError, testcan.__setitem__, 'bar', 'foo') + self.assertEqual(testcan["foo"], "bar") + self.assertEqual(testcan.foo, "bar") + self.assertRaises(AttributeError, setattr, testcan, "bar", "foo") + self.assertRaises(KeyError, testcan.__setitem__, "bar", "foo") def test_mixed_init(self): class FooBar(propcan.PropCanBase): - __slots__ = ('foo', 'bar') - testcan = FooBar({'foo': 'bar'}) + __slots__ = ("foo", "bar") + + testcan = FooBar({"foo": "bar"}) self.assertEqual(len(testcan), 1) - self.assertEqual(testcan['foo'], 'bar') + self.assertEqual(testcan["foo"], "bar") self.assertEqual(len(testcan), 1) - self.assertEqual(testcan.foo, 'bar') - self.assertRaises(KeyError, testcan.__getitem__, 'bar') - self.assertRaises(AttributeError, getattr, testcan, 'bar') - self.assertRaises(KeyError, testcan.__delitem__, 'bar') - self.assertRaises(AttributeError, delattr, testcan, 'bar') + self.assertEqual(testcan.foo, "bar") + self.assertRaises(KeyError, testcan.__getitem__, "bar") + self.assertRaises(AttributeError, getattr, testcan, "bar") + self.assertRaises(KeyError, testcan.__delitem__, "bar") + self.assertRaises(AttributeError, delattr, testcan, "bar") def test_subclass_single_init_setter(self): class FooBar(propcan.PropCanBase): - __slots__ = ('foo', ) + __slots__ = ("foo",) it_works = False def set_foo(self, value): - self.__dict_set__('foo', value) - if value == 'bar': - self.__super_set__('it_works', True) + self.__dict_set__("foo", value) + if value == "bar": + self.__super_set__("it_works", True) + testcan = FooBar() self.assertEqual(len(testcan), 0) self.assertFalse(testcan.it_works) - self.assertRaises(KeyError, testcan.__getitem__, 'foo') - self.assertRaises(AttributeError, getattr, testcan, 'foo') - testcan['foo'] = 'bar' + self.assertRaises(KeyError, testcan.__getitem__, "foo") + self.assertRaises(AttributeError, getattr, testcan, "foo") + testcan["foo"] = "bar" self.assertEqual(len(testcan), 1) self.assertTrue(testcan.it_works) def test_subclass_single_init_getter(self): class FooBar(propcan.PropCanBase): - __slots__ = ('foo', ) + __slots__ = ("foo",) it_works = False def get_foo(self): - value = self.__dict_get__('foo') - if value == 'bar': - self.__super_set__('it_works', True) + value = self.__dict_get__("foo") + if value == "bar": + self.__super_set__("it_works", True) return value + testcan = FooBar() self.assertFalse(testcan.it_works) self.assertEqual(len(testcan), 0) - testcan['foo'] = 'bar' + testcan["foo"] = "bar" self.assertEqual(len(testcan), 1) # verify __super_set__() doesn't call getter self.assertFalse(testcan.it_works) - self.assertEqual(testcan['foo'], 'bar') - self.assertEqual(testcan.foo, 'bar') + self.assertEqual(testcan["foo"], "bar") + self.assertEqual(testcan.foo, "bar") self.assertTrue(testcan.it_works) def test_subclass_single_init_delter(self): class FooBar(propcan.PropCanBase): - __slots__ = ('foo', ) + __slots__ = ("foo",) it_works = False def del_foo(self): - value = self.__dict_get__('foo') - if value == 'bar': - self.__super_set__('it_works', True) - self.__dict_del__('foo') + value = self.__dict_get__("foo") + if value == "bar": + self.__super_set__("it_works", True) + self.__dict_del__("foo") + testcan = FooBar() self.assertEqual(len(testcan), 0) self.assertFalse(testcan.it_works) - self.assertFalse(hasattr(testcan, 'foo')) - self.assertFalse('foo' in testcan) - testcan['foo'] = 'bar' + self.assertFalse(hasattr(testcan, "foo")) + self.assertFalse("foo" in testcan) + testcan["foo"] = "bar" self.assertEqual(len(testcan), 1) - self.assertEqual(testcan['foo'], 'bar') - self.assertEqual(testcan.foo, 'bar') - del testcan['foo'] + self.assertEqual(testcan["foo"], "bar") + self.assertEqual(testcan.foo, "bar") + del testcan["foo"] self.assertEqual(len(testcan), 0) self.assertTrue(testcan.it_works) def test_subclass_no_mask_attributeerror(self): class FooBar(propcan.PropCanBase): - __slots__ = ('foo', ) + __slots__ = ("foo",) def del_foo(self): raise AttributeError("Del Test") @@ -138,18 +142,20 @@ def set_foo(self, value): def get_foo(self): raise AttributeError("Get Test") + testcan = FooBar() - self.assertRaises(AttributeError, testcan.__getitem__, 'foo') - self.assertRaises(AttributeError, testcan.__setitem__, 'foo', None) - self.assertRaises(AttributeError, testcan.__delitem__, 'foo') - self.assertRaises(AttributeError, testcan.__getattr__, 'foo') - self.assertRaises(AttributeError, testcan.__setattr__, 'foo', None) - self.assertRaises(AttributeError, testcan.__delattr__, 'foo') + self.assertRaises(AttributeError, testcan.__getitem__, "foo") + self.assertRaises(AttributeError, testcan.__setitem__, "foo", None) + self.assertRaises(AttributeError, testcan.__delitem__, "foo") + self.assertRaises(AttributeError, testcan.__getattr__, "foo") + self.assertRaises(AttributeError, testcan.__setattr__, "foo", None) + self.assertRaises(AttributeError, testcan.__delattr__, "foo") def test_dict_methods_1(self): class FooBar(propcan.PropCanBase): - __slots__ = ('foo', 'bar') - testcan = FooBar(foo='bar', bar='foo') + __slots__ = ("foo", "bar") + + testcan = FooBar(foo="bar", bar="foo") testdict = {} for key, value in list(testcan.items()): testdict[key] = value @@ -157,101 +163,117 @@ class FooBar(propcan.PropCanBase): def test_dict_methods_2(self): class FooBar(propcan.PropCanBase): - __slots__ = ('foo', 'bar') - testcan = FooBar(foo='bar', bar='foo') + __slots__ = ("foo", "bar") + + testcan = FooBar(foo="bar", bar="foo") testdict = testcan.copy() self.assertEqual(testcan, testdict) - testcan['foo'] = 'foo' - testcan['bar'] = 'bar' + testcan["foo"] = "foo" + testcan["bar"] = "bar" self.assertTrue(testcan.foo != testdict.foo) self.assertTrue(testcan.bar != testdict.bar) - testdict['foo'] = 'foo' - testdict['bar'] = 'bar' + testdict["foo"] = "foo" + testdict["bar"] = "bar" self.assertTrue(testcan.foo == testdict.foo) self.assertTrue(testcan.bar == testdict.bar) def test_update(self): class FooBar(propcan.PropCanBase): - __slots__ = ('foo', 'bar') + __slots__ = ("foo", "bar") testdict = FooBar() - other = {'foo': 1, 'bar': 2} + other = {"foo": 1, "bar": 2} testdict.update(other) self.assertEqual(testdict, other) - other = 'string' + other = "string" self.assertRaises(ValueError, testdict.update, other) - other = {'foo': 1, 'bar': 2, 'v3': 3} + other = {"foo": 1, "bar": 2, "v3": 3} self.assertRaises(KeyError, testdict.update, other) - kwargs = {'foo': "foo", 'bar': "bar"} + kwargs = {"foo": "foo", "bar": "bar"} testdict.update(**kwargs) self.assertEqual(testdict, kwargs) class TestPropCan(unittest.TestCase): - def setUp(self): logging.disable(logging.INFO) def test_extranious_init(self): class FooBar(propcan.PropCan): - __slots__ = ('foo', ) - testcan = FooBar((('foo', 'bar'), ('bar', 'foo'),)) + __slots__ = ("foo",) + + testcan = FooBar( + ( + ("foo", "bar"), + ("bar", "foo"), + ) + ) self.assertEqual(len(testcan), 1) - testcan = FooBar(bar='foo') + testcan = FooBar(bar="foo") self.assertEqual(len(testcan), 0) def test_init_None_value(self): class FooBar(propcan.PropCan): - __slots__ = ('foo', ) + __slots__ = ("foo",) + testcan = FooBar(foo=None) self.assertEqual(len(testcan), 0) - self.assertEqual(testcan['foo'], None) + self.assertEqual(testcan["foo"], None) self.assertEqual(testcan.foo, None) def test_compare(self): class FooBar(propcan.PropCan): - __slots__ = ('foo', 'bar') - testcan = FooBar(foo=None, bar='foo') + __slots__ = ("foo", "bar") + + testcan = FooBar(foo=None, bar="foo") self.assertEqual(len(testcan), 1) - self.assertTrue(testcan == {'bar': 'foo'}) - testcan.foo = 'bar' + self.assertTrue(testcan == {"bar": "foo"}) + testcan.foo = "bar" self.assertEqual(len(testcan), 2) - self.assertTrue(testcan == {'bar': 'foo', 'foo': 'bar'}) - self.assertTrue(testcan == {'foo': 'bar', 'bar': 'foo'}) + self.assertTrue(testcan == {"bar": "foo", "foo": "bar"}) + self.assertTrue(testcan == {"foo": "bar", "bar": "foo"}) testcan.bar = None self.assertEqual(len(testcan), 1) - self.assertTrue(testcan == {'foo': 'bar'}) + self.assertTrue(testcan == {"foo": "bar"}) def test_odd_values(self): class FooBar(propcan.PropCan): - __slots__ = ('foo', 'bar', 'baz') + __slots__ = ("foo", "bar", "baz") + testcan = FooBar() self.assertEqual(len(testcan), 0) - testcan.foo = type('blah', (), {}) + testcan.foo = type("blah", (), {}) self.assertEqual(len(testcan), 1) - testcan['bar'] = testcan + testcan["bar"] = testcan self.assertEqual(len(testcan), 2) - setattr(testcan, 'baz', lambda self: str(self)) + setattr(testcan, "baz", lambda self: str(self)) self.assertEqual(len(testcan), 3) def test_printables(self): class FooBar(propcan.PropCan): - __slots__ = ('foo', 'bar', 'baz') + __slots__ = ("foo", "bar", "baz") + testcan = FooBar() self.assertEqual(len(testcan), 0) try: tmp_long = long(12345) except NameError: tmp_long = 12345 - for value in ('foobar', u'foobar', 1, 1.1, tmp_long, ): - setattr(testcan, 'bar', value) + for value in ( + "foobar", + "foobar", + 1, + 1.1, + tmp_long, + ): + setattr(testcan, "bar", value) self.assertEqual(len(testcan), 1) - self.assertTrue(testcan == {'bar': value}) - self.assertEqual(str(testcan), str({'bar': value})) + self.assertTrue(testcan == {"bar": value}) + self.assertEqual(str(testcan), str({"bar": value})) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_qemu_devices.py b/selftests/unit/test_qemu_devices.py index 86a99d2503..87d6e42e49 100644 --- a/selftests/unit/test_qemu_devices.py +++ b/selftests/unit/test_qemu_devices.py @@ -14,7 +14,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest.unittest_utils import mock @@ -25,8 +25,9 @@ from six.moves import xrange -UNITTEST_DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), - "unittest_data") +UNITTEST_DATA_DIR = os.path.join( + os.path.dirname(os.path.abspath(__file__)), "unittest_data" +) # Dummy variables # qemu-1.5.0 human monitor help output @@ -36,34 +37,32 @@ # qemu-1.5.0 -help QEMU_HELP = open(os.path.join(UNITTEST_DATA_DIR, "qemu-1.5.0__help")).read() # qemu-1.5.0 -devices ? -QEMU_DEVICES = open( - os.path.join(UNITTEST_DATA_DIR, "qemu-1.5.0__devices_help")).read() +QEMU_DEVICES = open(os.path.join(UNITTEST_DATA_DIR, "qemu-1.5.0__devices_help")).read() # qemu-1.5.0 -M ? -QEMU_MACHINE = open( - os.path.join(UNITTEST_DATA_DIR, "qemu-1.5.0__machine_help")).read() +QEMU_MACHINE = open(os.path.join(UNITTEST_DATA_DIR, "qemu-1.5.0__machine_help")).read() class ParamsDict(dict): - """ params like dictionary """ + """params like dictionary""" def objects(self, item): if self.get(item): - return self.get(item).split(' ') + return self.get(item).split(" ") def object_params(self, obj): ret = self.copy() for (param, value) in six.iteritems(self): - if param.endswith('_%s' % obj): - ret[param[:-len('_%s' % obj)]] = value + if param.endswith("_%s" % obj): + ret[param[: -len("_%s" % obj)]] = value return ret class MockHMPMonitor(qemu_monitor.HumanMonitor): - """ Dummy class inherited from qemu_monitor.HumanMonitor """ + """Dummy class inherited from qemu_monitor.HumanMonitor""" - def __init__(self): # pylint: disable=W0231 + def __init__(self): # pylint: disable=W0231 self.debug_log = False def __del__(self): @@ -72,22 +71,26 @@ def __del__(self): class Devices(unittest.TestCase): - """ set of qemu devices tests """ + """set of qemu devices tests""" def test_q_base_device(self): - """ QBaseDevice tests """ - qdevice = qdevices.QBaseDevice('MyType', - {'ParamA': 'ValueA', - 'AUTOREMOVE': None}, - 'Object1', - {'type': 'pci'}) - self.assertEqual(qdevice['ParamA'], 'ValueA', 'Param added during ' - '__init__ is corrupted %s != %s' % (qdevice['ParamA'], - 'ValueA')) - qdevice['ParamA'] = 'ValueB' - qdevice.set_param('BoolTrue', True) - qdevice.set_param('BoolFalse', 'off', bool) - qdevice['Empty'] = 'EMPTY_STRING' + """QBaseDevice tests""" + qdevice = qdevices.QBaseDevice( + "MyType", + {"ParamA": "ValueA", "AUTOREMOVE": None}, + "Object1", + {"type": "pci"}, + ) + self.assertEqual( + qdevice["ParamA"], + "ValueA", + "Param added during " + "__init__ is corrupted %s != %s" % (qdevice["ParamA"], "ValueA"), + ) + qdevice["ParamA"] = "ValueB" + qdevice.set_param("BoolTrue", True) + qdevice.set_param("BoolFalse", "off", bool) + qdevice["Empty"] = "EMPTY_STRING" out = """MyType aid = None @@ -100,142 +103,210 @@ def test_q_base_device(self): BoolFalse = off Empty = "" """ - self.assertEqual(qdevice.str_long(), out, "Device output doesn't match" - "\n%s\n\n%s" % (qdevice.str_long(), out)) + self.assertEqual( + qdevice.str_long(), + out, + "Device output doesn't match" "\n%s\n\n%s" % (qdevice.str_long(), out), + ) def test_q_string_device(self): - """ QStringDevice tests """ - qdevice = qdevices.QStringDevice('MyType', {'addr': '0x7'}, - cmdline='-qdevice ahci,addr=%(addr)s') - self.assertEqual(qdevice.cmdline(), '-qdevice ahci,addr=0x7', "Cmdline" - " doesn't match expected one:\n%s\n%s" - % (qdevice.cmdline(), '-qdevice ahci,addr=0x7')) + """QStringDevice tests""" + qdevice = qdevices.QStringDevice( + "MyType", {"addr": "0x7"}, cmdline="-qdevice ahci,addr=%(addr)s" + ) + self.assertEqual( + qdevice.cmdline(), + "-qdevice ahci,addr=0x7", + "Cmdline" + " doesn't match expected one:\n%s\n%s" + % (qdevice.cmdline(), "-qdevice ahci,addr=0x7"), + ) def test_q_device(self): - """ QDevice tests """ - qdevice = qdevices.QDevice('ahci', {'addr': '0x7'}) - - self.assertEqual(str(qdevice), "a'ahci'", "Alternative name error %s " - "!= %s" % (str(qdevice), "a'ahci'")) - - qdevice['id'] = 'ahci1' - self.assertEqual(str(qdevice), "q'ahci1'", "Id name error %s " - "!= %s" % (str(qdevice), "q'ahci1'")) + """QDevice tests""" + qdevice = qdevices.QDevice("ahci", {"addr": "0x7"}) + + self.assertEqual( + str(qdevice), + "a'ahci'", + "Alternative name error %s " "!= %s" % (str(qdevice), "a'ahci'"), + ) + + qdevice["id"] = "ahci1" + self.assertEqual( + str(qdevice), + "q'ahci1'", + "Id name error %s " "!= %s" % (str(qdevice), "q'ahci1'"), + ) exp = "device_add ahci,addr=0x7,id=ahci1" out = qdevice.hotplug_hmp() - self.assertEqual(out, exp, "HMP command corrupted:\n%s\n%s" - % (out, exp)) + self.assertEqual(out, exp, "HMP command corrupted:\n%s\n%s" % (out, exp)) - exp = ("('device_add', OrderedDict([('addr', '0x7'), " - "('driver', 'ahci'), ('id', 'ahci1')]))") + exp = ( + "('device_add', OrderedDict([('addr', '0x7'), " + "('driver', 'ahci'), ('id', 'ahci1')]))" + ) out = str(qdevice.hotplug_qmp()) - self.assertEqual(out, exp, "QMP command corrupted:\n%s\n%s" - % (out, exp)) + self.assertEqual(out, exp, "QMP command corrupted:\n%s\n%s" % (out, exp)) class Buses(unittest.TestCase): - """ Set of bus-representation tests """ + """Set of bus-representation tests""" def test_q_sparse_bus(self): - """ Sparse bus tests (general bus testing) """ - bus = qdevices.QSparseBus('bus', - (['addr1', 'addr2', 'addr3'], [2, 6, 4]), - 'my_bus', - 'bus_type', - 'autotest_bus') + """Sparse bus tests (general bus testing)""" + bus = qdevices.QSparseBus( + "bus", + (["addr1", "addr2", "addr3"], [2, 6, 4]), + "my_bus", + "bus_type", + "autotest_bus", + ) qdevice = qdevices.QDevice # Correct records - params = {'addr1': '0', 'addr2': '0', 'addr3': '0', 'bus': 'my_bus'} - dev = qdevice('dev1', params, parent_bus={'type': 'bus_type'}) + params = {"addr1": "0", "addr2": "0", "addr3": "0", "bus": "my_bus"} + dev = qdevice("dev1", params, parent_bus={"type": "bus_type"}) exp = [] out = bus.insert(dev, False) - self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) - - params = {'addr1': '1', 'addr2': '0', 'addr3': '0', 'bus': 'my_bus'} - dev = qdevice('dev2', params, parent_bus={'type': 'bus_type'}) + self.assertEqual( + out, + exp, + "Failed to add device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) + + params = {"addr1": "1", "addr2": "0", "addr3": "0", "bus": "my_bus"} + dev = qdevice("dev2", params, parent_bus={"type": "bus_type"}) exp = [] out = bus.insert(dev, False) - self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) - - params = {'addr1': '1', 'addr2': '1', 'addr3': '0', 'bus': 'my_bus'} - dev = qdevice('dev3', params, parent_bus={'type': 'bus_type'}) + self.assertEqual( + out, + exp, + "Failed to add device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) + + params = {"addr1": "1", "addr2": "1", "addr3": "0", "bus": "my_bus"} + dev = qdevice("dev3", params, parent_bus={"type": "bus_type"}) exp = [] out = bus.insert(dev, False) - self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) - - params = {'addr1': '1', 'addr2': '1', 'addr3': '1', 'bus': 'my_bus'} - dev = qdevice('dev4', params, parent_bus={'type': 'bus_type'}) + self.assertEqual( + out, + exp, + "Failed to add device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) + + params = {"addr1": "1", "addr2": "1", "addr3": "1", "bus": "my_bus"} + dev = qdevice("dev4", params, parent_bus={"type": "bus_type"}) exp = [] out = bus.insert(dev, False) - self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) - - params = {'addr1': '1', 'bus': 'my_bus'} - dev = qdevice('dev5', params, parent_bus={'type': 'bus_type'}) + self.assertEqual( + out, + exp, + "Failed to add device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) + + params = {"addr1": "1", "bus": "my_bus"} + dev = qdevice("dev5", params, parent_bus={"type": "bus_type"}) exp = [] out = bus.insert(dev, False) - self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) - - params = {'bus': 'my_bus'} - dev = qdevice('dev6', params, parent_bus={'type': 'bus_type'}) + self.assertEqual( + out, + exp, + "Failed to add device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) + + params = {"bus": "my_bus"} + dev = qdevice("dev6", params, parent_bus={"type": "bus_type"}) exp = [] out = bus.insert(dev, False) - self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) + self.assertEqual( + out, + exp, + "Failed to add device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) params = {} - dev2 = qdevice('dev7', params, parent_bus={'type': 'bus_type'}) + dev2 = qdevice("dev7", params, parent_bus={"type": "bus_type"}) exp = [] out = bus.insert(dev2, False) - self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" - % (out, exp, dev2.str_long(), bus.str_long())) + self.assertEqual( + out, + exp, + "Failed to add device; %s != %s\n%s\n\n%s" + % (out, exp, dev2.str_long(), bus.str_long()), + ) # Compare short repr - exp = ("my_bus(bus_type): {0-0-0:a'dev1',0-0-1:a'dev6',0-0-2:a'dev7'," - "1-0-0:a'dev2',1-0-1:a'dev5',1-1-0:a'dev3',1-1-1:a'dev4'}") + exp = ( + "my_bus(bus_type): {0-0-0:a'dev1',0-0-1:a'dev6',0-0-2:a'dev7'," + "1-0-0:a'dev2',1-0-1:a'dev5',1-1-0:a'dev3',1-1-1:a'dev4'}" + ) out = str(bus.str_short()) - self.assertEqual(out, exp, "Short representation corrupted:\n%s\n%s" - "\n\n%s" % (out, exp, bus.str_long())) + self.assertEqual( + out, + exp, + "Short representation corrupted:\n%s\n%s" + "\n\n%s" % (out, exp, bus.str_long()), + ) # Incorrect records # Used address - params = {'addr1': '0', 'addr2': '0', 'addr3': '0', 'bus': 'my_bus'} - dev = qdevice('devI1', params, parent_bus={'type': 'bus_type'}) + params = {"addr1": "0", "addr2": "0", "addr3": "0", "bus": "my_bus"} + dev = qdevice("devI1", params, parent_bus={"type": "bus_type"}) exp = "UsedSlot" out = bus.insert(dev, False) - self.assertEqual(out, exp, "Added bad device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) + self.assertEqual( + out, + exp, + "Added bad device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) # Out of range address - params = {'addr1': '0', 'addr2': '6', 'addr3': '0', 'bus': 'my_bus'} - dev = qdevice('devI2', params, parent_bus={'type': 'bus_type'}) + params = {"addr1": "0", "addr2": "6", "addr3": "0", "bus": "my_bus"} + dev = qdevice("devI2", params, parent_bus={"type": "bus_type"}) exp = "BadAddr(False)" out = bus.insert(dev, False) - self.assertEqual(out, exp, "Added bad device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) + self.assertEqual( + out, + exp, + "Added bad device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) # Incorrect bus name - params = {'bus': 'other_bus'} - dev = qdevice('devI3', params, parent_bus={'type': 'bus_type'}) + params = {"bus": "other_bus"} + dev = qdevice("devI3", params, parent_bus={"type": "bus_type"}) exp = "BusId" out = bus.insert(dev, False) - self.assertEqual(out, exp, "Added bad device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) + self.assertEqual( + out, + exp, + "Added bad device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) # Compare short repr - exp = ("my_bus(bus_type): {0-0-0:a'dev1',0-0-1:a'dev6',0-0-2:a'dev7'," - "1-0-0:a'dev2',1-0-1:a'dev5',1-1-0:a'dev3',1-1-1:a'dev4'}") + exp = ( + "my_bus(bus_type): {0-0-0:a'dev1',0-0-1:a'dev6',0-0-2:a'dev7'," + "1-0-0:a'dev2',1-0-1:a'dev5',1-1-0:a'dev3',1-1-1:a'dev4'}" + ) out = str(bus.str_short()) - self.assertEqual(out, exp, "Short representation corrupted:\n%s\n%s" - "\n\n%s" % (out, exp, bus.str_long())) + self.assertEqual( + out, + exp, + "Short representation corrupted:\n%s\n%s" + "\n\n%s" % (out, exp, bus.str_long()), + ) # Compare long repr exp = """Bus my_bus, type=bus_type @@ -317,108 +388,150 @@ def test_q_sparse_bus(self): driver = dev7 """ out = str(bus.str_long()) - self.assertEqual(out, exp, "Long representation corrupted:\n%s\n%s" - % (repr(out), exp)) + self.assertEqual( + out, exp, "Long representation corrupted:\n%s\n%s" % (repr(out), exp) + ) # Low level functions # Get device by object exp = dev2 out = bus.get(dev2) - self.assertEqual(out, exp, "Failed to get device from bus:\n%s\n%s" - "\n\n%s" % (out, exp, bus.str_long())) - - dev2.aid = 'bad_device3' + self.assertEqual( + out, + exp, + "Failed to get device from bus:\n%s\n%s" + "\n\n%s" % (out, exp, bus.str_long()), + ) + + dev2.aid = "bad_device3" exp = dev2 - out = bus.get('bad_device3') - self.assertEqual(out, exp, "Failed to get device from bus:\n%s\n%s" - "\n\n%s" % (out, exp, bus.str_long())) + out = bus.get("bad_device3") + self.assertEqual( + out, + exp, + "Failed to get device from bus:\n%s\n%s" + "\n\n%s" % (out, exp, bus.str_long()), + ) exp = None - out = bus.get('missing_bad_device') - self.assertEqual(out, exp, "Got device while expecting None:\n%s\n%s" - "\n\n%s" % (out, exp, bus.str_long())) + out = bus.get("missing_bad_device") + self.assertEqual( + out, + exp, + "Got device while expecting None:\n%s\n%s" + "\n\n%s" % (out, exp, bus.str_long()), + ) # Remove all devices devs = [dev for dev in bus] for dev in devs: bus.remove(dev) - exp = 'Bus my_bus, type=bus_type\nSlots:\n' + exp = "Bus my_bus, type=bus_type\nSlots:\n" out = str(bus.str_long()) - self.assertEqual(out, exp, "Long representation corrupted:\n%s\n%s" - % (out, exp)) + self.assertEqual( + out, exp, "Long representation corrupted:\n%s\n%s" % (out, exp) + ) def test_q_pci_bus(self): - """ PCI bus tests """ - bus = qdevices.QPCIBus('pci.0', 'pci', 'my_pci') + """PCI bus tests""" + bus = qdevices.QPCIBus("pci.0", "pci", "my_pci") qdevice = qdevices.QDevice # Good devices - params = {'addr': '0'} - dev = qdevice('dev1', params, parent_bus={'type': 'pci'}) + params = {"addr": "0"} + dev = qdevice("dev1", params, parent_bus={"type": "pci"}) exp = [] out = bus.insert(dev, False) - self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) - - params = {'addr': 10, 'bus': 'pci.0'} - dev = qdevice('dev2', params, parent_bus={'type': 'pci'}) + self.assertEqual( + out, + exp, + "Failed to add device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) + + params = {"addr": 10, "bus": "pci.0"} + dev = qdevice("dev2", params, parent_bus={"type": "pci"}) exp = [] out = bus.insert(dev, False) - self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) - - params = {'addr': '0x1f'} - dev = qdevice('dev3', params, parent_bus={'type': 'pci'}) + self.assertEqual( + out, + exp, + "Failed to add device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) + + params = {"addr": "0x1f"} + dev = qdevice("dev3", params, parent_bus={"type": "pci"}) exp = [] out = bus.insert(dev, False) - self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) + self.assertEqual( + out, + exp, + "Failed to add device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) # Compare short repr - exp = ("pci.0(pci): {0x0-0x0:a'dev1',0x1f-0x0:a'dev3',0xa-0x0:a'dev2'}") + exp = "pci.0(pci): {0x0-0x0:a'dev1',0x1f-0x0:a'dev3',0xa-0x0:a'dev2'}" out = str(bus.str_short()) - self.assertEqual(out, exp, "Short representation corrupted:\n%s\n%s" - "\n\n%s" % (out, exp, bus.str_long())) + self.assertEqual( + out, + exp, + "Short representation corrupted:\n%s\n%s" + "\n\n%s" % (out, exp, bus.str_long()), + ) # Incorrect records # Used address - params = {'addr': 0} - dev = qdevice('devI1', params, parent_bus={'type': 'pci'}) + params = {"addr": 0} + dev = qdevice("devI1", params, parent_bus={"type": "pci"}) exp = "UsedSlot" out = bus.insert(dev, False) - self.assertEqual(out, exp, "Added bad device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) + self.assertEqual( + out, + exp, + "Added bad device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) # Out of range address - params = {'addr': '0xffff'} - dev = qdevice('devI2', params, parent_bus={'type': 'pci'}) + params = {"addr": "0xffff"} + dev = qdevice("devI2", params, parent_bus={"type": "pci"}) exp = "BadAddr(False)" out = bus.insert(dev, False) - self.assertEqual(out, exp, "Added bad device; %s != %s\n%s\n\n%s" - % (out, exp, dev.str_long(), bus.str_long())) + self.assertEqual( + out, + exp, + "Added bad device; %s != %s\n%s\n\n%s" + % (out, exp, dev.str_long(), bus.str_long()), + ) # Compare short repr - exp = ("pci.0(pci): {0x0-0x0:a'dev1',0x1f-0x0:a'dev3',0xa-0x0:a'dev2'}") + exp = "pci.0(pci): {0x0-0x0:a'dev1',0x1f-0x0:a'dev3',0xa-0x0:a'dev2'}" out = str(bus.str_short()) - self.assertEqual(out, exp, "Short representation corrupted:\n%s\n%s" - "\n\n%s" % (out, exp, bus.str_long())) + self.assertEqual( + out, + exp, + "Short representation corrupted:\n%s\n%s" + "\n\n%s" % (out, exp, bus.str_long()), + ) def test_q_pci_bus_strict(self): - """ PCI bus tests in strict_mode (enforce additional options) """ - bus = qdevices.QPCIBus('pci.0', 'pci', 'my_pci') + """PCI bus tests in strict_mode (enforce additional options)""" + bus = qdevices.QPCIBus("pci.0", "pci", "my_pci") qdevice = qdevices.QDevice params = {} - bus.insert(qdevice('dev1', params, parent_bus={'type': 'pci'}), True) - bus.insert(qdevice('dev2', params, parent_bus={'type': 'pci'}), True) - bus.insert(qdevice('dev3', params, parent_bus={'type': 'pci'}), True) - params = {'addr': '0x1f'} - bus.insert(qdevice('dev1', params, parent_bus={'type': 'pci'}), True) - params = {'addr': 30} - bus.insert(qdevice('dev1', params, parent_bus={'type': 'pci'}), True) - params = {'addr': 12} - bus.insert(qdevice('dev1', params, parent_bus={'type': 'pci'}), True) + bus.insert(qdevice("dev1", params, parent_bus={"type": "pci"}), True) + bus.insert(qdevice("dev2", params, parent_bus={"type": "pci"}), True) + bus.insert(qdevice("dev3", params, parent_bus={"type": "pci"}), True) + params = {"addr": "0x1f"} + bus.insert(qdevice("dev1", params, parent_bus={"type": "pci"}), True) + params = {"addr": 30} + bus.insert(qdevice("dev1", params, parent_bus={"type": "pci"}), True) + params = {"addr": 12} + bus.insert(qdevice("dev1", params, parent_bus={"type": "pci"}), True) # All devices will have 'addr' set as we are in the strict mode exp = """Bus pci.0, type=pci @@ -485,79 +598,82 @@ def test_q_pci_bus_strict(self): bus = pci.0 """ out = str(bus.str_long()) - self.assertEqual(out, exp, "Long representation corrupted:\n%s\n%s" - % (out, exp)) + self.assertEqual( + out, exp, "Long representation corrupted:\n%s\n%s" % (out, exp) + ) def test_usb_bus(self): - """ Tests the specific handlings of QUSBBus """ - usbc1 = qdevices.QUSBBus(2, 'usb1.0', 'uhci') + """Tests the specific handlings of QUSBBus""" + usbc1 = qdevices.QUSBBus(2, "usb1.0", "uhci") # Insert device into usb controller, default port - dev = qdevices.QDevice('usb-kbd', parent_bus={'type': 'uhci'}) + dev = qdevices.QDevice("usb-kbd", parent_bus={"type": "uhci"}) assert usbc1.insert(dev) == [] # Insert usb-hub into usb controller, default port - dev = qdevices.QDevice('usb-hub', parent_bus={'type': 'uhci'}) + dev = qdevices.QDevice("usb-hub", parent_bus={"type": "uhci"}) assert usbc1.insert(dev) == [] hub1 = dev.child_bus[-1] # Insert usb-hub into usb-hub, exact port - dev = qdevices.QDevice('usb-hub', {'port': '2.4'}, - parent_bus={'type': 'uhci'}) + dev = qdevices.QDevice("usb-hub", {"port": "2.4"}, parent_bus={"type": "uhci"}) assert hub1.insert(dev) == [] hub2 = dev.child_bus[-1] # Insert usb-hub into usb-hub in usb-hub, exact port - dev = qdevices.QDevice('usb-hub', {'port': '2.4.3'}, - parent_bus={'type': 'uhci'}) + dev = qdevices.QDevice( + "usb-hub", {"port": "2.4.3"}, parent_bus={"type": "uhci"} + ) assert hub2.insert(dev) == [] hub3 = dev.child_bus[-1] # verify that port is updated correctly self.assertEqual("2.4.3", dev.get_param("port")) # Insert usb-device into usb-hub in usb-hub in usb-hub, exact port - dev = qdevices.QDevice('usb-kbd', {'port': '2.4.3.1'}, - parent_bus={'type': 'uhci'}) + dev = qdevices.QDevice( + "usb-kbd", {"port": "2.4.3.1"}, parent_bus={"type": "uhci"} + ) assert hub3.insert(dev) == [] # Insert usb-device into usb-hub in usb-hub in usb-hub, default port - dev = qdevices.QDevice('usb-kbd', parent_bus={'type': 'uhci'}) + dev = qdevices.QDevice("usb-kbd", parent_bus={"type": "uhci"}) assert hub3.insert(dev) == [] # Try to insert device into specific port which belongs to inferior bus - out = hub2.insert(qdevices.QDevice('usb-kbd', - {'port': '2.4.3.3'}, - parent_bus={'type': 'uhci'})) + out = hub2.insert( + qdevices.QDevice( + "usb-kbd", {"port": "2.4.3.3"}, parent_bus={"type": "uhci"} + ) + ) assert out == "BusId" # Try to insert device into specific port which belongs to superior bus - out = hub2.insert(qdevices.QDevice('usb-kbd', {'port': '2.4'}, - parent_bus={'type': 'uhci'})) + out = hub2.insert( + qdevices.QDevice("usb-kbd", {"port": "2.4"}, parent_bus={"type": "uhci"}) + ) assert out == "BusId" # Try to insert device into specific port which belongs to same level # but different port - out = hub2.insert(qdevices.QDevice('usb-kbd', {'port': '2.3.4'}, - parent_bus={'type': 'uhci'})) + out = hub2.insert( + qdevices.QDevice("usb-kbd", {"port": "2.3.4"}, parent_bus={"type": "uhci"}) + ) assert out == "BusId" # Force insert device with port which belongs to other hub - dev = qdevices.QDevice('usb-hub', {'port': '2.4.3.4'}, - parent_bus={'type': 'uhci'}) + dev = qdevices.QDevice( + "usb-hub", {"port": "2.4.3.4"}, parent_bus={"type": "uhci"} + ) # Check the overall buses correctness - self.assertEqual("usb1.0(uhci): {1:a'usb-kbd',2:a'usb-hub'}", - usbc1.str_short()) - self.assertEqual("usb1.0(uhci): {4:a'usb-hub'}", - hub1.str_short()) - self.assertEqual("usb1.0(uhci): {3:a'usb-hub'}", - hub2.str_short()) - self.assertEqual("usb1.0(uhci): {1:a'usb-kbd',2:a'usb-kbd'}", - hub3.str_short()) + self.assertEqual("usb1.0(uhci): {1:a'usb-kbd',2:a'usb-hub'}", usbc1.str_short()) + self.assertEqual("usb1.0(uhci): {4:a'usb-hub'}", hub1.str_short()) + self.assertEqual("usb1.0(uhci): {3:a'usb-hub'}", hub2.str_short()) + self.assertEqual("usb1.0(uhci): {1:a'usb-kbd',2:a'usb-kbd'}", hub3.str_short()) class Container(unittest.TestCase): - """ Tests related to the abstract representation of qemu machine """ + """Tests related to the abstract representation of qemu machine""" def setUp(self): self.god = mock.mock_god(ut=self) @@ -566,69 +682,69 @@ def setUp(self): def tearDown(self): self.god.unstub_all() - def create_qdev(self, vm_name='vm1', strict_mode="no", - allow_hotplugged_vm="yes"): - """ :return: Initialized qcontainer.DevContainer object """ - qemu_cmd = '/usr/bin/qemu_kvm' - qcontainer.process.system_output.expect_call('%s -help' % qemu_cmd, - timeout=10, - ignore_status=True, - shell=True, - verbose=False - ).and_return(QEMU_HELP) - qcontainer.process.system_output.expect_call("%s -device \? 2>&1" - % qemu_cmd, timeout=10, - ignore_status=True, - shell=True, - verbose=False - ).and_return(QEMU_DEVICES) - qcontainer.process.system_output.expect_call("%s -M \?" % qemu_cmd, - timeout=10, - ignore_status=True, - shell=True, - verbose=False - ).and_return(QEMU_MACHINE) + def create_qdev(self, vm_name="vm1", strict_mode="no", allow_hotplugged_vm="yes"): + """:return: Initialized qcontainer.DevContainer object""" + qemu_cmd = "/usr/bin/qemu_kvm" + qcontainer.process.system_output.expect_call( + "%s -help" % qemu_cmd, + timeout=10, + ignore_status=True, + shell=True, + verbose=False, + ).and_return(QEMU_HELP) + qcontainer.process.system_output.expect_call( + "%s -device \? 2>&1" % qemu_cmd, + timeout=10, + ignore_status=True, + shell=True, + verbose=False, + ).and_return(QEMU_DEVICES) + qcontainer.process.system_output.expect_call( + "%s -M \?" % qemu_cmd, + timeout=10, + ignore_status=True, + shell=True, + verbose=False, + ).and_return(QEMU_MACHINE) cmd = "echo -e 'help\nquit' | %s -monitor stdio -vnc none" % qemu_cmd - qcontainer.process.system_output.expect_call(cmd, timeout=10, - ignore_status=True, - shell=True, - verbose=False - ).and_return(QEMU_HMP) - cmd = ('echo -e \'{ "execute": "qmp_capabilities" }\n' - '{ "execute": "query-commands", "id": "RAND91" }\n' - '{ "execute": "quit" }\'' - '| %s -qmp stdio -vnc none | grep return |' - ' grep RAND91' % qemu_cmd) - qcontainer.process.system_output.expect_call(cmd, timeout=10, - ignore_status=True, - shell=True, - verbose=False - ).and_return('') - - cmd = ('echo -e \'{ "execute": "qmp_capabilities" }\n' - '{ "execute": "query-commands", "id": "RAND91" }\n' - '{ "execute": "quit" }\' | (sleep 1; cat )' - '| %s -qmp stdio -vnc none | grep return |' - ' grep RAND91' % qemu_cmd) - qcontainer.process.system_output.expect_call(cmd, timeout=10, - ignore_status=True, - shell=True, - verbose=False - ).and_return(QEMU_QMP) - - qdev = qcontainer.DevContainer(qemu_cmd, vm_name, strict_mode, 'no', - allow_hotplugged_vm) + qcontainer.process.system_output.expect_call( + cmd, timeout=10, ignore_status=True, shell=True, verbose=False + ).and_return(QEMU_HMP) + cmd = ( + 'echo -e \'{ "execute": "qmp_capabilities" }\n' + '{ "execute": "query-commands", "id": "RAND91" }\n' + '{ "execute": "quit" }\'' + "| %s -qmp stdio -vnc none | grep return |" + " grep RAND91" % qemu_cmd + ) + qcontainer.process.system_output.expect_call( + cmd, timeout=10, ignore_status=True, shell=True, verbose=False + ).and_return("") + + cmd = ( + 'echo -e \'{ "execute": "qmp_capabilities" }\n' + '{ "execute": "query-commands", "id": "RAND91" }\n' + '{ "execute": "quit" }\' | (sleep 1; cat )' + "| %s -qmp stdio -vnc none | grep return |" + " grep RAND91" % qemu_cmd + ) + qcontainer.process.system_output.expect_call( + cmd, timeout=10, ignore_status=True, shell=True, verbose=False + ).and_return(QEMU_QMP) + + qdev = qcontainer.DevContainer( + qemu_cmd, vm_name, strict_mode, "no", allow_hotplugged_vm + ) self.god.check_playback() return qdev def test_qdev_functional(self): - """ Test basic qdev workflow """ - qdev = self.create_qdev('vm1') + """Test basic qdev workflow""" + qdev = self.create_qdev("vm1") # Add basic 'pc' devices - out = qdev.insert(qdev.machine_by_params(ParamsDict({'machine_type': - 'pc'}))) + out = qdev.insert(qdev.machine_by_params(ParamsDict({"machine_type": "pc"}))) assert isinstance(out, list) assert len(out) == 6, len(out) @@ -682,85 +798,98 @@ def test_qdev_functional(self): child_bus = \[.*QFloppyBus.*\] params:""" out = qdev.str_long() - self.assertNotEqual(re.findall(exp, out), None, 'Long representation is' - 'corrupted:\n%s\n%s' % (out, exp)) - - exp = ("Buses of vm1\n" - " floppy(floppy): [None,None]\n" - " ide(ide): [None,None,None,None]\n" - " _PCI_CHASSIS_NR(None): {}\n" - " _PCI_CHASSIS(None): {}\n" - " pci.0(PCI): {0x0-0x0:t'i440FX',0x1-0x0:t'PIIX3'," - "0x1-0x1:t'piix3-ide',0x1-0x3:t'PIIX4_PM'}") + self.assertNotEqual( + re.findall(exp, out), + None, + "Long representation is" "corrupted:\n%s\n%s" % (out, exp), + ) + + exp = ( + "Buses of vm1\n" + " floppy(floppy): [None,None]\n" + " ide(ide): [None,None,None,None]\n" + " _PCI_CHASSIS_NR(None): {}\n" + " _PCI_CHASSIS(None): {}\n" + " pci.0(PCI): {0x0-0x0:t'i440FX',0x1-0x0:t'PIIX3'," + "0x1-0x1:t'piix3-ide',0x1-0x3:t'PIIX4_PM'}" + ) out = qdev.str_bus_short() - assert out == exp, "Bus representation is ocrrupted:\n%s\n%s" % (out, - exp) + assert out == exp, "Bus representation is ocrrupted:\n%s\n%s" % (out, exp) # Insert some good devices qdevice = qdevices.QDevice # Device with child bus - bus = qdevices.QSparseBus('bus', [['addr'], [6]], 'hba1.0', 'hba', - 'a_hba') - dev = qdevice('HBA', {'id': 'hba1', 'addr': 10}, - parent_bus={'aobject': 'pci.0'}, child_bus=bus) + bus = qdevices.QSparseBus("bus", [["addr"], [6]], "hba1.0", "hba", "a_hba") + dev = qdevice( + "HBA", + {"id": "hba1", "addr": 10}, + parent_bus={"aobject": "pci.0"}, + child_bus=bus, + ) out = qdev.insert(dev) assert isinstance(out, list), out assert len(out) == 1, len(out) # Device inside a child bus by type (most common) - dev = qdevice('dev', {}, parent_bus={'type': 'hba'}) + dev = qdevice("dev", {}, parent_bus={"type": "hba"}) out = qdev.insert(dev) assert isinstance(out, list), out assert len(out) == 1, len(out) # Device inside a child bus by autotest_id - dev = qdevice('dev', {}, 'autotest_remove', {'aobject': 'a_hba'}) + dev = qdevice("dev", {}, "autotest_remove", {"aobject": "a_hba"}) out = qdev.insert(dev) assert isinstance(out, list), out assert len(out) == 1, len(out) # Device inside a child bus by busid - dev = qdevice('dev', {}, 'autoremove', {'busid': 'hba1.0'}) + dev = qdevice("dev", {}, "autoremove", {"busid": "hba1.0"}) out = qdev.insert(dev) assert isinstance(out, list), out assert len(out) == 1, len(out) # Check the representation - exp = ("Devices of vm1: [t'machine',t'i440FX',t'PIIX4_PM',t'PIIX3'," - "t'piix3-ide',t'fdc',hba1,a'dev',a'dev',a'dev']") + exp = ( + "Devices of vm1: [t'machine',t'i440FX',t'PIIX4_PM',t'PIIX3'," + "t'piix3-ide',t'fdc',hba1,a'dev',a'dev',a'dev']" + ) out = qdev.str_short() - self.assertEqual(out, exp, "Short representation is corrupted:\n%s\n%s" - % (out, exp)) - exp = ("Buses of vm1\n" - " hba1.0(hba): {0:a'dev',1:a'dev',2:a'dev'}\n" - " floppy(floppy): [None,None]\n" - " ide(ide): [None,None,None,None]\n" - " _PCI_CHASSIS_NR(None): {}\n" - " _PCI_CHASSIS(None): {}\n" - " pci.0(PCI): {0x0-0x0:t'i440FX',0x1-0x0:t'PIIX3'," - "0x1-0x1:t'piix3-ide',0x1-0x3:t'PIIX4_PM',0xa-0x0:hba1}") + self.assertEqual( + out, exp, "Short representation is corrupted:\n%s\n%s" % (out, exp) + ) + exp = ( + "Buses of vm1\n" + " hba1.0(hba): {0:a'dev',1:a'dev',2:a'dev'}\n" + " floppy(floppy): [None,None]\n" + " ide(ide): [None,None,None,None]\n" + " _PCI_CHASSIS_NR(None): {}\n" + " _PCI_CHASSIS(None): {}\n" + " pci.0(PCI): {0x0-0x0:t'i440FX',0x1-0x0:t'PIIX3'," + "0x1-0x1:t'piix3-ide',0x1-0x3:t'PIIX4_PM',0xa-0x0:hba1}" + ) out = qdev.str_bus_short() - assert out == exp, 'Bus representation iscorrupted:\n%s\n%s' % (out, - exp) + assert out == exp, "Bus representation iscorrupted:\n%s\n%s" % (out, exp) # Check the representation - exp = ("Devices of vm1: [t'machine',t'i440FX',t'PIIX4_PM',t'PIIX3'," - "t'piix3-ide',t'fdc',hba1,a'dev',a'dev',a'dev']") + exp = ( + "Devices of vm1: [t'machine',t'i440FX',t'PIIX4_PM',t'PIIX3'," + "t'piix3-ide',t'fdc',hba1,a'dev',a'dev',a'dev']" + ) out = qdev.str_short() - assert out == exp, "Short representation is corrupted:\n%s\n%s" % (out, - exp) - exp = ("Buses of vm1\n" - " hba1.0(hba): {0:a'dev',1:a'dev',2:a'dev'}\n" - " floppy(floppy): [None,None]\n" - " ide(ide): [None,None,None,None]\n" - " _PCI_CHASSIS_NR(None): {}\n" - " _PCI_CHASSIS(None): {}\n" - " pci.0(PCI): {0x0-0x0:t'i440FX',0x1-0x0:t'PIIX3'," - "0x1-0x1:t'piix3-ide',0x1-0x3:t'PIIX4_PM',0xa-0x0:hba1}") + assert out == exp, "Short representation is corrupted:\n%s\n%s" % (out, exp) + exp = ( + "Buses of vm1\n" + " hba1.0(hba): {0:a'dev',1:a'dev',2:a'dev'}\n" + " floppy(floppy): [None,None]\n" + " ide(ide): [None,None,None,None]\n" + " _PCI_CHASSIS_NR(None): {}\n" + " _PCI_CHASSIS(None): {}\n" + " pci.0(PCI): {0x0-0x0:t'i440FX',0x1-0x0:t'PIIX3'," + "0x1-0x1:t'piix3-ide',0x1-0x3:t'PIIX4_PM',0xa-0x0:hba1}" + ) out = qdev.str_bus_short() - assert out == exp, 'Bus representation is corrupted:\n%s\n%s' % (out, - exp) + assert out == exp, "Bus representation is corrupted:\n%s\n%s" % (out, exp) # Now representation contains some devices, play with it a bit # length @@ -768,143 +897,160 @@ def test_qdev_functional(self): assert out == 10, "Length of qdev is incorrect: %s != %s" % (out, 10) # compare - qdev2 = self.create_qdev('vm1') - self.assertNotEqual(qdev, qdev2, "This qdev matches empty one:" - "\n%s\n%s" % (qdev, qdev2)) - self.assertNotEqual(qdev2, qdev, "Empty qdev matches current one:" - "\n%s\n%s" % (qdev, qdev2)) + qdev2 = self.create_qdev("vm1") + self.assertNotEqual( + qdev, qdev2, "This qdev matches empty one:" "\n%s\n%s" % (qdev, qdev2) + ) + self.assertNotEqual( + qdev2, qdev, "Empty qdev matches current one:" "\n%s\n%s" % (qdev, qdev2) + ) for _ in xrange(10): qdev2.insert(qdevice()) - self.assertNotEqual(qdev, qdev2, "This qdev matches different one:" - "\n%s\n%s" % (qdev, qdev2)) - self.assertNotEqual(qdev2, qdev, "Other qdev matches this one:\n%s\n%s" - % (qdev, qdev2)) + self.assertNotEqual( + qdev, qdev2, "This qdev matches different one:" "\n%s\n%s" % (qdev, qdev2) + ) + self.assertNotEqual( + qdev2, qdev, "Other qdev matches this one:\n%s\n%s" % (qdev, qdev2) + ) # cmdline - exp = ("-machine pc -device HBA,id=hba1,addr=0xa,bus=pci.0 -device dev " - "-device dev -device dev") + exp = ( + "-machine pc -device HBA,id=hba1,addr=0xa,bus=pci.0 -device dev " + "-device dev -device dev" + ) out = qdev.cmdline() - self.assertEqual(out, exp, 'Corrupted qdev.cmdline() output:\n%s\n%s' - % (out, exp)) + self.assertEqual( + out, exp, "Corrupted qdev.cmdline() output:\n%s\n%s" % (out, exp) + ) # get_by_qid (currently we have 2 devices of the same qid) - out = qdev.get_by_qid('hba1') - self.assertEqual(len(out), 1, 'Incorrect number of devices by qid ' - '"hba1": %s != 1\n%s' % (len(out), qdev.str_long())) + out = qdev.get_by_qid("hba1") + self.assertEqual( + len(out), + 1, + "Incorrect number of devices by qid " + '"hba1": %s != 1\n%s' % (len(out), qdev.str_long()), + ) # Remove some devices # Remove based on aid # pylint: disable=E1111 - out = qdev.remove('__6') - self.assertEqual(out, None, 'Failed to remove device:\n%s\nRepr:\n%s' - % ('hba1__0', qdev.str_long())) + out = qdev.remove("__6") + self.assertEqual( + out, + None, + "Failed to remove device:\n%s\nRepr:\n%s" % ("hba1__0", qdev.str_long()), + ) # Remove device which contains other devices (without recursive) - self.assertRaises(qcontainer.DeviceRemoveError, qdev.remove, 'hba1', - False) + self.assertRaises(qcontainer.DeviceRemoveError, qdev.remove, "hba1", False) # Remove device which contains other devices (recursive) # pylint: disable=E1111 - out = qdev.remove('hba1') - self.assertEqual(out, None, 'Failed to remove device:\n%s\nRepr:\n%s' - % ('hba1', qdev.str_long())) + out = qdev.remove("hba1") + self.assertEqual( + out, + None, + "Failed to remove device:\n%s\nRepr:\n%s" % ("hba1", qdev.str_long()), + ) # Check the representation - exp = ("Devices of vm1: [t'machine',t'i440FX',t'PIIX4_PM',t'PIIX3'," - "t'piix3-ide',t'fdc']") + exp = ( + "Devices of vm1: [t'machine',t'i440FX',t'PIIX4_PM',t'PIIX3'," + "t'piix3-ide',t'fdc']" + ) out = qdev.str_short() - assert out == exp, "Short representation is corrupted:\n%s\n%s" % (out, - exp) - exp = ("Buses of vm1\n" - " floppy(floppy): [None,None]\n" - " ide(ide): [None,None,None,None]\n" - " _PCI_CHASSIS_NR(None): {}\n" - " _PCI_CHASSIS(None): {}\n" - " pci.0(PCI): {0x0-0x0:t'i440FX',0x1-0x0:t'PIIX3'," - "0x1-0x1:t'piix3-ide',0x1-0x3:t'PIIX4_PM'}") + assert out == exp, "Short representation is corrupted:\n%s\n%s" % (out, exp) + exp = ( + "Buses of vm1\n" + " floppy(floppy): [None,None]\n" + " ide(ide): [None,None,None,None]\n" + " _PCI_CHASSIS_NR(None): {}\n" + " _PCI_CHASSIS(None): {}\n" + " pci.0(PCI): {0x0-0x0:t'i440FX',0x1-0x0:t'PIIX3'," + "0x1-0x1:t'piix3-ide',0x1-0x3:t'PIIX4_PM'}" + ) out = qdev.str_bus_short() - assert out == exp, 'Bus representation is corrupted:\n%s\n%s' % (out, - exp) + assert out == exp, "Bus representation is corrupted:\n%s\n%s" % (out, exp) def test_qdev_hotplug(self): - """ Test the hotplug/unplug functionality """ - qdev = self.create_qdev('vm1', False, True) - devs = qdev.machine_by_params(ParamsDict({'machine_type': 'pc'})) + """Test the hotplug/unplug functionality""" + qdev = self.create_qdev("vm1", False, True) + devs = qdev.machine_by_params(ParamsDict({"machine_type": "pc"})) for dev in devs: qdev.insert(dev) monitor = MockHMPMonitor() out = qdev.get_state() - assert out == -1, ("Status after init is not -1" - " (%s)" % out) + assert out == -1, "Status after init is not -1" " (%s)" % out out = len(qdev) assert out == 6, "Number of devices of this VM is not 5 (%s)" % out - devs = qdev.images_define_by_variables('disk', '/tmp/a', - {'aobject': 'pci.0'}, - fmt="virtio") + devs = qdev.images_define_by_variables( + "disk", "/tmp/a", {"aobject": "pci.0"}, fmt="virtio" + ) dev1, dev2 = devs[0], devs[1] out = dev1.hotplug_hmp() exp = "drive_add auto id=drive_disk,if=none,file=/tmp/a" - assert out == exp, ("Hotplug command of drive is incorrect:\n%s\n%s" - % (exp, out)) + assert out == exp, "Hotplug command of drive is incorrect:\n%s\n%s" % (exp, out) # hotplug of drive will return " OK" (pass) dev1.hotplug = lambda _monitor: "OK" dev1.verify_hotplug = lambda _out, _monitor: True out, ver_out = qdev.simple_hotplug(dev1, monitor) assert out == "OK", "Return value of hotplug is not OK (%s)" % out - assert ver_out is True, ("Return value of hotplug" - " is not True (%s)" % ver_out) + assert ver_out is True, "Return value of hotplug" " is not True (%s)" % ver_out out = qdev.get_state() - assert out == 0, ("Status after verified hotplug is not 0 (%s)" % out) + assert out == 0, "Status after verified hotplug is not 0 (%s)" % out # hotplug of virtio-blk-pci will return "" out = dev2.hotplug_hmp() exp = "device_add virtio-blk-pci,id=disk,drive=drive_disk" - assert out == exp, ("Hotplug command of device is incorrect:\n%s\n%s" - % (exp, out)) + assert out == exp, "Hotplug command of device is incorrect:\n%s\n%s" % ( + exp, + out, + ) dev2.hotplug = lambda _monitor: "" dev2.verify_hotplug = lambda _out, _monitor: "" out, ver_out = qdev.simple_hotplug(dev2, monitor) # automatic verification is not supported, hotplug returns the original # monitor message ("") - assert ver_out == "", ("Return value of hotplug is" - " not "" (%s)" % ver_out) + assert ver_out == "", "Return value of hotplug is" " not " " (%s)" % ver_out assert out == "", 'Return value of hotplug is not "" (%s)' % out out = qdev.get_state() - assert out == 1, ("Status after verified hotplug is not 1 (%s)" % out) + assert out == 1, "Status after verified hotplug is not 1 (%s)" % out qdev.hotplug_verified() out = qdev.get_state() - assert out == 0, ("Status after verified hotplug is not 0 (%s)" % out) + assert out == 0, "Status after verified hotplug is not 0 (%s)" % out out = len(qdev) assert out == 8, "Number of devices of this VM is not 8 (%s)" % out # Hotplug is expected to pass but monitor reports failure - dev3 = qdevices.QDrive('a_dev1') - dev3.hotplug = lambda _monitor: ("could not open disk image /tmp/qqq: " - "No such file or directory") + dev3 = qdevices.QDrive("a_dev1") + dev3.hotplug = lambda _monitor: ( + "could not open disk image /tmp/qqq: " "No such file or directory" + ) out, ver_out = qdev.simple_hotplug(dev3, monitor) exp = "could not open disk image /tmp/qqq: No such file or directory" - assert out, "Return value of hotplug is incorrect:\n%s\n%s" % (out, - exp) + assert out, "Return value of hotplug is incorrect:\n%s\n%s" % (out, exp) out = qdev.get_state() - assert out == 1, ("Status after failed hotplug is not 1 (%s)" % out) + assert out == 1, "Status after failed hotplug is not 1 (%s)" % out # device is still in qdev, but is not in qemu, we should remove it qdev.remove(dev3, recursive=False) out = qdev.get_state() - assert out == 1, ("Status after verified hotplug is not 1 (%s)" % out) + assert out == 1, "Status after verified hotplug is not 1 (%s)" % out qdev.hotplug_verified() out = qdev.get_state() - assert out == 0, ("Status after verified hotplug is not 0 (%s)" % out) + assert out == 0, "Status after verified hotplug is not 0 (%s)" % out # Hotplug is expected to fail (missing bus XXX) - dev4 = qdevices.QBaseDevice("bad_dev", parent_bus={'type': "XXX"}) + dev4 = qdevices.QBaseDevice("bad_dev", parent_bus={"type": "XXX"}) dev4.hotplug = lambda _monitor: ("") dev4.cmdline = lambda: "-device bad_device,id=fooBarBaz" - self.assertRaises(qcontainer.DeviceHotplugError, qdev.simple_hotplug, - dev4, True) + self.assertRaises( + qcontainer.DeviceHotplugError, qdev.simple_hotplug, dev4, True + ) out = qdev.get_state() assert out == 1, "Status after impossible hotplug is not 0 (%s)" % out # We should check the DeviceHotplugError.ver_out if it failed, let's @@ -915,25 +1061,27 @@ def test_qdev_hotplug(self): # Unplug used drive (automatic verification not supported) out = dev1.unplug_hmp() exp = "drive_del drive_disk" - assert out == exp, ("Hotplug command of device is incorrect:\n%s\n%s" - % (exp, out)) + assert out == exp, "Hotplug command of device is incorrect:\n%s\n%s" % ( + exp, + out, + ) dev1.unplug = lambda _monitor: "" dev1.verify_unplug = lambda _monitor, _out: "" out, ver_out = qdev.simple_unplug(dev1, monitor) # I verified, that device was unplugged successfully qdev.hotplug_verified() out = qdev.get_state() - assert out == 0, ("Status after verified hotplug is not 0 (%s)" % out) + assert out == 0, "Status after verified hotplug is not 0 (%s)" % out out = len(qdev) assert out == 7, "Number of devices of this VM is not 7 (%s)" % out # Removal of drive should also set drive of the disk device to None - out = dev2.get_param('drive') + out = dev2.get_param("drive") assert out is None, "Drive was not removed from disk device" # pylint: disable=W0212 def test_qdev_low_level(self): - """ Test low level functions """ - qdev = self.create_qdev('vm1') + """Test low level functions""" + qdev = self.create_qdev("vm1") # Representation state (used for hotplug or other nasty things) out = qdev.get_state() @@ -963,25 +1111,25 @@ def test_qdev_low_level(self): dev = qdevices.QDevice() qdev.insert(dev) out = dev.get_aid() - self.assertEqual(out, '__0', "incorrect aid %s != %s" % (out, '__0')) + self.assertEqual(out, "__0", "incorrect aid %s != %s" % (out, "__0")) - dev = qdevices.QDevice(None, {'id': 'qid'}) + dev = qdevices.QDevice(None, {"id": "qid"}) qdev.insert(dev) out = dev.get_aid() - self.assertEqual(out, 'qid', "incorrect aid %s != %s" % (out, 'qid')) + self.assertEqual(out, "qid", "incorrect aid %s != %s" % (out, "qid")) # has_option - out = qdev.has_option('device') + out = qdev.has_option("device") self.assertEqual(out, True) - out = qdev.has_option('missing_option') + out = qdev.has_option("missing_option") self.assertEqual(out, False) # has_device - out = qdev.has_device('ide-drive') + out = qdev.has_device("ide-drive") self.assertEqual(out, True) - out = qdev.has_device('missing_device') + out = qdev.has_device("missing_device") self.assertEqual(out, False) # get_help_text @@ -989,120 +1137,147 @@ def test_qdev_low_level(self): self.assertEqual(out, QEMU_HELP) # has_hmp_cmd - self.assertTrue(qdev.has_hmp_cmd('pcie_aer_inject_error')) - self.assertTrue(qdev.has_hmp_cmd('c')) - self.assertTrue(qdev.has_hmp_cmd('cont')) - self.assertFalse(qdev.has_hmp_cmd('off')) - self.assertFalse(qdev.has_hmp_cmd('\ndump-guest-memory')) - self.assertFalse(qdev.has_hmp_cmd('The')) + self.assertTrue(qdev.has_hmp_cmd("pcie_aer_inject_error")) + self.assertTrue(qdev.has_hmp_cmd("c")) + self.assertTrue(qdev.has_hmp_cmd("cont")) + self.assertFalse(qdev.has_hmp_cmd("off")) + self.assertFalse(qdev.has_hmp_cmd("\ndump-guest-memory")) + self.assertFalse(qdev.has_hmp_cmd("The")) # has_qmp_cmd - self.assertTrue(qdev.has_qmp_cmd('device_add')) - self.assertFalse(qdev.has_qmp_cmd('RAND91')) + self.assertTrue(qdev.has_qmp_cmd("device_add")) + self.assertFalse(qdev.has_qmp_cmd("RAND91")) # Add some buses - bus1 = qdevices.QPCIBus('pci.0', 'pci', 'a_pci0') - qdev.insert(qdevices.QDevice(params={'id': 'pci0'}, - child_bus=bus1)) - bus2 = qdevices.QPCIBus('pci.1', 'pci', 'a_pci1') + bus1 = qdevices.QPCIBus("pci.0", "pci", "a_pci0") + qdev.insert(qdevices.QDevice(params={"id": "pci0"}, child_bus=bus1)) + bus2 = qdevices.QPCIBus("pci.1", "pci", "a_pci1") qdev.insert(qdevices.QDevice(child_bus=bus2)) - bus3 = qdevices.QPCIBus('pci.2', 'pci', 'a_pci2') + bus3 = qdevices.QPCIBus("pci.2", "pci", "a_pci2") qdev.insert(qdevices.QDevice(child_bus=bus3)) - bus4 = qdevices.QPCIBus('pcie.0', 'pcie', 'a_pcie0') + bus4 = qdevices.QPCIBus("pcie.0", "pcie", "a_pcie0") qdev.insert(qdevices.QDevice(child_bus=bus4)) # get_buses (all buses of this type) - out = qdev.get_buses({'type': 'pci'}) - self.assertEqual(len(out), 3, 'get_buses should return 3 buses but ' - 'returned %s instead:\n%s' % (len(out), out)) + out = qdev.get_buses({"type": "pci"}) + self.assertEqual( + len(out), + 3, + "get_buses should return 3 buses but " + "returned %s instead:\n%s" % (len(out), out), + ) # get_first_free_bus (last added bus of this type) - out = qdev.get_first_free_bus({'type': 'pci'}, [None, None]) + out = qdev.get_first_free_bus({"type": "pci"}, [None, None]) self.assertEqual(bus3, out) # fill the first pci bus for _ in xrange(32): - qdev.insert(qdevices.QDevice(parent_bus={'type': 'pci'})) + qdev.insert(qdevices.QDevice(parent_bus={"type": "pci"})) # get_first_free_bus (last one is full, return the previous one) - out = qdev.get_first_free_bus({'type': 'pci'}, [None, None]) + out = qdev.get_first_free_bus({"type": "pci"}, [None, None]) self.assertEqual(bus2, out) # list_named_buses - out = qdev.list_missing_named_buses('pci.', 'pci', 5) - self.assertEqual(len(out), 2, 'Number of missing named buses is ' - 'incorrect: %s != %s\n%s' % (len(out), 2, out)) - out = qdev.list_missing_named_buses('pci.', 'abc', 5) - self.assertEqual(len(out), 5, 'Number of missing named buses is ' - 'incorrect: %s != %s\n%s' % (len(out), 2, out)) + out = qdev.list_missing_named_buses("pci.", "pci", 5) + self.assertEqual( + len(out), + 2, + "Number of missing named buses is " + "incorrect: %s != %s\n%s" % (len(out), 2, out), + ) + out = qdev.list_missing_named_buses("pci.", "abc", 5) + self.assertEqual( + len(out), + 5, + "Number of missing named buses is " + "incorrect: %s != %s\n%s" % (len(out), 2, out), + ) # idx_of_next_named_bus - out = qdev.idx_of_next_named_bus('pci.') - self.assertEqual(out, 3, 'Incorrect idx of next named bus: %s !=' - ' %s' % (out, 3)) + out = qdev.idx_of_next_named_bus("pci.") + self.assertEqual( + out, 3, "Incorrect idx of next named bus: %s !=" " %s" % (out, 3) + ) # get_children - dev = qdevices.QDevice(parent_bus={'aobject': 'a_pci0'}) - bus = qdevices.QPCIBus('test1', 'test', 'a_test1') + dev = qdevices.QDevice(parent_bus={"aobject": "a_pci0"}) + bus = qdevices.QPCIBus("test1", "test", "a_test1") dev.add_child_bus(bus) - bus = qdevices.QPCIBus('test2', 'test', 'a_test2') + bus = qdevices.QPCIBus("test2", "test", "a_test2") dev.add_child_bus(bus) qdev.insert(dev) - qdev.insert(qdevices.QDevice(parent_bus={'aobject': 'a_test1'})) - qdev.insert(qdevices.QDevice(parent_bus={'aobject': 'a_test2'})) + qdev.insert(qdevices.QDevice(parent_bus={"aobject": "a_test1"})) + qdev.insert(qdevices.QDevice(parent_bus={"aobject": "a_test2"})) out = dev.get_children() - assert len(out) == 2, ("Not all children were listed %d != 2:\n%s" - % (len(out), out)) + assert len(out) == 2, "Not all children were listed %d != 2:\n%s" % ( + len(out), + out, + ) out = bus.get_device() - assert out == dev, ("bus.get_device() returned different device " - "than the one in which it was plugged:\n" - "%s\n%s\n%s" % (out.str_long(), dev.str_long(), - qdev.str_long())) + assert out == dev, ( + "bus.get_device() returned different device " + "than the one in which it was plugged:\n" + "%s\n%s\n%s" % (out.str_long(), dev.str_long(), qdev.str_long()) + ) def test_qdev_equal(self): - qdev1 = self.create_qdev('vm1', allow_hotplugged_vm='no') - qdev2 = self.create_qdev('vm1', allow_hotplugged_vm='no') - qdev3 = self.create_qdev('vm1', allow_hotplugged_vm='yes') + qdev1 = self.create_qdev("vm1", allow_hotplugged_vm="no") + qdev2 = self.create_qdev("vm1", allow_hotplugged_vm="no") + qdev3 = self.create_qdev("vm1", allow_hotplugged_vm="yes") monitor = MockHMPMonitor() - assert qdev1 == qdev2, ("Init qdevs are not alike\n%s\n%s" - % (qdev1.str_long(), qdev2.str_long())) + assert qdev1 == qdev2, "Init qdevs are not alike\n%s\n%s" % ( + qdev1.str_long(), + qdev2.str_long(), + ) # Insert a device to qdev1 - dev = qdevices.QDevice('dev1', {'id': 'dev1'}) + dev = qdevices.QDevice("dev1", {"id": "dev1"}) qdev1.insert(dev) - assert qdev1 != qdev2, ("Different qdevs match:\n%s\n%s" - % (qdev1.str_long(), qdev2.str_long())) + assert qdev1 != qdev2, "Different qdevs match:\n%s\n%s" % ( + qdev1.str_long(), + qdev2.str_long(), + ) # Insert similar device to qdev2 - dev = qdevices.QDevice('dev1', {'id': 'dev1'}) + dev = qdevices.QDevice("dev1", {"id": "dev1"}) qdev2.insert(dev) - assert qdev1 == qdev2, ("Similar qdevs are not alike\n%s\n%s" - % (qdev1.str_long(), qdev2.str_long())) + assert qdev1 == qdev2, "Similar qdevs are not alike\n%s\n%s" % ( + qdev1.str_long(), + qdev2.str_long(), + ) # Hotplug similar device to qdev3 - dev = qdevices.QDevice('dev1', {'id': 'dev1'}) - dev.hotplug = lambda _monitor: "" # override the hotplug method + dev = qdevices.QDevice("dev1", {"id": "dev1"}) + dev.hotplug = lambda _monitor: "" # override the hotplug method dev.verify_hotplug = lambda _out, _monitor: True qdev3.simple_hotplug(dev, monitor) - assert qdev1 == qdev3, ("Similar hotplugged qdevs are not alike\n%s\n" - "%s" % (qdev1.str_long(), qdev2.str_long())) + assert qdev1 == qdev3, "Similar hotplugged qdevs are not alike\n%s\n" "%s" % ( + qdev1.str_long(), + qdev2.str_long(), + ) # Eq. is not symmetrical, qdev1 doesn't allow hotplugged VMs. - assert qdev3 != qdev1, ("Similar hotplugged qdevs match even thought " - "qdev1 doesn't allow hotplugged VM\n%s\n%s" - % (qdev1.str_long(), qdev2.str_long())) + assert qdev3 != qdev1, ( + "Similar hotplugged qdevs match even thought " + "qdev1 doesn't allow hotplugged VM\n%s\n%s" + % (qdev1.str_long(), qdev2.str_long()) + ) qdev2.__qemu_help = "I support only this :-)" # pylint: disable=W0212 - assert qdev1 == qdev2, ("qdevs of different qemu versions match:\n%s\n" - "%s" % (qdev1.str_long(), qdev2.str_long())) + assert qdev1 == qdev2, "qdevs of different qemu versions match:\n%s\n" "%s" % ( + qdev1.str_long(), + qdev2.str_long(), + ) def test_pci(self): - qdev = self.create_qdev('vm1') - devs = qdev.machine_by_params(ParamsDict({'machine_type': 'pc'})) + qdev = self.create_qdev("vm1") + devs = qdev.machine_by_params(ParamsDict({"machine_type": "pc"})) for dev in devs: qdev.insert(dev) # machine creates main pci (pci.0) @@ -1110,54 +1285,79 @@ def test_pci(self): # root.1: ioh3420(pci.0) # pci_switch: x3130(root.1) # pci_bridge: pci-bridge(root.1) - devs = qdev.pcic_by_params('root.1', {'pci_bus': 'pci.0', - 'type': 'ioh3420'}) + devs = qdev.pcic_by_params("root.1", {"pci_bus": "pci.0", "type": "ioh3420"}) qdev.insert(devs) - devs = qdev.pcic_by_params('pci_switch', {'pci_bus': 'root.1', - 'type': 'x3130'}) + devs = qdev.pcic_by_params("pci_switch", {"pci_bus": "root.1", "type": "x3130"}) qdev.insert(devs) - devs = qdev.pcic_by_params('pci_bridge', {'pci_bus': 'root.1', - 'type': 'pci-bridge'}) + devs = qdev.pcic_by_params( + "pci_bridge", {"pci_bus": "root.1", "type": "pci-bridge"} + ) qdev.insert(devs) - qdev.insert(qdevices.QDevice("ahci", {'id': 'in_bridge'}, - parent_bus={'type': ('PCI', 'PCIE'), - 'aobject': 'pci_bridge'})) - - qdev.insert(qdevices.QDevice("ahci", {'id': 'in_switch1'}, - parent_bus={'type': ('PCI', 'PCIE'), - 'aobject': 'pci_switch'})) - qdev.insert(qdevices.QDevice("ahci", {'id': 'in_switch2'}, - parent_bus={'type': ('PCI', 'PCIE'), - 'aobject': 'pci_switch'})) - qdev.insert(qdevices.QDevice("ahci", {'id': 'in_switch3'}, - parent_bus={'type': ('PCI', 'PCIE'), - 'aobject': 'pci_switch'})) - - qdev.insert(qdevices.QDevice("ahci", {'id': 'in_root1'}, - parent_bus={'type': ('PCI', 'PCIE'), - 'aobject': 'root.1'})) - - qdev.insert(qdevices.QDevice("ahci", {'id': 'in_pci.0'}, - parent_bus={'type': ('PCI', 'PCIE'), - 'aobject': 'pci.0'})) - - exp = ("-machine pc -device ioh3420,id=root.1,bus=pci.0,addr=0x2 " - "-device x3130-upstream,id=pci_switch,bus=root.1,addr=0x0 " - "-device pci-bridge,id=pci_bridge,bus=root.1,addr=0x1," - "chassis_nr=1 -device ahci,id=in_bridge,bus=pci_bridge,addr=0x1" - " -device xio3130-downstream,bus=pci_switch,id=pci_switch.0," - "addr=0x0,chassis=1 -device ahci,id=in_switch1,bus=pci_switch.0" - ",addr=0x0 " - "-device xio3130-downstream,bus=pci_switch,id=pci_switch.1," - "addr=0x1,chassis=2 -device ahci,id=in_switch2,bus=pci_switch.1" - ",addr=0x0 " - "-device xio3130-downstream,bus=pci_switch,id=pci_switch.2," - "addr=0x2,chassis=3 -device ahci,id=in_switch3,bus=pci_switch.2" - ",addr=0x0 " - "-device ahci,id=in_root1,bus=root.1,addr=0x2 " - "-device ahci,id=in_pci.0,bus=pci.0,addr=0x3") + qdev.insert( + qdevices.QDevice( + "ahci", + {"id": "in_bridge"}, + parent_bus={"type": ("PCI", "PCIE"), "aobject": "pci_bridge"}, + ) + ) + + qdev.insert( + qdevices.QDevice( + "ahci", + {"id": "in_switch1"}, + parent_bus={"type": ("PCI", "PCIE"), "aobject": "pci_switch"}, + ) + ) + qdev.insert( + qdevices.QDevice( + "ahci", + {"id": "in_switch2"}, + parent_bus={"type": ("PCI", "PCIE"), "aobject": "pci_switch"}, + ) + ) + qdev.insert( + qdevices.QDevice( + "ahci", + {"id": "in_switch3"}, + parent_bus={"type": ("PCI", "PCIE"), "aobject": "pci_switch"}, + ) + ) + + qdev.insert( + qdevices.QDevice( + "ahci", + {"id": "in_root1"}, + parent_bus={"type": ("PCI", "PCIE"), "aobject": "root.1"}, + ) + ) + + qdev.insert( + qdevices.QDevice( + "ahci", + {"id": "in_pci.0"}, + parent_bus={"type": ("PCI", "PCIE"), "aobject": "pci.0"}, + ) + ) + + exp = ( + "-machine pc -device ioh3420,id=root.1,bus=pci.0,addr=0x2 " + "-device x3130-upstream,id=pci_switch,bus=root.1,addr=0x0 " + "-device pci-bridge,id=pci_bridge,bus=root.1,addr=0x1," + "chassis_nr=1 -device ahci,id=in_bridge,bus=pci_bridge,addr=0x1" + " -device xio3130-downstream,bus=pci_switch,id=pci_switch.0," + "addr=0x0,chassis=1 -device ahci,id=in_switch1,bus=pci_switch.0" + ",addr=0x0 " + "-device xio3130-downstream,bus=pci_switch,id=pci_switch.1," + "addr=0x1,chassis=2 -device ahci,id=in_switch2,bus=pci_switch.1" + ",addr=0x0 " + "-device xio3130-downstream,bus=pci_switch,id=pci_switch.2," + "addr=0x2,chassis=3 -device ahci,id=in_switch3,bus=pci_switch.2" + ",addr=0x0 " + "-device ahci,id=in_root1,bus=root.1,addr=0x2 " + "-device ahci,id=in_pci.0,bus=pci.0,addr=0x3" + ) out = qdev.cmdline() assert out == exp, (out, exp) diff --git a/selftests/unit/test_qemu_monitor.py b/selftests/unit/test_qemu_monitor.py index 8ff7a28485..178adf6b78 100644 --- a/selftests/unit/test_qemu_monitor.py +++ b/selftests/unit/test_qemu_monitor.py @@ -6,7 +6,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import qemu_monitor @@ -16,9 +16,9 @@ class MockMonitor(qemu_monitor.Monitor): - """ Dummy class inherited from qemu_monitor.HumanMonitor """ + """Dummy class inherited from qemu_monitor.HumanMonitor""" - def __init__(self): # pylint: disable=W0231 + def __init__(self): # pylint: disable=W0231 pass def __del__(self): @@ -26,25 +26,24 @@ def __del__(self): class InfoNumaTests(unittest.TestCase): - def testZeroNodes(self): d = "0 nodes\n" r = qemu_monitor.Monitor.parse_info_numa(d) self.assertEquals(r, []) def testTwoNodes(self): - d = "2 nodes\n" + \ - "node 0 cpus: 0 2 4\n" + \ - "node 0 size: 12 MB\n" + \ - "node 1 cpus: 1 3 5\n" + \ - "node 1 size: 34 MB\n" + d = ( + "2 nodes\n" + + "node 0 cpus: 0 2 4\n" + + "node 0 size: 12 MB\n" + + "node 1 cpus: 1 3 5\n" + + "node 1 size: 34 MB\n" + ) r = qemu_monitor.Monitor.parse_info_numa(d) - self.assertEquals(r, [(12, set([0, 2, 4])), - (34, set([1, 3, 5]))]) + self.assertEquals(r, [(12, set([0, 2, 4])), (34, set([1, 3, 5]))]) class InfoBlocks(unittest.TestCase): - def testParseBlocks(self): info_1_4 = """ide0-hd0: removable=0 io-status=ok file=c.qcow2 backing_file=b.qcow2 backing_file_depth=2 ro=0 drv=qcow2 encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0 scsi0-hd0: removable=0 io-status=ok file=a.qcow ro=1 drv=raw encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0 @@ -67,132 +66,303 @@ def testParseBlocks(self): sd0: [not inserted] Removable device: not locked, tray closed""" - info_qmp = [{"io-status": "ok", "device": "ide0-hd0", "locked": - False, "removable": False, "inserted": {"iops_rd": 0, - "iops_wr": 0, "ro": False, "backing_file_depth": 2, - "drv": "qcow2", "iops": 0, "bps_wr": 0, "backing_file": - "b.qcow2", "encrypted": False, "bps": 0, "bps_rd": 0, - "file": "c.qcow2", "encryption_key_missing": False}, - "type": "unknown"}, {"io-status": "ok", "device": - "scsi0-hd0", "locked": False, "removable": False, - "inserted": {"iops_rd": 0, "iops_wr": 0, "ro": True, - "backing_file_depth": 0, "drv": "raw", "iops": 0, - "bps_wr": 0, "encrypted": False, "bps": 0, "bps_rd": 0, - "file": "a.qcow", "encryption_key_missing": False}, - "type": "unknown"}, {"io-status": "ok", "device": - "scsi0-hd1", "locked": False, "removable": False, - "inserted": {"iops_rd": 0, "iops_wr": 0, "ro": False, - "backing_file_depth": 0, "drv": "qcow2", "iops": 0, - "bps_wr": 0, "encrypted": True, "bps": 0, "bps_rd": 0, - "file": "enc.qcow2", "encryption_key_missing": True}, - "type": "unknown"}, {"io-status": "ok", "device": - "ide1-cd0", "locked": False, "removable": True, - "tray_open": False, "type": "unknown"}, {"device": - "floppy0", "locked": False, "removable": True, - "tray_open": False, "type": "unknown"}, {"device": "sd0", - "locked": False, "removable": True, "tray_open": False, - "type": "unknown"}] + info_qmp = [ + { + "io-status": "ok", + "device": "ide0-hd0", + "locked": False, + "removable": False, + "inserted": { + "iops_rd": 0, + "iops_wr": 0, + "ro": False, + "backing_file_depth": 2, + "drv": "qcow2", + "iops": 0, + "bps_wr": 0, + "backing_file": "b.qcow2", + "encrypted": False, + "bps": 0, + "bps_rd": 0, + "file": "c.qcow2", + "encryption_key_missing": False, + }, + "type": "unknown", + }, + { + "io-status": "ok", + "device": "scsi0-hd0", + "locked": False, + "removable": False, + "inserted": { + "iops_rd": 0, + "iops_wr": 0, + "ro": True, + "backing_file_depth": 0, + "drv": "raw", + "iops": 0, + "bps_wr": 0, + "encrypted": False, + "bps": 0, + "bps_rd": 0, + "file": "a.qcow", + "encryption_key_missing": False, + }, + "type": "unknown", + }, + { + "io-status": "ok", + "device": "scsi0-hd1", + "locked": False, + "removable": False, + "inserted": { + "iops_rd": 0, + "iops_wr": 0, + "ro": False, + "backing_file_depth": 0, + "drv": "qcow2", + "iops": 0, + "bps_wr": 0, + "encrypted": True, + "bps": 0, + "bps_rd": 0, + "file": "enc.qcow2", + "encryption_key_missing": True, + }, + "type": "unknown", + }, + { + "io-status": "ok", + "device": "ide1-cd0", + "locked": False, + "removable": True, + "tray_open": False, + "type": "unknown", + }, + { + "device": "floppy0", + "locked": False, + "removable": True, + "tray_open": False, + "type": "unknown", + }, + { + "device": "sd0", + "locked": False, + "removable": True, + "tray_open": False, + "type": "unknown", + }, + ] monitor = MockMonitor() # Test "info block" version 1.4 monitor.info = lambda _what, _debug: info_1_4 out1 = monitor.info_block() - exp = {'sd0': {'tray-open': 0, 'locked': 0, 'not-inserted': 1, - 'removable': 1}, - 'ide0-hd0': {'bps_rd': 0, 'backing_file_depth': 2, - 'removable': 0, 'encrypted': 0, 'bps_wr': 0, - 'io-status': 'ok', 'drv': 'qcow2', 'bps': 0, - 'iops': 0, 'file': 'c.qcow2', 'iops_rd': 0, - 'ro': 0, 'backing_file': 'b.qcow2', 'iops_wr': 0}, - 'floppy0': {'tray-open': 0, 'locked': 0, 'not-inserted': 1, - 'removable': 1}, - 'ide1-cd0': {'tray-open': 0, 'locked': 0, 'not-inserted': 1, - 'io-status': 'ok', 'removable': 1}, - 'scsi0-hd0': {'bps_rd': 0, 'removable': 0, 'encrypted': 0, - 'bps_wr': 0, 'io-status': 'ok', 'drv': 'raw', - 'bps': 0, 'iops': 0, 'file': 'a.qcow', - 'iops_rd': 0, 'ro': 1, 'iops_wr': 0}, - 'scsi0-hd1': {'bps_rd': 0, 'removable': 0, 'encrypted': 1, - 'bps_wr': 0, 'io-status': 'ok', 'drv': 'qcow2', - 'bps': 0, 'iops': 0, 'file': 'enc.qcow2', - 'iops_rd': 0, 'ro': 0, 'iops_wr': 0}} - assert out1 == exp, ("Info block of qemu 1.4 is parsed incorrectly\n%s" - "\n%s" % (out1, exp)) + exp = { + "sd0": {"tray-open": 0, "locked": 0, "not-inserted": 1, "removable": 1}, + "ide0-hd0": { + "bps_rd": 0, + "backing_file_depth": 2, + "removable": 0, + "encrypted": 0, + "bps_wr": 0, + "io-status": "ok", + "drv": "qcow2", + "bps": 0, + "iops": 0, + "file": "c.qcow2", + "iops_rd": 0, + "ro": 0, + "backing_file": "b.qcow2", + "iops_wr": 0, + }, + "floppy0": {"tray-open": 0, "locked": 0, "not-inserted": 1, "removable": 1}, + "ide1-cd0": { + "tray-open": 0, + "locked": 0, + "not-inserted": 1, + "io-status": "ok", + "removable": 1, + }, + "scsi0-hd0": { + "bps_rd": 0, + "removable": 0, + "encrypted": 0, + "bps_wr": 0, + "io-status": "ok", + "drv": "raw", + "bps": 0, + "iops": 0, + "file": "a.qcow", + "iops_rd": 0, + "ro": 1, + "iops_wr": 0, + }, + "scsi0-hd1": { + "bps_rd": 0, + "removable": 0, + "encrypted": 1, + "bps_wr": 0, + "io-status": "ok", + "drv": "qcow2", + "bps": 0, + "iops": 0, + "file": "enc.qcow2", + "iops_rd": 0, + "ro": 0, + "iops_wr": 0, + }, + } + assert ( + out1 == exp + ), "Info block of qemu 1.4 is parsed incorrectly\n%s" "\n%s" % (out1, exp) # Test "info block" version 1.5 monitor.info = lambda _what, _debug: info_1_5 out2 = monitor.info_block() - exp = {'sd0': {'not-inserted': 1, 'removable': 1}, - 'ide0-hd0': {'backing_file_depth': 2, 'drv': 'qcow2', - 'backing_file': 'b.qcow2', 'file': 'c.qcow2'}, - 'floppy0': {'not-inserted': 1, 'removable': 1}, - 'ide1-cd0': {'not-inserted': 1, 'removable': 1}, - 'scsi0-hd0': {'drv': 'raw', 'ro': 1, 'file': 'a.qcow'}, - 'scsi0-hd1': {'encrypted': 1, 'drv': 'qcow2', - 'file': 'enc.qcow2'}} - assert out2 == exp, ("Info block of qemu 1.5 is parsed incorrectly\n%s" - "\n%s" % (out2, exp)) + exp = { + "sd0": {"not-inserted": 1, "removable": 1}, + "ide0-hd0": { + "backing_file_depth": 2, + "drv": "qcow2", + "backing_file": "b.qcow2", + "file": "c.qcow2", + }, + "floppy0": {"not-inserted": 1, "removable": 1}, + "ide1-cd0": {"not-inserted": 1, "removable": 1}, + "scsi0-hd0": {"drv": "raw", "ro": 1, "file": "a.qcow"}, + "scsi0-hd1": {"encrypted": 1, "drv": "qcow2", "file": "enc.qcow2"}, + } + assert ( + out2 == exp + ), "Info block of qemu 1.5 is parsed incorrectly\n%s" "\n%s" % (out2, exp) # verify, that booth representation gives the same results # (qemu-1.5 is less informative so not all params are checked) for name, params in six.iteritems(out2): - assert name in out1, ("missing disk '%s' in info-1.5\n%s\n%s" - % (name, out2, out1)) + assert name in out1, "missing disk '%s' in info-1.5\n%s\n%s" % ( + name, + out2, + out1, + ) for key, value in six.iteritems(params): - assert out1[name].get(key, 0) == value, ("value of disk %s " - "mismatch in info-1.5 %s=%s (%s)\n%s\n%s" - % (name, key, value, out1[ - name].get(key, 0), - out2, out1)) + assert ( + out1[name].get(key, 0) == value + ), "value of disk %s " "mismatch in info-1.5 %s=%s (%s)\n%s\n%s" % ( + name, + key, + value, + out1[name].get(key, 0), + out2, + out1, + ) # Test "query-block" qmp version monitor.info = lambda _what, _debug: info_qmp out3 = monitor.info_block() - exp = {'sd0': {'type': 'unknown', 'tray_open': False, - 'not-inserted': True, 'removable': True, - 'locked': False}, - 'ide0-hd0': {'bps_rd': 0, 'backing_file_depth': 2, - 'removable': False, 'type': 'unknown', - 'encrypted': False, 'bps_wr': 0, 'locked': False, - 'drv': 'qcow2', 'bps': 0, 'iops': 0, - 'io-status': 'ok', 'file': 'c.qcow2', - 'iops_rd': 0, 'encryption_key_missing': False, - 'ro': False, 'backing_file': 'b.qcow2', - 'iops_wr': 0}, - 'floppy0': {'type': 'unknown', 'tray_open': False, - 'not-inserted': True, 'removable': True, - 'locked': False}, - 'ide1-cd0': {'locked': False, 'tray_open': False, - 'io-status': 'ok', 'removable': True, - 'not-inserted': True, 'type': 'unknown'}, - 'scsi0-hd0': {'bps_rd': 0, 'backing_file_depth': 0, - 'removable': False, 'encrypted': False, - 'bps_wr': 0, 'locked': False, 'drv': 'raw', - 'bps': 0, 'iops': 0, 'io-status': 'ok', - 'file': 'a.qcow', 'iops_rd': 0, - 'encryption_key_missing': False, 'ro': True, - 'type': 'unknown', 'iops_wr': 0}, - 'scsi0-hd1': {'bps_rd': 0, 'backing_file_depth': 0, - 'removable': False, 'encrypted': True, - 'bps_wr': 0, 'locked': False, 'drv': 'qcow2', - 'bps': 0, 'iops': 0, 'io-status': 'ok', - 'file': 'enc.qcow2', 'iops_rd': 0, - 'encryption_key_missing': True, 'ro': False, - 'type': 'unknown', 'iops_wr': 0}} - assert out3 == exp, ("QMP query-block of qemu is parsed incorrectly\n" - "%s\n%s" % (out3, exp)) + exp = { + "sd0": { + "type": "unknown", + "tray_open": False, + "not-inserted": True, + "removable": True, + "locked": False, + }, + "ide0-hd0": { + "bps_rd": 0, + "backing_file_depth": 2, + "removable": False, + "type": "unknown", + "encrypted": False, + "bps_wr": 0, + "locked": False, + "drv": "qcow2", + "bps": 0, + "iops": 0, + "io-status": "ok", + "file": "c.qcow2", + "iops_rd": 0, + "encryption_key_missing": False, + "ro": False, + "backing_file": "b.qcow2", + "iops_wr": 0, + }, + "floppy0": { + "type": "unknown", + "tray_open": False, + "not-inserted": True, + "removable": True, + "locked": False, + }, + "ide1-cd0": { + "locked": False, + "tray_open": False, + "io-status": "ok", + "removable": True, + "not-inserted": True, + "type": "unknown", + }, + "scsi0-hd0": { + "bps_rd": 0, + "backing_file_depth": 0, + "removable": False, + "encrypted": False, + "bps_wr": 0, + "locked": False, + "drv": "raw", + "bps": 0, + "iops": 0, + "io-status": "ok", + "file": "a.qcow", + "iops_rd": 0, + "encryption_key_missing": False, + "ro": True, + "type": "unknown", + "iops_wr": 0, + }, + "scsi0-hd1": { + "bps_rd": 0, + "backing_file_depth": 0, + "removable": False, + "encrypted": True, + "bps_wr": 0, + "locked": False, + "drv": "qcow2", + "bps": 0, + "iops": 0, + "io-status": "ok", + "file": "enc.qcow2", + "iops_rd": 0, + "encryption_key_missing": True, + "ro": False, + "type": "unknown", + "iops_wr": 0, + }, + } + assert ( + out3 == exp + ), "QMP query-block of qemu is parsed incorrectly\n" "%s\n%s" % (out3, exp) # verify, that booth representation gives the same results # (qemu-1.4 is less informative so not all params are checked) for name, params in six.iteritems(out1): - assert name in out3, ("missing disk '%s' in info-1.5\n%s\n%s" - % (name, out1, out3)) + assert name in out3, "missing disk '%s' in info-1.5\n%s\n%s" % ( + name, + out1, + out3, + ) for key, value in six.iteritems(params): - assert out3[name].get(key, 0) == value, ("value of disk %s " - "mismatch in QMP version %s=%s (%s)\n%s\n%s" - % (name, key, value, out3[ - name].get(key, 0), - out1, out3)) + assert ( + out3[name].get(key, 0) == value + ), "value of disk %s " "mismatch in QMP version %s=%s (%s)\n%s\n%s" % ( + name, + key, + value, + out3[name].get(key, 0), + out1, + out3, + ) if __name__ == "__main__": diff --git a/selftests/unit/test_qemu_qtree.py b/selftests/unit/test_qemu_qtree.py index a1afaa2152..fd4797f90c 100644 --- a/selftests/unit/test_qemu_qtree.py +++ b/selftests/unit/test_qemu_qtree.py @@ -13,7 +13,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest.unittest_utils import mock @@ -29,26 +29,26 @@ # Dummy classes and functions class ParamsDict(dict): - """ params like dictionary """ + """params like dictionary""" def objects(self, item): if self.get(item): - return self.get(item).split(' ') + return self.get(item).split(" ") def object_params(self, obj): ret = self.copy() for (param, value) in six.iteritems(self): - if param.endswith('_%s' % obj): - ret[param[:-len('_%s' % obj)]] = value + if param.endswith("_%s" % obj): + ret[param[: -len("_%s" % obj)]] = value return ret def combine(first, second, offset): - """ Add string line-by-line with offset*OFFSET_PER_LEVEL """ + """Add string line-by-line with offset*OFFSET_PER_LEVEL""" out = first[:] - offset = ' ' * OFFSET_PER_LEVEL * offset + offset = " " * OFFSET_PER_LEVEL * offset for line in second.splitlines(): - out += '\n' + offset + line + out += "\n" + offset + line return out @@ -134,50 +134,83 @@ class USB controller, addr 00:04.0, pci id 8086:2934 (sub 1af4:1100) mmio ffffffffffffffff/0000000000000002 mmio ffffffffffffffff/0000000000000001""" -info_block = {'ide0-hd0': {'removable': 0, 'io-status': 'ok', - 'file': '/tmp/vl.UWzrkU', - 'backing_file': '/dummy/directory/f16-64.qcow2', - 'ro': 1, 'drv': 'qcow2', 'encrypted': 0, 'bps': 0, - 'bps_rd': 0, 'bps_wr': 0, 'iops': 0, 'iops_rd': 0, - 'iops_wr': 0}, - 'usb2.6': {'removable': 0, 'io-status': 'ok', - 'file': '/tmp/stg4.qcow2', 'ro': 0, 'drv': 'qcow2', - 'encrypted': 0, 'bps': 0, 'bps_rd': 0, 'bps_wr': 0, - 'iops': 0, 'iops_rd': 0, 'iops_wr': 0}} +info_block = { + "ide0-hd0": { + "removable": 0, + "io-status": "ok", + "file": "/tmp/vl.UWzrkU", + "backing_file": "/dummy/directory/f16-64.qcow2", + "ro": 1, + "drv": "qcow2", + "encrypted": 0, + "bps": 0, + "bps_rd": 0, + "bps_wr": 0, + "iops": 0, + "iops_rd": 0, + "iops_wr": 0, + }, + "usb2.6": { + "removable": 0, + "io-status": "ok", + "file": "/tmp/stg4.qcow2", + "ro": 0, + "drv": "qcow2", + "encrypted": 0, + "bps": 0, + "bps_rd": 0, + "bps_wr": 0, + "iops": 0, + "iops_rd": 0, + "iops_wr": 0, + }, +} guest_proc_scsi = """Attached devices: Host: scsi4 Channel: 00 Id: 00 Lun: 00 Vendor: QEMU Model: QEMU HARDDISK Rev: 1.0. Type: Direct-Access ANSI SCSI revision: 05""" -params = ParamsDict({'images': 'image1 stg4', - 'drive_format': 'ide', - 'drive_format_stg4': 'usb2', - 'drive_index_image1': '0', - 'drive_index_stg4': '6', - 'image_format': 'qcow2', - 'image_name': '/dummy/directory/f16-64', - 'image_name_stg4': 'stg4', - 'image_size': '10G', - 'image_size_stg4': '1M', - 'image_snapshot': 'yes', - 'image_snapshot_stg4': 'no', - 'image_readonly_image1': 'yes', - 'cdroms': 'cdrom1'}) +params = ParamsDict( + { + "images": "image1 stg4", + "drive_format": "ide", + "drive_format_stg4": "usb2", + "drive_index_image1": "0", + "drive_index_stg4": "6", + "image_format": "qcow2", + "image_name": "/dummy/directory/f16-64", + "image_name_stg4": "stg4", + "image_size": "10G", + "image_size_stg4": "1M", + "image_snapshot": "yes", + "image_snapshot_stg4": "no", + "image_readonly_image1": "yes", + "cdroms": "cdrom1", + } +) class QtreeContainerTest(unittest.TestCase): - """ QtreeContainer tests """ + """QtreeContainer tests""" def test_qtree(self): - """ Correct workflow """ - reference_nodes = [qemu_qtree.QtreeDisk, qemu_qtree.QtreeBus, - qemu_qtree.QtreeDev, qemu_qtree.QtreeDev, - qemu_qtree.QtreeDev, qemu_qtree.QtreeDisk, - qemu_qtree.QtreeBus, qemu_qtree.QtreeDev, - qemu_qtree.QtreeBus, qemu_qtree.QtreeDev, - qemu_qtree.QtreeDev, qemu_qtree.QtreeBus] + """Correct workflow""" + reference_nodes = [ + qemu_qtree.QtreeDisk, + qemu_qtree.QtreeBus, + qemu_qtree.QtreeDev, + qemu_qtree.QtreeDev, + qemu_qtree.QtreeDev, + qemu_qtree.QtreeDisk, + qemu_qtree.QtreeBus, + qemu_qtree.QtreeDev, + qemu_qtree.QtreeBus, + qemu_qtree.QtreeDev, + qemu_qtree.QtreeDev, + qemu_qtree.QtreeBus, + ] info = qtree_header info = combine(info, dev_ide_disk, 1) @@ -189,24 +222,38 @@ def test_qtree(self): qtree.parse_info_qtree(info) nodes = qtree.get_nodes() - self.assertEqual(len(nodes), len(reference_nodes), ("Number of parsed " - "nodes is not equal to the number of qtree nodes. " - "%s != %s" % (len(nodes), len(reference_nodes)))) + self.assertEqual( + len(nodes), + len(reference_nodes), + ( + "Number of parsed " + "nodes is not equal to the number of qtree nodes. " + "%s != %s" % (len(nodes), len(reference_nodes)) + ), + ) for i in xrange(len(nodes)): - self.assertTrue(isinstance(nodes[i], reference_nodes[i]), - ("Node %d should be class %s but is %s instead" % - (i, reference_nodes[i], type(reference_nodes)))) + self.assertTrue( + isinstance(nodes[i], reference_nodes[i]), + ( + "Node %d should be class %s but is %s instead" + % (i, reference_nodes[i], type(reference_nodes)) + ), + ) tree = qtree.get_qtree() - self.assertTrue(isinstance(tree.str_qtree(), six.string_types), - "qtree.str_qtree() returns nonstring output.") + self.assertTrue( + isinstance(tree.str_qtree(), six.string_types), + "qtree.str_qtree() returns nonstring output.", + ) - self.assertTrue(isinstance(str(tree), six.string_types), - "str(qtree) returns nonstring output.") + self.assertTrue( + isinstance(str(tree), six.string_types), + "str(qtree) returns nonstring output.", + ) def test_bad_qtree(self): - """ Incorrect qtree """ + """Incorrect qtree""" qtree = qemu_qtree.QtreeContainer() info = combine(qtree_header, "Very_bad_line", 1) self.assertRaises(ValueError, qtree.parse_info_qtree, info) @@ -214,14 +261,15 @@ def test_bad_qtree(self): class QtreeDiskContainerTest(unittest.TestCase): - """ QtreeDiskContainer tests """ + """QtreeDiskContainer tests""" def setUp(self): # Get rid of logging errors def dumm(*args, **kvargs): pass + self.god = mock.mock_god(ut=self) - self.god.stub_with(qemu_qtree.logging, 'error', dumm) + self.god.stub_with(qemu_qtree.logging, "error", dumm) info = qtree_header info = combine(info, dev_ide_disk, 1) @@ -240,36 +288,36 @@ def tearDown(self): self.god.unstub_all() def test_check_params(self): - """ Correct workflow """ + """Correct workflow""" disks = self.disks self.assertEqual(len(self.disks.disks), self.no_disks) self.assertEqual(disks.parse_info_block(info_block), (0, 0)) self.assertEqual(disks.generate_params(), 0) self.assertEqual(disks.check_disk_params(params), 2) - self.assertEqual(disks.check_guests_proc_scsi(guest_proc_scsi), - (0, 0, 1, 0)) + self.assertEqual(disks.check_guests_proc_scsi(guest_proc_scsi), (0, 0, 1, 0)) # Check the full disk output (including params) for disk in disks.disks: - self.assertTrue(isinstance(str(disk), six.string_types), - "str(disk) returns nonstring output.") + self.assertTrue( + isinstance(str(disk), six.string_types), + "str(disk) returns nonstring output.", + ) def test_check_params_bad(self): - """ Whole workflow with bad data """ + """Whole workflow with bad data""" disks = self.disks # missing disk in info block _info_block = info_block.copy() - _info_block.pop('ide0-hd0') + _info_block.pop("ide0-hd0") # snapshot in info qtree but not in params - _info_block['usb2.6']['file'] = 'none.qcow2' - _info_block['usb2.6']['backing_file'] = '/tmp/stg4.qcow2' + _info_block["usb2.6"]["file"] = "none.qcow2" + _info_block["usb2.6"]["backing_file"] = "/tmp/stg4.qcow2" # additional disk in info block - _info_block['missing_bad_disk1'] = {} + _info_block["missing_bad_disk1"] = {} # additional disk in params _params = ParamsDict(params) - _params['images'] += ' bad_disk2' + _params["images"] += " bad_disk2" # Missing disk in proc_scsi - _guest_proc_scsi = guest_proc_scsi.replace('Channel: 00', - 'Channel: 01') + _guest_proc_scsi = guest_proc_scsi.replace("Channel: 00", "Channel: 01") # Ignored disk in proc_scsi _guest_proc_scsi += """ Host: scsi1 Channel: 00 Id: 00 Lun: 00 @@ -279,34 +327,35 @@ def test_check_params_bad(self): self.assertEqual(disks.parse_info_block(_info_block), (1, 1)) self.assertEqual(disks.generate_params(), 1) self.assertEqual(disks.check_disk_params(_params), 4) - self.assertEqual(disks.check_guests_proc_scsi(_guest_proc_scsi), - (0, 1, 1, 0)) + self.assertEqual(disks.check_guests_proc_scsi(_guest_proc_scsi), (0, 1, 1, 0)) class KvmQtreeClassTest(unittest.TestCase): - """ Additional tests for qemu_qtree classes """ + """Additional tests for qemu_qtree classes""" def test_qtree_bus_bus(self): - """ Bus' child can't be Bus() """ + """Bus' child can't be Bus()""" test = qemu_qtree.QtreeBus() - self.assertRaises(qemu_qtree.IncompatibleTypeError, - test.add_child, qemu_qtree.QtreeBus()) + self.assertRaises( + qemu_qtree.IncompatibleTypeError, test.add_child, qemu_qtree.QtreeBus() + ) def test_qtree_dev_dev(self): - """ Dev's child can't be Dev() """ + """Dev's child can't be Dev()""" test = qemu_qtree.QtreeDev() - self.assertRaises(qemu_qtree.IncompatibleTypeError, - test.add_child, qemu_qtree.QtreeDev()) + self.assertRaises( + qemu_qtree.IncompatibleTypeError, test.add_child, qemu_qtree.QtreeDev() + ) def test_qtree_disk_missing_filename(self): - """ in info_block must contain info about file or backing_file """ + """in info_block must contain info about file or backing_file""" test = qemu_qtree.QtreeDisk() - test.set_qtree({'something': 'something'}) - test.set_block_prop('prop', 'value') + test.set_qtree({"something": "something"}) + test.set_block_prop("prop", "value") self.assertRaises(ValueError, test.generate_params) if __name__ == "__main__": - """ Run unittest """ + """Run unittest""" unittest.main() diff --git a/selftests/unit/test_remote.py b/selftests/unit/test_remote.py index a175a4c4b2..26437d02ad 100644 --- a/selftests/unit/test_remote.py +++ b/selftests/unit/test_remote.py @@ -6,7 +6,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import remote @@ -28,8 +28,9 @@ def _new_remote_file(self): test_file = open(self.test_file_path, "w") test_file.writelines(self.default_data) test_file.close() - remote_file = remote.RemoteFile(None, "test", None, None, None, - self.test_file_path) + remote_file = remote.RemoteFile( + None, "test", None, None, None, self.test_file_path + ) return remote_file def _read_test_file(self): @@ -43,11 +44,13 @@ def testAdd(self): _add_list = ["add_line_1", "add_line_2", "add_line_3"] remote_file.add(_add_list) test_data = self._read_test_file() - except_data = ["RemoteFile Test.\n", - "Pattern Line.\n", - "add_line_1\n", - "add_line_2\n", - "add_line_3"] + except_data = [ + "RemoteFile Test.\n", + "Pattern Line.\n", + "add_line_1\n", + "add_line_2\n", + "add_line_3", + ] for index in range(len(except_data)): self.assertEqual(except_data[index], test_data[index]) del remote_file @@ -59,8 +62,7 @@ def testSub(self): _pattern2repl = {r"Remote": "Local", r"^Pat.*$": "Replace Line"} remote_file.sub(_pattern2repl) test_data = self._read_test_file() - except_data = ["LocalFile Test.\n", - "Replace Line"] + except_data = ["LocalFile Test.\n", "Replace Line"] for index in range(len(except_data)): self.assertEqual(except_data[index], test_data[index]) del remote_file @@ -84,9 +86,7 @@ def testSEEA(self): _pattern2repl = {r"Remote": "Local", r"NoMatch": "ADD line."} remote_file.sub_else_add(_pattern2repl) test_data = self._read_test_file() - except_data = ["LocalFile Test.\n", - "Pattern Line.\n", - "ADD line."] + except_data = ["LocalFile Test.\n", "Pattern Line.\n", "ADD line."] for index in range(len(except_data)): self.assertEqual(except_data[index], test_data[index]) del remote_file diff --git a/selftests/unit/test_setup_networking.py b/selftests/unit/test_setup_networking.py index 95175665ce..bfcc2d9b44 100644 --- a/selftests/unit/test_setup_networking.py +++ b/selftests/unit/test_setup_networking.py @@ -6,7 +6,6 @@ class TestProxySetuper(unittest.TestCase): - def setUp(self): self._test_mock = Mock() self._env_mock = Mock() @@ -21,12 +20,15 @@ def setUp(self): self._installer_mock = Mock() self._patchers = [] - self._patchers.append(patch('urllib.request.ProxyHandler', - self._proxy_handler_mock)) - self._patchers.append(patch('urllib.request.build_opener', - self._build_opener_mock)) - self._patchers.append(patch('urllib.request.install_opener', - self._installer_mock)) + self._patchers.append( + patch("urllib.request.ProxyHandler", self._proxy_handler_mock) + ) + self._patchers.append( + patch("urllib.request.build_opener", self._build_opener_mock) + ) + self._patchers.append( + patch("urllib.request.install_opener", self._installer_mock) + ) for patcher in self._patchers: patcher.start() @@ -35,8 +37,9 @@ def tearDown(self): patcher.stop() def test_no_config(self): - params = {'other_key': 'yes', - } + params = { + "other_key": "yes", + } np = NetworkProxies(self._test_mock, params, self._env_mock) np.setup() self._proxy_handler_mock.assert_not_called() @@ -44,9 +47,10 @@ def test_no_config(self): self._installer_mock.assert_not_called() def test_empty_config(self): - params = {'network_proxies': '', - 'other_key': 'yes', - } + params = { + "network_proxies": "", + "other_key": "yes", + } np = NetworkProxies(self._test_mock, params, self._env_mock) np.setup() self._proxy_handler_mock.assert_not_called() @@ -54,9 +58,10 @@ def test_empty_config(self): self._installer_mock.assert_not_called() def test_invalid_config(self): - params = {'network_proxies': '', - 'other_key': 'yes', - } + params = { + "network_proxies": "", + "other_key": "yes", + } np = NetworkProxies(self._test_mock, params, self._env_mock) np.setup() self._proxy_handler_mock.assert_not_called() @@ -64,10 +69,11 @@ def test_invalid_config(self): self._installer_mock.assert_not_called() def test_config_unique_proxy(self): - params = {'network_proxies': 'https_proxy: https://proxy.com:8080/', - 'other_key': 'no', - } - proxies_dict = {'https': 'https://proxy.com:8080/'} + params = { + "network_proxies": "https_proxy: https://proxy.com:8080/", + "other_key": "no", + } + proxies_dict = {"https": "https://proxy.com:8080/"} np = NetworkProxies(self._test_mock, params, self._env_mock) np.setup() self._proxy_handler_mock.assert_called_once_with(proxies_dict) @@ -75,13 +81,15 @@ def test_config_unique_proxy(self): self._installer_mock.assert_called_once_with(self._opener_mock) def test_config_multiple_proxies(self): - params = {'network_proxies': - 'https_proxy: https://proxy.com:8080/; ' - 'ftp_proxy: ftp://proxy.com:3128/', - 'other_key': 'no', - } - proxies_dict = {'https': 'https://proxy.com:8080/', - 'ftp': 'ftp://proxy.com:3128/'} + params = { + "network_proxies": "https_proxy: https://proxy.com:8080/; " + "ftp_proxy: ftp://proxy.com:3128/", + "other_key": "no", + } + proxies_dict = { + "https": "https://proxy.com:8080/", + "ftp": "ftp://proxy.com:3128/", + } np = NetworkProxies(self._test_mock, params, self._env_mock) np.setup() self._proxy_handler_mock.assert_called_once_with(proxies_dict) @@ -89,28 +97,26 @@ def test_config_multiple_proxies(self): self._installer_mock.assert_called_once_with(self._opener_mock) def test_config_half_valid_config(self): - params = {'network_proxies': - 'https_proxy: https://proxy.com:8080/; ' - 'nonsense: nonsense://bar.foo:0000; ', - 'other_key': 'no', - } - proxies_dict = {'https': 'https://proxy.com:8080/'} + params = { + "network_proxies": "https_proxy: https://proxy.com:8080/; " + "nonsense: nonsense://bar.foo:0000; ", + "other_key": "no", + } + proxies_dict = {"https": "https://proxy.com:8080/"} np = NetworkProxies(self._test_mock, params, self._env_mock) with self.assertRaises(ValueError): np.setup() class TestBridgeSetuper(unittest.TestCase): - def setUp(self): self._test_mock = Mock() self._env_mock = Mock() - @patch('virttest.test_setup.networking.PrivateBridgeConfig') - @patch('virttest.test_setup.networking.PrivateOvsBridgeConfig') + @patch("virttest.test_setup.networking.PrivateBridgeConfig") + @patch("virttest.test_setup.networking.PrivateOvsBridgeConfig") def test_no_nics(self, ovs_mock, bridge_mock): - params = Params({ - }) + params = Params({}) brcfg = BridgeConfig(self._test_mock, params, self._env_mock) brcfg.setup() ovs_mock.setup.assert_not_called() @@ -119,13 +125,15 @@ def test_no_nics(self, ovs_mock, bridge_mock): ovs_mock.cleanup.assert_not_called() bridge_mock.cleanup.assert_not_called() - @patch('virttest.test_setup.networking.PrivateBridgeConfig') - @patch('virttest.test_setup.networking.PrivateOvsBridgeConfig') + @patch("virttest.test_setup.networking.PrivateBridgeConfig") + @patch("virttest.test_setup.networking.PrivateOvsBridgeConfig") def test_nics_not_private(self, ovs_mock, bridge_mock): - params = Params({ - 'nics': 'bridge', - 'netdst': 'virbr0', - }) + params = Params( + { + "nics": "bridge", + "netdst": "virbr0", + } + ) brcfg = BridgeConfig(self._test_mock, params, self._env_mock) brcfg.setup() ovs_mock.setup.assert_not_called() @@ -134,60 +142,72 @@ def test_nics_not_private(self, ovs_mock, bridge_mock): ovs_mock.cleanup.assert_not_called() bridge_mock.cleanup.assert_not_called() - @patch('virttest.test_setup.networking.PrivateBridgeConfig') + @patch("virttest.test_setup.networking.PrivateBridgeConfig") def test_nics_prbr(self, pbc_mock): mock_instance = Mock() pbc_mock.return_value = mock_instance - params = Params({ - 'nics': 'bridge', - 'netdst': 'private', - 'priv_brname': 'foobr0', - }) + params = Params( + { + "nics": "bridge", + "netdst": "private", + "priv_brname": "foobr0", + } + ) brcfg = BridgeConfig(self._test_mock, params, self._env_mock) brcfg.setup() - pbc_mock.assert_called_once_with({ - 'nics': 'bridge', - 'netdst': 'private', - 'priv_brname': 'foobr0', - }) + pbc_mock.assert_called_once_with( + { + "nics": "bridge", + "netdst": "private", + "priv_brname": "foobr0", + } + ) mock_instance.setup.assert_called_once() pbc_mock.reset_mock() brcfg.cleanup() - pbc_mock.assert_called_once_with({ - 'nics': 'bridge', - 'netdst': 'private', - 'netdst_bridge': 'foobr0', - 'priv_brname': 'foobr0', - }) + pbc_mock.assert_called_once_with( + { + "nics": "bridge", + "netdst": "private", + "netdst_bridge": "foobr0", + "priv_brname": "foobr0", + } + ) mock_instance.cleanup.assert_called_once() - @patch('virttest.test_setup.networking.PrivateOvsBridgeConfig') + @patch("virttest.test_setup.networking.PrivateOvsBridgeConfig") def test_nics_ovs(self, povsbc_mock): mock_instance = Mock() povsbc_mock.return_value = mock_instance - params = Params({ - 'nics': 'ovs', - 'netdst': 'private', - 'priv_br_type': 'openvswitch', - }) + params = Params( + { + "nics": "ovs", + "netdst": "private", + "priv_br_type": "openvswitch", + } + ) brcfg = BridgeConfig(self._test_mock, params, self._env_mock) brcfg.setup() - povsbc_mock.assert_called_once_with({ - 'nics': 'ovs', - 'netdst': 'private', - 'priv_br_type': 'openvswitch', - }) + povsbc_mock.assert_called_once_with( + { + "nics": "ovs", + "netdst": "private", + "priv_br_type": "openvswitch", + } + ) mock_instance.setup.assert_called_once() povsbc_mock.reset_mock() brcfg.cleanup() - povsbc_mock.assert_called_once_with({ - 'nics': 'ovs', - 'netdst': 'private', - 'priv_br_type': 'openvswitch', - 'netdst_ovs': 'atbr0', - }) + povsbc_mock.assert_called_once_with( + { + "nics": "ovs", + "netdst": "private", + "priv_br_type": "openvswitch", + "netdst_ovs": "atbr0", + } + ) mock_instance.cleanup.assert_called_once() -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_test.py b/selftests/unit/test_test.py index 839e172dc2..0254c82c41 100644 --- a/selftests/unit/test_test.py +++ b/selftests/unit/test_test.py @@ -11,28 +11,24 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from avocado_vt import test as vt_test class FakeJob(object): - def __init__(self): self.args = argparse.Namespace() self.args.vt_config = True -FAKE_PARAMS = {'shortname': 'fake', - 'vm_type': 'fake'} +FAKE_PARAMS = {"shortname": "fake", "vm_type": "fake"} class VirtTestTest(unittest.TestCase): - def setUp(self): - self.test = vt_test.VirtTest(job=FakeJob(), - vt_params=FAKE_PARAMS) + self.test = vt_test.VirtTest(job=FakeJob(), vt_params=FAKE_PARAMS) def test_basedir(self): if self.test.filename is None: @@ -45,5 +41,5 @@ def test_filename(self): self.assertIsNone(self.test.filename) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_utils_cgroup.py b/selftests/unit/test_utils_cgroup.py index 1aaadc9c18..0953b450c4 100644 --- a/selftests/unit/test_utils_cgroup.py +++ b/selftests/unit/test_utils_cgroup.py @@ -10,7 +10,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest.staging import utils_cgroup @@ -122,19 +122,20 @@ ] mount_cases = [ - {"mount_txt": mount_1, - "controllers": controllers_1, - "mount_points": mount_points_1, - }, - {"mount_txt": mount_2, - "controllers": controllers_2, - "mount_points": mount_points_2, - }, + { + "mount_txt": mount_1, + "controllers": controllers_1, + "mount_points": mount_points_1, + }, + { + "mount_txt": mount_2, + "controllers": controllers_2, + "mount_points": mount_points_2, + }, ] class CgroupTest(unittest.TestCase): - def test_get_cgroup_mountpoint(self): for case in mount_cases: # Let's work around the fact that NamedTemporaryFile @@ -144,23 +145,25 @@ def test_get_cgroup_mountpoint(self): mount_file.close() # Now let's do our own management of the file - mount_file = open(mount_file_path, 'w') + mount_file = open(mount_file_path, "w") mount_file.write(case["mount_txt"]) mount_file.close() try: for idx, controller in enumerate(case["controllers"]): res = utils_cgroup.get_cgroup_mountpoint( - controller, mount_file_path) + controller, mount_file_path + ) self.assertEqual(case["mount_points"][idx], res) self.assertRaises( exceptions.TestError, utils_cgroup.get_cgroup_mountpoint, "non_exit_ctlr", - mount_file_path) + mount_file_path, + ) finally: os.remove(mount_file_path) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_utils_config.py b/selftests/unit/test_utils_config.py index 250cd85cba..edba5938c9 100644 --- a/selftests/unit/test_utils_config.py +++ b/selftests/unit/test_utils_config.py @@ -6,14 +6,16 @@ if sys.version_info[:2] == (2, 6): import unittest2 as unittest + PYTHON_26 = True else: import unittest + PYTHON_26 = False # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import utils_config @@ -40,14 +42,13 @@ class SectionlessConfigTest(unittest.TestCase): - def test_accessers(self): config_file = tempfile.NamedTemporaryFile() config_path = config_file.name config_file.close() try: - config_file = open(config_path, 'w') + config_file = open(config_path, "w") config_file.write(content) config_file.close() @@ -56,38 +57,36 @@ def test_accessers(self): try: # Test loader. self.assertEqual(len(config), 5) - self.assertEqual(config['a'], '1') - self.assertEqual(config['b'], '[hi, there]') - self.assertEqual(config['c'], 'hello') - self.assertEqual(config['d'], '"hi, there"') - self.assertEqual(config['e'], '[hi,\nthere]') + self.assertEqual(config["a"], "1") + self.assertEqual(config["b"], "[hi, there]") + self.assertEqual(config["c"], "hello") + self.assertEqual(config["d"], '"hi, there"') + self.assertEqual(config["e"], "[hi,\nthere]") # Test getter. try: - config['f'] + config["f"] except Exception as e: - self.assertEqual( - utils_config.ConfigNoOptionError, e.__class__) - self.assertTrue('no option' in str(e)) + self.assertEqual(utils_config.ConfigNoOptionError, e.__class__) + self.assertTrue("no option" in str(e)) # Test setter. - config['f'] = 'test' - self.assertEqual(config['f'], 'test') + config["f"] = "test" + self.assertEqual(config["f"], "test") # Test deleter. # delete exist option. - del config['f'] + del config["f"] # delete non-exist option. try: - del config['f'] + del config["f"] except Exception as e: - self.assertEqual( - utils_config.ConfigNoOptionError, e.__class__) - self.assertTrue('no option' in str(e)) + self.assertEqual(utils_config.ConfigNoOptionError, e.__class__) + self.assertTrue("no option" in str(e)) # Test contain. - self.assertTrue('a' in config) - self.assertFalse('f' in config) + self.assertTrue("a" in config) + self.assertFalse("f" in config) finally: config.restore() @@ -96,36 +95,34 @@ def test_accessers(self): with utils_config.SectionlessConfig(config_path) as config: # Test loader. self.assertEqual(len(config), 5) - self.assertEqual(config['a'], '1') - self.assertEqual(config['b'], '[hi, there]') - self.assertEqual(config['c'], 'hello') - self.assertEqual(config['d'], '"hi, there"') - self.assertEqual(config['e'], '[hi,\nthere]') + self.assertEqual(config["a"], "1") + self.assertEqual(config["b"], "[hi, there]") + self.assertEqual(config["c"], "hello") + self.assertEqual(config["d"], '"hi, there"') + self.assertEqual(config["e"], "[hi,\nthere]") # Test getter. try: - config['f'] + config["f"] except Exception as e: - self.assertEqual( - utils_config.ConfigNoOptionError, e.__class__) - self.assertTrue('no option' in str(e)) + self.assertEqual(utils_config.ConfigNoOptionError, e.__class__) + self.assertTrue("no option" in str(e)) # Test setter. - config['f'] = 'test' - self.assertEqual(config['f'], 'test') + config["f"] = "test" + self.assertEqual(config["f"], "test") # Test deleter. - del config['f'] + del config["f"] try: - config['f'] + config["f"] except Exception as e: - self.assertEqual( - utils_config.ConfigNoOptionError, e.__class__) - self.assertTrue('no option' in str(e)) + self.assertEqual(utils_config.ConfigNoOptionError, e.__class__) + self.assertTrue("no option" in str(e)) # Test contain. - self.assertTrue('a' in config) - self.assertFalse('f' in config) + self.assertTrue("a" in config) + self.assertFalse("f" in config) finally: os.remove(config_path) @@ -135,61 +132,61 @@ def test_specific_accessers(self): config_file.close() try: - config_file = open(config_path, 'w') + config_file = open(config_path, "w") config_file.write(content) config_file.close() config = utils_config.SectionlessConfig(config_path) try: - config.set_string('a', 'Hi') - self.assertEqual(config['a'], '"Hi"') - self.assertEqual(config.get_string('a'), 'Hi') - config['a'] = "'Hi'" - self.assertEqual(config.get_string('a'), 'Hi') - config['a'] = 'Hi' - self.assertRaises(ValueError, config.get_string, 'a') - config['a'] = '"Hi' - self.assertRaises(ValueError, config.get_string, 'a') - - config.set_int('a', 15) - self.assertEqual(config['a'], '15') - self.assertEqual(config.get_int('a'), 15) - config.set_int('a', -15) - self.assertEqual(config.get_int('a'), -15) - config.set_string('a', 'invalid') - self.assertRaises(ValueError, config.get_float, 'a') - - config.set_float('a', 15.123) - self.assertEqual(config['a'], '15.123') - self.assertEqual(config.get_float('a'), 15.123) - config.set_string('a', 'invalid') - self.assertRaises(ValueError, config.get_float, 'a') - - config.set_boolean('a', True) - self.assertEqual(config['a'], '1') - self.assertTrue(config.get_boolean('a')) - config.set_string('a', 'Yes') - self.assertTrue(config.get_boolean('a')) - config.set_string('a', 'ON') - self.assertTrue(config.get_boolean('a')) - config.set_boolean('a', False) - self.assertEqual(config['a'], '0') - self.assertFalse(config.get_boolean('a')) - config.set_string('a', 'fAlSe') - self.assertFalse(config.get_boolean('a')) - config.set_string('a', 'off') - self.assertFalse(config.get_boolean('a')) - config.set_string('a', 'invalid') - self.assertRaises(ValueError, config.get_boolean, 'a') - - config.set_list('a', [15, 'Hello']) - self.assertEqual(config['a'], '["15", "Hello"]') - config.set_list('a', [15, 'Hello']) - self.assertEqual(config.get_list('a'), ["15", "Hello"]) - config['a'] = '[15, \n "Hello"]' - self.assertEqual(config.get_list('a'), ["15", "Hello"]) - config['a'] = '[15, "Hi, there"]' - self.assertEqual(config.get_list('a'), ["15", "Hi, there"]) + config.set_string("a", "Hi") + self.assertEqual(config["a"], '"Hi"') + self.assertEqual(config.get_string("a"), "Hi") + config["a"] = "'Hi'" + self.assertEqual(config.get_string("a"), "Hi") + config["a"] = "Hi" + self.assertRaises(ValueError, config.get_string, "a") + config["a"] = '"Hi' + self.assertRaises(ValueError, config.get_string, "a") + + config.set_int("a", 15) + self.assertEqual(config["a"], "15") + self.assertEqual(config.get_int("a"), 15) + config.set_int("a", -15) + self.assertEqual(config.get_int("a"), -15) + config.set_string("a", "invalid") + self.assertRaises(ValueError, config.get_float, "a") + + config.set_float("a", 15.123) + self.assertEqual(config["a"], "15.123") + self.assertEqual(config.get_float("a"), 15.123) + config.set_string("a", "invalid") + self.assertRaises(ValueError, config.get_float, "a") + + config.set_boolean("a", True) + self.assertEqual(config["a"], "1") + self.assertTrue(config.get_boolean("a")) + config.set_string("a", "Yes") + self.assertTrue(config.get_boolean("a")) + config.set_string("a", "ON") + self.assertTrue(config.get_boolean("a")) + config.set_boolean("a", False) + self.assertEqual(config["a"], "0") + self.assertFalse(config.get_boolean("a")) + config.set_string("a", "fAlSe") + self.assertFalse(config.get_boolean("a")) + config.set_string("a", "off") + self.assertFalse(config.get_boolean("a")) + config.set_string("a", "invalid") + self.assertRaises(ValueError, config.get_boolean, "a") + + config.set_list("a", [15, "Hello"]) + self.assertEqual(config["a"], '["15", "Hello"]') + config.set_list("a", [15, "Hello"]) + self.assertEqual(config.get_list("a"), ["15", "Hello"]) + config["a"] = '[15, \n "Hello"]' + self.assertEqual(config.get_list("a"), ["15", "Hello"]) + config["a"] = '[15, "Hi, there"]' + self.assertEqual(config.get_list("a"), ["15", "Hi, there"]) finally: config.restore() finally: @@ -203,44 +200,42 @@ def test_restore(self): # Restore after use. try: - config_file = open(config_path, 'w') + config_file = open(config_path, "w") config_file.write(content) config_file.close() config = utils_config.SectionlessConfig(config_path) try: # Change the config. - config['f'] = 'test' - self.assertEqual(config['f'], 'test') - del config['e'] + config["f"] = "test" + self.assertEqual(config["f"], "test") + del config["e"] finally: config.restore() finally: final_file = open(config_path) try: - self.assertEqual(final_file.read(), - content) + self.assertEqual(final_file.read(), content) finally: final_file.close() os.remove(config_path) # Don't restore after use. try: - config_file = open(config_path, 'w') + config_file = open(config_path, "w") config_file.write(content) config_file.close() config = utils_config.SectionlessConfig(config_path) # Change the config. - config['f'] = 'test' - self.assertEqual(config['f'], 'test') - del config['e'] + config["f"] = "test" + self.assertEqual(config["f"], "test") + del config["e"] finally: final_file = open(config_path) try: - self.assertEqual(final_file.read(), - changed_content) + self.assertEqual(final_file.read(), changed_content) finally: final_file.close() os.remove(config_path) @@ -252,22 +247,21 @@ def test_sync_file(self): config_file.close() try: - config_file = open(config_path, 'w') + config_file = open(config_path, "w") config_file.write(content) config_file.close() config = utils_config.SectionlessConfig(config_path) try: # Change the config. - config['f'] = 'test' - self.assertEqual(config['f'], 'test') - del config['e'] + config["f"] = "test" + self.assertEqual(config["f"], "test") + del config["e"] # Test the change is applied to target file. cur_file = open(config_path) try: - self.assertEqual(cur_file.read(), - changed_content) + self.assertEqual(cur_file.read(), changed_content) finally: cur_file.close() finally: @@ -277,19 +271,18 @@ def test_sync_file(self): class LibvirtConfigCommonTest(unittest.TestCase): - class UnimplementedConfig(utils_config.LibvirtConfigCommon): pass class NoTypesConfig(utils_config.LibvirtConfigCommon): - conf_path = '/tmp/config_unittest.conf' + conf_path = "/tmp/config_unittest.conf" class UndefinedTypeConfig(utils_config.LibvirtConfigCommon): __option_types__ = { - 'test': 'invalid_type', - 'test2': 'boolean', + "test": "invalid_type", + "test2": "boolean", } - conf_path = '/tmp/config_unittest.conf' + conf_path = "/tmp/config_unittest.conf" def test_unimplemented(self): try: @@ -313,8 +306,8 @@ def test_undefined_type(self): self.assertTrue("don't exists" in str(e)) try: - config_file = open('/tmp/config_unittest.conf', 'w') - config_file.write('') + config_file = open("/tmp/config_unittest.conf", "w") + config_file.write("") config_file.close() config = self.UndefinedTypeConfig() @@ -328,18 +321,20 @@ def test_undefined_type(self): self.assertEqual(config.test2, False) # Set unknown type try: - config.test = '1' + config.test = "1" except Exception as e: self.assertEqual( - utils_config.LibvirtConfigUnknownKeyTypeError, e.__class__) - self.assertTrue('Unknown type' in str(e)) + utils_config.LibvirtConfigUnknownKeyTypeError, e.__class__ + ) + self.assertTrue("Unknown type" in str(e)) # Get unknown type try: print(config.test) except Exception as e: self.assertEqual( - utils_config.LibvirtConfigUnknownKeyTypeError, e.__class__) - self.assertTrue('Unknown type' in str(e)) + utils_config.LibvirtConfigUnknownKeyTypeError, e.__class__ + ) + self.assertTrue("Unknown type" in str(e)) # Set Get not defined type config.test3 = "abc" self.assertEqual(config.test3, "abc") @@ -356,30 +351,29 @@ def test_undefined_type(self): del config.test except Exception as e: self.assertEqual( - utils_config.LibvirtConfigUnknownKeyTypeError, e.__class__) - self.assertTrue('Unknown type' in str(e)) + utils_config.LibvirtConfigUnknownKeyTypeError, e.__class__ + ) + self.assertTrue("Unknown type" in str(e)) # Not defined option try: del config.test3 except Exception as e: - self.assertEqual( - utils_config.LibvirtConfigUnknownKeyError, e.__class__) - self.assertTrue('Unknown config key' in str(e)) + self.assertEqual(utils_config.LibvirtConfigUnknownKeyError, e.__class__) + self.assertTrue("Unknown config key" in str(e)) config.restore() finally: - os.remove('/tmp/config_unittest.conf') + os.remove("/tmp/config_unittest.conf") class LibvirtConfigTest(unittest.TestCase): - def test_accessers(self): config_file = tempfile.NamedTemporaryFile() config_path = config_file.name config_file.close() try: - config_file = open(config_path, 'w') - config_file.write('') + config_file = open(config_path, "w") + config_file.write("") config_file.close() config = utils_config.LibvirtdConfig(path=config_path) @@ -390,45 +384,43 @@ def test_accessers(self): try: config.undefined_property except Exception as e: - self.assertEqual( - utils_config.LibvirtConfigUnknownKeyError, e.__class__) - self.assertTrue('Unknown config key' in str(e)) + self.assertEqual(utils_config.LibvirtConfigUnknownKeyError, e.__class__) + self.assertTrue("Unknown config key" in str(e)) # Test defined boolean property. self.assertEqual(config.listen_tls, None) config.listen_tls = 1 - self.assertEqual(config.get_raw('listen_tls'), '1') + self.assertEqual(config.get_raw("listen_tls"), "1") self.assertEqual(config.listen_tls, 1) config.listen_tls = False - self.assertEqual(config.get_raw('listen_tls'), '0') + self.assertEqual(config.get_raw("listen_tls"), "0") self.assertEqual(config.listen_tls, 0) config.listen_tls = "1" - self.assertEqual(config.get_raw('listen_tls'), '1') + self.assertEqual(config.get_raw("listen_tls"), "1") config.listen_tls = "undefined" - self.assertEqual(config.get_raw('listen_tls'), 'undefined') + self.assertEqual(config.get_raw("listen_tls"), "undefined") del config.listen_tls self.assertEqual(config.listen_tls, None) # Test defined string property. self.assertEqual(config.host_uuid, None) config.host_uuid = 1 - self.assertEqual(config.get_raw('host_uuid'), '"1"') - config.host_uuid = 'a' - self.assertEqual(config.get_raw('host_uuid'), '"a"') + self.assertEqual(config.get_raw("host_uuid"), '"1"') + config.host_uuid = "a" + self.assertEqual(config.get_raw("host_uuid"), '"a"') # Test defined integer property. self.assertEqual(config.max_clients, None) config.max_clients = 1 - self.assertEqual(config.get_raw('max_clients'), '1') + self.assertEqual(config.get_raw("max_clients"), "1") # Test defined list property. self.assertEqual(config.access_drivers, None) config.access_drivers = [1, "a"] - self.assertEqual( - config.get_raw('access_drivers'), '["1", "a"]') + self.assertEqual(config.get_raw("access_drivers"), '["1", "a"]') finally: os.remove(config_path) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_utils_env.py b/selftests/unit/test_utils_env.py index 4ef301076a..40983c48d4 100644 --- a/selftests/unit/test_utils_env.py +++ b/selftests/unit/test_utils_env.py @@ -8,7 +8,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import utils_env @@ -17,15 +17,15 @@ class FakeVm(object): - def __init__(self, vm_name, params): self.name = vm_name self.params = params - self.vm_type = self.params.get('vm_type') - self.driver_type = self.params.get('driver_type') - self.instance = ("%s-%s" % ( + self.vm_type = self.params.get("vm_type") + 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))) + utils_misc.generate_random_string(16), + ) def get_params(self): return self.params @@ -35,11 +35,11 @@ def is_alive(self): class FakeSyncListenServer(object): - - def __init__(self, address='', port=123, tmpdir=None): - self.instance = ("%s-%s" % ( + 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))) + utils_misc.generate_random_string(16), + ) self.port = port def close(self): @@ -47,7 +47,6 @@ def close(self): class TestEnv(unittest.TestCase): - def setUp(self): self.envfilename = "/dev/shm/EnvUnittest" + self.id() @@ -68,13 +67,13 @@ def test_save(self): env = utils_env.Env() self.assertRaises(utils_env.EnvSaveError, env.save, {}) - params = utils_params.Params({"main_vm": 'rhel7-migration'}) - vm1 = FakeVm(params['main_vm'], params) + params = utils_params.Params({"main_vm": "rhel7-migration"}) + vm1 = FakeVm(params["main_vm"], params) vm1.is_alive() - env.register_vm(params['main_vm'], vm1) + env.register_vm(params["main_vm"], vm1) env.save(filename=self.envfilename) env2 = utils_env.Env(filename=self.envfilename) - vm2 = env2.get_vm(params['main_vm']) + vm2 = env2.get_vm(params["main_vm"]) vm2.is_alive() assert vm1.instance == vm2.instance @@ -97,11 +96,11 @@ def test_register_vm(self): 4) Verify that the 2 objects are the same. """ env = utils_env.Env(filename=self.envfilename) - params = utils_params.Params({"main_vm": 'rhel7-migration'}) - vm1 = FakeVm(params['main_vm'], params) + params = utils_params.Params({"main_vm": "rhel7-migration"}) + vm1 = FakeVm(params["main_vm"], params) vm1.is_alive() - env.register_vm(params['main_vm'], vm1) - vm2 = env.get_vm(params['main_vm']) + env.register_vm(params["main_vm"], vm1) + vm2 = env.get_vm(params["main_vm"]) vm2.is_alive() assert vm1 == vm2 @@ -114,16 +113,16 @@ def test_unregister_vm(self): 5) Verify that the removed vm is no longer in env. """ env = utils_env.Env(filename=self.envfilename) - params = utils_params.Params({"main_vm": 'rhel7-migration'}) - vm1 = FakeVm(params['main_vm'], params) + params = utils_params.Params({"main_vm": "rhel7-migration"}) + vm1 = FakeVm(params["main_vm"], params) vm1.is_alive() - vm2 = FakeVm('vm2', params) + vm2 = FakeVm("vm2", params) vm2.is_alive() - env.register_vm(params['main_vm'], vm1) - env.register_vm('vm2', vm2) + env.register_vm(params["main_vm"], vm1) + env.register_vm("vm2", vm2) assert vm1 in env.get_all_vms() assert vm2 in env.get_all_vms() - env.unregister_vm('vm2') + env.unregister_vm("vm2") assert vm1 in env.get_all_vms() assert vm2 not in env.get_all_vms() @@ -136,13 +135,13 @@ def test_get_all_vms(self): 5) Verify that the sync server is not in the output of get_all_vms. """ env = utils_env.Env(filename=self.envfilename) - params = utils_params.Params({"main_vm": 'rhel7-migration'}) - vm1 = FakeVm(params['main_vm'], params) + params = utils_params.Params({"main_vm": "rhel7-migration"}) + vm1 = FakeVm(params["main_vm"], params) vm1.is_alive() - vm2 = FakeVm('vm2', params) + vm2 = FakeVm("vm2", params) vm2.is_alive() - env.register_vm(params['main_vm'], vm1) - env.register_vm('vm2', vm2) + env.register_vm(params["main_vm"], vm1) + env.register_vm("vm2", vm2) sync1 = FakeSyncListenServer(port=333) env.register_syncserver(333, sync1) assert vm1 in env.get_all_vms() @@ -209,8 +208,7 @@ def _update_env(env, key, value): if termination_event.is_set(): break - changing_thread = threading.Thread(target=update_env, - args=(env,)) + changing_thread = threading.Thread(target=update_env, args=(env,)) changing_thread.start() time.sleep(0.3) try: @@ -219,5 +217,5 @@ def _update_env(env, key, value): termination_event.set() -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_utils_libguestfs.py b/selftests/unit/test_utils_libguestfs.py index 25db21836a..a37b086077 100644 --- a/selftests/unit/test_utils_libguestfs.py +++ b/selftests/unit/test_utils_libguestfs.py @@ -9,29 +9,28 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import utils_libguestfs as lgf class LibguestfsTest(unittest.TestCase): - def test_lgf_cmd_check(self): - cmds = ['virt-ls', 'virt-cat'] + cmds = ["virt-ls", "virt-cat"] for cmd in cmds: try: path.find_command(cmd) self.assertTrue(lgf.lgf_cmd_check(cmd)) except path.CmdNotFoundError: - logging.warning("Command %s not installed, skipping " - "unittest...", cmd) + logging.warning( + "Command %s not installed, skipping " "unittest...", cmd + ) def test_lgf_cmd_check_raises(self): - cmds = ['virt-test-fail', ''] + cmds = ["virt-test-fail", ""] for cmd in cmds: - self.assertRaises(lgf.LibguestfsCmdError, - lgf.lgf_cmd_check, cmd) + self.assertRaises(lgf.LibguestfsCmdError, lgf.lgf_cmd_check, cmd) def test_lgf_cmd(self): cmd = "libguestfs-test-tool" @@ -39,12 +38,10 @@ def test_lgf_cmd(self): path.find_command(cmd) self.assertEqual(lgf.lgf_command(cmd).exit_status, 0) except path.CmdNotFoundError: - logging.warning("Command %s not installed, skipping unittest...", - cmd) + logging.warning("Command %s not installed, skipping unittest...", cmd) class SlotsCheckTest(unittest.TestCase): - def test_LibguestfsBase_default_slots(self): """Default slots' value check""" lfb = lgf.LibguestfsBase() @@ -69,16 +66,17 @@ def test_Guestfish_slots(self): try: gf = lgf.Guestfish() self.assertEqual(gf.lgf_exec, "guestfish") - gf = lgf.Guestfish( - disk_img="test.img", ro_mode=True, inspector=True) + gf = lgf.Guestfish(disk_img="test.img", ro_mode=True, inspector=True) self.assertEqual(gf.lgf_exec, "guestfish -a 'test.img' --ro -i") - gf = lgf.Guestfish(libvirt_domain="test", inspector=True, - uri="qemu+ssh://root@EXAMPLE/system") + gf = lgf.Guestfish( + libvirt_domain="test", + inspector=True, + uri="qemu+ssh://root@EXAMPLE/system", + ) gf_cmd = "guestfish -c 'qemu+ssh://root@EXAMPLE/system' -d 'test' -i" self.assertEqual(gf.lgf_exec, gf_cmd) except lgf.LibguestfsCmdError: - logging.warning("Command guestfish not present, skipping " - "unittest...") + logging.warning("Command guestfish not present, skipping " "unittest...") if __name__ == "__main__": diff --git a/selftests/unit/test_utils_misc.py b/selftests/unit/test_utils_misc.py index 690f73c4e7..683bc4a804 100644 --- a/selftests/unit/test_utils_misc.py +++ b/selftests/unit/test_utils_misc.py @@ -10,7 +10,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest.unittest_utils import mock @@ -20,7 +20,6 @@ class TestUtilsMisc(unittest.TestCase): - def test_cpu_vendor_intel(self): cpu_info = """processor : 0 vendor_id : GenuineIntel @@ -29,7 +28,7 @@ def test_cpu_vendor_intel(self): model name : Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz """ vendor = utils_misc.get_cpu_vendor(cpu_info, False) - self.assertEqual(vendor, 'GenuineIntel') + self.assertEqual(vendor, "GenuineIntel") def test_cpu_vendor_amd(self): cpu_info = """processor : 3 @@ -39,30 +38,26 @@ def test_cpu_vendor_amd(self): model name : AMD A10-5800K APU with Radeon(tm) HD Graphics """ vendor = utils_misc.get_cpu_vendor(cpu_info, False) - self.assertEqual(vendor, 'AuthenticAMD') + self.assertEqual(vendor, "AuthenticAMD") def test_vendor_unknown(self): cpu_info = "this is an unknown cpu" vendor = utils_misc.get_cpu_vendor(cpu_info, False) - self.assertEqual(vendor, 'unknown') + self.assertEqual(vendor, "unknown") def test_get_archive_tarball_name(self): - tarball_name = utils_misc.get_archive_tarball_name('/tmp', - 'tmp-archive', - 'bz2') - self.assertEqual(tarball_name, 'tmp-archive.tar.bz2') + tarball_name = utils_misc.get_archive_tarball_name("/tmp", "tmp-archive", "bz2") + self.assertEqual(tarball_name, "tmp-archive.tar.bz2") def test_get_archive_tarball_name_absolute(self): - tarball_name = utils_misc.get_archive_tarball_name('/tmp', - '/var/tmp/tmp', - 'bz2') - self.assertEqual(tarball_name, '/var/tmp/tmp.tar.bz2') + tarball_name = utils_misc.get_archive_tarball_name( + "/tmp", "/var/tmp/tmp", "bz2" + ) + self.assertEqual(tarball_name, "/var/tmp/tmp.tar.bz2") def test_get_archive_tarball_name_from_dir(self): - tarball_name = utils_misc.get_archive_tarball_name('/tmp', - None, - 'bz2') - self.assertEqual(tarball_name, 'tmp.tar.bz2') + tarball_name = utils_misc.get_archive_tarball_name("/tmp", None, "bz2") + self.assertEqual(tarball_name, "tmp.tar.bz2") def test_git_repo_param_helper(self): config = """git_repo_foo_uri = git://git.foo.org/foo.git @@ -74,11 +69,11 @@ def test_git_repo_param_helper(self): config_parser.parse_string(config) params = next(config_parser.get_dicts()) - h = build_helper.GitRepoParamHelper(params, 'foo', '/tmp/foo') - self.assertEqual(h.name, 'foo') - self.assertEqual(h.branch, 'next') - self.assertEqual(h.lbranch, 'local') - self.assertEqual(h.commit, 'bc732ad8b2ed8be52160b893735417b43a1e91a8') + h = build_helper.GitRepoParamHelper(params, "foo", "/tmp/foo") + self.assertEqual(h.name, "foo") + self.assertEqual(h.branch, "next") + self.assertEqual(h.lbranch, "local") + self.assertEqual(h.commit, "bc732ad8b2ed8be52160b893735417b43a1e91a8") def test_normalize_data_size(self): n1 = utils_misc.normalize_data_size("12M") @@ -96,11 +91,11 @@ def test_normalize_data_size(self): class FakeCmd(object): - def __init__(self, cmd): self.fake_cmds = [ - {"cmd": "numactl --hardware", - "stdout": """ + { + "cmd": "numactl --hardware", + "stdout": """ available: 1 nodes (0) node 0 cpus: 0 1 2 3 4 5 6 7 node 0 size: 18431 MB @@ -108,9 +103,11 @@ def __init__(self, cmd): node distances: node 0 0: 10 -"""}, - {"cmd": "ps -eLf | awk '{print $4}'", - "stdout": """ +""", + }, + { + "cmd": "ps -eLf | awk '{print $4}'", + "stdout": """ 1230 1231 1232 @@ -119,7 +116,8 @@ def __init__(self, cmd): 1235 1236 1237 -"""}, +""", + }, {"cmd": "taskset -cp 0 1230", "stdout": ""}, {"cmd": "taskset -cp 1 1231", "stdout": ""}, {"cmd": "taskset -cp 2 1232", "stdout": ""}, @@ -128,15 +126,14 @@ def __init__(self, cmd): {"cmd": "taskset -cp 5 1235", "stdout": ""}, {"cmd": "taskset -cp 6 1236", "stdout": ""}, {"cmd": "taskset -cp 7 1237", "stdout": ""}, - ] self.stdout = self.get_stdout(cmd) def get_stdout(self, cmd): for fake_cmd in self.fake_cmds: - if fake_cmd['cmd'] == cmd: - return fake_cmd['stdout'] + if fake_cmd["cmd"] == cmd: + return fake_cmd["stdout"] raise ValueError("Could not locate locate '%s' on fake cmd db" % cmd) @@ -149,10 +146,9 @@ def utils_run(cmd, shell=True): class TestNumaNode(unittest.TestCase): - def setUp(self): self.god = mock.mock_god(ut=self) - self.god.stub_with(process, 'run', utils_run) + self.god.stub_with(process, "run", utils_run) all_nodes = tempfile.NamedTemporaryFile(delete=False) all_nodes.write(all_nodes_contents) all_nodes.close() @@ -161,12 +157,12 @@ def setUp(self): online_nodes.close() self.all_nodes_path = all_nodes.name self.online_nodes_path = online_nodes.name - self.numa_node = utils_misc.NumaNode(-1, - self.all_nodes_path, - self.online_nodes_path) + self.numa_node = utils_misc.NumaNode( + -1, self.all_nodes_path, self.online_nodes_path + ) def test_get_node_cpus(self): - self.assertEqual(self.numa_node.get_node_cpus(0), '0 1 2 3 4 5 6 7') + self.assertEqual(self.numa_node.get_node_cpus(0), "0 1 2 3 4 5 6 7") def test_pin_cpu(self): self.assertEqual(self.numa_node.pin_cpu("1230"), "0") @@ -207,15 +203,63 @@ def test_free_cpu(self): self.assertEqual(self.numa_node.dict["1"], ["1231"]) def test_bitlist_to_string(self): - string = 'foo' - bitlist = [0, 1, 1, 0, 0, 1, 1, 0, 0, 1, - 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1] + string = "foo" + bitlist = [ + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + ] self.assertEqual(utils_misc.string_to_bitlist(string), bitlist) def test_string_to_bitlist(self): - bitlist = [0, 1, 1, 0, 0, 0, 1, 0, 0, 1, - 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0] - string = 'bar' + bitlist = [ + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + ] + string = "bar" self.assertEqual(utils_misc.bitlist_to_string(bitlist), string) def tearDown(self): @@ -224,5 +268,5 @@ def tearDown(self): os.unlink(self.online_nodes_path) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_utils_net.py b/selftests/unit/test_utils_net.py index 02f7f0e023..a17d6fe9d1 100644 --- a/selftests/unit/test_utils_net.py +++ b/selftests/unit/test_utils_net.py @@ -16,7 +16,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest.unittest_utils import mock @@ -32,15 +32,15 @@ class FakeVm(object): - def __init__(self, vm_name, params): self.name = vm_name self.params = params - self.vm_type = self.params.get('vm_type') - self.driver_type = self.params.get('driver_type') - self.instance = ("%s-%s" % ( + self.vm_type = self.params.get("vm_type") + 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))) + utils_misc.generate_random_string(16), + ) def get_params(self): return self.params @@ -50,7 +50,6 @@ def is_alive(self): class TestBridge(unittest.TestCase): - class FakeCmd(object): iter = 0 @@ -78,7 +77,8 @@ def __init__(self, *args, **kargs): virbr10-nic virbr40-nic virbr50-nic -"""] +""", + ] self.stdout = self.get_stdout() self.__class__.iter += 1 @@ -87,7 +87,7 @@ def get_stdout(self): try: return self.fake_cmds[self.__class__.iter] except IndexError: - return '' + return "" def setUp(self): self.god = mock.mock_god(ut=self) @@ -95,31 +95,43 @@ def setUp(self): def utils_run(*args, **kargs): return TestBridge.FakeCmd(*args, **kargs) - self.god.stub_with(process, 'run', utils_run) + self.god.stub_with(process, "run", utils_run) def test_getstructure(self): br = utils_net.Bridge().get_structure() self.assertEqual( - br, {'virbr1': {'iface': ['em1', 'virbr1-nic'], 'stp': 'yes'}, - 'virbr0': {'iface': ['virbr0-nic'], 'stp': 'yes'}}) + br, + { + "virbr1": {"iface": ["em1", "virbr1-nic"], "stp": "yes"}, + "virbr0": {"iface": ["virbr0-nic"], "stp": "yes"}, + }, + ) br = utils_net.Bridge().get_structure() - self.assertEqual(br, {'virbr0': {"iface": [], "stp": "yes"}}) + self.assertEqual(br, {"virbr0": {"iface": [], "stp": "yes"}}) br = utils_net.Bridge().get_structure() self.assertEqual(br, {}) br = utils_net.Bridge().get_structure() - self.assertEqual(br, {'virbr2': {"iface": ['em1', 'virbr10-nic', - 'virbr40-nic', 'virbr50-nic'], - "stp": "yes"}, - 'virbr1': {"iface": ['em1', 'virbr1-nic', - 'virbr4-nic', 'virbr5-nic'], - "stp": "yes"}, - 'virbr0': {"iface": ['virbr0-nic', 'virbr2-nic', - 'virbr3-nic'], - "stp": "yes"}}) + self.assertEqual( + br, + { + "virbr2": { + "iface": ["em1", "virbr10-nic", "virbr40-nic", "virbr50-nic"], + "stp": "yes", + }, + "virbr1": { + "iface": ["em1", "virbr1-nic", "virbr4-nic", "virbr5-nic"], + "stp": "yes", + }, + "virbr0": { + "iface": ["virbr0-nic", "virbr2-nic", "virbr3-nic"], + "stp": "yes", + }, + }, + ) def tearDown(self): self.god.unstub_all() @@ -149,6 +161,7 @@ def loop_assert(self, virtiface, test_keys, what_func): def test_half_set(self): def what_func(propertea): return props[propertea] + half_prop_end = (len(self.VirtIface.__all_slots__) // 2) + 1 props = {} for propertea in self.VirtIface.__all_slots__[0:half_prop_end]: @@ -159,6 +172,7 @@ def what_func(propertea): def test_full_set(self): def what_func(propertea): return props[propertea] + props = {} for propertea in self.VirtIface.__all_slots__: props[propertea] = utils_misc.generate_random_string(16) @@ -169,8 +183,10 @@ def test_apendex_set(self): """ Verify container ignores unknown key names """ + def what_func(propertea): return props[propertea] + props = {} for propertea in self.VirtIface.__all_slots__: props[propertea] = utils_misc.generate_random_string(16) @@ -188,35 +204,43 @@ def what_func(propertea): self.loop_assert(virtiface, list(props.keys()), what_func) def test_mac_completer(self): - for test_mac in ['9a', '01:02:03:04:05:06', '00', '1:2:3:4:5:6', - '0a:0b:0c:0d:0e:0f', 'A0:B0:C0:D0:E0:F0', - "01:02:03:04:05:", "01:02:03:04::", "01:02::::", - "00:::::::::::::::::::", ":::::::::::::::::::", - ":"]: + for test_mac in [ + "9a", + "01:02:03:04:05:06", + "00", + "1:2:3:4:5:6", + "0a:0b:0c:0d:0e:0f", + "A0:B0:C0:D0:E0:F0", + "01:02:03:04:05:", + "01:02:03:04::", + "01:02::::", + "00:::::::::::::::::::", + ":::::::::::::::::::", + ":", + ]: # Tosses an exception if test_mac can't be completed self.VirtIface.complete_mac_address(test_mac) - self.assertRaises(TypeError, self.VirtIface.complete_mac_address, - '01:f0:0:ba:r!:00') - self.assertRaises(TypeError, self.VirtIface.complete_mac_address, - "01:02:03::05:06") + self.assertRaises( + TypeError, self.VirtIface.complete_mac_address, "01:f0:0:ba:r!:00" + ) + self.assertRaises( + TypeError, self.VirtIface.complete_mac_address, "01:02:03::05:06" + ) class TestQemuIface(TestVirtIface): - def setUp(self): super(TestQemuIface, self).setUp() self.VirtIface = utils_net.QemuIface class TestLibvirtIface(TestVirtIface): - def setUp(self): super(TestLibvirtIface, self).setUp() self.VirtIface = utils_net.LibvirtIface class TestVmNetStyle(unittest.TestCase): - def setUp(self): logging.disable(logging.INFO) logging.disable(logging.WARNING) @@ -225,21 +249,20 @@ 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)) - self.assertEqual(style['mac_prefix'], '9a') - self.assertEqual(style['container_class'], utils_net.QemuIface) - self.assert_(issubclass(style['container_class'], utils_net.VirtIface)) + style = self.get_style( + utils_misc.generate_random_string(16), utils_misc.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)) - self.assertEqual(style['container_class'], utils_net.LibvirtIface) - self.assert_(issubclass(style['container_class'], utils_net.VirtIface)) + style = self.get_style("libvirt", utils_misc.generate_random_string(16)) + self.assertEqual(style["container_class"], utils_net.LibvirtIface) + self.assert_(issubclass(style["container_class"], utils_net.VirtIface)) class TestVmNet(unittest.TestCase): - def setUp(self): logging.disable(logging.INFO) logging.disable(logging.WARNING) @@ -252,33 +275,30 @@ def test_string_container(self): def test_VirtIface_container(self): test_data = [ - {'nic_name': 'nic1', - 'mac': '0a'}, - {'nic_name': ''}, # test data index 1 - {'foo': 'bar', - 'nic_name': 'nic2'}, - {'nic_name': 'nic3', - 'mac': '01:02:03:04:05:06'} + {"nic_name": "nic1", "mac": "0a"}, + {"nic_name": ""}, # test data index 1 + {"foo": "bar", "nic_name": "nic2"}, + {"nic_name": "nic3", "mac": "01:02:03:04:05:06"}, ] - self.assertRaises(utils_net.VMNetError, - utils_net.VMNet, - utils_net.VirtIface, test_data) + self.assertRaises( + utils_net.VMNetError, utils_net.VMNet, utils_net.VirtIface, test_data + ) del test_data[1] vmnet = utils_net.VMNet(utils_net.VirtIface, test_data) - self.assertEqual(vmnet[0].nic_name, test_data[0]['nic_name']) - self.assertEqual(vmnet['nic1'].__class__, utils_net.VirtIface) - self.assertEqual(False, hasattr(vmnet['nic1'], 'mac')) - self.assertEqual(False, 'mac' in list(vmnet['nic1'].keys())) - self.assertEqual(vmnet.nic_name_list(), ['nic1', 'nic2', 'nic3']) - self.assertEqual(vmnet.nic_name_index('nic2'), 1) + self.assertEqual(vmnet[0].nic_name, test_data[0]["nic_name"]) + self.assertEqual(vmnet["nic1"].__class__, utils_net.VirtIface) + self.assertEqual(False, hasattr(vmnet["nic1"], "mac")) + self.assertEqual(False, "mac" in list(vmnet["nic1"].keys())) + self.assertEqual(vmnet.nic_name_list(), ["nic1", "nic2", "nic3"]) + self.assertEqual(vmnet.nic_name_index("nic2"), 1) self.assertRaises(TypeError, vmnet.nic_name_index, 0) - self.assertEqual(True, hasattr(vmnet[2], 'mac')) - self.assertEqual(test_data[2]['mac'], vmnet[2]['mac']) + self.assertEqual(True, hasattr(vmnet[2], "mac")) + self.assertEqual(test_data[2]["mac"], vmnet[2]["mac"]) class TestVmNetSubclasses(unittest.TestCase): - nettests_cartesian = (""" + nettests_cartesian = """ variants: - onevm: vms=vm1 @@ -346,10 +366,10 @@ class TestVmNetSubclasses(unittest.TestCase): nettype_nic3 = bridge netdst_nic3 = virbr3 netdev_nic3 = qwerty - """) + """ mac_prefix = "01:02:03:04:05:" - db_filename = '/dev/shm/UnitTest_AddressPool' + db_filename = "/dev/shm/UnitTest_AddressPool" db_item_count = 0 counter = 0 # for printing dots @@ -371,15 +391,15 @@ def setUp(self): for d in parser.get_dicts(): params = utils_params.Params(d) self.CartesianResult.append(params) - for vm_name in params.objects('vms'): + for vm_name in params.objects("vms"): vm = params.object_params(vm_name) - nics = vm.get('nics') + nics = vm.get("nics") if nics and len(nics.split()) > 0: self.db_item_count += 1 def fakevm_generator(self): for params in self.CartesianResult: - for vm_name in params.get('vms').split(): + for vm_name in params.get("vms").split(): # Return iterator covering all types of vms # in exactly the same order each time. For more info, see: # http://docs.python.org/reference/simple_stmts.html#yield @@ -407,25 +427,28 @@ def test_cmp_Virtnet(self): if to_test < 1: break fvm1p = fakevm1.get_params() - fakevm1.virtnet = utils_net.VirtNet(fvm1p, fakevm1.name, - fakevm1.instance, - self.db_filename) + fakevm1.virtnet = utils_net.VirtNet( + fvm1p, fakevm1.name, fakevm1.instance, self.db_filename + ) if len(fakevm1.virtnet) < 2: continue fakevm2 = FakeVm(fakevm1.name + "_2", fvm1p) - fakevm2.virtnet = utils_net.VirtNet(fvm1p, fakevm2.name, - fakevm2.instance, - self.db_filename) + fakevm2.virtnet = utils_net.VirtNet( + fvm1p, fakevm2.name, fakevm2.instance, self.db_filename + ) # Verify nic order doesn't matter fvm3p = utils_params.Params(list(fvm1p.items())) # work on copy - nic_list = fvm1p.object_params(fakevm1.name).get( - "nics", fvm1p.get('nics', "")).split() + nic_list = ( + fvm1p.object_params(fakevm1.name) + .get("nics", fvm1p.get("nics", "")) + .split() + ) random.shuffle(nic_list) - fvm3p['nics'] = " ".join(nic_list) + fvm3p["nics"] = " ".join(nic_list) fakevm3 = FakeVm(fakevm1.name + "_3", fvm3p) - fakevm3.virtnet = utils_net.VirtNet(fvm3p, fakevm3.name, - fakevm3.instance, - self.db_filename) + fakevm3.virtnet = utils_net.VirtNet( + fvm3p, fakevm3.name, fakevm3.instance, self.db_filename + ) self.assertTrue(fakevm1.virtnet == fakevm1.virtnet) self.assertTrue(fakevm1.virtnet == fakevm2.virtnet) self.assertTrue(fakevm1.virtnet == fakevm3.virtnet) @@ -448,15 +471,17 @@ def test_01_Params(self): self.zero_counter() for fakevm in self.fakevm_generator(): test_params = fakevm.get_params() - virtnet = utils_net.ParamsNet(test_params, - fakevm.name) + virtnet = utils_net.ParamsNet(test_params, fakevm.name) self.assert_(virtnet.container_class) self.assert_(virtnet.mac_prefix) self.assert_(issubclass(virtnet.__class__, list)) # Assume params actually came from CartesianResult because # Checking this takes a very long time across all combinations - param_nics = test_params.object_params(fakevm.name).get( - "nics", test_params.get('nics', "")).split() + param_nics = ( + test_params.object_params(fakevm.name) + .get("nics", test_params.get("nics", "")) + .split() + ) # Size of list should match number of nics configured self.assertEqual(len(param_nics), len(virtnet)) # Test each interface data @@ -464,18 +489,20 @@ def test_01_Params(self): # index correspondence already established/asserted virtnet_nic = virtnet[virtnet_index] params_nic = param_nics[virtnet_index] - self.assert_(issubclass(virtnet_nic.__class__, - propcan.PropCan)) - self.assertEqual(virtnet_nic.nic_name, params_nic, - "%s != %s" % (virtnet_nic.nic_name, - params_nic)) + self.assert_(issubclass(virtnet_nic.__class__, propcan.PropCan)) + self.assertEqual( + virtnet_nic.nic_name, + params_nic, + "%s != %s" % (virtnet_nic.nic_name, params_nic), + ) # __slots__ functionality established/asserted elsewhere props_to_check = list(utils_net.VirtIface.__all_slots__) # other tests check mac address handling - del props_to_check[props_to_check.index('mac')] + del props_to_check[props_to_check.index("mac")] for propertea in props_to_check: - params_propertea = test_params.object_params(params_nic - ).get(propertea) + params_propertea = test_params.object_params(params_nic).get( + propertea + ) # Double-verify dual-mode access works try: virtnet_propertea1 = getattr(virtnet_nic, propertea) @@ -486,8 +513,7 @@ def test_01_Params(self): # Only check stuff cartesian config actually set if params_propertea: self.assertEqual(params_propertea, virtnet_propertea1) - self.assertEqual( - virtnet_propertea1, virtnet_propertea2) + self.assertEqual(virtnet_propertea1, virtnet_propertea2) self.print_and_inc() def test_02_db(self): @@ -501,22 +527,23 @@ def test_02_db(self): self.zero_counter() for fakevm in self.fakevm_generator(): test_params = fakevm.get_params() - virtnet = utils_net.DbNet(test_params, fakevm.name, - self.db_filename, fakevm.instance) - self.assert_(hasattr(virtnet, 'container_class')) - self.assert_(hasattr(virtnet, 'mac_prefix')) - self.assert_(not hasattr(virtnet, 'lock')) - self.assert_(not hasattr(virtnet, 'db')) + virtnet = utils_net.DbNet( + test_params, fakevm.name, self.db_filename, fakevm.instance + ) + self.assert_(hasattr(virtnet, "container_class")) + self.assert_(hasattr(virtnet, "mac_prefix")) + self.assert_(not hasattr(virtnet, "lock")) + self.assert_(not hasattr(virtnet, "db")) vm_name_params = test_params.object_params(fakevm.name) - nic_name_list = vm_name_params.objects('nics') + nic_name_list = vm_name_params.objects("nics") for nic_name in nic_name_list: # nic name is only in params scope - nic_dict = {'nic_name': nic_name} + nic_dict = {"nic_name": nic_name} nic_params = test_params.object_params(nic_name) # avoid processing unsupported properties proplist = list(virtnet.container_class().__all_slots__) # name was already set, remove from __slots__ list copy - del proplist[proplist.index('nic_name')] + del proplist[proplist.index("nic_name")] for propertea in proplist: nic_dict[propertea] = nic_params.get(propertea) virtnet.append(nic_dict) @@ -539,7 +566,7 @@ def test_03_db(self): self.assert_(len(db_value) > 0) self.assert_(isinstance(db_value[0], dict)) for nic in db_value: - mac = nic.get('mac') + mac = nic.get("mac") if mac: # Another test already checked mac_is_valid behavior self.assert_(utils_net.VirtIface.mac_is_valid(mac)) @@ -563,19 +590,21 @@ def test_04_VirtNet(self): mac = "%s0%x" % (self.mac_prefix, lastbyte) else: mac = "%s%x" % (self.mac_prefix, lastbyte) - params = utils_params.Params({ - "nics": "nic1", - "vms": vm_name, - "mac_nic1": mac, - }) - virtnet = utils_net.VirtNet(params, vm_name, - vm_name, self.db_filename) + params = utils_params.Params( + { + "nics": "nic1", + "vms": vm_name, + "mac_nic1": mac, + } + ) + virtnet = utils_net.VirtNet(params, vm_name, vm_name, self.db_filename) virtnet.mac_prefix = self.mac_prefix - self.assertEqual(virtnet['nic1'].mac, mac) + self.assertEqual(virtnet["nic1"].mac, mac) self.assertEqual(virtnet.get_mac_address(0), mac) # Confirm only lower-case macs are stored - self.assertEqual(virtnet.get_mac_address(0).lower(), - virtnet.get_mac_address(0)) + self.assertEqual( + virtnet.get_mac_address(0).lower(), virtnet.get_mac_address(0) + ) self.assertEqual(virtnet.mac_list(), [mac]) self.print_and_inc() @@ -590,17 +619,13 @@ def test_05_VirtNet(self): # also confirming params merge with db data for lastbyte in xrange(0, 0xFF): vm_name = "vm%d" % lastbyte - params = utils_params.Params({ - "nics": "nic1", - "vms": vm_name - }) - virtnet = utils_net.VirtNet(params, vm_name, - vm_name, self.db_filename) + params = utils_params.Params({"nics": "nic1", "vms": vm_name}) + virtnet = utils_net.VirtNet(params, vm_name, vm_name, self.db_filename) if lastbyte < 16: mac = "%s0%x" % (self.mac_prefix, lastbyte) else: mac = "%s%x" % (self.mac_prefix, lastbyte) - self.assertEqual(virtnet['nic1'].mac, mac) + self.assertEqual(virtnet["nic1"].mac, mac) self.assertEqual(virtnet.get_mac_address(0), mac) self.print_and_inc() @@ -612,59 +637,49 @@ def test_06_VirtNet(self): """ self.zero_counter(25) # test two nics, second mac generation should fail (pool exhausted) - params = utils_params.Params({ - "nics": "nic1 nic2", - "vms": "vm255" - }) - virtnet = utils_net.VirtNet(params, 'vm255', - 'vm255', self.db_filename) + params = utils_params.Params({"nics": "nic1 nic2", "vms": "vm255"}) + virtnet = utils_net.VirtNet(params, "vm255", "vm255", self.db_filename) virtnet.mac_prefix = self.mac_prefix - self.assertRaises(AttributeError, virtnet.get_mac_address, 'nic1') + self.assertRaises(AttributeError, virtnet.get_mac_address, "nic1") mac = "%s%x" % (self.mac_prefix, 255) # This will grab the last available address # only try 300 times, guarantees LASTBYTE counter will loop once self.assertEqual(virtnet.generate_mac_address(0, 300), mac) # This will fail allocation - self.assertRaises(utils_net.NetError, - virtnet.generate_mac_address, 1, 300) + self.assertRaises(utils_net.NetError, virtnet.generate_mac_address, 1, 300) def test_07_VirtNet(self): """ Release mac from beginning, middle, and end, re-generate + verify value """ self.zero_counter(1) - beginning_params = utils_params.Params({ - "nics": "nic1 nic2", - "vms": "vm0" - }) - middle_params = utils_params.Params({ - "nics": "nic1 nic2", - "vms": "vm127" - }) - end_params = utils_params.Params({ - "nics": "nic1 nic2", - "vms": "vm255", - }) + beginning_params = utils_params.Params({"nics": "nic1 nic2", "vms": "vm0"}) + middle_params = utils_params.Params({"nics": "nic1 nic2", "vms": "vm127"}) + end_params = utils_params.Params( + { + "nics": "nic1 nic2", + "vms": "vm255", + } + ) for params in (beginning_params, middle_params, end_params): - vm_name = params['vms'] - virtnet = utils_net.VirtNet(params, vm_name, - vm_name, self.db_filename) + vm_name = params["vms"] + virtnet = utils_net.VirtNet(params, vm_name, vm_name, self.db_filename) virtnet.mac_prefix = self.mac_prefix - iface = virtnet['nic1'] + iface = virtnet["nic1"] last_db_mac_byte = iface.mac_str_to_int_list(iface.mac)[-1] last_vm_name_byte = int(vm_name[2:]) # Sequential generation from test_04_VirtNet guarantee self.assertEqual(last_db_mac_byte, last_vm_name_byte) # only try 300 times, guarantees LASTBYTE counter will loop once - self.assertRaises( - utils_net.NetError, virtnet.generate_mac_address, 1, 300) + self.assertRaises(utils_net.NetError, virtnet.generate_mac_address, 1, 300) virtnet.free_mac_address(0) virtnet.free_mac_address(1) # generate new on nic1 to verify mac_index generator catches it # and to signify database updated after generation virtnet.generate_mac_address(1, 300) - last_db_mac_byte = virtnet['nic2'].mac_str_to_int_list( - virtnet['nic2'].mac)[-1] + last_db_mac_byte = virtnet["nic2"].mac_str_to_int_list(virtnet["nic2"].mac)[ + -1 + ] self.assertEqual(last_db_mac_byte, last_vm_name_byte) self.assertEqual(virtnet.get_mac_address(1), virtnet[1].mac) self.print_and_inc() @@ -672,12 +687,10 @@ def test_07_VirtNet(self): def test_08_ifname(self): for fakevm in self.fakevm_generator(): # only need to test kvm instance - if fakevm.vm_type != 'qemu': + if fakevm.vm_type != "qemu": continue test_params = fakevm.get_params() - virtnet = utils_net.VirtNet(test_params, - fakevm.name, - fakevm.name) + virtnet = utils_net.VirtNet(test_params, fakevm.name, fakevm.name) for virtnet_index in xrange(0, len(virtnet)): result = virtnet.generate_ifname(virtnet_index) self.assertEqual(result, virtnet[virtnet_index].ifname) @@ -694,5 +707,5 @@ def test_99_ifname(self): pass -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_utils_params.py b/selftests/unit/test_utils_params.py index 35a023fca2..4ee2b1ba0f 100644 --- a/selftests/unit/test_utils_params.py +++ b/selftests/unit/test_utils_params.py @@ -7,85 +7,94 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import utils_params BASE_DICT = { - 'image_boot': 'yes', - 'image_boot_stg': 'no', - 'image_chain': '', - 'image_clone_command': 'cp --reflink=auto %s %s', - 'image_format': 'qcow2', - 'image_format_stg': 'qcow2', - 'image_name': 'images/f18-64', - 'image_name_stg': 'enospc', - 'image_raw_device': 'no', - 'image_remove_command': 'rm -rf %s', - 'image_size': '10G', - 'image_snapshot_stg': 'no', - 'image_unbootable_pattern': 'Hard Disk.*not a bootable disk', - 'image_verify_bootable': 'yes', - 'images': 'image1 stg', + "image_boot": "yes", + "image_boot_stg": "no", + "image_chain": "", + "image_clone_command": "cp --reflink=auto %s %s", + "image_format": "qcow2", + "image_format_stg": "qcow2", + "image_name": "images/f18-64", + "image_name_stg": "enospc", + "image_raw_device": "no", + "image_remove_command": "rm -rf %s", + "image_size": "10G", + "image_snapshot_stg": "no", + "image_unbootable_pattern": "Hard Disk.*not a bootable disk", + "image_verify_bootable": "yes", + "images": "image1 stg", } -CORRECT_RESULT_MAPPING = {"image1": {'image_boot_stg': 'no', - 'image_snapshot_stg': 'no', - 'image_chain': '', - 'image_unbootable_pattern': 'Hard Disk.*not a bootable disk', - 'image_name': 'images/f18-64', - 'image_remove_command': 'rm -rf %s', - 'image_name_stg': 'enospc', - 'image_clone_command': 'cp --reflink=auto %s %s', - 'image_size': '10G', 'images': 'image1 stg', - 'image_raw_device': 'no', - 'image_format': 'qcow2', - 'image_boot': 'yes', - 'image_verify_bootable': 'yes', - 'image_format_stg': 'qcow2'}, - "stg": {'image_snapshot': 'no', - 'image_boot_stg': 'no', - 'image_snapshot_stg': 'no', - 'image_chain': '', - 'image_unbootable_pattern': 'Hard Disk.*not a bootable disk', - 'image_name': 'enospc', - 'image_remove_command': 'rm -rf %s', - 'image_name_stg': 'enospc', - 'image_clone_command': 'cp --reflink=auto %s %s', - 'image_size': '10G', - 'images': 'image1 stg', - 'image_raw_device': 'no', - 'image_format': 'qcow2', - 'image_boot': 'no', - 'image_verify_bootable': 'yes', - 'image_format_stg': 'qcow2'}} +CORRECT_RESULT_MAPPING = { + "image1": { + "image_boot_stg": "no", + "image_snapshot_stg": "no", + "image_chain": "", + "image_unbootable_pattern": "Hard Disk.*not a bootable disk", + "image_name": "images/f18-64", + "image_remove_command": "rm -rf %s", + "image_name_stg": "enospc", + "image_clone_command": "cp --reflink=auto %s %s", + "image_size": "10G", + "images": "image1 stg", + "image_raw_device": "no", + "image_format": "qcow2", + "image_boot": "yes", + "image_verify_bootable": "yes", + "image_format_stg": "qcow2", + }, + "stg": { + "image_snapshot": "no", + "image_boot_stg": "no", + "image_snapshot_stg": "no", + "image_chain": "", + "image_unbootable_pattern": "Hard Disk.*not a bootable disk", + "image_name": "enospc", + "image_remove_command": "rm -rf %s", + "image_name_stg": "enospc", + "image_clone_command": "cp --reflink=auto %s %s", + "image_size": "10G", + "images": "image1 stg", + "image_raw_device": "no", + "image_format": "qcow2", + "image_boot": "no", + "image_verify_bootable": "yes", + "image_format_stg": "qcow2", + }, +} class TestParams(unittest.TestCase): - def setUp(self): self.params = utils_params.Params(BASE_DICT) def testObjects(self): - self.assertEquals(self.params.objects("images"), ['image1', 'stg']) + self.assertEquals(self.params.objects("images"), ["image1", "stg"]) def testObjectsParams(self): for key in list(CORRECT_RESULT_MAPPING.keys()): - self.assertEquals(self.params.object_params(key), - CORRECT_RESULT_MAPPING[key]) + self.assertEquals( + self.params.object_params(key), CORRECT_RESULT_MAPPING[key] + ) def testGetItemMissing(self): try: - self.params['bogus'] - raise ValueError("Did not get a ParamNotFound error when trying " - "to access a non-existing param") + self.params["bogus"] + raise ValueError( + "Did not get a ParamNotFound error when trying " + "to access a non-existing param" + ) # pylint: disable=E0712 except utils_params.ParamNotFound: pass def testGetItem(self): - self.assertEqual(self.params['image_size'], "10G") + self.assertEqual(self.params["image_size"], "10G") def testGetBoolean(self): self.params["foo1"] = "yes" @@ -94,42 +103,46 @@ def testGetBoolean(self): self.params["foo4"] = "off" self.params["foo5"] = "true" self.params["foo6"] = "false" - self.assertEqual(True, self.params.get_boolean('foo1')) - self.assertEqual(False, self.params.get_boolean('foo2')) - self.assertEqual(True, self.params.get_boolean('foo3')) - self.assertEqual(False, self.params.get_boolean('foo4')) - self.assertEqual(True, self.params.get_boolean('foo5')) - self.assertEqual(False, self.params.get_boolean('foo6')) - self.assertEqual(False, self.params.get_boolean('notgiven')) + self.assertEqual(True, self.params.get_boolean("foo1")) + self.assertEqual(False, self.params.get_boolean("foo2")) + self.assertEqual(True, self.params.get_boolean("foo3")) + self.assertEqual(False, self.params.get_boolean("foo4")) + self.assertEqual(True, self.params.get_boolean("foo5")) + self.assertEqual(False, self.params.get_boolean("foo6")) + self.assertEqual(False, self.params.get_boolean("notgiven")) def testGetNumeric(self): self.params["something"] = "7" self.params["foobar"] = 11 self.params["barsome"] = 13.17 - self.assertEqual(7, self.params.get_numeric('something')) - self.assertEqual(7, self.params.get_numeric('something'), int) - self.assertEqual(7.0, self.params.get_numeric('something'), float) - self.assertEqual(11, self.params.get_numeric('foobar')) - self.assertEqual(11, self.params.get_numeric('something'), int) - self.assertEqual(11.0, self.params.get_numeric('foobar'), float) - self.assertEqual(13, self.params.get_numeric('barsome')) - self.assertEqual(13, self.params.get_numeric('barsome'), int) - self.assertEqual(13.17, self.params.get_numeric('barsome'), float) - self.assertEqual(17, self.params.get_numeric('joke', 17)) - self.assertEqual(17.13, self.params.get_numeric('joke', 17.13), float) + self.assertEqual(7, self.params.get_numeric("something")) + self.assertEqual(7, self.params.get_numeric("something"), int) + self.assertEqual(7.0, self.params.get_numeric("something"), float) + self.assertEqual(11, self.params.get_numeric("foobar")) + self.assertEqual(11, self.params.get_numeric("something"), int) + self.assertEqual(11.0, self.params.get_numeric("foobar"), float) + self.assertEqual(13, self.params.get_numeric("barsome")) + self.assertEqual(13, self.params.get_numeric("barsome"), int) + self.assertEqual(13.17, self.params.get_numeric("barsome"), float) + self.assertEqual(17, self.params.get_numeric("joke", 17)) + self.assertEqual(17.13, self.params.get_numeric("joke", 17.13), float) def testGetList(self): self.params["primes"] = "7 11 13 17" self.params["dashed"] = "7-11-13" - self.assertEqual(["7", "11", "13", "17"], self.params.get_list('primes')) - self.assertEqual([7, 11, 13, 17], self.params.get_list('primes', "1 2 3", " ", int)) - self.assertEqual([1, 2, 3], self.params.get_list('missing', "1 2 3", " ", int)) - self.assertEqual([7, 11, 13], self.params.get_list('dashed', "1 2 3", "-", int)) + self.assertEqual(["7", "11", "13", "17"], self.params.get_list("primes")) + self.assertEqual( + [7, 11, 13, 17], self.params.get_list("primes", "1 2 3", " ", int) + ) + self.assertEqual([1, 2, 3], self.params.get_list("missing", "1 2 3", " ", int)) + self.assertEqual([7, 11, 13], self.params.get_list("dashed", "1 2 3", "-", int)) def testGetDict(self): self.params["dummy"] = "name1=value1 name2=value2" - self.assertEqual({"name1": "value1", "name2": "value2"}, self.params.get_dict('dummy')) - result_dict = self.params.get_dict('dummy', need_order=True) + self.assertEqual( + {"name1": "value1", "name2": "value2"}, self.params.get_dict("dummy") + ) + result_dict = self.params.get_dict("dummy", need_order=True) right_dict, wrong_dict = OrderedDict(), OrderedDict() right_dict["name1"] = "value1" right_dict["name2"] = "value2" diff --git a/selftests/unit/test_versionable_class.py b/selftests/unit/test_versionable_class.py index a8bc1e7ac1..37770be3bf 100644 --- a/selftests/unit/test_versionable_class.py +++ b/selftests/unit/test_versionable_class.py @@ -3,6 +3,7 @@ import unittest import os import sys + try: import pickle as cPickle except ImportError: @@ -10,7 +11,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest.unittest_utils import mock @@ -34,7 +35,7 @@ class VM(object): def _is_right_ver(cls, *args, **kargs): ver = None if "qemu_version" in kargs: - ver = kargs['qemu_version'] + ver = kargs["qemu_version"] else: ver = qemu_version() if ver < 1: @@ -66,7 +67,7 @@ class VM1(VM): def _is_right_ver(cls, *args, **kargs): ver = None if "qemu_version" in kargs: - ver = kargs['qemu_version'] + ver = kargs["qemu_version"] else: ver = qemu_version() if ver > 1: @@ -117,12 +118,11 @@ def system_version(): class System(object): - @classmethod def _is_right_ver(cls, *args, **kargs): ver = None if "system_version" in kargs: - ver = kargs['system_version'] + ver = kargs["system_version"] else: ver = system_version() if ver < 1: @@ -139,12 +139,11 @@ def __str__(self): class System1(System): - @classmethod def _is_right_ver(cls, *args, **kargs): ver = None if "system_version" in kargs: - ver = kargs['system_version'] + ver = kargs["system_version"] else: ver = system_version() if ver > 1: @@ -168,12 +167,11 @@ def __new__(cls, *args, **kargs): class Q(object): - @classmethod def _is_right_ver(cls, *args, **kargs): ver = None if "q_version" in kargs: - ver = kargs['q_version'] + ver = kargs["q_version"] else: ver = system_version() if ver < 1: @@ -190,12 +188,11 @@ def __str__(self): class Q1(Q): - @classmethod def _is_right_ver(cls, *args, **kargs): ver = None if "q_version" in kargs: - ver = kargs['q_version'] + ver = kargs["q_version"] else: ver = system_version() if ver > 1: @@ -216,12 +213,11 @@ class Q_Container(VersionableClass): class Sys(Q_Container): - @classmethod def _is_right_ver(cls, *args, **kargs): ver = None if "system_version" in kargs: - ver = kargs['system_version'] + ver = kargs["system_version"] else: ver = system_version() if ver < 1: @@ -238,12 +234,11 @@ def __str__(self): class Sys1(Sys): - @classmethod def _is_right_ver(cls, *args, **kargs): ver = None if "system_version" in kargs: - ver = kargs['system_version'] + ver = kargs["system_version"] else: ver = system_version() if ver > 1: @@ -267,13 +262,11 @@ def __new__(cls, *args, **kargs): class AA(Sys_Container, BB, System_Container): - def __new__(cls, *args, **kargs): return super(man[cls, AA], cls).__new__(cls, *args, **kargs) class TestVersionableClass(unittest.TestCase): - def setUp(self): self.god = mock.mock_god(ut=self) self.version = 1 @@ -291,8 +284,8 @@ def test_simple_versioning(self): mm = factory(BB)() # check class name. self.assertEqual(str(mm), "managed_BB_VM1") - mm.func2() # call BB.func2(m) -> VM1.func2 - mm.func1() # call VM1.func1(m) -> VM.func1 + mm.func2() # call BB.func2(m) -> VM1.func2 + mm.func1() # call VM1.func1(m) -> VM.func1 self.god.check_playback() @@ -308,7 +301,7 @@ def wrap(mm): mm = factory(BB, qemu_version=0)() # check class name. self.assertEqual(str(mm), "managed_BB_VM") - mm.func3() # call VM1.func1(m) -> VM.func1 + mm.func3() # call VM1.func1(m) -> VM.func1 self.assertRaises(AttributeError, wrap, mm) self.god.check_playback() @@ -322,7 +315,7 @@ def test_simple_create_by_params_v1(self): mm = factory(BB, qemu_version=2)() # check class name. self.assertEqual(str(mm), "managed_BB_VM1") - mm.func3() # call VM1.func1(m) -> VM.func1 + mm.func3() # call VM1.func1(m) -> VM.func1 self.assertEqual(mm.VM1_cls, "VM1") self.god.check_playback() @@ -337,17 +330,13 @@ def test_sharing_data_in_same_version(self): man[bb.__class__, BB].test_class_bb = 2 man[cc.__class__, BB].test_class_bb = 3 # check class name. - self.assertEqual(bb.__class__.test_class_vm1, - mm.__class__.test_class_vm1) - self.assertEqual(bb.__class__.test_class_bb, - mm.__class__.test_class_bb) + self.assertEqual(bb.__class__.test_class_vm1, mm.__class__.test_class_vm1) + self.assertEqual(bb.__class__.test_class_bb, mm.__class__.test_class_bb) # In class hierarchy is class which don't have to be versioned # because that first value should be equal and second one shouldn't. - self.assertEqual(bb.__class__.test_class_vm1, - cc.__class__.test_class_vm1) - self.assertNotEqual(bb.__class__.test_class_bb, - cc.__class__.test_class_bb) + self.assertEqual(bb.__class__.test_class_vm1, cc.__class__.test_class_vm1) + self.assertNotEqual(bb.__class__.test_class_bb, cc.__class__.test_class_bb) def test_complicated_versioning(self): self.god.stub_function(VM, "func3") @@ -358,7 +347,7 @@ def test_complicated_versioning(self): mm = factory(AA)() # check class name. self.assertEqual(str(mm), "managed_AA_Sys1_Q1_VM1_System1") - mm.func3() # call VM1.func1(m) -> VM.func1 + mm.func3() # call VM1.func1(m) -> VM.func1 self.god.check_playback() @@ -371,7 +360,7 @@ def test_complicated_multiple_create_params(self): mm = factory(AA, qemu_version=0, system_version=2, q_version=0)() # check class name. self.assertEqual(str(mm), "managed_AA_Sys1_Q_VM_System1") - mm.func3() # call VM1.func1(m) -> VM.func1 + mm.func3() # call VM1.func1(m) -> VM.func1 self.god.check_playback() diff --git a/selftests/unit/test_virsh.py b/selftests/unit/test_virsh.py index faaffae55d..b0b4d72698 100644 --- a/selftests/unit/test_virsh.py +++ b/selftests/unit/test_virsh.py @@ -10,7 +10,7 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) @@ -21,20 +21,27 @@ def process_is_alive(name_pattern): So look only for command whose initial pathname ends with name. Name itself is an egrep pattern, so it can use | etc for variations. """ - return process.system("pgrep -f '^([^ /]*/)*(%s)([ ]|$)'" % name_pattern, - ignore_status=True, shell=True) == 0 + return ( + process.system( + "pgrep -f '^([^ /]*/)*(%s)([ ]|$)'" % name_pattern, + ignore_status=True, + shell=True, + ) + == 0 + ) class bogusVirshFailureException(unittest.TestCase.failureException): - def __init__(self, *args, **dargs): self.virsh_args = args self.virsh_dargs = dargs def __str__(self): - msg = ("Codepath under unittest attempted call to un-mocked virsh" - " method, with args: '%s' and dargs: '%s'" - % (self.virsh_args, self.virsh_dargs)) + msg = ( + "Codepath under unittest attempted call to un-mocked virsh" + " method, with args: '%s' and dargs: '%s'" + % (self.virsh_args, self.virsh_dargs) + ) return msg @@ -52,9 +59,9 @@ def raise_bogusVirshFailureException(*args, **dargs): if preserve is None: preserve = [] - fake_virsh = virsh.Virsh(virsh_exec='/bin/false', - uri='qemu:///system', debug=True, - ignore_status=True) + fake_virsh = virsh.Virsh( + virsh_exec="/bin/false", uri="qemu:///system", debug=True, ignore_status=True + ) # Make all virsh commands throw an exception by calling it for symbol in dir(virsh): # Get names of just closure functions by Virsh class @@ -71,22 +78,19 @@ class ModuleLoad(unittest.TestCase): class ConstantsTest(ModuleLoad): - def test_ModuleLoad(self): - self.assertTrue(hasattr(self.virsh, 'NOCLOSE')) - self.assertTrue(hasattr(self.virsh, 'SCREENSHOT_ERROR_COUNT')) - self.assertTrue(hasattr(self.virsh, 'VIRSH_COMMAND_CACHE')) - self.assertTrue(hasattr(self.virsh, 'VIRSH_EXEC')) + self.assertTrue(hasattr(self.virsh, "NOCLOSE")) + self.assertTrue(hasattr(self.virsh, "SCREENSHOT_ERROR_COUNT")) + self.assertTrue(hasattr(self.virsh, "VIRSH_COMMAND_CACHE")) + self.assertTrue(hasattr(self.virsh, "VIRSH_EXEC")) class TestVirshClosure(ModuleLoad): - @staticmethod def somefunc(*args, **dargs): return (args, dargs) class SomeClass(dict): - def somemethod(self): return "foobar" @@ -102,9 +106,9 @@ def test_args(self): VC = self.virsh.VirshClosure tcinst = self.SomeClass() vcinst = VC(self.somefunc, tcinst) - args, dargs = vcinst('foo') + args, dargs = vcinst("foo") self.assertEqual(len(args), 1) - self.assertEqual(args[0], 'foo') + self.assertEqual(args[0], "foo") self.assertEqual(len(dargs), 0) def test_fake_virsh(self): @@ -118,59 +122,59 @@ def test_fake_virsh(self): def test_dargs(self): # save some typing VC = self.virsh.VirshClosure - tcinst = self.SomeClass(foo='bar') + tcinst = self.SomeClass(foo="bar") vcinst = VC(self.somefunc, tcinst) args, dargs = vcinst() self.assertEqual(len(args), 0) self.assertEqual(len(dargs), 1) - self.assertEqual(list(dargs.keys()), ['foo']) - self.assertEqual(list(dargs.values()), ['bar']) + self.assertEqual(list(dargs.keys()), ["foo"]) + self.assertEqual(list(dargs.values()), ["bar"]) def test_args_and_dargs(self): # save some typing VC = self.virsh.VirshClosure - tcinst = self.SomeClass(foo='bar') + tcinst = self.SomeClass(foo="bar") vcinst = VC(self.somefunc, tcinst) - args, dargs = vcinst('foo') + args, dargs = vcinst("foo") self.assertEqual(len(args), 1) - self.assertEqual(args[0], 'foo') + self.assertEqual(args[0], "foo") self.assertEqual(len(dargs), 1) - self.assertEqual(list(dargs.keys()), ['foo']) - self.assertEqual(list(dargs.values()), ['bar']) + self.assertEqual(list(dargs.keys()), ["foo"]) + self.assertEqual(list(dargs.values()), ["bar"]) def test_args_dargs_subclass(self): # save some typing VC = self.virsh.VirshClosure - tcinst = self.SomeClass(foo='bar') + tcinst = self.SomeClass(foo="bar") vcinst = VC(self.somefunc, tcinst) - args, dargs = vcinst('foo') + args, dargs = vcinst("foo") self.assertEqual(len(args), 1) - self.assertEqual(args[0], 'foo') + self.assertEqual(args[0], "foo") self.assertEqual(len(dargs), 1) - self.assertEqual(list(dargs.keys()), ['foo']) - self.assertEqual(list(dargs.values()), ['bar']) + self.assertEqual(list(dargs.keys()), ["foo"]) + self.assertEqual(list(dargs.values()), ["bar"]) def test_update_args_dargs_subclass(self): # save some typing VC = self.virsh.VirshClosure - tcinst = self.SomeClass(foo='bar') + tcinst = self.SomeClass(foo="bar") vcinst = VC(self.somefunc, tcinst) - args, dargs = vcinst('foo') + args, dargs = vcinst("foo") self.assertEqual(len(args), 1) - self.assertEqual(args[0], 'foo') + self.assertEqual(args[0], "foo") self.assertEqual(len(dargs), 1) - self.assertEqual(list(dargs.keys()), ['foo']) - self.assertEqual(list(dargs.values()), ['bar']) + self.assertEqual(list(dargs.keys()), ["foo"]) + self.assertEqual(list(dargs.values()), ["bar"]) # Update dictionary - tcinst['sna'] = 'fu' + tcinst["sna"] = "fu" # Is everything really the same? - args, dargs = vcinst('foo', 'baz') + args, dargs = vcinst("foo", "baz") self.assertEqual(len(args), 2) - self.assertEqual(args[0], 'foo') - self.assertEqual(args[1], 'baz') + self.assertEqual(args[0], "foo") + self.assertEqual(args[1], "baz") self.assertEqual(len(dargs), 2) - self.assertEqual(dargs['foo'], 'bar') - self.assertEqual(dargs['sna'], 'fu') + self.assertEqual(dargs["foo"], "bar") + self.assertEqual(dargs["sna"], "fu") def test_multi_inst(self): # save some typing @@ -188,12 +192,11 @@ def test_multi_inst(self): self.assertEqual(args2[0], 2) self.assertEqual(len(dargs1), 1) self.assertEqual(len(dargs2), 1) - self.assertEqual(dargs1['darg1'], 1) - self.assertEqual(dargs2['darg1'], 2) + self.assertEqual(dargs1["darg1"], 1) + self.assertEqual(dargs2["darg1"], 2) class ConstructorsTest(ModuleLoad): - def test_VirshBase(self): vb = self.virsh.VirshBase() del vb # keep pylint happy @@ -204,7 +207,7 @@ def test_Virsh(self): def test_VirshPersistent(self): test_virsh = self.virsh.Virsh() - if test_virsh['virsh_exec'] == '/bin/true': + if test_virsh["virsh_exec"] == "/bin/true": return else: logging.disable(logging.INFO) @@ -214,6 +217,7 @@ def test_VirshPersistent(self): def TestVirshClosure(self): class MyDict(dict): pass + vc = self.virsh.VirshClosure(None, MyDict()) del vc # keep pylint happy @@ -224,14 +228,13 @@ class ModuleLoadCheckVirsh(unittest.TestCase): def run(self, *args, **dargs): test_virsh = self.virsh.Virsh() - if test_virsh['virsh_exec'] == '/bin/true': + if test_virsh["virsh_exec"] == "/bin/true": return # Don't run any tests, no virsh executable was found else: super(ModuleLoadCheckVirsh, self).run(*args, **dargs) class SessionManagerTest(ModuleLoadCheckVirsh): - def test_del_VirshPersistent(self): """ Unittest for __del__ of VirshPersistent. @@ -251,7 +254,7 @@ def test_VirshSession(self): This test use VirshSession over VirshPersistent with auto_close=True. """ - virsh_exec = self.virsh.Virsh()['virsh_exec'] + virsh_exec = self.virsh.Virsh()["virsh_exec"] # Build a VirshSession object. session_1 = self.virsh.VirshSession(virsh_exec, auto_close=True) self.assertTrue(process_is_alive(virsh_exec)) @@ -262,7 +265,7 @@ def test_VirshPersistent(self): """ Unittest for session manager of VirshPersistent. """ - virsh_exec = self.virsh.Virsh()['virsh_exec'] + virsh_exec = self.virsh.Virsh()["virsh_exec"] vp_1 = self.virsh.VirshPersistent() self.assertTrue(process_is_alive(virsh_exec)) # Init the vp_2 with same params of vp_1. @@ -279,32 +282,31 @@ def test_VirshPersistent(self): class VirshHasHelpCommandTest(ModuleLoadCheckVirsh): - def setUp(self): # subclasses override self.virsh self.VIRSH_COMMAND_CACHE = self.virsh.VIRSH_COMMAND_CACHE def test_false_command(self): - self.assertFalse(self.virsh.has_help_command('print')) - self.assertFalse(self.virsh.has_help_command('Commands:')) - self.assertFalse(self.virsh.has_help_command('dom')) - self.assertFalse(self.virsh.has_help_command('pool')) + self.assertFalse(self.virsh.has_help_command("print")) + self.assertFalse(self.virsh.has_help_command("Commands:")) + self.assertFalse(self.virsh.has_help_command("dom")) + self.assertFalse(self.virsh.has_help_command("pool")) def test_true_command(self): - self.assertTrue(self.virsh.has_help_command('uri')) - self.assertTrue(self.virsh.has_help_command('help')) - self.assertTrue(self.virsh.has_help_command('list')) + self.assertTrue(self.virsh.has_help_command("uri")) + self.assertTrue(self.virsh.has_help_command("help")) + self.assertTrue(self.virsh.has_help_command("list")) def test_no_cache(self): self.VIRSH_COMMAND_CACHE = None - self.assertTrue(self.virsh.has_help_command('uri')) + self.assertTrue(self.virsh.has_help_command("uri")) self.VIRSH_COMMAND_CACHE = [] - self.assertTrue(self.virsh.has_help_command('uri')) + self.assertTrue(self.virsh.has_help_command("uri")) def test_subcommand_help(self): - regex = r'--command' - self.assertTrue(self.virsh.has_command_help_match('help', regex)) - self.assertFalse(self.virsh.has_command_help_match('uri', regex)) + regex = r"--command" + self.assertTrue(self.virsh.has_command_help_match("help", regex)) + self.assertFalse(self.virsh.has_command_help_match("uri", regex)) def test_groups_in_commands(self): # groups will be empty in older libvirt, but test will still work @@ -325,7 +327,6 @@ def test_groups_in_commands(self): class VirshHelpCommandTest(ModuleLoadCheckVirsh): - def test_cache_command(self): l1 = self.virsh.help_command(cache=True) l2 = self.virsh.help_command() @@ -336,7 +337,6 @@ def test_cache_command(self): class VirshClassHasHelpCommandTest(VirshHasHelpCommandTest): - def setUp(self): logging.disable(logging.INFO) super(VirshClassHasHelpCommandTest, self).setUp() @@ -344,7 +344,6 @@ def setUp(self): class VirshPersistentClassHasHelpCommandTest(VirshHasHelpCommandTest): - def setUp(self): logging.disable(logging.INFO) super(VirshPersistentClassHasHelpCommandTest, self).setUp() @@ -363,5 +362,5 @@ def tearDown(self): self.assertFalse(process_is_alive(self.virsh.virsh_exec)) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_vt_init.py b/selftests/unit/test_vt_init.py index 7d932afd45..b50b8a5fac 100644 --- a/selftests/unit/test_vt_init.py +++ b/selftests/unit/test_vt_init.py @@ -7,7 +7,6 @@ class VtInitTest(unittest.TestCase): - @staticmethod def _swap(): data_dir = os.path.join(BASEDIR, "selftests", ".data") @@ -27,11 +26,11 @@ def test_early_settings(self): When the `avocado.core.settings` is imported the vt plugin is initialized. """ - settings = getattr(importlib.import_module('avocado.core.settings'), - 'settings') - tmp_dir = settings.as_dict().get('vt.common.tmp_dir', "") - address_pool_filename = getattr(importlib.import_module( - 'virttest.utils_net'), 'ADDRESS_POOL_FILENAME') + settings = getattr(importlib.import_module("avocado.core.settings"), "settings") + tmp_dir = settings.as_dict().get("vt.common.tmp_dir", "") + address_pool_filename = getattr( + importlib.import_module("virttest.utils_net"), "ADDRESS_POOL_FILENAME" + ) self.assertEqual(tmp_dir, "/tmp") self.assertEqual(address_pool_filename, "/tmp/address_pool") @@ -39,5 +38,5 @@ def tearDown(self): self._swap() -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/selftests/unit/test_wrappers.py b/selftests/unit/test_wrappers.py index 56cba94dbf..c136a3642d 100644 --- a/selftests/unit/test_wrappers.py +++ b/selftests/unit/test_wrappers.py @@ -12,29 +12,31 @@ from virttest import _wrappers -def create_module(name, inner_val, path=''): - """ Creates a module with a variable in it named inner_variable whose - value is equal to the one set - :param name: name of the module - :type name: String - :param inner_val: value that will be assigned to inner_variable - :type inner_val: Int - :param path: path to the module - :type path: String +def create_module(name, inner_val, path=""): + """Creates a module with a variable in it named inner_variable whose + value is equal to the one set + :param name: name of the module + :type name: String + :param inner_val: value that will be assigned to inner_variable + :type inner_val: Int + :param path: path to the module + :type path: String """ - module_code = """# This is a file created during virttest._wrappers tests + module_code = ( + """# This is a file created during virttest._wrappers tests # This file should be deleted once the tests are finished inner_variable = %s -""" % inner_val - if path != '' and not os.path.isdir(path): +""" + % inner_val + ) + if path != "" and not os.path.isdir(path): os.makedirs(path) - with open(os.path.join(path, f"{name}.py"), 'w') as new_module: + with open(os.path.join(path, f"{name}.py"), "w") as new_module: new_module.write(module_code) def check_imported_module(testcase, name, module, value): - """ Wraps general checks that are repeated across almost all tests. - """ + """Wraps general checks that are repeated across almost all tests.""" testcase.assertIsNotNone(module) testcase.assertEqual(module.inner_variable, value) testcase.assertTrue(module in sys.modules.values()) @@ -52,8 +54,9 @@ class baseImportTests(ABC): @classmethod def setUpClass(cls): create_module(cls._tmp_in_module_name, cls._indir_inner_val) - create_module(cls._tmp_sub_module_name, cls._subdir_inner_val, - cls._tmp_sub_module_dir) + create_module( + cls._tmp_sub_module_name, cls._subdir_inner_val, cls._tmp_sub_module_dir + ) os.makedirs(cls._aux_sub_mod_dir) # Wait a bit so the import mechanism cache can be refreshed sleep(2) @@ -83,15 +86,16 @@ def _compare_mods(self, one, other): self.assertEqual(one.__spec__.origin, other.__spec__.origin) def test_import_from_subdir(self): - """ Imports a module that's in another directory """ + """Imports a module that's in another directory""" pre_sys_path = deepcopy(sys.path) - self._check_import(self._tmp_sub_module_name, self._subdir_inner_val, - self._tmp_sub_module_dir) + self._check_import( + self._tmp_sub_module_name, self._subdir_inner_val, self._tmp_sub_module_dir + ) self.assertEqual(pre_sys_path, sys.path) def test_import_just_created(self): - """ Creates modules repeatedly and checks it can import them - without waiting any time + """Creates modules repeatedly and checks it can import them + without waiting any time """ n_repeats = 10 for i in range(n_repeats): @@ -102,42 +106,44 @@ def test_import_just_created(self): self._check_import(mod_name, i, self._tmp_sub_module_dir) def test_import_from_dir(self): - """ Imports a module that's in the same directory """ + """Imports a module that's in the same directory""" pre_sys_path = deepcopy(sys.path) self._check_import(self._tmp_in_module_name, self._indir_inner_val) self.assertEqual(pre_sys_path, sys.path) class ImportModuleTest(baseImportTests, unittest.TestCase): - - def _check_import(self, name, value, path=''): - """ Wraps the import checking workflow used in some tests """ + def _check_import(self, name, value, path=""): + """Wraps the import checking workflow used in some tests""" module = _wrappers.import_module(name, path) check_imported_module(self, name, module, value) def test_import_from_pythonpath(self): - """ Imports a module that's in the python path """ + """Imports a module that's in the python path""" # Import os which is also being used in the other tests - module = _wrappers.import_module('os') + module = _wrappers.import_module("os") self.assertIsNotNone(module) self._compare_mods(module, os) def test_import_from_builtins(self): - """ Imports a module that's in the python path """ + """Imports a module that's in the python path""" # Import os which is also being used in the other tests import pwd - module = _wrappers.import_module('pwd') + + module = _wrappers.import_module("pwd") self.assertIsNotNone(module) self._compare_mods(module, pwd) def test_thread_safety(self): - """ Create 5 pairs of modules. Each pair consists of two equally named - files with a different inner value, and saved in different - directories. + """Create 5 pairs of modules. Each pair consists of two equally named + files with a different inner value, and saved in different + directories. """ + def check_routine(module_check_data): - module = _wrappers.import_module(module_check_data["name"], - module_check_data["path"]) + module = _wrappers.import_module( + module_check_data["name"], module_check_data["path"] + ) val = module.inner_variable return val @@ -152,12 +158,14 @@ def get_random_name(length=20): for mod_name in check_mod_names: value = random.randint(0, 100) create_module(mod_name, value, self._aux_sub_mod_dir) - in_dir = {"name": mod_name, "value": value, - "path": self._aux_sub_mod_dir} + in_dir = {"name": mod_name, "value": value, "path": self._aux_sub_mod_dir} value = random.randint(0, 100) create_module(mod_name, value, self._tmp_sub_module_dir) - sub_dir = {"name": mod_name, "value": value, - "path": self._tmp_sub_module_dir} + sub_dir = { + "name": mod_name, + "value": value, + "path": self._tmp_sub_module_dir, + } # We don't want to test if two modules with the same name # are imported safely in the same execution. # We want to test that sys.path priorities are not mixed up @@ -173,8 +181,7 @@ def get_random_name(length=20): class LoadSourceTest(baseImportTests, unittest.TestCase): - - def _check_import(self, name, value, path=''): + def _check_import(self, name, value, path=""): path = os.path.join(path, f"{name}.py") module = _wrappers.load_source(name, path) check_imported_module(self, name, module, value) @@ -187,4 +194,4 @@ def test_mismatching_names(self): def test_no_existing_file(self): # Assert an error is launched if a non existing file is imported with self.assertRaises(FileNotFoundError): - self._check_import('os', None, 'os.py') + self._check_import("os", None, "os.py") diff --git a/selftests/unit/test_xml_utils.py b/selftests/unit/test_xml_utils.py index 05485a7b0e..aaf0aaa126 100644 --- a/selftests/unit/test_xml_utils.py +++ b/selftests/unit/test_xml_utils.py @@ -10,20 +10,19 @@ # simple magic for using scripts within a source tree basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -if os.path.isdir(os.path.join(basedir, 'virttest')): +if os.path.isdir(os.path.join(basedir, "virttest")): sys.path.append(basedir) from virttest import xml_utils class xml_test_data(unittest.TestCase): - def get_tmp_files(self, prefix, sufix): - path_string = os.path.join('/tmp', "%s*%s" % (prefix, sufix)) + path_string = os.path.join("/tmp", "%s*%s" % (prefix, sufix)) return glob.glob(path_string) def setUp(self): - xml_utils.TMPPFX = 'xml_utils_unittest_temp_' + xml_utils.TMPPFX = "xml_utils_unittest_temp_" # Compacted to save excess scrolling self.TEXT_REPLACE_KEY = "TEST_XML_TEXT_REPLACE" self.XMLSTR = """ @@ -48,8 +47,9 @@ def setUp(self): /usr/libexec/qemu-kvm""" - (fd, self.XMLFILE) = tempfile.mkstemp(suffix=xml_utils.TMPSFX, - prefix=xml_utils.TMPPFX) + (fd, self.XMLFILE) = tempfile.mkstemp( + suffix=xml_utils.TMPSFX, prefix=xml_utils.TMPPFX + ) os.write(fd, self.XMLSTR) os.close(fd) self.canonicalize_test_xml() @@ -58,7 +58,7 @@ def tearDown(self): os.unlink(self.XMLFILE) leftovers = self.get_tmp_files(xml_utils.TMPPFX, xml_utils.TMPSFX) if len(leftovers) > 0: - self.fail('Leftover files: %s' % str(leftovers)) + self.fail("Leftover files: %s" % str(leftovers)) def canonicalize_test_xml(self): et = ElementTree.parse(self.XMLFILE) @@ -83,13 +83,11 @@ def is_same_contents(self, filename, other=None): class test_ElementTree(xml_test_data): - def test_bundled_elementtree(self): self.assertEqual(xml_utils.ElementTree.VERSION, ElementTree.VERSION) class test_TempXMLFile(xml_test_data): - def test_prefix_sufix(self): filename = os.path.basename(self.XMLFILE) self.assert_(filename.startswith(xml_utils.TMPPFX)) @@ -106,6 +104,7 @@ def test_TempXMLFile_implicit(self): def out_of_scope_tempxmlfile(): tmpf = xml_utils.TempXMLFile() return tmpf.name + self.assertRaises(OSError, os.stat, out_of_scope_tempxmlfile()) def test_TempXMLFile_explicit(self): @@ -155,6 +154,7 @@ def test_TempXMLBackup_implicit(self): def out_of_scope_xmlbackup(): tmpf = self.class_to_test(self.XMLFILE) return tmpf.name + filename = out_of_scope_xmlbackup() self.assertRaises(OSError, os.unlink, filename) @@ -186,23 +186,21 @@ def test_stringify(self): def test_sourcebackupfile_closed_file(self): xml = self.class_to_test(self.XMLFILE) - self.assertRaises(ValueError, xml.sourcebackupfile.write, 'foobar') + self.assertRaises(ValueError, xml.sourcebackupfile.write, "foobar") def test_sourcebackupfile_closed_string(self): xml = self.class_to_test(self.XMLSTR) - self.assertRaises(ValueError, xml.sourcebackupfile.write, 'foobar') + self.assertRaises(ValueError, xml.sourcebackupfile.write, "foobar") def test_init_str(self): xml = self.class_to_test(self.XMLSTR) self.assert_(xml.sourcefilename is not None) - self.assertEqual(xml.sourcebackupfile.name, - xml.sourcefilename) + self.assertEqual(xml.sourcebackupfile.name, xml.sourcefilename) def test_init_xml(self): xml = self.class_to_test(self.XMLFILE) self.assert_(xml.sourcefilename is not None) - self.assertEqual(xml.sourcebackupfile.name, - xml.sourcefilename) + self.assertEqual(xml.sourcebackupfile.name, xml.sourcefilename) def test_restore_from_string(self): xmlbackup = self.class_to_test(self.XMLSTR) @@ -223,8 +221,8 @@ def test_backup_backup_and_remove(self): bu_tmps = tmps.backup_copy() self.assertTrue(self.is_same_contents(bu_tmpf.name, tmpf.name)) self.assertTrue(self.is_same_contents(bu_tmps.name, tmps.name)) - tmpf.remove_by_xpath('guest/arch/wordsize') - tmps.find('guest/arch/wordsize').text = 'FOOBAR' + tmpf.remove_by_xpath("guest/arch/wordsize") + tmps.find("guest/arch/wordsize").text = "FOOBAR" tmpf.write() tmps.write() self.assertFalse(self.is_same_contents(bu_tmpf.name, tmpf.name)) @@ -234,7 +232,7 @@ def test_backup_backup_and_remove(self): def test_write_default(self): xmlbackup = self.class_to_test(self.XMLFILE) - wordsize = xmlbackup.find('guest/arch/wordsize') + wordsize = xmlbackup.find("guest/arch/wordsize") self.assertTrue(wordsize is not None) self.assertEqual(int(wordsize.text), 32) wordsize.text = str(64) @@ -251,7 +249,7 @@ def test_write_other(self): def test_write_other_changed(self): xmlbackup = self.class_to_test(self.XMLSTR) otherfile = xml_utils.TempXMLFile() - wordsize = xmlbackup.find('guest/arch/wordsize') + wordsize = xmlbackup.find("guest/arch/wordsize") wordsize.text = str(64) xmlbackup.write(otherfile) otherfile.close() @@ -262,7 +260,7 @@ def test_write_other_changed(self): def test_read_other_changed(self): xmlbackup = self.class_to_test(self.XMLSTR) - wordsize = xmlbackup.find('guest/arch/wordsize') + wordsize = xmlbackup.find("guest/arch/wordsize") wordsize.text = str(64) otherfile = xml_utils.TempXMLFile() xmlbackup.write(otherfile) @@ -286,27 +284,26 @@ def get_xpath_elements(self, target_path_string): def test_get_xpath(self): # 2.6 ElementPath doesn't support predicates as in 2.7 :( # (it blindly returns the first match) - self.assertEqual(*self.get_xpath_elements('guest/arch/wordsize')) - self.assertEqual(*self.get_xpath_elements('guest/arch/machine')) - self.assertEqual(*self.get_xpath_elements('host/cpu/arch')) + self.assertEqual(*self.get_xpath_elements("guest/arch/wordsize")) + self.assertEqual(*self.get_xpath_elements("guest/arch/machine")) + self.assertEqual(*self.get_xpath_elements("host/cpu/arch")) def test_create_by_xpath(self): testxml = self.class_to_test(self.XMLSTR) - self.assertTrue(testxml.find('host/cpu') is not None) - self.assertFalse(testxml.find('host/cpu/foo') is not None) - testxml.create_by_xpath('host/cpu/foo/bar') - self.assertTrue(testxml.find('host/cpu/foo/bar') is not None) - self.assertFalse(testxml.find('host/cpu/foo/baz') is not None) - testxml.create_by_xpath('host/cpu/foo/bar/baz') - self.assertTrue(testxml.find('host/cpu/foo/bar/baz') is not None) + self.assertTrue(testxml.find("host/cpu") is not None) + self.assertFalse(testxml.find("host/cpu/foo") is not None) + testxml.create_by_xpath("host/cpu/foo/bar") + self.assertTrue(testxml.find("host/cpu/foo/bar") is not None) + self.assertFalse(testxml.find("host/cpu/foo/baz") is not None) + testxml.create_by_xpath("host/cpu/foo/bar/baz") + self.assertTrue(testxml.find("host/cpu/foo/bar/baz") is not None) # something totally new - self.assertFalse(testxml.find('foo/bar/baz') is not None) - testxml.create_by_xpath('foo/bar/baz') - self.assertTrue(testxml.find('foo/bar/baz') is not None) + self.assertFalse(testxml.find("foo/bar/baz") is not None) + testxml.create_by_xpath("foo/bar/baz") + self.assertTrue(testxml.find("foo/bar/baz") is not None) class test_templatized_xml(xml_test_data): - def setUp(self): self.MAPPING = {"foo": "bar", "bar": "baz", "baz": "foo"} self.FULLREPLACE = """<$foo $bar="$baz">${baz}${foo}${bar}""" @@ -321,7 +318,7 @@ def test_MappingTreeBuilder_standalone(self): txtb = xml_utils.TemplateXMLTreeBuilder(**self.MAPPING) txtb.feed(self.FULLREPLACE) et = txtb.close() - result = ElementTree.tostring(et, encoding='unicode') + result = ElementTree.tostring(et, encoding="unicode") self.assertEqual(result, self.RESULTCHECK) def test_TemplateXMLTreeBuilder_nosub(self): @@ -329,13 +326,13 @@ def test_TemplateXMLTreeBuilder_nosub(self): # elementtree pukes on identifiers starting with $ txtb.feed(self.RESULTCHECK) et = txtb.close() - result = ElementTree.tostring(et, encoding='unicode') + result = ElementTree.tostring(et, encoding="unicode") self.assertEqual(result, self.RESULTCHECK) def test_TemplateXML(self): tx = xml_utils.TemplateXML(self.FULLREPLACE, **self.MAPPING) et = ElementTree.ElementTree(None, tx.name) - check = ElementTree.tostring(et.getroot(), encoding='unicode') + check = ElementTree.tostring(et.getroot(), encoding="unicode") self.assertEqual(check, self.RESULTCHECK) def test_restore_fails(self): diff --git a/setup.py b/setup.py index 61372d70b0..1af2ab63aa 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,7 @@ from pathlib import Path from setuptools import setup, find_packages -VERSION = open('VERSION', 'r').read().strip() +VERSION = open("VERSION", "r").read().strip() class Clean(clean): @@ -29,11 +29,20 @@ class Clean(clean): def run(self): super().run() - cleaning_list = ["MANIFEST", "BUILD", "BUILDROOT", "SPECS", - "RPMS", "SRPMS", "SOURCES", "PYPI_UPLOAD", "./build"] + cleaning_list = [ + "MANIFEST", + "BUILD", + "BUILDROOT", + "SPECS", + "RPMS", + "SRPMS", + "SOURCES", + "PYPI_UPLOAD", + "./build", + ] - cleaning_list += list(Path('.').rglob("*.pyc")) - cleaning_list += list(Path('.').rglob("__pycache__")) + cleaning_list += list(Path(".").rglob("*.pyc")) + cleaning_list += list(Path(".").rglob("__pycache__")) for e in cleaning_list: if not os.path.exists(e): @@ -45,47 +54,47 @@ def run(self): if __name__ == "__main__": - setup(name='avocado-framework-plugin-vt', - version=VERSION, - description='Avocado Plugin for Virtualization Testing', - author='Avocado Developers', - author_email='avocado-devel@redhat.com', - url='http://github.com/avocado-framework/avocado-vt', - packages=find_packages(exclude=('selftests*',)), - include_package_data=True, - entry_points={ - 'console_scripts': [ - 'avocado-runner-avocado-vt = avocado_vt.plugins.vt_runner:main', - ], - 'avocado.plugins.settings': [ - 'vt-settings = avocado_vt.plugins.vt_settings:VTSettings', - ], - 'avocado.plugins.cli': [ - 'vt-list = avocado_vt.plugins.vt_list:VTLister', - 'vt = avocado_vt.plugins.vt:VTRun', - ], - 'avocado.plugins.cli.cmd': [ - 'vt-bootstrap = avocado_vt.plugins.vt_bootstrap:VTBootstrap', - 'vt-list-guests = avocado_vt.plugins.vt_list_guests:VTListGuests', - 'vt-list-archs = avocado_vt.plugins.vt_list_archs:VTListArchs', - ], - 'avocado.plugins.result_events': [ - 'vt-joblock = avocado_vt.plugins.vt_joblock:VTJobLock', - ], - 'avocado.plugins.init': [ - 'vt-init = avocado_vt.plugins.vt_init:VtInit', - ], - 'avocado.plugins.resolver': [ - 'avocado-vt = avocado_vt.plugins.vt_resolver:VTResolver' - ], - 'avocado.plugins.discoverer': [ - 'avocado-vt = avocado_vt.plugins.vt_resolver:VTDiscoverer' - ], - 'avocado.plugins.runnable.runner': [ - 'avocado-vt = avocado_vt.plugins.vt_runner:VTTestRunner', - ], - }, - install_requires=["netifaces", "six", "aexpect", - "avocado-framework>=82.1"], - cmdclass={'clean': Clean}, - ) + setup( + name="avocado-framework-plugin-vt", + version=VERSION, + description="Avocado Plugin for Virtualization Testing", + author="Avocado Developers", + author_email="avocado-devel@redhat.com", + url="http://github.com/avocado-framework/avocado-vt", + packages=find_packages(exclude=("selftests*",)), + include_package_data=True, + entry_points={ + "console_scripts": [ + "avocado-runner-avocado-vt = avocado_vt.plugins.vt_runner:main", + ], + "avocado.plugins.settings": [ + "vt-settings = avocado_vt.plugins.vt_settings:VTSettings", + ], + "avocado.plugins.cli": [ + "vt-list = avocado_vt.plugins.vt_list:VTLister", + "vt = avocado_vt.plugins.vt:VTRun", + ], + "avocado.plugins.cli.cmd": [ + "vt-bootstrap = avocado_vt.plugins.vt_bootstrap:VTBootstrap", + "vt-list-guests = avocado_vt.plugins.vt_list_guests:VTListGuests", + "vt-list-archs = avocado_vt.plugins.vt_list_archs:VTListArchs", + ], + "avocado.plugins.result_events": [ + "vt-joblock = avocado_vt.plugins.vt_joblock:VTJobLock", + ], + "avocado.plugins.init": [ + "vt-init = avocado_vt.plugins.vt_init:VtInit", + ], + "avocado.plugins.resolver": [ + "avocado-vt = avocado_vt.plugins.vt_resolver:VTResolver" + ], + "avocado.plugins.discoverer": [ + "avocado-vt = avocado_vt.plugins.vt_resolver:VTDiscoverer" + ], + "avocado.plugins.runnable.runner": [ + "avocado-vt = avocado_vt.plugins.vt_runner:VTTestRunner", + ], + }, + install_requires=["netifaces", "six", "aexpect", "avocado-framework>=82.1"], + cmdclass={"clean": Clean}, + ) diff --git a/virttest/RFBDes.py b/virttest/RFBDes.py index ecea439f2c..7b54030cdf 100644 --- a/virttest/RFBDes.py +++ b/virttest/RFBDes.py @@ -8,130 +8,884 @@ class Des(object): For details, please refer to: http://en.wikipedia.org/wiki/Data_Encryption_Standard """ + # Permutation and translation tables for DES PC1 = [ - 56, 48, 40, 32, 24, 16, 8, - 0, 57, 49, 41, 33, 25, 17, - 9, 1, 58, 50, 42, 34, 26, - 18, 10, 2, 59, 51, 43, 35, - 62, 54, 46, 38, 30, 22, 14, - 6, 61, 53, 45, 37, 29, 21, - 13, 5, 60, 52, 44, 36, 28, - 20, 12, 4, 27, 19, 11, 3 + 56, + 48, + 40, + 32, + 24, + 16, + 8, + 0, + 57, + 49, + 41, + 33, + 25, + 17, + 9, + 1, + 58, + 50, + 42, + 34, + 26, + 18, + 10, + 2, + 59, + 51, + 43, + 35, + 62, + 54, + 46, + 38, + 30, + 22, + 14, + 6, + 61, + 53, + 45, + 37, + 29, + 21, + 13, + 5, + 60, + 52, + 44, + 36, + 28, + 20, + 12, + 4, + 27, + 19, + 11, + 3, ] # Number left rotations of pc1 - left_rotations = [ - 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 - ] + left_rotations = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1] # get_sub_listd choice key (table 2) PC2 = [ - 13, 16, 10, 23, 0, 4, - 2, 27, 14, 5, 20, 9, - 22, 18, 11, 3, 25, 7, - 15, 6, 26, 19, 12, 1, - 40, 51, 30, 36, 46, 54, - 29, 39, 50, 44, 32, 47, - 43, 48, 38, 55, 33, 52, - 45, 41, 49, 35, 28, 31 + 13, + 16, + 10, + 23, + 0, + 4, + 2, + 27, + 14, + 5, + 20, + 9, + 22, + 18, + 11, + 3, + 25, + 7, + 15, + 6, + 26, + 19, + 12, + 1, + 40, + 51, + 30, + 36, + 46, + 54, + 29, + 39, + 50, + 44, + 32, + 47, + 43, + 48, + 38, + 55, + 33, + 52, + 45, + 41, + 49, + 35, + 28, + 31, ] # Initial permutation IP IP = [ - 57, 49, 41, 33, 25, 17, 9, 1, - 59, 51, 43, 35, 27, 19, 11, 3, - 61, 53, 45, 37, 29, 21, 13, 5, - 63, 55, 47, 39, 31, 23, 15, 7, - 56, 48, 40, 32, 24, 16, 8, 0, - 58, 50, 42, 34, 26, 18, 10, 2, - 60, 52, 44, 36, 28, 20, 12, 4, - 62, 54, 46, 38, 30, 22, 14, 6 + 57, + 49, + 41, + 33, + 25, + 17, + 9, + 1, + 59, + 51, + 43, + 35, + 27, + 19, + 11, + 3, + 61, + 53, + 45, + 37, + 29, + 21, + 13, + 5, + 63, + 55, + 47, + 39, + 31, + 23, + 15, + 7, + 56, + 48, + 40, + 32, + 24, + 16, + 8, + 0, + 58, + 50, + 42, + 34, + 26, + 18, + 10, + 2, + 60, + 52, + 44, + 36, + 28, + 20, + 12, + 4, + 62, + 54, + 46, + 38, + 30, + 22, + 14, + 6, ] # Expansion table for turning 32 bit blocks into 48 bits E = [ - 31, 0, 1, 2, 3, 4, - 3, 4, 5, 6, 7, 8, - 7, 8, 9, 10, 11, 12, - 11, 12, 13, 14, 15, 16, - 15, 16, 17, 18, 19, 20, - 19, 20, 21, 22, 23, 24, - 23, 24, 25, 26, 27, 28, - 27, 28, 29, 30, 31, 0 + 31, + 0, + 1, + 2, + 3, + 4, + 3, + 4, + 5, + 6, + 7, + 8, + 7, + 8, + 9, + 10, + 11, + 12, + 11, + 12, + 13, + 14, + 15, + 16, + 15, + 16, + 17, + 18, + 19, + 20, + 19, + 20, + 21, + 22, + 23, + 24, + 23, + 24, + 25, + 26, + 27, + 28, + 27, + 28, + 29, + 30, + 31, + 0, ] # The (in)famous S-boxes sbox = [ # S1 - [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, - 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, - 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, - 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13], - + [ + 14, + 4, + 13, + 1, + 2, + 15, + 11, + 8, + 3, + 10, + 6, + 12, + 5, + 9, + 0, + 7, + 0, + 15, + 7, + 4, + 14, + 2, + 13, + 1, + 10, + 6, + 12, + 11, + 9, + 5, + 3, + 8, + 4, + 1, + 14, + 8, + 13, + 6, + 2, + 11, + 15, + 12, + 9, + 7, + 3, + 10, + 5, + 0, + 15, + 12, + 8, + 2, + 4, + 9, + 1, + 7, + 5, + 11, + 3, + 14, + 10, + 0, + 6, + 13, + ], # S2 - [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, - 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, - 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, - 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9], - + [ + 15, + 1, + 8, + 14, + 6, + 11, + 3, + 4, + 9, + 7, + 2, + 13, + 12, + 0, + 5, + 10, + 3, + 13, + 4, + 7, + 15, + 2, + 8, + 14, + 12, + 0, + 1, + 10, + 6, + 9, + 11, + 5, + 0, + 14, + 7, + 11, + 10, + 4, + 13, + 1, + 5, + 8, + 12, + 6, + 9, + 3, + 2, + 15, + 13, + 8, + 10, + 1, + 3, + 15, + 4, + 2, + 11, + 6, + 7, + 12, + 0, + 5, + 14, + 9, + ], # S3 - [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, - 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, - 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, - 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12], - + [ + 10, + 0, + 9, + 14, + 6, + 3, + 15, + 5, + 1, + 13, + 12, + 7, + 11, + 4, + 2, + 8, + 13, + 7, + 0, + 9, + 3, + 4, + 6, + 10, + 2, + 8, + 5, + 14, + 12, + 11, + 15, + 1, + 13, + 6, + 4, + 9, + 8, + 15, + 3, + 0, + 11, + 1, + 2, + 12, + 5, + 10, + 14, + 7, + 1, + 10, + 13, + 0, + 6, + 9, + 8, + 7, + 4, + 15, + 14, + 3, + 11, + 5, + 2, + 12, + ], # S4 - [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, - 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, - 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, - 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14], - + [ + 7, + 13, + 14, + 3, + 0, + 6, + 9, + 10, + 1, + 2, + 8, + 5, + 11, + 12, + 4, + 15, + 13, + 8, + 11, + 5, + 6, + 15, + 0, + 3, + 4, + 7, + 2, + 12, + 1, + 10, + 14, + 9, + 10, + 6, + 9, + 0, + 12, + 11, + 7, + 13, + 15, + 1, + 3, + 14, + 5, + 2, + 8, + 4, + 3, + 15, + 0, + 6, + 10, + 1, + 13, + 8, + 9, + 4, + 5, + 11, + 12, + 7, + 2, + 14, + ], # S5 - [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, - 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, - 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, - 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3], - + [ + 2, + 12, + 4, + 1, + 7, + 10, + 11, + 6, + 8, + 5, + 3, + 15, + 13, + 0, + 14, + 9, + 14, + 11, + 2, + 12, + 4, + 7, + 13, + 1, + 5, + 0, + 15, + 10, + 3, + 9, + 8, + 6, + 4, + 2, + 1, + 11, + 10, + 13, + 7, + 8, + 15, + 9, + 12, + 5, + 6, + 3, + 0, + 14, + 11, + 8, + 12, + 7, + 1, + 14, + 2, + 13, + 6, + 15, + 0, + 9, + 10, + 4, + 5, + 3, + ], # S6 - [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, - 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, - 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, - 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13], - + [ + 12, + 1, + 10, + 15, + 9, + 2, + 6, + 8, + 0, + 13, + 3, + 4, + 14, + 7, + 5, + 11, + 10, + 15, + 4, + 2, + 7, + 12, + 9, + 5, + 6, + 1, + 13, + 14, + 0, + 11, + 3, + 8, + 9, + 14, + 15, + 5, + 2, + 8, + 12, + 3, + 7, + 0, + 4, + 10, + 1, + 13, + 11, + 6, + 4, + 3, + 2, + 12, + 9, + 5, + 15, + 10, + 11, + 14, + 1, + 7, + 6, + 0, + 8, + 13, + ], # S7 - [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, - 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, - 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, - 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12], - + [ + 4, + 11, + 2, + 14, + 15, + 0, + 8, + 13, + 3, + 12, + 9, + 7, + 5, + 10, + 6, + 1, + 13, + 0, + 11, + 7, + 4, + 9, + 1, + 10, + 14, + 3, + 5, + 12, + 2, + 15, + 8, + 6, + 1, + 4, + 11, + 13, + 12, + 3, + 7, + 14, + 10, + 15, + 6, + 8, + 0, + 5, + 9, + 2, + 6, + 11, + 13, + 8, + 1, + 4, + 10, + 7, + 9, + 5, + 0, + 15, + 14, + 2, + 3, + 12, + ], # S8 - [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, - 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, - 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, - 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11], + [ + 13, + 2, + 8, + 4, + 6, + 15, + 11, + 1, + 10, + 9, + 3, + 14, + 5, + 0, + 12, + 7, + 1, + 15, + 13, + 8, + 10, + 3, + 7, + 4, + 12, + 5, + 6, + 11, + 0, + 14, + 9, + 2, + 7, + 11, + 4, + 1, + 9, + 12, + 14, + 2, + 0, + 6, + 10, + 13, + 15, + 3, + 5, + 8, + 2, + 1, + 14, + 7, + 4, + 10, + 8, + 13, + 15, + 12, + 9, + 0, + 3, + 5, + 6, + 11, + ], ] # 32-bit permutation function P used on the output of the S-boxes P = [ - 15, 6, 19, 20, 28, 11, - 27, 16, 0, 14, 22, 25, - 4, 17, 30, 9, 1, 7, - 23, 13, 31, 26, 2, 8, - 18, 12, 29, 5, 21, 10, - 3, 24 + 15, + 6, + 19, + 20, + 28, + 11, + 27, + 16, + 0, + 14, + 22, + 25, + 4, + 17, + 30, + 9, + 1, + 7, + 23, + 13, + 31, + 26, + 2, + 8, + 18, + 12, + 29, + 5, + 21, + 10, + 3, + 24, ] # Final permutation IP^-1 FP = [ - 39, 7, 47, 15, 55, 23, 63, 31, - 38, 6, 46, 14, 54, 22, 62, 30, - 37, 5, 45, 13, 53, 21, 61, 29, - 36, 4, 44, 12, 52, 20, 60, 28, - 35, 3, 43, 11, 51, 19, 59, 27, - 34, 2, 42, 10, 50, 18, 58, 26, - 33, 1, 41, 9, 49, 17, 57, 25, - 32, 0, 40, 8, 48, 16, 56, 24 + 39, + 7, + 47, + 15, + 55, + 23, + 63, + 31, + 38, + 6, + 46, + 14, + 54, + 22, + 62, + 30, + 37, + 5, + 45, + 13, + 53, + 21, + 61, + 29, + 36, + 4, + 44, + 12, + 52, + 20, + 60, + 28, + 35, + 3, + 43, + 11, + 51, + 19, + 59, + 27, + 34, + 2, + 42, + 10, + 50, + 18, + 58, + 26, + 33, + 1, + 41, + 9, + 49, + 17, + 57, + 25, + 32, + 0, + 40, + 8, + 48, + 16, + 56, + 24, ] # Initialisation @@ -142,11 +896,11 @@ def __init__(self, key): :param key: Original used in DES. """ if len(key) != 8: - key = (key + '\0' * 8)[:8] + key = (key + "\0" * 8)[:8] self.L = [] self.R = [] - self.Kn = [[0] * 48] * 16 # 16 48-bit keys (K1 - K16) + self.Kn = [[0] * 48] * 16 # 16 48-bit keys (K1 - K16) self.setKey(key) @@ -194,8 +948,7 @@ def create_Kn(self): """ Create the 16 subkeys,from K[0] to K[15], from the given key """ - key = self.get_sub_list( - self.PC1, utils_misc.string_to_bitlist(self.getKey())) + key = self.get_sub_list(self.PC1, utils_misc.string_to_bitlist(self.getKey())) self.L = key[:28] self.R = key[28:] for i in range(16): @@ -228,8 +981,16 @@ def f(self, K): self.R = list(map(lambda x, y: x ^ y, self.R, K)) # The block is divided into eight 6-bit pieces - B = [self.R[:6], self.R[6:12], self.R[12:18], self.R[18:24], - self.R[24:30], self.R[30:36], self.R[36:42], self.R[42:]] + B = [ + self.R[:6], + self.R[6:12], + self.R[12:18], + self.R[18:24], + self.R[24:30], + self.R[30:36], + self.R[36:42], + self.R[42:], + ] # Substitution: Bn = [0] * 32 @@ -303,7 +1064,7 @@ def crypt(self, data, crypt_type=0): result = [] while i < len(data): # Test code for caching encryption results - block = utils_misc.string_to_bitlist(data[i:i + 8]) + block = utils_misc.string_to_bitlist(data[i : i + 8]) pro_block = self.des_crypt(block, crypt_type) # Add the resulting block to our list @@ -311,4 +1072,4 @@ def crypt(self, data, crypt_type=0): i += 8 # Return the full encrypted/decrypted string - return ''.join(result) + return "".join(result) diff --git a/virttest/_wrappers.py b/virttest/_wrappers.py index bb2fbfcbd5..d1e749f2e2 100644 --- a/virttest/_wrappers.py +++ b/virttest/_wrappers.py @@ -7,22 +7,29 @@ import sys import importlib import importlib.util -from importlib.machinery import (SourceFileLoader, SOURCE_SUFFIXES, - SourcelessFileLoader, BYTECODE_SUFFIXES, - ExtensionFileLoader, EXTENSION_SUFFIXES) +from importlib.machinery import ( + SourceFileLoader, + SOURCE_SUFFIXES, + SourcelessFileLoader, + BYTECODE_SUFFIXES, + ExtensionFileLoader, + EXTENSION_SUFFIXES, +) -_LOADERS = ((SourceFileLoader, SOURCE_SUFFIXES), - (SourcelessFileLoader, BYTECODE_SUFFIXES), - (ExtensionFileLoader, EXTENSION_SUFFIXES)) +_LOADERS = ( + (SourceFileLoader, SOURCE_SUFFIXES), + (SourcelessFileLoader, BYTECODE_SUFFIXES), + (ExtensionFileLoader, EXTENSION_SUFFIXES), +) def _load_from_spec(spec): - """ Just for code refactoring, it takes a module spec and does the needed - things so as to import it into the execution environment. - :param spec: module spec. - :type spec: ModSpec - :returns : Imported module instance + """Just for code refactoring, it takes a module spec and does the needed + things so as to import it into the execution environment. + :param spec: module spec. + :type spec: ModSpec + :returns : Imported module instance """ module = importlib.util.module_from_spec(spec) sys.modules[spec.name] = module @@ -31,16 +38,16 @@ def _load_from_spec(spec): def import_module(name, path=None): - """ Imports a module named to the execution environment. - If the module is not in the PYTHONPATH or the local path, - its path can be determined by the argument. - :param name: Name of the module that is going to be imported - :type name: String - :param path: Path in which the module is located. - If None, it will find the module in the current dir - and the PYTHONPATH. - :type path: String, list of strings or None - :returns: The imported module + """Imports a module named to the execution environment. + If the module is not in the PYTHONPATH or the local path, + its path can be determined by the argument. + :param name: Name of the module that is going to be imported + :type name: String + :param path: Path in which the module is located. + If None, it will find the module in the current dir + and the PYTHONPATH. + :type path: String, list of strings or None + :returns: The imported module """ if path is None: if name in sys.builtin_module_names: @@ -61,20 +68,20 @@ def import_module(name, path=None): def load_source(name, path): - """ Imports the contents of a source file from to the - execution environment inside a module named . - :param name: Name of the module that is going to be imported - :type name: String - :param path: Path of the source file being imported. - :type path: String - :returns: The imported module + """Imports the contents of a source file from to the + execution environment inside a module named . + :param name: Name of the module that is going to be imported + :type name: String + :param path: Path of the source file being imported. + :type path: String + :returns: The imported module """ spec = importlib.util.spec_from_file_location(name, path) return _load_from_spec(spec) def lazy_import(name): - """ Allows for an early import that is initialized upon use (lazy import). + """Allows for an early import that is initialized upon use (lazy import). :param name: Name of the module that is going to be imported :type name: String diff --git a/virttest/arch.py b/virttest/arch.py index 201471ff62..0f85571229 100644 --- a/virttest/arch.py +++ b/virttest/arch.py @@ -4,7 +4,7 @@ ARCH = platform.machine() -if ARCH in ('ppc64', 'ppc64le'): +if ARCH in ("ppc64", "ppc64le"): # From include/linux/sockios.h SIOCSIFHWADDR = 0x8924 SIOCGIFHWADDR = 0x8927 @@ -17,15 +17,15 @@ SIOCGIFMTU = 0x8921 SIOCSIFMTU = 0x8922 SIOCGIFINDEX = 0x8933 - SIOCBRADDIF = 0x89a2 - SIOCBRDELIF = 0x89a3 - SIOCBRADDBR = 0x89a0 - SIOCBRDELBR = 0x89a1 + SIOCBRADDIF = 0x89A2 + SIOCBRDELIF = 0x89A3 + SIOCBRADDBR = 0x89A0 + SIOCBRDELBR = 0x89A1 # From linux/include/linux/if_tun.h - TUNSETIFF = 0x800454ca - TUNGETIFF = 0x400454d2 - TUNGETFEATURES = 0x400454cf - TUNSETQUEUE = 0x800454d9 + TUNSETIFF = 0x800454CA + TUNGETIFF = 0x400454D2 + TUNGETFEATURES = 0x400454CF + TUNSETQUEUE = 0x800454D9 IFF_MULTI_QUEUE = 0x0100 IFF_TAP = 0x2 IFF_NO_PI = 0x1000 @@ -42,7 +42,7 @@ # From linux/socket.h AF_PACKET = 17 # From linux/vhost.h - VHOST_VSOCK_SET_GUEST_CID = 0x8008af60 + VHOST_VSOCK_SET_GUEST_CID = 0x8008AF60 else: # From include/linux/sockios.h SIOCSIFHWADDR = 0x8924 @@ -56,15 +56,15 @@ SIOCGIFMTU = 0x8921 SIOCSIFMTU = 0x8922 SIOCGIFINDEX = 0x8933 - SIOCBRADDIF = 0x89a2 - SIOCBRDELIF = 0x89a3 - SIOCBRADDBR = 0x89a0 - SIOCBRDELBR = 0x89a1 + SIOCBRADDIF = 0x89A2 + SIOCBRDELIF = 0x89A3 + SIOCBRADDBR = 0x89A0 + SIOCBRDELBR = 0x89A1 # From linux/include/linux/if_tun.h - TUNSETIFF = 0x400454ca - TUNGETIFF = 0x800454d2 - TUNGETFEATURES = 0x800454cf - TUNSETQUEUE = 0x400454d9 + TUNSETIFF = 0x400454CA + TUNGETIFF = 0x800454D2 + TUNGETFEATURES = 0x800454CF + TUNSETQUEUE = 0x400454D9 IFF_MULTI_QUEUE = 0x0100 IFF_TAP = 0x0002 IFF_NO_PI = 0x1000 @@ -81,17 +81,21 @@ # From linux/socket.h AF_PACKET = 17 # From linux/vhost.h - VHOST_VSOCK_SET_GUEST_CID = 0x4008af60 + VHOST_VSOCK_SET_GUEST_CID = 0x4008AF60 def get_kvm_module_list(): - if ARCH == 'x86_64': - vendor = cpu.get_vendor() if hasattr(cpu, 'get_vendor') else cpu.get_cpu_vendor_name() + if ARCH == "x86_64": + vendor = ( + cpu.get_vendor() + if hasattr(cpu, "get_vendor") + else cpu.get_cpu_vendor_name() + ) return ["kvm", "kvm-%s" % vendor] - elif ARCH in ('ppc64', 'ppc64le'): + elif ARCH in ("ppc64", "ppc64le"): # FIXME: Please correct it if anyone still want to use KVM-PR mode return ["kvm", "kvm-hv"] - elif ARCH in ('s390', 's390x'): + elif ARCH in ("s390", "s390x"): return ["kvm"] elif ARCH == "aarch64": return [] diff --git a/virttest/asset.py b/virttest/asset.py index eacf1a274c..eba60acb13 100644 --- a/virttest/asset.py +++ b/virttest/asset.py @@ -4,6 +4,7 @@ import glob import shutil from six import StringIO + try: import configparser as ConfigParser except ImportError: @@ -21,7 +22,7 @@ from virttest import data_dir -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class ConfigLoader: @@ -47,12 +48,12 @@ def __init__(self, cfg, tmpdir=data_dir.get_tmp_dir(), raise_errors=False): # Raise errors when lacking values self.raise_errors = raise_errors # File is already a file like object - if hasattr(cfg, 'read'): + if hasattr(cfg, "read"): self.cfg = cfg self.parser.readfp(self.cfg) elif isinstance(cfg, string_types): # Config file is a URL. Download it to a temp dir - if cfg.startswith('http') or cfg.startswith('ftp'): + if cfg.startswith("http") or cfg.startswith("ftp"): self.cfg = os.path.join(tmpdir, os.path.basename(cfg)) download.url_download(cfg, self.cfg) self.parser.read(self.cfg) @@ -62,7 +63,7 @@ def __init__(self, cfg, tmpdir=data_dir.get_tmp_dir(), raise_errors=False): self.cfg = os.path.abspath(cfg) self.parser.read(self.cfg) else: - e_msg = 'Invalid config file path: %s' % cfg + e_msg = "Invalid config file path: %s" % cfg raise IOError(e_msg) # Config file is just a string, convert it to a python file like # object using StringIO @@ -84,8 +85,10 @@ def get(self, section, option, default=None): """ if not self.parser.has_option(section, option): if self.raise_errors: - raise ValueError('No value for option %s. Please check your ' - 'config file "%s".' % (option, self.cfg)) + raise ValueError( + "No value for option %s. Please check your " + 'config file "%s".' % (option, self.cfg) + ) else: return default @@ -114,7 +117,7 @@ def save(self): """ if not self.cfg: return - with open(self.cfg, 'w') as fileobj: + with open(self.cfg, "w") as fileobj: self.parser.write(fileobj) def check(self, section): @@ -127,7 +130,7 @@ def check(self, section): options = self.parser.items(section) for i in range(options.__len__()): param = options[i][0] - aux = str.split(param, '.') + aux = str.split(param, ".") if aux.__len__ < 2: return False, "Invalid parameter syntax at %s" % (param) @@ -141,7 +144,7 @@ def check_parameter(self, param_type, parameter): """ Check if a option has a valid value """ - if parameter == '' or parameter is None: + if parameter == "" or parameter is None: return False elif param_type == "ip" and self.__isipaddress(parameter): return True @@ -197,7 +200,7 @@ def get_known_backends(): """ # Generic means the test can run in multiple backends, such as libvirt # and qemu. - known_backends = ['generic', 'multi_host_migration'] + known_backends = ["generic", "multi_host_migration"] known_backends += os.listdir(data_dir.BASE_BACKEND_DIR) return known_backends @@ -210,11 +213,11 @@ def get_test_provider_names(backend=None): """ provider_name_list = [] provider_dir = data_dir.get_test_providers_dir() - for provider in glob.glob(os.path.join(provider_dir, '*.ini')): - provider_name = os.path.basename(provider).split('.')[0] + for provider in glob.glob(os.path.join(provider_dir, "*.ini")): + provider_name = os.path.basename(provider).split(".")[0] provider_info = get_test_provider_info(provider_name) if backend is not None: - if backend in provider_info['backends']: + if backend in provider_info["backends"]: provider_name_list.append(provider_name) else: provider_name_list.append(provider_name) @@ -233,13 +236,13 @@ def get_test_provider_subdirs(backend=None): subdir_list = [] for provider_name in get_test_provider_names(): provider_info = get_test_provider_info(provider_name) - backends_info = provider_info['backends'] + backends_info = provider_info["backends"] if backend is not None: if backend in backends_info: - subdir_list.append(backends_info[backend]['path']) + subdir_list.append(backends_info[backend]["path"]) else: for b in backends_info: - subdir_list.append(backends_info[b]['path']) + subdir_list.append(backends_info[b]["path"]) return subdir_list @@ -255,35 +258,32 @@ def get_test_provider_info(provider): :param provider: Test provider name, such as 'io-github-autotest-qemu'. """ provider_info = {} - provider_path = os.path.join(data_dir.get_test_providers_dir(), - '%s.ini' % provider) + provider_path = os.path.join(data_dir.get_test_providers_dir(), "%s.ini" % provider) provider_cfg = ConfigLoader(provider_path) - provider_info['name'] = provider - provider_info['uri'] = provider_cfg.get('provider', 'uri') - provider_info['branch'] = provider_cfg.get('provider', 'branch', 'master') - provider_info['ref'] = provider_cfg.get('provider', 'ref') - provider_info['pubkey'] = provider_cfg.get('provider', 'pubkey') - provider_info['backends'] = {} + provider_info["name"] = provider + provider_info["uri"] = provider_cfg.get("provider", "uri") + provider_info["branch"] = provider_cfg.get("provider", "branch", "master") + provider_info["ref"] = provider_cfg.get("provider", "ref") + provider_info["pubkey"] = provider_cfg.get("provider", "pubkey") + provider_info["backends"] = {} for backend in get_known_backends(): - subdir = provider_cfg.get(backend, 'subdir') - cart_cfgs = provider_cfg.get(backend, 'configs') + subdir = provider_cfg.get(backend, "subdir") + cart_cfgs = provider_cfg.get(backend, "configs") backend_dic = {} if cart_cfgs is not None: # Give ability to specify few required configs separated with comma - cart_cfgs = [x.strip() for x in cart_cfgs.split(',')] - backend_dic.update({'cartesian_configs': cart_cfgs}) + cart_cfgs = [x.strip() for x in cart_cfgs.split(",")] + backend_dic.update({"cartesian_configs": cart_cfgs}) if subdir is not None: - if provider_info['uri'].startswith('file://'): - src = os.path.join(provider_info['uri'][7:], - subdir) + if provider_info["uri"].startswith("file://"): + src = os.path.join(provider_info["uri"][7:], subdir) else: - src = os.path.join(data_dir.get_test_provider_dir(provider), - subdir) - backend_dic.update({'path': src}) - provider_info['backends'].update({backend: backend_dic}) + src = os.path.join(data_dir.get_test_provider_dir(provider), subdir) + backend_dic.update({"path": src}) + provider_info["backends"].update({backend: backend_dic}) return provider_info @@ -298,39 +298,43 @@ def download_test_provider(provider, update=False): :param provider: Test provider name, such as 'io-github-autotest-qemu'. """ provider_info = get_test_provider_info(provider) - uri = provider_info.get('uri') - if not uri.startswith('file://'): - uri = provider_info.get('uri') - branch = provider_info.get('branch') - ref = provider_info.get('ref') - pubkey = provider_info.get('pubkey') + uri = provider_info.get("uri") + if not uri.startswith("file://"): + uri = provider_info.get("uri") + branch = provider_info.get("branch") + ref = provider_info.get("ref") + pubkey = provider_info.get("pubkey") download_dst = data_dir.get_test_provider_dir(provider) dir_existed = os.path.exists(download_dst) - repo_downloaded = os.path.isdir(os.path.join(download_dst, '.git')) + repo_downloaded = os.path.isdir(os.path.join(download_dst, ".git")) original_dir = os.getcwd() try: if not repo_downloaded or update: - download_dst = git.get_repo(uri=uri, branch=branch, commit=ref, - destination_dir=download_dst) + download_dst = git.get_repo( + uri=uri, branch=branch, commit=ref, destination_dir=download_dst + ) os.chdir(download_dst) try: - process.run('git remote add origin %s' % uri) + process.run("git remote add origin %s" % uri) except process.CmdError: pass except Exception: if not dir_existed and os.path.isdir(download_dst): - LOG.error('Cleaning up provider %s download dir %s', provider, - download_dst) + LOG.error( + "Cleaning up provider %s download dir %s", provider, download_dst + ) shutil.rmtree(download_dst) raise # sanity check to ensure the git repository is OK: try: os.chdir(download_dst) - process.system('git log -1') + process.system("git log -1") except process.CmdError: - LOG.error('Something is unexpectedly wrong with the git repository at %s', - download_dst) + LOG.error( + "Something is unexpectedly wrong with the git repository at %s", + download_dst, + ) raise finally: os.chdir(original_dir) @@ -343,7 +347,7 @@ def test_providers_not_downloaded(): result = [] for provider in get_test_provider_names(): download_dst = data_dir.get_test_provider_dir(provider) - repo_downloaded = os.path.isdir(os.path.join(download_dst, '.git')) + repo_downloaded = os.path.isdir(os.path.join(download_dst, ".git")) if not repo_downloaded: result.append(provider) return result @@ -360,7 +364,7 @@ def download_all_test_providers(update=False): def get_all_assets(): asset_data_list = [] download_dir = data_dir.get_download_dir() - for asset in glob.glob(os.path.join(download_dir, '*.ini')): + for asset in glob.glob(os.path.join(download_dir, "*.ini")): asset_name = os.path.basename(asset)[:-4] asset_data_list.append(get_asset_info(asset_name)) return asset_data_list @@ -373,25 +377,36 @@ def get_file_asset(title, src_path, destination): for ext in (".xz", ".gz", ".7z", ".bz2"): if os.path.exists(src_path + ext): destination = destination + ext - LOG.debug('Found source image %s', destination) + LOG.debug("Found source image %s", destination) return { - 'url': None, 'sha1_url': None, 'destination': src_path + ext, - 'destination_uncompressed': destination, - 'uncompress_cmd': None, 'shortname': title, 'title': title, - 'downloaded': True} + "url": None, + "sha1_url": None, + "destination": src_path + ext, + "destination_uncompressed": destination, + "uncompress_cmd": None, + "shortname": title, + "title": title, + "downloaded": True, + } if os.path.exists(src_path): - LOG.debug('Found source image %s', destination) - return {'url': src_path, 'sha1_url': None, 'destination': destination, - 'destination_uncompressed': None, 'uncompress_cmd': None, - 'shortname': title, 'title': title, - 'downloaded': os.path.exists(destination)} + LOG.debug("Found source image %s", destination) + return { + "url": src_path, + "sha1_url": None, + "destination": destination, + "destination_uncompressed": None, + "uncompress_cmd": None, + "shortname": title, + "title": title, + "downloaded": os.path.exists(destination), + } return None def get_asset_info(asset, ini_dir=None, section=None): - """" + """ " Parse $asset.ini file and returns a dictionary suitable for asset.download_file() @@ -402,68 +417,71 @@ def get_asset_info(asset, ini_dir=None, section=None): """ asset_info = {} ini_dir = ini_dir or data_dir.get_download_dir() - asset_path = os.path.join(ini_dir, '%s.ini' % asset) + asset_path = os.path.join(ini_dir, "%s.ini" % asset) assert os.path.exists(asset_path), "Missing asset file %s" % asset_path asset_cfg = ConfigLoader(asset_path) section = section or asset - asset_info['url'] = asset_cfg.get(section, 'url') - asset_info['sha1_url'] = asset_cfg.get(section, 'sha1_url') - asset_info['title'] = asset_cfg.get(section, 'title') - destination = asset_cfg.get(section, 'destination') + asset_info["url"] = asset_cfg.get(section, "url") + asset_info["sha1_url"] = asset_cfg.get(section, "sha1_url") + asset_info["title"] = asset_cfg.get(section, "title") + destination = asset_cfg.get(section, "destination") if not os.path.isabs(destination): destination = os.path.join(data_dir.get_data_dir(), destination) - asset_info['destination'] = destination - asset_info['asset_exists'] = os.path.isfile(destination) + asset_info["destination"] = destination + asset_info["asset_exists"] = os.path.isfile(destination) # Optional fields - d_uncompressed = asset_cfg.get(section, 'destination_uncompressed') + d_uncompressed = asset_cfg.get(section, "destination_uncompressed") if d_uncompressed is not None and not os.path.isabs(d_uncompressed): - d_uncompressed = os.path.join(data_dir.get_data_dir(), - d_uncompressed) - asset_info['destination_uncompressed'] = d_uncompressed - asset_info['uncompress_cmd'] = asset_cfg.get(section, 'uncompress_cmd') + d_uncompressed = os.path.join(data_dir.get_data_dir(), d_uncompressed) + asset_info["destination_uncompressed"] = d_uncompressed + asset_info["uncompress_cmd"] = asset_cfg.get(section, "uncompress_cmd") return asset_info def uncompress_asset(asset_info, force=False): - destination = asset_info['destination'] - uncompress_cmd = asset_info['uncompress_cmd'] - destination_uncompressed = asset_info['destination_uncompressed'] + destination = asset_info["destination"] + uncompress_cmd = asset_info["uncompress_cmd"] + destination_uncompressed = asset_info["destination_uncompressed"] archive_re = re.compile(r".*\.(gz|xz|7z|bz2)$") if destination_uncompressed is not None: if uncompress_cmd is None: match = archive_re.match(destination) if match: - if match.group(1) == 'gz': - uncompress_cmd = ('gzip -cd %s > %s' % - (destination, destination_uncompressed)) - elif match.group(1) == 'xz': - uncompress_cmd = ('xz -cd %s > %s' % - (destination, destination_uncompressed)) - elif match.group(1) == 'bz2': - uncompress_cmd = ('bzip2 -cd %s > %s' % - (destination, destination_uncompressed)) - elif match.group(1) == '7z': - uncompress_cmd = '7za -y e %s' % destination + if match.group(1) == "gz": + uncompress_cmd = "gzip -cd %s > %s" % ( + destination, + destination_uncompressed, + ) + elif match.group(1) == "xz": + uncompress_cmd = "xz -cd %s > %s" % ( + destination, + destination_uncompressed, + ) + elif match.group(1) == "bz2": + uncompress_cmd = "bzip2 -cd %s > %s" % ( + destination, + destination_uncompressed, + ) + elif match.group(1) == "7z": + uncompress_cmd = "7za -y e %s" % destination else: uncompress_cmd = "%s %s" % (uncompress_cmd, destination) if uncompress_cmd is not None: uncompressed_file_exists = os.path.exists(destination_uncompressed) - force = (force or not uncompressed_file_exists) + force = force or not uncompressed_file_exists if os.path.isfile(destination) and force: os.chdir(os.path.dirname(destination_uncompressed)) - LOG.debug('Uncompressing %s -> %s', destination, - destination_uncompressed) + LOG.debug("Uncompressing %s -> %s", destination, destination_uncompressed) process.run(uncompress_cmd, shell=True) - backup_file = destination_uncompressed + '.backup' + backup_file = destination_uncompressed + ".backup" if os.path.isfile(backup_file): - LOG.debug('Copying %s -> %s', destination_uncompressed, - backup_file) + LOG.debug("Copying %s -> %s", destination_uncompressed, backup_file) shutil.copy(destination_uncompressed, backup_file) @@ -481,10 +499,10 @@ def download_file(asset_info, interactive=False, force=False): had_to_download = False sha1 = None - url = asset_info['url'] - sha1_url = asset_info['sha1_url'] - destination = asset_info['destination'] - title = asset_info['title'] + url = asset_info["url"] + sha1_url = asset_info["sha1_url"] + destination = asset_info["destination"] + title = asset_info["title"] if sha1_url is not None: try: @@ -507,56 +525,60 @@ def download_file(asset_info, interactive=False, force=False): if interactive: answer = genio.ask("Would you like to download it from %s?" % url) else: - answer = 'y' - if answer == 'y': + answer = "y" + if answer == "y": try: - download.url_download_interactive(url, destination, - "Downloading %s" % title) + download.url_download_interactive( + url, destination, "Downloading %s" % title + ) had_to_download = True except Exception as download_failure: - LOG.error("Check your internet connection: %s", - download_failure) + LOG.error("Check your internet connection: %s", download_failure) else: LOG.warning("Missing file %s", destination) else: LOG.info("Found %s", destination) if sha1 is None: - answer = 'n' + answer = "n" else: - answer = 'y' + answer = "y" - if answer == 'y': - actual_sha1 = crypto.hash_file(destination, algorithm='sha1') + if answer == "y": + actual_sha1 = crypto.hash_file(destination, algorithm="sha1") if actual_sha1 != sha1: LOG.info("Actual SHA1 sum: %s", actual_sha1) if interactive: - answer = genio.ask("The file seems corrupted or outdated. " - "Would you like to download it?") + answer = genio.ask( + "The file seems corrupted or outdated. " + "Would you like to download it?" + ) else: LOG.info("The file seems corrupted or outdated") - answer = 'y' - if answer == 'y': + answer = "y" + if answer == "y": LOG.info("Updating image to the latest available...") while not file_ok: try: - download.url_download_interactive(url, destination, - title) + download.url_download_interactive(url, destination, title) except Exception as download_failure: - LOG.error("Check your internet connection: %s", - download_failure) - sha1_post_download = crypto.hash_file(destination, - algorithm='sha1') + LOG.error( + "Check your internet connection: %s", download_failure + ) + sha1_post_download = crypto.hash_file( + destination, algorithm="sha1" + ) had_to_download = True if sha1_post_download != sha1: LOG.error("Actual SHA1 sum: %s", actual_sha1) if interactive: - answer = genio.ask("The file downloaded %s is " - "corrupted. Would you like " - "to try again?" % - destination) + answer = genio.ask( + "The file downloaded %s is " + "corrupted. Would you like " + "to try again?" % destination + ) else: - answer = 'n' - if answer == 'n': + answer = "n" + if answer == "n": problems_ignored = True LOG.error("File %s is corrupted" % destination) file_ok = True @@ -569,8 +591,7 @@ def download_file(asset_info, interactive=False, force=False): LOG.info("SHA1 sum check OK") else: problems_ignored = True - LOG.info("File %s present, but did not verify integrity", - destination) + LOG.info("File %s present, but did not verify integrity", destination) if file_ok: if not problems_ignored: @@ -610,5 +631,4 @@ def download_asset(asset, interactive=True, restore_image=False): """ asset_info = get_asset_info(asset) - download_file(asset_info=asset_info, interactive=interactive, - force=restore_image) + download_file(asset_info=asset_info, interactive=interactive, force=restore_image) diff --git a/virttest/base_installer.py b/virttest/base_installer.py index 78a3448ca9..19d4386212 100644 --- a/virttest/base_installer.py +++ b/virttest/base_installer.py @@ -20,7 +20,7 @@ from . import arch from .staging import utils_koji -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class NoModuleError(Exception): @@ -28,6 +28,7 @@ class NoModuleError(Exception): """ Error raised when no suitable modules were found to load """ + pass @@ -36,6 +37,7 @@ class VirtInstallException(Exception): """ Base virtualization software components installation exception """ + pass @@ -44,6 +46,7 @@ class VirtInstallFailed(VirtInstallException): """ Installation of virtualization software components failed """ + pass @@ -52,6 +55,7 @@ class VirtInstallNotInstalled(VirtInstallException): """ Virtualization software components are not installed """ + pass @@ -77,12 +81,11 @@ def __init__(self, mode, name, test=None, params=None): self.name = name self.params = params self.test = test - self.param_key_prefix = '%s_%s' % (self.mode, - self.name) + self.param_key_prefix = "%s_%s" % (self.mode, self.name) # If a installer has a failure that can be worked around, save that self.minor_failure = False - self.minor_failure_reason = '' + self.minor_failure_reason = "" if test and params: self.set_install_params(test, params) @@ -133,10 +136,10 @@ def _set_param_load_module(self): Configuration file parameter: load_modules Class attribute set: should_load_modules """ - load_modules = self.params.get('load_modules', 'no') - if not load_modules or load_modules == 'yes': + load_modules = self.params.get("load_modules", "no") + if not load_modules or load_modules == "yes": self.should_load_modules = True - elif load_modules == 'no': + elif load_modules == "no": self.should_load_modules = False def _set_param_module_list(self): @@ -146,7 +149,7 @@ def _set_param_module_list(self): Configuration file parameter: module_list Class attribute set: module_list """ - self.module_list = self.params.get('module_list', []) + self.module_list = self.params.get("module_list", []) def _set_param_save_results(self): """ @@ -156,8 +159,8 @@ def _set_param_save_results(self): Class attribute set: save_results """ self.save_results = True - save_results = self.params.get('save_results', 'no') - if save_results == 'no': + save_results = self.params.get("save_results", "no") + if save_results == "no": self.save_results = False def _set_param_install_debug_info(self): @@ -168,8 +171,8 @@ def _set_param_install_debug_info(self): Class attribute set: install_debug_info """ self.install_debug_info = True - install_debug_info = self.params.get('install_debug_info', 'no') - if install_debug_info == 'no': + install_debug_info = self.params.get("install_debug_info", "no") + if install_debug_info == "no": self.install_debug_info = False def _set_param_cleanup(self): @@ -180,8 +183,8 @@ def _set_param_cleanup(self): Class attribute set: cleanup """ self.cleanup = True - cleanup = self.params.get('installer_cleanup', 'yes') - if cleanup == 'no': + cleanup = self.params.get("installer_cleanup", "yes") + if cleanup == "no": LOG.debug("Setting installer cleanup attribute to False") self.cleanup = False @@ -332,18 +335,17 @@ def _install_phase_init_verify(self): pass def _install_phase_package(self): - """ - """ + """ """ pass def _install_phase_package_verify(self): - ''' + """ Optional install phase for checking that software is packaged. This should verify that the packages are actually created. Ideas for using this include: * checking /root/rpmbuild/RPMS' - ''' + """ pass def write_version_keyval(self, test): @@ -351,7 +353,7 @@ def write_version_keyval(self, test): version = self.get_version() except AttributeError: version = "Unknown" - sw_version = {('software_version_%s' % self.name): version} + sw_version = {("software_version_%s" % self.name): version} LOG.debug("Writing test keyval %s", sw_version) test.write_test_keyval(sw_version) @@ -400,8 +402,16 @@ def reload_modules_if_needed(self): if self.should_load_modules: self.reload_modules() - def install(self, cleanup=True, download=True, prepare=True, - build=True, install=True, package=False, init=True): + def install( + self, + cleanup=True, + download=True, + prepare=True, + build=True, + install=True, + package=False, + init=True, + ): """ Performs the installation of the virtualization software @@ -409,7 +419,7 @@ def install(self, cleanup=True, download=True, prepare=True, be reimplemented completely, or simply implement one or many of the install phases. """ - if (cleanup and self.cleanup): + if cleanup and self.cleanup: self._install_phase_cleanup() self._install_phase_cleanup_verify() @@ -439,8 +449,7 @@ def install(self, cleanup=True, download=True, prepare=True, self.reload_modules_if_needed() if self.save_results: - utils_misc.archive_as_tarball(self.test_workdir, - self.test_resultsdir) + utils_misc.archive_as_tarball(self.test_workdir, self.test_resultsdir) def uninstall(self): """ @@ -466,13 +475,14 @@ def __init__(self, mode, name, test=None, params=None): :param test: Virt test object. :param params: Dict with test params. """ - if params['vm_type'] == 'qemu': - params['module_list'] = arch.get_kvm_module_list() + if params["vm_type"] == "qemu": + params["module_list"] = arch.get_kvm_module_list() super(NoopInstaller, self).__init__(mode, name, test, params) def install(self): - LOG.info("Assuming virtualization software to be already " - "installed. Doing nothing") + LOG.info( + "Assuming virtualization software to be already " "installed. Doing nothing" + ) class YumInstaller(BaseInstaller): @@ -497,8 +507,7 @@ def set_install_params(self, test, params): path.find_command("yum") if self.install_debug_info: path.find_command("debuginfo-install") - self.yum_pkgs = eval(params.get("%s_pkgs" % self.param_key_prefix, - "[]")) + self.yum_pkgs = eval(params.get("%s_pkgs" % self.param_key_prefix, "[]")) def get_version(self): return " ".join(self.yum_pkgs) @@ -514,11 +523,12 @@ def _install_phase_package(self): def _install_phase_install(self): if self.yum_pkgs: os.chdir(self.test_workdir) - process.system("yum --nogpgcheck -y install %s" % - " ".join(self.yum_pkgs)) + process.system("yum --nogpgcheck -y install %s" % " ".join(self.yum_pkgs)) if self.install_debug_info: - process.system("debuginfo-install --enablerepo='*debuginfo' -y %s" % - " ".join(self.yum_pkgs)) + process.system( + "debuginfo-install --enablerepo='*debuginfo' -y %s" + % " ".join(self.yum_pkgs) + ) class KojiInstaller(BaseInstaller): @@ -542,13 +552,13 @@ def set_install_params(self, test, params): self.koji_cmd = params.get("%s_cmd" % self.param_key_prefix, None) if self.tag is not None: utils_koji.set_default_koji_tag(self.tag) - self.koji_pkgs = params.get("%s_pkgs" % self.param_key_prefix, - "").split() - self.koji_scratch_pkgs = params.get("%s_scratch_pkgs" % - self.param_key_prefix, - "").split() - self.koji_yumrepo_baseurl = params.get("%s_yumrepo_baseurl" % - self.param_key_prefix, None) + self.koji_pkgs = params.get("%s_pkgs" % self.param_key_prefix, "").split() + self.koji_scratch_pkgs = params.get( + "%s_scratch_pkgs" % self.param_key_prefix, "" + ).split() + self.koji_yumrepo_baseurl = params.get( + "%s_yumrepo_baseurl" % self.param_key_prefix, None + ) if self.install_debug_info: self._expand_koji_pkgs_with_debuginfo() @@ -566,7 +576,7 @@ def _expand_koji_pkgs_with_debuginfo(self): koji_pkgs_with_debug = [] for pkg_text in self.koji_pkgs: pkg = utils_koji.KojiPkgSpec(pkg_text) - debuginfo_pkg_name = '%s-debuginfo' % pkg.package + debuginfo_pkg_name = "%s-debuginfo" % pkg.package # if no subpackages are set, then all packages will be installed # so there's no need to manually include debuginfo packages if pkg.subpackages: @@ -576,8 +586,9 @@ def _expand_koji_pkgs_with_debuginfo(self): pkg.subpackages.append(debuginfo_pkg_name) pkg_with_debug_text = pkg.to_text() - LOG.debug("KojiPkgSpec with debuginfo package added: %s", - pkg_with_debug_text) + LOG.debug( + "KojiPkgSpec with debuginfo package added: %s", pkg_with_debug_text + ) koji_pkgs_with_debug.append(pkg_with_debug_text) # swap current koji_pkgs with on that includes debuginfo pkgs @@ -596,7 +607,7 @@ def _get_rpm_names(self): file_names = list(map(os.path.basename, rpm_urls)) for f in file_names: r = utils_koji.RPMFileNameInfo(f) - all_rpm_names.append(r.get_nvr_info()['name']) + all_rpm_names.append(r.get_nvr_info()["name"]) return all_rpm_names def _get_rpm_file_names(self): @@ -624,18 +635,22 @@ def _install_phase_download(self): if pkg.is_valid(): koji_client.get_pkgs(pkg, dst_dir=self.test_workdir) else: - LOG.error('Package specification (%s) is invalid: %s' % - (pkg, pkg.describe_invalid())) + LOG.error( + "Package specification (%s) is invalid: %s" + % (pkg, pkg.describe_invalid()) + ) for pkg_text in self.koji_scratch_pkgs: pkg = utils_koji.KojiScratchPkgSpec(pkg_text) koji_client.get_scratch_pkgs(pkg, dst_dir=self.test_workdir) def _install_phase_install(self): if self.koji_yumrepo_baseurl is not None: - repo = yumrepo.YumRepo(self.param_key_prefix, - self.koji_yumrepo_baseurl) - LOG.debug('Enabling YUM Repo "%s" at "%s"', - self.param_key_prefix, self.koji_yumrepo_baseurl) + repo = yumrepo.YumRepo(self.param_key_prefix, self.koji_yumrepo_baseurl) + LOG.debug( + 'Enabling YUM Repo "%s" at "%s"', + self.param_key_prefix, + self.koji_yumrepo_baseurl, + ) repo.save() os.chdir(self.test_workdir) @@ -643,13 +658,15 @@ def _install_phase_install(self): process.system("yum --nogpgcheck -y install %s" % rpm_file_names) if self.koji_yumrepo_baseurl is not None: - LOG.debug('Disabling YUM Repo "%s" at "%s"', - self.param_key_prefix, self.koji_yumrepo_baseurl) + LOG.debug( + 'Disabling YUM Repo "%s" at "%s"', + self.param_key_prefix, + self.koji_yumrepo_baseurl, + ) repo.remove() class BaseLocalSourceInstaller(BaseInstaller): - def set_install_params(self, test, params): super(BaseLocalSourceInstaller, self).set_install_params(test, params) self._set_install_prefix() @@ -659,9 +676,8 @@ def set_install_params(self, test, params): # There are really no choices for patch helpers # self.patch_helper = build_helper.PatchParamHelper( - self.params, - self.param_key_prefix, - self.source_destination) + self.params, self.param_key_prefix, self.source_destination + ) # # These helpers should be set by child classes @@ -677,31 +693,33 @@ def _set_install_prefix(self): the resulting binaries will be installed. Usually this is the value passed to the configure script, ie: ./configure --prefix= """ - prefix = os.path.join(self.test_builddir, 'install_root') + prefix = os.path.join(self.test_builddir, "install_root") self.install_prefix = os.path.abspath(prefix) def _set_source_destination(self): """ Sets the source code destination directory path """ - self.source_destination = os.path.join(self.test_workdir, - self.name) + self.source_destination = os.path.join(self.test_workdir, self.name) def _set_build_helper(self): """ Sets the build helper, default is 'gnu_autotools' """ - build_helper_name = self.params.get('%s_build_helper' % - self.param_key_prefix, - 'gnu_autotools') - if build_helper_name == 'gnu_autotools': + build_helper_name = self.params.get( + "%s_build_helper" % self.param_key_prefix, "gnu_autotools" + ) + if build_helper_name == "gnu_autotools": self.build_helper = build_helper.GnuSourceBuildParamHelper( - self.params, self.param_key_prefix, - self.source_destination, self.install_prefix) - elif build_helper_name == 'linux_kernel': + self.params, + self.param_key_prefix, + self.source_destination, + self.install_prefix, + ) + elif build_helper_name == "linux_kernel": self.build_helper = build_helper.LinuxKernelBuildHelper( - self.params, self.param_key_prefix, - self.source_destination) + self.params, self.param_key_prefix, self.source_destination + ) def _install_phase_prepare(self): if self.patch_helper is not None: @@ -713,10 +731,9 @@ def _install_phase_download(self): def _install_phase_build(self): if self.build_helper is not None: - if (isinstance(self.build_helper, - build_helper.GnuSourceBuildHelper) or - isinstance(self.build_helper, - build_helper.LinuxKernelBuildHelper)): + if isinstance( + self.build_helper, build_helper.GnuSourceBuildHelper + ) or isinstance(self.build_helper, build_helper.LinuxKernelBuildHelper): try: self.build_helper.execute() except build_helper.SourceBuildParallelFailed: @@ -726,8 +743,7 @@ def _install_phase_build(self): except build_helper.SourceBuildFailed: # Failed the current test - raise exceptions.TestError( - "Failed to build %s" % self.name) + raise exceptions.TestError("Failed to build %s" % self.name) else: self.build_helper.execute() @@ -746,9 +762,8 @@ def set_install_params(self, test, params): super(LocalSourceDirInstaller, self).set_install_params(test, params) self.content_helper = build_helper.LocalSourceDirParamHelper( - params, - self.name, - self.source_destination) + params, self.name, self.source_destination + ) self._set_build_helper() @@ -763,9 +778,8 @@ def set_install_params(self, test, params): super(LocalSourceTarInstaller, self).set_install_params(test, params) self.content_helper = build_helper.LocalTarParamHelper( - params, - self.name, - self.source_destination) + params, self.name, self.source_destination + ) self._set_build_helper() @@ -780,22 +794,19 @@ def set_install_params(self, test, params): super(RemoteSourceTarInstaller, self).set_install_params(test, params) self.content_helper = build_helper.RemoteTarParamHelper( - params, - self.name, - self.source_destination) + params, self.name, self.source_destination + ) self._set_build_helper() class GitRepoInstaller(BaseLocalSourceInstaller): - def set_install_params(self, test, params): super(GitRepoInstaller, self).set_install_params(test, params) self.content_helper = build_helper.GitRepoParamHelper( - params, - self.name, - self.source_destination) + params, self.name, self.source_destination + ) self._set_build_helper() @@ -821,5 +832,4 @@ def load_modules(self): """ Will refuse to load the kerkel modules as install failed """ - raise VirtInstallFailed("Kernel modules not available. reason: %s" % - self._msg) + raise VirtInstallFailed("Kernel modules not available. reason: %s" % self._msg) diff --git a/virttest/bootstrap.py b/virttest/bootstrap.py index f2e7d24961..8c3395a437 100644 --- a/virttest/bootstrap.py +++ b/virttest/bootstrap.py @@ -24,61 +24,72 @@ LOG = logging.getLogger("avocado.app") -basic_program_requirements = ['xz', 'tcpdump', 'nc', 'ip', 'arping', 'diff'] - -recommended_programs = {'qemu': [('qemu-kvm', 'kvm'), ('qemu-img',), - ('qemu-io',), ('virsh',)], - 'spice': [('qemu-kvm', 'kvm'), ('qemu-img',), - ('qemu-io',)], - 'libvirt': [('virsh',), ('virt-install',), - ('fakeroot',), ('semanage',), - ('getfattr',), ('restorecon',)], - 'openvswitch': [], - 'lvsb': [('semanage',), ('getfattr',), ('restorecon',), ('virt-sandbox')], - 'v2v': [], - 'libguestfs': [('perl',)], - 'virttools': []} - -mandatory_programs = {'qemu': basic_program_requirements + ['gcc'], - 'spice': basic_program_requirements + ['gcc'], - 'libvirt': basic_program_requirements, - 'openvswitch': basic_program_requirements, - 'lvsb': ['virt-sandbox', 'virt-sandbox-service', 'virsh'], - 'v2v': basic_program_requirements, - 'libguestfs': basic_program_requirements, - 'virttools': basic_program_requirements + ['virt-install', - 'virt-clone', - 'virt-manager', - 'virt-xml']} - -mandatory_headers = {'qemu': [], - 'spice': [], - 'libvirt': [], - 'openvswitch': [], - 'v2v': [], - 'lvsb': [], - 'libguestfs': [], - 'virttools': []} - -first_subtest = {'qemu': ['unattended_install', 'steps'], - 'spice': ['unattended_install', 'steps'], - 'libvirt': ['unattended_install'], - 'openvswitch': ['unattended_install'], - 'v2v': ['unattended_install'], - 'libguestfs': ['unattended_install'], - 'lvsb': [], - 'virttools': []} - -last_subtest = {'qemu': ['shutdown'], - 'spice': ['shutdown'], - 'libvirt': ['shutdown', 'remove_guest'], - 'openvswitch': ['shutdown'], - 'v2v': ['shutdown'], - 'libguestfs': ['shutdown'], - 'lvsb': [], - 'virttools': []} - -test_filter = ['__init__', 'cfg', 'dropin.py'] +basic_program_requirements = ["xz", "tcpdump", "nc", "ip", "arping", "diff"] + +recommended_programs = { + "qemu": [("qemu-kvm", "kvm"), ("qemu-img",), ("qemu-io",), ("virsh",)], + "spice": [("qemu-kvm", "kvm"), ("qemu-img",), ("qemu-io",)], + "libvirt": [ + ("virsh",), + ("virt-install",), + ("fakeroot",), + ("semanage",), + ("getfattr",), + ("restorecon",), + ], + "openvswitch": [], + "lvsb": [("semanage",), ("getfattr",), ("restorecon",), ("virt-sandbox")], + "v2v": [], + "libguestfs": [("perl",)], + "virttools": [], +} + +mandatory_programs = { + "qemu": basic_program_requirements + ["gcc"], + "spice": basic_program_requirements + ["gcc"], + "libvirt": basic_program_requirements, + "openvswitch": basic_program_requirements, + "lvsb": ["virt-sandbox", "virt-sandbox-service", "virsh"], + "v2v": basic_program_requirements, + "libguestfs": basic_program_requirements, + "virttools": basic_program_requirements + + ["virt-install", "virt-clone", "virt-manager", "virt-xml"], +} + +mandatory_headers = { + "qemu": [], + "spice": [], + "libvirt": [], + "openvswitch": [], + "v2v": [], + "lvsb": [], + "libguestfs": [], + "virttools": [], +} + +first_subtest = { + "qemu": ["unattended_install", "steps"], + "spice": ["unattended_install", "steps"], + "libvirt": ["unattended_install"], + "openvswitch": ["unattended_install"], + "v2v": ["unattended_install"], + "libguestfs": ["unattended_install"], + "lvsb": [], + "virttools": [], +} + +last_subtest = { + "qemu": ["shutdown"], + "spice": ["shutdown"], + "libvirt": ["shutdown", "remove_guest"], + "openvswitch": ["shutdown"], + "v2v": ["shutdown"], + "libguestfs": ["shutdown"], + "lvsb": [], + "virttools": [], +} + +test_filter = ["__init__", "cfg", "dropin.py"] def get_guest_os_info_list(test_name, guest_os): @@ -90,27 +101,31 @@ def get_guest_os_info_list(test_name, guest_os): cartesian_parser = cartesian_config.Parser() cartesian_parser.parse_file( - data_dir.get_backend_cfg_path(test_name, 'guest-os.cfg')) + data_dir.get_backend_cfg_path(test_name, "guest-os.cfg") + ) cartesian_parser.only_filter(guest_os) dicts = cartesian_parser.get_dicts() for params in dicts: - image_name = params.get('image_name', 'image').split('/')[-1] - shortname = params.get('shortname', guest_os) - os_info_list.append({'asset': image_name, 'variant': shortname}) + image_name = params.get("image_name", "image").split("/")[-1] + shortname = params.get("shortname", guest_os) + os_info_list.append({"asset": image_name, "variant": shortname}) if not os_info_list: - LOG.error("Could not find any assets compatible with %s for %s", - guest_os, test_name) + LOG.error( + "Could not find any assets compatible with %s for %s", guest_os, test_name + ) raise ValueError("Missing compatible assets for %s" % guest_os) return os_info_list def get_config_filter(): - config_filter = ['__init__', ] + config_filter = [ + "__init__", + ] for provider_subdir in asset.get_test_provider_subdirs(): - config_filter.append(os.path.join('%s' % provider_subdir, 'cfg')) + config_filter.append(os.path.join("%s" % provider_subdir, "cfg")) return config_filter @@ -122,19 +137,25 @@ def verify_recommended_programs(t_type): found = None try: found = utils_path.find_command(cmd) - LOG.debug('%s OK', found) + LOG.debug("%s OK", found) break except utils_path.CmdNotFoundError: pass if not found: if len(cmd_aliases) == 1: - LOG.info("Recommended command %s missing. You may " - "want to install it if not building from " - "source.", cmd_aliases[0]) + LOG.info( + "Recommended command %s missing. You may " + "want to install it if not building from " + "source.", + cmd_aliases[0], + ) else: - LOG.info("Recommended command missing. You may " - "want to install it if not building it from " - "source. Aliases searched: %s", cmd_aliases) + LOG.info( + "Recommended command missing. You may " + "want to install it if not building it from " + "source. Aliases searched: %s", + cmd_aliases, + ) def verify_mandatory_programs(t_type, guest_os): @@ -142,29 +163,29 @@ def verify_mandatory_programs(t_type, guest_os): cmds = mandatory_programs[t_type] for cmd in cmds: try: - LOG.debug('%s OK', utils_path.find_command(cmd)) + LOG.debug("%s OK", utils_path.find_command(cmd)) except utils_path.CmdNotFoundError: - LOG.error("Required command %s is missing. You must " - "install it", cmd) + LOG.error("Required command %s is missing. You must " "install it", cmd) failed_cmds.append(cmd) includes = mandatory_headers[t_type] - available_includes = glob.glob('/usr/include/*/*') + available_includes = glob.glob("/usr/include/*/*") for include in available_includes: include_basename = os.path.basename(include) if include_basename in includes: - LOG.debug('%s OK', include) + LOG.debug("%s OK", include) includes.pop(includes.index(include_basename)) if includes: for include in includes: - LOG.error("Required include %s is missing. You may have to " - "install it", include) + LOG.error( + "Required include %s is missing. You may have to " "install it", include + ) failures = failed_cmds + includes if failures: - raise ValueError('Missing (cmds/includes): %s' % " ".join(failures)) + raise ValueError("Missing (cmds/includes): %s" % " ".join(failures)) def write_subtests_files(config_file_list, output_file_object, test_type=None): @@ -179,13 +200,13 @@ def write_subtests_files(config_file_list, output_file_object, test_type=None): output_file_object.write(" variants subtest:\n") for provider_name, config_path in config_file_list: - config_file = open(config_path, 'r') + config_file = open(config_path, "r") write_test_type_line = False write_provider_line = False for line in config_file.readlines(): - if line.startswith('- ') and provider_name is not None: + if line.startswith("- ") and provider_name is not None: name, deps = line.split(":") name = name[1:].strip() if name[0] == "@": @@ -195,23 +216,20 @@ def write_subtests_files(config_file_list, output_file_object, test_type=None): # special virt_test_type line output if test_type is not None: if write_test_type_line: - type_line = (" virt_test_type = %s\n" % - test_type) + type_line = " virt_test_type = %s\n" % test_type output_file_object.write(type_line) - provider_line = (" provider = %s\n" % - provider_name) + provider_line = " provider = %s\n" % provider_name output_file_object.write(provider_line) write_test_type_line = False - elif line.startswith('- '): + elif line.startswith("- "): write_test_type_line = True output_file_object.write(" %s" % line) else: if write_provider_line: - provider_line = (" provider = %s\n" % - provider_name) + provider_line = " provider = %s\n" % provider_name output_file_object.write(provider_line) write_provider_line = False - elif line.startswith('- '): + elif line.startswith("- "): write_provider_line = True # regular line output output_file_object.write(" %s" % line) @@ -248,32 +266,29 @@ def get_directory_structure(rootdir, guest_file, first_variant=None): base_cfg = "%s.cfg" % base_folder base_cfg_path = os.path.join(os.path.dirname(path), base_cfg) if os.path.isfile(base_cfg_path): - base_file = open(base_cfg_path, 'r') + base_file = open(base_cfg_path, "r") for line in base_file.readlines(): offset = first_variant_offset + indent - 1 guest_file.write("%s%s" % ((4 * offset * " "), line)) else: if base_folder: offset = first_variant_offset + indent - 1 - guest_file.write("%s- %s:\n" % - ((4 * offset * " "), base_folder)) + guest_file.write("%s- %s:\n" % ((4 * offset * " "), base_folder)) variant_printed = False if files: files.sort() for f in files: if f.endswith(".cfg"): - bf = f[:len(f) - 4] + bf = f[: len(f) - 4] if bf not in subdirs: if not variant_printed: offset = first_variant_offset + indent - guest_file.write("%svariants:\n" % - ((4 * offset * " "))) + guest_file.write("%svariants:\n" % ((4 * offset * " "))) variant_printed = True - base_file = open(os.path.join(path, f), 'r') + base_file = open(os.path.join(path, f), "r") for line in base_file.readlines(): offset = first_variant_offset + indent + 1 - guest_file.write("%s%s" - % ((4 * offset * " "), line)) + guest_file.write("%s%s" % ((4 * offset * " "), line)) indent -= number_variants previous_indent = indent @@ -281,44 +296,43 @@ def get_directory_structure(rootdir, guest_file, first_variant=None): def sync_download_dir(interactive): base_download_dir = data_dir.get_base_download_dir() download_dir = data_dir.get_download_dir() - LOG.debug("Copying downloadable assets file definitions from %s " - "into %s", base_download_dir, download_dir) - download_file_list = glob.glob(os.path.join(base_download_dir, - "*.ini")) + LOG.debug( + "Copying downloadable assets file definitions from %s " "into %s", + base_download_dir, + download_dir, + ) + download_file_list = glob.glob(os.path.join(base_download_dir, "*.ini")) for src_file in download_file_list: - dst_file = os.path.join(download_dir, - os.path.basename(src_file)) + dst_file = os.path.join(download_dir, os.path.basename(src_file)) if not os.path.isfile(dst_file): shutil.copyfile(src_file, dst_file) else: diff_cmd = "diff -Naur %s %s" % (dst_file, src_file) - diff_result = process.run( - diff_cmd, ignore_status=True, verbose=False) + diff_result = process.run(diff_cmd, ignore_status=True, verbose=False) if diff_result.exit_status != 0: - LOG.debug("%s result:\n %s", - diff_result.command, - diff_result.stdout_text) - answer = genio.ask('Download file "%s" differs from "%s". ' - 'Overwrite?' % (dst_file, src_file), - auto=not interactive) + LOG.debug( + "%s result:\n %s", diff_result.command, diff_result.stdout_text + ) + answer = genio.ask( + 'Download file "%s" differs from "%s". ' + "Overwrite?" % (dst_file, src_file), + auto=not interactive, + ) if answer == "y": - LOG.debug("Restoring download file %s from sample", - dst_file) + LOG.debug("Restoring download file %s from sample", dst_file) shutil.copyfile(src_file, dst_file) else: LOG.debug("Preserving existing %s file", dst_file) else: - LOG.debug('Download file %s exists, not touching', - dst_file) + LOG.debug("Download file %s exists, not touching", dst_file) def create_guest_os_cfg(t_type): - guest_os_cfg_dir = os.path.join(data_dir.get_shared_dir(), 'cfg', 'guest-os') - guest_os_cfg_path = data_dir.get_backend_cfg_path(t_type, 'guest-os.cfg') - guest_os_cfg_file = open(guest_os_cfg_path, 'w') + guest_os_cfg_dir = os.path.join(data_dir.get_shared_dir(), "cfg", "guest-os") + guest_os_cfg_path = data_dir.get_backend_cfg_path(t_type, "guest-os.cfg") + guest_os_cfg_file = open(guest_os_cfg_path, "w") get_directory_structure(guest_os_cfg_dir, guest_os_cfg_file, "Guest") - LOG.debug("Config file %s auto generated from guest OS samples", - guest_os_cfg_path) + LOG.debug("Config file %s auto generated from guest OS samples", guest_os_cfg_path) def host_os_get_distro_name(options, detected): @@ -333,12 +347,12 @@ def host_os_get_distro_name(options, detected): :param detected: result of :class:`avocado.utils.distro.detect` :type detected: :class:`avocado.utils.distro.LinuxDistro` """ - if get_opt(options, 'vt_host_distro_name'): - return get_opt(options, 'vt_host_distro_name') - if detected.name == 'rhel': - return 'RHEL' - elif detected.name == 'fedora': - return 'Fedora' + if get_opt(options, "vt_host_distro_name"): + return get_opt(options, "vt_host_distro_name") + if detected.name == "rhel": + return "RHEL" + elif detected.name == "fedora": + return "Fedora" return "Host_%s" % detected.name @@ -348,27 +362,38 @@ def _forced_or_detected(forced, detected): return forced else: return detected - host_os_cfg_path = data_dir.get_backend_cfg_path(get_opt(options, 'vt.type'), - 'host.cfg') - with open(host_os_cfg_path, 'w') as cfg: + + host_os_cfg_path = data_dir.get_backend_cfg_path( + get_opt(options, "vt.type"), "host.cfg" + ) + with open(host_os_cfg_path, "w") as cfg: detected = distro.detect() name = host_os_get_distro_name(options, detected) - version = _forced_or_detected(get_opt(options, 'vt_host_distro_version'), - "m%s" % detected.version) - release = _forced_or_detected(get_opt(options, 'vt_host_distro_release'), - "u%s" % detected.release) - arch = _forced_or_detected(get_opt(options, 'vt_host_distro_arch'), - "Host_arch_%s" % detected.arch) - vendor = cpu.get_vendor() if hasattr(cpu, 'get_vendor') else cpu.get_cpu_vendor_name() + version = _forced_or_detected( + get_opt(options, "vt_host_distro_version"), "m%s" % detected.version + ) + release = _forced_or_detected( + get_opt(options, "vt_host_distro_release"), "u%s" % detected.release + ) + arch = _forced_or_detected( + get_opt(options, "vt_host_distro_arch"), "Host_arch_%s" % detected.arch + ) + vendor = ( + cpu.get_vendor() + if hasattr(cpu, "get_vendor") + else cpu.get_cpu_vendor_name() + ) family = None - if hasattr(cpu, 'get_family'): + if hasattr(cpu, "get_family"): try: family = cpu.get_family() except Exception: pass - cpu_version = cpu.get_version() if hasattr(cpu, 'get_version') else None + cpu_version = cpu.get_version() if hasattr(cpu, "get_version") else None # Replace special chars with _ to avoid bootstrap failure - cpu_version = re.sub(r'[^\w-]', '_', cpu_version) if cpu_version else cpu_version + cpu_version = ( + re.sub(r"[^\w-]", "_", cpu_version) if cpu_version else cpu_version + ) cfg.write("variants:\n") cfg.write(" - @Host:\n") @@ -396,10 +421,12 @@ def _forced_or_detected(forced, detected): cfg.write(" variants:\n") cfg.write(" - @%s:\n" % cpu_version) - count = [get_opt(options, 'vt_host_distro_name'), - get_opt(options, 'vt_host_distro_version'), - get_opt(options, 'vt_host_distro_release'), - get_opt(options, 'vt_host_distro_arch')].count(None) + count = [ + get_opt(options, "vt_host_distro_name"), + get_opt(options, "vt_host_distro_version"), + get_opt(options, "vt_host_distro_release"), + get_opt(options, "vt_host_distro_arch"), + ].count(None) if count == 4: source = "distro detection" elif count == 0: @@ -418,70 +445,60 @@ def create_subtests_cfg(t_type): provider_info_specific = [] for specific_provider in provider_names_specific: - provider_info_specific.append( - asset.get_test_provider_info(specific_provider)) + provider_info_specific.append(asset.get_test_provider_info(specific_provider)) for subdir in specific_subdirs: - specific_test_list += data_dir.SubdirGlobList(subdir, - '*.py', - test_filter) - specific_file_list += data_dir.SubdirGlobList(subdir, - '*.cfg', - config_filter) + specific_test_list += data_dir.SubdirGlobList(subdir, "*.py", test_filter) + specific_file_list += data_dir.SubdirGlobList(subdir, "*.cfg", config_filter) shared_test_list = [] shared_file_list = [] - shared_subdirs = asset.get_test_provider_subdirs('generic') - shared_subdirs += asset.get_test_provider_subdirs('multi_host_migration') - provider_names_shared = asset.get_test_provider_names('generic') - provider_names_shared += asset.get_test_provider_names('multi_host_migration') + shared_subdirs = asset.get_test_provider_subdirs("generic") + shared_subdirs += asset.get_test_provider_subdirs("multi_host_migration") + provider_names_shared = asset.get_test_provider_names("generic") + provider_names_shared += asset.get_test_provider_names("multi_host_migration") provider_info_shared = [] for shared_provider in provider_names_shared: - provider_info_shared.append( - asset.get_test_provider_info(shared_provider)) + provider_info_shared.append(asset.get_test_provider_info(shared_provider)) - if not t_type == 'lvsb': + if not t_type == "lvsb": for subdir in shared_subdirs: - shared_test_list += data_dir.SubdirGlobList(subdir, - '*.py', - test_filter) - shared_file_list += data_dir.SubdirGlobList(subdir, - '*.cfg', - config_filter) + shared_test_list += data_dir.SubdirGlobList(subdir, "*.py", test_filter) + shared_file_list += data_dir.SubdirGlobList(subdir, "*.cfg", config_filter) all_specific_test_list = [] for test in specific_test_list: for p in provider_info_specific: - provider_base_path = p['backends'][t_type]['path'] + provider_base_path = p["backends"][t_type]["path"] if provider_base_path in test: - provider_name = p['name'] + provider_name = p["name"] break basename = os.path.basename(test) if basename != "__init__.py": - all_specific_test_list.append("%s.%s" % - (provider_name, - basename.split(".")[0])) + all_specific_test_list.append( + "%s.%s" % (provider_name, basename.split(".")[0]) + ) all_shared_test_list = [] for test in shared_test_list: for p in provider_info_shared: - if 'generic' in p['backends']: - provider_base_path = p['backends']['generic']['path'] + if "generic" in p["backends"]: + provider_base_path = p["backends"]["generic"]["path"] if provider_base_path in test: - provider_name = p['name'] + provider_name = p["name"] break - if 'multi_host_migration' in p['backends']: - provider_base_path = p['backends']['multi_host_migration']['path'] + if "multi_host_migration" in p["backends"]: + provider_base_path = p["backends"]["multi_host_migration"]["path"] if provider_base_path in test: - provider_name = p['name'] + provider_name = p["name"] break basename = os.path.basename(test) if basename != "__init__.py": - all_shared_test_list.append("%s.%s" % - (provider_name, - basename.split(".")[0])) + all_shared_test_list.append( + "%s.%s" % (provider_name, basename.split(".")[0]) + ) all_specific_test_list.sort() all_shared_test_list.sort() @@ -494,27 +511,26 @@ def create_subtests_cfg(t_type): for shared_file in shared_file_list: provider_name = None for p in provider_info_shared: - provider_base_path = p['backends']['generic']['path'] + provider_base_path = p["backends"]["generic"]["path"] if provider_base_path in shared_file: - provider_name = p['name'] + provider_name = p["name"] break - provider_base_path = p['backends']['multi_host_migration']['path'] + provider_base_path = p["backends"]["multi_host_migration"]["path"] if provider_base_path in test: - provider_name = p['name'] + provider_name = p["name"] break - shared_file_obj = open(shared_file, 'r') + shared_file_obj = open(shared_file, "r") for line in shared_file_obj.readlines(): line = line.strip() if re.match("type\s*=.*", line): cartesian_parser = cartesian_config.Parser() cartesian_parser.parse_string(line) td = next(cartesian_parser.get_dicts()) - values = td['type'].split(" ") + values = td["type"].split(" ") for value in values: if t_type not in non_dropin_tests: - non_dropin_tests.append("%s.%s" % - (provider_name, value)) + non_dropin_tests.append("%s.%s" % (provider_name, value)) shared_file_name = os.path.basename(shared_file) shared_file_name = shared_file_name.split(".")[0] @@ -533,23 +549,22 @@ def create_subtests_cfg(t_type): for shared_file in specific_file_list: provider_name = None for p in provider_info_specific: - provider_base_path = p['backends'][t_type]['path'] + provider_base_path = p["backends"][t_type]["path"] if provider_base_path in shared_file: - provider_name = p['name'] + provider_name = p["name"] break - shared_file_obj = open(shared_file, 'r') + shared_file_obj = open(shared_file, "r") for line in shared_file_obj.readlines(): line = line.strip() if re.match("type\s*=.*", line): cartesian_parser = cartesian_config.Parser() cartesian_parser.parse_string(line) td = next(cartesian_parser.get_dicts()) - values = td['type'].split(" ") + values = td["type"].split(" ") for value in values: if value not in non_dropin_tests: - non_dropin_tests.append("%s.%s" % - (provider_name, value)) + non_dropin_tests.append("%s.%s" % (provider_name, value)) shared_file_name = os.path.basename(shared_file) shared_file_name = shared_file_name.split(".")[0] @@ -564,11 +579,9 @@ def create_subtests_cfg(t_type): tmp.append([provider_name, shared_file]) specific_file_list = tmp - subtests_cfg = os.path.join(data_dir.get_backend_dir(t_type), 'cfg', - 'subtests.cfg') - subtests_file = open(subtests_cfg, 'w') - subtests_file.write( - "# Do not edit, auto generated file from subtests config\n") + subtests_cfg = os.path.join(data_dir.get_backend_dir(t_type), "cfg", "subtests.cfg") + subtests_file = open(subtests_cfg, "w") + subtests_file.write("# Do not edit, auto generated file from subtests config\n") subtests_file.write("variants subtest:\n") write_subtests_files(first_subtest_file, subtests_file) @@ -577,15 +590,16 @@ def create_subtests_cfg(t_type): write_subtests_files(last_subtest_file, subtests_file) subtests_file.close() - LOG.debug("Config file %s auto generated from subtest samples", - subtests_cfg) + LOG.debug("Config file %s auto generated from subtest samples", subtests_cfg) -def create_config_files(test_dir, shared_dir, interactive, t_type, step=None, - force_update=False): +def create_config_files( + test_dir, shared_dir, interactive, t_type, step=None, force_update=False +): def is_file_tracked(fl): - tracked_result = process.run("git ls-files %s --error-unmatch" % fl, - ignore_status=True, verbose=False) + tracked_result = process.run( + "git ls-files %s --error-unmatch" % fl, ignore_status=True, verbose=False + ) return tracked_result.exit_status == 0 if step is None: @@ -593,24 +607,22 @@ def is_file_tracked(fl): LOG.info("") step += 1 LOG.info("%d - Generating config set", step) - config_file_list = data_dir.SubdirGlobList(os.path.join(test_dir, "cfg"), - "*.cfg", - get_config_filter()) + config_file_list = data_dir.SubdirGlobList( + os.path.join(test_dir, "cfg"), "*.cfg", get_config_filter() + ) config_file_list = [cf for cf in config_file_list if is_file_tracked(cf)] - config_file_list_shared = glob.glob(os.path.join(shared_dir, "cfg", - "*.cfg")) + config_file_list_shared = glob.glob(os.path.join(shared_dir, "cfg", "*.cfg")) provider_info_specific = [] provider_names_specific = asset.get_test_provider_names(t_type) for specific_provider in provider_names_specific: - provider_info_specific.append( - asset.get_test_provider_info(specific_provider)) + provider_info_specific.append(asset.get_test_provider_info(specific_provider)) specific_subdirs = asset.get_test_provider_subdirs(t_type) for subdir in specific_subdirs: for p in provider_info_specific: - if 'cartesian_configs' in p['backends'][t_type]: - for c in p['backends'][t_type]['cartesian_configs']: + if "cartesian_configs" in p["backends"][t_type]: + for c in p["backends"][t_type]["cartesian_configs"]: cfg = os.path.join(subdir, "cfg", c) config_file_list.append(cfg) @@ -634,27 +646,27 @@ def is_file_tracked(fl): shutil.copyfile(src_file, dst_file) else: diff_cmd = "diff -Naur %s %s" % (dst_file, src_file) - diff_result = process.run( - diff_cmd, ignore_status=True, verbose=False) + diff_result = process.run(diff_cmd, ignore_status=True, verbose=False) if diff_result.exit_status != 0: - LOG.info("%s result:\n %s", - diff_result.command, - diff_result.stdout_text) - answer = genio.ask("Config file %s differs from %s." - "Overwrite?" % (dst_file, src_file), - auto=force_update or not interactive) + LOG.info( + "%s result:\n %s", diff_result.command, diff_result.stdout_text + ) + answer = genio.ask( + "Config file %s differs from %s." + "Overwrite?" % (dst_file, src_file), + auto=force_update or not interactive, + ) if answer == "y": - LOG.debug("Restoring config file %s from sample", - dst_file) + LOG.debug("Restoring config file %s from sample", dst_file) shutil.copyfile(src_file, dst_file) else: LOG.debug("Preserving existing %s file", dst_file) else: if force_update: - update_msg = 'Config file %s exists, equal to sample' + update_msg = "Config file %s exists, equal to sample" else: - update_msg = 'Config file %s exists, not touching' + update_msg = "Config file %s exists, not touching" LOG.debug(update_msg, dst_file) return step @@ -693,10 +705,10 @@ def haz_defcon(datadir, imagesdir, isosdir, tmpdir): tmp_type = utils_selinux.get_type_from_context(tmp_type) # hard-coded values b/c only four of them and wildly-used - if data_type == 'virt_var_lib_t': - if images_type == 'virt_image_t': - if isos_type == 'virt_content_t': - if tmp_type == 'user_tmp_t': + if data_type == "virt_var_lib_t": + if images_type == "virt_image_t": + if isos_type == "virt_content_t": + if tmp_type == "user_tmp_t": return True # No changes needed return False @@ -736,49 +748,48 @@ def set_defcon(datadir, imagesdir, isosdir, tmpdir): could_be_slow = False msg = "Defining default contexts, this could take a few seconds..." # Changing default contexts is *slow*, avoid it if not necessary - if existing_data is None or existing_data != 'virt_var_lib_t': + if existing_data is None or existing_data != "virt_var_lib_t": # semanage gives errors if don't treat /usr & /usr/local the same data_regex = utils_selinux.transmogrify_usr_local(datadir) LOG.info(msg) could_be_slow = True # This applies only to datadir symlink, not sub-directories! - utils_selinux.set_defcon('virt_var_lib_t', data_regex) + utils_selinux.set_defcon("virt_var_lib_t", data_regex) made_changes = True - if existing_images is None or existing_images != 'virt_image_t': + if existing_images is None or existing_images != "virt_image_t": # Applies to imagesdir and everything below images_regex = utils_selinux.transmogrify_usr_local(imagesdir) images_regex = utils_selinux.transmogrify_sub_dirs(images_regex) if not could_be_slow: LOG.info(msg) could_be_slow = True - utils_selinux.set_defcon('virt_image_t', images_regex) + utils_selinux.set_defcon("virt_image_t", images_regex) made_changes = True - if existing_isos is None or existing_isos != 'virt_content_t': + if existing_isos is None or existing_isos != "virt_content_t": # Applies to isosdir and everything below isos_regex = utils_selinux.transmogrify_usr_local(isosdir) isos_regex = utils_selinux.transmogrify_sub_dirs(isos_regex) if not could_be_slow: LOG.info(msg) could_be_slow = True - utils_selinux.set_defcon('virt_content_t', isos_regex) + utils_selinux.set_defcon("virt_content_t", isos_regex) made_changes = True - if existing_tmp is None or existing_tmp != 'user_tmp_t': + if existing_tmp is None or existing_tmp != "user_tmp_t": tmp_regex = utils_selinux.transmogrify_usr_local(tmpdir) tmp_regex = utils_selinux.transmogrify_sub_dirs(tmp_regex) if not could_be_slow: LOG.info(msg) could_be_slow = True - utils_selinux.set_defcon('user_tmp_t', tmp_regex) + utils_selinux.set_defcon("user_tmp_t", tmp_regex) made_changes = True return made_changes -def verify_selinux(datadir, imagesdir, isosdir, tmpdir, - interactive, selinux=False): +def verify_selinux(datadir, imagesdir, isosdir, tmpdir, interactive, selinux=False): """ Verify/Set/Warn about SELinux and default file contexts for testing. @@ -796,15 +807,17 @@ def verify_selinux(datadir, imagesdir, isosdir, tmpdir, needs_relabel = None try: # Raise SeCmdError if selinux not installed - if utils_selinux.get_status() == 'enforcing': + if utils_selinux.get_status() == "enforcing": # Check if default contexts are set if not haz_defcon(datadir, imagesdir, isosdir, tmpdir): if selinux: answer = "y" else: - answer = genio.ask("Setup all undefined default SE" - "Linux contexts for shared/data/?", - auto=not interactive) + answer = genio.ask( + "Setup all undefined default SE" + "Linux contexts for shared/data/?", + auto=not interactive, + ) else: answer = "n" if answer.lower() == "y": @@ -822,24 +835,37 @@ def verify_selinux(datadir, imagesdir, isosdir, tmpdir, needs_relabel = True # Disabled or Permissive mode is same result as not installed else: - LOG.info("SELinux in permissive or disabled, testing" - "in enforcing mode is highly encourraged.") + LOG.info( + "SELinux in permissive or disabled, testing" + "in enforcing mode is highly encourraged." + ) except utils_selinux.SemanageError: LOG.info("Could not set default SELinux contexts. Please") LOG.info("consider installing the semanage program then ") LOG.info("verifying and/or running running:") # Paths must be transmogrified (changed) into regular expressions - LOG.info("semanage fcontext --add -t virt_var_lib_t '%s'", - utils_selinux.transmogrify_usr_local(datadir)) - LOG.info("semanage fcontext --add -t virt_image_t '%s'", - utils_selinux.transmogrify_usr_local( - utils_selinux.transmogrify_sub_dirs(imagesdir))) - LOG.info("semanage fcontext --add -t virt_content_t '%s'", - utils_selinux.transmogrify_usr_local( - utils_selinux.transmogrify_sub_dirs(isosdir))) - LOG.info("semanage fcontext --add -t user_tmp_t '%s'", - utils_selinux.transmogrify_usr_local( - utils_selinux.transmogrify_sub_dirs(tmpdir))) + LOG.info( + "semanage fcontext --add -t virt_var_lib_t '%s'", + utils_selinux.transmogrify_usr_local(datadir), + ) + LOG.info( + "semanage fcontext --add -t virt_image_t '%s'", + utils_selinux.transmogrify_usr_local( + utils_selinux.transmogrify_sub_dirs(imagesdir) + ), + ) + LOG.info( + "semanage fcontext --add -t virt_content_t '%s'", + utils_selinux.transmogrify_usr_local( + utils_selinux.transmogrify_sub_dirs(isosdir) + ), + ) + LOG.info( + "semanage fcontext --add -t user_tmp_t '%s'", + utils_selinux.transmogrify_usr_local( + utils_selinux.transmogrify_sub_dirs(tmpdir) + ), + ) needs_relabel = None # Next run will catch if relabeling needed except utils_selinux.SelinuxError: # Catchall SELinux related LOG.info("SELinux not available, or error in command/setup.") @@ -849,15 +875,13 @@ def verify_selinux(datadir, imagesdir, isosdir, tmpdir, if selinux: answer = "y" else: - answer = genio.ask("Relabel from default contexts?", - auto=not interactive) - if answer.lower() == 'y': + answer = genio.ask("Relabel from default contexts?", auto=not interactive) + if answer.lower() == "y": changes = utils_selinux.apply_defcon(datadir, False) changes += utils_selinux.apply_defcon(imagesdir, True) changes += utils_selinux.apply_defcon(isosdir, True) changes += utils_selinux.apply_defcon(tmpdir, True) - LOG.info("Corrected contexts on %d files/dirs", - len(changes)) + LOG.info("Corrected contexts on %d files/dirs", len(changes)) def bootstrap(options, interactive=False): @@ -867,23 +891,22 @@ def bootstrap(options, interactive=False): :param options: Command line options. :param interactive: Whether to ask for confirmation. """ - if get_opt(options, 'yes_to_all'): + if get_opt(options, "yes_to_all"): interactive = False - vt_type = get_opt(options, 'vt.type') + vt_type = get_opt(options, "vt.type") LOG.info("Running bootstrap for %s", vt_type) step = 0 LOG.info("") step += 1 LOG.info("%d - Checking the mandatory programs and headers", step) - guest_os = get_opt(options, 'vt.guest_os') or defaults.DEFAULT_GUEST_OS + guest_os = get_opt(options, "vt.guest_os") or defaults.DEFAULT_GUEST_OS try: verify_mandatory_programs(vt_type, guest_os) except Exception as details: LOG.debug(details) - LOG.debug('Install the missing programs and/or headers and ' - 're-run boostrap') + LOG.debug("Install the missing programs and/or headers and " "re-run boostrap") sys.exit(1) LOG.info("") @@ -903,15 +926,17 @@ def bootstrap(options, interactive=False): action = "Downloading" else: action = "Updating" - if not get_opt(options, 'vt_no_downloads'): + if not get_opt(options, "vt_no_downloads"): LOG.info("") step += 1 LOG.info("%d - %s the test providers from remote repos", step, action) - asset.download_all_test_providers(get_opt(options, 'vt_update_providers')) + asset.download_all_test_providers(get_opt(options, "vt_update_providers")) else: if not_downloaded: - LOG.warn("The following test providers have not been downloaded: %s", - ", ".join(not_downloaded)) + LOG.warn( + "The following test providers have not been downloaded: %s", + ", ".join(not_downloaded), + ) LOG.info("") step += 1 @@ -925,66 +950,84 @@ def bootstrap(options, interactive=False): LOG.debug("Creating %s", sub_dir_path) os.makedirs(sub_dir_path) else: - LOG.debug("Dir %s exists, not creating", - sub_dir_path) + LOG.debug("Dir %s exists, not creating", sub_dir_path) base_backend_dir = data_dir.get_base_backend_dir() local_backend_dir = data_dir.get_local_backend_dir() LOG.info("") step += 1 - LOG.info("%d - Syncing backend dirs %s -> %s", step, base_backend_dir, - local_backend_dir) + LOG.info( + "%d - Syncing backend dirs %s -> %s", step, base_backend_dir, local_backend_dir + ) dir_util.copy_tree(base_backend_dir, local_backend_dir) sync_download_dir(interactive) test_dir = data_dir.get_backend_dir(vt_type) - if vt_type == 'libvirt': - step = create_config_files(test_dir, shared_dir, interactive, - vt_type, step, - force_update=get_opt(options, 'vt_update_config')) + if vt_type == "libvirt": + step = create_config_files( + test_dir, + shared_dir, + interactive, + vt_type, + step, + force_update=get_opt(options, "vt_update_config"), + ) create_subtests_cfg(vt_type) create_guest_os_cfg(vt_type) # Don't bother checking if changes can't be made if os.getuid() == 0: - verify_selinux(datadir, - os.path.join(datadir, 'images'), - os.path.join(datadir, 'isos'), - data_dir.get_tmp_dir(), - interactive, get_opt(options, 'vt_selinux_setup')) + verify_selinux( + datadir, + os.path.join(datadir, "images"), + os.path.join(datadir, "isos"), + data_dir.get_tmp_dir(), + interactive, + get_opt(options, "vt_selinux_setup"), + ) # lvsb test doesn't use any shared configs - elif vt_type == 'lvsb': + elif vt_type == "lvsb": create_subtests_cfg(vt_type) if os.getuid() == 0: # Don't bother checking if changes can't be made - verify_selinux(datadir, - os.path.join(datadir, 'images'), - os.path.join(datadir, 'isos'), - data_dir.get_tmp_dir(), - interactive, get_opt(options, 'vt_selinux_setup')) + verify_selinux( + datadir, + os.path.join(datadir, "images"), + os.path.join(datadir, "isos"), + data_dir.get_tmp_dir(), + interactive, + get_opt(options, "vt_selinux_setup"), + ) else: # Some other test - step = create_config_files(test_dir, shared_dir, interactive, - vt_type, step, - force_update=get_opt(options, 'vt_update_config')) + step = create_config_files( + test_dir, + shared_dir, + interactive, + vt_type, + step, + force_update=get_opt(options, "vt_update_config"), + ) create_subtests_cfg(vt_type) create_guest_os_cfg(vt_type) create_host_os_cfg(options) - if not (get_opt(options, 'vt_no_downloads') or - get_opt(options, 'vt_skip_verify_download_assets')): + if not ( + get_opt(options, "vt_no_downloads") + or get_opt(options, "vt_skip_verify_download_assets") + ): LOG.info("") step += 1 - LOG.info("%s - Verifying (and possibly downloading) guest image", - step) + LOG.info("%s - Verifying (and possibly downloading) guest image", step) try: for os_info in get_guest_os_info_list(vt_type, guest_os): - os_asset = os_info['asset'] + os_asset = os_info["asset"] try: - asset.download_asset(os_asset, interactive=interactive, - restore_image=True) + asset.download_asset( + os_asset, interactive=interactive, restore_image=True + ) except AssertionError: - pass # Not all files are managed via asset + pass # Not all files are managed via asset except ValueError as details: LOG.error(details) @@ -999,12 +1042,12 @@ def bootstrap(options, interactive=False): if check_modules: LOG.info("") step += 1 - LOG.info("%d - Checking for modules %s", step, - ", ".join(check_modules)) + LOG.info("%d - Checking for modules %s", step, ", ".join(check_modules)) for module in check_modules: if not linux_modules.module_is_loaded(module): - LOG.warning("Module %s is not loaded. You might want to " - "load it", module) + LOG.warning( + "Module %s is not loaded. You might want to " "load it", module + ) else: LOG.debug("Module %s loaded", module) diff --git a/virttest/build_helper.py b/virttest/build_helper.py index 6b0fa25a0e..000cc22cdd 100644 --- a/virttest/build_helper.py +++ b/virttest/build_helper.py @@ -13,7 +13,7 @@ from virttest import data_dir -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) def _force_copy(src, dest): @@ -66,91 +66,100 @@ def _parse_params(self): methods. That means it's not strictly necessary to call parent's __init__(). """ - config_prefix = 'git_repo_%s' % self.name - LOG.debug('Parsing parameters for git repo %s, configuration ' - 'prefix is %s' % (self.name, config_prefix)) + config_prefix = "git_repo_%s" % self.name + LOG.debug( + "Parsing parameters for git repo %s, configuration " + "prefix is %s" % (self.name, config_prefix) + ) - self.base_uri = self.params.get('%s_base_uri' % config_prefix) + self.base_uri = self.params.get("%s_base_uri" % config_prefix) if self.base_uri is None: - LOG.debug('Git repo %s base uri is not set' % self.name) + LOG.debug("Git repo %s base uri is not set" % self.name) else: - LOG.debug('Git repo %s base uri: %s' % (self.name, self.base_uri)) + LOG.debug("Git repo %s base uri: %s" % (self.name, self.base_uri)) - self.uri = self.params.get('%s_uri' % config_prefix) - LOG.debug('Git repo %s uri: %s' % (self.name, self.uri)) + self.uri = self.params.get("%s_uri" % config_prefix) + LOG.debug("Git repo %s uri: %s" % (self.name, self.uri)) - self.branch = self.params.get('%s_branch' % config_prefix, 'master') - LOG.debug('Git repo %s branch: %s' % (self.name, self.branch)) + self.branch = self.params.get("%s_branch" % config_prefix, "master") + LOG.debug("Git repo %s branch: %s" % (self.name, self.branch)) - self.lbranch = self.params.get('%s_lbranch' % config_prefix) + self.lbranch = self.params.get("%s_lbranch" % config_prefix) if self.lbranch is None: self.lbranch = self.branch - LOG.debug('Git repo %s lbranch: %s' % (self.name, self.lbranch)) + LOG.debug("Git repo %s lbranch: %s" % (self.name, self.lbranch)) - self.commit = self.params.get('%s_commit' % config_prefix) + self.commit = self.params.get("%s_commit" % config_prefix) if self.commit is None: - LOG.debug('Git repo %s commit is not set' % self.name) + LOG.debug("Git repo %s commit is not set" % self.name) else: - LOG.debug('Git repo %s commit: %s' % (self.name, self.commit)) + LOG.debug("Git repo %s commit: %s" % (self.name, self.commit)) - self.tag = self.params.get('%s_tag' % config_prefix) + self.tag = self.params.get("%s_tag" % config_prefix) if self.tag is None: - LOG.debug('Git repo %s tag is not set' % self.name) + LOG.debug("Git repo %s tag is not set" % self.name) else: - LOG.debug('Git repo %s tag: %s' % (self.name, self.tag)) + LOG.debug("Git repo %s tag: %s" % (self.name, self.tag)) self.key_file = None - tag_signed = self.params.get('%s_tag_signed' % config_prefix) + tag_signed = self.params.get("%s_tag_signed" % config_prefix) if tag_signed is None: - LOG.warning('Git repo %s tag is not signed' % self.name) - LOG.warning('This means we will not verify if the key was ' - 'made by whomever claims to have made it ' - '(dangerous)') + LOG.warning("Git repo %s tag is not signed" % self.name) + LOG.warning( + "This means we will not verify if the key was " + "made by whomever claims to have made it " + "(dangerous)" + ) else: - self.key_file = os.path.join(data_dir.get_data_dir(), 'gpg', - tag_signed) + self.key_file = os.path.join(data_dir.get_data_dir(), "gpg", tag_signed) if os.path.isfile(self.key_file): - LOG.debug('Git repo %s tag %s will be verified with public ' - 'key file %s', self.name, self.tag, self.key_file) + LOG.debug( + "Git repo %s tag %s will be verified with public " "key file %s", + self.name, + self.tag, + self.key_file, + ) else: - raise exceptions.TestError('GPG public key file %s not found, ' - 'will not proceed with testing' % - self.key_file) + raise exceptions.TestError( + "GPG public key file %s not found, " + "will not proceed with testing" % self.key_file + ) - self.cmd = path.find_command('git') + self.cmd = path.find_command("git") - self.recursive = self.params.get('%s_recursive', 'yes') + self.recursive = self.params.get("%s_recursive", "yes") def execute(self): super(GitRepoParamHelper, self).execute() cwd = os.path.curdir os.chdir(self.destination_dir) - process.system('git remote add origin %s' % - self.uri, ignore_status=True) - if self.recursive == 'yes': - process.system('git submodule init') - process.system('git submodule update') + process.system("git remote add origin %s" % self.uri, ignore_status=True) + if self.recursive == "yes": + process.system("git submodule init") + process.system("git submodule update") if self.tag: - process.system('git checkout %s' % self.tag) + process.system("git checkout %s" % self.tag) if self.key_file is not None: try: - gnupg_home = os.path.join(data_dir.get_tmp_dir(), - 'gnupg') + gnupg_home = os.path.join(data_dir.get_tmp_dir(), "gnupg") if not os.path.isdir(gnupg_home): os.makedirs(gnupg_home) - os.environ['GNUPGHOME'] = gnupg_home - process.system('gpg --import %s' % self.key_file) - LOG.debug('Verifying if tag is actually signed with ' - 'GPG key ID %s' % self.key_file) - process.system('git tag -v %s' % self.tag) + os.environ["GNUPGHOME"] = gnupg_home + process.system("gpg --import %s" % self.key_file) + LOG.debug( + "Verifying if tag is actually signed with " + "GPG key ID %s" % self.key_file + ) + process.system("git tag -v %s" % self.tag) except process.CmdError: - raise exceptions.TestError("GPG signature check for git repo " - "%s failed" % self.name) + raise exceptions.TestError( + "GPG signature check for git repo " "%s failed" % self.name + ) # Log the top commit message, good for quick reference - process.system('git log -1') + process.system("git log -1") os.chdir(cwd) @@ -210,12 +219,14 @@ def _parse_params(self): """ Parses the params items for entries related to source dir """ - config_prefix = 'local_src_%s' % self.name - LOG.debug('Parsing parameters for local source %s, configuration ' - 'prefix is %s' % (self.name, config_prefix)) + config_prefix = "local_src_%s" % self.name + LOG.debug( + "Parsing parameters for local source %s, configuration " + "prefix is %s" % (self.name, config_prefix) + ) - self.path = self.params.get('%s_path' % config_prefix) - LOG.debug('Local source directory %s path: %s' % (self.name, self.path)) + self.path = self.params.get("%s_path" % config_prefix) + LOG.debug("Local source directory %s path: %s" % (self.name, self.path)) self.source = self.path self.destination = self.destination_dir @@ -240,10 +251,8 @@ def extract(self): if os.path.isfile(self.source) and tarfile.is_tarfile(self.source): name = os.path.basename(self.destination) - temp_dir = os.path.join(os.path.dirname(self.destination), - '%s.tmp' % name) - LOG.debug('Temporary directory for extracting tarball is %s' % - temp_dir) + temp_dir = os.path.join(os.path.dirname(self.destination), "%s.tmp" % name) + LOG.debug("Temporary directory for extracting tarball is %s" % temp_dir) if not os.path.isdir(temp_dir): os.makedirs(temp_dir) @@ -257,8 +266,7 @@ def extract(self): # tarball_info = tarball.members[0] if tarball_info.isdir(): - content_path = os.path.join(temp_dir, - tarball_info.name) + content_path = os.path.join(temp_dir, tarball_info.name) else: content_path = temp_dir @@ -309,12 +317,14 @@ def _parse_params(self): """ Parses the params items for entries related to this local tar helper """ - config_prefix = 'local_tar_%s' % self.name - LOG.debug('Parsing parameters for local tar %s, configuration ' - 'prefix is %s' % (self.name, config_prefix)) + config_prefix = "local_tar_%s" % self.name + LOG.debug( + "Parsing parameters for local tar %s, configuration " + "prefix is %s" % (self.name, config_prefix) + ) - self.path = self.params.get('%s_path' % config_prefix) - LOG.debug('Local source tar %s path: %s' % (self.name, self.path)) + self.path = self.params.get("%s_path" % config_prefix) + LOG.debug("Local source tar %s path: %s" % (self.name, self.path)) self.source = self.path self.destination = self.destination_dir @@ -376,12 +386,14 @@ def _parse_params(self): """ Parses the params items for entries related to this remote tar helper """ - config_prefix = 'remote_tar_%s' % self.name - LOG.debug('Parsing parameters for remote tar %s, configuration ' - 'prefix is %s' % (self.name, config_prefix)) + config_prefix = "remote_tar_%s" % self.name + LOG.debug( + "Parsing parameters for remote tar %s, configuration " + "prefix is %s" % (self.name, config_prefix) + ) - self.uri = self.params.get('%s_uri' % config_prefix) - LOG.debug('Remote source tar %s uri: %s' % (self.name, self.uri)) + self.uri = self.params.get("%s_uri" % config_prefix) + LOG.debug("Remote source tar %s uri: %s" % (self.name, self.uri)) self.source = self.uri self.destination = self.destination_dir @@ -404,8 +416,9 @@ def download(self): Copies patch files from remote locations to the source directory """ for patch in self.patches: - download.get_file(patch, os.path.join(self.source_dir, - os.path.basename(patch))) + download.get_file( + patch, os.path.join(self.source_dir, os.path.basename(patch)) + ) def patch(self): """ @@ -413,8 +426,7 @@ def patch(self): """ os.chdir(self.source_dir) for patch in self.patches: - process.system('patch -p1 < %s' % os.path.basename(patch), - shell=True) + process.system("patch -p1 < %s" % os.path.basename(patch), shell=True) def execute(self): """ @@ -463,16 +475,14 @@ def _parse_params(self): methods. That means it's not strictly necessary to call parent's __init__(). """ - LOG.debug('Parsing patch parameters for prefix %s' % self.prefix) - patches_param_key = '%s_patches' % self.prefix + LOG.debug("Parsing patch parameters for prefix %s" % self.prefix) + patches_param_key = "%s_patches" % self.prefix - self.patches_str = self.params.get(patches_param_key, '[]') - LOG.debug('Patches config for prefix %s: %s' % (self.prefix, - self.patches_str)) + self.patches_str = self.params.get(patches_param_key, "[]") + LOG.debug("Patches config for prefix %s: %s" % (self.prefix, self.patches_str)) self.patches = eval(self.patches_str) - LOG.debug('Patches for prefix %s: %s' % (self.prefix, - ", ".join(self.patches))) + LOG.debug("Patches for prefix %s: %s" % (self.prefix, ", ".join(self.patches))) class GnuSourceBuildInvalidSource(Exception): @@ -480,6 +490,7 @@ class GnuSourceBuildInvalidSource(Exception): """ Exception raised when build source dir/file is not valid """ + pass @@ -491,6 +502,7 @@ class SourceBuildFailed(Exception): This serves as feedback for code using :class:`virttest.build_helper.BuildHelper`. """ + pass @@ -502,6 +514,7 @@ class SourceBuildParallelFailed(Exception): This serves as feedback for code using :class:`virttest.build_helper.BuildHelper`. """ + pass @@ -514,8 +527,7 @@ class GnuSourceBuildHelper(object): autotools steps: ./configure, make, make install """ - def __init__(self, source, build_dir, prefix, - configure_options=[]): + def __init__(self, source, build_dir, prefix, configure_options=[]): """ :type source: string :param source: source directory or tarball @@ -546,37 +558,36 @@ def include_pkg_config_path(self): :return: None """ - env_var = 'PKG_CONFIG_PATH' + env_var = "PKG_CONFIG_PATH" - include_paths = [os.path.join(self.prefix, 'share', 'pkgconfig'), - os.path.join(self.prefix, 'lib', 'pkgconfig')] + include_paths = [ + os.path.join(self.prefix, "share", "pkgconfig"), + os.path.join(self.prefix, "lib", "pkgconfig"), + ] if env_var in os.environ: - paths = os.environ[env_var].split(':') + paths = os.environ[env_var].split(":") for include_path in include_paths: if include_path not in paths: paths.append(include_path) - os.environ[env_var] = ':'.join(paths) + os.environ[env_var] = ":".join(paths) else: - os.environ[env_var] = ':'.join(include_paths) + os.environ[env_var] = ":".join(include_paths) - LOG.debug('PKG_CONFIG_PATH is: %s' % os.environ['PKG_CONFIG_PATH']) + LOG.debug("PKG_CONFIG_PATH is: %s" % os.environ["PKG_CONFIG_PATH"]) def get_configure_path(self): """ Checks if 'configure' exists, if not, return 'autogen.sh' as a fallback """ - configure_path = os.path.abspath(os.path.join(self.source, - "configure")) - autogen_path = os.path.abspath(os.path.join(self.source, - "autogen.sh")) + configure_path = os.path.abspath(os.path.join(self.source, "configure")) + autogen_path = os.path.abspath(os.path.join(self.source, "autogen.sh")) if os.path.exists(configure_path): return configure_path elif os.path.exists(autogen_path): return autogen_path else: - raise GnuSourceBuildInvalidSource( - 'configure script does not exist') + raise GnuSourceBuildInvalidSource("configure script does not exist") def get_available_configure_options(self): """ @@ -586,8 +597,9 @@ def get_available_configure_options(self): :return: list of options accepted by configure script """ - help_raw = process.run('%s --help' % self.get_configure_path(), - ignore_status=True).stdout_text + help_raw = process.run( + "%s --help" % self.get_configure_path(), ignore_status=True + ).stdout_text help_output = help_raw.split("\n") option_list = [] for line in help_output: @@ -608,8 +620,7 @@ def enable_debug_symbols(self): enable_debug_option = "--disable-strip" if enable_debug_option in self.get_available_configure_options(): self.configure_options.append(enable_debug_option) - LOG.debug('Enabling debug symbols with option: %s' % - enable_debug_option) + LOG.debug("Enabling debug symbols with option: %s" % enable_debug_option) def get_configure_command(self): """ @@ -620,15 +631,14 @@ def get_configure_command(self): prefix_option = "--prefix=%s" % self.prefix options = self.configure_options options.append(prefix_option) - return "%s %s" % (self.get_configure_path(), - " ".join(options)) + return "%s %s" % (self.get_configure_path(), " ".join(options)) def configure(self): """ Runs the "configure" script passing appropriate command line options """ configure_command = self.get_configure_command() - LOG.info('Running configure on build dir') + LOG.info("Running configure on build dir") os.chdir(self.build_dir) process.system(configure_command) @@ -729,24 +739,21 @@ def _parse_params(self): """ Parses the params items for entries related to guest kernel """ - configure_opt_key = '%s_config' % self.prefix - self.config = self.params.get(configure_opt_key, '') + configure_opt_key = "%s_config" % self.prefix + self.config = self.params.get(configure_opt_key, "") - build_image_key = '%s_build_image' % self.prefix - self.build_image = self.params.get(build_image_key, - 'arch/x86/boot/bzImage') + build_image_key = "%s_build_image" % self.prefix + self.build_image = self.params.get(build_image_key, "arch/x86/boot/bzImage") - build_target_key = '%s_build_target' % self.prefix - self.build_target = self.params.get(build_target_key, 'bzImage') + build_target_key = "%s_build_target" % self.prefix + self.build_target = self.params.get(build_target_key, "bzImage") - kernel_path_key = '%s_kernel_path' % self.prefix - image_dir = os.path.join(data_dir.get_data_dir(), 'images') - default_kernel_path = os.path.join(image_dir, - self.build_target) - self.kernel_path = self.params.get(kernel_path_key, - default_kernel_path) + kernel_path_key = "%s_kernel_path" % self.prefix + image_dir = os.path.join(data_dir.get_data_dir(), "images") + default_kernel_path = os.path.join(image_dir, self.build_target) + self.kernel_path = self.params.get(kernel_path_key, default_kernel_path) - LOG.info('Parsing Linux kernel build parameters for %s', self.prefix) + LOG.info("Parsing Linux kernel build parameters for %s", self.prefix) def make_guest_kernel(self): """ @@ -755,14 +762,13 @@ def make_guest_kernel(self): os.chdir(self.source) LOG.info("Building guest kernel") LOG.debug("Kernel config is %s" % self.config) - download.get_file(self.config, '.config') + download.get_file(self.config, ".config") # FIXME currently no support for builddir # run old config process.system('yes "" | make oldconfig > /dev/null', shell=True) parallel_make_jobs = multiprocessing.cpu_count() - make_command = "make -j %s %s" % ( - parallel_make_jobs, self.build_target) + make_command = "make -j %s %s" % (parallel_make_jobs, self.build_target) LOG.info("Running parallel make on src dir") process.system(make_command) @@ -839,12 +845,11 @@ def _parse_params(self): methods. That means it's not strictly necessary to call parent's __init__(). """ - LOG.debug('Parsing gnu_autotools build parameters for %s' % self.name) + LOG.debug("Parsing gnu_autotools build parameters for %s" % self.name) - configure_opt_key = '%s_configure_options' % self.name - configure_options = self.params.get(configure_opt_key, '').split() - LOG.debug('Configure options for %s: %s' % (self.name, - configure_options)) + configure_opt_key = "%s_configure_options" % self.name + configure_options = self.params.get(configure_opt_key, "").split() + LOG.debug("Configure options for %s: %s" % (self.name, configure_options)) self.source = self.destination_dir self.build_dir = self.destination_dir diff --git a/virttest/cartesian_config.py b/virttest/cartesian_config.py index f403d09bb3..345d5e5d28 100755 --- a/virttest/cartesian_config.py +++ b/virttest/cartesian_config.py @@ -139,16 +139,17 @@ import re import sys -_reserved_keys = set(("name", "shortname", "dep", "_short_name_map_file", "_name_map_file")) +_reserved_keys = set( + ("name", "shortname", "dep", "_short_name_map_file", "_name_map_file") +) options = None num_failed_cases = 5 -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class ParserError(Exception): - def __init__(self, msg, line=None, filename=None, linenum=None): Exception.__init__(self) self.msg = msg @@ -158,8 +159,7 @@ def __init__(self, msg, line=None, filename=None, linenum=None): def __str__(self): if self.line: - return "%s: %r (%s:%s)" % (self.msg, self.line, - self.filename, self.linenum) + return "%s: %r (%s:%s)" % (self.msg, self.line, self.filename, self.linenum) else: return "%s (%s:%s)" % (self.msg, self.filename, self.linenum) @@ -169,7 +169,6 @@ class LexerError(ParserError): class MissingIncludeError(Exception): - def __init__(self, line, filename, linenum): Exception.__init__(self) self.line = line @@ -177,15 +176,20 @@ def __init__(self, line, filename, linenum): self.linenum = linenum def __str__(self): - return ("%r (%s:%s): file does not exist or it's not a regular " - "file" % (self.line, self.filename, self.linenum)) + return "%r (%s:%s): file does not exist or it's not a regular " "file" % ( + self.line, + self.filename, + self.linenum, + ) if sys.version_info[0] == 2 and sys.version_info[1] < 6: + def enum(iterator, start_pos=0): for i in iterator: yield start_pos, i start_pos += 1 + else: enum = enumerate @@ -199,20 +203,20 @@ def _match_adjacent(block, ctx, ctx_set): if block[0] not in ctx_set: return 0 if len(block) == 1: - return 1 # First match and length is 1. + return 1 # First match and length is 1. if block[1] not in ctx_set: - return int(ctx[-1] == block[0]) # Check match with last from ctx. + return int(ctx[-1] == block[0]) # Check match with last from ctx. k = 0 i = ctx.index(block[0]) - while i < len(ctx): # Try to match all of blocks. + while i < len(ctx): # Try to match all of blocks. if k > 0 and ctx[i] != block[k]: # Block not match i -= k - 1 - k = 0 # Start from first block in next ctx. + k = 0 # Start from first block in next ctx. if ctx[i] == block[k]: k += 1 - if k >= len(block): # match all of blocks + if k >= len(block): # match all of blocks break - if block[k] not in ctx_set: # block in not in whole ctx. + if block[k] not in ctx_set: # block in not in whole ctx. break i += 1 return k @@ -220,7 +224,7 @@ def _match_adjacent(block, ctx, ctx_set): def _might_match_adjacent(block, ctx, ctx_set, descendant_labels): matched = _match_adjacent(block, ctx, ctx_set) - for elem in block[matched:]: # Try to find rest of blocks in subtree + for elem in block[matched:]: # Try to find rest of blocks in subtree if elem not in descendant_labels: # print "Can't match %s, ctx %s" % (block, ctx) return False @@ -237,20 +241,19 @@ def __init__(self, lfilter): def match(self, ctx, ctx_set): for word in self.filter: # Go through , - for block in word: # Go through .. + for block in word: # Go through .. if _match_adjacent(block, ctx, ctx_set) != len(block): break else: # print "Filter pass: %s ctx: %s" % (self.filter, ctx) - return True # All match + return True # All match return False def might_match(self, ctx, ctx_set, descendant_labels): # There is some possibility to match in children blocks. for word in self.filter: for block in word: - if not _might_match_adjacent(block, ctx, ctx_set, - descendant_labels): + if not _might_match_adjacent(block, ctx, ctx_set, descendant_labels): break else: return True @@ -259,7 +262,7 @@ def might_match(self, ctx, ctx_set, descendant_labels): class NoOnlyFilter(Filter): - __slots__ = ("line") + __slots__ = "line" def __init__(self, lfilter, line): super(NoOnlyFilter, self).__init__(lfilter) @@ -284,12 +287,12 @@ def requires_action(self, ctx, ctx_set, descendant_labels): # Impossible to match in this tree. return not self.might_match(ctx, ctx_set, descendant_labels) - def might_pass(self, failed_ctx, failed_ctx_set, ctx, ctx_set, - descendant_labels): + def might_pass(self, failed_ctx, failed_ctx_set, ctx, ctx_set, descendant_labels): for word in self.filter: for block in word: - if (_match_adjacent(block, ctx, ctx_set) > - _match_adjacent(block, failed_ctx, failed_ctx_set)): + if _match_adjacent(block, ctx, ctx_set) > _match_adjacent( + block, failed_ctx, failed_ctx_set + ): return self.might_match(ctx, ctx_set, descendant_labels) return False @@ -301,7 +304,6 @@ def __repr__(self): class NoFilter(NoOnlyFilter): - def is_irrelevant(self, ctx, ctx_set, descendant_labels): return not self.might_match(ctx, ctx_set, descendant_labels) @@ -310,12 +312,12 @@ def requires_action(self, ctx, ctx_set, descendant_labels): return self.match(ctx, ctx_set) # pylint: disable=W0613 - def might_pass(self, failed_ctx, failed_ctx_set, ctx, ctx_set, - descendant_labels): + def might_pass(self, failed_ctx, failed_ctx_set, ctx, ctx_set, descendant_labels): for word in self.filter: for block in word: - if (_match_adjacent(block, ctx, ctx_set) < - _match_adjacent(block, failed_ctx, failed_ctx_set)): + if _match_adjacent(block, ctx, ctx_set) < _match_adjacent( + block, failed_ctx, failed_ctx_set + ): return not self.match(ctx, ctx_set) return False @@ -327,7 +329,6 @@ def __repr__(self): class JoinFilter(NoOnlyFilter): - def __str__(self): return "Join %s" % (self.filter) @@ -395,9 +396,11 @@ def __init__(self, s): line = line.rstrip().expandtabs() stripped_line = line.lstrip() indent = len(line) - len(stripped_line) - if (not stripped_line or - stripped_line.startswith("#") or - stripped_line.startswith("//")): + if ( + not stripped_line + or stripped_line.startswith("#") + or stripped_line.startswith("//") + ): continue self._lines.append((stripped_line, indent, linenum + 1)) @@ -511,9 +514,19 @@ def hash_variant(self): class Node(object): - __slots__ = ["var_name", "name", "filename", "dep", "content", "children", - "labels", "append_to_shortname", "failed_cases", "default", - "q_dict"] + __slots__ = [ + "var_name", + "name", + "filename", + "dep", + "content", + "children", + "labels", + "append_to_shortname", + "failed_cases", + "default", + "q_dict", + ] def __init__(self): self.var_name = [] @@ -585,7 +598,7 @@ def _drop_suffixes(d): else: # merge suffixes, preserve reverse order of suffixes new_key = key[:1] + key[1:][::-1] - new_key = ''.join((map(str, new_key))) + new_key = "".join((map(str, new_key))) d_flat[new_key] = d_flat.pop(key) return d_flat @@ -611,12 +624,12 @@ def _substitution(value, d): match = match_substitute.search(value, start) while match: val = d_flat[match.group(1)] - st += value[start:match.start()] + str(val) + st += value[start : match.start()] + str(val) start = match.end() match = match_substitute.search(value, start) except KeyError: pass - st += value[start:len(value)] + st += value[start : len(value)] return st else: return value @@ -703,8 +716,10 @@ def checkCharAlpha(self, chars): """ for t in self: if not (t in chars or t.isalpha()): - raise ParserError("Char %s is not alpha or one of special" - "chars [%s] in %s" % (t, chars, self)) + raise ParserError( + "Char %s is not alpha or one of special" + "chars [%s] in %s" % (t, chars, self) + ) return self def checkCharAlphaNum(self, chars): @@ -713,8 +728,10 @@ def checkCharAlphaNum(self, chars): """ for t in self: if not (t in chars or t.isalnum()): - raise ParserError("Char %s is not alphanum or one of special" - "chars [%s] in %s" % (t, chars, self)) + raise ParserError( + "Char %s is not alphanum or one of special" + "chars [%s] in %s" % (t, chars, self) + ) return self def checkCharNumeric(self, chars): @@ -723,8 +740,10 @@ def checkCharNumeric(self, chars): """ for t in self: if not (t in chars or t.isdigit()): - raise ParserError("Char %s is not digit or one of special" - "chars [%s] in %s" % (t, chars, self)) + raise ParserError( + "Char %s is not digit or one of special" + "chars [%s] in %s" % (t, chars, self) + ) return self @@ -959,7 +978,7 @@ class LApplyPreDict(LOperators): identifier = "apply_pre_dict" def set_operands(self, name, value): - self.name = name # pylint: disable=W0201,E0237 + self.name = name # pylint: disable=W0201,E0237 self.value = value # pylint: disable=W0201,E0237 return self @@ -1030,26 +1049,29 @@ def apply_to_dict(self, d): spec_oper = "+ m.end()): chars = "" - yield LIdentifier(line[:m.start()].rstrip()) + yield LIdentifier(line[: m.start()].rstrip()) yield tokens_oper[m.group()[:-1]]() - yield LString(line[m.end():].lstrip()) + yield LString(line[m.end() :].lstrip()) else: li = enum(line[pos:], pos) for pos, char in li: - if char.isalnum() or char in spec_iden: # alfanum+_- + if char.isalnum() or char in spec_iden: # alfanum+_- chars += char - elif char in spec_oper: # <+?=~ + elif char in spec_oper: # <+?=~ if chars: yield LIdentifier(chars) oper = "" @@ -1162,7 +1183,7 @@ def match(self, line, pos): if chars: yield LIdentifier(chars) chars = "" - if char.isspace(): # Whitespace + if char.isspace(): # Whitespace for pos, char in li: if not char.isspace(): if not self.ignore_white: @@ -1174,17 +1195,19 @@ def match(self, line, pos): if oper in tokens_oper: yield tokens_oper[oper]() else: - raise LexerError("Unexpected character %s on" - " pos %s" % (char, pos), - self.line, self.filename, - self.linenum) + raise LexerError( + "Unexpected character %s on" " pos %s" % (char, pos), + self.line, + self.filename, + self.linenum, + ) oper = "" elif char in tokens_map: token = tokens_map[char]() - elif char == "\"": + elif char == '"': chars = "" pos, char = next(li) - while char != "\"": + while char != '"': chars += char pos, char = next(li) yield LString(chars) @@ -1193,17 +1216,21 @@ def match(self, line, pos): elif char in spec_oper: oper += char else: - raise LexerError("Unexpected character %s on" - " pos %s. Special chars are allowed" - " only in variable assignation" - " statement" % (char, pos), line, - self.filename, self.linenum) + raise LexerError( + "Unexpected character %s on" + " pos %s. Special chars are allowed" + " only in variable assignation" + " statement" % (char, pos), + line, + self.filename, + self.linenum, + ) if token is not None: yield token token = None if self.rest_as_string: self.rest_as_string = False - yield LString(line[pos + 1:].lstrip()) + yield LString(line[pos + 1 :].lstrip()) break if chars: yield LIdentifier(chars) @@ -1214,8 +1241,7 @@ def get_lexer(self): cr = self.reader indent = 0 while True: - (self.line, indent, - self.linenum) = cr.get_next_line(self.prev_indent) + (self.line, indent, self.linenum) = cr.get_next_line(self.prev_indent) if not self.line: yield LEndBlock(indent) @@ -1262,8 +1288,12 @@ def get_until_check(self, lType, end_tokens=None): if type(token) in lType: tokens.append(token) else: - raise ParserError("Expected %s got %s" % (lType, type(token)), - self.line, self.filename, self.linenum) + raise ParserError( + "Expected %s got %s" % (lType, type(token)), + self.line, + self.filename, + self.linenum, + ) return tokens def get_until_no_white(self, end_tokens=None): @@ -1300,10 +1330,13 @@ def get_next_check(self, lType): if type(token) in lType: return type(token), token else: - raise ParserError("Expected %s got ['%s']=[%s]" % - ([x.identifier for x in lType], - token.identifier, token), - self.line, self.filename, self.linenum) + raise ParserError( + "Expected %s got ['%s']=[%s]" + % ([x.identifier for x in lType], token.identifier, token), + self.line, + self.filename, + self.linenum, + ) def get_next_check_nw(self, lType): token = next(self.generator) @@ -1312,19 +1345,25 @@ def get_next_check_nw(self, lType): if type(token) in lType: return type(token), token else: - raise ParserError("Expected %s got ['%s']" % - ([x.identifier for x in lType], - token.identifier), - self.line, self.filename, self.linenum) + raise ParserError( + "Expected %s got ['%s']" + % ([x.identifier for x in lType], token.identifier), + self.line, + self.filename, + self.linenum, + ) def check_token(self, token, lType): if type(token) in lType: return type(token), token else: - raise ParserError("Expected %s got ['%s']" % - ([x.identifier for x in lType], - token.identifier), - self.line, self.filename, self.linenum) + raise ParserError( + "Expected %s got ['%s']" + % ([x.identifier for x in lType], token.identifier), + self.line, + self.filename, + self.linenum, + ) def next_nw(gener): @@ -1354,23 +1393,23 @@ def parse_filter(lexer, tokens): """ or_filters = [] tokens = iter(tokens + [LEndL()]) - typet, token = lexer.check_token(next(tokens), [LIdentifier, LLRBracket, - LEndL, LWhite]) + typet, token = lexer.check_token( + next(tokens), [LIdentifier, LLRBracket, LEndL, LWhite] + ) and_filter = [] con_filter = [] dots = 1 while typet not in [LEndL]: - if typet in [LIdentifier, LLRBracket]: # join identifier - if typet == LLRBracket: # (xxx=ttt) - _, ident = lexer.check_token(next_nw(tokens), - [LIdentifier]) # (iden - typet, _ = lexer.check_token(next_nw(tokens), - [LSet, LRRBracket]) # = + if typet in [LIdentifier, LLRBracket]: # join identifier + if typet == LLRBracket: # (xxx=ttt) + _, ident = lexer.check_token(next_nw(tokens), [LIdentifier]) # (iden + typet, _ = lexer.check_token(next_nw(tokens), [LSet, LRRBracket]) # = if typet == LRRBracket: # (xxx) token = Label(str(ident)) - elif typet == LSet: # (xxx = yyyy) - _, value = lexer.check_token(next_nw(tokens), - [LIdentifier, LString]) + elif typet == LSet: # (xxx = yyyy) + _, value = lexer.check_token( + next_nw(tokens), [LIdentifier, LString] + ) lexer.check_token(next_nw(tokens), [LRRBracket]) token = Label(str(ident), str(value)) else: @@ -1381,18 +1420,24 @@ def parse_filter(lexer, tokens): and_filter.append(con_filter) con_filter = [token] elif dots == 0 or dots > 2: - raise ParserError("Syntax Error expected \".\" between" - " Identifier.", lexer.line, lexer.filename, - lexer.linenum) + raise ParserError( + 'Syntax Error expected "." between' " Identifier.", + lexer.line, + lexer.filename, + lexer.linenum, + ) dots = 0 - elif typet == LDot: # xxx.xxxx or xxx..xxxx + elif typet == LDot: # xxx.xxxx or xxx..xxxx dots += 1 elif typet in [LComa, LWhite]: if dots > 0: - raise ParserError("Syntax Error expected identifier between" - " \".\" and \",\".", lexer.line, - lexer.filename, lexer.linenum) + raise ParserError( + "Syntax Error expected identifier between" ' "." and ",".', + lexer.line, + lexer.filename, + lexer.linenum, + ) if and_filter: if con_filter: and_filter.append(con_filter) @@ -1405,20 +1450,23 @@ def parse_filter(lexer, tokens): elif typet == LIdentifier: or_filters.append([[Label(token)]]) else: - raise ParserError("Syntax Error expected \",\" between" - " Identifier.", lexer.line, lexer.filename, - lexer.linenum) + raise ParserError( + 'Syntax Error expected "," between' " Identifier.", + lexer.line, + lexer.filename, + lexer.linenum, + ) dots = 1 token = next(tokens) while isinstance(token, LWhite): token = next(tokens) - typet, token = lexer.check_token(token, [LIdentifier, - LComa, LDot, - LLRBracket, LEndL]) + typet, token = lexer.check_token( + token, [LIdentifier, LComa, LDot, LLRBracket, LEndL] + ) continue - typet, token = lexer.check_token(next(tokens), [LIdentifier, LComa, - LDot, LLRBracket, - LEndL, LWhite]) + typet, token = lexer.check_token( + next(tokens), [LIdentifier, LComa, LDot, LLRBracket, LEndL, LWhite] + ) if and_filter: if con_filter: and_filter.append(con_filter) @@ -1434,8 +1482,7 @@ def parse_filter(lexer, tokens): class Parser(object): # pylint: disable=W0102 - def __init__(self, filename=None, defaults=False, expand_defaults=[], - debug=False): + def __init__(self, filename=None, defaults=False, expand_defaults=[], debug=False): self.node = Node() self.debug = debug self.defaults = defaults @@ -1522,15 +1569,31 @@ def assign(self, key, value): def _parse(self, lexer, node=None, prev_indent=-1): if not node: node = self.node - block_allowed = [LVariants, LIdentifier, LOnly, - LNo, LInclude, LDel, LNotCond, LSuffix, LJoin] + block_allowed = [ + LVariants, + LIdentifier, + LOnly, + LNo, + LInclude, + LDel, + LNotCond, + LSuffix, + LJoin, + ] variants_allowed = [LVariant] - identifier_allowed = [LSet, LAppend, LPrepend, LLazySet, - LRegExpSet, LRegExpAppend, - LRegExpPrepend, LColon, - LEndL] + identifier_allowed = [ + LSet, + LAppend, + LPrepend, + LLazySet, + LRegExpSet, + LRegExpAppend, + LRegExpPrepend, + LColon, + LEndL, + ] varianst_allowed_in = [LLBracket, LColon, LIdentifier, LEndL] indent_allowed = [LIndent, LEndBlock] @@ -1578,14 +1641,15 @@ def _parse(self, lexer, node=None, prev_indent=-1): # identifier ?= xxx # etc.. op = identifier[-1] - if (len(identifier) == 1): + if len(identifier) == 1: identifier = token else: identifier = [token] + identifier[:-1] identifier = "".join([str(x) for x in identifier]) _, value = lexer.get_next_check([LString]) - if value and (value[0] == value[-1] == '"' or - value[0] == value[-1] == "'"): + if value and ( + value[0] == value[-1] == '"' or value[0] == value[-1] == "'" + ): value = value[1:-1] op.set_operands(identifier, value) @@ -1602,12 +1666,9 @@ def _parse(self, lexer, node=None, prev_indent=-1): lexer.get_next_check([LEndL]) continue else: - pre_dict = apply_predict(lexer, node, - pre_dict) + pre_dict = apply_predict(lexer, node, pre_dict) - node.content += [(lexer.filename, - lexer.linenum, - op)] + node.content += [(lexer.filename, lexer.linenum, op)] lexer.get_next_check([LEndL]) elif isinstance(identifier[-1], LColon): # condition: @@ -1617,17 +1678,21 @@ def _parse(self, lexer, node=None, prev_indent=-1): cfilter = parse_filter(lexer, identifier + [LEndL()]) next_line = lexer.rest_line_as_LString() if next_line != "": - lexer.reader.set_next_line(next_line, indent + 1, - lexer.linenum) + lexer.reader.set_next_line( + next_line, indent + 1, lexer.linenum + ) cond = Condition(cfilter, lexer.line) self._parse(lexer, cond, prev_indent=indent) pre_dict = apply_predict(lexer, node, pre_dict) node.content += [(lexer.filename, lexer.linenum, cond)] else: - raise ParserError("Syntax ERROR expected \":\" or" - " operand", lexer.line, - lexer.filename, lexer.linenum) + raise ParserError( + 'Syntax ERROR expected ":" or' " operand", + lexer.line, + lexer.filename, + lexer.linenum, + ) elif typet == LVariant: # Parse @@ -1649,36 +1714,33 @@ def _parse(self, lexer, node=None, prev_indent=-1): while True: lexer.set_prev_indent(var_indent) # Get token from lexer and check syntax. - typet, token = lexer.get_next_check_nw([LIdentifier, - LDefault, - LIndent, - LEndBlock]) + typet, token = lexer.get_next_check_nw( + [LIdentifier, LDefault, LIndent, LEndBlock] + ) if typet == LEndBlock: break if typet == LIndent: lexer.get_next_check_nw([LVariant]) typet, token = lexer.get_next_check_nw( - [LIdentifier, - LDefault]) + [LIdentifier, LDefault] + ) if typet == LDefault: # @ is_default = True - name = lexer.get_until_check([LIdentifier, LDot], - [LColon]) + name = lexer.get_until_check([LIdentifier, LDot], [LColon]) else: # identificator is_default = False name = [token] + lexer.get_until_check( - [LIdentifier, LDot], - [LColon]) + [LIdentifier, LDot], [LColon] + ) if len(name) == 2: name = [name[0]] raw_name = name else: raw_name = [x for x in name[:-1]] - name = [x for x in name[:-1] - if isinstance(x, LIdentifier)] + name = [x for x in name[:-1] if isinstance(x, LIdentifier)] token = next(lexer.generator) while isinstance(token, LWhite): @@ -1696,18 +1758,16 @@ def _parse(self, lexer, node=None, prev_indent=-1): node2.labels = node.labels if var_name: - op = LSet().set_operands(var_name, - ".".join([str(n) for n in name])) - node2.content += [(lexer.filename, - lexer.linenum, - op)] + op = LSet().set_operands( + var_name, ".".join([str(n) for n in name]) + ) + node2.content += [(lexer.filename, lexer.linenum, op)] node3 = self._parse(lexer, node2, prev_indent=indent) if var_name: node3.var_name = var_name - node3.name = [Label(var_name, str(n)) - for n in name] + node3.name = [Label(var_name, str(n)) for n in name] else: node3.name = [Label(str(n)) for n in name] @@ -1721,29 +1781,29 @@ def _parse(self, lexer, node=None, prev_indent=-1): is_default = True meta["default"].remove(wd) - if (is_default and not already_default and - meta_in_expand_defautls): + if ( + is_default + and not already_default + and meta_in_expand_defautls + ): node3.default = True already_default = True node3.append_to_shortname = not is_default op = LUpdateFileMap() - op.set_operands(lexer.filename, - ".".join(str(x) - for x in node3.name)) - node3.content += [(lexer.filename, - lexer.linenum, - op)] + op.set_operands( + lexer.filename, ".".join(str(x) for x in node3.name) + ) + node3.content += [(lexer.filename, lexer.linenum, op)] op = LUpdateFileMap() - op.set_operands(lexer.filename, - ".".join(str(x.name) - for x in node3.name), - "_short_name_map_file") - node3.content += [(lexer.filename, - lexer.linenum, - op)] + op.set_operands( + lexer.filename, + ".".join(str(x.name) for x in node3.name), + "_short_name_map_file", + ) + node3.content += [(lexer.filename, lexer.linenum, op)] if node3.default and self.defaults: # Move default variant in front of rest @@ -1756,9 +1816,12 @@ def _parse(self, lexer, node=None, prev_indent=-1): node4.labels.update(node3.name) if "default" in meta and meta["default"]: - raise ParserError("Missing default variant %s" % - (meta["default"]), lexer.line, - lexer.filename, lexer.linenum) + raise ParserError( + "Missing default variant %s" % (meta["default"]), + lexer.line, + lexer.filename, + lexer.linenum, + ) allowed = block_allowed node = node4 @@ -1766,13 +1829,17 @@ def _parse(self, lexer, node=None, prev_indent=-1): # Parse # variants _name_ [meta1] [meta2]: if type(node) in [Condition, NegativeCondition]: - raise ParserError("'variants' is not allowed inside a " - "conditional block", lexer.line, - lexer.reader.filename, lexer.linenum) + raise ParserError( + "'variants' is not allowed inside a " "conditional block", + lexer.line, + lexer.reader.filename, + lexer.linenum, + ) lexer.set_strict() - tokens = lexer.get_until_no_white([LLBracket, LColon, - LIdentifier, LEndL]) + tokens = lexer.get_until_no_white( + [LLBracket, LColon, LIdentifier, LEndL] + ) vtypet = type(tokens[-1]) var_name = "" meta.clear() @@ -1780,32 +1847,33 @@ def _parse(self, lexer, node=None, prev_indent=-1): while vtypet not in [LColon, LEndL]: if vtypet == LIdentifier: if var_name != "": - raise ParserError("Syntax ERROR expected" - " \"[\" or \":\"", - lexer.line, lexer.filename, - lexer.linenum) + raise ParserError( + "Syntax ERROR expected" ' "[" or ":"', + lexer.line, + lexer.filename, + lexer.linenum, + ) var_name = tokens[0] elif vtypet == LLBracket: # [ _, ident = lexer.get_next_check_nw([LIdentifier]) - typet, _ = lexer.get_next_check_nw([LSet, - LRBracket]) + typet, _ = lexer.get_next_check_nw([LSet, LRBracket]) if typet == LRBracket: # [xxx] if ident not in meta: meta[ident] = [] meta[ident].append(True) elif typet == LSet: # [xxx = yyyy] - tokens = lexer.get_until_no_white([LRBracket, - LEndL]) + tokens = lexer.get_until_no_white([LRBracket, LEndL]) if isinstance(tokens[-1], LRBracket): if ident not in meta: meta[ident] = [] meta[ident].append(tokens[:-1]) else: - raise ParserError("Syntax ERROR" - " expected \"]\"", - lexer.line, - lexer.filename, - lexer.linenum) + raise ParserError( + "Syntax ERROR" ' expected "]"', + lexer.line, + lexer.filename, + lexer.linenum, + ) tokens = lexer.get_next_check_nw(varianst_allowed_in) vtypet = type(tokens[-1]) @@ -1813,16 +1881,20 @@ def _parse(self, lexer, node=None, prev_indent=-1): if "default" in meta: for wd in meta["default"]: if not isinstance(wd, list): - raise ParserError("Syntax ERROR expected " - "[default=xxx]", - lexer.line, - lexer.filename, - lexer.linenum) + raise ParserError( + "Syntax ERROR expected " "[default=xxx]", + lexer.line, + lexer.filename, + lexer.linenum, + ) if vtypet == LEndL: - raise ParserError("Syntax ERROR expected \":\"", - lexer.line, lexer.filename, - lexer.linenum) + raise ParserError( + 'Syntax ERROR expected ":"', + lexer.line, + lexer.filename, + lexer.linenum, + ) lexer.get_next_check_nw([LEndL]) allowed = variants_allowed var_indent = indent @@ -1834,11 +1906,21 @@ def _parse(self, lexer, node=None, prev_indent=-1): pre_dict = apply_predict(lexer, node, pre_dict) if typet == LOnly: - node.content += [(lexer.filename, lexer.linenum, - OnlyFilter(lfilter, lexer.line))] + node.content += [ + ( + lexer.filename, + lexer.linenum, + OnlyFilter(lfilter, lexer.line), + ) + ] else: # LNo - node.content += [(lexer.filename, lexer.linenum, - NoFilter(lfilter, lexer.line))] + node.content += [ + ( + lexer.filename, + lexer.linenum, + NoFilter(lfilter, lexer.line), + ) + ] elif typet == LJoin: # Parse: @@ -1848,7 +1930,9 @@ def _parse(self, lexer, node=None, prev_indent=-1): pre_dict = apply_predict(lexer, node, pre_dict) - node.content += [(lexer.filename, lexer.linenum, JoinFilter(lfilter, lexer.line))] + node.content += [ + (lexer.filename, lexer.linenum, JoinFilter(lfilter, lexer.line)) + ] elif typet == LSuffix: # Parse: @@ -1866,14 +1950,16 @@ def _parse(self, lexer, node=None, prev_indent=-1): # include relative file patch to working directory. path = lexer.rest_line_as_LString() filename = os.path.expanduser(path) - if (isinstance(lexer.reader, FileReader) and - not os.path.isabs(filename)): + if isinstance(lexer.reader, FileReader) and not os.path.isabs( + filename + ): filename = os.path.join( - os.path.dirname(lexer.filename), - filename) + os.path.dirname(lexer.filename), filename + ) if not os.path.isfile(filename): - raise MissingIncludeError(lexer.line, lexer.filename, - lexer.linenum) + raise MissingIncludeError( + lexer.line, lexer.filename, lexer.linenum + ) pre_dict = apply_predict(lexer, node, pre_dict) lch = Lexer(FileReader(filename)) node = self._parse(lch, node, -1) @@ -1887,19 +1973,17 @@ def _parse(self, lexer, node=None, prev_indent=-1): token.set_operands(to_del, None) pre_dict = apply_predict(lexer, node, pre_dict) - node.content += [(lexer.filename, lexer.linenum, - token)] + node.content += [(lexer.filename, lexer.linenum, token)] elif typet == LNotCond: # Parse: # !xxx.yyy.(aaa=bbb): vvv - lfilter = parse_filter(lexer, - lexer.get_until_no_white( - [LColon, LEndL])[:-1]) + lfilter = parse_filter( + lexer, lexer.get_until_no_white([LColon, LEndL])[:-1] + ) next_line = lexer.rest_line_as_LString() if next_line != "": - lexer.reader.set_next_line(next_line, indent + 1, - lexer.linenum) + lexer.reader.set_next_line(next_line, indent + 1, lexer.linenum) cond = NegativeCondition(lfilter, lexer.line) self._parse(lexer, cond, prev_indent=indent) lexer.set_prev_indent(prev_indent) @@ -1907,11 +1991,14 @@ def _parse(self, lexer, node=None, prev_indent=-1): pre_dict = apply_predict(lexer, node, pre_dict) node.content += [(lexer.filename, lexer.linenum, cond)] else: - raise ParserError("Syntax ERROR expected", lexer.line, - lexer.filename, lexer.linenum) + raise ParserError( + "Syntax ERROR expected", + lexer.line, + lexer.filename, + lexer.linenum, + ) except Exception: - self._debug("%s %s: %s" % (lexer.filename, lexer.linenum, - lexer.line)) + self._debug("%s %s: %s" % (lexer.filename, lexer.linenum, lexer.line)) raise def get_dicts(self, node=None, ctx=[], content=[], shortname=[], dep=[]): @@ -1984,10 +2071,10 @@ def get_dicts(self, node=None, ctx=[], content=[], shortname=[], dep=[]): def mk_name(self, n1, n2): """Make name for test. Case: two dics were merged""" - common_prefix = n1[:[x[0] == x[1] for x in list(zip(n1, n2))].index(0)] - cp = ".".join(common_prefix.split('.')[:-1]) - p1 = re.sub(r"^"+cp, "", n1) - p2 = re.sub(r"^"+cp, "", n2) + common_prefix = n1[: [x[0] == x[1] for x in list(zip(n1, n2))].index(0)] + cp = ".".join(common_prefix.split(".")[:-1]) + p1 = re.sub(r"^" + cp, "", n1) + p2 = re.sub(r"^" + cp, "", n2) if cp: name = cp + p1 + p2 else: @@ -2014,7 +2101,9 @@ def multiply_join(self, onlys, node=None, ctx=[], content=[], shortname=[], dep= for d1 in self.get_dicts_plain(node, ctx, content, shortname, dep): # Current frame multiply by all variants from bottom node.content = content_orig - for d2 in self.multiply_join(remains, node, ctx, content, shortname, dep): + for d2 in self.multiply_join( + remains, node, ctx, content, shortname, dep + ): d = d1.copy() d.update(d2) @@ -2029,6 +2118,7 @@ def get_dicts_plain(self, node=None, ctx=[], content=[], shortname=[], dep=[]): :return: A dict generator. """ + def process_content(content, failed_filters): # 1. Check that the filters in content are OK with the current # context (ctx). @@ -2052,22 +2142,29 @@ def process_content(content, failed_filters): # This filter requires action now if type(obj) is OnlyFilter or type(obj) is NoFilter: if obj not in blocked_filters: - self._debug(" filter did not pass: %r (%s:%s)", - obj.line, filename, linenum) + self._debug( + " filter did not pass: %r (%s:%s)", + obj.line, + filename, + linenum, + ) failed_filters.append(t) return False else: continue else: - self._debug(" conditional block matches:" - " %r (%s:%s)", obj.line, filename, linenum) + self._debug( + " conditional block matches:" " %r (%s:%s)", + obj.line, + filename, + linenum, + ) # Check and unpack the content inside this Condition # object (note: the failed filters should go into # new_internal_filters because we don't expect them to # come from outside this node, even if the Condition # itself was external) - if not process_content(obj.content, - new_internal_filters): + if not process_content(obj.content, new_internal_filters): failed_filters.append(t) return False continue @@ -2079,20 +2176,18 @@ def process_content(content, failed_filters): new_content.append(t) return True - def might_pass(failed_ctx, - failed_ctx_set, - failed_external_filters, - failed_internal_filters): + def might_pass( + failed_ctx, failed_ctx_set, failed_external_filters, failed_internal_filters + ): all_content = content + node.content for t in failed_external_filters + failed_internal_filters: if t not in all_content: return True for t in failed_external_filters: _, _, external_filter = t - if not external_filter.might_pass(failed_ctx, - failed_ctx_set, - ctx, ctx_set, - labels): + if not external_filter.might_pass( + failed_ctx, failed_ctx_set, ctx, ctx_set, labels + ): return False for t in failed_internal_filters: if t not in node.content: @@ -2100,17 +2195,16 @@ def might_pass(failed_ctx, for t in failed_internal_filters: _, _, internal_filter = t - if not internal_filter.might_pass(failed_ctx, - failed_ctx_set, - ctx, ctx_set, - labels): + if not internal_filter.might_pass( + failed_ctx, failed_ctx_set, ctx, ctx_set, labels + ): return False return True def add_failed_case(): - node.failed_cases.appendleft((ctx, ctx_set, - new_external_filters, - new_internal_filters)) + node.failed_cases.appendleft( + (ctx, ctx_set, new_external_filters, new_internal_filters) + ) if len(node.failed_cases) > num_failed_cases: node.failed_cases.pop() @@ -2134,10 +2228,14 @@ def add_failed_case(): # Check previously failed filters for i, failed_case in enumerate(node.failed_cases): if not might_pass(*failed_case): - self._debug("\n* this subtree has failed before %s\n" - " content: %s\n" - " failcase:%s\n", - name, content + node.content, failed_case) + self._debug( + "\n* this subtree has failed before %s\n" + " content: %s\n" + " failcase:%s\n", + name, + content + node.content, + failed_case, + ) del node.failed_cases[i] node.failed_cases.appendleft(failed_case) return @@ -2146,8 +2244,9 @@ def add_failed_case(): new_content = [] new_external_filters = [] new_internal_filters = [] - if (not process_content(node.content, new_internal_filters) or - not process_content(content, new_external_filters)): + if not process_content( + node.content, new_internal_filters + ) or not process_content(content, new_external_filters): add_failed_case() self._debug("Failed_cases %s", node.failed_cases) return @@ -2173,8 +2272,11 @@ def add_failed_case(): # Reached leaf? if not node.children: self._debug(" reached leaf, returning it") - d = {"name": name, "dep": dep, - "shortname": ".".join([str(sn.name) for sn in shortname])} + d = { + "name": name, + "dep": dep, + "shortname": ".".join([str(sn.name) for sn in shortname]), + } for _, _, op in new_content: op.apply_to_dict(d) postfix_parse(d) @@ -2198,6 +2300,7 @@ def print_dicts_default(options, dicts): # pylint: disable=W0613 def print_dicts_repr(options, dicts): import pprint + print("[") for dic in dicts: print("%s," % (pprint.pformat(dic))) @@ -2211,7 +2314,7 @@ def print_dicts(options, dicts): print_dicts_default(options, dicts) -def convert_data_size(size, default_sufix='B'): +def convert_data_size(size, default_sufix="B"): """ Convert data size from human readable units to an int of arbitrary size. @@ -2219,12 +2322,13 @@ def convert_data_size(size, default_sufix='B'): :param default_sufix: Default sufix used to represent data. :return: Int with data size in the appropriate order of magnitude. """ - orders = {'B': 1, - 'K': 1024, - 'M': 1024 * 1024, - 'G': 1024 * 1024 * 1024, - 'T': 1024 * 1024 * 1024 * 1024, - } + orders = { + "B": 1, + "K": 1024, + "M": 1024 * 1024, + "G": 1024 * 1024 * 1024, + "T": 1024 * 1024 * 1024 * 1024, + } order = re.findall("([BbKkMmGgTt])", size[-1]) if not order: @@ -2270,13 +2374,11 @@ def postfix_parse(dic): continue if key.endswith("_max"): tmp_key = key.split("_max")[0] - if (tmp_key not in dic or - compare_string(dic[tmp_key], dic[key]) > 0): + if tmp_key not in dic or compare_string(dic[tmp_key], dic[key]) > 0: tmp_dict[tmp_key] = dic[key] elif key.endswith("_min"): tmp_key = key.split("_min")[0] - if (tmp_key not in dic or - compare_string(dic[tmp_key], dic[key]) < 0): + if tmp_key not in dic or compare_string(dic[tmp_key], dic[key]) < 0: tmp_dict[tmp_key] = dic[key] elif key.endswith("_fixed"): tmp_key = key.split("_fixed")[0] @@ -2286,25 +2388,62 @@ def postfix_parse(dic): if __name__ == "__main__": - parser = optparse.OptionParser('usage: %prog [options] filename ' - '[extra code] ...\n\nExample:\n\n ' - '%prog tests.cfg "only my_set" "no qcow2"') - parser.add_option("-v", "--verbose", dest="debug", action="store_true", - help="include debug messages in console output") - parser.add_option("-f", "--fullname", dest="fullname", action="store_true", - help="show full dict names instead of short names") - parser.add_option("-c", "--contents", dest="contents", action="store_true", - help="show dict contents") - parser.add_option("-r", "--repr", dest="repr_mode", action="store_true", - help="output parsing results Python format") - parser.add_option("-d", "--defaults", dest="defaults", action="store_true", - help="use only default variant of variants if there" - " is some") - parser.add_option("-e", "--expand", dest="expand", type="string", - help="list of vartiant which should be expanded when" - " defaults is enabled. \"name, name, name\"") - parser.add_option("-s", "--skip-dups", dest="skipdups", default=True, action="store_false", - help="Don't drop variables with different suffixes and same val") + parser = optparse.OptionParser( + "usage: %prog [options] filename " + "[extra code] ...\n\nExample:\n\n " + '%prog tests.cfg "only my_set" "no qcow2"' + ) + parser.add_option( + "-v", + "--verbose", + dest="debug", + action="store_true", + help="include debug messages in console output", + ) + parser.add_option( + "-f", + "--fullname", + dest="fullname", + action="store_true", + help="show full dict names instead of short names", + ) + parser.add_option( + "-c", + "--contents", + dest="contents", + action="store_true", + help="show dict contents", + ) + parser.add_option( + "-r", + "--repr", + dest="repr_mode", + action="store_true", + help="output parsing results Python format", + ) + parser.add_option( + "-d", + "--defaults", + dest="defaults", + action="store_true", + help="use only default variant of variants if there" " is some", + ) + parser.add_option( + "-e", + "--expand", + dest="expand", + type="string", + help="list of vartiant which should be expanded when" + ' defaults is enabled. "name, name, name"', + ) + parser.add_option( + "-s", + "--skip-dups", + dest="skipdups", + default=True, + action="store_false", + help="Don't drop variables with different suffixes and same val", + ) options, args = parser.parse_args() if not args: @@ -2316,8 +2455,9 @@ def postfix_parse(dic): expand = [] if options.expand: expand = [x.strip() for x in options.expand.split(",")] - c = Parser(args[0], defaults=options.defaults, expand_defaults=expand, - debug=options.debug) + c = Parser( + args[0], defaults=options.defaults, expand_defaults=expand, debug=options.debug + ) for s in args[1:]: c.parse_string(s) diff --git a/virttest/ceph.py b/virttest/ceph.py index 47d87d7dcc..57ef5d420b 100644 --- a/virttest/ceph.py +++ b/virttest/ceph.py @@ -18,7 +18,7 @@ from virttest import error_context from virttest import utils_misc -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class CephError(Exception): @@ -26,9 +26,16 @@ class CephError(Exception): @error_context.context_aware -def rbd_image_create(ceph_monitor, rbd_pool_name, rbd_image_name, - rbd_image_size, force_create=False, ceph_conf=None, - keyfile=None, rbd_namespace_name=None): +def rbd_image_create( + ceph_monitor, + rbd_pool_name, + rbd_image_name, + rbd_image_size, + force_create=False, + ceph_conf=None, + keyfile=None, + rbd_namespace_name=None, +): """ Create a rbd image. :params ceph_monitor: The specified monitor to connect to @@ -45,37 +52,66 @@ def rbd_image_create(ceph_monitor, rbd_pool_name, rbd_image_name, int(rbd_image_size) compare_str = rbd_image_size except ValueError: - compare_str = utils_numeric.normalize_data_size(rbd_image_size, 'M') - - if rbd_image_exist(ceph_monitor, rbd_pool_name, rbd_image_name, - ceph_conf, keyfile, rbd_namespace_name): + compare_str = utils_numeric.normalize_data_size(rbd_image_size, "M") + + if rbd_image_exist( + ceph_monitor, + rbd_pool_name, + rbd_image_name, + ceph_conf, + keyfile, + rbd_namespace_name, + ): create_image = False - image_info = rbd_image_info(ceph_monitor, rbd_pool_name, - rbd_image_name, ceph_conf, keyfile, - rbd_namespace_name) - if image_info['size'] != compare_str or force_create: - rbd_image_rm(ceph_monitor, rbd_pool_name, rbd_image_name, - ceph_conf, keyfile, rbd_namespace_name) + image_info = rbd_image_info( + ceph_monitor, + rbd_pool_name, + rbd_image_name, + ceph_conf, + keyfile, + rbd_namespace_name, + ) + if image_info["size"] != compare_str or force_create: + rbd_image_rm( + ceph_monitor, + rbd_pool_name, + rbd_image_name, + ceph_conf, + keyfile, + rbd_namespace_name, + ) create_image = True if create_image: cmd = "rbd {opts} create {pool}/{namespace}{image} {size} {keyring}" - c_opt = '-c %s' % ceph_conf if ceph_conf else '' - m_opt = '-m %s' % ceph_monitor if ceph_monitor else '' - opts = m_opt + ' ' + c_opt - namespace = '%s/' % rbd_namespace_name if rbd_namespace_name else '' - size = '-s %d' % utils_numeric.align_value(compare_str, 1) - keyring = '--keyring %s' % keyfile if keyfile else '' - cmd = cmd.format(opts=opts, pool=rbd_pool_name, namespace=namespace, - image=rbd_image_name, size=size, keyring=keyring) + c_opt = "-c %s" % ceph_conf if ceph_conf else "" + m_opt = "-m %s" % ceph_monitor if ceph_monitor else "" + opts = m_opt + " " + c_opt + namespace = "%s/" % rbd_namespace_name if rbd_namespace_name else "" + size = "-s %d" % utils_numeric.align_value(compare_str, 1) + keyring = "--keyring %s" % keyfile if keyfile else "" + cmd = cmd.format( + opts=opts, + pool=rbd_pool_name, + namespace=namespace, + image=rbd_image_name, + size=size, + keyring=keyring, + ) process.system(cmd, verbose=True) else: LOG.debug("Image already exist skip the create.") @error_context.context_aware -def rbd_image_rm(ceph_monitor, rbd_pool_name, rbd_image_name, - ceph_conf=None, keyfile=None, rbd_namespace_name=None): +def rbd_image_rm( + ceph_monitor, + rbd_pool_name, + rbd_image_name, + ceph_conf=None, + keyfile=None, + rbd_namespace_name=None, +): """ Remove a rbd image :params ceph_monitor: The specified monitor to connect to @@ -85,25 +121,41 @@ def rbd_image_rm(ceph_monitor, rbd_pool_name, rbd_image_name, :params ceph_conf: The path to the ceph configuration file :params keyfile: The path to the ceph keyring configuration file """ - if rbd_image_exist(ceph_monitor, rbd_pool_name, rbd_image_name, ceph_conf, - keyfile, rbd_namespace_name): + if rbd_image_exist( + ceph_monitor, + rbd_pool_name, + rbd_image_name, + ceph_conf, + keyfile, + rbd_namespace_name, + ): cmd = "rbd {opts} rm {pool}/{namespace}{image} {keyring}" - c_opt = '-c %s' % ceph_conf if ceph_conf else '' - m_opt = '-m %s' % ceph_monitor if ceph_monitor else '' - opts = m_opt + ' ' + c_opt - namespace = '%s/' % rbd_namespace_name if rbd_namespace_name else '' - keyring = '--keyring %s' % keyfile if keyfile else '' - cmd = cmd.format(opts=opts, pool=rbd_pool_name, namespace=namespace, - image=rbd_image_name, keyring=keyring) + c_opt = "-c %s" % ceph_conf if ceph_conf else "" + m_opt = "-m %s" % ceph_monitor if ceph_monitor else "" + opts = m_opt + " " + c_opt + namespace = "%s/" % rbd_namespace_name if rbd_namespace_name else "" + keyring = "--keyring %s" % keyfile if keyfile else "" + cmd = cmd.format( + opts=opts, + pool=rbd_pool_name, + namespace=namespace, + image=rbd_image_name, + keyring=keyring, + ) process.run(cmd, verbose=True) else: LOG.debug("Image not exist, skip to remove it.") @error_context.context_aware -def rbd_image_exist(ceph_monitor, rbd_pool_name, - rbd_image_name, ceph_conf=None, keyfile=None, - rbd_namespace_name=None): +def rbd_image_exist( + ceph_monitor, + rbd_pool_name, + rbd_image_name, + ceph_conf=None, + keyfile=None, + rbd_namespace_name=None, +): """ Check if rbd image is exist :params ceph_monitor: The specified monitor to connect to @@ -114,25 +166,31 @@ def rbd_image_exist(ceph_monitor, rbd_pool_name, :params keyfile: The path to the ceph keyring configuration file """ cmd = "rbd {opts} ls {pool}{namespace} {keyring}" - c_opt = '-c %s' % ceph_conf if ceph_conf else '' - m_opt = '-m %s' % ceph_monitor if ceph_monitor else '' - opts = m_opt + ' ' + c_opt - keyring = '--keyring %s' % keyfile if keyfile else '' - namespace = '/%s' % rbd_namespace_name if rbd_namespace_name else '' - cmd = cmd.format(opts=opts, pool=rbd_pool_name, keyring=keyring, - namespace=namespace) + c_opt = "-c %s" % ceph_conf if ceph_conf else "" + m_opt = "-m %s" % ceph_monitor if ceph_monitor else "" + opts = m_opt + " " + c_opt + keyring = "--keyring %s" % keyfile if keyfile else "" + namespace = "/%s" % rbd_namespace_name if rbd_namespace_name else "" + cmd = cmd.format( + opts=opts, pool=rbd_pool_name, keyring=keyring, namespace=namespace + ) - output = process.run(cmd, ignore_status=True, - verbose=True).stdout_text + output = process.run(cmd, ignore_status=True, verbose=True).stdout_text LOG.debug("Response from rbd ls command is: %s" % output) - return (rbd_image_name.strip() in output.splitlines()) + return rbd_image_name.strip() in output.splitlines() @error_context.context_aware -def rbd_image_info(ceph_monitor, rbd_pool_name, rbd_image_name, - ceph_conf=None, keyfile=None, rbd_namespace_name=None): +def rbd_image_info( + ceph_monitor, + rbd_pool_name, + rbd_image_name, + ceph_conf=None, + keyfile=None, + rbd_namespace_name=None, +): """ Get information of a rbd image :params ceph_monitor: The specified monitor to connect to @@ -143,28 +201,33 @@ def rbd_image_info(ceph_monitor, rbd_pool_name, rbd_image_name, :params keyfile: The path to the ceph keyring configuration file """ cmd = "rbd {opts} info {pool}/{namespace}{image} {keyring}" - c_opt = '-c %s' % ceph_conf if ceph_conf else '' - m_opt = '-m %s' % ceph_monitor if ceph_monitor else '' - opts = m_opt + ' ' + c_opt - namespace = '%s/' % rbd_namespace_name if rbd_namespace_name else '' - keyring = '--keyring %s' % keyfile if keyfile else '' - cmd = cmd.format(opts=opts, pool=rbd_pool_name, image=rbd_image_name, - namespace=namespace, keyring=keyring) + c_opt = "-c %s" % ceph_conf if ceph_conf else "" + m_opt = "-m %s" % ceph_monitor if ceph_monitor else "" + opts = m_opt + " " + c_opt + namespace = "%s/" % rbd_namespace_name if rbd_namespace_name else "" + keyring = "--keyring %s" % keyfile if keyfile else "" + cmd = cmd.format( + opts=opts, + pool=rbd_pool_name, + image=rbd_image_name, + namespace=namespace, + keyring=keyring, + ) output = process.run(cmd).stdout_text - info_pattern = "rbd image \'%s\':.*?$" % rbd_image_name + info_pattern = "rbd image '%s':.*?$" % rbd_image_name rbd_image_info_str = re.findall(info_pattern, output, re.S)[0] rbd_image_info = {} for rbd_image_line in rbd_image_info_str.splitlines(): if ":" not in rbd_image_line: if "size" in rbd_image_line: - size_str = re.findall("size\s+(\d+\s+\w+)\s+", - rbd_image_line)[0] - size = utils_numeric.normalize_data_size(size_str, 'M') - rbd_image_info['size'] = size + size_str = re.findall("size\s+(\d+\s+\w+)\s+", rbd_image_line)[0] + size = utils_numeric.normalize_data_size(size_str, "M") + rbd_image_info["size"] = size if "order" in rbd_image_line: - rbd_image_info['order'] = int(re.findall("order\s+(\d+)", - rbd_image_line)) + rbd_image_info["order"] = int( + re.findall("order\s+(\d+)", rbd_image_line) + ) else: tmp_str = rbd_image_line.strip().split(":") rbd_image_info[tmp_str[0]] = tmp_str[1] @@ -179,8 +242,7 @@ def rbd_image_map(ceph_monitor, rbd_pool_name, rbd_image_name): :params rbd_pool_name: The name of rbd pool :params rbd_image_name: The name of rbd image """ - cmd = "rbd map %s --pool %s -m %s" % (rbd_image_name, rbd_pool_name, - ceph_monitor) + cmd = "rbd map %s --pool %s -m %s" % (rbd_image_name, rbd_pool_name, ceph_monitor) output = process.run(cmd, verbose=True).stdout_text if os.path.exist(os.path.join("/dev/rbd", rbd_pool_name, rbd_image_name)): return os.path.join("/dev/rbd", rbd_pool_name, rbd_image_name) @@ -203,8 +265,9 @@ def rbd_image_unmap(rbd_pool_name, rbd_image_name): @error_context.context_aware -def get_image_filename(ceph_monitor, rbd_pool_name, rbd_image_name, - ceph_conf=None, rbd_namespace_name=None): +def get_image_filename( + ceph_monitor, rbd_pool_name, rbd_image_name, ceph_conf=None, rbd_namespace_name=None +): """ Configuration has already been configured in the conf file Return the rbd image file name @@ -214,14 +277,15 @@ def get_image_filename(ceph_monitor, rbd_pool_name, rbd_image_name, :params rbd_image_name: The name of rbd image :params ceph_conf: The path to the ceph configuration file """ - uri = 'rbd:{pool}/{namespace}{image}{opts}' - conf_opt = ':conf=%s' % ceph_conf if ceph_conf else '' - mon_opt = ':mon_host=%s' % ceph_monitor if ceph_monitor else '' + uri = "rbd:{pool}/{namespace}{image}{opts}" + conf_opt = ":conf=%s" % ceph_conf if ceph_conf else "" + mon_opt = ":mon_host=%s" % ceph_monitor if ceph_monitor else "" opts = conf_opt + mon_opt - namespace = '%s/' % rbd_namespace_name if rbd_namespace_name else '' + namespace = "%s/" % rbd_namespace_name if rbd_namespace_name else "" - return uri.format(pool=rbd_pool_name, namespace=namespace, - image=rbd_image_name, opts=opts) + return uri.format( + pool=rbd_pool_name, namespace=namespace, image=rbd_image_name, opts=opts + ) @error_context.context_aware @@ -235,8 +299,8 @@ def create_config_file(ceph_monitor): if not os.path.exists(ceph_dir): os.makedirs(ceph_dir) if not os.path.exists(ceph_cfg): - with open(ceph_cfg, 'w+') as f: - f.write('mon_host = %s\n' % ceph_monitor) + with open(ceph_cfg, "w+") as f: + f.write("mon_host = %s\n" % ceph_monitor) return ceph_cfg @@ -255,9 +319,9 @@ def create_keyring_file(client_name, client_key): os.makedirs(ceph_dir) if os.path.exists(keyring_file): os.remove(keyring_file) - with open(keyring_file, 'w') as f: - f.write('[%s]\n' % client_name) - f.write(' key = %s\n' % client_key) + with open(keyring_file, "w") as f: + f.write("[%s]\n" % client_name) + f.write(" key = %s\n" % client_key) return keyring_file @@ -273,11 +337,11 @@ def cephfs_mount(ceph_uri, mount_point, options=None, verbose=False, session=Non :param session: mount within the session if given """ cephfs_umount(ceph_uri, mount_point, verbose, session) - mount_cmd = ['mount -t ceph'] + mount_cmd = ["mount -t ceph"] if options: - mount_cmd.extend(['-o', options]) + mount_cmd.extend(["-o", options]) mount_cmd.extend([ceph_uri, mount_point]) - mount_cmd = ' '.join(mount_cmd) + mount_cmd = " ".join(mount_cmd) if session: create_mount_point = "mkdir -p %s" % mount_point session.cmd(create_mount_point, ok_status=[0, 1], ignore_all_errors=True) @@ -287,7 +351,9 @@ def cephfs_mount(ceph_uri, mount_point, options=None, verbose=False, session=Non try: utils_misc.make_dirs(mount_point) except OSError as dirError: - LOG.debug("Creation of the directory:%s failed:%s", mount_point, str(dirError)) + LOG.debug( + "Creation of the directory:%s failed:%s", mount_point, str(dirError) + ) else: LOG.debug("Successfully created the directory %s", mount_point) process.system(mount_cmd, verbose=verbose) @@ -314,6 +380,8 @@ def cephfs_umount(ceph_uri, mount_point, verbose=False, session=None): try: utils_misc.safe_rmdir(mount_point) except OSError as dirError: - LOG.debug("Delete of the directory:%s failed:%s", mount_point, str(dirError)) + LOG.debug( + "Delete of the directory:%s failed:%s", mount_point, str(dirError) + ) else: LOG.debug("Successfully deleted the directory %s", mount_point) diff --git a/virttest/compat.py b/virttest/compat.py index 732b053b90..6915c516d9 100644 --- a/virttest/compat.py +++ b/virttest/compat.py @@ -15,12 +15,17 @@ def is_registering_settings_required(): TODO: remove this once support for Avocado releases before 81.0, including 69.x LTS is dropped. """ - return all((hasattr(settings, 'add_argparser_to_option'), - hasattr(settings, 'register_option'), - hasattr(settings, 'as_json'))) + return all( + ( + hasattr(settings, "add_argparser_to_option"), + hasattr(settings, "register_option"), + hasattr(settings, "as_json"), + ) + ) if is_registering_settings_required(): + def get_opt(opt, name): """ Compatibility handler to Avocado with configuration as dict @@ -45,19 +50,21 @@ def set_opt_from_settings(opt, section, key, **kwargs): pass def get_settings_value(section, key, **kwargs): - namespace = '%s.%s' % (section, key) + namespace = "%s.%s" % (section, key) return settings.as_dict().get(namespace) def add_option(parser, arg, **kwargs): """Add a command-line argument parser to an existing option.""" settings.add_argparser_to_option( - namespace=kwargs.get('dest'), - action=kwargs.get('action', 'store'), + namespace=kwargs.get("dest"), + action=kwargs.get("action", "store"), parser=parser, allow_multiple=True, - long_arg=arg) + long_arg=arg, + ) else: + def get_opt(opt, name): """ Compatibility handler for options in either argparse.Namespace or dict @@ -86,7 +93,7 @@ def set_opt(opt, name, value): def set_opt_from_settings(opt, section, key, **kwargs): """Sets option default value from the configuration file.""" value = settings.get_value(section, key, **kwargs) - namespace = '%s.%s' % (section, key) + namespace = "%s.%s" % (section, key) set_opt(opt, namespace, value) def get_settings_value(section, key, **kwargs): diff --git a/virttest/cpu.py b/virttest/cpu.py index bfdfde7030..ea4b5865f5 100644 --- a/virttest/cpu.py +++ b/virttest/cpu.py @@ -38,71 +38,107 @@ # lazy imports for dependencies that are not needed in all modes of use # (as the cpu module is generic to qemu vm use only, libvirt is optional) from virttest._wrappers import lazy_import + virsh = lazy_import("virttest.virsh") libvirt_xml = lazy_import("virttest.libvirt_xml") -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) ARCH = platform.machine() -CPU_TYPES = {"AuthenticAMD": ["EPYC-Genoa", "EPYC-Milan", "EPYC-Rome", "EPYC", - "Opteron_G5", "Opteron_G4", "Opteron_G3", - "Opteron_G2", "Opteron_G1"], - "GenuineIntel": ["SapphireRapids", "Snowridge", "Cooperlake", - "Icelake-Server", "Icelake-Server-noTSX", - "Icelake-Client", "Icelake-Client-noTSX", - "Cascadelake-Server", "Cascadelake-Server-noTSX", - "Skylake-Server", "Skylake-Server-noTSX-IBRS", - "Skylake-Client", "Skylake-Client-noTSX-IBRS", - "Broadwell", "Broadwell-noTSX", - "Haswell", "Haswell-noTSX", "IvyBridge", - "SandyBridge", "Westmere", "Nehalem", - "Penryn", "Conroe"]} -CPU_TYPES_RE = {"EPYC-Genoa": "la57,vnmi,avx512f,avx512dq,avx512ifma,avx512cd," - "avx512bw,avx512vl,avx512vbmi,avx512_vbmi2,gfni,avx512_vnni," - "avx512_bitalg,avx512_vpopcntdq,avx512_bf16", - "EPYC-Milan": "ibrs,pcid,ssbd,erms,fsrm,invpcid,pku", - "EPYC-Rome": "rdpid,wbnoinvd,stibp,clwb,umip", - "EPYC": "avx2,adx,bmi2,sha_ni", - "Opteron_G5": "f16c,fma4,xop,tbm", - "Opteron_G4": ("fma4,xop,avx,xsave,aes,sse4.2|sse4_2," - "sse4.1|sse4_1,cx16,ssse3,sse4a"), - "Opteron_G3": "cx16,sse4a", - "Opteron_G2": "cx16", - "Opteron_G1": "", - "SapphireRapids": ("serialize,tsxldtrk|tsx-ldtrk,amx_bf16," - "amx_tile,amx_int8,avx512_bf16,avx_vnni," - "avx512_fp16,hle,rtm,taa-no"), - "Snowridge": "split_lock_detect,gfni,movdiri,movdiri64b,cldemote", - "Cooperlake": "avx512_bf16,stibp,arch_capabilities,hle,rtm", - "Icelake-Server": "avx512_vnni,la57,clflushopt,hle,rtm", - "Icelake-Server-noTSX": "avx512_vnni,la57,clflushopt", - "Icelake-Client": ("avx512_vpopcntdq|avx512-vpopcntdq," - "avx512vbmi,avx512_vbmi2|avx512vbmi2,hle,rtm" - "gfni,vaes,vpclmulqdq,avx512_vnni,hle,rtm"), - "Icelake-Client-noTSX": ("avx512_vpopcntdq|avx512-vpopcntdq," - "avx512vbmi,avx512_vbmi2|avx512vbmi2," - "gfni,vaes,vpclmulqdq,avx512_vnni"), - "Cascadelake-Server": ("avx512f,avx512dq,avx512bw,avx512cd," - "avx512vl,clflushopt,avx512_vnni,hle,rtm"), - "Cascadelake-Server-noTSX": ("avx512f,avx512dq,avx512bw,avx512cd," - "avx512vl,clflushopt,avx512_vnni"), - "Skylake-Server": "avx512f,clwb,xgetbv1,pcid,hle,rtm", - "Skylake-Server-noTSX-IBRS": "avx512f,clwb,xgetbv1,pcid", - "Skylake-Client": "xgetbv1,pcid,hle,rtm", - "Skylake-Client-noTSX-IBRS": "xgetbv1,pcid", - "Broadwell": "adx,rdseed,3dnowprefetch,hle,rtm", - "Broadwell-noTSX": "adx,rdseed,3dnowprefetch", - "Haswell": "fma,avx2,movbe,hle,rtm", - "Haswell-noTSX": "fma,avx2,movbe", - "IvyBridge": "f16c,fsgsbase,erms", - "SandyBridge": ("avx,xsave,aes,sse4_2|sse4.2,sse4.1|sse4_1," - "cx16,ssse3"), - "Westmere": "aes,sse4.2|sse4_2,sse4.1|sse4_1,cx16,ssse3", - "Nehalem": "sse4.2|sse4_2,sse4.1|sse4_1,cx16,ssse3", - "Penryn": "sse4.1|sse4_1,cx16,ssse3", - "Conroe": "ssse3"} +CPU_TYPES = { + "AuthenticAMD": [ + "EPYC-Genoa", + "EPYC-Milan", + "EPYC-Rome", + "EPYC", + "Opteron_G5", + "Opteron_G4", + "Opteron_G3", + "Opteron_G2", + "Opteron_G1", + ], + "GenuineIntel": [ + "SapphireRapids", + "Snowridge", + "Cooperlake", + "Icelake-Server", + "Icelake-Server-noTSX", + "Icelake-Client", + "Icelake-Client-noTSX", + "Cascadelake-Server", + "Cascadelake-Server-noTSX", + "Skylake-Server", + "Skylake-Server-noTSX-IBRS", + "Skylake-Client", + "Skylake-Client-noTSX-IBRS", + "Broadwell", + "Broadwell-noTSX", + "Haswell", + "Haswell-noTSX", + "IvyBridge", + "SandyBridge", + "Westmere", + "Nehalem", + "Penryn", + "Conroe", + ], +} +CPU_TYPES_RE = { + "EPYC-Genoa": "la57,vnmi,avx512f,avx512dq,avx512ifma,avx512cd," + "avx512bw,avx512vl,avx512vbmi,avx512_vbmi2,gfni,avx512_vnni," + "avx512_bitalg,avx512_vpopcntdq,avx512_bf16", + "EPYC-Milan": "ibrs,pcid,ssbd,erms,fsrm,invpcid,pku", + "EPYC-Rome": "rdpid,wbnoinvd,stibp,clwb,umip", + "EPYC": "avx2,adx,bmi2,sha_ni", + "Opteron_G5": "f16c,fma4,xop,tbm", + "Opteron_G4": ( + "fma4,xop,avx,xsave,aes,sse4.2|sse4_2," "sse4.1|sse4_1,cx16,ssse3,sse4a" + ), + "Opteron_G3": "cx16,sse4a", + "Opteron_G2": "cx16", + "Opteron_G1": "", + "SapphireRapids": ( + "serialize,tsxldtrk|tsx-ldtrk,amx_bf16," + "amx_tile,amx_int8,avx512_bf16,avx_vnni," + "avx512_fp16,hle,rtm,taa-no" + ), + "Snowridge": "split_lock_detect,gfni,movdiri,movdiri64b,cldemote", + "Cooperlake": "avx512_bf16,stibp,arch_capabilities,hle,rtm", + "Icelake-Server": "avx512_vnni,la57,clflushopt,hle,rtm", + "Icelake-Server-noTSX": "avx512_vnni,la57,clflushopt", + "Icelake-Client": ( + "avx512_vpopcntdq|avx512-vpopcntdq," + "avx512vbmi,avx512_vbmi2|avx512vbmi2,hle,rtm" + "gfni,vaes,vpclmulqdq,avx512_vnni,hle,rtm" + ), + "Icelake-Client-noTSX": ( + "avx512_vpopcntdq|avx512-vpopcntdq," + "avx512vbmi,avx512_vbmi2|avx512vbmi2," + "gfni,vaes,vpclmulqdq,avx512_vnni" + ), + "Cascadelake-Server": ( + "avx512f,avx512dq,avx512bw,avx512cd," "avx512vl,clflushopt,avx512_vnni,hle,rtm" + ), + "Cascadelake-Server-noTSX": ( + "avx512f,avx512dq,avx512bw,avx512cd," "avx512vl,clflushopt,avx512_vnni" + ), + "Skylake-Server": "avx512f,clwb,xgetbv1,pcid,hle,rtm", + "Skylake-Server-noTSX-IBRS": "avx512f,clwb,xgetbv1,pcid", + "Skylake-Client": "xgetbv1,pcid,hle,rtm", + "Skylake-Client-noTSX-IBRS": "xgetbv1,pcid", + "Broadwell": "adx,rdseed,3dnowprefetch,hle,rtm", + "Broadwell-noTSX": "adx,rdseed,3dnowprefetch", + "Haswell": "fma,avx2,movbe,hle,rtm", + "Haswell-noTSX": "fma,avx2,movbe", + "IvyBridge": "f16c,fsgsbase,erms", + "SandyBridge": ("avx,xsave,aes,sse4_2|sse4.2,sse4.1|sse4_1," "cx16,ssse3"), + "Westmere": "aes,sse4.2|sse4_2,sse4.1|sse4_1,cx16,ssse3", + "Nehalem": "sse4.2|sse4_2,sse4.1|sse4_1,cx16,ssse3", + "Penryn": "sse4.1|sse4_1,cx16,ssse3", + "Conroe": "ssse3", +} class UnsupportedCPU(exceptions.TestError): @@ -118,21 +154,20 @@ def get_cpu_xmldata(vm, options=""): :return: vcpu count details on xml """ - cpu_xmldata = {'current_vcpu': 0, 'vcpu': None, - 'mtype': None, 'vcpus': None} + cpu_xmldata = {"current_vcpu": 0, "vcpu": None, "mtype": None, "vcpus": None} # Grab a dump of the guest - if we're using the --config, # then get an --inactive dump. extra_opts = "" if "--config" in options or vm.is_dead(): extra_opts = "--inactive" vm_xml = libvirt_xml.VMXML.new_from_dumpxml(vm.name, extra_opts) - cpu_xmldata['mtype'] = vm_xml.os.machine + cpu_xmldata["mtype"] = vm_xml.os.machine try: - cpu_xmldata['current_vcpu'] = int(vm_xml.current_vcpu) + cpu_xmldata["current_vcpu"] = int(vm_xml.current_vcpu) except libvirt_xml.xcepts.LibvirtXMLNotFoundError: LOG.debug("current vcpu value not present in xml, set as max value") - cpu_xmldata['current_vcpu'] = int(vm_xml.vcpu) - cpu_xmldata['vcpu'] = int(vm_xml.vcpu) + cpu_xmldata["current_vcpu"] = int(vm_xml.vcpu) + cpu_xmldata["vcpu"] = int(vm_xml.vcpu) return cpu_xmldata @@ -147,20 +182,19 @@ def hotplug_supported(vm_name, mtype): """ supported = False if "ppc64" in platform.machine(): - cmd = '{\"execute\":\"query-machines\"}' - json_result = virsh.qemu_monitor_command(vm_name, cmd, "--pretty", - debug=False) + cmd = '{"execute":"query-machines"}' + json_result = virsh.qemu_monitor_command(vm_name, cmd, "--pretty", debug=False) try: result = json.loads(json_result.stdout_text) except Exception: # Failure to parse json output and default support to False # TODO: Handle for failure cases return supported - for item in result['return']: + for item in result["return"]: try: - if item['name'] == mtype: + if item["name"] == mtype: try: - if item['hotpluggable-cpus'] == 'True': + if item["hotpluggable-cpus"] == "True": supported = True except KeyError: pass @@ -182,9 +216,10 @@ def affinity_from_vcpuinfo(vm): :return: affinity list of VM """ output = virsh.vcpuinfo(vm.name).stdout_text.rstrip() - affinity = re.findall('CPU Affinity: +[-y]+', output) - total_affinity = [list(vcpu_affinity.split()[-1].strip()) - for vcpu_affinity in affinity] + affinity = re.findall("CPU Affinity: +[-y]+", output) + total_affinity = [ + list(vcpu_affinity.split()[-1].strip()) for vcpu_affinity in affinity + ] return total_affinity @@ -197,19 +232,24 @@ def affinity_from_xml(vm): :return: dict of affinity of VM """ - host_cpu_count = utils.total_count() if hasattr(utils, 'total_count') else utils.total_cpus_count() + host_cpu_count = ( + utils.total_count() + if hasattr(utils, "total_count") + else utils.total_cpus_count() + ) xml_affinity_list = [] xml_affinity = {} try: vmxml = libvirt_xml.VMXML.new_from_dumpxml(vm.name) - xml_affinity_list = vmxml['cputune'].vcpupins + xml_affinity_list = vmxml["cputune"].vcpupins except libvirt_xml.xcepts.LibvirtXMLNotFoundError: LOG.debug("No element find in domain xml") return xml_affinity # Store xml_affinity_list to a dict for vcpu in xml_affinity_list: - xml_affinity[vcpu['vcpu']] = cpus_string_to_affinity_list(vcpu['cpuset'], - host_cpu_count) + xml_affinity[vcpu["vcpu"]] = cpus_string_to_affinity_list( + vcpu["cpuset"], host_cpu_count + ) return xml_affinity @@ -224,14 +264,20 @@ def affinity_from_vcpupin(vm, vcpu=None, options=None): """ vcpupin_output = {} vcpupin_affinity = {} - host_cpu_count = utils.total_count() if hasattr(utils, 'total_count') else utils.total_cpus_count() + host_cpu_count = ( + utils.total_count() + if hasattr(utils, "total_count") + else utils.total_cpus_count() + ) result = virsh.vcpupin(vm.name, vcpu=vcpu, options=options, debug=True) - for vcpu in result.stdout_text.strip().split('\n')[2:]: + for vcpu in result.stdout_text.strip().split("\n")[2:]: # On newer version of libvirt, there is no ':' in # vcpupin output anymore - vcpupin_output[int(vcpu.split()[0].rstrip(':'))] = vcpu.split()[1] + vcpupin_output[int(vcpu.split()[0].rstrip(":"))] = vcpu.split()[1] for vcpu in vcpupin_output: - vcpupin_affinity[vcpu] = cpus_string_to_affinity_list(vcpupin_output[vcpu], host_cpu_count) + vcpupin_affinity[vcpu] = cpus_string_to_affinity_list( + vcpupin_output[vcpu], host_cpu_count + ) return vcpupin_affinity @@ -246,7 +292,11 @@ def affinity_from_proc(vm): pid = vm.get_pid() proc_affinity = {} vcpu_pids = [] - host_cpu_count = utils.total_count() if hasattr(utils, 'total_count') else utils.total_cpus_count() + host_cpu_count = ( + utils.total_count() + if hasattr(utils, "total_count") + else utils.total_cpus_count() + ) vcpu_pids = vm.get_vcpus_pid() for vcpu in range(len(vcpu_pids)): output = cpu_allowed_list_by_task(pid, vcpu_pids[vcpu]) @@ -264,41 +314,44 @@ def get_vcpucount_details(vm, options): :return: tuple of result and dict of vcpucount output values """ - vcpucount_details = {'max_config': None, 'max_live': None, - 'cur_config': None, 'cur_live': None, - 'guest_live': None} + vcpucount_details = { + "max_config": None, + "max_live": None, + "cur_config": None, + "cur_live": None, + "guest_live": None, + } - result = virsh.vcpucount(vm.name, options, ignore_status=True, - debug=True) + result = virsh.vcpucount(vm.name, options, ignore_status=True, debug=True) if result.exit_status: LOG.debug("vcpu count command failed") return (result, vcpucount_details) if options: stdout = result.stdout_text.strip() - if 'guest' in options: - vcpucount_details['guest_live'] = int(stdout) - elif 'config' in options: - if 'maximum' in options: - vcpucount_details['max_config'] = int(stdout) + if "guest" in options: + vcpucount_details["guest_live"] = int(stdout) + elif "config" in options: + if "maximum" in options: + vcpucount_details["max_config"] = int(stdout) else: - vcpucount_details['cur_config'] = int(stdout) - elif 'live' in options: - if 'maximum' in options: - vcpucount_details['max_live'] = int(stdout) + vcpucount_details["cur_config"] = int(stdout) + elif "live" in options: + if "maximum" in options: + vcpucount_details["max_live"] = int(stdout) else: - vcpucount_details['cur_live'] = int(stdout) + vcpucount_details["cur_live"] = int(stdout) else: - output = result.stdout_text.strip().split('\n') + output = result.stdout_text.strip().split("\n") for item in output: - if ('maximum' in item) and ('config' in item): - vcpucount_details['max_config'] = int(item.split()[2].strip()) - elif ('maximum' in item) and ('live' in item): - vcpucount_details['max_live'] = int(item.split()[2].strip()) - elif ('current' in item) and ('config' in item): - vcpucount_details['cur_config'] = int(item.split()[2].strip()) - elif ('current' in item) and ('live' in item): - vcpucount_details['cur_live'] = int(item.split()[2].strip()) + if ("maximum" in item) and ("config" in item): + vcpucount_details["max_config"] = int(item.split()[2].strip()) + elif ("maximum" in item) and ("live" in item): + vcpucount_details["max_live"] = int(item.split()[2].strip()) + elif ("current" in item) and ("config" in item): + vcpucount_details["cur_config"] = int(item.split()[2].strip()) + elif ("current" in item) and ("live" in item): + vcpucount_details["cur_live"] = int(item.split()[2].strip()) else: pass return (result, vcpucount_details) @@ -314,14 +367,20 @@ def check_affinity(vm, expect_vcpupin): :return: True if affinity matches from different virsh API outputs, False if not """ - host_cpu_count = utils.total_count() if hasattr(utils, 'total_count') else utils.total_cpus_count() + host_cpu_count = ( + utils.total_count() + if hasattr(utils, "total_count") + else utils.total_cpus_count() + ) affinity_xml = affinity_from_xml(vm) affinity_vcpupin = affinity_from_vcpupin(vm) affinity_vcpuinfo = affinity_from_vcpuinfo(vm) result = True for vcpu in list(expect_vcpupin.keys()): - expect_affinity = cpus_string_to_affinity_list(str(expect_vcpupin[vcpu]), host_cpu_count) + expect_affinity = cpus_string_to_affinity_list( + str(expect_vcpupin[vcpu]), host_cpu_count + ) # Check for vcpuinfo affinity if affinity_vcpuinfo[int(vcpu)] != expect_affinity: LOG.error("CPU affinity in virsh vcpuinfo output is unexpected") @@ -360,26 +419,38 @@ def check_vcpucount(vm, exp_vcpu, option="", guest_agent=False): if vcresult.stderr_text: result = False if vcpucount_option == "--guest" and guest_agent: - if vcpucount_result['guest_live'] != exp_vcpu['guest_live']: - LOG.error("Virsh vcpucount output is unexpected\nExpected: " - "%s\nActual: %s", exp_vcpu, vcpucount_result) + if vcpucount_result["guest_live"] != exp_vcpu["guest_live"]: + LOG.error( + "Virsh vcpucount output is unexpected\nExpected: " "%s\nActual: %s", + exp_vcpu, + vcpucount_result, + ) result = False else: # Check for config option results if vm.is_dead(): - if (exp_vcpu['max_config'] != vcpucount_result['max_config'] or - exp_vcpu['cur_config'] != vcpucount_result['cur_config']): - LOG.error("Virsh vcpucount output is unexpected\nExpected" - ":%s\nActual:%s", exp_vcpu, vcpucount_result) + if ( + exp_vcpu["max_config"] != vcpucount_result["max_config"] + or exp_vcpu["cur_config"] != vcpucount_result["cur_config"] + ): + LOG.error( + "Virsh vcpucount output is unexpected\nExpected" ":%s\nActual:%s", + exp_vcpu, + vcpucount_result, + ) result = False else: - if (exp_vcpu['max_config'] != vcpucount_result['max_config'] or - exp_vcpu['max_live'] != vcpucount_result['max_live'] or - exp_vcpu['cur_config'] != vcpucount_result['cur_config'] or - exp_vcpu['cur_live'] != vcpucount_result['cur_live']): - LOG.error("Virsh vcpucount output is unexpected\n " - "Expected:%s\nActual:%s", exp_vcpu, - vcpucount_result) + if ( + exp_vcpu["max_config"] != vcpucount_result["max_config"] + or exp_vcpu["max_live"] != vcpucount_result["max_live"] + or exp_vcpu["cur_config"] != vcpucount_result["cur_config"] + or exp_vcpu["cur_live"] != vcpucount_result["cur_live"] + ): + LOG.error( + "Virsh vcpucount output is unexpected\n " "Expected:%s\nActual:%s", + exp_vcpu, + vcpucount_result, + ) result = False if result: LOG.debug("Command vcpucount check pass") @@ -398,15 +469,18 @@ def check_vcpuinfo(vm, exp_vcpu): result = True # Decide based on vm alive status to check actual vcpu count if vm.is_alive(): - idx = 'cur_live' + idx = "cur_live" else: - idx = 'cur_config' + idx = "cur_config" affinity_vcpuinfo = affinity_from_vcpuinfo(vm) vcpuinfo_num = len(affinity_vcpuinfo) if vcpuinfo_num != exp_vcpu[idx]: - LOG.error("Vcpu number in virsh vcpuinfo is unexpected\n" - "Expected: %s\nActual: %s", exp_vcpu[idx], vcpuinfo_num) + LOG.error( + "Vcpu number in virsh vcpuinfo is unexpected\n" "Expected: %s\nActual: %s", + exp_vcpu[idx], + vcpuinfo_num, + ) result = False else: LOG.debug("Command vcpuinfo check pass") @@ -430,17 +504,22 @@ def check_xmlcount(vm, exp_vcpu, option): exp_key = "cur_config" else: exp_key = "cur_live" - if cpu_xml['current_vcpu'] != exp_vcpu[exp_key]: - if cpu_xml['current_vcpu'] != exp_vcpu['cur_config']: - LOG.error("currrent vcpu number mismatch in xml\n" - "Expected: %s\nActual:%s", exp_vcpu[exp_key], - cpu_xml['current_vcpu']) + if cpu_xml["current_vcpu"] != exp_vcpu[exp_key]: + if cpu_xml["current_vcpu"] != exp_vcpu["cur_config"]: + LOG.error( + "currrent vcpu number mismatch in xml\n" "Expected: %s\nActual:%s", + exp_vcpu[exp_key], + cpu_xml["current_vcpu"], + ) result = False else: LOG.debug("current vcpu count in xml check pass") - if cpu_xml['vcpu'] != exp_vcpu['max_config']: - LOG.error("vcpu count mismatch in xml\nExpected: %s\nActual: %s", - exp_vcpu['max_config'], cpu_xml['vcpu']) + if cpu_xml["vcpu"] != exp_vcpu["max_config"]: + LOG.error( + "vcpu count mismatch in xml\nExpected: %s\nActual: %s", + exp_vcpu["max_config"], + cpu_xml["vcpu"], + ) result = False else: LOG.debug("vcpu count in xml check pass") @@ -457,8 +536,12 @@ def get_cpustats(vm, cpu=None): {0:[vcputime,emulatortime,cputime] .. 'total':[cputime]} - """ - host_cpu_online = utils.online_list() if hasattr(utils, 'online_list') else utils.cpu_online_list() + """ + host_cpu_online = ( + utils.online_list() + if hasattr(utils, "online_list") + else utils.cpu_online_list() + ) cpustats = {} if cpu: cpustats[cpu] = [] @@ -469,9 +552,11 @@ def get_cpustats(vm, cpu=None): return None output = result.stdout_text.strip().split() if re.match("CPU%s" % cpu, output[0]): - cpustats[cpu] = [float(output[5]), # vcputime - float(output[2]) - float(output[5]), # emulator - float(output[2])] # cputime + cpustats[cpu] = [ + float(output[5]), # vcputime + float(output[2]) - float(output[5]), # emulator + float(output[2]), + ] # cputime else: for i in range(len(host_cpu_online)): @@ -483,9 +568,11 @@ def get_cpustats(vm, cpu=None): return None output = result.stdout_text.strip().split() if re.match("CPU%s" % host_cpu_online[i], output[0]): - cpustats[host_cpu_online[i]] = [float(output[5]), - float(output[2]) - float(output[5]), - float(output[2])] + cpustats[host_cpu_online[i]] = [ + float(output[5]), + float(output[2]) - float(output[5]), + float(output[2]), + ] result = virsh.cpu_stats(vm.name, "--total") cpustats["total"] = [] if result.exit_status != 0: @@ -520,25 +607,32 @@ def check_vcpu_domstats(vm, exp_vcpu): cur_vcpu = int(get_domstats(vm, "vcpu.current")) max_vcpu = int(get_domstats(vm, "vcpu.maximum")) if vm.is_alive(): - exp_cur_vcpu = exp_vcpu['cur_live'] - exp_cur_max = exp_vcpu['max_live'] + exp_cur_vcpu = exp_vcpu["cur_live"] + exp_cur_max = exp_vcpu["max_live"] else: - exp_cur_vcpu = exp_vcpu['cur_config'] - exp_cur_max = exp_vcpu['max_config'] + exp_cur_vcpu = exp_vcpu["cur_config"] + exp_cur_max = exp_vcpu["max_config"] if exp_cur_vcpu != cur_vcpu: status = False - LOG.error("Mismatch in current vcpu in domstats output, " - "Expected: %s Actual: %s", exp_cur_vcpu, cur_vcpu) + LOG.error( + "Mismatch in current vcpu in domstats output, " "Expected: %s Actual: %s", + exp_cur_vcpu, + cur_vcpu, + ) if exp_cur_max != max_vcpu: status = False - LOG.error("Mismatch in maximum vcpu in domstats output, Expected:" - " %s Actual: %s", exp_cur_max, max_vcpu) + LOG.error( + "Mismatch in maximum vcpu in domstats output, Expected:" " %s Actual: %s", + exp_cur_max, + max_vcpu, + ) return status -def check_vcpu_value(vm, exp_vcpu, vcpupin=None, option="", guest_agent=False, - check_numa=True): +def check_vcpu_value( + vm, exp_vcpu, vcpupin=None, option="", guest_agent=False, check_numa=True +): """ Check domain vcpu, including vcpucount, vcpuinfo, vcpupin, vcpu number and cputune in domain xml, vcpu number inside the domain. @@ -583,11 +677,15 @@ def check_vcpu_value(vm, exp_vcpu, vcpupin=None, option="", guest_agent=False, final_result = False if vm.is_alive() and (not vm.is_paused()) and "live" in option: - vcpu_hotplug_timeout = 120 # maximum time to wait for a hotplug event to complete + vcpu_hotplug_timeout = ( + 120 # maximum time to wait for a hotplug event to complete + ) # 1.5 Check inside the guest - if not utils_misc.wait_for(lambda: check_if_vm_vcpu_match(exp_vcpu['guest_live'], - vm), - vcpu_hotplug_timeout, text="wait for vcpu online"): + if not utils_misc.wait_for( + lambda: check_if_vm_vcpu_match(exp_vcpu["guest_live"], vm), + vcpu_hotplug_timeout, + text="wait for vcpu online", + ): logging.debug("check_if_vm_vcpu_match failed") final_result = False # 1.6 Check guest numa @@ -643,7 +741,7 @@ def guest_numa_check(vm, exp_vcpu): status = True for node in range(node_num_xml): try: - node_cpu_xml = vmxml.cpu.numa_cell[node]['cpus'] + node_cpu_xml = vmxml.cpu.numa_cell[node]["cpus"] node_cpu_xml = cpus_parser(node_cpu_xml) except (TypeError, libvirt_xml.xcepts.LibvirtXMLNotFoundError): try: @@ -652,7 +750,7 @@ def guest_numa_check(vm, exp_vcpu): node_cpu_xml = vmxml.vcpu node_cpu_xml = list(range(int(node_cpu_xml))) try: - node_mem_xml = vmxml.cpu.numa_cell[node]['memory'] + node_mem_xml = vmxml.cpu.numa_cell[node]["memory"] except (TypeError, libvirt_xml.xcepts.LibvirtXMLNotFoundError): node_mem_xml = vmxml.memory node_mem_guest = int(vm.get_totalmem_sys(node=node)) @@ -670,18 +768,29 @@ def guest_numa_check(vm, exp_vcpu): # Check cpu if node_cpu_xml != node_cpu_guest: status = False - LOG.error("Mismatch in cpus in node %s: xml %s guest %s", node, - node_cpu_xml, node_cpu_guest) + LOG.error( + "Mismatch in cpus in node %s: xml %s guest %s", + node, + node_cpu_xml, + node_cpu_guest, + ) # Check memory if int(node_mem_xml) != node_mem_guest: status = False - LOG.error("Mismatch in memory in node %s: xml %s guest %s", node, - node_mem_xml, node_mem_guest) + LOG.error( + "Mismatch in memory in node %s: xml %s guest %s", + node, + node_mem_xml, + node_mem_guest, + ) # Check no. of nodes if exp_num_nodes != node_num_guest: status = False - LOG.error("Mismatch in numa nodes expected nodes: %s guest: %s", - exp_num_nodes, node_num_guest) + LOG.error( + "Mismatch in numa nodes expected nodes: %s guest: %s", + exp_num_nodes, + node_num_guest, + ) return status @@ -749,12 +858,13 @@ def get_cpu_info(session=None): cpu_info = {} cmd = "lscpu | tee" if session is None: - output = process.run(cmd, shell=True, - ignore_status=True).stdout_text.splitlines() + output = process.run( + cmd, shell=True, ignore_status=True + ).stdout_text.splitlines() else: try: output_raw = session.cmd_output(cmd) - output = re.sub('\n[\s]+', '', output_raw).splitlines() + output = re.sub("\n[\s]+", "", output_raw).splitlines() LOG.info("output is %s" % output) finally: session.close() @@ -767,6 +877,7 @@ class Flag(str): """ Class for easy merge cpuflags. """ + aliases = {} def __new__(cls, flag): @@ -793,38 +904,38 @@ def __hash__(self, *args, **kwargs): kvm_map_flags_to_test = { - Flag('avx'): set(['avx']), - Flag('sse3|pni'): set(['sse3']), - Flag('ssse3'): set(['ssse3']), - Flag('sse4.1|sse4_1|sse4.2|sse4_2'): set(['sse4']), - Flag('aes'): set(['aes', 'pclmul']), - Flag('pclmuldq'): set(['pclmul']), - Flag('pclmulqdq'): set(['pclmul']), - Flag('rdrand'): set(['rdrand']), - Flag('sse4a'): set(['sse4a']), - Flag('fma4'): set(['fma4']), - Flag('xop'): set(['xop']), + Flag("avx"): set(["avx"]), + Flag("sse3|pni"): set(["sse3"]), + Flag("ssse3"): set(["ssse3"]), + Flag("sse4.1|sse4_1|sse4.2|sse4_2"): set(["sse4"]), + Flag("aes"): set(["aes", "pclmul"]), + Flag("pclmuldq"): set(["pclmul"]), + Flag("pclmulqdq"): set(["pclmul"]), + Flag("rdrand"): set(["rdrand"]), + Flag("sse4a"): set(["sse4a"]), + Flag("fma4"): set(["fma4"]), + Flag("xop"): set(["xop"]), } kvm_map_flags_aliases = { - 'sse4_1': 'sse4.1', - 'sse4_2': 'sse4.2', - 'pclmuldq': 'pclmulqdq', - 'sse3': 'pni', - 'ffxsr': 'fxsr_opt', - 'xd': 'nx', - 'i64': 'lm', - 'psn': 'pn', - 'clfsh': 'clflush', - 'dts': 'ds', - 'htt': 'ht', - 'CMPXCHG8B': 'cx8', - 'Page1GB': 'pdpe1gb', - 'LahfSahf': 'lahf_lm', - 'ExtApicSpace': 'extapic', - 'AltMovCr8': 'cr8_legacy', - 'cr8legacy': 'cr8_legacy' + "sse4_1": "sse4.1", + "sse4_2": "sse4.2", + "pclmuldq": "pclmulqdq", + "sse3": "pni", + "ffxsr": "fxsr_opt", + "xd": "nx", + "i64": "lm", + "psn": "pn", + "clfsh": "clflush", + "dts": "ds", + "htt": "ht", + "CMPXCHG8B": "cx8", + "Page1GB": "pdpe1gb", + "LahfSahf": "lahf_lm", + "ExtApicSpace": "extapic", + "AltMovCr8": "cr8_legacy", + "cr8legacy": "cr8_legacy", } @@ -871,7 +982,7 @@ def get_cpu_vendor(cpu_info="", verbose=True): fd.close() vendor = re.findall(vendor_re, cpu_info) if not vendor: - vendor = 'unknown' + vendor = "unknown" else: vendor = vendor[0] if verbose: @@ -886,10 +997,10 @@ def get_recognized_cpuid_flags(qemu_binary="/usr/libexec/qemu-kvm"): :param qemu_binary: qemu-kvm binary file path :return: flags list """ - out = process.run("%s -cpu ?" % qemu_binary).stdout.decode(errors='replace') + out = process.run("%s -cpu ?" % qemu_binary).stdout.decode(errors="replace") match = re.search("Recognized CPUID flags:(.*)", out, re.M | re.S) try: - return list(filter(None, re.split('\s', match.group(1)))) + return list(filter(None, re.split("\s", match.group(1)))) except AttributeError: pass return [] @@ -899,6 +1010,7 @@ def get_host_cpu_models(): """ Get cpu model from host cpuinfo """ + def _cpu_flags_sort(cpu_flags): """ Update the cpu flags get from host to a certain order and format @@ -917,8 +1029,8 @@ def _make_up_pattern(flags): pattern += r".+(\b%s\b)" % i return pattern - if ARCH in ('ppc64', 'ppc64le'): - return [] # remove -cpu and leave it on qemu to decide + if ARCH in ("ppc64", "ppc64le"): + return [] # remove -cpu and leave it on qemu to decide fd = open("/proc/cpuinfo") cpu_info = fd.read() @@ -949,6 +1061,7 @@ def extract_qemu_cpu_models(qemu_cpu_help_text): :param qemu_cpu_help_text: text produced by -cpu '?' :return: list of cpu models """ + def check_model_list(pattern): cpu_re = re.compile(pattern) qemu_cpu_model_list = cpu_re.findall(qemu_cpu_help_text) @@ -966,8 +1079,10 @@ def check_model_list(pattern): if model_list is not None: return model_list - e_msg = ("CPU models reported by qemu -cpu ? not supported by avocado-vt. " - "Please work with us to add support for it") + e_msg = ( + "CPU models reported by qemu -cpu ? not supported by avocado-vt. " + "Please work with us to add support for it" + ) LOG.error(e_msg) for line in qemu_cpu_help_text.splitlines(): LOG.error(line) @@ -987,17 +1102,19 @@ def check_if_vm_vcpu_match(vcpu_desire, vm, connect_uri=None, session=None): :return: Boolean, True if actual vcpu value matches with vcpu_desire """ release = vm.get_distro(connect_uri=connect_uri) - if release and release in ['fedora', ]: - vcpu_actual = vm.get_cpu_count("cpu_chk_all_cmd", - connect_uri=connect_uri) + if release and release in [ + "fedora", + ]: + vcpu_actual = vm.get_cpu_count("cpu_chk_all_cmd", connect_uri=connect_uri) else: - vcpu_actual = vm.get_cpu_count("cpu_chk_cmd", - connect_uri=connect_uri) + vcpu_actual = vm.get_cpu_count("cpu_chk_cmd", connect_uri=connect_uri) if isinstance(vcpu_desire, str) and vcpu_desire.isdigit(): vcpu_desire = int(vcpu_desire) if vcpu_desire != vcpu_actual: - LOG.debug("CPU quantity mismatched !!! guest said it got %s " - "but we assigned %s" % (vcpu_actual, vcpu_desire)) + LOG.debug( + "CPU quantity mismatched !!! guest said it got %s " + "but we assigned %s" % (vcpu_actual, vcpu_desire) + ) return False LOG.info("CPU quantity matched: %s" % vcpu_actual) return True @@ -1020,19 +1137,19 @@ def get_model_features(model_name): try: if not libvirt_version.version_compare(5, 0, 0): - with open(conf, 'r') as output: + with open(conf, "r") as output: root = ET.fromstring(output.read()) while True: # Find model in file /usr/share/libvirt/cpu_map.xml - for model_n in root.findall('arch/model'): - if model_n.get('name') == model_name: + for model_n in root.findall("arch/model"): + if model_n.get("name") == model_name: model_node = model_n - for feature in model_n.findall('feature'): - features.append(feature.get('name')) + for feature in model_n.findall("feature"): + features.append(feature.get("name")) break # Handle nested model - if model_node.find('model') is not None: - model_name = model_node.find('model').get('name') + if model_node.find("model") is not None: + model_name = model_node.find("model").get("name") continue else: break @@ -1045,7 +1162,7 @@ def get_model_features(model_name): with open(os.path.join(conf_dir, file_name), "r") as output: model = ET.fromstring(output.read()) for feature in model.findall("model/feature"): - features.append(feature.get('name')) + features.append(feature.get("name")) break except ET.ParseError as error: LOG.warn("Configuration file %s has wrong xml format" % conf) @@ -1056,8 +1173,10 @@ def get_model_features(model_name): except Exception: # Other exceptions like IOError when open/read configuration file, # capture here - LOG.warn("Some other exceptions, like configuration file is not " - "found or not file: %s" % conf) + LOG.warn( + "Some other exceptions, like configuration file is not " + "found or not file: %s" % conf + ) raise return features @@ -1079,16 +1198,16 @@ def cpus_string_to_affinity_list(cpus_string, num_cpus): single_pattern = r"\d+" between_pattern = r"\d+-\d+" exclude_pattern = r"\^\d+" - sub_pattern = r"(%s)|(%s)|(%s)" % (exclude_pattern, - single_pattern, between_pattern) + sub_pattern = r"(%s)|(%s)|(%s)" % (exclude_pattern, single_pattern, between_pattern) pattern = r"^((%s),)*(%s)$" % (sub_pattern, sub_pattern) if not re.match(pattern, cpus_string): - LOG.debug("Cpus_string=%s is not a supported format for cpu_list." - % cpus_string) + LOG.debug( + "Cpus_string=%s is not a supported format for cpu_list." % cpus_string + ) # Init a list for result. affinity = [] for i in range(int(num_cpus)): - affinity.append('-') + affinity.append("-") # Letter 'r' means all cpus. if cpus_string == "r": for i in range(len(affinity)): @@ -1115,7 +1234,9 @@ def cpu_allowed_list_by_task(pid, tid): Get the Cpus_allowed_list in status of task. """ cmd = "cat /proc/%s/task/%s/status|grep Cpus_allowed_list:| awk '{print $2}'" % ( - pid, tid) + pid, + tid, + ) result = process.run(cmd, ignore_status=True, shell=True) if result.exit_status: return None @@ -1162,7 +1283,10 @@ def hotplug_domain_vcpu(vm, count, by_virsh=True, hotplug=True): for item in range(0, int(count), threads): if item < vcpu_count: continue - cmds.append("device_add host-spapr-cpu-core,id=core%d,core-id=%d" % (item, item)) + cmds.append( + "device_add host-spapr-cpu-core,id=core%d,core-id=%d" + % (item, item) + ) else: for item in range(int(count), vcpu_count, threads): cmds.append("device_del core%d" % item) @@ -1184,11 +1308,10 @@ def hotplug_domain_vcpu(vm, count, by_virsh=True, hotplug=True): # so, the caller should check the result. # hot-plug/hot-plug the CPU has maximal ID params = (cpu_opt, (count - 1)) - cmds.append('{\"execute\":\"%s\",\"arguments\":{\"id\":%d}}' % params) + cmds.append('{"execute":"%s","arguments":{"id":%d}}' % params) # Execute cmds to hot(un)plug for cmd in cmds: - result = virsh.qemu_monitor_command(vm.name, cmd, cmd_type, - debug=True) + result = virsh.qemu_monitor_command(vm.name, cmd, cmd_type, debug=True) if result.exit_status != 0: raise exceptions.TestFail(result.stderr_text) else: @@ -1224,8 +1347,7 @@ def cpus_parser(cpulist): try: commas.append(int(cpulist)) except ValueError: - LOG.error("The cpulist has to be an " - "integer. (%s)", cpulist) + LOG.error("The cpulist has to be an " "integer. (%s)", cpulist) elif "-" in cpulist: tmp = re.split("-", cpulist) hyphens = list(range(int(tmp[0]), int(tmp[-1]) + 1)) @@ -1318,7 +1440,7 @@ def get_cpu_info_from_virsh_qemu_cli(name): cmd = "ps aux | grep qemu | grep -e '\s-name\s\+guest=%s\(\,\|\s\)'" % name output = process.getoutput(cmd, shell=True) cpu_line = re.findall("-cpu\s+(\S*)\s+", output)[0] - cpu_cmd = cpu_line.split(',', 1) + cpu_cmd = cpu_line.split(",", 1) cpu_info = {} cpu_info["model"] = cpu_cmd[0] cpu_info["flags"] = "" if len(cpu_cmd) < 2 else cpu_cmd[1] @@ -1330,9 +1452,9 @@ def get_cpu_info_from_virsh(params): Try to get cpu model and features from virsh with 'host-model' """ vm_arch = params["vm_arch_name"] - if vm_arch not in ['x86_64', 'i686']: + if vm_arch not in ["x86_64", "i686"]: raise NotImplementedError("Arch '%s' is not supported" % vm_arch) - machine = params.get('machine_type') + machine = params.get("machine_type") name = uuid.uuid4().hex try: path.find_command("virsh") @@ -1349,7 +1471,11 @@ def get_cpu_info_from_virsh(params): - """ % (name, vm_arch, machine) + """ % ( + name, + vm_arch, + machine, + ) xml_file = os.path.join(data_dir.get_tmp_dir(), "temp_xml_for_cpu") with open(xml_file, "w") as f: f.write(xml) diff --git a/virttest/curl.py b/virttest/curl.py index 86dd64eedd..cfe714579c 100644 --- a/virttest/curl.py +++ b/virttest/curl.py @@ -4,8 +4,9 @@ @error_context.context_aware -def get_image_filename(curl_protocol, curl_server, curl_path, - curl_user=None, curl_passwd=None): +def get_image_filename( + curl_protocol, curl_server, curl_path, curl_user=None, curl_passwd=None +): """ Form the url: ://[[:]@]/ @@ -18,17 +19,17 @@ def get_image_filename(curl_protocol, curl_server, curl_path, """ url = "{protocol}://{auth}{host}/{path}" - protocols = ('http', 'https', 'ftp', 'ftps') + protocols = ("http", "https", "ftp", "ftps") if curl_protocol not in protocols: - raise ValueError('curl_protocol should be in %s.' % str(protocols)) + raise ValueError("curl_protocol should be in %s." % str(protocols)) - auth = '' + auth = "" if curl_user: - auth = '%s:%s@' % (curl_user, - curl_passwd) if curl_passwd else '%s@' % curl_user + auth = "%s:%s@" % (curl_user, curl_passwd) if curl_passwd else "%s@" % curl_user - return url.format(protocol=curl_protocol, auth=auth, - host=curl_server, path=curl_path) + return url.format( + protocol=curl_protocol, auth=auth, host=curl_server, path=curl_path + ) @error_context.context_aware @@ -40,8 +41,9 @@ def file_exists(params, filename): :param filename: The image filename :return: True if the image exists, else False """ - curl_cmd = 'curl -I -L -k {tmo} {filename}' - t = '-m %s' % params['curl_timeout'] if params.get('curl_timeout') else '' - o = process.run(curl_cmd.format(tmo=t, filename=filename), - ignore_status=True, verbose=True).stdout_text.strip() - return 'Content-Length:' in o + curl_cmd = "curl -I -L -k {tmo} {filename}" + t = "-m %s" % params["curl_timeout"] if params.get("curl_timeout") else "" + o = process.run( + curl_cmd.format(tmo=t, filename=filename), ignore_status=True, verbose=True + ).stdout_text.strip() + return "Content-Length:" in o diff --git a/virttest/data_dir.py b/virttest/data_dir.py index f524d87446..e76b2fedba 100755 --- a/virttest/data_dir.py +++ b/virttest/data_dir.py @@ -19,18 +19,18 @@ from six.moves import xrange -BASE_BACKEND_DIR = pkg_resources.resource_filename('virttest', 'backends') -TEST_PROVIDERS_DIR = pkg_resources.resource_filename('virttest', 'test-providers.d') -SHARED_DIR = pkg_resources.resource_filename('virttest', 'shared') -DEPS_DIR = os.path.join(SHARED_DIR, 'deps') -BASE_DOWNLOAD_DIR = os.path.join(SHARED_DIR, 'downloads') - -DATA_DIR = os.path.join(data_dir.get_data_dir(), 'avocado-vt') -DOWNLOAD_DIR = os.path.join(DATA_DIR, 'downloads') +BASE_BACKEND_DIR = pkg_resources.resource_filename("virttest", "backends") +TEST_PROVIDERS_DIR = pkg_resources.resource_filename("virttest", "test-providers.d") +SHARED_DIR = pkg_resources.resource_filename("virttest", "shared") +DEPS_DIR = os.path.join(SHARED_DIR, "deps") +BASE_DOWNLOAD_DIR = os.path.join(SHARED_DIR, "downloads") + +DATA_DIR = os.path.join(data_dir.get_data_dir(), "avocado-vt") +DOWNLOAD_DIR = os.path.join(DATA_DIR, "downloads") BACKING_DATA_DIR = None -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class MissingDepsDirError(Exception): @@ -38,14 +38,14 @@ class MissingDepsDirError(Exception): class UnknownBackendError(Exception): - def __init__(self, backend): self.backend = backend def __str__(self): - return ("Virt Backend %s is not currently supported by avocado-vt. " - "Check for typos and the list of supported backends" % - self.backend) + return ( + "Virt Backend %s is not currently supported by avocado-vt. " + "Check for typos and the list of supported backends" % self.backend + ) class SubdirList(list): @@ -69,7 +69,7 @@ def __set_initset__(self): # Don't modify list while in use del_list = [] for _dirname in dirnames: - if _dirname.startswith('.') or self.__in_filter__(_dirname): + if _dirname.startswith(".") or self.__in_filter__(_dirname): # Don't descend into filtered or hidden directories del_list.append(_dirname) else: @@ -134,7 +134,7 @@ def get_base_backend_dir(): def get_local_backend_dir(): - return os.path.join(get_data_dir(), 'backends') + return os.path.join(get_data_dir(), "backends") def get_backend_dir(backend_type): @@ -144,7 +144,7 @@ def get_backend_dir(backend_type): def get_backend_cfg_path(backend_type, cfg_basename): - return os.path.join(get_backend_dir(backend_type), 'cfg', cfg_basename) + return os.path.join(get_backend_dir(backend_type), "cfg", cfg_basename) def get_deps_dir(target=None): @@ -172,26 +172,28 @@ def get_deps_dir(target=None): for index in xrange(nesting_limit): files = os.listdir(path) origin_path = "" - if 'shared' in files: + if "shared" in files: origin_path = path - path = os.path.join(path, 'shared') + path = os.path.join(path, "shared") files = os.listdir(path) - if 'deps' in files: - deps = os.path.join(path, 'deps') + if "deps" in files: + deps = os.path.join(path, "deps") if target: if target in os.listdir(deps): return os.path.join(deps, target) else: return deps - if '.git' in os.listdir(path): - raise MissingDepsDirError("Could not find specified deps dir for " - "git repo %s" % path) + if ".git" in os.listdir(path): + raise MissingDepsDirError( + "Could not find specified deps dir for " "git repo %s" % path + ) if origin_path: path = origin_path path = os.path.dirname(path) - raise MissingDepsDirError("Could not find specified deps dir after " - "looking %s parent directories" % - nesting_limit) + raise MissingDepsDirError( + "Could not find specified deps dir after " + "looking %s parent directories" % nesting_limit + ) def get_tmp_dir(public=True): @@ -200,24 +202,32 @@ def get_tmp_dir(public=True): :param public: If public for all users' access """ - persistent_dir = get_settings_value('vt.common', 'tmp_dir', - default="") + persistent_dir = get_settings_value("vt.common", "tmp_dir", default="") if persistent_dir != "": return persistent_dir tmp_dir = None # apparmor deny /tmp/* /var/tmp/* and cause failure across tests # it is better to handle here - if distro.detect().name == 'Ubuntu': + if distro.detect().name == "Ubuntu": tmp_dir = "/var/lib/libvirt/images" if not utils_path.usable_rw_dir(tmp_dir): - LOG.warning("Unable to write in '/var/lib/libvirt/images' " - "on Ubuntu, apparmor might complain...") + LOG.warning( + "Unable to write in '/var/lib/libvirt/images' " + "on Ubuntu, apparmor might complain..." + ) tmp_dir = None tmp_dir = data_dir.get_tmp_dir(basedir=tmp_dir) if public: tmp_dir_st = os.stat(tmp_dir) - os.chmod(tmp_dir, tmp_dir_st.st_mode | stat.S_IXUSR | - stat.S_IXGRP | stat.S_IXOTH | stat.S_IRGRP | stat.S_IROTH) + os.chmod( + tmp_dir, + tmp_dir_st.st_mode + | stat.S_IXUSR + | stat.S_IXGRP + | stat.S_IXOTH + | stat.S_IRGRP + | stat.S_IROTH, + ) return tmp_dir @@ -229,10 +239,9 @@ def get_download_dir(): return DOWNLOAD_DIR -TEST_PROVIDERS_DOWNLOAD_DIR = os.path.join(get_data_dir(), - 'virttest', - 'test-providers.d', - 'downloads') +TEST_PROVIDERS_DOWNLOAD_DIR = os.path.join( + get_data_dir(), "virttest", "test-providers.d", "downloads" +) def get_base_test_providers_dir(): @@ -262,7 +271,7 @@ def clean_tmp_files(): shutil.rmtree(path, ignore_errors=True) -if __name__ == '__main__': +if __name__ == "__main__": print("root dir: " + get_root_dir()) print("tmp dir: " + get_tmp_dir()) print("data dir: " + DATA_DIR) diff --git a/virttest/defaults.py b/virttest/defaults.py index b8cb3d289c..c171ef9de1 100644 --- a/virttest/defaults.py +++ b/virttest/defaults.py @@ -1,10 +1,11 @@ import platform + ARCH = platform.machine() DEFAULT_MACHINE_TYPE = None -if ARCH in ('ppc64', 'ppc64le'): +if ARCH in ("ppc64", "ppc64le"): DEFAULT_MACHINE_TYPE = "pseries" -elif ARCH in ('x86_64', 'i386'): +elif ARCH in ("x86_64", "i386"): DEFAULT_MACHINE_TYPE = "i440fx" else: # TODO: Handle other supported archs @@ -16,12 +17,11 @@ def get_default_guest_os_info(): Gets the default asset and variant information TODO: Check for the ARCH and choose corresponding default asset """ - default_asset = 'jeos-27-' - default_asset = '%s%s' % (default_asset, ARCH) - return {'asset': default_asset, 'variant': 'JeOS.27'} + default_asset = "jeos-27-" + default_asset = "%s%s" % (default_asset, ARCH) + return {"asset": default_asset, "variant": "JeOS.27"} -DEFAULT_GUEST_OS = get_default_guest_os_info()['variant'] +DEFAULT_GUEST_OS = get_default_guest_os_info()["variant"] -__all__ = ['DEFAULT_MACHINE_TYPE', 'DEFAULT_GUEST_OS', - 'get_default_guest_os_info'] +__all__ = ["DEFAULT_MACHINE_TYPE", "DEFAULT_GUEST_OS", "get_default_guest_os_info"] diff --git a/virttest/env_process.py b/virttest/env_process.py index 08128e73c4..01cad29831 100644 --- a/virttest/env_process.py +++ b/virttest/env_process.py @@ -55,6 +55,7 @@ # lazy imports for dependencies that are not needed in all modes of use from virttest._wrappers import lazy_import + utils_libvirtd = lazy_import("virttest.utils_libvirtd") virsh = lazy_import("virttest.virsh") libvirt_vm = lazy_import("virttest.libvirt_vm") @@ -63,10 +64,11 @@ try: import PIL.Image except ImportError: - logging.getLogger('avocado.app').warning( - 'No python imaging library installed. PPM image conversion to JPEG ' - 'disabled. In order to enable it, please install python-imaging or the ' - 'equivalent for your distro.') + logging.getLogger("avocado.app").warning( + "No python imaging library installed. PPM image conversion to JPEG " + "disabled. In order to enable it, please install python-imaging or the " + "equivalent for your distro." + ) _screendump_thread = None _screendump_thread_termination_event = None @@ -97,7 +99,7 @@ THREAD_ERROR = False -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) def _get_qemu_version(qemu_cmd): @@ -106,9 +108,8 @@ def _get_qemu_version(qemu_cmd): :param qemu_cmd: Path to qemu binary """ - version_output = a_process.run("%s -version" % qemu_cmd, - verbose=False).stdout_text - version_line = version_output.split('\n')[0] + version_output = a_process.run("%s -version" % qemu_cmd, verbose=False).stdout_text + version_line = version_output.split("\n")[0] matches = re.match(QEMU_VERSION_RE, version_line) if matches: return "%s (%s)" % matches.groups() @@ -131,14 +132,14 @@ def preprocess_image(test, params, image_name, vm_process_status=None): if not storage.preprocess_image_backend(base_dir, params, image_name): LOG.error("Backend can't be prepared correctly.") - image_filename = storage.get_image_filename(params, - base_dir) + image_filename = storage.get_image_filename(params, base_dir) create_image = False if params.get("force_create_image") == "yes": create_image = True - elif (params.get("create_image") == "yes" and not - storage.file_exists(params, image_filename)): + elif params.get("create_image") == "yes" and not storage.file_exists( + params, image_filename + ): create_image = True if params.get("backup_image_before_testing", "no") == "yes": @@ -166,12 +167,12 @@ def preprocess_fs_source(test, params, fs_name, vm_process_status=None): Add it here only for keep it work with process_fs_sources() """ - fs_type = params.get('fs_source_type', 'mount') - fs_source_user_config = params.get('fs_source_user_config', 'no') + fs_type = params.get("fs_source_type", "mount") + fs_source_user_config = params.get("fs_source_user_config", "no") # mount: A host directory to mount in the vm. - if fs_type == 'mount': + if fs_type == "mount": if fs_source_user_config == "no": - fs_source = params.get('fs_source_dir') + fs_source = params.get("fs_source_dir") base_dir = params.get("fs_source_base_dir", data_dir.get_data_dir()) if not os.path.isabs(fs_source): fs_source = os.path.join(base_dir, fs_source) @@ -179,7 +180,9 @@ def preprocess_fs_source(test, params, fs_name, vm_process_status=None): create_fs_source = False if params.get("force_create_fs_source") == "yes": create_fs_source = True - elif params.get("create_fs_source") == "yes" and not os.path.exists(fs_source): + elif params.get("create_fs_source") == "yes" and not os.path.exists( + fs_source + ): create_fs_source = True if create_fs_source: @@ -202,16 +205,16 @@ def preprocess_vm(test, params, env, name): :param name: The name of the VM object. """ vm = env.get_vm(name) - vm_type = params.get('vm_type') - connect_uri = params.get('connect_uri') - target = params.get('target') + vm_type = params.get("vm_type") + connect_uri = params.get("connect_uri") + target = params.get("target") create_vm = False if not vm or not isinstance(vm, virt_vm.BaseVM.lookup_vm_class(vm_type, target)): create_vm = True - elif vm_type == 'libvirt': + elif vm_type == "libvirt": connect_uri = libvirt_vm.normalize_connect_uri(connect_uri) - if (not vm.connect_uri == connect_uri): + if not vm.connect_uri == connect_uri: create_vm = True else: pass @@ -226,33 +229,39 @@ def preprocess_vm(test, params, env, name): nested_cmdline = params.get("virtinstall_qemu_cmdline", "") # virt-install doesn't have option, so use qemu-cmdline if "cap-nested-hv=on" not in nested_cmdline: - params["virtinstall_qemu_cmdline"] = ("%s -M %s,cap-nested-hv=on" % - (nested_cmdline, - params["machine_type"])) + params[ + "virtinstall_qemu_cmdline" + ] = "%s -M %s,cap-nested-hv=on" % ( + nested_cmdline, + params["machine_type"], + ) elif params.get("vm_type") == "qemu": nested_cmdline = params.get("machine_type_extra_params", "") if "cap-nested-hv=on" not in nested_cmdline: - params["machine_type_extra_params"] = ("%s,cap-nested-hv=on" % - nested_cmdline) + params["machine_type_extra_params"] = ( + "%s,cap-nested-hv=on" % nested_cmdline + ) vm = env.create_vm(vm_type, target, name, params, test.bindir) - if params.get("create_vm_libvirt") == "yes" and vm_type == 'libvirt': + if params.get("create_vm_libvirt") == "yes" and vm_type == "libvirt": params["medium"] = "import" vm.create(params=params) old_vm = copy.copy(vm) - if vm_type == 'libvirt': - install_test = ('unattended_install.import.import.default_install.' - 'aio_native') - remove_test = 'remove_guest.without_disk' - if not vm.exists() and (params.get("type") != "unattended_install" and - params.get("type") != "svirt_virt_install"): + if vm_type == "libvirt": + install_test = "unattended_install.import.import.default_install." "aio_native" + remove_test = "remove_guest.without_disk" + if not vm.exists() and ( + params.get("type") != "unattended_install" + and params.get("type") != "svirt_virt_install" + ): error_msg = "Test VM %s does not exist." % name if name == params.get("main_vm"): - error_msg += (" Consider adding '%s' test as the first one " - "and '%s' test as last one to remove the " - "guest after testing" % - (install_test, remove_test)) + error_msg += ( + " Consider adding '%s' test as the first one " + "and '%s' test as last one to remove the " + "guest after testing" % (install_test, remove_test) + ) raise exceptions.TestError(error_msg) else: raise exceptions.TestSkipError(error_msg) @@ -272,16 +281,14 @@ def preprocess_vm(test, params, env, name): start_vm = True elif params.get("start_vm") == "yes": # need to deal with libvirt VM differently than qemu - if vm_type == 'libvirt' or vm_type == 'v2v': + if vm_type == "libvirt" or vm_type == "v2v": if not vm.is_alive(): start_vm = True else: if not vm.is_alive(): start_vm = True if params.get("check_vm_needs_restart", "yes") == "yes": - if vm.needs_restart(name=name, - params=params, - basedir=test.bindir): + if vm.needs_restart(name=name, params=params, basedir=test.bindir): vm.devices = None start_vm = True old_vm.destroy(gracefully=gracefully_kill) @@ -302,16 +309,22 @@ def preprocess_vm(test, params, env, name): if vm.is_alive(): vm.destroy(free_mac_addresses=False) if params.get("reuse_previous_config", "no") == "no": - vm.create(name, params, test.bindir, - timeout=int(params.get("vm_create_timeout", 90)), - migration_mode=params.get("migration_mode"), - migration_fd=params.get("migration_fd"), - migration_exec_cmd=params.get("migration_exec_cmd_dst")) + vm.create( + name, + params, + test.bindir, + timeout=int(params.get("vm_create_timeout", 90)), + migration_mode=params.get("migration_mode"), + migration_fd=params.get("migration_fd"), + migration_exec_cmd=params.get("migration_exec_cmd_dst"), + ) else: - vm.create(timeout=int(params.get("vm_create_timeout", 90)), - migration_mode=params.get("migration_mode"), - migration_fd=params.get("migration_fd"), - migration_exec_cmd=params.get("migration_exec_cmd_dst")) + vm.create( + timeout=int(params.get("vm_create_timeout", 90)), + migration_mode=params.get("migration_mode"), + migration_fd=params.get("migration_fd"), + migration_exec_cmd=params.get("migration_exec_cmd_dst"), + ) # Update kernel param serial_login = params.get_boolean("kernel_extra_params_serial_login") @@ -321,15 +334,20 @@ def preprocess_vm(test, params, env, name): disable_pci_msi = params.get("disable_pci_msi") if disable_pci_msi == "yes": if "pci=" in kernel_extra_params_add: - kernel_extra_params_add = re.sub("pci=.*?\s+", "pci=nomsi ", - kernel_extra_params_add) + kernel_extra_params_add = re.sub( + "pci=.*?\s+", "pci=nomsi ", kernel_extra_params_add + ) else: kernel_extra_params_add += " pci=nomsi" params["ker_remove_similar_pci"] = "yes" else: kernel_extra_params_remove += " pci=nomsi" - vendor = cpu_utils.get_vendor() if hasattr(cpu_utils, 'get_vendor') else cpu_utils.get_cpu_vendor_name() - if (params.get("enable_guest_iommu") and vendor == 'intel'): + vendor = ( + cpu_utils.get_vendor() + if hasattr(cpu_utils, "get_vendor") + else cpu_utils.get_cpu_vendor_name() + ) + if params.get("enable_guest_iommu") and vendor == "intel": enable_guest_iommu = params.get("enable_guest_iommu") if enable_guest_iommu == "yes": kernel_extra_params_add += " intel_iommu=on" @@ -339,18 +357,22 @@ def preprocess_vm(test, params, env, name): if guest_iommu_option: kernel_extra_params_add += " iommu=%s" % guest_iommu_option if kernel_extra_params_add or kernel_extra_params_remove: - utils_test.update_boot_option(vm, - args_added=kernel_extra_params_add, - args_removed=kernel_extra_params_remove, - serial_login=serial_login) - - elif not vm.is_alive(): # VM is dead and won't be started, update params + utils_test.update_boot_option( + vm, + args_added=kernel_extra_params_add, + args_removed=kernel_extra_params_remove, + serial_login=serial_login, + ) + + elif not vm.is_alive(): # VM is dead and won't be started, update params vm.devices = None vm.params = params else: # Only work when parameter 'start_vm' is no and VM is alive - if params.get("kill_vm_before_test") == "yes" and\ - params.get("start_vm") == "no": + if ( + params.get("kill_vm_before_test") == "yes" + and params.get("start_vm") == "no" + ): old_vm.destroy(gracefully=gracefully_kill) else: # VM is alive and we just need to open the serial console @@ -385,13 +407,11 @@ def preprocess_vm(test, params, env, name): return cmd_line = params.get("kernel_cmd_line_str", "Command line:") try: - output = vm.serial_console.read_until_output_matches(cmd_line, - timeout=60) + output = vm.serial_console.read_until_output_matches(cmd_line, timeout=60) kernel_cmd_line = re.findall("%s.*" % cmd_line, output[1])[0] kernel_options_exist = params.get("kernel_options_exist", "") - kernel_options_not_exist = params.get("kernel_options_not_exist", - "") + kernel_options_not_exist = params.get("kernel_options_not_exist", "") err_msg = "" for kernel_option in kernel_options_exist.split(): @@ -409,9 +429,11 @@ def preprocess_vm(test, params, env, name): LOG.info("Kernel command line get from serial port is as expect") except Exception as err: - LOG.warn("Did not get the kernel command line from serial " - "port output. Skip the kernel command line check." - "Error is %s" % err) + LOG.warn( + "Did not get the kernel command line from serial " + "port output. Skip the kernel command line check." + "Error is %s" % err + ) def check_image(test, params, image_name, vm_process_status=None): @@ -443,14 +465,20 @@ def check_image(test, params, image_name, vm_process_status=None): if len(option) == 2: image_info[option[0].strip()] = option[1].strip() else: - LOG.debug("Can not find matched image for selected guest " - "os, skip the image check.") + LOG.debug( + "Can not find matched image for selected guest " + "os, skip the image check." + ) check_image_flag = False - if ("lazy refcounts" in image_info and - image_info["lazy refcounts"] == "true"): - LOG.debug("Should not check image while guest is alive" - " when the image is create with lazy refcounts." - " Skip the image check.") + if ( + "lazy refcounts" in image_info + and image_info["lazy refcounts"] == "true" + ): + LOG.debug( + "Should not check image while guest is alive" + " when the image is create with lazy refcounts." + " Skip the image check." + ) check_image_flag = False # Save the potential bad image when the test is not passed. @@ -458,9 +486,13 @@ def check_image(test, params, image_name, vm_process_status=None): if params.get("save_image", "no") == "yes": if vm_process_status == "dead": hsh = utils_misc.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)) + name = "JOB-%s-TEST-%s-%s-%s.%s" % ( + test.job.unique_id[:7], + str(test.name.uid), + image_name, + hsh, + image.image_format, + ) image.save_image(params, name) else: LOG.error("Not saving images, VM is not stopped.") @@ -475,8 +507,9 @@ def check_image(test, params, image_name, vm_process_status=None): except Exception as e: # FIXME: remove it from params, maybe as an img object attr params["img_check_failed"] = "yes" - if (params.get("skip_cluster_leak_warn") == "yes" and - "Leaked clusters" in six.text_type(e)): + if params.get( + "skip_cluster_leak_warn" + ) == "yes" and "Leaked clusters" in six.text_type(e): LOG.warn(six.text_type(e)) else: raise e @@ -494,8 +527,9 @@ def postprocess_image(test, params, image_name, vm_process_status=None): or None for no vm exist. """ if vm_process_status == "running": - LOG.warn("Skipped processing image '%s' since " - "the VM is running!" % image_name) + LOG.warn( + "Skipped processing image '%s' since " "the VM is running!" % image_name + ) return restored, removed = (False, False) @@ -504,8 +538,9 @@ def postprocess_image(test, params, image_name, vm_process_status=None): image = None if params.get("img_check_failed") == "yes": - image = qemu_storage.QemuImg(params, base_dir, - image_name) if not image else image + image = ( + qemu_storage.QemuImg(params, base_dir, image_name) if not image else image + ) if params.get("restore_image_on_check_error", "no") == "yes": image.backup_image(params, base_dir, "restore", True) restored = True @@ -514,36 +549,44 @@ def postprocess_image(test, params, image_name, vm_process_status=None): # with a new backup. i.e. assume pre-existing image/backup # would not be usable after this test succeeds. The best # example for this is when 'unattended_install' is run. - if (params.get("backup_image_after_testing_passed", "no") == "yes" and - params.get("test_passed") == "True"): - image = qemu_storage.QemuImg(params, base_dir, - image_name) if not image else image + if ( + params.get("backup_image_after_testing_passed", "no") == "yes" + and params.get("test_passed") == "True" + ): + image = ( + qemu_storage.QemuImg(params, base_dir, image_name) + if not image + else image + ) image.backup_image(params, base_dir, "backup", True) - if (not restored and params.get("restore_image", "no") == "yes"): - image = qemu_storage.QemuImg(params, base_dir, - image_name) if not image else image + if not restored and params.get("restore_image", "no") == "yes": + image = ( + qemu_storage.QemuImg(params, base_dir, image_name) if not image else image + ) image.backup_image(params, base_dir, "restore", True) restored = True - if (not restored and - params.get("restore_image_after_testing", "no") == "yes"): - image = qemu_storage.QemuImg(params, base_dir, - image_name) if not image else image + if not restored and params.get("restore_image_after_testing", "no") == "yes": + image = ( + qemu_storage.QemuImg(params, base_dir, image_name) if not image else image + ) image.backup_image(params, base_dir, "restore", True) if params.get("img_check_failed") == "yes": - image = qemu_storage.QemuImg(params, base_dir, - image_name) if not image else image + image = ( + qemu_storage.QemuImg(params, base_dir, image_name) if not image else image + ) if params.get("remove_image_on_check_error", "no") == "yes": cl_images = params.get("master_images_clone", "") if image_name in cl_images.split(): image.remove() removed = True - if (not removed and params.get("remove_image", "yes") == "yes"): - image = qemu_storage.QemuImg(params, base_dir, - image_name) if not image else image + if not removed and params.get("remove_image", "yes") == "yes": + image = ( + qemu_storage.QemuImg(params, base_dir, image_name) if not image else image + ) LOG.info("Remove image on %s." % image.storage_type) if clone_master is None: image.remove() @@ -566,23 +609,26 @@ def postprocess_fs_source(test, params, fs_name, vm_process_status=None): running, dead or None for no vm exist. """ if vm_process_status == "running": - LOG.warn("Skipped processing filesystem '%s' since " - "the VM is running!" % fs_name) + LOG.warn( + "Skipped processing filesystem '%s' since " "the VM is running!" % fs_name + ) return - fs_type = params.get('fs_source_type', 'mount') - if fs_type == 'mount': - fs_source = params.get('fs_source_dir') + fs_type = params.get("fs_source_type", "mount") + if fs_type == "mount": + fs_source = params.get("fs_source_dir") base_dir = params.get("fs_source_base_dir", data_dir.get_data_dir()) if not os.path.isabs(fs_source): fs_source = os.path.join(base_dir, fs_source) - if params.get("remove_fs_source") == 'yes': + if params.get("remove_fs_source") == "yes": LOG.info("Remove filesystem source %s." % fs_source) shutil.rmtree(fs_source, ignore_errors=True) else: - LOG.info("Skipped processing filesystem '%s' since " - "unsupported type '%s'." % (fs_name, fs_type)) + LOG.info( + "Skipped processing filesystem '%s' since " + "unsupported type '%s'." % (fs_name, fs_type) + ) def postprocess_vm(test, params, env, name): @@ -618,10 +664,12 @@ def postprocess_vm(test, params, env, name): vm.start() elif params.get("vm_type") == "qemu": vm.create(params=params) - utils_test.update_boot_option(vm, - args_added=kernel_extra_params_remove, - args_removed=kernel_extra_params_add, - serial_login=serial_login) + utils_test.update_boot_option( + vm, + args_added=kernel_extra_params_remove, + args_removed=kernel_extra_params_add, + serial_login=serial_login, + ) # Close all SSH sessions that might be active to this VM for s in vm.remote_sessions[:]: @@ -641,19 +689,25 @@ def postprocess_vm(test, params, env, name): try: vm.copy_files_from(dump_path, vm_extra_dumps) except: - LOG.error("Could not copy the extra dump '%s' from the vm '%s'", - dump_path, vm.name) + LOG.error( + "Could not copy the extra dump '%s' from the vm '%s'", + dump_path, + vm.name, + ) if params.get("kill_vm") == "yes": kill_vm_timeout = float(params.get("kill_vm_timeout", 0)) if kill_vm_timeout: utils_misc.wait_for(vm.is_dead, kill_vm_timeout, 0, 1) vm.destroy(gracefully=params.get("kill_vm_gracefully") == "yes") - if params.get("kill_vm_libvirt") == "yes" and params.get("vm_type") == "libvirt": - vm.undefine(options=params.get('kill_vm_libvirt_options')) + if ( + params.get("kill_vm_libvirt") == "yes" + and params.get("vm_type") == "libvirt" + ): + vm.undefine(options=params.get("kill_vm_libvirt_options")) if vm.is_dead(): - if params.get('vm_type') == 'qemu': + if params.get("vm_type") == "qemu": if vm.devices is not None: vm.devices.cleanup_daemons() @@ -662,8 +716,7 @@ def postprocess_vm(test, params, env, name): strace.stop() -def process_command(test, params, env, command, command_timeout, - command_noncritical): +def process_command(test, params, env, command, command_timeout, command_noncritical): """ Pre- or post- custom commands to be executed before/after a test is run @@ -679,9 +732,7 @@ def process_command(test, params, env, command, command_timeout, os.putenv("KVM_TEST_%s" % k, str(params[k])) # Execute commands try: - a_process.system( - "cd %s; %s" % - (test.bindir, command), shell=True) + a_process.system("cd %s; %s" % (test.bindir, command), shell=True) except a_process.CmdError as e: if command_noncritical: LOG.warn(e) @@ -696,8 +747,7 @@ class _CreateImages(threading.Thread): in self.exc_info """ - def __init__(self, image_func, test, images, params, exit_event, - vm_process_status): + def __init__(self, image_func, test, images, params, exit_event, vm_process_status): threading.Thread.__init__(self) self.image_func = image_func self.test = test @@ -709,9 +759,14 @@ def __init__(self, image_func, test, images, params, exit_event, def run(self): try: - _process_images_serial(self.image_func, self.test, self.images, - self.params, self.exit_event, - self.vm_process_status) + _process_images_serial( + self.image_func, + self.test, + self.images, + self.params, + self.exit_event, + self.vm_process_status, + ) except Exception: self.exc_info = sys.exc_info() self.exit_event.set() @@ -728,12 +783,14 @@ def process_images(image_func, test, params, vm_process_status=None): or None for no vm exist. """ images = params.objects("images") - if len(images) > 20: # Lets do it in parallel - _process_images_parallel(image_func, test, params, - vm_process_status=vm_process_status) + if len(images) > 20: # Lets do it in parallel + _process_images_parallel( + image_func, test, params, vm_process_status=vm_process_status + ) else: - _process_images_serial(image_func, test, images, params, - vm_process_status=vm_process_status) + _process_images_serial( + image_func, test, images, params, vm_process_status=vm_process_status + ) def process_fs_sources(fs_source_func, test, params, vm_process_status=None): @@ -751,8 +808,9 @@ def process_fs_sources(fs_source_func, test, params, vm_process_status=None): fs_source_func(test, fs_params, filesystem, vm_process_status) -def _process_images_serial(image_func, test, images, params, exit_event=None, - vm_process_status=None): +def _process_images_serial( + image_func, test, images, params, exit_event=None, vm_process_status=None +): """ Original process_image function, which allows custom set of images :param image_func: Process function @@ -781,29 +839,31 @@ def _process_images_parallel(image_func, test, params, vm_process_status=None): or None for no vm exist. """ images = params.objects("images") - no_threads = min(len(images) // 5, - 2 * multiprocessing.cpu_count()) + no_threads = min(len(images) // 5, 2 * multiprocessing.cpu_count()) exit_event = threading.Event() threads = [] for i in xrange(no_threads): imgs = images[i::no_threads] - threads.append(_CreateImages(image_func, test, imgs, params, - exit_event, vm_process_status)) + threads.append( + _CreateImages(image_func, test, imgs, params, exit_event, vm_process_status) + ) threads[-1].start() for thread in threads: thread.join() - if exit_event.is_set(): # Failure in some thread + if exit_event.is_set(): # Failure in some thread LOG.error("Image processing failed:") for thread in threads: - if thread.exc_info: # Throw the first failure + if thread.exc_info: # Throw the first failure six.reraise(thread.exc_info[1], None, thread.exc_info[2]) del exit_event del threads[:] -def process(test, params, env, image_func, vm_func, vm_first=False, fs_source_func=None): +def process( + test, params, env, image_func, vm_func, vm_first=False, fs_source_func=None +): """ Pre- or post-process VMs and images according to the instructions in params. Call image_func for each image listed in params and vm_func for each VM. @@ -816,6 +876,7 @@ def process(test, params, env, image_func, vm_func, vm_first=False, fs_source_fu :param vm_first: Call vm_func first or not. :param fs_source_func: A function to call for each filesystem source. """ + def _call_vm_func(): for vm_name in params.objects("vms"): vm_params = params.object_params(vm_name) @@ -831,16 +892,15 @@ def _call_image_func(): vm = env.get_vm(vm_name) unpause_vm = False if vm is None or vm.is_dead(): - vm_process_status = 'dead' + vm_process_status = "dead" else: - vm_process_status = 'running' + vm_process_status = "running" if vm is not None and vm.is_alive() and not vm.is_paused(): vm.pause() unpause_vm = True - vm_params['skip_cluster_leak_warn'] = "yes" + vm_params["skip_cluster_leak_warn"] = "yes" try: - process_images(image_func, test, vm_params, - vm_process_status) + process_images(image_func, test, vm_params, vm_process_status) finally: if unpause_vm: vm.resume() @@ -854,20 +914,21 @@ def _call_fs_source_func(): if params.objects("vms"): for vm_name in params.objects("vms"): vm_params = params.object_params(vm_name) - if not vm_params.get('filesystems'): + if not vm_params.get("filesystems"): continue vm = env.get_vm(vm_name) unpause_vm = False if vm is None or vm.is_dead(): - vm_process_status = 'dead' + vm_process_status = "dead" else: - vm_process_status = 'running' + vm_process_status = "running" if vm is not None and vm.is_alive() and not vm.is_paused(): vm.pause() unpause_vm = True try: - process_fs_sources(fs_source_func, test, vm_params, - vm_process_status) + process_fs_sources( + fs_source_func, test, vm_params, vm_process_status + ) finally: if unpause_vm: vm.resume() @@ -882,18 +943,22 @@ def _call_check_image_func(): vm = env.get_vm(vm_name) unpause_vm = False if vm is None or vm.is_dead(): - vm_process_status = 'dead' + vm_process_status = "dead" else: - vm_process_status = 'running' + vm_process_status = "running" if vm is not None and vm.is_alive() and not vm.is_paused(): vm.pause() unpause_vm = True - vm_params['skip_cluster_leak_warn'] = "yes" + vm_params["skip_cluster_leak_warn"] = "yes" try: images = params.objects("images") _process_images_serial( - check_image, test, images, vm_params, - vm_process_status=vm_process_status) + check_image, + test, + images, + vm_params, + vm_process_status=vm_process_status, + ) finally: if unpause_vm: vm.resume() @@ -946,8 +1011,10 @@ def preprocess(test, params, env): os.chdir(qemu_builddir) # Looks like libvirt process does not have permissions to write to # coverage files, hence give write for all files in qemu source - reset_cmd = 'make clean-coverage;%s -version;' % qemu_bin - reset_cmd += 'find %s -name "*.gcda" -exec chmod a=rwx {} \;' % qemu_builddir + reset_cmd = "make clean-coverage;%s -version;" % qemu_bin + reset_cmd += ( + 'find %s -name "*.gcda" -exec chmod a=rwx {} \;' % qemu_builddir + ) a_process.system(reset_cmd, shell=True) # Check host for any errors to start with and just report and @@ -962,7 +1029,11 @@ def preprocess(test, params, env): # otherwise throw TestError with respective error message cpu_family = "unknown" try: - cpu_family = cpu_utils.get_family() if hasattr(cpu_utils, 'get_family') else cpu_utils.get_cpu_arch() + cpu_family = ( + cpu_utils.get_family() + if hasattr(cpu_utils, "get_family") + else cpu_utils.get_cpu_arch() + ) except Exception: LOG.warning("Could not get host cpu family") migration_setup = params.get("migration_setup", "no") == "yes" @@ -981,15 +1052,17 @@ def preprocess(test, params, env): if migration_setup: power9_compat_remote = "yes" == params.get("power9_compat_remote", "no") cpu_cmd = "grep cpu /proc/cpuinfo | awk '{print $3}' | head -n 1" - remote_host = {'server_ip': params.get("remote_ip"), - 'server_pwd': params.get("remote_pwd"), - 'server_user': params.get("remote_user", "root")} + remote_host = { + "server_ip": params.get("remote_ip"), + "server_pwd": params.get("remote_pwd"), + "server_user": params.get("remote_user", "root"), + } server_session = test_setup.remote_session(remote_host) cmd_output = server_session.cmd_status_output(cpu_cmd) - if (cmd_output[0] == 0): + if cmd_output[0] == 0: remote_cpu = cmd_output[1].strip().lower() cmd_output = server_session.cmd_status_output(pvr_cmd) - if (cmd_output[0] == 0): + if cmd_output[0] == 0: remote_pvr = float(cmd_output[1].strip()) server_session.close() if "power8" in remote_cpu: @@ -998,13 +1071,17 @@ def preprocess(test, params, env): test_setup.switch_indep_threads_mode(state="N", params=params) test_setup.switch_smt(state="off", params=params) if pvr != remote_pvr: - LOG.warning("Source and destinations system PVR " - "does not match\n PVR:\nSource: %s" - "\nDestination: %s", pvr, remote_pvr) + LOG.warning( + "Source and destinations system PVR " + "does not match\n PVR:\nSource: %s" + "\nDestination: %s", + pvr, + remote_pvr, + ) # First, let's verify if this test does require root or not. If it # does and the test suite is running as a regular user, we shall just # throw a TestSkipError exception, which will skip the test. - if params.get('requires_root', 'no') == 'yes': + if params.get("requires_root", "no") == "yes": utils_misc.verify_running_as_root() # throw a TestSkipError exception if command requested by test is not @@ -1022,20 +1099,18 @@ def preprocess(test, params, env): _setup_manager.register(BridgeConfig) _setup_manager.do_setup() - vm_type = params.get('vm_type') + vm_type = params.get("vm_type") - if vm_type == 'libvirt': + if vm_type == "libvirt": if params.get("enable_libvirtd_debug_log", "yes") == "yes": # By default log the info level log_level = params.get("libvirtd_debug_level", "2") log_file = params.get("libvirtd_debug_file", "") log_filters = params.get("libvirtd_debug_filters", "%s:*" % log_level) log_permission = params.get("libvirtd_log_permission") - libvirtd_debug_log = test_setup.LibvirtdDebugLog(test, - log_level, - log_file, - log_filters, - log_permission) + libvirtd_debug_log = test_setup.LibvirtdDebugLog( + test, log_level, log_file, log_filters, log_permission + ) libvirtd_debug_log.enable() base_dir = data_dir.get_data_dir() @@ -1051,8 +1126,8 @@ def preprocess(test, params, env): env.register_lvmdev("lvm_%s" % params["main_vm"], lvmdev) if params.get("storage_type") == "nfs": - selinux_local = params.get('set_sebool_local', 'yes') == "yes" - selinux_remote = params.get('set_sebool_remote', 'no') == "yes" + selinux_local = params.get("set_sebool_local", "yes") == "yes" + selinux_remote = params.get("set_sebool_remote", "no") == "yes" image_nfs = nfs.Nfs(params) image_nfs.setup() if migration_setup: @@ -1068,16 +1143,16 @@ def preprocess(test, params, env): nfs_client = nfs.NFSClient(params) nfs_client.setup() distro_details = distro.detect() - if distro_details.name.upper() != 'UBUNTU': + if distro_details.name.upper() != "UBUNTU": if selinux_local: - params['set_sebool_local'] = "yes" - params['local_boolean_varible'] = "virt_use_nfs" - params['local_boolean_value'] = params.get("local_boolean_value", "on") + params["set_sebool_local"] = "yes" + params["local_boolean_varible"] = "virt_use_nfs" + params["local_boolean_value"] = params.get("local_boolean_value", "on") # configure selinux on remote host to permit migration if migration_setup: cmd = "cat /etc/os-release | grep '^PRETTY_NAME'" session = test_setup.remote_session(params) - if 'UBUNTU' not in str(session.cmd_output(cmd)).upper(): + if "UBUNTU" not in str(session.cmd_output(cmd)).upper(): params["set_sebool_remote"] = "yes" params["remote_boolean_varible"] = "virt_use_nfs" params["remote_boolean_value"] = "on" @@ -1090,33 +1165,32 @@ def preprocess(test, params, env): name_tag = "image_name_%s" % image_name if params.get(name_tag): image_name_only = os.path.basename(params[name_tag]) - params[name_tag] = os.path.join(image_nfs.mount_dir, - image_name_only) + params[name_tag] = os.path.join(image_nfs.mount_dir, image_name_only) - firewalld_service = params.get('firewalld_service') - if firewalld_service == 'disable': + firewalld_service = params.get("firewalld_service") + if firewalld_service == "disable": firewalld = service.Service("firewalld") if firewalld.status(): firewalld.stop() if firewalld.status(): - test.log.warning('Failed to stop firewalld') + test.log.warning("Failed to stop firewalld") else: - if firewalld_service == 'enable': + if firewalld_service == "enable": firewalld = service.Service("firewalld") if not firewalld.status(): firewalld.start() if not firewalld.status(): - test.log.warning('Failed to start firewalld') + test.log.warning("Failed to start firewalld") - if distro.detect().name == 'Ubuntu': - params['firewalld_dhcp_workaround'] = "no" + if distro.detect().name == "Ubuntu": + params["firewalld_dhcp_workaround"] = "no" # Workaround know issue where firewall blocks dhcp from guest # through virbr0 - if params.get('firewalld_dhcp_workaround', "no") == "yes": + if params.get("firewalld_dhcp_workaround", "no") == "yes": firewall_cmd = utils_iptables.Firewall_cmd() - if not firewall_cmd.add_service('dhcp', permanent=True): - test.log.warning('Failed to add dhcp service to be permitted') + if not firewall_cmd.add_service("dhcp", permanent=True): + test.log.warning("Failed to add dhcp service to be permitted") # Start ip sniffing if it isn't already running # The fact it has to be started here is so that the test params @@ -1127,22 +1201,23 @@ def preprocess(test, params, env): migrate_vms = params.objects("migrate_vms") if migrate_vms: vms = list(set(params.objects("vms") + migrate_vms)) - params["vms"] = ' '.join(vms) + params["vms"] = " ".join(vms) # Permit iptables to permit 49152-49216 ports to libvirt for # migration and if arch is ppc with power8 then switch off smt # will be taken care in remote machine for migration to succeed if migration_setup: - dest_uri = libvirt_vm.complete_uri(params.get("server_ip", - params.get("remote_ip"))) + dest_uri = libvirt_vm.complete_uri( + params.get("server_ip", params.get("remote_ip")) + ) migrate_setup = migration.MigrationTest() migrate_setup.migrate_pre_setup(dest_uri, params) # Map hostname and IP address of the hosts to avoid virsh # to error out of resolving - hostname_ip = {str(virsh.hostname()): params['local_ip']} + hostname_ip = {str(virsh.hostname()): params["local_ip"]} session = test_setup.remote_session(params) - _, remote_hostname = session.cmd_status_output('hostname') - hostname_ip[str(remote_hostname.strip())] = params['remote_ip'] + _, remote_hostname = session.cmd_status_output("hostname") + hostname_ip[str(remote_hostname.strip())] = params["remote_ip"] if not utils_net.map_hostname_ipaddress(hostname_ip): test.cancel("Failed to map hostname and ipaddress of source host") if not utils_net.map_hostname_ipaddress(hostname_ip, session=session): @@ -1159,15 +1234,20 @@ def preprocess(test, params, env): # leave them untouched if they have to be disregarded only for this test requested_vms = params.objects("vms") keep_unrequested_vms = params.get_boolean("keep_unrequested_vms", False) - kill_unrequested_vms_gracefully = params.get_boolean("kill_unrequested_vms_gracefully", True) + kill_unrequested_vms_gracefully = params.get_boolean( + "kill_unrequested_vms_gracefully", True + ) for key in list(env.keys()): vm = env[key] if not isinstance(vm, virt_vm.BaseVM): continue if vm.name not in requested_vms: if keep_unrequested_vms: - LOG.debug("The vm %s is registered in the env and disregarded " - "in the current test", vm.name) + LOG.debug( + "The vm %s is registered in the env and disregarded " + "in the current test", + vm.name, + ) else: vm.destroy(gracefully=kill_unrequested_vms_gracefully) del env[key] @@ -1176,12 +1256,11 @@ def preprocess(test, params, env): kvm_modules = arch.get_kvm_module_list() for module in reversed(kvm_modules): param_prefix = module if module == "kvm" else "kvm_probe" - module_force_load = params.get_boolean("%s_module_force_load" - % param_prefix) - module_parameters = params.get("%s_module_parameters" % param_prefix, - "") - module_handler = utils_kernel_module.reload(module, module_force_load, - module_parameters) + module_force_load = params.get_boolean("%s_module_force_load" % param_prefix) + module_parameters = params.get("%s_module_parameters" % param_prefix, "") + module_handler = utils_kernel_module.reload( + module, module_force_load, module_parameters + ) if module_handler is not None: KVM_MODULE_HANDLERS.append(module_handler) @@ -1203,13 +1282,15 @@ def preprocess(test, params, env): if params.get("required_kernel"): required_kernel = params.get("required_kernel") LOG.info("Test requires kernel version: %s" % required_kernel) - match = re.search(r'[0-9]+\.[0-9]+\.[0-9]+(\-[0-9]+)?', kvm_version) + match = re.search(r"[0-9]+\.[0-9]+\.[0-9]+(\-[0-9]+)?", kvm_version) if match is None: test.cancel("Can not get host kernel version.") host_kernel = match.group(0) if host_kernel not in VersionInterval(required_kernel): - test.cancel("Got host kernel version:%s, which is not in %s" % - (host_kernel, required_kernel)) + test.cancel( + "Got host kernel version:%s, which is not in %s" + % (host_kernel, required_kernel) + ) # Get the KVM userspace version kvm_userspace_ver_cmd = params.get("kvm_userspace_ver_cmd", "") @@ -1217,7 +1298,8 @@ def preprocess(test, params, env): if kvm_userspace_ver_cmd: try: kvm_userspace_version = a_process.run( - kvm_userspace_ver_cmd, shell=True).stdout_text.strip() + kvm_userspace_ver_cmd, shell=True + ).stdout_text.strip() except a_process.CmdError: kvm_userspace_version = "Unknown" else: @@ -1225,8 +1307,9 @@ def preprocess(test, params, env): kvm_userspace_version = _get_qemu_version(qemu_path) qemu_dst_path = utils_misc.get_qemu_dst_binary(params) if qemu_dst_path and qemu_dst_path != qemu_path: - LOG.debug("KVM userspace dst version(qemu): %s", - _get_qemu_version(qemu_dst_path)) + LOG.debug( + "KVM userspace dst version(qemu): %s", _get_qemu_version(qemu_dst_path) + ) LOG.debug("KVM userspace version(qemu): %s", kvm_userspace_version) version_info["qemu_version"] = str(kvm_userspace_version) @@ -1235,21 +1318,23 @@ def preprocess(test, params, env): if params.get("required_qemu"): required_qemu = params.get("required_qemu") LOG.info("Test requires qemu version: %s" % required_qemu) - match = re.search(r'[0-9]+\.[0-9]+\.[0-9]+(\-[0-9]+)?', - kvm_userspace_version) + match = re.search(r"[0-9]+\.[0-9]+\.[0-9]+(\-[0-9]+)?", kvm_userspace_version) if match is None: test.cancel("Can not get host qemu version.") host_qemu = match.group(0) if host_qemu not in VersionInterval(required_qemu): - test.cancel("Got host qemu version:%s, which is not in %s" % - (host_qemu, required_qemu)) + test.cancel( + "Got host qemu version:%s, which is not in %s" + % (host_qemu, required_qemu) + ) # Get the version of bootloader vm_bootloader_ver_cmd = params.get("vm_bootloader_ver_cmd", "") if vm_bootloader_ver_cmd: try: vm_bootloader_ver = a_process.run( - vm_bootloader_ver_cmd, shell=True).stdout_text.strip() + vm_bootloader_ver_cmd, shell=True + ).stdout_text.strip() except a_process.CmdError: vm_bootloader_ver = "Unkown" version_info["vm_bootloader_version"] = str(vm_bootloader_ver) @@ -1259,29 +1344,49 @@ def preprocess(test, params, env): if params.get("required_virtio_win") or params.get("required_virtio_win_prewhql"): if params.get("cdrom_virtio"): cdrom_virtio = params["cdrom_virtio"] - cdrom_virtio_path = os.path.basename(utils_misc.get_path(data_dir.get_data_dir(), cdrom_virtio)) - virtio_win_range = params.get("required_virtio_win_prewhql") if re.search( - 'prewhql', cdrom_virtio_path) else params.get("required_virtio_win") + cdrom_virtio_path = os.path.basename( + utils_misc.get_path(data_dir.get_data_dir(), cdrom_virtio) + ) + virtio_win_range = ( + params.get("required_virtio_win_prewhql") + if re.search("prewhql", cdrom_virtio_path) + else params.get("required_virtio_win") + ) if virtio_win_range: LOG.info("Checking required virtio-win version: %s" % virtio_win_range) - match = re.search("virtio-win-(?:prewhql-)?(\d+\.\d+(?:\.\d+)?-\d+)", cdrom_virtio_path) + match = re.search( + "virtio-win-(?:prewhql-)?(\d+\.\d+(?:\.\d+)?-\d+)", + cdrom_virtio_path, + ) if match.group(1) is None: - test.error("Can not get virtio-win version from \"cdrom_virtio\": %s" % cdrom_virtio) - cdrom_virtio_version = re.sub('-', '.', match.group(1)) + test.error( + 'Can not get virtio-win version from "cdrom_virtio": %s' + % cdrom_virtio + ) + cdrom_virtio_version = re.sub("-", ".", match.group(1)) if cdrom_virtio_version not in VersionInterval(virtio_win_range): - test.cancel("Got virtio-win version:%s, which is not in %s" % - (cdrom_virtio_version, virtio_win_range)) + test.cancel( + "Got virtio-win version:%s, which is not in %s" + % (cdrom_virtio_version, virtio_win_range) + ) else: - test.error("The limitation for virtio-win is not suitable for the cdrom_virtio") + test.error( + "The limitation for virtio-win is not suitable for the cdrom_virtio" + ) else: - LOG.warning("required_virtio_win(prewhql) can not take effect without cdrom_virtio") + LOG.warning( + "required_virtio_win(prewhql) can not take effect without cdrom_virtio" + ) # Get the Libvirt version if vm_type == "libvirt": - libvirt_ver_cmd = params.get("libvirt_ver_cmd", "libvirtd -V|awk -F' ' '{print $3}'") + libvirt_ver_cmd = params.get( + "libvirt_ver_cmd", "libvirtd -V|awk -F' ' '{print $3}'" + ) try: libvirt_version = a_process.run( - libvirt_ver_cmd, shell=True).stdout_text.strip() + libvirt_ver_cmd, shell=True + ).stdout_text.strip() except a_process.CmdError: libvirt_version = "Unknown" version_info["libvirt_version"] = str(libvirt_version) @@ -1302,7 +1407,7 @@ def preprocess(test, params, env): _pre_hugepages_surp = h.ext_hugepages_surp suggest_mem = h.setup() if suggest_mem is not None: - params['mem'] = suggest_mem + params["mem"] = suggest_mem if not params.get("hugepage_path"): params["hugepage_path"] = h.hugepage_path if vm_type == "libvirt": @@ -1327,7 +1432,7 @@ def preprocess(test, params, env): connect_uri = libvirt_vm.normalize_connect_uri(connect_uri) # Set the LIBVIRT_DEFAULT_URI to make virsh command # work on connect_uri as default behavior. - os.environ['LIBVIRT_DEFAULT_URI'] = connect_uri + os.environ["LIBVIRT_DEFAULT_URI"] = connect_uri if params.get("setup_libvirt_polkit") == "yes": pol = test_setup.LibvirtPolkitConfig(params) try: @@ -1341,9 +1446,14 @@ def preprocess(test, params, env): # Execute any pre_commands if params.get("pre_command"): - process_command(test, params, env, params.get("pre_command"), - int(params.get("pre_command_timeout", "600")), - params.get("pre_command_noncritical") == "yes") + process_command( + test, + params, + env, + params.get("pre_command"), + int(params.get("pre_command_timeout", "600")), + params.get("pre_command_noncritical") == "yes", + ) # Sysprep the master image if requested, to customize image before cloning if params.get("sysprep_required", "no") == "yes": @@ -1356,7 +1466,8 @@ def preprocess(test, params, env): LOG.info("Syspreping the image as requested before cloning.") try: utils_libguestfs.virt_sysprep_cmd( - image_filename, options=sysprep_options, ignore_status=False) + image_filename, options=sysprep_options, ignore_status=False + ) except utils_libguestfs.LibguestfsCmdError as detail: # when virt-sysprep fails the original master image is unchanged. # We can remove backup image, so that test would not spend much time @@ -1377,11 +1488,15 @@ def preprocess(test, params, env): image_obj = qemu_storage.QemuImg(vm_params, base_dir, image) image_obj.clone_image(vm_params, vm_name, image, base_dir) params["image_name_%s" % vm_name] = vm_params["image_name_%s" % vm_name] - params["image_name_%s_%s" % (image, vm_name)] = vm_params["image_name_%s_%s" % (image, vm_name)] + params["image_name_%s_%s" % (image, vm_name)] = vm_params[ + "image_name_%s_%s" % (image, vm_name) + ] if params.get("auto_cpu_model") == "yes" and vm_type == "qemu": - policy_map = {"libvirt_host_model": cpu.get_cpu_info_from_virsh, - "virttest": cpu.get_qemu_best_cpu_info} + policy_map = { + "libvirt_host_model": cpu.get_cpu_info_from_virsh, + "virttest": cpu.get_qemu_best_cpu_info, + } auto_cpu_policy = params.get("auto_cpu_policy", "virttest").split() for policy in auto_cpu_policy: try: @@ -1389,57 +1504,67 @@ def preprocess(test, params, env): if cpu_info: break except Exception as err: - LOG.error("Failed to get cpu info with policy %s: %s" - % (policy, err)) + LOG.error("Failed to get cpu info with policy %s: %s" % (policy, err)) continue else: - raise exceptions.TestCancel("Failed to get cpu info with " - "policy %s" % auto_cpu_policy) + raise exceptions.TestCancel( + "Failed to get cpu info with " "policy %s" % auto_cpu_policy + ) params["cpu_model"] = cpu_info["model"] if cpu_info["flags"]: cpu_flags = params.get("cpu_model_flags") - params["cpu_model_flags"] = cpu.recombine_qemu_cpu_flags(cpu_info["flags"], - cpu_flags) + params["cpu_model_flags"] = cpu.recombine_qemu_cpu_flags( + cpu_info["flags"], cpu_flags + ) if vm_type == "qemu": qemu_path = utils_misc.get_qemu_binary(params) - if (utils_qemu.has_device_category(qemu_path, "CPU") - and params.get("cpu_driver") is None): + if ( + utils_qemu.has_device_category(qemu_path, "CPU") + and params.get("cpu_driver") is None + ): cpu_model = params.get("cpu_model") if cpu_model: search_pattern = r"%s-\w+-cpu" % cpu_model - cpu_driver = utils_qemu.find_supported_devices(qemu_path, - search_pattern, - "CPU") + cpu_driver = utils_qemu.find_supported_devices( + qemu_path, search_pattern, "CPU" + ) if cpu_driver: env["cpu_driver"] = cpu_driver[0] params["cpu_driver"] = env.get("cpu_driver") # Preprocess all VMs and images if params.get("not_preprocess", "no") == "no": - process(test, params, env, preprocess_image, preprocess_vm, - fs_source_func=preprocess_fs_source) + process( + test, + params, + env, + preprocess_image, + preprocess_vm, + fs_source_func=preprocess_fs_source, + ) # Start the screendump thread if params.get("take_regular_screendumps") == "yes": global _screendump_thread, _screendump_thread_termination_event _screendump_thread_termination_event = threading.Event() - _screendump_thread = threading.Thread(target=_take_screendumps, - name='ScreenDump', - args=(test, params, env)) + _screendump_thread = threading.Thread( + target=_take_screendumps, name="ScreenDump", args=(test, params, env) + ) _screendump_thread.start() # Start the register query thread if params.get("store_vm_info") == "yes": global _vm_info_thread, _vm_info_thread_termination_event _vm_info_thread_termination_event = threading.Event() - _vm_info_thread = threading.Thread(target=_store_vm_info, - name='VmMonInfo', - args=(test, params, env)) + _vm_info_thread = threading.Thread( + target=_store_vm_info, name="VmMonInfo", args=(test, params, env) + ) _vm_info_thread.start() # start test in nested guest if params.get("run_nested_guest_test", "no") == "yes": + def thread_func(obj): """ Thread method to trigger nested VM test @@ -1452,6 +1577,7 @@ def thread_func(obj): except Exception as info: LOG.error(info) THREAD_ERROR = True + nest_params = params.copy() nested_params = eval(nest_params.get("nested_params", "{}")) # update the current level's param with nested params sent @@ -1467,37 +1593,44 @@ def thread_func(obj): # Have buffer memory 1G for VMs to work seamlessly nest_memory = (int(nest_params.get("mem")) // len(nest_vms)) - 1024 if nest_memory < 512: - raise exceptions.TestCancel("Memory is not sufficient for " - "VMs to boot and perform nested " - "virtualization tests") + raise exceptions.TestCancel( + "Memory is not sufficient for " + "VMs to boot and perform nested " + "virtualization tests" + ) # set memory for the nested VM - nest_params["vt_extra_params"] = "mem=\"%s\"" % nest_memory + nest_params["vt_extra_params"] = 'mem="%s"' % nest_memory # pass the params current level to next level - nest_params["vt_extra_params"] += " nested_params=\"%s\"" % nested_params + nest_params["vt_extra_params"] += ' nested_params="%s"' % nested_params # update the current_level for next_level guest - nest_params["vt_extra_params"] += (" nested_guest_level=\"L%s\"" % - (int(current_level.lstrip("L")) + - 1)) + nest_params["vt_extra_params"] += ' nested_guest_level="L%s"' % ( + int(current_level.lstrip("L")) + 1 + ) # persist the max_level in every level of guest - nest_params["vt_extra_params"] += (" nested_guest_max_level=\"L%s\"" % - int(max_level.lstrip("L"))) - nest_params["vt_extra_params"] += " run_nested_guest_test=\"yes\"" + nest_params["vt_extra_params"] += ' nested_guest_max_level="L%s"' % int( + max_level.lstrip("L") + ) + nest_params["vt_extra_params"] += ' run_nested_guest_test="yes"' LOG.debug("Test is running in Guest level: %s", current_level) for vm in nest_vms: # params with nested level specific configuration new_params = nest_params.object_params(current_level) # params with VM name specific in that particular level new_params = new_params.object_params(vm.name) - testlist = [new_params.get("avocado_guest_vt_test", - "boot")] + testlist = [new_params.get("avocado_guest_vt_test", "boot")] avocadotestargs = new_params.get("avocado_guest_add_args", "") - obj = utils_test.AvocadoGuest(vm, new_params, test, testlist, - testrepo='', - timeout=nest_timeout, - installtype=install_type, - avocado_vt=True, - reinstall=False, - add_args=avocadotestargs) + obj = utils_test.AvocadoGuest( + vm, + new_params, + test, + testlist, + testrepo="", + timeout=nest_timeout, + installtype=install_type, + avocado_vt=True, + reinstall=False, + add_args=avocadotestargs, + ) thread = threading.Thread(target=thread_func, args=(obj,)) threads.append(thread) for thread in threads: @@ -1505,8 +1638,9 @@ def thread_func(obj): for thread in threads: thread.join() if THREAD_ERROR: - raise exceptions.TestFail("Test inside nested guest " - "reported failure") + raise exceptions.TestFail( + "Test inside nested guest " "reported failure" + ) # Run this hook after any network setup stage and vm creation. if callable(preprocess_vm_on_hook): @@ -1533,14 +1667,21 @@ def postprocess(test, params, env): try: postprocess_vm_on_hook(test, params, env) # pylint: disable=E1102 except Exception as details: - err += "\nPostprocessing living vm hook: %s" % str(details).replace('\\n', '\n ') + err += "\nPostprocessing living vm hook: %s" % str(details).replace( + "\\n", "\n " + ) LOG.error(details) migration_setup = params.get("migration_setup", "no") == "yes" - if params.get("verify_guest_dmesg", "yes") == "yes" and params.get("start_vm", "no") == "yes": + if ( + params.get("verify_guest_dmesg", "yes") == "yes" + and params.get("start_vm", "no") == "yes" + ): guest_dmesg_log_file = params.get("guest_dmesg_logfile", "guest_dmesg.log") guest_dmesg_log_file = utils_misc.get_path(test.debugdir, guest_dmesg_log_file) - living_vms = [vm for vm in env.get_all_vms() if (vm.is_alive() and not vm.is_paused())] + living_vms = [ + vm for vm in env.get_all_vms() if (vm.is_alive() and not vm.is_paused()) + ] for vm in living_vms: if params.get("guest_dmesg_dump_console") == "yes": guest_dmesg_log_file = None @@ -1549,8 +1690,7 @@ def postprocess(test, params, env): try: vm.verify_dmesg(dmesg_log_file=guest_dmesg_log_file) except Exception as details: - err += ("\n: Guest %s dmesg verification failed: %s" - % (vm.name, details)) + err += "\n: Guest %s dmesg verification failed: %s" % (vm.name, details) base_dir = data_dir.get_data_dir() # if sysprep was requested in preprocess then restore back the original image @@ -1563,7 +1703,9 @@ def postprocess(test, params, env): # collect sosreport of guests during postprocess if enabled if params.get("enable_guest_sosreport", "no") == "yes": - living_vms = [vm for vm in env.get_all_vms() if (vm.is_alive() and not vm.is_paused())] + living_vms = [ + vm for vm in env.get_all_vms() if (vm.is_alive() and not vm.is_paused()) + ] for vm in living_vms: sosreport_path = vm.sosreport() LOG.info("Sosreport for guest: %s", sosreport_path) @@ -1576,24 +1718,39 @@ def postprocess(test, params, env): os.makedirs(gcov_qemu_dir) os.chdir(qemu_builddir) collect_cmd_opts = params.get("gcov_qemu_collect_cmd_opts", "--html") - online_count = cpu_utils.online_count() if hasattr(cpu_utils, 'online_count') else cpu_utils.online_cpus_count() - collect_cmd = "gcovr -j %s -o %s -s %s ." % (online_count, - os.path.join(gcov_qemu_dir, "gcov.html"), - collect_cmd_opts) + online_count = ( + cpu_utils.online_count() + if hasattr(cpu_utils, "online_count") + else cpu_utils.online_cpus_count() + ) + collect_cmd = "gcovr -j %s -o %s -s %s ." % ( + online_count, + os.path.join(gcov_qemu_dir, "gcov.html"), + collect_cmd_opts, + ) a_process.system(collect_cmd, shell=True) if params.get("gcov_qemu_compress", "no") == "yes": os.chdir(test.debugdir) archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir) shutil.rmtree(gcov_qemu_dir, ignore_errors=True) else: - LOG.warning("Check either qemu build directory availablilty" - " or install gcovr package for qemu coverage report") + LOG.warning( + "Check either qemu build directory availablilty" + " or install gcovr package for qemu coverage report" + ) # Postprocess all VMs and images try: - process(test, params, env, postprocess_image, - postprocess_vm, True, postprocess_fs_source) + process( + test, + params, + env, + postprocess_image, + postprocess_vm, + True, + postprocess_fs_source, + ) except Exception as details: - err += "\nPostprocess: %s" % str(details).replace('\\n', '\n ') + err += "\nPostprocess: %s" % str(details).replace("\\n", "\n ") LOG.error(details) # Terminate the screendump thread @@ -1607,8 +1764,9 @@ def postprocess(test, params, env): dir_rex = "(screendump\S*_[0-9]+_iter%s)" % test.iteration for screendump_dir in re.findall(dir_rex, str(os.listdir(test.debugdir))): screendump_dir = os.path.join(test.debugdir, screendump_dir) - if (params.get("encode_video_files", "yes") == "yes" and - glob.glob("%s/*" % screendump_dir)): + if params.get("encode_video_files", "yes") == "yes" and glob.glob( + "%s/*" % screendump_dir + ): try: # Loading video_maker at the top level is causing # gst to be loaded at the top level, generating @@ -1616,9 +1774,9 @@ def postprocess(test, params, env): # move the import to the precise place where it's # needed. from . import video_maker + video = video_maker.get_video_maker_klass() - if ((video.has_element('vp8enc') and - video.has_element('webmmux'))): + if video.has_element("vp8enc") and video.has_element("webmmux"): video_file = "%s.webm" % screendump_dir else: video_file = "%s.ogg" % screendump_dir @@ -1627,14 +1785,12 @@ def postprocess(test, params, env): video.encode(screendump_dir, video_file) except Exception as detail: - LOG.info( - "Video creation failed for %s: %s", screendump_dir, detail) + LOG.info("Video creation failed for %s: %s", screendump_dir, detail) # Warn about corrupt PPM files screendump_temp_dir = params.get("screendump_temp_dir") if screendump_temp_dir: - screendump_temp_dir = utils_misc.get_path( - test.bindir, screendump_temp_dir) + screendump_temp_dir = utils_misc.get_path(test.bindir, screendump_temp_dir) else: screendump_temp_dir = test.debugdir ppm_file_rex = "*_iter%s.ppm" % test.iteration @@ -1645,12 +1801,11 @@ def postprocess(test, params, env): # Should we convert PPM files to PNG format? if params.get("convert_ppm_files_to_png", "no") == "yes": try: - for f in glob.glob( - os.path.join(screendump_temp_dir, ppm_file_rex)): + for f in glob.glob(os.path.join(screendump_temp_dir, ppm_file_rex)): if ppm_utils.image_verify_ppm_file(f): new_path = f.replace(".ppm", ".png") image = PIL.Image.open(f) - image.save(new_path, format='PNG') + image.save(new_path, format="PNG") except NameError: pass @@ -1667,8 +1822,9 @@ def postprocess(test, params, env): # Should we keep the video files? if params.get("keep_video_files", "yes") != "yes": - for f in (glob.glob(os.path.join(test.debugdir, '*.ogg')) + - glob.glob(os.path.join(test.debugdir, '*.webm'))): + for f in glob.glob(os.path.join(test.debugdir, "*.ogg")) + glob.glob( + os.path.join(test.debugdir, "*.webm") + ): os.unlink(f) # Terminate the register query thread @@ -1689,8 +1845,7 @@ def postprocess(test, params, env): session = vm.wait_for_login(timeout=vm.LOGIN_WAIT_TIMEOUT) session.close() else: - session = vm.wait_for_serial_login( - timeout=vm.LOGIN_WAIT_TIMEOUT) + session = vm.wait_for_serial_login(timeout=vm.LOGIN_WAIT_TIMEOUT) session.close() except (remote.LoginError, virt_vm.VMError, IndexError) as e: LOG.warn(e) @@ -1700,13 +1855,11 @@ def postprocess(test, params, env): for vm in env.get_all_vms(): destroy = False vm_params = params.object_params(vm.name) - for image in vm_params.objects('images'): - if params.object_params(image).get('remove_image') == 'yes': + for image in vm_params.objects("images"): + if params.object_params(image).get("remove_image") == "yes": destroy = True if destroy and not vm.is_dead(): - LOG.debug( - 'Image of VM %s was removed, destroying it.', - vm.name) + LOG.debug("Image of VM %s was removed, destroying it.", vm.name) vm.destroy() # Terminate the ip sniffer thread @@ -1720,14 +1873,19 @@ def postprocess(test, params, env): sosreport_path = utils_misc.get_sosreport(sosreport_name="host") LOG.info("Sosreport for host: %s", sosreport_path) if params.get("enable_remote_host_sosreport", "no") == "yes": - remote_params = {'server_ip': params['remote_ip'], 'server_pwd': params['remote_pwd']} - remote_params['server_user'] = params['remote_user'] + remote_params = { + "server_ip": params["remote_ip"], + "server_pwd": params["remote_pwd"], + } + remote_params["server_user"] = params["remote_user"] session = test_setup.remote_session(remote_params) - sosreport_path = utils_misc.get_sosreport(session=session, - remote_ip=params['remote_ip'], - remote_pwd=params['remote_pwd'], - remote_user=params['remote_user'], - sosreport_name="host_remote") + sosreport_path = utils_misc.get_sosreport( + session=session, + remote_ip=params["remote_ip"], + remote_pwd=params["remote_pwd"], + remote_user=params["remote_user"], + sosreport_name="host_remote", + ) LOG.info("Sosreport for remote host: %s", sosreport_path) living_vms = [vm for vm in env.get_all_vms() if vm.is_alive()] # Close all monitor socket connections of living vm. @@ -1747,7 +1905,11 @@ def postprocess(test, params, env): vm_type = params.get("vm_type") cpu_family = "unknown" try: - cpu_family = cpu_utils.get_family() if hasattr(cpu_utils, 'get_family') else cpu_utils.get_cpu_arch() + cpu_family = ( + cpu_utils.get_family() + if hasattr(cpu_utils, "get_family") + else cpu_utils.get_cpu_arch() + ) except Exception: LOG.warning("Could not get host cpu family") if cpu_family is not None and "power" in cpu_family: @@ -1765,10 +1927,10 @@ def postprocess(test, params, env): cpu_cmd = "grep cpu /proc/cpuinfo | awk '{print $3}' | head -n 1" server_session = test_setup.remote_session(params) cmd_output = server_session.cmd_status_output(cpu_cmd) - if (cmd_output[0] == 0): + if cmd_output[0] == 0: remote_cpu = cmd_output[1].strip().lower() cmd_output = server_session.cmd_status_output(pvr_cmd) - if (cmd_output[0] == 0): + if cmd_output[0] == 0: remote_pvr = float(cmd_output[1].strip()) server_session.close() if ("power9" in remote_cpu) and power9_compat_remote and remote_pvr < 2.2: @@ -1785,7 +1947,7 @@ def postprocess(test, params, env): libvirtd_inst = utils_libvirtd.Libvirtd() libvirtd_inst.restart() except Exception as details: - err += "\nHP cleanup: %s" % str(details).replace('\\n', '\n ') + err += "\nHP cleanup: %s" % str(details).replace("\\n", "\n ") LOG.error(details) else: _post_hugepages_surp = h.ext_hugepages_surp @@ -1795,7 +1957,7 @@ def postprocess(test, params, env): thp = test_setup.TransparentHugePageConfig(test, params, env) thp.cleanup() except Exception as details: - err += "\nTHP cleanup: %s" % str(details).replace('\\n', '\n ') + err += "\nTHP cleanup: %s" % str(details).replace("\\n", "\n ") LOG.error(details) for kvm_module in KVM_MODULE_HANDLERS: @@ -1806,7 +1968,7 @@ def postprocess(test, params, env): ksm = test_setup.KSMConfig(params, env) ksm.cleanup(env) except Exception as details: - err += "\nKSM cleanup: %s" % str(details).replace('\\n', '\n ') + err += "\nKSM cleanup: %s" % str(details).replace("\\n", "\n ") LOG.error(details) if params.get("setup_egd") == "yes" and params.get("kill_vm") == "yes": @@ -1814,7 +1976,7 @@ def postprocess(test, params, env): egd = test_setup.EGDConfig(params, env) egd.cleanup() except Exception as details: - err += "\negd.pl cleanup: %s" % str(details).replace('\\n', '\n ') + err += "\negd.pl cleanup: %s" % str(details).replace("\\n", "\n ") LOG.error(details) if vm_type == "libvirt": @@ -1826,11 +1988,10 @@ def postprocess(test, params, env): libvirtd_inst = utils_libvirtd.Libvirtd(all_daemons=True) libvirtd_inst.restart() except test_setup.PolkitConfigCleanupError as e: - err += "\nPolkit cleanup: %s" % str(e).replace('\\n', '\n ') + err += "\nPolkit cleanup: %s" % str(e).replace("\\n", "\n ") LOG.error(e) except Exception as details: - err += "\nPolkit cleanup: %s" % str(details - ).replace('\\n', '\n ') + err += "\nPolkit cleanup: %s" % str(details).replace("\\n", "\n ") LOG.error("Unexpected error: %s" % details) if params.get("enable_libvirtd_debug_log", "yes") == "yes": libvirtd_debug_log = test_setup.LibvirtdDebugLog(test) @@ -1839,12 +2000,16 @@ def postprocess(test, params, env): # Execute any post_commands if params.get("post_command"): try: - process_command(test, params, env, params.get("post_command"), - int(params.get("post_command_timeout", "600")), - params.get("post_command_noncritical") == "yes") + process_command( + test, + params, + env, + params.get("post_command"), + int(params.get("post_command_timeout", "600")), + params.get("post_command_noncritical") == "yes", + ) except Exception as details: - err += "\nPostprocess command: %s" % str(details).replace('\n', - '\n ') + err += "\nPostprocess command: %s" % str(details).replace("\n", "\n ") LOG.error(details) if params.get("storage_type") == "iscsi": @@ -1852,7 +2017,7 @@ def postprocess(test, params, env): iscsidev = qemu_storage.Iscsidev(params, base_dir, "iscsi") iscsidev.cleanup() except Exception as details: - err += "\niscsi cleanup: %s" % str(details).replace('\\n', '\n ') + err += "\niscsi cleanup: %s" % str(details).replace("\\n", "\n ") LOG.error(details) if params.get("storage_type") == "lvm": @@ -1860,7 +2025,7 @@ def postprocess(test, params, env): lvmdev = env.get_lvmdev("lvm_%s" % params["main_vm"]) lvmdev.cleanup() except Exception as details: - err += "\nLVM cleanup: %s" % str(details).replace('\\n', '\n ') + err += "\nLVM cleanup: %s" % str(details).replace("\\n", "\n ") LOG.error(details) env.unregister_lvmdev("lvm_%s" % params["main_vm"]) @@ -1876,12 +2041,13 @@ def postprocess(test, params, env): seLinuxBool = utils_misc.SELinuxBoolean(params) seLinuxBool.cleanup(keep_authorized_keys=True) except Exception as details: - err += "\nnfs cleanup: %s" % str(details).replace('\\n', '\n ') + err += "\nnfs cleanup: %s" % str(details).replace("\\n", "\n ") # cleanup migration presetup in post process if migration_setup: - dest_uri = libvirt_vm.complete_uri(params.get("server_ip", - params.get("remote_ip"))) + dest_uri = libvirt_vm.complete_uri( + params.get("server_ip", params.get("remote_ip")) + ) migrate_setup = migration.MigrationTest() migrate_setup.migrate_pre_setup(dest_uri, params, cleanup=True) if params.get("setup_ssh") == "yes" and params.get("ssh_conn_obj"): @@ -1894,10 +2060,12 @@ def postprocess(test, params, env): ignore_result = params.get("host_dmesg_ignore", "no") == "yes" dmesg_log_file = utils_misc.get_path(test.debugdir, dmesg_log_file) try: - utils_misc.verify_dmesg(dmesg_log_file=dmesg_log_file, - ignore_result=ignore_result, - level_check=level, - expected_dmesg=expected_host_dmesg) + utils_misc.verify_dmesg( + dmesg_log_file=dmesg_log_file, + ignore_result=ignore_result, + level_check=level, + expected_dmesg=expected_host_dmesg, + ) except exceptions.TestFail as details: err += "\nHost dmesg verification failed: %s" % details @@ -1909,7 +2077,9 @@ def postprocess(test, params, env): try: postprocess_vm_off_hook(test, params, env) # pylint: disable=E1102 except Exception as details: - err += "\nPostprocessing dead vm hook: %s" % str(details).replace('\\n', '\n ') + err += "\nPostprocessing dead vm hook: %s" % str(details).replace( + "\\n", "\n " + ) LOG.error(details) if err: @@ -1934,8 +2104,7 @@ def _take_screendumps(test, params, env): global _screendump_thread_termination_event temp_dir = test.debugdir if params.get("screendump_temp_dir"): - temp_dir = utils_misc.get_path(test.bindir, - params.get("screendump_temp_dir")) + temp_dir = utils_misc.get_path(test.bindir, params.get("screendump_temp_dir")) try: os.makedirs(temp_dir) except OSError: @@ -1976,8 +2145,11 @@ def _take_screendumps(test, params, env): LOG.warn("VM '%s' produced an invalid screendump", vm.name) os.unlink(temp_filename) continue - screendump_dir = "screendumps_%s_%s_iter%s" % (vm.name, vm_pid, - test.iteration) + screendump_dir = "screendumps_%s_%s_iter%s" % ( + vm.name, + vm_pid, + test.iteration, + ) screendump_dir = os.path.join(test.debugdir, screendump_dir) try: os.makedirs(screendump_dir) @@ -1991,19 +2163,20 @@ def _take_screendumps(test, params, env): if image_hash in cache: time_inactive = time.time() - inactivity[vm.instance] if time_inactive > inactivity_treshold: - msg = ( - "%s screen is inactive for more than %d s (%d min)" % - (vm.name, time_inactive, time_inactive // 60)) + msg = "%s screen is inactive for more than %d s (%d min)" % ( + vm.name, + time_inactive, + time_inactive // 60, + ) if inactivity_watcher == "error": try: - raise virt_vm.VMScreenInactiveError(vm, - time_inactive) + raise virt_vm.VMScreenInactiveError(vm, time_inactive) except virt_vm.VMScreenInactiveError: LOG.error(msg) # Let's reset the counter inactivity[vm.instance] = time.time() test.background_errors.put(sys.exc_info()) - elif inactivity_watcher == 'log': + elif inactivity_watcher == "log": LOG.debug(msg) else: inactivity[vm.instance] = time.time() @@ -2013,11 +2186,13 @@ def _take_screendumps(test, params, env): timestamp = os.stat(temp_filename).st_ctime image = PIL.Image.open(temp_filename) image = ppm_utils.add_timestamp(image, timestamp) - image.save(screendump_filename, format="JPEG", - quality=quality) + image.save(screendump_filename, format="JPEG", quality=quality) except (IOError, OSError) as error_detail: - LOG.warning("VM '%s' failed to produce a " - "screendump: %s", vm.name, error_detail) + LOG.warning( + "VM '%s' failed to produce a " "screendump: %s", + vm.name, + error_detail, + ) # Decrement the counter as we in fact failed to # produce a converted screendump counter[vm.instance] -= 1 @@ -2035,8 +2210,7 @@ def _take_screendumps(test, params, env): break -def store_vm_info(vm, log_filename, info_cmd='registers', - append=False, vmtype='qemu'): +def store_vm_info(vm, log_filename, info_cmd="registers", append=False, vmtype="qemu"): """ Store the info information of vm into a log file @@ -2065,9 +2239,9 @@ def store_vm_info(vm, log_filename, info_cmd='registers', return False elif vmtype == "libvirt": try: - result = virsh.qemu_monitor_command(vm.name, - "info %s" % info_cmd, - "--hmp", debug=False) + result = virsh.qemu_monitor_command( + vm.name, "info %s" % info_cmd, "--hmp", debug=False + ) output = result.stdout except Exception as details: LOG.warn(details) @@ -2075,11 +2249,11 @@ def store_vm_info(vm, log_filename, info_cmd='registers', log_filename = "%s_%s" % (log_filename, timestamp) if append: - vr_log = open(log_filename, 'r+') + vr_log = open(log_filename, "r+") vr_log.seek(0, 2) output += "\n" else: - vr_log = open(log_filename, 'w') + vr_log = open(log_filename, "w") vr_log.write(str(output)) vr_log.close() return True @@ -2091,66 +2265,73 @@ def report_result(status, cmd, results): for vm_instance in list(results.keys()): if results[vm_instance] > 0: msg += " Used to failed to get %s info from guest" % cmd - msg += " %s for %s times." % (vm_instance, - results[vm_instance]) + msg += " %s for %s times." % (vm_instance, results[vm_instance]) if msg != "%s." % status: LOG.debug(msg) global _vm_info_thread_termination_event delay = float(params.get("vm_info_delay", 5)) - cmds = params.get('vm_info_cmds', 'registers').split(',') + cmds = params.get("vm_info_cmds", "registers").split(",") cmd_details = {} for cmd in cmds: - cmd_details.update({cmd: {'counter': {}, - 'vm_info_error_count': {}} - }) + cmd_details.update({cmd: {"counter": {}, "vm_info_error_count": {}}}) while True: for cmd in cmds: for vm in env.get_all_vms(): - if vm.instance not in cmd_details[cmd]['vm_info_error_count']: - cmd_details[cmd]['vm_info_error_count'][vm.instance] = 0 + if vm.instance not in cmd_details[cmd]["vm_info_error_count"]: + cmd_details[cmd]["vm_info_error_count"][vm.instance] = 0 if not vm.is_alive(): - if cmd_details[cmd]['vm_info_error_count'][vm.instance] < 1: + if cmd_details[cmd]["vm_info_error_count"][vm.instance] < 1: LOG.warning( - "%s is not alive. Can't query the %s status", cmd, vm.name) - cmd_details[cmd]['vm_info_error_count'][vm.instance] += 1 + "%s is not alive. Can't query the %s status", cmd, vm.name + ) + cmd_details[cmd]["vm_info_error_count"][vm.instance] += 1 continue vm_pid = vm.get_pid() - vr_dir = utils_misc.get_path(test.debugdir, - "vm_info_%s_%s" % (vm.name, - vm_pid)) + vr_dir = utils_misc.get_path( + test.debugdir, "vm_info_%s_%s" % (vm.name, vm_pid) + ) try: os.makedirs(vr_dir) except OSError: pass - if vm.instance not in cmd_details[cmd]['counter']: - cmd_details[cmd]['counter'][vm.instance] = 1 - vr_filename = "%04d_%s" % (cmd_details[cmd]['counter'][vm.instance], cmd) + if vm.instance not in cmd_details[cmd]["counter"]: + cmd_details[cmd]["counter"][vm.instance] = 1 + vr_filename = "%04d_%s" % ( + cmd_details[cmd]["counter"][vm.instance], + cmd, + ) vr_filename = utils_misc.get_path(vr_dir, vr_filename) vmtype = params.get("vm_type") stored_log = store_vm_info(vm, vr_filename, cmd, vmtype=vmtype) - if cmd_details[cmd]['vm_info_error_count'][vm.instance] >= 1: - LOG.debug("%s alive now. Used to failed to get register" - " info from guest %s" - " times", vm.name, cmd_details[cmd]['vm_info_error_count'][vm.instance]) - cmd_details[cmd]['vm_info_error_count'][vm.instance] = 0 + if cmd_details[cmd]["vm_info_error_count"][vm.instance] >= 1: + LOG.debug( + "%s alive now. Used to failed to get register" + " info from guest %s" + " times", + vm.name, + cmd_details[cmd]["vm_info_error_count"][vm.instance], + ) + cmd_details[cmd]["vm_info_error_count"][vm.instance] = 0 if stored_log: - cmd_details[cmd]['counter'][vm.instance] += 1 + cmd_details[cmd]["counter"][vm.instance] += 1 if _vm_info_thread_termination_event is not None: if _vm_info_thread_termination_event.is_set(): _vm_info_thread_termination_event = None for cmd in cmds: - report_result("Thread quit", cmd, - cmd_details[cmd]['vm_info_error_count']) + report_result( + "Thread quit", cmd, cmd_details[cmd]["vm_info_error_count"] + ) break _vm_info_thread_termination_event.wait(delay) else: for cmd in cmds: - report_result("Thread quit", cmd, - cmd_details[cmd]['vm_info_error_count']) + report_result( + "Thread quit", cmd, cmd_details[cmd]["vm_info_error_count"] + ) # Exit event was deleted, exit this thread break diff --git a/virttest/error_context.py b/virttest/error_context.py index 84687c4137..714178f11b 100644 --- a/virttest/error_context.py +++ b/virttest/error_context.py @@ -5,8 +5,13 @@ # Add names you want to be imported by 'from errors import *' to this list. # This must be list not a tuple as we modify it to include all of our # the Exception classes we define below at the end of this file. -__all__ = ['format_error', 'context_aware', 'context', 'get_context', - 'exception_context'] +__all__ = [ + "format_error", + "context_aware", + "context", + "get_context", + "exception_context", +] def format_error(): @@ -14,9 +19,9 @@ def format_error(): trace = format_exception(t, o, tb) # Clear the backtrace to prevent a circular reference # in the heap -- as per tutorial - tb = '' + tb = "" - return ''.join(trace) + return "".join(trace) # Exception context information: @@ -127,6 +132,7 @@ def join_contexts(s1, s2): def context_aware(fn): """A decorator that must be applied to functions that call context().""" + def new_fn(*args, **kwargs): _new_context() _new_context("(%s)" % fn.__name__) @@ -140,6 +146,7 @@ def new_fn(*args, **kwargs): finally: _pop_context() _pop_context() + new_fn.__name__ = fn.__name__ new_fn.__doc__ = fn.__doc__ new_fn.__dict__.update(fn.__dict__) diff --git a/virttest/funcatexit.py b/virttest/funcatexit.py index 5b11ab4910..9c669383eb 100644 --- a/virttest/funcatexit.py +++ b/virttest/funcatexit.py @@ -50,22 +50,25 @@ def register(env, test_type, func, *targs, **kargs): """ # Check for unpickable arguments if func.__name__ not in func.__globals__: - raise exceptions.TestError("Trying to register function '%s', which is not " - "declared at module scope (not in globals). " - "Please contact the test developer to fix it." - % func) + raise exceptions.TestError( + "Trying to register function '%s', which is not " + "declared at module scope (not in globals). " + "Please contact the test developer to fix it." % func + ) for arg in targs: - if hasattr(arg, '__slots__') and not hasattr(arg, '__getstate__'): - raise exceptions.TestError("Trying to register exitfunction '%s' with " - "unpickable targument '%s'. Please contact " - "the test developer to fix it." - % (func, arg)) + if hasattr(arg, "__slots__") and not hasattr(arg, "__getstate__"): + raise exceptions.TestError( + "Trying to register exitfunction '%s' with " + "unpickable targument '%s'. Please contact " + "the test developer to fix it." % (func, arg) + ) for key, arg in six.iteritems(kargs): - if hasattr(arg, '__slots__') and not hasattr(arg, '__getstate__'): - raise exceptions.TestError("Trying to register exitfunction '%s' with " - "unpickable kargument '%s=%s'. Please " - "contact the test developer to fix it." - % (func, key, arg)) + if hasattr(arg, "__slots__") and not hasattr(arg, "__getstate__"): + raise exceptions.TestError( + "Trying to register exitfunction '%s' with " + "unpickable kargument '%s=%s'. Please " + "contact the test developer to fix it." % (func, key, arg) + ) exithandlers = "exithandlers__%s" % test_type if not env.data.get(exithandlers): env.data[exithandlers] = [] diff --git a/virttest/gluster.py b/virttest/gluster.py index a3cf470977..1e9004dc7e 100644 --- a/virttest/gluster.py +++ b/virttest/gluster.py @@ -23,7 +23,7 @@ from virttest import utils_net from virttest import error_context -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class GlusterError(Exception): @@ -31,7 +31,6 @@ class GlusterError(Exception): class GlusterBrickError(GlusterError): - def __init__(self, error_mgs): super(GlusterBrickError, self).__init__(error_mgs) self.error_mgs = error_mgs @@ -48,7 +47,7 @@ def glusterd_start(): cmd = "service glusterd status" output = process.run(cmd, ignore_status=True).stdout_text # The blank before 'active' makes a distinction with 'inactive' - if ' active' not in output or 'running' not in output: + if " active" not in output or "running" not in output: cmd = "service glusterd start" error_context.context("Starting gluster daemon failed") output = process.run(cmd).stdout_text @@ -63,14 +62,13 @@ def is_gluster_vol_started(vol_name, session=None): :param session: ShellSession object of remote host """ cmd = "gluster volume info %s" % vol_name - error_context.context( - "Gluster volume info failed for volume: %s" % vol_name) + error_context.context("Gluster volume info failed for volume: %s" % vol_name) if session: vol_info = session.cmd_output(cmd) else: vol_info = process.run(cmd).stdout_text - volume_status = re.findall(r'Status: (\S+)', vol_info) - if 'Started' in volume_status: + volume_status = re.findall(r"Status: (\S+)", vol_info) + if "Started" in volume_status: return True else: return False @@ -86,8 +84,7 @@ def gluster_vol_start(vol_name, session=None): """ # Check if the volume is stopped, if then start if not is_gluster_vol_started(vol_name, session): - error_context.context( - "Gluster volume start failed for volume; %s" % vol_name) + error_context.context("Gluster volume start failed for volume; %s" % vol_name) cmd = "gluster volume start %s" % vol_name if session: session.cmd(cmd) @@ -158,7 +155,7 @@ def is_gluster_vol_avail(vol_name, session=None): output = session.cmd_output(cmd) else: output = process.run(cmd).stdout_text - volume_name = re.findall(r'Volume Name: (%s)\n' % vol_name, output) + volume_name = re.findall(r"Volume Name: (%s)\n" % vol_name, output) if volume_name: return gluster_vol_start(vol_name, session) @@ -194,7 +191,7 @@ def gluster_brick_delete(brick_path, session=None): :param brick_path: path of gluster brick :param session: ShellSession object of remote host """ - cmd2 = 'rm -rf %s' % brick_path + cmd2 = "rm -rf %s" % brick_path if session: cmd1_str = "session.cmd_status('[ ! -d %s ]' % brick_path)" cmd2_str = "session.cmd(cmd2)" @@ -232,8 +229,12 @@ def gluster_vol_create(vol_name, hostname, brick_path, force=False, session=None else: force_opt = "" - cmd = "gluster volume create %s %s:/%s %s" % (vol_name, hostname, - brick_path, 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) @@ -253,8 +254,7 @@ def glusterfs_mount(g_uri, mount_point, session=None): :session: mount within the session if given """ glusterfs_umount(g_uri, mount_point, session) - utils_disk.mount(g_uri, mount_point, "glusterfs", None, - False, session) + utils_disk.mount(g_uri, mount_point, "glusterfs", None, False, session) @error_context.context_aware @@ -268,8 +268,7 @@ def glusterfs_umount(g_uri, mount_point, session=None): :session: mount within the session if given :return: if unmounted return True else return False """ - return utils_disk.umount(g_uri, mount_point, - "fuse.glusterfs", False, session) + return utils_disk.umount(g_uri, mount_point, "fuse.glusterfs", False, session) @error_context.context_aware @@ -287,7 +286,7 @@ def glusterfs_is_mounted(g_uri, session=None): @error_context.context_aware def create_gluster_vol(params): vol_name = params.get("gluster_volume_name") - force = params.get('force_recreate_gluster') == "yes" + 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 @@ -346,13 +345,14 @@ def create_gluster_uri(params, stripped=False): if is_unix_socket: sock = "?socket=%s" % params["gluster_unix_socket"] - img = params.get("image_name").split('/')[-1] + img = params.get("image_name").split("/")[-1] fmt = params.get("image_format", "qcow2") - image_path = img if params.get_boolean( - "image_raw_device") else "%s.%s" % (img, fmt) - gluster_uri = "gluster+unix://{v}{p}{s}".format(v=volume, - p=image_path, - s=sock) + image_path = ( + img if params.get_boolean("image_raw_device") else "%s.%s" % (img, fmt) + ) + gluster_uri = "gluster+unix://{v}{p}{s}".format( + v=volume, p=image_path, s=sock + ) else: # Access gluster server by hostname or ip gluster_transport = params.get("gluster_transport") @@ -373,9 +373,7 @@ def create_gluster_uri(params, stripped=False): host = "%s%s" % (server, port) - gluster_uri = "gluster{t}://{h}{v}".format(t=transport, - h=host, - v=volume) + gluster_uri = "gluster{t}://{h}{v}".format(t=transport, h=host, v=volume) return gluster_uri @@ -396,13 +394,14 @@ def file_exists(params, filename_path): try: os.mkdir(tmpdir_path) glusterfs_mount(sg_uri, tmpdir_path) - mount_filename_path = os.path.join(tmpdir_path, - filename_path[len(g_uri):]) + 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: - LOG.error("Failed to mount gluster volume %s to" - " mount dir %s: %s" % (sg_uri, tmpdir_path, e)) + LOG.error( + "Failed to mount gluster volume %s to" + " mount dir %s: %s" % (sg_uri, tmpdir_path, e) + ) finally: if glusterfs_umount(sg_uri, tmpdir_path): try: @@ -410,8 +409,10 @@ def file_exists(params, filename_path): except OSError: pass else: - LOG.warning("Unable to unmount tmp directory %s with glusterfs" - " mount.", tmpdir_path) + LOG.warning( + "Unable to unmount tmp directory %s with glusterfs" " mount.", + tmpdir_path, + ) return ret @@ -425,9 +426,12 @@ def get_image_filename(params, image_name, image_format): if uri.startswith("gluster+unix:"): filename = uri else: - img_name = image_name.split('/')[-1] - image_path = img_name if params.get_boolean( - "image_raw_device") else "%s.%s" % (img_name, image_format) + img_name = image_name.split("/")[-1] + image_path = ( + img_name + if params.get_boolean("image_raw_device") + else "%s.%s" % (img_name, image_format) + ) filename = "{vol_uri}{path}".format(vol_uri=uri, path=image_path) return filename @@ -453,7 +457,7 @@ def gluster_allow_insecure(vol_name, session=None): process.system(cmd1) output = process.run(cmd2).stdout_text - match = re.findall(r'server.allow-insecure: on', output) + match = re.findall(r"server.allow-insecure: on", output) if not match: return False @@ -468,11 +472,14 @@ def add_rpc_insecure(filepath): cmd = "cat %s" % filepath content = process.run(cmd).stdout_text - match = re.findall(r'rpc-auth-allow-insecure on', content) + match = re.findall(r"rpc-auth-allow-insecure on", content) LOG.info("match is %s", match) if not match: LOG.info("not match") - cmd = "sed -i '/end-volume/i \ \ \ \ option rpc-auth-allow-insecure on' %s" % filepath + cmd = ( + "sed -i '/end-volume/i \ \ \ \ option rpc-auth-allow-insecure on' %s" + % filepath + ) process.system(cmd, shell=True) process.system("service glusterd restart; sleep 2", shell=True) @@ -497,11 +504,17 @@ def gluster_nfs_disable(vol_name, session=None): process.system(cmd1) output = process.run(cmd2).stdout_text - return 'nfs.disable: on' in output + return "nfs.disable: on" in output -def setup_or_cleanup_gluster(is_setup, vol_name, brick_path="", pool_name="", - file_path="/etc/glusterfs/glusterd.vol", **kwargs): +def setup_or_cleanup_gluster( + is_setup, + vol_name, + brick_path="", + pool_name="", + file_path="/etc/glusterfs/glusterd.vol", + **kwargs +): # pylint: disable=E1121 """ Set up or clean up glusterfs environment on localhost or remote. These actions @@ -537,9 +550,15 @@ def setup_or_cleanup_gluster(is_setup, vol_name, brick_path="", pool_name="", remote_user = kwargs.get("gluster_server_user") remote_pwd = kwargs.get("gluster_server_pwd") remote_identity_file = kwargs.get("gluster_identity_file") - session = remote.remote_login("ssh", ip_addr, "22", - remote_user, remote_pwd, "#", - identity_file=remote_identity_file) + session = remote.remote_login( + "ssh", + ip_addr, + "22", + remote_user, + remote_pwd, + "#", + identity_file=remote_identity_file, + ) if is_setup: if ip_addr == "": ip_addr = utils_net.get_host_ip_address() diff --git a/virttest/graphical_console.py b/virttest/graphical_console.py index 504ff124b8..88f7b5621d 100644 --- a/virttest/graphical_console.py +++ b/virttest/graphical_console.py @@ -12,9 +12,9 @@ from virttest import ppm_utils -_STR_DASH = '-' +_STR_DASH = "-" -KEYMAP_DIR = os.path.join(data_dir.get_shared_dir(), 'keymaps') +KEYMAP_DIR = os.path.join(data_dir.get_shared_dir(), "keymaps") def get_keymap(keymap_file): @@ -63,6 +63,7 @@ def motion(src, dst): break yield (round(x), round(y)) yield (end_x, end_y) + return motion @@ -70,6 +71,7 @@ class UnsupportedKeyError(Exception): """ Unsupported Key Error for Console """ + pass @@ -77,6 +79,7 @@ class BaseConsole(object): """ Base Console """ + KEY_DOWN = 1 KEY_UP = 0 @@ -93,7 +96,7 @@ def __init__(self, vm, logfile=None): self._width, self._height = self.screen_size # TODO: screen size is changeable, should ensure they were synchronized before access. if not self._pointer_pos: - self._set_pointer_pos((self._width//2, self._height//2)) + self._set_pointer_pos((self._width // 2, self._height // 2)) # TODO: logfile trace def key_press(self, keystroke, interval=0): @@ -335,6 +338,7 @@ class DummyConsole(BaseConsole): """ Dummy console """ + pass @@ -343,6 +347,7 @@ class QMPConsole(BaseConsole): """ QMP console """ + KEY_DOWN = True KEY_UP = False @@ -410,10 +415,10 @@ def _key_event(self, down, keys): keys = [keys] for key in keys: k = self._key_convert(key) - event = {"type": "key", - "data": {"down": down, - "key": {"type": "qcode", - "data": k}}} + event = { + "type": "key", + "data": {"down": down, "key": {"type": "qcode", "data": k}}, + } events.append(event) self._vm.qmp_monitors[0].input_send_event(events) @@ -425,9 +430,7 @@ def _btn_event(self, event, btn): :param btn: Mouse button. """ - events = [{"type": "btn", - "data": {"down": event, - "button": btn}}] + events = [{"type": "btn", "data": {"down": event, "button": btn}}] self._vm.monitor.input_send_event(events) def _scroll_event(self, scroll): @@ -453,13 +456,11 @@ def _motion_event(self, pos, absolute): """ x, y = self._translate_pos_qmp(pos, absolute) - mtype = 'abs' if absolute else 'rel' - events = [{"type": mtype, - "data": {"axis": "x", - "value": int(x)}}, - {"type": mtype, - "data": {"axis": "y", - "value": int(y)}}] + mtype = "abs" if absolute else "rel" + events = [ + {"type": mtype, "data": {"axis": "x", "value": int(x)}}, + {"type": mtype, "data": {"axis": "y", "value": int(y)}}, + ] self._vm.monitor.input_send_event(events) self._set_pointer_pos(pos) @@ -491,8 +492,9 @@ def screen_size(self): Returns a tuple of 2 integers """ - tmp_dir = os.path.join(data_dir.get_tmp_dir(), - "graphic_console_%s" % self._vm.name) + tmp_dir = os.path.join( + data_dir.get_tmp_dir(), "graphic_console_%s" % self._vm.name + ) if not os.path.exists(tmp_dir): os.mkdir(tmp_dir) image = os.path.join(tmp_dir, "screendump") diff --git a/virttest/guest_agent.py b/virttest/guest_agent.py index f0296cfb20..192fa1d7bb 100644 --- a/virttest/guest_agent.py +++ b/virttest/guest_agent.py @@ -19,7 +19,7 @@ import six -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class VAgentError(MonitorError): @@ -31,7 +31,6 @@ class VAgentConnectError(VAgentError): class VAgentSocketError(VAgentError): - def __init__(self, msg, e): VAgentError.__init__(self) self.msg = msg @@ -54,7 +53,6 @@ class VAgentNotSupportedError(VAgentError): class VAgentCmdError(VAgentError): - def __init__(self, cmd, args, data): VAgentError.__init__(self) self.ecmd = cmd @@ -62,23 +60,22 @@ def __init__(self, cmd, args, data): self.edata = data def __str__(self): - return ("Virt Agent command %r failed (arguments: %r, " - "error message: %r)" % (self.ecmd, self.eargs, self.edata)) + return ( + "Virt Agent command %r failed (arguments: %r, " + "error message: %r)" % (self.ecmd, self.eargs, self.edata) + ) class VAgentCmdNotSupportedError(VAgentError): - def __init__(self, cmd): VAgentError.__init__(self) self.ecmd = cmd def __str__(self): - return ("The command %s is not supported by the current version qga" - % self.ecmd) + return "The command %s is not supported by the current version qga" % self.ecmd class VAgentSyncError(VAgentError): - def __init__(self, vm_name): VAgentError.__init__(self) self.vm_name = vm_name @@ -92,7 +89,6 @@ class VAgentSuspendError(VAgentError): class VAgentSuspendUnknownModeError(VAgentSuspendError): - def __init__(self, mode): VAgentSuspendError.__init__(self) self.mode = mode @@ -102,7 +98,6 @@ def __str__(self): class VAgentFreezeStatusError(VAgentError): - def __init__(self, vm_name, status, expected): VAgentError.__init__(self) self.vm_name = vm_name @@ -110,8 +105,11 @@ def __init__(self, vm_name, status, expected): self.expected = expected def __str__(self): - return ("Unexpected guest FS status '%s' (expected '%s') in vm " - "'%s'" % (self.status, self.expected, self.vm_name)) + return "Unexpected guest FS status '%s' (expected '%s') in vm " "'%s'" % ( + self.status, + self.expected, + self.vm_name, + ) class QemuAgent(Monitor): @@ -141,8 +139,15 @@ class QemuAgent(Monitor): FSFREEZE_STATUS_FROZEN = "frozen" FSFREEZE_STATUS_THAWED = "thawed" - def __init__(self, vm, name, serial_type, gagent_params, - get_supported_cmds=False, suppress_exceptions=False): + def __init__( + self, + vm, + name, + serial_type, + gagent_params, + get_supported_cmds=False, + suppress_exceptions=False, + ): """ Connect to the guest agent socket, Also make sure the json module is available. @@ -165,16 +170,18 @@ def __init__(self, vm, name, serial_type, gagent_params, """ try: if serial_type not in self.SUPPORTED_SERIAL_TYPE: - raise VAgentNotSupportedError("Not supported serial type: " - "'%s'" % serial_type) + raise VAgentNotSupportedError( + "Not supported serial type: " "'%s'" % serial_type + ) Monitor.__init__(self, vm, name, gagent_params) # Make sure json is available try: json except NameError: - raise VAgentNotSupportedError("guest agent requires the json" - " module (Python 2.6 and up)") + raise VAgentNotSupportedError( + "guest agent requires the json" " module (Python 2.6 and up)" + ) # Set a reference to the VM object that has this GuestAgent. self.vm = vm @@ -227,7 +234,7 @@ def _read_objects(self, timeout=READ_OBJECTS_TIMEOUT): objs = [] for line in s.splitlines(): try: - if line[0:1] == b'\xff': + if line[0:1] == b"\xff": line = line[1:] objs += [json.loads(line)] self._log_lines(line.decode(errors="replace")) @@ -277,16 +284,19 @@ def _sync(self, sync_mode="guest-sync", timeout=RESPONSE_TIMEOUT * 3): :param sync_mode: sync or sync-delimited :return: True if socket is synced. """ + def check_result(response): if response: self._log_response(cmd, r) if "return" in response: return response["return"] if "error" in response: - raise VAgentError("Get an error message when waiting for sync" - " with qemu guest agent, check the debug log" - " for the future message," - " detail: '%s'" % r["error"]) + raise VAgentError( + "Get an error message when waiting for sync" + " with qemu guest agent, check the debug log" + " for the future message," + " detail: '%s'" % r["error"] + ) cmd = sync_mode rnd_num = random.randint(1000, 9999) @@ -318,8 +328,9 @@ def _get_supported_cmds(self): cmds = self.guest_info() if cmds and "supported_commands" in cmds: cmd_list = cmds["supported_commands"] - self._supported_cmds = [n["name"] for n in cmd_list if - isinstance(n, dict) and "name" in n] + self._supported_cmds = [ + n["name"] for n in cmd_list if isinstance(n, dict) and "name" in n + ] if not self._supported_cmds: # If initiation fails, set supported list to a None-only list. @@ -356,8 +367,7 @@ def _log_command(self, cmd, debug=True, extra_str=""): :param extra_str: Extra string would be printed in log. """ if self.debug_log or debug: - LOG.debug("(vagent %s) Sending command '%s' %s", - self.name, cmd, extra_str) + LOG.debug("(vagent %s) Sending command '%s' %s", self.name, cmd, extra_str) def _log_response(self, cmd, resp, debug=True): """ @@ -367,6 +377,7 @@ def _log_response(self, cmd, resp, debug=True): :param resp: Response from guest agent command. :param debug: Whether to print the commands. """ + def _log_output(o, indent=0): LOG.debug("(vagent %s) %s%s", self.name, " " * indent, o) @@ -391,8 +402,7 @@ def _dump_dict(di, indent=0): _log_output(o, indent) if self.debug_log or debug: - LOG.debug("(vagent %s) Response to '%s' " - "(re-formatted)", self.name, cmd) + LOG.debug("(vagent %s) Response to '%s' " "(re-formatted)", self.name, cmd) if isinstance(resp, dict): _dump_dict(resp) elif isinstance(resp, list): @@ -402,8 +412,7 @@ def _dump_dict(di, indent=0): _log_output(l) # Public methods - def cmd(self, cmd, args=None, timeout=CMD_TIMEOUT, debug=True, - success_resp=True): + def cmd(self, cmd, args=None, timeout=CMD_TIMEOUT, debug=True, success_resp=True): """ Send a guest agent command and return the response if success_resp. @@ -451,8 +460,9 @@ def cmd_raw(self, data, timeout=CMD_TIMEOUT, success_resp=True): :raise VAgentProtocolError: Raised if no response is received """ if not self._acquire_lock(): - raise VAgentLockError("Could not acquire exclusive lock to send " - "data: %r" % data) + raise VAgentLockError( + "Could not acquire exclusive lock to send " "data: %r" % data + ) try: self._read_objects() @@ -468,8 +478,7 @@ def cmd_raw(self, data, timeout=CMD_TIMEOUT, success_resp=True): self._lock.release() if r is None: - raise VAgentProtocolError( - "Received no response to data: %r" % data) + raise VAgentProtocolError("Received no response to data: %r" % data) return r def cmd_obj(self, obj, timeout=CMD_TIMEOUT): @@ -509,8 +518,11 @@ def shutdown(self, mode=SHUTDOWN_MODE_POWERDOWN): cmd = "guest-shutdown" self.check_has_command(cmd) args = None - if mode in [self.SHUTDOWN_MODE_POWERDOWN, self.SHUTDOWN_MODE_REBOOT, - self.SHUTDOWN_MODE_HALT]: + if mode in [ + self.SHUTDOWN_MODE_POWERDOWN, + self.SHUTDOWN_MODE_REBOOT, + self.SHUTDOWN_MODE_HALT, + ]: args = {"mode": mode} try: self.cmd(cmd=cmd, args=args) @@ -539,10 +551,13 @@ def set_user_password(self, password, crypted=False, username="root"): if crypted: openssl_cmd = "openssl passwd -6 %s" % password - password = process.run(openssl_cmd).stdout_text.strip('\n') + password = process.run(openssl_cmd).stdout_text.strip("\n") - args = {"crypted": crypted, "username": username, - "password": base64.b64encode(password.encode()).decode()} + args = { + "crypted": crypted, + "username": username, + "password": base64.b64encode(password.encode()).decode(), + } return self.cmd(cmd=cmd, args=args) @error_context.context_aware @@ -633,13 +648,16 @@ def suspend(self, mode=SUSPEND_MODE_RAM): ``suspend`` is unsupported. :raise VAgentSuspendUnknownModeError: Raise if mode is not supported. """ - error_context.context( - "Suspend guest '%s' to '%s'" % (self.vm.name, mode)) + error_context.context("Suspend guest '%s' to '%s'" % (self.vm.name, mode)) - if mode not in [self.SUSPEND_MODE_DISK, self.SUSPEND_MODE_RAM, - self.SUSPEND_MODE_HYBRID]: - raise VAgentSuspendUnknownModeError("Not supported suspend" - " mode '%s'" % mode) + if mode not in [ + self.SUSPEND_MODE_DISK, + self.SUSPEND_MODE_RAM, + self.SUSPEND_MODE_HYBRID, + ]: + raise VAgentSuspendUnknownModeError( + "Not supported suspend" " mode '%s'" % mode + ) cmd = "guest-suspend-%s" % mode self.check_has_command(cmd) @@ -674,8 +692,13 @@ def verify_fsfreeze_status(self, expected): raise VAgentFreezeStatusError(self.vm.name, status, expected) @error_context.context_aware - def fsfreeze(self, check_status=True, timeout=FSFREEZE_TIMEOUT, - fsfreeze_list=False, mountpoints=None): + def fsfreeze( + self, + check_status=True, + timeout=FSFREEZE_TIMEOUT, + fsfreeze_list=False, + mountpoints=None, + ): """ Freeze File system on guest, there are two commands, "guest-fsfreeze-freeze" and "guest-fsfreeze-freeze-list". @@ -804,8 +827,9 @@ def guest_file_write(self, handle, content, count=None): """ cmd = "guest-file-write" con_encode = base64.b64encode(content.encode()).decode() - return self._cmd_args_update(cmd, handle=handle, - buf_b64=con_encode, count=count) + return self._cmd_args_update( + cmd, handle=handle, buf_b64=con_encode, count=count + ) def guest_file_read(self, handle, count=None): """ @@ -841,11 +865,11 @@ def guest_file_seek(self, handle, offset, whence): :return: a dict with position and eof. """ cmd = "guest-file-seek" - return self._cmd_args_update(cmd, handle=handle, offset=offset, - whence=whence) + return self._cmd_args_update(cmd, handle=handle, offset=offset, whence=whence) - def guest_exec(self, path, arg=None, env=None, input_data=None, - capture_output=None): + def guest_exec( + self, path, arg=None, env=None, input_data=None, capture_output=None + ): """ Execute a command in the guest. @@ -858,9 +882,14 @@ def guest_exec(self, path, arg=None, env=None, input_data=None, :return: PID on success """ cmd = "guest-exec" - return self._cmd_args_update(cmd, path=path, arg=arg, env=env, - input_data=input_data, - capture_output=capture_output) + return self._cmd_args_update( + cmd, + path=path, + arg=arg, + env=env, + input_data=input_data, + capture_output=capture_output, + ) def guest_exec_status(self, pid): """ @@ -893,9 +922,8 @@ def ssh_add_authorized_keys(self, username, *keys, **kwargs): """ cmd = "guest-ssh-add-authorized-keys" keys = list(keys) - reset = kwargs.get('reset') - return self._cmd_args_update(cmd, username=username, - keys=keys, reset=reset) + reset = kwargs.get("reset") + return self._cmd_args_update(cmd, username=username, keys=keys, reset=reset) def ssh_remove_authorized_keys(self, username, *keys): """ diff --git a/virttest/http_server.py b/virttest/http_server.py index c1d0c0c618..de57dc7757 100644 --- a/virttest/http_server.py +++ b/virttest/http_server.py @@ -1,6 +1,7 @@ import os import posixpath import logging + try: from urllib.parse import unquote, urlparse except ImportError: @@ -15,11 +16,10 @@ from avocado.utils.astring import to_text -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class HTTPRequestHandler(SimpleHTTPRequestHandler): - def do_GET(self): """ Serve a GET request. @@ -37,13 +37,13 @@ def do_GET(self): f.close() def parse_header_byte_range(self): - range_param = 'Range' - range_discard = 'bytes=' + range_param = "Range" + range_discard = "bytes=" if range_param in self.headers: rg = self.headers.get(range_param) if rg.startswith(range_discard): - rg = rg[len(range_discard):] - begin, end = rg.split('-') + rg = rg[len(range_discard) :] + begin, end = rg.split("-") return (int(begin), int(end)) return None @@ -72,7 +72,7 @@ def send_head_range(self, range_begin, range_end): # Always read in binary mode. Opening files in text mode may cause # newline translations, making the actual size of the content # transmitted *less* than the content-length! - f = open(path, 'rb') + f = open(path, "rb") except IOError: self.send_error(404, "File not found") return None @@ -81,9 +81,9 @@ def send_head_range(self, range_begin, range_end): range_size = str(range_end - range_begin + 1) self.send_header("Accept-Ranges", "bytes") self.send_header("Content-Length", range_size) - self.send_header("Content-Range", "bytes %s-%s/%s" % (range_begin, - range_end, - file_size)) + self.send_header( + "Content-Range", "bytes %s-%s/%s" % (range_begin, range_end, file_size) + ) self.send_header("Content-type", ctype) self.end_headers() return f @@ -100,7 +100,7 @@ def translate_path(self, path): # abandon query parameters path = urlparse(to_text(path))[2] path = posixpath.normpath(unquote(path)) - words = path.split('/') + words = path.split("/") words = list(filter(None, words)) path = self.server.cwd for word in words: @@ -112,23 +112,25 @@ def translate_path(self, path): return path def address_string(self): - ''' + """ This HTTP server does not care about name resolution for the requests The first reason is that most of the times our clients are going to be virtual machines without a proper name resolution setup. Also, by not resolving names, we should be a bit faster and be resilient about misconfigured or resilient name servers. - ''' + """ return self.client_address[0] def log_message(self, fmt, *args): - LOG.debug("builtin http server handling request from %s: %s" % - (self.address_string(), fmt % args)) + LOG.debug( + "builtin http server handling request from %s: %s" + % (self.address_string(), fmt % args) + ) def http_server(port=8000, cwd=None, terminate_callable=None): - http = HTTPServer(('', port), HTTPRequestHandler) + http = HTTPServer(("", port), HTTPRequestHandler) http.timeout = 1 if cwd is None: @@ -147,5 +149,5 @@ def http_server(port=8000, cwd=None, terminate_callable=None): http.handle_request() -if __name__ == '__main__': +if __name__ == "__main__": http_server() diff --git a/virttest/installer.py b/virttest/installer.py index 1911278fda..45ec2c23f1 100644 --- a/virttest/installer.py +++ b/virttest/installer.py @@ -12,8 +12,12 @@ from . import qemu_installer from . import libvirt_installer -__all__ = ['InstallerRegistry', 'INSTALLER_REGISTRY', 'make_installer', - 'run_installers'] +__all__ = [ + "InstallerRegistry", + "INSTALLER_REGISTRY", + "make_installer", + "run_installers", +] class InstallerRegistry(dict): @@ -39,7 +43,7 @@ class InstallerRegistry(dict): installer if set to true. """ - DEFAULT_VIRT_NAME = 'base' + DEFAULT_VIRT_NAME = "base" def __init__(self, **kwargs): dict.__init__(self, **kwargs) @@ -99,44 +103,26 @@ def get_modes(self, virt=None): # # Register base installers # -INSTALLER_REGISTRY.register('yum', - base_installer.YumInstaller) -INSTALLER_REGISTRY.register('koji', - base_installer.KojiInstaller) -INSTALLER_REGISTRY.register('git_repo', - base_installer.GitRepoInstaller) -INSTALLER_REGISTRY.register('local_src', - base_installer.LocalSourceDirInstaller) -INSTALLER_REGISTRY.register('local_tar', - base_installer.LocalSourceTarInstaller) -INSTALLER_REGISTRY.register('remote_tar', - base_installer.RemoteSourceTarInstaller) +INSTALLER_REGISTRY.register("yum", base_installer.YumInstaller) +INSTALLER_REGISTRY.register("koji", base_installer.KojiInstaller) +INSTALLER_REGISTRY.register("git_repo", base_installer.GitRepoInstaller) +INSTALLER_REGISTRY.register("local_src", base_installer.LocalSourceDirInstaller) +INSTALLER_REGISTRY.register("local_tar", base_installer.LocalSourceTarInstaller) +INSTALLER_REGISTRY.register("remote_tar", base_installer.RemoteSourceTarInstaller) # # Register KVM specific installers # -INSTALLER_REGISTRY.register('yum', - base_installer.YumInstaller, - 'qemu') -INSTALLER_REGISTRY.register('koji', - base_installer.KojiInstaller, - 'qemu') -INSTALLER_REGISTRY.register('git_repo', - qemu_installer.GitRepoInstaller, - 'qemu') -INSTALLER_REGISTRY.register('local_src', - qemu_installer.LocalSourceDirInstaller, - 'qemu') -INSTALLER_REGISTRY.register('local_tar', - qemu_installer.LocalSourceTarInstaller, - 'qemu') -INSTALLER_REGISTRY.register('remote_tar', - qemu_installer.RemoteSourceTarInstaller, - 'qemu') - -INSTALLER_REGISTRY.register('git_repo', - libvirt_installer.GitRepoInstaller, - 'libvirt') +INSTALLER_REGISTRY.register("yum", base_installer.YumInstaller, "qemu") +INSTALLER_REGISTRY.register("koji", base_installer.KojiInstaller, "qemu") +INSTALLER_REGISTRY.register("git_repo", qemu_installer.GitRepoInstaller, "qemu") +INSTALLER_REGISTRY.register("local_src", qemu_installer.LocalSourceDirInstaller, "qemu") +INSTALLER_REGISTRY.register("local_tar", qemu_installer.LocalSourceTarInstaller, "qemu") +INSTALLER_REGISTRY.register( + "remote_tar", qemu_installer.RemoteSourceTarInstaller, "qemu" +) + +INSTALLER_REGISTRY.register("git_repo", libvirt_installer.GitRepoInstaller, "libvirt") def installer_name_split(fullname, virt=None): @@ -148,7 +134,7 @@ def installer_name_split(fullname, virt=None): local_src_foo -> (local_src, foo) """ for mode in INSTALLER_REGISTRY.get_modes(virt): - if fullname.startswith('%s_' % mode): + if fullname.startswith("%s_" % mode): _, _name = fullname.split(mode) name = _name[1:] return (mode, name) @@ -175,8 +161,10 @@ def make_installer(fullname, params, test=None): mode, name = installer_name_split(fullname, virt) if mode is None or name is None: - error_msg = ('Invalid installer mode or name for "%s". Probably an ' - 'installer has not been registered' % fullname) + error_msg = ( + 'Invalid installer mode or name for "%s". Probably an ' + "installer has not been registered" % fullname + ) if virt is not None: error_msg += ' specifically for virt type "%s"' % virt @@ -184,8 +172,7 @@ def make_installer(fullname, params, test=None): klass = INSTALLER_REGISTRY.get_installer(mode, virt) if klass is None: - raise exceptions.TestError( - 'Installer mode %s is not registered' % mode) + raise exceptions.TestError("Installer mode %s is not registered" % mode) else: return klass(mode, name, test, params) diff --git a/virttest/ip_sniffing.py b/virttest/ip_sniffing.py index 53afb4d023..bcfdb6c40e 100644 --- a/virttest/ip_sniffing.py +++ b/virttest/ip_sniffing.py @@ -21,7 +21,7 @@ from virttest.utils_logfile import log_line from virttest.utils_version import VersionInterval -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class AddrCache(object): @@ -57,8 +57,9 @@ def __setitem__(self, hwaddr, ipaddr): if self._data.get(hwaddr) == ipaddr: return self._data[hwaddr] = ipaddr - LOG.debug("Updated HWADDR (%s)<->(%s) IP pair " - "into address cache", hwaddr, ipaddr) + LOG.debug( + "Updated HWADDR (%s)<->(%s) IP pair " "into address cache", hwaddr, ipaddr + ) def __getitem__(self, hwaddr): hwaddr = self._format_hwaddr(hwaddr) @@ -184,22 +185,28 @@ def _output_logger_handler(self, line): match = self._re_sniffer_finished.match(line) if match: if match.group(1) != "0": - LOG.error("IP sniffer (%s) terminated unexpectedly! " - "please check the log to get the details " - "(status: %s)", self.command, match.group(1)) + LOG.error( + "IP sniffer (%s) terminated unexpectedly! " + "please check the log to get the details " + "(status: %s)", + self.command, + match.group(1), + ) def _start_remote(self): address, port, username, password, prompt = self._remote_opts cmd = "%s %s" % (self.command, self.options) LOG.debug("Run '%s' on host '%s'", cmd, address) - login_cmd = ("ssh -o UserKnownHostsFile=/dev/null " - "-o StrictHostKeyChecking=no " - "-o PreferredAuthentications=password -p %s %s@%s" % - (port, username, address)) + login_cmd = ( + "ssh -o UserKnownHostsFile=/dev/null " + "-o StrictHostKeyChecking=no " + "-o PreferredAuthentications=password -p %s %s@%s" + % (port, username, address) + ) self._process = aexpect.ShellSession( - login_cmd, - output_func=self._output_logger_handler) + login_cmd, output_func=self._output_logger_handler + ) handle_prompts(self._process, username, password, prompt) self._process.sendline(cmd) @@ -209,8 +216,8 @@ def _start(self): self._start_remote() else: self._process = aexpect.run_tail( - command=cmd, - output_func=self._output_logger_handler) + command=cmd, output_func=self._output_logger_handler + ) def is_alive(self): """Check if the sniffer is alive.""" @@ -314,8 +321,7 @@ def _get_version(cls, session): if session: out = session.cmd_output(cmd) else: - out = process.system_output(cmd, verbose=False, - ignore_status=True).decode() + out = process.system_output(cmd, verbose=False, ignore_status=True).decode() matches = cls._re_version.search(out) if matches: return matches.group(1) @@ -344,10 +350,12 @@ def _output_handler(self, line): if re.match(r"[0-9a-fA-F]{1,4}:\S+", packet[0]): # TODO: support DHCPv6 if not self.__dict__.setdefault("_ip6_warned", False): - LOG.warn("IPv6 address sniffing is not supported yet by " - "using TShark, please fallback to use other " - "sniffers by uninstalling TShark when testing " - "with IPv6") + LOG.warn( + "IPv6 address sniffing is not supported yet by " + "using TShark, please fallback to use other " + "sniffers by uninstalling TShark when testing " + "with IPv6" + ) self._ip6_warned = True return True @@ -358,12 +366,14 @@ class TShark1To2(TSharkSniffer): TShark sniffer class for version 1.0.0 - 2.x.x. """ - options = ("-npi any -T fields -E separator=/s -E occurrence=f " - "-E header=y -e ip.src -e ip.dst -e bootp.type -e bootp.id " - "-e bootp.hw.mac_addr -e bootp.ip.your -e bootp.option.dhcp " - "-e ipv6.src -e ipv6.dst " - # Positional arguments must be the last arguments - "'port 68 or port 546'") + options = ( + "-npi any -T fields -E separator=/s -E occurrence=f " + "-E header=y -e ip.src -e ip.dst -e bootp.type -e bootp.id " + "-e bootp.hw.mac_addr -e bootp.ip.your -e bootp.option.dhcp " + "-e ipv6.src -e ipv6.dst " + # Positional arguments must be the last arguments + "'port 68 or port 546'" + ) supported_versions = VersionInterval("[1.0.0, 3.0.0)") @@ -373,12 +383,14 @@ class TShark3ToLatest(TSharkSniffer): TShark sniffer class for version 3.0.0 - latest. """ - options = ("-npi any -T fields -E separator=/s -E occurrence=f " - "-E header=y -e ip.src -e ip.dst -e dhcp.type -e dhcp.id " - "-e dhcp.hw.mac_addr -e dhcp.ip.your -e dhcp.option.dhcp " - "-e ipv6.src -e ipv6.dst " - # Positional arguments must be the last arguments - "'port 68 or port 546'") + options = ( + "-npi any -T fields -E separator=/s -E occurrence=f " + "-E header=y -e ip.src -e ip.dst -e dhcp.type -e dhcp.id " + "-e dhcp.hw.mac_addr -e dhcp.ip.your -e dhcp.option.dhcp " + "-e ipv6.src -e ipv6.dst " + # Positional arguments must be the last arguments + "'port 68 or port 546'" + ) supported_versions = VersionInterval("[3.0.0,)") diff --git a/virttest/iscsi.py b/virttest/iscsi.py index 928b186098..143368de3d 100644 --- a/virttest/iscsi.py +++ b/virttest/iscsi.py @@ -24,7 +24,7 @@ from virttest import utils_package from virttest.staging import service -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) def get_image_filename(portal, target, lun=0, user=None, password=None): @@ -32,9 +32,12 @@ def get_image_filename(portal, target, lun=0, user=None, password=None): Form the iscsi image name, now only tcp is supported by qemu e.g. iscsi://10.66.10.26/iqn.2019-09.com.example:zhencliu/0 """ - uri = 'iscsi://{auth}{portal}/{target}/{lun}' - auth = '{user}:{password}@'.format( - user=user, password=password) if user and password else '' + uri = "iscsi://{auth}{portal}/{target}/{lun}" + auth = ( + "{user}:{password}@".format(user=user, password=password) + if user and password + else "" + ) return uri.format(auth=auth, portal=portal, target=target, lun=lun) @@ -42,8 +45,8 @@ def restart_tgtd(reset_failed=True): """ Restart tgtd service. """ - path.find_command('tgtd') - tgtd = service.Factory.create_service('tgtd') + path.find_command("tgtd") + tgtd = service.Factory.create_service("tgtd") if reset_failed: tgtd.reset_failed() if not tgtd.restart(): @@ -61,7 +64,7 @@ def iscsi_get_sessions(): sessions = [] if "No active sessions" not in output: for session in output.splitlines(): - ip_addr = session.split()[2].split(',')[0] + ip_addr = session.split()[2].split(",")[0] target = session.split()[3] sessions.append((ip_addr, target)) return sessions @@ -107,7 +110,7 @@ def iscsi_node_del(target_name=None): :params target_name: Name of the target. """ node_list = iscsi_get_nodes() - cmd = '' + cmd = "" if target_name: for node_tup in node_list: if target_name in node_tup: @@ -116,8 +119,10 @@ def iscsi_node_del(target_name=None): process.system(cmd, ignore_status=True) break if not cmd: - LOG.error("The target '%s' for delete is not in target node" - " record", target_name) + LOG.error( + "The target '%s' for delete is not in target node" " record", + target_name, + ) else: for node_tup in node_list: cmd = "iscsiadm -m node -o delete -T %s " % node_tup[1] @@ -137,14 +142,14 @@ def iscsi_logout(target_name=None): else: cmd = "iscsiadm --mode node --logout all" - output = '' + output = "" try: output = process.run(cmd).stdout_text except process.CmdError as detail: # iscsiadm will fail when no matching sessions found # This failure makes no sense when target name is not specified stderr = detail.result.stderr_text - if not target_name and 'No matching sessions' in stderr: + if not target_name and "No matching sessions" in stderr: LOG.info("%s: %s", detail, stderr) else: raise @@ -190,7 +195,7 @@ def __init__(self, params, root_dir): self.export_flag = False self.luns = None self.iscsi_lun_attrs = params.get("iscsi_lun_attrs") - self.restart_tgtd = 'yes' == params.get("restart_tgtd", "no") + self.restart_tgtd = "yes" == params.get("restart_tgtd", "no") if params.get("portal_ip"): self.portal_ip = params.get("portal_ip") else: @@ -231,20 +236,26 @@ def __init__(self, params, root_dir): self.unit = self.emulated_size[-1].upper() self.emulated_size = self.emulated_size[:-1] # maps K,M,G,T => (count, bs) - emulated_size = {'K': (1, 1), - 'M': (1, 1024), - 'G': (1024, 1024), - 'T': (1024, 1048576), - } + emulated_size = { + "K": (1, 1), + "M": (1, 1024), + "G": (1024, 1024), + "T": (1024, 1048576), + } if self.unit in emulated_size: block_size = emulated_size[self.unit][1] size = int(self.emulated_size) * emulated_size[self.unit][0] self.emulated_expect_size = block_size * size - self.create_cmd = ("dd if=/dev/zero of=%s count=%s bs=%sK" - % (self.emulated_image, size, block_size)) + self.create_cmd = "dd if=/dev/zero of=%s count=%s bs=%sK" % ( + self.emulated_image, + size, + block_size, + ) else: - raise exceptions.TestError("Image size provided is not in valid" - " format, specify proper units [K|M|G|T]") + raise exceptions.TestError( + "Image size provided is not in valid" + " format, specify proper units [K|M|G|T]" + ) else: self.emulated_image = emulated_image self.device = "device.%s" % os.path.basename(self.emulated_image) @@ -263,8 +274,9 @@ def portal_visible(self): """ Check if the portal can be found or not. """ - return bool(re.findall("%s$" % self.target, - iscsi_discover(self.portal_ip), re.M)) + return bool( + re.findall("%s$" % self.target, iscsi_discover(self.portal_ip), re.M) + ) def login(self): """ @@ -293,15 +305,14 @@ def get_device_name(self): device_name = [] if self.logged_in(): output = process.run(cmd).stdout_text - targets = output.split('Target: ')[1:] + targets = output.split("Target: ")[1:] for target in targets: if self.target in target: device_name = re.findall(pattern, target, re.S) try: device_name = "/dev/%s" % device_name[0] except IndexError: - LOG.error( - "Can not find target '%s' after login.", self.target) + LOG.error("Can not find target '%s' after login.", self.target) else: LOG.error("Session is not logged in yet.") return device_name @@ -310,9 +321,9 @@ def set_chap_auth_initiator(self): """ Set CHAP authentication for initiator. """ - name_dict = {'node.session.auth.authmethod': 'CHAP'} - name_dict['node.session.auth.username'] = self.chap_user - name_dict['node.session.auth.password'] = self.chap_passwd + name_dict = {"node.session.auth.authmethod": "CHAP"} + name_dict["node.session.auth.username"] = self.chap_user + name_dict["node.session.auth.password"] = self.chap_passwd for name in list(name_dict.keys()): cmd = "iscsiadm --mode node --targetname %s " % self.target cmd += "--op update --name %s --value %s" % (name, name_dict[name]) @@ -412,8 +423,9 @@ def get_target_account_info(self): pattern = r"Target\s+\d:\s+%s" % self.target pattern += ".*Account information:\s(.*)ACL information" try: - target_account = re.findall(pattern, target_info, - re.S)[0].strip().splitlines() + target_account = ( + re.findall(pattern, target_info, re.S)[0].strip().splitlines() + ) except IndexError: target_account = [] return list(map(str.strip, target_account)) @@ -426,8 +438,7 @@ def set_chap_auth_target(self): if self.chap_user not in self.get_chap_accounts(): self.add_chap_account() if self.chap_user in self.get_target_account_info(): - LOG.debug("Target %s already has account %s", self.target, - self.chap_user) + LOG.debug("Target %s already has account %s", self.target, self.chap_user) else: cmd = "tgtadm --lld iscsi --op bind --mode account" cmd += " --tid %s --user %s" % (self.emulated_id, self.chap_user) @@ -474,14 +485,15 @@ def export_target(self): cmd += "--tid %s -I ALL" % self.emulated_id process.system(cmd) else: - target_strs = re.findall("Target\s+(\d+):\s+%s$" % - self.target, output, re.M) - self.emulated_id = target_strs[0].split(':')[0].split()[-1] + target_strs = re.findall( + "Target\s+(\d+):\s+%s$" % self.target, output, re.M + ) + self.emulated_id = target_strs[0].split(":")[0].split()[-1] cmd = "tgtadm --lld iscsi --mode target --op show" try: output = process.run(cmd).stdout_text - except process.CmdError: # In case service stopped + except process.CmdError: # In case service stopped restart_tgtd() output = process.run(cmd).stdout_text @@ -491,11 +503,11 @@ def export_target(self): LOG.debug("Exported image already exists.") self.export_flag = True else: - tgt_str = re.search(r'.*(Target\s+\d+:\s+%s\s*.*)$' % self.target, - output, re.DOTALL) + tgt_str = re.search( + r".*(Target\s+\d+:\s+%s\s*.*)$" % self.target, output, re.DOTALL + ) if tgt_str: - luns = len(re.findall("\s+LUN:\s(\d+)", - tgt_str.group(1), re.M)) + luns = len(re.findall("\s+LUN:\s(\d+)", tgt_str.group(1), re.M)) else: luns = len(re.findall("\s+LUN:\s(\d+)", output, re.M)) cmd = "tgtadm --mode logicalunit --op new " @@ -581,10 +593,11 @@ def set_chap_auth_target(self): for all Endpoints in a TPG """ auth_cmd = "targetcli /iscsi/%s/tpg1/ " % self.target - attr_cmd = ("set attribute %s %s %s" % - ("demo_mode_write_protect=0", - "generate_node_acls=1", - "cache_dynamic_acls=1")) + attr_cmd = "set attribute %s %s %s" % ( + "demo_mode_write_protect=0", + "generate_node_acls=1", + "cache_dynamic_acls=1", + ) if self.enable_authentication: attr_cmd += " authentication=1" process.system(auth_cmd + attr_cmd) @@ -614,8 +627,7 @@ def export_target(self): if not os.path.isfile(self.emulated_image): process.system(self.create_cmd) else: - emulated_image_size = os.path.getsize( - self.emulated_image) // 1024 + emulated_image_size = os.path.getsize(self.emulated_image) // 1024 if emulated_image_size != self.emulated_expect_size: # No need to remove, rebuild is fine process.system(self.create_cmd) @@ -642,27 +654,34 @@ def export_target(self): # which enables the local file system cache. # Create a backstore - device_cmd = ("targetcli /backstores/%s/ create %s %s" % - (self.iscsi_backend, self.device, - self.emulated_image)) + device_cmd = "targetcli /backstores/%s/ create %s %s" % ( + self.iscsi_backend, + self.device, + self.emulated_image, + ) output = process.run(device_cmd).stdout_text if "Created %s" % self.iscsi_backend not in output: - raise exceptions.TestFail("Failed to create %s %s. (%s)" % - (self.iscsi_backend, self.device, - output)) + raise exceptions.TestFail( + "Failed to create %s %s. (%s)" + % (self.iscsi_backend, self.device, output) + ) # Set attribute if self.iscsi_lun_attrs: attr_cmd = "targetcli /backstores/%s/%s set attribute %s" % ( - self.iscsi_backend, self.device, self.iscsi_lun_attrs) + self.iscsi_backend, + self.device, + self.iscsi_lun_attrs, + ) process.system(attr_cmd) # Create an IQN with a target named target_name target_cmd = "targetcli /iscsi/ create %s" % self.target output = process.run(target_cmd).stdout_text if "Created target" not in output: - raise exceptions.TestFail("Failed to create target %s. (%s)" % - (self.target, output)) + raise exceptions.TestFail( + "Failed to create target %s. (%s)" % (self.target, output) + ) check_portal = "targetcli /iscsi/%s/tpg1/portals ls" % self.target portal_info = process.run(check_portal).stdout_text @@ -670,36 +689,37 @@ def export_target(self): # Create portal # 0.0.0.0 means binding to INADDR_ANY # and using default IP port 3260 - portal_cmd = ("targetcli /iscsi/%s/tpg1/portals/ create %s" - % (self.target, "0.0.0.0")) + portal_cmd = "targetcli /iscsi/%s/tpg1/portals/ create %s" % ( + self.target, + "0.0.0.0", + ) output = process.run(portal_cmd).stdout_text if "Created network portal" not in output: - raise exceptions.TestFail("Failed to create portal. (%s)" % - output) - if ("ipv6" == utils_net.IPAddress(self.portal_ip).version and - self.portal_ip not in portal_info): + raise exceptions.TestFail("Failed to create portal. (%s)" % output) + if ( + "ipv6" == utils_net.IPAddress(self.portal_ip).version + and self.portal_ip not in portal_info + ): # Ipv6 portal address can't be created by default, # create ipv6 portal if needed. - portal_cmd = ("targetcli /iscsi/%s/tpg1/portals/ create %s" - % (self.target, self.portal_ip)) + portal_cmd = "targetcli /iscsi/%s/tpg1/portals/ create %s" % ( + self.target, + self.portal_ip, + ) output = process.run(portal_cmd).stdout_text if "Created network portal" not in output: - raise exceptions.TestFail("Failed to create portal. (%s)" % - output) + raise exceptions.TestFail("Failed to create portal. (%s)" % output) # Create lun lun_cmd = "targetcli /iscsi/%s/tpg1/luns/ " % self.target - dev_cmd = "create /backstores/%s/%s" % ( - self.iscsi_backend, self.device) + dev_cmd = "create /backstores/%s/%s" % (self.iscsi_backend, self.device) output = process.run(lun_cmd + dev_cmd).stdout_text luns = re.findall(r"Created LUN (\d+).", output) if not luns: - raise exceptions.TestFail("Failed to create lun. (%s)" % - output) + raise exceptions.TestFail("Failed to create lun. (%s)" % output) self.luns = luns[0] # Set firewall if it's enabled - output = process.run("firewall-cmd --state", - ignore_status=True).stdout_text + output = process.run("firewall-cmd --state", ignore_status=True).stdout_text if re.findall("^running", output, re.M): # firewall is running process.system("firewall-cmd --permanent --add-port=3260/tcp") @@ -726,11 +746,12 @@ def export_target(self): # so that they can access all LUNs in the TPG # without further authentication. auth_cmd = "targetcli /iscsi/%s/tpg1/ " % self.target - attr_cmd = ("set attribute %s %s %s %s" % - ("authentication=0", - "demo_mode_write_protect=0", - "generate_node_acls=1", - "cache_dynamic_acls=1")) + attr_cmd = "set attribute %s %s %s %s" % ( + "authentication=0", + "demo_mode_write_protect=0", + "generate_node_acls=1", + "cache_dynamic_acls=1", + ) output = process.run(auth_cmd + attr_cmd).stdout_text LOG.info("Define access rights: %s" % output) # Discovery the target @@ -748,8 +769,10 @@ def delete_target(self): cmd = "targetcli /backstores/%s ls" % self.iscsi_backend output = process.run(cmd).stdout_text if re.findall("%s" % self.device, output, re.M): - dev_del = ("targetcli /backstores/%s/ delete %s" - % (self.iscsi_backend, self.device)) + dev_del = "targetcli /backstores/%s/ delete %s" % ( + self.iscsi_backend, + self.device, + ) process.system(dev_del) # Delete IQN @@ -774,10 +797,11 @@ class Iscsi(object): The class support different kinds of iSCSI backend (TGT and LIO), and return ISCSI instance. """ + @staticmethod def create_iSCSI(params, root_dir=data_dir.get_tmp_dir()): iscsi_instance = None - ubuntu = distro.detect().name == 'Ubuntu' + ubuntu = distro.detect().name == "Ubuntu" # check and install iscsi initiator packages if ubuntu: iscsi_package = ["open-iscsi"] @@ -785,21 +809,23 @@ def create_iSCSI(params, root_dir=data_dir.get_tmp_dir()): iscsi_package = ["iscsi-initiator-utils"] if not utils_package.package_install(iscsi_package): - raise exceptions.TestError("Failed to install iscsi initiator" - " packages") + raise exceptions.TestError("Failed to install iscsi initiator" " packages") # Install linux iscsi target software targetcli iscsi_package = ["targetcli"] if not utils_package.package_install(iscsi_package): - LOG.error("Failed to install targetcli trying with scsi-" - "target-utils or tgt package") + LOG.error( + "Failed to install targetcli trying with scsi-" + "target-utils or tgt package" + ) # try with scsi target utils if targetcli is not available if ubuntu: iscsi_package = ["tgt"] else: iscsi_package = ["scsi-target-utils"] if not utils_package.package_install(iscsi_package): - raise exceptions.TestError("Failed to install iscsi target and" - " initiator packages") + raise exceptions.TestError( + "Failed to install iscsi target and" " initiator packages" + ) iscsi_instance = IscsiTGT(params, root_dir) else: iscsi_instance = IscsiLIO(params, root_dir) diff --git a/virttest/kernel_interface.py b/virttest/kernel_interface.py index 049a6cc8a2..f779670b47 100644 --- a/virttest/kernel_interface.py +++ b/virttest/kernel_interface.py @@ -5,7 +5,7 @@ from avocado.utils import process -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class FS(object): @@ -59,8 +59,9 @@ def fs_value(self, value): cmd = "echo %s > %s" % (value, self.fs) status, output = self.func(cmd) if status != 0: - LOG.error("Failed to set %s to %s, error: %s", self.fs, - value, output.strip()) + LOG.error( + "Failed to set %s to %s, error: %s", self.fs, value, output.strip() + ) return False return True diff --git a/virttest/libvirt_cgroup.py b/virttest/libvirt_cgroup.py index 076774f1ed..cfe0d29a99 100644 --- a/virttest/libvirt_cgroup.py +++ b/virttest/libvirt_cgroup.py @@ -13,50 +13,64 @@ from virttest import virsh from virttest.staging import utils_cgroup -VIRSH_BLKIOTUNE_OUTPUT_MAPPING = {"weight": "weight", - "device_read_iops_sec": "riops", - "device_write_iops_sec": "wiops", - "device_read_bytes_sec": "rbps", - "device_write_bytes_sec": "wbps"} -CGROUP_V1_BLKIO_FILE_MAPPING = {"weight": "blkio.bfq.weight", - "wiops": "blkio.throttle.write_iops_device", - "riops": "blkio.throttle.read_iops_device", - "rbps": "blkio.throttle.read_bps_device", - "wbps": "blkio.throttle.write_bps_device"} -CGROUP_V2_BLKIO_FILE_MAPPING = {"weight": "io.bfq.weight", - "wiops": "io.max", - "riops": "io.max", - "rbps": "io.max", - "wbps": "io.max"} -CGROUP_V1_MEM_FILE_MAPPING = {"hard_limit": "memory.limit_in_bytes", - "soft_limit": "memory.soft_limit_in_bytes", - "swap_hard_limit": "memory.memsw.limit_in_bytes"} -CGROUP_V2_MEM_FILE_MAPPING = {"hard_limit": "memory.max", - "soft_limit": "memory.high", - "swap_hard_limit": "memory.swap.max"} -CGROUP_V1_SCHEDINFO_FILE_MAPPING = {"cpu_shares": "cpu.shares", - "vcpu_period": "/cpu.cfs_period_us", - "vcpu_quota": "/cpu.cfs_quota_us", - "emulator_period": "emulator/cpu.cfs_period_us", - "emulator_quota": "emulator/cpu.cfs_quota_us", - "global_period": "cpu.cfs_period_us", - "global_quota": "cpu.cfs_quota_us", - "iothread_period": "/cpu.cfs_period_us", - "iothread_quota": "/cpu.cfs_quota_us"} -CGROUP_V2_SCHEDINFO_FILE_MAPPING = {"cpu_shares": "cpu.weight", - "vcpu_period": "/cpu.max", - "vcpu_quota": "/cpu.max", - "emulator_period": "emulator/cpu.max", - "emulator_quota": "emulator/cpu.max", - "global_period": "cpu.max", - "global_quota": "cpu.max", - "iothread_period": "/cpu.max", - "iothread_quota": "/cpu.max"} - -LOG = logging.getLogger('avocado.' + __name__) - - -#cgroup related functions +VIRSH_BLKIOTUNE_OUTPUT_MAPPING = { + "weight": "weight", + "device_read_iops_sec": "riops", + "device_write_iops_sec": "wiops", + "device_read_bytes_sec": "rbps", + "device_write_bytes_sec": "wbps", +} +CGROUP_V1_BLKIO_FILE_MAPPING = { + "weight": "blkio.bfq.weight", + "wiops": "blkio.throttle.write_iops_device", + "riops": "blkio.throttle.read_iops_device", + "rbps": "blkio.throttle.read_bps_device", + "wbps": "blkio.throttle.write_bps_device", +} +CGROUP_V2_BLKIO_FILE_MAPPING = { + "weight": "io.bfq.weight", + "wiops": "io.max", + "riops": "io.max", + "rbps": "io.max", + "wbps": "io.max", +} +CGROUP_V1_MEM_FILE_MAPPING = { + "hard_limit": "memory.limit_in_bytes", + "soft_limit": "memory.soft_limit_in_bytes", + "swap_hard_limit": "memory.memsw.limit_in_bytes", +} +CGROUP_V2_MEM_FILE_MAPPING = { + "hard_limit": "memory.max", + "soft_limit": "memory.high", + "swap_hard_limit": "memory.swap.max", +} +CGROUP_V1_SCHEDINFO_FILE_MAPPING = { + "cpu_shares": "cpu.shares", + "vcpu_period": "/cpu.cfs_period_us", + "vcpu_quota": "/cpu.cfs_quota_us", + "emulator_period": "emulator/cpu.cfs_period_us", + "emulator_quota": "emulator/cpu.cfs_quota_us", + "global_period": "cpu.cfs_period_us", + "global_quota": "cpu.cfs_quota_us", + "iothread_period": "/cpu.cfs_period_us", + "iothread_quota": "/cpu.cfs_quota_us", +} +CGROUP_V2_SCHEDINFO_FILE_MAPPING = { + "cpu_shares": "cpu.weight", + "vcpu_period": "/cpu.max", + "vcpu_quota": "/cpu.max", + "emulator_period": "emulator/cpu.max", + "emulator_quota": "emulator/cpu.max", + "global_period": "cpu.max", + "global_quota": "cpu.max", + "iothread_period": "/cpu.max", + "iothread_quota": "/cpu.max", +} + +LOG = logging.getLogger("avocado." + __name__) + + +# cgroup related functions class CgroupTest(object): """Class for libvirt cgroup related test""" @@ -91,17 +105,17 @@ def get_cgroup_path(self, controller=None): cgroup_path = "" if self.is_cgroup_v2_enabled(): vm_proc_cgroup_path = "/proc/%s/cgroup" % self.__vm_pid - with open('/proc/mounts', 'r') as mnts: + with open("/proc/mounts", "r") as mnts: cg_mount_point = re.findall(r"\s(\S*cgroup)\s", mnts.read()) - with open(vm_proc_cgroup_path, 'r') as vm_cg_file: + with open(vm_proc_cgroup_path, "r") as vm_cg_file: cg_vm_scope = re.findall(r"\S*::(\S*)", vm_cg_file.read()) - cgroup_path = os.path.join(cg_mount_point[0], - cg_vm_scope[0].strip("/")) + cgroup_path = os.path.join(cg_mount_point[0], cg_vm_scope[0].strip("/")) if "emulator" in cgroup_path: cgroup_path += "/.." else: cgroup_path = utils_cgroup.resolve_task_cgroup_path( - int(self.__vm_pid), controller) + int(self.__vm_pid), controller + ) if not os.path.exists(cgroup_path): LOG.error("cgroup path '%s' doesn't exist" % cgroup_path) return None @@ -121,9 +135,12 @@ def __get_cpu_subdirs(self, controller_path=None, dir_keyword=None): if dir_keyword in filename: dir_names.append(filename) if not dir_names and "iothread" in dir_keyword: - LOG.debug("No sub dirs found with keyword: '%s'. " - "Pls check if you've executed virsh cmd " - "'iothreadadd'.", dir_keyword) + LOG.debug( + "No sub dirs found with keyword: '%s'. " + "Pls check if you've executed virsh cmd " + "'iothreadadd'.", + dir_keyword, + ) return None return sorted(dir_names) @@ -154,15 +171,19 @@ def __get_cg_file_path(cg_key, cg_path, cg_file_name): standardized_cgroup_info = {} if virsh_cmd == "blkiotune": cgroup_path = self.get_cgroup_path("blkio") - dev_init_dict = {"rbps": "max", "wbps": "max", "riops": "max", - "wiops": "max"} + dev_init_dict = { + "rbps": "max", + "wbps": "max", + "riops": "max", + "wiops": "max", + } dev_list = [] if not self.is_bfq(): - CGROUP_V1_BLKIO_FILE_MAPPING['weight'] = 'blkio.weight' - CGROUP_V1_BLKIO_FILE_MAPPING['weight_device'] = 'blkio.weight_device' + CGROUP_V1_BLKIO_FILE_MAPPING["weight"] = "blkio.weight" + CGROUP_V1_BLKIO_FILE_MAPPING["weight_device"] = "blkio.weight_device" for cg_key, cg_file_name in list(CGROUP_V1_BLKIO_FILE_MAPPING.items()): cg_file_path = __get_cg_file_path(cg_key, cgroup_path, cg_file_name) - with open(cg_file_path, 'r') as cg_file: + with open(cg_file_path, "r") as cg_file: if cg_key in ["weight"]: standardized_cgroup_info[cg_key] = cg_file.read().strip() if cg_key in ["rbps", "wbps", "riops", "wiops", "weight_device"]: @@ -177,7 +198,9 @@ def __get_cg_file_path(cg_key, cg_path, cg_file_name): elif virsh_cmd == "memtune": cgroup_path = self.get_cgroup_path("memory") cmd = "getconf PAGE_SIZE" - page_size = process.run(cmd, ignore_status=True, shell=True).stdout_text.strip() + page_size = process.run( + cmd, ignore_status=True, shell=True + ).stdout_text.strip() LOG.debug("page_size is %d" % int(page_size)) if int(page_size) == 65536: max_mem_value = "9223372036854710272" @@ -185,7 +208,7 @@ def __get_cg_file_path(cg_key, cg_path, cg_file_name): max_mem_value = "9223372036854771712" LOG.debug("max_mem_value is %s" % max_mem_value) for cg_key, cg_file_name in list(CGROUP_V1_MEM_FILE_MAPPING.items()): - with open(os.path.join(cgroup_path, cg_file_name), 'r') as cg_file: + with open(os.path.join(cgroup_path, cg_file_name), "r") as cg_file: cg_file_value = cg_file.read().strip() if cg_file_value == max_mem_value: cg_file_value = "max" @@ -200,17 +223,22 @@ def __get_cg_file_path(cg_key, cg_path, cg_file_name): cg_file_name = cg_file_name.replace("", vcpu_dirs[0]) if "" in cg_file_name: if iothread_dirs: - cg_file_name = cg_file_name.replace("", iothread_dirs[0]) + cg_file_name = cg_file_name.replace( + "", iothread_dirs[0] + ) else: continue cg_file_path = __get_cg_file_path(cg_key, cgroup_path, cg_file_name) - with open(cg_file_path, 'r') as cg_file: + with open(cg_file_path, "r") as cg_file: cg_file_value = cg_file.read().strip() if cg_file_value == max_cpu_value: cg_file_value = "max" if "quota" in cg_key: - if cg_file_value in ["-1", "18446744073709551", - "17592186044415"]: + if cg_file_value in [ + "-1", + "18446744073709551", + "17592186044415", + ]: standardized_cgroup_info[cg_key] = "max" continue standardized_cgroup_info[cg_key] = cg_file_value @@ -233,15 +261,16 @@ def __get_standardized_cgroup2_info(self, virsh_cmd=None): if virsh_cmd == "blkiotune": weight_file_name = CGROUP_V2_BLKIO_FILE_MAPPING["weight"] iomax_file_name = CGROUP_V2_BLKIO_FILE_MAPPING["wiops"] - path_to_weight = os.path.join(cgroup_path.split("libvirt")[0], - weight_file_name) - with open(path_to_weight, 'r') as weight_file: - weight_value = re.search(r'\d+', weight_file.read()) + path_to_weight = os.path.join( + cgroup_path.split("libvirt")[0], weight_file_name + ) + with open(path_to_weight, "r") as weight_file: + weight_value = re.search(r"\d+", weight_file.read()) if weight_value: weight_value = weight_value.group() standardized_cgroup_info["weight"] = weight_value path_to_iomax = os.path.join(cgroup_path, iomax_file_name) - with open(path_to_iomax, 'r') as iomax_file: + with open(path_to_iomax, "r") as iomax_file: iomax_info = iomax_file.readlines() for line in iomax_info: dev_iomax_info = line.strip().split() @@ -253,7 +282,7 @@ def __get_standardized_cgroup2_info(self, virsh_cmd=None): standardized_cgroup_info[dev_num] = dev_iomax_dict elif virsh_cmd == "memtune": for cg_key, cg_file_name in list(CGROUP_V2_MEM_FILE_MAPPING.items()): - with open(os.path.join(cgroup_path, cg_file_name), 'r') as cg_file: + with open(os.path.join(cgroup_path, cg_file_name), "r") as cg_file: cg_file_value = cg_file.read().strip() standardized_cgroup_info[cg_key] = cg_file_value elif virsh_cmd == "schedinfo": @@ -264,13 +293,15 @@ def __get_standardized_cgroup2_info(self, virsh_cmd=None): cg_file_name = cg_file_name.replace("", vcpu_dirs[0]) if "" in cg_file_name: if iothread_dirs: - cg_file_name = cg_file_name.replace("", iothread_dirs[0]) + cg_file_name = cg_file_name.replace( + "", iothread_dirs[0] + ) else: continue cg_dir = cgroup_path if cg_key == "cpu_shares": cg_dir = cgroup_path.split("libvirt")[0] - with open(os.path.join(cg_dir, cg_file_name), 'r') as cg_file: + with open(os.path.join(cg_dir, cg_file_name), "r") as cg_file: list_index = 0 cg_file_values = cg_file.read().strip().split() if "period" in cg_key: @@ -386,8 +417,12 @@ def get_standardized_virsh_info(self, virsh_cmd=None, virsh_dict=None): if virsh_cmd == "blkiotune": virsh_output_mapping = VIRSH_BLKIOTUNE_OUTPUT_MAPPING.copy() dev_list = [] - dev_init_dict = {"rbps": "max", "wbps": "max", "riops": "max", - "wiops": "max"} + dev_init_dict = { + "rbps": "max", + "wbps": "max", + "riops": "max", + "wiops": "max", + } for io_item, io_item_value in list(virsh_dict.items()): if io_item in ["weight"]: standardized_virsh_output_info[io_item] = io_item_value @@ -397,31 +432,43 @@ def get_standardized_virsh_info(self, virsh_cmd=None, virsh_dict=None): if "dev" in io_value_list[i]: dev_num = self.__get_dev_major_minor(io_value_list[i]) if dev_num not in dev_list: - standardized_virsh_output_info[dev_num] = dev_init_dict.copy() + standardized_virsh_output_info[ + dev_num + ] = dev_init_dict.copy() dev_list.append(dev_num) - standardized_virsh_output_info[dev_num][virsh_output_mapping[io_item]] = io_value_list[i+1] + standardized_virsh_output_info[dev_num][ + virsh_output_mapping[io_item] + ] = io_value_list[i + 1] elif virsh_cmd == "memtune": - standardized_virsh_output_info = {"hard_limit": "max", - "soft_limit": "max", - "swap_hard_limit": "max"} + standardized_virsh_output_info = { + "hard_limit": "max", + "soft_limit": "max", + "swap_hard_limit": "max", + } for mem_item, mem_item_value in list(virsh_dict.items()): if mem_item_value in ["unlimited"]: standardized_virsh_output_info[mem_item] = "max" elif mem_item_value.isdigit(): - standardized_virsh_output_info[mem_item] = str(int(mem_item_value) * 1024) + standardized_virsh_output_info[mem_item] = str( + int(mem_item_value) * 1024 + ) else: standardized_virsh_output_info[mem_item] = mem_item_value - LOG.debug("memtune: the value '%s' for '%s' is " - "new to us, pls check.", - mem_item_value, mem_item) + LOG.debug( + "memtune: the value '%s' for '%s' is " "new to us, pls check.", + mem_item_value, + mem_item, + ) elif virsh_cmd == "schedinfo": for schedinfo_item, schedinfo_value in list(virsh_dict.items()): if schedinfo_item.lower() in ["scheduler"]: # no need to check scheduler type, it's fixed for qemu continue if "quota" in schedinfo_item: - if (schedinfo_value in ["18446744073709551", "17592186044415"] - or int(schedinfo_value) < 0): + if ( + schedinfo_value in ["18446744073709551", "17592186044415"] + or int(schedinfo_value) < 0 + ): # When set cfs_quota values with negative values or # maximum acceptable values, it's means 'max' or # 'unlimited', so match these values to 'max'. @@ -429,8 +476,7 @@ def get_standardized_virsh_info(self, virsh_cmd=None, virsh_dict=None): continue standardized_virsh_output_info[schedinfo_item] = schedinfo_value else: - LOG.error("You've provided an unsupported virsh cmd: %s", - virsh_cmd) + LOG.error("You've provided an unsupported virsh cmd: %s", virsh_cmd) return None return standardized_virsh_output_info @@ -444,8 +490,8 @@ def is_bfq(self): first_blk = utils_disk.get_first_disk() schedulerfd = "/sys/block/%s/queue/scheduler" % first_blk bfq_scheduler = False - with open(schedulerfd, 'r') as iosche: - if 'bfq' in iosche.readline(): + with open(schedulerfd, "r") as iosche: + if "bfq" in iosche.readline(): bfq_scheduler = True return bfq_scheduler @@ -474,7 +520,7 @@ def _produce_cpuset_cpus_file(self, cpuset_path, vm_name, is_cgroup2=False): if not os.path.exists(cpuset_path): # Produce the cpuset.cpus file by starting the vm - virsh_dargs = {'ignore_status': False, 'debug': True} + virsh_dargs = {"ignore_status": False, "debug": True} virsh.start(vm_name, **virsh_dargs) virsh.destroy(vm_name, **virsh_dargs) @@ -488,7 +534,7 @@ def set_cpuset_cpus(self, value, vm_name): if self.is_cgroup_v2_enabled(): LOG.debug("v2 cgroup doesn't have cpuset.cpus") return - cpuset_path = '/sys/fs/cgroup/cpuset/machine.slice/cpuset.cpus' + cpuset_path = "/sys/fs/cgroup/cpuset/machine.slice/cpuset.cpus" self._produce_cpuset_cpus_file(cpuset_path, vm_name) LOG.debug("Set %s to %s", cpuset_path, value) cmd = "echo %s > %s" % (value, cpuset_path) @@ -504,7 +550,7 @@ def get_cpuset_cpus(self, vm_name): if self.is_cgroup_v2_enabled(): LOG.debug("v2 cgroup doesn't have cpuset.cpus") return - cpuset_path = '/sys/fs/cgroup/cpuset/machine.slice/cpuset.cpus' + cpuset_path = "/sys/fs/cgroup/cpuset/machine.slice/cpuset.cpus" self._produce_cpuset_cpus_file(cpuset_path, vm_name) LOG.debug("Get %s value", cpuset_path) cmd = "cat %s" % cpuset_path diff --git a/virttest/libvirt_installer.py b/virttest/libvirt_installer.py index 2f8d18ef26..f54bd61be0 100644 --- a/virttest/libvirt_installer.py +++ b/virttest/libvirt_installer.py @@ -12,17 +12,21 @@ from virttest import base_installer -__all__ = ['GitRepoInstaller', 'LocalSourceDirInstaller', - 'LocalSourceTarInstaller', 'RemoteSourceTarInstaller'] +__all__ = [ + "GitRepoInstaller", + "LocalSourceDirInstaller", + "LocalSourceTarInstaller", + "RemoteSourceTarInstaller", +] -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class LIBVIRTBaseInstaller(base_installer.BaseInstaller): - ''' + """ Base class for libvirt installations - ''' + """ def _set_install_prefix(self): """ @@ -51,9 +55,8 @@ def _install_phase_package_verify(self): """ LOG.debug("Check for libvirt rpms") found = False - for fl in os.listdir('%s/RPMS/%s/' % (self.rpmbuild_path, - platform.machine())): - if fl.endswith('.rpm'): + for fl in os.listdir("%s/RPMS/%s/" % (self.rpmbuild_path, platform.machine())): + if fl.endswith(".rpm"): found = True if not found: self.test.fail("Failed to build rpms") @@ -65,8 +68,10 @@ def _install_phase_install(self): LOG.debug("Install libvirt rpms") package_install_cmd = "rpm -Uvh --nodeps --replacepkgs" package_install_cmd += " --replacefiles --oldpackage" - package_install_cmd += " %s/RPMS/%s/libvirt*" % (self.rpmbuild_path, - platform.machine()) + package_install_cmd += " %s/RPMS/%s/libvirt*" % ( + self.rpmbuild_path, + platform.machine(), + ) process.system(package_install_cmd) def _install_phase_init(self): @@ -89,11 +94,11 @@ def _install_phase_init_verify(self): process.system("virsh capabilities") def uninstall(self): - ''' + """ Performs the uninstallation of KVM userspace component :return: None - ''' + """ self._cleanup_links() super(LIBVIRTBaseInstaller, self).uninstall() @@ -101,37 +106,43 @@ def install(self): super(LIBVIRTBaseInstaller, self).install(package=True) -class GitRepoInstaller(LIBVIRTBaseInstaller, - base_installer.GitRepoInstaller): +class GitRepoInstaller(LIBVIRTBaseInstaller, base_installer.GitRepoInstaller): - ''' + """ Installer that deals with source code on Git repositories - ''' + """ + pass -class LocalSourceDirInstaller(LIBVIRTBaseInstaller, - base_installer.LocalSourceDirInstaller): +class LocalSourceDirInstaller( + LIBVIRTBaseInstaller, base_installer.LocalSourceDirInstaller +): """ Installer that deals with source code on local directories """ + pass -class LocalSourceTarInstaller(LIBVIRTBaseInstaller, - base_installer.LocalSourceTarInstaller): +class LocalSourceTarInstaller( + LIBVIRTBaseInstaller, base_installer.LocalSourceTarInstaller +): """ Installer that deals with source code on local tarballs """ + pass -class RemoteSourceTarInstaller(LIBVIRTBaseInstaller, - base_installer.RemoteSourceTarInstaller): +class RemoteSourceTarInstaller( + LIBVIRTBaseInstaller, base_installer.RemoteSourceTarInstaller +): """ Installer that deals with source code on remote tarballs """ + pass diff --git a/virttest/libvirt_remote.py b/virttest/libvirt_remote.py index 0fc362a881..8dfb2d9a39 100644 --- a/virttest/libvirt_remote.py +++ b/virttest/libvirt_remote.py @@ -11,12 +11,12 @@ from virttest.utils_test import libvirt -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) -def update_remote_file(params, value, - file_path='/etc/libvirt/libvirtd.conf', - restart_libvirt=True): +def update_remote_file( + params, value, file_path="/etc/libvirt/libvirtd.conf", restart_libvirt=True +): """ Update file on remote and restart libvirtd if needed @@ -34,16 +34,19 @@ def update_remote_file(params, value, remote_pwd = params.get("server_pwd", params.get("remote_pwd")) remote_user = params.get("server_user", params.get("remote_user")) - remote_file_obj = remote.RemoteFile(address=remote_ip, client='scp', - username=remote_user, - password=remote_pwd, - port='22', remote_path=file_path) + remote_file_obj = remote.RemoteFile( + address=remote_ip, + client="scp", + username=remote_user, + password=remote_pwd, + port="22", + remote_path=file_path, + ) remote_file_obj.sub_else_add(tmp_value) if restart_libvirt: - libvirt.remotely_control_libvirtd(remote_ip, remote_user, - remote_pwd, action='restart', - status_error='no') + libvirt.remotely_control_libvirtd( + remote_ip, remote_user, remote_pwd, action="restart", status_error="no" + ) return remote_file_obj except Exception as err: - raise exceptions.TestFail("Unable to update {}: {}" - .format(file_path, err)) + raise exceptions.TestFail("Unable to update {}: {}".format(file_path, err)) diff --git a/virttest/libvirt_storage.py b/virttest/libvirt_storage.py index 0cdf65b165..77b7c5817a 100644 --- a/virttest/libvirt_storage.py +++ b/virttest/libvirt_storage.py @@ -15,7 +15,7 @@ from virttest import storage from virttest import virsh -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) class QemuImg(storage.QemuImg): @@ -174,7 +174,7 @@ def pool_state(self, name): :return: active/inactive, and None when something wrong. """ try: - return self.list_pools()[name]['State'] + return self.list_pools()[name]["State"] except (process.CmdError, KeyError): return None @@ -185,7 +185,7 @@ def pool_autostart(self, name): :return: yes/no, and None when something wrong. """ try: - return self.list_pools()[name]['Autostart'] + return self.list_pools()[name]["Autostart"] except (process.CmdError, KeyError): return None @@ -204,7 +204,7 @@ def pool_info(self, name): return info for line in result.stdout_text.splitlines(): - params = line.split(':') + params = line.split(":") if len(params) == 2: name = params[0].strip() value = params[1].strip() @@ -323,8 +323,9 @@ def define_dir_pool(self, name, target_path): Define a directory type pool. """ try: - self.virsh_instance.pool_define_as(name, "dir", target_path, - ignore_status=False) + self.virsh_instance.pool_define_as( + name, "dir", target_path, ignore_status=False + ) except process.CmdError: LOG.error("Define dir pool '%s' failed.", name) return False @@ -336,9 +337,13 @@ def define_fs_pool(self, name, block_device, target_path): Define a filesystem type pool. """ try: - self.virsh_instance.pool_define_as(name, "fs", target_path, - extra="--source-dev %s" % block_device, - ignore_status=False) + self.virsh_instance.pool_define_as( + name, + "fs", + target_path, + extra="--source-dev %s" % block_device, + ignore_status=False, + ) except process.CmdError: LOG.error("Define fs pool '%s' failed.", name) return False @@ -350,10 +355,10 @@ def define_lvm_pool(self, name, block_device, vg_name, target_path): Define a lvm type pool. """ try: - extra = "--source-dev %s --source-name %s" % (block_device, - vg_name) - self.virsh_instance.pool_define_as(name, "logical", target_path, - extra, ignore_status=False) + extra = "--source-dev %s --source-name %s" % (block_device, vg_name) + self.virsh_instance.pool_define_as( + name, "logical", target_path, extra, ignore_status=False + ) except process.CmdError: LOG.error("Define logic pool '%s' failed.", name) return False @@ -366,8 +371,9 @@ def define_disk_pool(self, name, block_device, target_path): """ try: extra = "--source-dev %s" % block_device - self.virsh_instance.pool_define_as(name, "disk", target_path, - extra, ignore_status=False) + self.virsh_instance.pool_define_as( + name, "disk", target_path, extra, ignore_status=False + ) except process.CmdError: LOG.error("Define disk pool '%s' failed.", name) return False @@ -379,10 +385,10 @@ def define_iscsi_pool(self, name, source_host, source_dev, target_path): Define a iscsi type pool. """ try: - extra = "--source-host %s --source-dev %s" % (source_host, - source_dev) - self.virsh_instance.pool_define_as(name, "iscsi", target_path, - extra, ignore_status=False) + extra = "--source-host %s --source-dev %s" % (source_host, source_dev) + self.virsh_instance.pool_define_as( + name, "iscsi", target_path, extra, ignore_status=False + ) except process.CmdError: LOG.error("Define iscsi pool '%s' failed.", name) return False @@ -394,10 +400,10 @@ def define_netfs_pool(self, name, source_host, source_path, target_path): Define a netfs type pool. """ try: - extra = "--source-host %s --source-path %s" % (source_host, - target_path) - self.virsh_instance.pool_define_as(name, "netfs", target_path, - extra, ignore_status=False) + extra = "--source-host %s --source-path %s" % (source_host, target_path) + self.virsh_instance.pool_define_as( + name, "netfs", target_path, extra, ignore_status=False + ) except process.CmdError: LOG.error("Define netfs pool '%s' failed.", name) return False @@ -409,10 +415,14 @@ def define_rbd_pool(self, name, source_host, source_name, extra=""): Define a rbd type pool. """ try: - extra = ("--source-host %s --source-name %s %s" % - (source_host, source_name, extra)) - self.virsh_instance.pool_define_as(name, "rbd", "", - extra, ignore_status=False) + extra = "--source-host %s --source-name %s %s" % ( + source_host, + source_name, + extra, + ) + self.virsh_instance.pool_define_as( + name, "rbd", "", extra, ignore_status=False + ) except process.CmdError: LOG.error("Define rbd pool '%s' failed.", name) return False @@ -434,10 +444,9 @@ def list_volumes(self): """ volumes = {} try: - result = self.virsh_instance.vol_list(self.pool_name, - ignore_status=False) + result = self.virsh_instance.vol_list(self.pool_name, ignore_status=False) except process.CmdError as detail: - LOG.error('List volume failed: %s', detail) + LOG.error("List volume failed: %s", detail) return volumes lines = result.stdout_text.strip().splitlines() @@ -468,20 +477,20 @@ def volume_info(self, name): """ info = {} try: - result = self.virsh_instance.vol_info(name, self.pool_name, - ignore_status=False) + result = self.virsh_instance.vol_info( + name, self.pool_name, ignore_status=False + ) except process.CmdError as detail: LOG.error("Get volume information failed: %s", detail) return info for line in result.stdout_text.strip().splitlines(): - attr = line.split(':')[0] + attr = line.split(":")[0] value = line.split("%s:" % attr)[-1].strip() info[attr] = value return info - def create_volume(self, name, capability, - allocation=None, frmt=None): + def create_volume(self, name, capability, allocation=None, frmt=None): """ Create a volume in pool. """ @@ -489,9 +498,15 @@ def create_volume(self, name, capability, LOG.debug("Volume '%s' already exists.", name) return False try: - self.virsh_instance.vol_create_as(name, self.pool_name, - capability, allocation, frmt, - ignore_status=False, debug=True) + self.virsh_instance.vol_create_as( + name, + self.pool_name, + capability, + allocation, + frmt, + ignore_status=False, + debug=True, + ) except process.CmdError as detail: LOG.error("Create volume failed:%s", detail) return False @@ -507,8 +522,9 @@ def delete_volume(self, name): """ if self.volume_exists(name): try: - self.virsh_instance.vol_delete(name, self.pool_name, - ignore_status=False) + self.virsh_instance.vol_delete( + name, self.pool_name, ignore_status=False + ) except process.CmdError as detail: LOG.error("Delete volume failed:%s", detail) return False @@ -520,7 +536,7 @@ def delete_volume(self, name): return False else: LOG.info("Volume '%s' does not exist.", name) - return True # Return True for expected result + return True # Return True for expected result def clone_volume(self, old_name, new_name): """ @@ -528,9 +544,9 @@ def clone_volume(self, old_name, new_name): """ if self.volume_exists(old_name) and not self.volume_exists(new_name): try: - self.virsh_instance.vol_clone(old_name, new_name, - self.pool_name, - ignore_status=False) + self.virsh_instance.vol_clone( + old_name, new_name, self.pool_name, ignore_status=False + ) except process.CmdError as detail: LOG.error("Clone volume failed:%s", detail) return False @@ -541,8 +557,10 @@ def clone_volume(self, old_name, new_name): LOG.debug("Volume '%s' clone failed.", old_name) return False else: - LOG.info("Volume '%s' does not exist or '%s' has been exist." - % (old_name, new_name)) + LOG.info( + "Volume '%s' does not exist or '%s' has been exist." + % (old_name, new_name) + ) return False @@ -559,8 +577,8 @@ def check_qemu_image_lock_support(): try: binary_path = process.run("which %s" % cmd).stdout_text.strip() except process.CmdError: - raise process.CmdError(cmd, binary_path, - "qemu-img command is not found") - cmd_result = process.run(binary_path + ' -h', ignore_status=True, - shell=True, verbose=False) - return '-U' in cmd_result.stdout_text + raise process.CmdError(cmd, binary_path, "qemu-img command is not found") + cmd_result = process.run( + binary_path + " -h", ignore_status=True, shell=True, verbose=False + ) + return "-U" in cmd_result.stdout_text diff --git a/virttest/libvirt_version.py b/virttest/libvirt_version.py index 94e7f3ef41..cd7b71de4b 100644 --- a/virttest/libvirt_version.py +++ b/virttest/libvirt_version.py @@ -11,7 +11,7 @@ from avocado.utils.astring import to_text -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) def version_compare(major, minor, update, session=None): @@ -38,7 +38,7 @@ def version_compare(major, minor, update, session=None): if session: func = session.cmd_output cmd = "virtqemud" - if session.cmd_status('which %s' % cmd): + if session.cmd_status("which %s" % cmd): cmd = "libvirtd" else: try: @@ -48,15 +48,17 @@ def version_compare(major, minor, update, session=None): cmd = "libvirtd" try: - regex = r'\w*d\s*\(libvirt\)\s*' - regex += r'(\d+)\.(\d+)\.(\d+)' + regex = r"\w*d\s*\(libvirt\)\s*" + regex += r"(\d+)\.(\d+)\.(\d+)" lines = to_text(func("%s -V" % cmd)).splitlines() for line in lines: mobj = re.search(regex, line, re.I) if bool(mobj): - LIBVIRT_LIB_VERSION = int(mobj.group(1)) * 1000000 + \ - int(mobj.group(2)) * 1000 + \ - int(mobj.group(3)) + LIBVIRT_LIB_VERSION = ( + int(mobj.group(1)) * 1000000 + + int(mobj.group(2)) * 1000 + + int(mobj.group(3)) + ) break except (ValueError, TypeError, AttributeError): LOG.warning("Error determining libvirt version") @@ -90,10 +92,11 @@ def is_libvirt_feature_supported(params, ignore_error=False): libvirt's (major, minor, update) version. """ func_supported_since_libvirt_ver = eval( - params.get("func_supported_since_libvirt_ver", '()')) - unsupported_err_msg = params.get("unsupported_err_msg", - "This libvirt version doesn't support " - "this function.") + params.get("func_supported_since_libvirt_ver", "()") + ) + unsupported_err_msg = params.get( + "unsupported_err_msg", "This libvirt version doesn't support " "this function." + ) if func_supported_since_libvirt_ver: if not version_compare(*func_supported_since_libvirt_ver): diff --git a/virttest/libvirt_vm.py b/virttest/libvirt_vm.py index 8f0139a6ee..451c3aa731 100644 --- a/virttest/libvirt_vm.py +++ b/virttest/libvirt_vm.py @@ -39,7 +39,7 @@ # Using as lower capital is not the best way to do, but this is just a # workaround to avoid changing the entire file. -LOG = logging.getLogger('avocado.' + __name__) +LOG = logging.getLogger("avocado." + __name__) def normalize_connect_uri(connect_uri): @@ -55,8 +55,9 @@ def normalize_connect_uri(connect_uri): result = virsh.canonical_uri(uri=connect_uri) if not result: - raise ValueError("Normalizing connect_uri '%s' failed, is libvirt " - "running?" % connect_uri) + raise ValueError( + "Normalizing connect_uri '%s' failed, is libvirt " "running?" % connect_uri + ) return result @@ -82,7 +83,7 @@ def complete_uri(ip_address, protocol=None, port=None): return complete_uri -def get_uri_with_transport(uri_type='qemu', transport="", dest_ip=""): +def get_uri_with_transport(uri_type="qemu", transport="", dest_ip=""): """ Return a URI to connect driver on dest with a specified transport. @@ -90,12 +91,14 @@ def get_uri_with_transport(uri_type='qemu', transport="", dest_ip=""): :param transport: The transport type connect to dest. :param dest_ip: The ip of destination. """ - _type2uri_ = {'qemu': "qemu:///system", - 'qemu_system': "qemu:///system", - 'qemu_session': "qemu:///session", - 'lxc': "lxc:///", - 'xen': "xen:///", - 'esx': "esx:///"} + _type2uri_ = { + "qemu": "qemu:///system", + "qemu_system": "qemu:///system", + "qemu_session": "qemu:///session", + "lxc": "lxc:///", + "xen": "xen:///", + "esx": "esx:///", + } try: origin_uri = _type2uri_[uri_type] except KeyError: @@ -108,10 +111,10 @@ def get_uri_with_transport(uri_type='qemu', transport="", dest_ip=""): transport_uri_driver = origin_uri_elems[0] transport_uri_dest = origin_uri_elems[-1] if transport: - transport_uri_driver = ("%s+%s" % (transport_uri_driver, transport)) + transport_uri_driver = "%s+%s" % (transport_uri_driver, transport) - transport_uri_dest = ("://%s/%s" % (dest_ip, transport_uri_dest)) - return ("%s%s" % (transport_uri_driver, transport_uri_dest)) + transport_uri_dest = "://%s/%s" % (dest_ip, transport_uri_dest) + return "%s%s" % (transport_uri_driver, transport_uri_dest) class Monitor(object): @@ -139,11 +142,13 @@ def command(self, cmd, **dargs): :param dargs: standardized virsh function API keywords :return: standard output from monitor command executed """ - result = virsh.qemu_monitor_command(self.name, cmd, - options=self.protocol, **dargs) + result = virsh.qemu_monitor_command( + self.name, cmd, options=self.protocol, **dargs + ) if result.exit_status != 0: - raise exceptions.TestError("Failed to execute monitor cmd %s: %s" - % cmd, result.stderr_text) + raise exceptions.TestError( + "Failed to execute monitor cmd %s: %s" % cmd, result.stderr_text + ) return result.stderr_text def system_powerdown(self): @@ -201,39 +206,42 @@ def __init__(self, name, params, root_dir, address_cache, state=None): self.root_dir = root_dir self.address_cache = address_cache self.vnclisten = "0.0.0.0" - self.connect_uri = normalize_connect_uri(params.get("connect_uri", - "default")) + self.connect_uri = normalize_connect_uri(params.get("connect_uri", "default")) self.driver_type = virsh.driver(uri=self.connect_uri) - self.params['driver_type_' + self.name] = self.driver_type + self.params["driver_type_" + self.name] = self.driver_type self.monitor = Monitor(self.name) # virtnet init depends on vm_type/driver_type being set w/in params super(VM, self).__init__(name, params) - LOG.info("Libvirt VM '%s', driver '%s', uri '%s'", - self.name, self.driver_type, self.connect_uri) + LOG.info( + "Libvirt VM '%s', driver '%s', uri '%s'", + self.name, + self.driver_type, + self.connect_uri, + ) def is_lxc(self): """ Return True if VM is linux container. """ - return (self.connect_uri and self.connect_uri.count("lxc")) + return self.connect_uri and self.connect_uri.count("lxc") def is_qemu(self): """ Return True if VM is a qemu guest. """ - return (self.connect_uri and self.connect_uri.count("qemu")) + return self.connect_uri and self.connect_uri.count("qemu") def is_xen(self): """ Return True if VM is a xen guest. """ - return (self.connect_uri and self.connect_uri.count("xen")) + return self.connect_uri and self.connect_uri.count("xen") def is_esx(self): """ Return True if VM is a esx guest. """ - return (self.connect_uri and self.connect_uri.count("esx")) + return self.connect_uri and self.connect_uri.count("esx") def verify_alive(self): """ @@ -242,8 +250,7 @@ def verify_alive(self): :raise VMDeadError: If the VM is dead """ if not self.is_alive(): - raise virt_vm.VMDeadError("Domain %s is inactive" % self.name, - self.state()) + raise virt_vm.VMDeadError("Domain %s is inactive" % self.name, self.state()) def is_alive(self): """ @@ -261,7 +268,7 @@ def is_paused(self): """ Return True if VM is paused. """ - return (self.state() == "paused") + return self.state() == "paused" def is_persistent(self): """ @@ -270,8 +277,7 @@ def is_persistent(self): try: result = virsh.dominfo(self.name, uri=self.connect_uri) dominfo = result.stdout_text.strip() - return bool(re.search(r"^Persistent:\s+[Yy]es", dominfo, - re.MULTILINE)) + return bool(re.search(r"^Persistent:\s+[Yy]es", dominfo, re.MULTILINE)) except process.CmdError: return False @@ -282,8 +288,7 @@ def is_autostart(self): try: result = virsh.dominfo(self.name, uri=self.connect_uri) dominfo = result.stdout_text.strip() - return bool(re.search(r"^Autostart:\s+enable", dominfo, - re.MULTILINE)) + return bool(re.search(r"^Autostart:\s+enable", dominfo, re.MULTILINE)) except process.CmdError: return False @@ -305,8 +310,9 @@ def undefine(self, options=None): if "--nvram" not in options: options += " --nvram" try: - virsh.undefine(self.name, options=options, uri=self.connect_uri, - ignore_status=False) + virsh.undefine( + self.name, options=options, uri=self.connect_uri, ignore_status=False + ) except process.CmdError as detail: LOG.error("Undefined VM %s failed:\n%s", self.name, detail) return False @@ -320,8 +326,7 @@ def define(self, xml_file): LOG.error("File %s not found." % xml_file) return False try: - virsh.define(xml_file, uri=self.connect_uri, - ignore_status=False) + virsh.define(xml_file, uri=self.connect_uri, ignore_status=False) except process.CmdError as detail: LOG.error("Defined VM from %s failed:\n%s", xml_file, detail) return False @@ -362,8 +367,9 @@ def backup_xml(self, active=False): else: extra = "--inactive" - virsh.dumpxml(self.name, extra=extra, - to_file=xml_file, uri=self.connect_uri) + virsh.dumpxml( + self.name, extra=extra, to_file=xml_file, uri=self.connect_uri + ) return xml_file except Exception as detail: if os.path.exists(xml_file): @@ -371,8 +377,14 @@ def backup_xml(self, active=False): LOG.error("Failed to backup xml file:\n%s", detail) return "" - def clone(self, name=None, params=None, root_dir=None, address_cache=None, - copy_state=False): + def clone( + self, + name=None, + params=None, + root_dir=None, + address_cache=None, + copy_state=False, + ): """ Return a clone of the VM object with optionally modified parameters. The clone is initially not alive and needs to be started using create(). @@ -446,9 +458,9 @@ def has_os_variant(os_text, os_variant): return bool(re.search(r"%s" % os_variant, os_text, re.MULTILINE)) def has_sub_option(option, sub_option): - option_help_text = process.run("%s --%s help" % - (virt_install_binary, option), - verbose=False).stdout_text + option_help_text = process.run( + "%s --%s help" % (virt_install_binary, option), verbose=False + ).stdout_text return bool(re.search(r"%s" % sub_option, option_help_text, re.MULTILINE)) # Wrappers for all supported libvirt command line parameters. @@ -475,31 +487,37 @@ def add_hvm_or_pv(help_text, hvm_or_pv): LOG.warning("Unknown virt type hvm_or_pv, using default.") return "" - def add_mem(help_text, mem, maxmem=None, hugepage=False, - hotplugmaxmem=None, hotplugmemslots=1): + def add_mem( + help_text, + mem, + maxmem=None, + hugepage=False, + hotplugmaxmem=None, + hotplugmemslots=1, + ): if has_option(help_text, "memory"): cmd = " --memory=%s" % mem if maxmem: - if not has_sub_option('memory', 'maxmemory'): - LOG.warning("maxmemory option not supported by " - "virt-install") + if not has_sub_option("memory", "maxmemory"): + LOG.warning("maxmemory option not supported by " "virt-install") else: cmd += ",maxmemory=%s" % maxmem if hugepage: - if not has_sub_option('memory', 'hugepages'): - LOG.warning("hugepages option not supported by " - "virt-install") + if not has_sub_option("memory", "hugepages"): + LOG.warning("hugepages option not supported by " "virt-install") else: cmd += ",hugepages=yes" if hotplugmaxmem: - if not has_sub_option('memory', 'hotplugmemorymax'): - LOG.warning("hotplugmemorymax option not supported" - "by virt-install") + if not has_sub_option("memory", "hotplugmemorymax"): + LOG.warning( + "hotplugmemorymax option not supported" "by virt-install" + ) else: cmd += ",hotplugmemorymax=%s" % hotplugmaxmem - if not has_sub_option('memory', 'hotplugmemoryslots'): - LOG.warning("hotplugmemoryslots option not " - "supported by virt-install") + if not has_sub_option("memory", "hotplugmemoryslots"): + LOG.warning( + "hotplugmemoryslots option not " "supported by virt-install" + ) else: cmd += ",hotplugmemoryslots=%d" % hotplugmemslots return cmd @@ -512,8 +530,9 @@ def add_check_cpu(help_text): else: return "" - def add_smp(help_text, smp, maxvcpus=None, sockets=None, - cores=None, threads=None): + def add_smp( + help_text, smp, maxvcpus=None, sockets=None, cores=None, threads=None + ): cmd = " --vcpu=%s" % smp if maxvcpus: cmd += ",maxvcpus=%s" % maxvcpus @@ -530,7 +549,7 @@ def add_numa(): Method to add Numa node to guest :return: appended numa parameter to virt-install cmd """ - if not has_sub_option('cpu', 'cell'): + if not has_sub_option("cpu", "cell"): LOG.warning("virt-install version does not support numa cmd line") return "" cmd = " --cpu" @@ -544,20 +563,31 @@ def add_numa(): numa_nodeid = numa_params.get("numa_nodeid") numa_memdev = numa_params.get("numa_memdev") numa_distance = numa_params.get("numa_distance", "").split() - numa_val[numa_nodeid] = [numa_cpus, numa_mem, numa_memdev, - numa_distance] + numa_val[numa_nodeid] = [ + numa_cpus, + numa_mem, + numa_memdev, + numa_distance, + ] if numa_val: for cellid, value in numa_val.items(): - cells += "%s," % cell % (cellid, value[0], cellid, cellid, - cellid, value[1]) + cells += ( + "%s," + % cell + % (cellid, value[0], cellid, cellid, cellid, value[1]) + ) if value[3]: # numa_distance for siblingid in range(len(value[3])): - cells += "cell%s.distances.sibling%s.id=%s," % (cellid, - siblingid, - siblingid) - cells += "cell%s.distances.sibling%s.value=%s," % (cellid, - siblingid, - value[3][siblingid]) + cells += "cell%s.distances.sibling%s.id=%s," % ( + cellid, + siblingid, + siblingid, + ) + cells += "cell%s.distances.sibling%s.value=%s," % ( + cellid, + siblingid, + value[3][siblingid], + ) else: # Lets calculate and assign the node cpu and memory vcpus = int(params.get("smp")) @@ -572,7 +602,7 @@ def add_numa(): # we need at least 1 vcpu for 1 numa node if numa_nodes > vcpus: numa_nodes = vcpus - params['numa_nodes'] = vcpus + params["numa_nodes"] = vcpus if vcpus > 1: cpus = vcpus // numa_nodes cpus_balance = vcpus % numa_nodes @@ -612,20 +642,23 @@ def pin_numa(help_text, host_numa_node_list): # node is in online numa list and use. host_numa = str(params.get("pin_to_host_numa_node", "")) if host_numa: - host_numa_list = host_numa.split(',') + host_numa_list = host_numa.split(",") for each_numa in host_numa_list: if each_numa not in host_numa_node_list: - LOG.error("host numa node - %s is not online or " - "doesn't have memory", each_numa) + LOG.error( + "host numa node - %s is not online or " + "doesn't have memory", + each_numa, + ) host_numa_list.remove(each_numa) if host_numa_list: - host_numa = ','.join(map(str, host_numa_list)) + host_numa = ",".join(map(str, host_numa_list)) else: return "" # If user haven't mention any specific host numa nodes, use # available online numa nodes else: - host_numa = ','.join((map(str, host_numa_node_list))) + host_numa = ",".join((map(str, host_numa_node_list))) cmd += " %s,mode=%s" % (host_numa, numa_pin_mode) return cmd @@ -655,8 +688,7 @@ def pin_hugepage(help_text, hp_size, guest_numa): cmd += ",locked=yes" return cmd - def add_cpu_mode(virt_install_cmd, mode='', model='', - match='', vendor=False): + def add_cpu_mode(virt_install_cmd, mode="", model="", match="", vendor=False): """ To add cpu mode, model etc... params :param virt_install_cmd: previous virt install cmd line @@ -666,21 +698,21 @@ def add_cpu_mode(virt_install_cmd, mode='', model='', :param vendor: cpu vendor :return: updated virt_install_cmd """ - cmd = '' + cmd = "" cpu_match = re.match(r".*\s--cpu\s(\S+)\s", virt_install_cmd) if cpu_match: cmd = " --cpu %s," % cpu_match.group(1) else: cmd = " --cpu " - if mode and has_sub_option('cpu', 'mode'): + if mode and has_sub_option("cpu", "mode"): cmd += 'mode="%s",' % mode - if model and has_sub_option('cpu', 'model'): + if model and has_sub_option("cpu", "model"): cmd += 'model="%s",' % model - if match and has_sub_option('cpu', 'match'): + if match and has_sub_option("cpu", "match"): cmd += 'match="%s",' % match - if vendor and has_sub_option('cpu', 'vendor'): + if vendor and has_sub_option("cpu", "vendor"): cmd += 'vendor="%s",' % libvirt_xml.CapabilityXML().vendor - virt_install_cmd += cmd.strip(',') + virt_install_cmd += cmd.strip(",") return virt_install_cmd @@ -715,7 +747,7 @@ def add_controller(model=None): :param model: string, controller model. :return: string, empty or controller option. """ - if model == 'virtio-scsi': + if model == "virtio-scsi": return " --controller type=scsi,model=virtio-scsi" else: return "" @@ -731,16 +763,27 @@ def check_controller(virt_install_cmd_line, controller): """ found = False output = re.findall( - r"controller\stype=(\S+),model=(\S+)", virt_install_cmd_line) + r"controller\stype=(\S+),model=(\S+)", virt_install_cmd_line + ) for item in output: if controller in item[1]: found = True break return found - def add_drive(help_text, filename, pool=None, vol=None, device=None, - bus=None, perms=None, size=None, sparse=False, - cache=None, fmt=None): + def add_drive( + help_text, + filename, + pool=None, + vol=None, + device=None, + bus=None, + perms=None, + size=None, + sparse=False, + cache=None, + fmt=None, + ): cmd = " --disk" if filename: cmd += " path=%s" % filename @@ -846,15 +889,14 @@ def add_security(help_text, sec_type, sec_label=None, sec_relabel=None): """ if has_option(help_text, "security"): result = " --security" - if sec_type == 'static': + if sec_type == "static": if sec_label is None: raise ValueError("Seclabel is not setted for static.") result += " type=static,label=%s" % (sec_label) - elif sec_type == 'dynamic': + elif sec_type == "dynamic": result += " type=dynamic" else: - raise ValueError("Security type %s is not supported." - % sec_type) + raise ValueError("Security type %s is not supported." % sec_type) if sec_relabel is not None: result += ",relabel=%s" % sec_relabel else: @@ -866,12 +908,12 @@ def add_nic(help_text, nic_params): """ Return additional command line params based on dict-like nic_params """ - mac = nic_params.get('mac') - nettype = nic_params.get('nettype') - netdst = nic_params.get('netdst') - nic_model = nic_params.get('nic_model') - nic_queues = nic_params.get('queues') - nic_driver = nic_params.get('net_driver') + mac = nic_params.get("mac") + nettype = nic_params.get("nettype") + netdst = nic_params.get("netdst") + nic_model = nic_params.get("nic_model") + nic_queues = nic_params.get("queues") + nic_driver = nic_params.get("net_driver") if nettype: result = " --network=%s" % nettype else: @@ -879,26 +921,29 @@ def add_nic(help_text, nic_params): if has_option(help_text, "bridge"): # older libvirt (--network=NATdev --bridge=bridgename # --mac=mac) - if nettype != 'user': - result += ':%s' % netdst + if nettype != "user": + result += ":%s" % netdst if mac: # possible to specify --mac w/o --network result += " --mac=%s" % mac else: # newer libvirt (--network=mynet,model=virtio,mac=00:11) - if nettype != 'user': - result += '=%s' % netdst + if nettype != "user": + result += "=%s" % netdst if nettype and nic_model: # only supported along with nettype result += ",model=%s" % nic_model if nettype and mac: - result += ',mac=%s' % mac - if nettype and nic_queues and has_sub_option('network', 'driver_queues'): - result += ',driver_queues=%s' % nic_queues - if nic_driver and has_sub_option('network', 'driver_name'): - result += ',driver_name=%s' % nic_driver + result += ",mac=%s" % mac + if ( + nettype + and nic_queues + and has_sub_option("network", "driver_queues") + ): + result += ",driver_queues=%s" % nic_queues + if nic_driver and has_sub_option("network", "driver_name"): + result += ",driver_name=%s" % nic_driver elif mac: # possible to specify --mac w/o --network result += " --mac=%s" % mac - LOG.debug("vm.make_create_command.add_nic returning: %s", - result) + LOG.debug("vm.make_create_command.add_nic returning: %s", result) return result def add_memballoon(help_text, memballoon_model): @@ -917,8 +962,9 @@ def add_memballoon(help_text, memballoon_model): LOG.debug("vm.add_memballoon returning: %s", result) return result - def add_kernel(help_text, cmdline, kernel_path=None, initrd_path=None, - kernel_args=None): + def add_kernel( + help_text, cmdline, kernel_path=None, initrd_path=None, kernel_args=None + ): """ Adding Custom kernel option to boot. @@ -938,11 +984,11 @@ def add_kernel(help_text, cmdline, kernel_path=None, initrd_path=None, if has_sub_option("boot", "initrd") and initrd_path: result += "initrd=%s," % initrd_path if has_sub_option("boot", "kernel_args") and kernel_args: - result += "kernel_args=\"%s\"," % kernel_args + result += 'kernel_args="%s",' % kernel_args else: result = "" LOG.warning("boot option is not supported") - return result.rstrip(',') + return result.rstrip(",") def add_cputune(vcpu_cputune=""): """ @@ -956,7 +1002,12 @@ def add_cputune(vcpu_cputune=""): for vcpu, cpulist in enumerate(cputune_list): if "N" in cpulist: continue - cputune_str += "vcpupin%s.cpuset=\"%s\",vcpupin%s.vcpu=\"%s\"," % (item, cpulist, item, vcpu) + cputune_str += 'vcpupin%s.cpuset="%s",vcpupin%s.vcpu="%s",' % ( + item, + cpulist, + item, + vcpu, + ) item += 1 return cputune_str.rstrip(",") @@ -998,29 +1049,36 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): vm = self.clone(name, params, root_dir, copy_state=True) virt_install_binary = utils_misc.get_path( - root_dir, - params.get("virt_install_binary", - "virt-install")) + root_dir, params.get("virt_install_binary", "virt-install") + ) - help_text = process.run("%s --help" % virt_install_binary, - verbose=False).stdout_text + help_text = process.run( + "%s --help" % virt_install_binary, verbose=False + ).stdout_text try: - os_text = process.run("osinfo-query os --fields short-id", - verbose=False).stdout_text + os_text = process.run( + "osinfo-query os --fields short-id", verbose=False + ).stdout_text except process.CmdError: - os_text = process.run("%s --os-variant list" % virt_install_binary, - verbose=False).stdout_text + os_text = process.run( + "%s --os-variant list" % virt_install_binary, verbose=False + ).stdout_text # Find all supported machine types, so we can rule out an unsupported # machine type option passed in the configuration. hvm_or_pv = params.get("hvm_or_pv", "hvm") # default to 'uname -m' output arch_name = params.get("vm_arch_name", platform.machine()) - support_machine_type = libvirt.get_machine_types(arch_name, hvm_or_pv, - ignore_status=False) - LOG.debug("Machine types supported for %s/%s: %s", - hvm_or_pv, arch_name, support_machine_type) + support_machine_type = libvirt.get_machine_types( + arch_name, hvm_or_pv, ignore_status=False + ) + LOG.debug( + "Machine types supported for %s/%s: %s", + hvm_or_pv, + arch_name, + support_machine_type, + ) # Start constructing the qemu command virt_install_cmd = "" @@ -1043,13 +1101,14 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): # The machine_type format is [avocado-type:]machine_type # where avocado-type is optional part and is used in # tp-qemu to use different devices. Use only the second part - machine_type = params.get("machine_type").split(':', 1)[-1] + machine_type = params.get("machine_type").split(":", 1)[-1] if machine_type: if machine_type in support_machine_type: virt_install_cmd += add_machine_type(help_text, machine_type) else: - raise exceptions.TestSkipError("Unsupported machine type %s." % - (machine_type)) + raise exceptions.TestSkipError( + "Unsupported machine type %s." % (machine_type) + ) mem = params.get("mem") maxmemory = params.get("maxmemory", None) @@ -1059,8 +1118,9 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): hotplugmaxmem = params.get("hotplugmaxmem", None) hotplugmemslots = int(params.get("hotplugmemslots", 1)) if mem: - virt_install_cmd += add_mem(help_text, mem, maxmemory, hugepage, - hotplugmaxmem, hotplugmemslots) + virt_install_cmd += add_mem( + help_text, mem, maxmemory, hugepage, hotplugmaxmem, hotplugmemslots + ) # TODO: should we do the check before we call ? negative case ? check_cpu = params.get("use_check_cpu") @@ -1073,8 +1133,9 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): vcpu_cores = params.get("vcpu_cores") vcpu_threads = params.get("vcpu_threads") if smp: - virt_install_cmd += add_smp(help_text, smp, vcpu_max_cpus, - vcpu_sockets, vcpu_cores, vcpu_threads) + virt_install_cmd += add_smp( + help_text, smp, vcpu_max_cpus, vcpu_sockets, vcpu_cores, vcpu_threads + ) numa = params.get("numa", "no") == "yes" if numa: virt_install_cmd += add_numa() @@ -1086,21 +1147,24 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): for each_numa in host_numa_node_list: if hugepage: hp = test_setup.HugePageConfig(params) - free_hp = host_numa_node.read_from_node_meminfo(each_numa, - "HugePages_Free") + free_hp = host_numa_node.read_from_node_meminfo( + each_numa, "HugePages_Free" + ) free_mem = int(free_hp) * int(hp.get_hugepage_size()) else: - free_mem = int(host_numa_node.read_from_node_meminfo(each_numa, - 'MemFree')) + free_mem = int( + host_numa_node.read_from_node_meminfo(each_numa, "MemFree") + ) # Numa might be online but if it doesn't have free memory, # skip it if free_mem == 0: - LOG.debug("Host numa node: %s doesn't have memory", - each_numa) + LOG.debug("Host numa node: %s doesn't have memory", each_numa) host_numa_node_list.remove(each_numa) if not host_numa_node_list: - LOG.error("Host Numa nodes are not online or doesn't " - "have memory to pin") + LOG.error( + "Host Numa nodes are not online or doesn't " + "have memory to pin" + ) else: virt_install_cmd += pin_numa(help_text, host_numa_node_list) @@ -1113,51 +1177,54 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): # string, hugepage_pinnned_numa = "0-2,4" to back guest numa # nodes 0 to 2 and 4. guest_numa = str(params.get("hugepage_pinned_numa")) - if guest_numa == 'None': + if guest_numa == "None": # if user didn't mention hugepage_pinnned_numa use # numa_nodes to back all the numa nodes. guest_numa = int(params.get("numa_nodes", 2)) - guest_numa = ','.join(map(str, list(range(guest_numa)))) + guest_numa = ",".join(map(str, list(range(guest_numa)))) virt_install_cmd += pin_hugepage(help_text, hp_size, guest_numa) else: - LOG.error("Can't pin hugepage without hugepage enabled" - "and Numa enabled") + LOG.error( + "Can't pin hugepage without hugepage enabled" "and Numa enabled" + ) - cpu_mode = params.get("virt_cpu_mode", '') + cpu_mode = params.get("virt_cpu_mode", "") if cpu_mode: - virt_install_cmd = add_cpu_mode(virt_install_cmd, - mode=cpu_mode, - model=params.get('virt_cpu_model', ''), - match=params.get('virt_cpu_match', ''), - vendor=params.get('virt_cpu_vendor', False)) + virt_install_cmd = add_cpu_mode( + virt_install_cmd, + mode=cpu_mode, + model=params.get("virt_cpu_model", ""), + match=params.get("virt_cpu_match", ""), + vendor=params.get("virt_cpu_vendor", False), + ) cputune_list = params.get("vcpu_cputune", "") if cputune_list: virt_install_cmd += add_cputune(cputune_list) # TODO: directory location for vmlinuz/kernel for cdrom install ? location = None - if params.get("medium") == 'url': - location = params.get('url') + if params.get("medium") == "url": + location = params.get("url") - elif params.get("medium") == 'kernel_initrd': + elif params.get("medium") == "kernel_initrd": # directory location of kernel/initrd pair (directory layout must # be in format libvirt will recognize) location = params.get("image_dir") - elif params.get("medium") == 'nfs': - location = "nfs:%s:%s" % (params.get("nfs_server"), - params.get("nfs_dir")) + elif params.get("medium") == "nfs": + location = "nfs:%s:%s" % (params.get("nfs_server"), params.get("nfs_dir")) - elif params.get("medium") == 'cdrom': - if params.get("use_libvirt_cdrom_switch") == 'yes': - virt_install_cmd += add_cdrom( - help_text, params.get("cdrom_cd1")) + elif params.get("medium") == "cdrom": + if params.get("use_libvirt_cdrom_switch") == "yes": + virt_install_cmd += add_cdrom(help_text, params.get("cdrom_cd1")) elif params.get("unattended_delivery_method") == "integrated": - cdrom_path = os.path.join(data_dir.get_data_dir(), - params.get("cdrom_unattended")) + cdrom_path = os.path.join( + data_dir.get_data_dir(), params.get("cdrom_unattended") + ) virt_install_cmd += add_cdrom(help_text, cdrom_path) else: - location = os.path.join(data_dir.get_data_dir(), - params.get("cdrom_cd1")) + location = os.path.join( + data_dir.get_data_dir(), params.get("cdrom_cd1") + ) kernel_dir = os.path.dirname(params.get("kernel")) kernel_parent_dir = os.path.dirname(kernel_dir) pxeboot_link = os.path.join(kernel_parent_dir, "pxeboot") @@ -1212,12 +1279,11 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): # selectable OS variant if params.get("use_os_variant") == "yes": if not has_os_variant(os_text, params.get("os_variant")): - raise exceptions.TestSkipError("Unsupported OS variant: %s.\n" - "Supported variants: %s" % - (params.get('os_variant'), - os_text)) - virt_install_cmd += add_os_variant( - help_text, params.get("os_variant")) + raise exceptions.TestSkipError( + "Unsupported OS variant: %s.\n" + "Supported variants: %s" % (params.get("os_variant"), os_text) + ) + virt_install_cmd += add_os_variant(help_text, params.get("os_variant")) # Add serial console virt_install_cmd += add_serial(help_text) @@ -1237,67 +1303,68 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): basename = False image_params = params.object_params(image_name) - base_dir = image_params.get("images_base_dir", - data_dir.get_data_dir()) + base_dir = image_params.get("images_base_dir", data_dir.get_data_dir()) if params.get("storage_type") == "nfs": basename = True base_dir = params["nfs_mount_dir"] - filename = storage.get_image_filename(image_params, - base_dir, basename=basename) + filename = storage.get_image_filename( + image_params, base_dir, basename=basename + ) if image_params.get("use_storage_pool") == "yes": filename = None - virt_install_cmd += add_drive(help_text, - filename, - image_params.get("image_pool"), - image_params.get("image_vol"), - image_params.get("image_device"), - image_params.get("image_bus"), - image_params.get("image_perms"), - image_params.get("image_size"), - image_params.get("drive_sparse"), - image_params.get("drive_cache"), - image_params.get("image_format")) + virt_install_cmd += add_drive( + help_text, + filename, + image_params.get("image_pool"), + image_params.get("image_vol"), + image_params.get("image_device"), + image_params.get("image_bus"), + image_params.get("image_perms"), + image_params.get("image_size"), + image_params.get("drive_sparse"), + image_params.get("drive_cache"), + image_params.get("image_format"), + ) if image_params.get("boot_drive") == "no": continue if filename: - libvirt_controller = image_params.get( - "libvirt_controller", None) + libvirt_controller = image_params.get("libvirt_controller", None) _drive_format = image_params.get("drive_format") if libvirt_controller: if not check_controller(virt_install_cmd, libvirt_controller): virt_install_cmd += add_controller(libvirt_controller) # this will reset the scsi-hd to scsi as we are adding controller # to mention the drive format - if 'scsi' in _drive_format: + if "scsi" in _drive_format: _drive_format = "scsi" - virt_install_cmd += add_drive(help_text, - filename, - None, - None, - None, - _drive_format, - None, - image_params.get("image_size"), - image_params.get("drive_sparse"), - image_params.get("drive_cache"), - image_params.get("image_format")) - - unattended_integrated = (params.get('unattended_delivery_method') != - 'integrated') - xen_pv = self.driver_type == 'xen' and params.get('hvm_or_pv') == 'pv' + virt_install_cmd += add_drive( + help_text, + filename, + None, + None, + None, + _drive_format, + None, + image_params.get("image_size"), + image_params.get("drive_sparse"), + image_params.get("drive_cache"), + image_params.get("image_format"), + ) + + unattended_integrated = params.get("unattended_delivery_method") != "integrated" + xen_pv = self.driver_type == "xen" and params.get("hvm_or_pv") == "pv" if unattended_integrated and not xen_pv: for cdrom in params.objects("cdroms"): cdrom_params = params.object_params(cdrom) iso = cdrom_params.get("cdrom") - if params.get("use_libvirt_cdrom_switch") == 'yes': + if params.get("use_libvirt_cdrom_switch") == "yes": # we don't want to skip the winutils iso - if not cdrom == 'winutils': - LOG.debug( - "Using --cdrom instead of --disk for install") + if not cdrom == "winutils": + LOG.debug("Using --cdrom instead of --disk for install") LOG.debug("Skipping CDROM:%s:%s", cdrom, iso) continue - if params.get("medium") == 'cdrom': + if params.get("medium") == "cdrom": if iso == params.get("cdrom_cd1"): LOG.debug("Using cdrom or url for install") LOG.debug("Skipping CDROM: %s", iso) @@ -1307,17 +1374,19 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): iso_path = utils_misc.get_path(root_dir, iso) iso_image_pool = image_params.get("iso_image_pool") iso_image_vol = image_params.get("iso_image_vol") - virt_install_cmd += add_drive(help_text, - iso_path, - iso_image_pool, - virt_install_cmd, - 'cdrom', - None, - None, - None, - None, - None, - None) + virt_install_cmd += add_drive( + help_text, + iso_path, + iso_image_pool, + virt_install_cmd, + "cdrom", + None, + None, + None, + None, + None, + None, + ) # We may want to add {floppy_otps} parameter for -fda # {fat:floppy:}/path/. However vvfat is not usually recommended. @@ -1326,23 +1395,27 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): floppy = params.get("floppy_name") if floppy: floppy = utils_misc.get_path(data_dir.get_data_dir(), floppy) - virt_install_cmd += add_drive(help_text, floppy, - None, - None, - 'floppy', - None, - None, - None, - None, - None, - None) + virt_install_cmd += add_drive( + help_text, + floppy, + None, + None, + "floppy", + None, + None, + None, + None, + None, + None, + ) # setup networking parameters for nic in vm.virtnet: # make_create_command can be called w/o vm.create() nic = vm.add_nic(**dict(nic)) - LOG.debug("make_create_command() setting up command for" - " nic: %s" % str(nic)) + LOG.debug( + "make_create_command() setting up command for" " nic: %s" % str(nic) + ) virt_install_cmd += add_nic(help_text, nic) if params.get("use_no_reboot") == "yes": @@ -1356,14 +1429,14 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): emulator_path = params.get("emulator_path", None) if emulator_path: - if not has_sub_option('boot', 'emulator'): + if not has_sub_option("boot", "emulator"): LOG.warning("emulator option not supported by virt-install") else: virt_install_cmd += " --boot emulator=%s" % emulator_path bios_path = params.get("bios_path", None) if bios_path: - if not has_sub_option('boot', 'loader'): + if not has_sub_option("boot", "loader"): LOG.warning("bios option not supported by virt-install") else: if "--boot" in virt_install_cmd: @@ -1376,13 +1449,13 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): initrd = params.get("initrd", None) kernel_args = params.get("kernel_args", None) if (kernel or initrd) and kernel_args: - virt_install_cmd += add_kernel(help_text, virt_install_cmd, kernel, - initrd, kernel_args) + virt_install_cmd += add_kernel( + help_text, virt_install_cmd, kernel, initrd, kernel_args + ) # bz still open, not fully functional yet if params.get("use_virt_install_wait") == "yes": - virt_install_cmd += (" --wait %s" % - params.get("virt_install_wait_time")) + virt_install_cmd += " --wait %s" % params.get("virt_install_wait_time") kernel_params = params.get("kernel_params") if kernel_params: @@ -1394,22 +1467,28 @@ def add_tpmdevice(help_text, device_path, model=None, type=None): if tpm_device: tpm_model = params.get("tpm_model", None) tpm_type = params.get("tpm_type", None) - virt_install_cmd += add_tpmdevice(help_text, tpm_device, tpm_model, - tpm_type) + virt_install_cmd += add_tpmdevice( + help_text, tpm_device, tpm_model, tpm_type + ) sec_type = params.get("sec_type", None) if sec_type: sec_label = params.get("sec_label", None) sec_relabel = params.get("sec_relabel", None) - virt_install_cmd += add_security(help_text, sec_type=sec_type, - sec_label=sec_label, - sec_relabel=sec_relabel) + virt_install_cmd += add_security( + help_text, + sec_type=sec_type, + sec_label=sec_label, + sec_relabel=sec_relabel, + ) # Additional qemu commandline options to virt-install directly # helps to test new feature from qemu if has_option(help_text, "qemu-commandline"): virtinstall_qemu_cmdline = params.get("virtinstall_qemu_cmdline", "") if virtinstall_qemu_cmdline: - virt_install_cmd += ' --qemu-commandline="%s"' % virtinstall_qemu_cmdline + virt_install_cmd += ( + ' --qemu-commandline="%s"' % virtinstall_qemu_cmdline + ) compat = params.get("qemu_compat") if compat: @@ -1428,16 +1507,20 @@ 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)) + return "serial-%s-%s-%s.log" % ( + name, + self.name, + utils_misc.generate_random_string(4), + ) def get_serial_console_filenames(self): """ Return a list of all serial console filenames (as specified in the VM's params). """ - return [self.get_serial_console_filename(_) for _ in - self.params.objects("serials")] + return [ + self.get_serial_console_filename(_) for _ in self.params.objects("serials") + ] def _create_serial_console(self): """ @@ -1450,10 +1533,10 @@ def _create_serial_console(self): self.serial_ports.append(serial) if self.serial_console is None or self.serial_console.closed: try: - cmd = 'virsh' + cmd = "virsh" if self.connect_uri: - cmd += ' -c %s' % self.connect_uri - cmd += (" console %s %s" % (self.name, self.serial_ports[0])) + cmd += " -c %s" % self.connect_uri + cmd += " console %s %s" % (self.name, self.serial_ports[0]) except IndexError: raise virt_vm.VMConfigMissingError(self.name, "serial") output_func = utils_logfile.log_line # Because qemu-kvm uses this @@ -1463,16 +1546,22 @@ def _create_serial_console(self): output_params = (output_filename,) prompt = self.params.get("shell_prompt", "[\#\$]") LOG.debug("Command used to create serial console: %s", cmd) - self.serial_console = aexpect.ShellSession(command=cmd, auto_close=False, - output_func=output_func, - output_params=output_params, - prompt=prompt) + self.serial_console = aexpect.ShellSession( + command=cmd, + auto_close=False, + output_func=output_func, + output_params=output_params, + prompt=prompt, + ) if not self.serial_console.is_alive(): LOG.error("Failed to create serial_console") - self.serial_console_log = os.path.join(utils_logfile.get_log_file_dir(), - output_filename) + self.serial_console_log = os.path.join( + utils_logfile.get_log_file_dir(), output_filename + ) # Cause serial_console.close() to close open log file - self.serial_console.close_hooks += [utils_logfile.close_own_log_file(self.serial_console_log)] + self.serial_console.close_hooks += [ + utils_logfile.close_own_log_file(self.serial_console_log) + ] def set_root_serial_console(self, device, remove=False): """ @@ -1494,8 +1583,7 @@ def set_root_serial_console(self, device, remove=False): session.sendline("echo %s >> /etc/securetty" % device) else: if remove: - session.sendline("sed -i -e /%s/d /etc/securetty" - % device) + session.sendline("sed -i -e /%s/d /etc/securetty" % device) LOG.debug("Set root login for %s successfully.", device) return True finally: @@ -1503,8 +1591,9 @@ def set_root_serial_console(self, device, remove=False): LOG.debug("Set root login for %s failed.", device) return False - def set_kernel_console(self, device, speed=None, remove=False, - guest_arch_name='x86_64'): + def set_kernel_console( + self, device, speed=None, remove=False, guest_arch_name="x86_64" + ): """ Set kernel parameter for given console device. @@ -1514,15 +1603,18 @@ def set_kernel_console(self, device, speed=None, remove=False, :param guest_arch_name: architecture of the guest to update kernel param """ from . import utils_test + kernel_params = "console=%s" % device if speed is not None: kernel_params += ",%s" % speed if remove: - utils_test.update_boot_option(self, args_removed=kernel_params, - guest_arch_name=guest_arch_name) + utils_test.update_boot_option( + self, args_removed=kernel_params, guest_arch_name=guest_arch_name + ) else: - utils_test.update_boot_option(self, args_added=kernel_params, - guest_arch_name=guest_arch_name) + utils_test.update_boot_option( + self, args_added=kernel_params, guest_arch_name=guest_arch_name + ) LOG.debug("Set kernel params for %s is successful", device) return True @@ -1545,15 +1637,20 @@ def set_kernel_param(self, parameter, value=None, remove=False): if not grub_path: return False grub_text = session.cmd_output("cat %s" % grub_path) - kernel_lines = [l.strip() for l in grub_text.splitlines() - if re.match(r"\s*(linux|kernel).*", l)] + kernel_lines = [ + l.strip() + for l in grub_text.splitlines() + if re.match(r"\s*(linux|kernel).*", l) + ] if not kernel_lines: - LOG.error("Can't find any kernel lines in grub " - "file %s:\n%s" % (grub_path, grub_text)) + LOG.error( + "Can't find any kernel lines in grub " + "file %s:\n%s" % (grub_path, grub_text) + ) return False for line in kernel_lines: - line = line.replace('\t', r'\t') + line = line.replace("\t", r"\t") if remove: new_string = "" else: @@ -1579,21 +1676,18 @@ def set_kernel_param(self, parameter, value=None, remove=False): new_line = " ".join((line, new_string)) line_patt = "\s*".join(line.split()) - LOG.debug("Substituting grub line '%s' to '%s'." % - (line, new_line)) + LOG.debug("Substituting grub line '%s' to '%s'." % (line, new_line)) stat_sed, output = session.cmd_status_output( - "sed -i --follow-symlinks -e \"s@%s@%s@g\" %s" % - (line_patt, new_line, grub_path)) + 'sed -i --follow-symlinks -e "s@%s@%s@g" %s' + % (line_patt, new_line, grub_path) + ) if stat_sed: - LOG.error("Failed to substitute grub file:\n%s" % - output) + LOG.error("Failed to substitute grub file:\n%s" % output) return False if remove: - LOG.debug("Remove kernel params %s successfully.", - parameter) + LOG.debug("Remove kernel params %s successfully.", parameter) else: - LOG.debug("Set kernel params %s to %s successfully.", - parameter, value) + LOG.debug("Set kernel params %s to %s successfully.", parameter, value) return True finally: session.close() @@ -1632,7 +1726,10 @@ def set_boot_kernel(self, index, debug_kernel=False): index = -1 LOG.info("Setting debug kernel as default") for i in range(len(kernel_list)): - if "debug" in kernel_list[i] and 'rescue' not in kernel_list[i].lower(): + if ( + "debug" in kernel_list[i] + and "rescue" not in kernel_list[i].lower() + ): index = i break if index == -1: @@ -1644,7 +1741,7 @@ def set_boot_kernel(self, index, debug_kernel=False): boot_kernel = kernel_list[index].strip("-") else: boot_kernel = kernel_list[index].split("'")[1].strip("'") - cmd_set_grub = 'grub2-set-default %d' % index + cmd_set_grub = "grub2-set-default %d" % index session.cmd(cmd_set_grub) return boot_kernel finally: @@ -1703,7 +1800,7 @@ def create_swap_partition(self, swap_path=None): LOG.error("Failed to create a swap partition.") return False - def create_swap_file(self, swapfile='/swapfile'): + def create_swap_file(self, swapfile="/swapfile"): """ Make a swap file and active it through a session. @@ -1722,13 +1819,14 @@ def create_swap_file(self, swapfile='/swapfile'): swap_size = self.get_used_mem() // 1024 # Create, change permission, and make a swap file. - cmd = ("dd if=/dev/zero of={1} bs=1M count={0} && " - "chmod 600 {1} && " - "mkswap {1}".format(swap_size, swapfile)) + cmd = ( + "dd if=/dev/zero of={1} bs=1M count={0} && " + "chmod 600 {1} && " + "mkswap {1}".format(swap_size, swapfile) + ) stat_create, output = session.cmd_status_output(cmd) if stat_create: - LOG.error("Fail to create swap file in guest." - "\n%s" % output) + LOG.error("Fail to create swap file in guest." "\n%s" % output) return False self.created_swap_file = swapfile @@ -1750,8 +1848,8 @@ def create_swap_file(self, swapfile='/swapfile'): # 0 0 2465792 32768 # ... offset_line = output.splitlines()[3] - if '..' in offset_line: - offset = offset_line.split()[3].rstrip('..') + if ".." in offset_line: + offset = offset_line.split()[3].rstrip("..") else: offset = offset_line.split()[2] @@ -1777,8 +1875,7 @@ def create_swap_file(self, swapfile='/swapfile'): cmd = "swapon %s" % swapfile stat_swapon, output = session.cmd_status_output(cmd) if stat_create: - LOG.error("Fail to activate swap file in guest." - "\n%s" % output) + LOG.error("Fail to activate swap file in guest." "\n%s" % output) return False finally: session.close() @@ -1816,8 +1913,7 @@ def cleanup_swap(self): # Cold unplug attached swap disk if self.shutdown(): if "created_swap_device" in dir(self): - self.detach_disk( - self.created_swap_device, extra="--persistent") + self.detach_disk(self.created_swap_device, extra="--persistent") del self.created_swap_device if "created_swap_path" in dir(self): os.remove(self.created_swap_path) @@ -1852,8 +1948,9 @@ def set_console_getty(self, device, getty="mgetty", remove=False): session.sendline("echo %s >> /etc/inittab" % getty_str) else: if remove: - session.sendline("sed -i -e /%s/d " - "/etc/inittab" % matched_str) + session.sendline( + "sed -i -e /%s/d " "/etc/inittab" % matched_str + ) LOG.debug("Set inittab for %s successfully.", device) return True finally: @@ -1878,10 +1975,16 @@ def cleanup_serial_console(self): except OSError: pass - def wait_for_login(self, nic_index=0, timeout=None, - internal_timeout=None, - serial=False, restart_network=False, - username=None, password=None): + def wait_for_login( + self, + nic_index=0, + timeout=None, + internal_timeout=None, + serial=False, + restart_network=False, + username=None, + password=None, + ): """ Override the wait_for_login method of virt_vm to support other guest in libvirt. @@ -1901,18 +2004,31 @@ def wait_for_login(self, nic_index=0, timeout=None, if self.is_lxc(): self.cleanup_serial_console() self.create_serial_console() - return self.wait_for_serial_login(timeout, internal_timeout, - restart_network, - username, password) - - return super(VM, self).wait_for_login(nic_index, timeout, - internal_timeout, - serial, restart_network, - username, password) + return self.wait_for_serial_login( + timeout, internal_timeout, restart_network, username, password + ) + + return super(VM, self).wait_for_login( + nic_index, + timeout, + internal_timeout, + serial, + restart_network, + username, + password, + ) @error_context.context_aware - def create(self, name=None, params=None, root_dir=None, timeout=5.0, - migration_mode=None, mac_source=None, autoconsole=True): + def create( + self, + name=None, + params=None, + root_dir=None, + timeout=5.0, + migration_mode=None, + mac_source=None, + autoconsole=True, + ): """ Start the VM by running a qemu command. All parameters are optional. If name, params or root_dir are not @@ -1951,17 +2067,15 @@ def create(self, name=None, params=None, root_dir=None, timeout=5.0, root_dir = self.root_dir if self.params.get("storage_type") == "nfs": - storage.copy_nfs_image(self.params, data_dir.get_data_dir(), - basename=True) + storage.copy_nfs_image(self.params, data_dir.get_data_dir(), basename=True) # Verify the md5sum of the ISO images for cdrom in params.objects("cdroms"): if params.get("medium") == "import": break cdrom_params = params.object_params(cdrom) iso = cdrom_params.get("cdrom") - xen_pv = (self.driver_type == 'xen' and - params.get('hvm_or_pv') == 'pv') - iso_is_ks = os.path.basename(iso) == 'ks.iso' + xen_pv = self.driver_type == "xen" and params.get("hvm_or_pv") == "pv" + iso_is_ks = os.path.basename(iso) == "ks.iso" if xen_pv and iso_is_ks: continue if iso: @@ -1972,21 +2086,24 @@ def create(self, name=None, params=None, root_dir=None, timeout=5.0, if cdrom_params.get("skip_hash", "no") == "yes": LOG.debug("Skipping hash comparison") elif cdrom_params.get("md5sum_1m"): - LOG.debug("Comparing expected MD5 sum with MD5 sum of " - "first MB of ISO file...") - actual_hash = crypto.hash_file( - iso, 1048576, algorithm="md5") + LOG.debug( + "Comparing expected MD5 sum with MD5 sum of " + "first MB of ISO file..." + ) + actual_hash = crypto.hash_file(iso, 1048576, algorithm="md5") expected_hash = cdrom_params.get("md5sum_1m") compare = True elif cdrom_params.get("md5sum"): - LOG.debug("Comparing expected MD5 sum with MD5 sum of " - "ISO file...") + LOG.debug( + "Comparing expected MD5 sum with MD5 sum of " "ISO file..." + ) actual_hash = crypto.hash_file(iso, algorithm="md5") expected_hash = cdrom_params.get("md5sum") compare = True elif cdrom_params.get("sha1sum"): - LOG.debug("Comparing expected SHA1 sum with SHA1 sum " - "of ISO file...") + LOG.debug( + "Comparing expected SHA1 sum with SHA1 sum " "of ISO file..." + ) actual_hash = crypto.hash_file(iso, algorithm="sha1") expected_hash = cdrom_params.get("sha1sum") compare = True @@ -1994,21 +2111,20 @@ def create(self, name=None, params=None, root_dir=None, timeout=5.0, if actual_hash == expected_hash: LOG.debug("Hashes match") else: - raise virt_vm.VMHashMismatchError(actual_hash, - expected_hash) + raise virt_vm.VMHashMismatchError(actual_hash, expected_hash) # Make sure the following code is not executed by more than one thread # at the same time - lockfilename = os.path.join(data_dir.get_tmp_dir(), - "libvirt-autotest-vm-create.lock") + lockfilename = os.path.join( + data_dir.get_tmp_dir(), "libvirt-autotest-vm-create.lock" + ) lockfile = open(lockfilename, "w+") fcntl.lockf(lockfile, fcntl.LOCK_EX) try: # Handle port redirections redir_names = params.objects("redirs") - host_ports = utils_misc.find_free_ports( - 5000, 5899, len(redir_names)) + host_ports = utils_misc.find_free_ports(5000, 5899, len(redir_names)) self.redirs = {} for i in range(len(redir_names)): redir_params = params.object_params(redir_names[i]) @@ -2045,13 +2161,15 @@ def create(self, name=None, params=None, root_dir=None, timeout=5.0, if mac_source is not None: # Will raise exception if source doesn't # have corresponding nic - LOG.debug("Copying mac for nic %s from VM %s", - nic.nic_name, mac_source.name) - nic_params['mac'] = mac_source.get_mac_address( - nic.nic_name) + LOG.debug( + "Copying mac for nic %s from VM %s", + nic.nic_name, + mac_source.name, + ) + nic_params["mac"] = mac_source.get_mac_address(nic.nic_name) # make_create_command() calls vm.add_nic (i.e. on a copy) nic = self.add_nic(**nic_params) - LOG.debug('VM.create activating nic %s' % nic) + LOG.debug("VM.create activating nic %s" % nic) self.activate_nic(nic.nic_name) # Make qemu command @@ -2065,36 +2183,42 @@ def create(self, name=None, params=None, root_dir=None, timeout=5.0, except process.CmdError as details: stderr = details.result.stderr_text.strip() # This is a common newcomer mistake, be more helpful... - if stderr.count('IDE CDROM must use'): - testname = params.get('name', "") - if testname.count('unattended_install.cdrom'): - if not testname.count('http_ks'): - e_msg = ("Install command " - "failed:\n%s \n\nNote: " - "Older versions of " - "libvirt won't work " - "properly with kickstart " - "on cdrom install. " - "Try using the " - "unattended_install.cdrom.http_ks method " - "instead." % details.result) + if stderr.count("IDE CDROM must use"): + testname = params.get("name", "") + if testname.count("unattended_install.cdrom"): + if not testname.count("http_ks"): + e_msg = ( + "Install command " + "failed:\n%s \n\nNote: " + "Older versions of " + "libvirt won't work " + "properly with kickstart " + "on cdrom install. " + "Try using the " + "unattended_install.cdrom.http_ks method " + "instead." % details.result + ) raise exceptions.TestSkipError(e_msg) - if stderr.count('failed to launch bridge helper'): + if stderr.count("failed to launch bridge helper"): if utils_selinux.is_enforcing(): - raise exceptions.TestSkipError("SELinux is enabled " - "and preventing the " - "bridge helper from " - "accessing the bridge. " - "Consider running as " - "root or placing " - "SELinux into " - "permissive mode.") + raise exceptions.TestSkipError( + "SELinux is enabled " + "and preventing the " + "bridge helper from " + "accessing the bridge. " + "Consider running as " + "root or placing " + "SELinux into " + "permissive mode." + ) # 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)) + utils_misc.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 = result.stdout_text.strip() # Create isa serial ports. @@ -2103,8 +2227,7 @@ def create(self, name=None, params=None, root_dir=None, timeout=5.0, fcntl.lockf(lockfile, fcntl.LOCK_UN) lockfile.close() - def migrate(self, dest_uri="", option="--live --timeout 60", extra="", - **dargs): + def migrate(self, dest_uri="", option="--live --timeout 60", extra="", **dargs): """ Migrate a VM to a remote host. @@ -2116,11 +2239,12 @@ def migrate(self, dest_uri="", option="--live --timeout 60", extra="", """ # Close down serial_console logging process self.cleanup_serial_console() - LOG.info("Migrating VM %s from %s to %s" % - (self.name, self.connect_uri, dest_uri)) - result = virsh.migrate(self.name, dest_uri, option, - extra, uri=self.connect_uri, - **dargs) + LOG.info( + "Migrating VM %s from %s to %s" % (self.name, self.connect_uri, dest_uri) + ) + result = virsh.migrate( + self.name, dest_uri, option, extra, uri=self.connect_uri, **dargs + ) # On successful migration, point to guests new hypervisor. # Since dest_uri could be None, checking it is necessary. if result.exit_status == 0 and dest_uri: @@ -2137,8 +2261,15 @@ def migrate(self, dest_uri="", option="--live --timeout 60", extra="", self.name = migrate_options_list[migrate_options_list.index("--dname") + 1] return result - def attach_disk(self, source, target=None, prefix="vd", extra="", - ignore_status=False, debug=False): + def attach_disk( + self, + source, + target=None, + prefix="vd", + extra="", + ignore_status=False, + debug=False, + ): """ Attach a disk to VM and return the target device name. @@ -2156,49 +2287,62 @@ def attach_disk(self, source, target=None, prefix="vd", extra="", if target not in disks: break - result = virsh.attach_disk(self.name, source, target, extra, - uri=self.connect_uri, - ignore_status=ignore_status, - debug=debug) + result = virsh.attach_disk( + self.name, + source, + target, + extra, + uri=self.connect_uri, + ignore_status=ignore_status, + debug=debug, + ) if result.exit_status: - LOG.error("Failed to attach disk %s to VM." - "Detail: %s." - % (source, result.stderr_text)) + LOG.error( + "Failed to attach disk %s to VM." + "Detail: %s." % (source, result.stderr_text) + ) return None return target - def detach_disk(self, target, extra="", - ignore_status=False, debug=False): + def detach_disk(self, target, extra="", ignore_status=False, debug=False): """ Detach a disk from VM. :param target: target of disk device need to be detached. :param extra: additional arguments to command """ - return virsh.detach_disk(self.name, target, extra, - uri=self.connect_uri, - ignore_status=ignore_status, - debug=debug) + return virsh.detach_disk( + self.name, + target, + extra, + uri=self.connect_uri, + ignore_status=ignore_status, + debug=debug, + ) - def attach_interface(self, option="", ignore_status=False, - debug=False): + def attach_interface(self, option="", ignore_status=False, debug=False): """ Attach a NIC to VM. """ - return virsh.attach_interface(self.name, option, - uri=self.connect_uri, - ignore_status=ignore_status, - debug=debug) + return virsh.attach_interface( + self.name, + option, + uri=self.connect_uri, + ignore_status=ignore_status, + debug=debug, + ) - def detach_interface(self, option="", ignore_status=False, - debug=False): + def detach_interface(self, option="", ignore_status=False, debug=False): """ Detach a NIC from VM. """ - return virsh.detach_interface(self.name, option, - uri=self.connect_uri, - ignore_status=ignore_status, - debug=debug) + return virsh.detach_interface( + self.name, + option, + uri=self.connect_uri, + ignore_status=ignore_status, + debug=debug, + ) def destroy(self, gracefully=True, free_mac_addresses=True): """ @@ -2219,8 +2363,11 @@ def destroy(self, gracefully=True, free_mac_addresses=True): LOG.debug("Destroying VM") if self.is_paused(): self.resume() - if (not self.is_lxc() and gracefully and - self.params.get("shutdown_command")): + if ( + not self.is_lxc() + and gracefully + and self.params.get("shutdown_command") + ): # Try to destroy with shell command LOG.debug("Trying to shutdown VM with shell command") try: @@ -2230,10 +2377,10 @@ def destroy(self, gracefully=True, free_mac_addresses=True): else: try: # Send the shutdown command - session.sendline( - self.params.get("shutdown_command")) - LOG.debug("Shutdown command sent; waiting for VM " - "to go down...") + session.sendline(self.params.get("shutdown_command")) + LOG.debug( + "Shutdown command sent; waiting for VM " "to go down..." + ) if utils_misc.wait_for(self.is_dead, 60, 1, 1): LOG.debug("VM is down") return @@ -2241,17 +2388,19 @@ def destroy(self, gracefully=True, free_mac_addresses=True): session.close() # Destroy VM directly, as 'ignore_status=True' by default, so destroy # a shutoff domain is also acceptable here. - destroy_opt = '' + destroy_opt = "" if gracefully: - destroy_opt = '--graceful' + destroy_opt = "--graceful" virsh.destroy(self.name, destroy_opt, uri=self.connect_uri) finally: self.cleanup_serial_console() if free_mac_addresses: if self.is_persistent(): - LOG.warning("Requested MAC address release from " - "persistent vm %s. Ignoring." % self.name) + LOG.warning( + "Requested MAC address release from " + "persistent vm %s. Ignoring." % self.name + ) else: LOG.debug("Releasing MAC addresses for vm %s." % self.name) for nic_name in self.virtnet.nic_name_list(): @@ -2287,7 +2436,7 @@ def remove_with_storage(self): blklist = list(self.get_disk_devices().values()) self.remove() for blk in blklist: - path = blk['source'] + path = blk["source"] if os.path.exists(path): os.remove(path) @@ -2315,14 +2464,15 @@ def get_virsh_mac_address(self, nic_index=0): """ cmd_result = virsh.dumpxml(self.name, uri=self.connect_uri) if cmd_result.exit_status: - raise exceptions.TestFail("dumpxml %s failed.\n" - "Detail: %s.\n" % (self.name, cmd_result)) + raise exceptions.TestFail( + "dumpxml %s failed.\n" "Detail: %s.\n" % (self.name, cmd_result) + ) thexml = cmd_result.stdout_text.strip() xtf = xml_utils.XMLTreeFile(thexml) - interfaces = xtf.find('devices').findall('interface') + interfaces = xtf.find("devices").findall("interface") # Range check try: - mac = interfaces[nic_index].find('mac').get('address') + mac = interfaces[nic_index].find("mac").get("address") if mac is not None: return mac except IndexError: @@ -2369,8 +2519,11 @@ def get_pid(self): except IOError: LOG.error("Could not read %s to get PID", pid_file) except TypeError: - LOG.error("PID file %s has invalid contents: '%s'", - pid_file, pid_file_contents) + LOG.error( + "PID file %s has invalid contents: '%s'", + pid_file, + pid_file_contents, + ) else: LOG.debug("PID file %s not present", pid_file) @@ -2382,10 +2535,10 @@ def get_vcpus_pid(self): :return: list of PID of vcpus of a VM. """ - output = virsh.qemu_monitor_command(self.name, "info cpus", "--hmp", - uri=self.connect_uri) - vcpu_pids = re.findall(r'thread_id=(\d+)', - output.stdout_text) + output = virsh.qemu_monitor_command( + self.name, "info cpus", "--hmp", uri=self.connect_uri + ) + vcpu_pids = re.findall(r"thread_id=(\d+)", output.stdout_text) return vcpu_pids def get_shell_pid(self): @@ -2424,8 +2577,7 @@ def get_cpu_topology_in_cmdline(self): LOG.error("Fail to get VM pid") else: cmdline = open("/proc/%d/cmdline" % vm_pid).read() - values = re.findall("sockets=(\d+),cores=(\d+),threads=(\d+)", - cmdline)[0] + values = re.findall("sockets=(\d+),cores=(\d+),threads=(\d+)", cmdline)[0] cpu_topology = dict(zip(["sockets", "cores", "threads"], values)) return cpu_topology @@ -2433,9 +2585,9 @@ def get_cpu_topology_in_vm(self): cpu_topology = {} cpu_info = cpu.get_cpu_info(self.wait_for_login()) if cpu_info: - cpu_topology['sockets'] = cpu_info['Socket(s)'] - cpu_topology['cores'] = cpu_info['Core(s) per socket'] - cpu_topology['threads'] = cpu_info['Thread(s) per core'] + cpu_topology["sockets"] = cpu_info["Socket(s)"] + cpu_topology["cores"] = cpu_info["Core(s) per socket"] + cpu_topology["threads"] = cpu_info["Thread(s) per core"] return cpu_topology def activate_nic(self, nic_index_or_name): @@ -2447,8 +2599,9 @@ def deactivate_nic(self, nic_index_or_name): pass # Just a stub for now @error_context.context_aware - def reboot(self, session=None, method="shell", nic_index=0, timeout=240, - serial=False): + def reboot( + self, session=None, method="shell", nic_index=0, timeout=240, serial=False + ): """ Reboot the VM and wait for it to come back up by trying to log in until timeout expires. @@ -2473,9 +2626,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", LOG.info) - if not utils_misc.wait_for(lambda: not - session.is_responsive(timeout=30), - 120, 0, 1): + if not utils_misc.wait_for( + lambda: not session.is_responsive(timeout=30), 120, 0, 1 + ): raise virt_vm.VMRebootError("Guest refuses to go down") session.close() @@ -2500,12 +2653,15 @@ 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 = utils_misc.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") + raise virt_vm.VMStartError( + self.name, "libvirt domain not " "active after start" + ) uid_result = virsh.domuuid(self.name, uri=self.connect_uri) self.uuid = uid_result.stdout_text.strip() # Establish a session with the serial console @@ -2513,25 +2669,32 @@ def start(self, autoconsole=True): self.create_serial_console() else: LOG.error("VM fails to start with:%s", result) - raise virt_vm.VMStartError(self.name, - reason=result.stderr_text.strip(), - status=result.exit_status) + raise virt_vm.VMStartError( + self.name, reason=result.stderr_text.strip(), status=result.exit_status + ) # Pull in mac addresses from libvirt guest definition for index, nic in enumerate(self.virtnet): try: mac = self.get_virsh_mac_address(index) - if 'mac' not in nic: - LOG.debug("Updating nic %d with mac %s on vm %s" - % (index, mac, self.name)) + if "mac" not in nic: + LOG.debug( + "Updating nic %d with mac %s on vm %s" % (index, mac, self.name) + ) nic.mac = mac elif nic.mac != mac: - LOG.warning("Requested mac %s doesn't match mac %s " - "as defined for vm %s", nic.mac, mac, self.name) + LOG.warning( + "Requested mac %s doesn't match mac %s " "as defined for vm %s", + nic.mac, + mac, + self.name, + ) # TODO: Checkout/Set nic_model, nettype, netdst also except virt_vm.VMMACAddressMissingError: - LOG.warning("Nic %d requested by test but not defined for" - " vm %s" % (index, self.name)) + LOG.warning( + "Nic %d requested by test but not defined for" + " vm %s" % (index, self.name) + ) def wait_for_shutdown(self, count=60): """ @@ -2560,7 +2723,7 @@ def shutdown(self): Shuts down this VM. """ try: - if self.state() != 'shut off': + if self.state() != "shut off": virsh.shutdown(self.name, uri=self.connect_uri) if self.wait_for_shutdown(): LOG.debug("VM %s shut down", self.name) @@ -2576,9 +2739,8 @@ def shutdown(self): def pause(self): try: state = self.state() - if state != 'paused': - virsh.suspend( - self.name, uri=self.connect_uri, ignore_status=False) + if state != "paused": + virsh.suspend(self.name, uri=self.connect_uri, ignore_status=False) return True except Exception: LOG.error("VM %s failed to suspend", self.name) @@ -2601,14 +2763,13 @@ def save_to_file(self, path): Override BaseVM save_to_file method """ if self.is_dead(): - raise virt_vm.VMStatusError( - "Cannot save a VM that is %s" % self.state()) + raise virt_vm.VMStatusError("Cannot save a VM that is %s" % self.state()) LOG.debug("Saving VM %s to %s" % (self.name, path)) result = virsh.save(self.name, path, uri=self.connect_uri) if result.exit_status: - raise virt_vm.VMError("Save VM to %s failed.\n" - "Detail: %s." - % (path, result.stderr_text)) + raise virt_vm.VMError( + "Save VM to %s failed.\n" "Detail: %s." % (path, result.stderr_text) + ) if self.is_alive(): raise virt_vm.VMStatusError("VM not shut off after save") self.cleanup_serial_console() @@ -2618,17 +2779,18 @@ def restore_from_file(self, path): Override BaseVM restore_from_file method """ if self.is_alive(): - raise virt_vm.VMStatusError( - "Can not restore VM that is %s" % self.state()) + raise virt_vm.VMStatusError("Can not restore VM that is %s" % self.state()) LOG.debug("Restoring VM from %s" % path) result = virsh.restore(path, uri=self.connect_uri) if result.exit_status: - raise virt_vm.VMError("Restore VM from %s failed.\n" - "Detail: %s." - % (path, result.stderr_text)) + raise virt_vm.VMError( + "Restore VM from %s failed.\n" + "Detail: %s." % (path, result.stderr_text) + ) if self.is_dead(): raise virt_vm.VMStatusError( - "VM should not be %s after restore." % self.state()) + "VM should not be %s after restore." % self.state() + ) self.create_serial_console() def managedsave(self): @@ -2636,32 +2798,33 @@ def managedsave(self): Managed save of VM's state """ if self.is_dead(): - raise virt_vm.VMStatusError( - "Cannot save a VM that is %s" % self.state()) + raise virt_vm.VMStatusError("Cannot save a VM that is %s" % self.state()) LOG.debug("Managed saving VM %s" % self.name) result = virsh.managedsave(self.name, uri=self.connect_uri) if result.exit_status: - raise virt_vm.VMError("Managed save VM failed.\n" - "Detail: %s." - % result.stderr_text) + raise virt_vm.VMError( + "Managed save VM failed.\n" "Detail: %s." % result.stderr_text + ) if self.is_alive(): raise virt_vm.VMStatusError("VM not shut off after managed save") self.cleanup_serial_console() - def pmsuspend(self, target='mem', duration=0): + def pmsuspend(self, target="mem", duration=0): """ Suspend a domain gracefully using power management functions """ if self.is_dead(): raise virt_vm.VMStatusError( - "Cannot pmsuspend a VM that is %s" % self.state()) + "Cannot pmsuspend a VM that is %s" % self.state() + ) LOG.debug("PM suspending VM %s" % self.name) - result = virsh.dompmsuspend(self.name, target=target, - duration=duration, uri=self.connect_uri) + result = virsh.dompmsuspend( + self.name, target=target, duration=duration, uri=self.connect_uri + ) if result.exit_status: - raise virt_vm.VMError("PM suspending VM failed.\n" - "Detail: %s." - % result.stderr_text) + raise virt_vm.VMError( + "PM suspending VM failed.\n" "Detail: %s." % result.stderr_text + ) self.cleanup_serial_console() def pmwakeup(self): @@ -2670,24 +2833,25 @@ def pmwakeup(self): """ if self.is_dead(): raise virt_vm.VMStatusError( - "Cannot pmwakeup a VM that is %s" % self.state()) + "Cannot pmwakeup a VM that is %s" % self.state() + ) LOG.debug("PM waking up VM %s" % self.name) result = virsh.dompmwakeup(self.name, uri=self.connect_uri) if result.exit_status: - raise virt_vm.VMError("PM waking up VM failed.\n" - "Detail: %s." - % result.stderr_text) + raise virt_vm.VMError( + "PM waking up VM failed.\n" "Detail: %s." % result.stderr_text + ) self.create_serial_console() def vcpupin(self, vcpu, cpu_list, options=""): """ To pin vcpu to cpu_list """ - result = virsh.vcpupin(self.name, vcpu, cpu_list, - options, uri=self.connect_uri) + result = virsh.vcpupin(self.name, vcpu, cpu_list, options, uri=self.connect_uri) if result.exit_status: - raise exceptions.TestFail("Virsh vcpupin command failed.\n" - "Detail: %s.\n" % result) + raise exceptions.TestFail( + "Virsh vcpupin command failed.\n" "Detail: %s.\n" % result + ) def dominfo(self): """ @@ -2698,8 +2862,8 @@ def dominfo(self): # Key: word before ':' | value: content after ':' (stripped) dominfo_dict = {} for line in output.splitlines(): - key = line.split(':')[0].strip() - value = line.split(':')[-1].strip() + key = line.split(":")[0].strip() + value = line.split(":")[-1].strip() dominfo_dict[key] = value return dominfo_dict @@ -2713,8 +2877,8 @@ def vcpuinfo(self): vcpuinfo_list = [] vcpuinfo_dict = {} for line in output.splitlines(): - key = line.split(':')[0].strip() - value = line.split(':')[-1].strip() + key = line.split(":")[0].strip() + value = line.split(":")[-1].strip() vcpuinfo_dict[key] = value if key == "CPU Affinity": vcpuinfo_list.append(vcpuinfo_dict) @@ -2725,8 +2889,7 @@ def domfsinfo(self): Return a dict's list include domain mounted filesystem information via virsh command """ - result = virsh.domfsinfo(self.name, ignore_status=False, - uri=self.connect_uri) + result = virsh.domfsinfo(self.name, ignore_status=False, uri=self.connect_uri) lines = result.stdout_text.strip().splitlines() domfsinfo_list = [] if len(lines) > 2: @@ -2744,7 +2907,7 @@ def get_used_mem(self): Get vm's current memory(kilobytes). """ dominfo_dict = self.dominfo() - memory = dominfo_dict['Used memory'].split(' ')[0] # strip off ' kb' + memory = dominfo_dict["Used memory"].split(" ")[0] # strip off ' kb' return int(memory) def get_blk_devices(self): @@ -2757,8 +2920,9 @@ def get_blk_devices(self): """ domblkdict = {} options = "--details" - result = virsh.domblklist(self.name, options, ignore_status=True, - uri=self.connect_uri) + result = virsh.domblklist( + self.name, options, ignore_status=True, uri=self.connect_uri + ) blklist = result.stdout_text.strip().splitlines() if result.exit_status != 0: LOG.info("Get vm devices failed.") @@ -2767,9 +2931,11 @@ def get_blk_devices(self): for line in blklist: linesplit = line.split(None, 4) target = linesplit[2] - blk_detail = {'type': linesplit[0], - 'device': linesplit[1], - 'source': linesplit[3]} + blk_detail = { + "type": linesplit[0], + "device": linesplit[1], + "source": linesplit[3], + } domblkdict[target] = blk_detail return domblkdict @@ -2781,7 +2947,7 @@ def get_disk_devices(self): disk_devices = {} for target in blk_devices: details = blk_devices[target] - if details['device'] == "disk": + if details["device"] == "disk": disk_devices[target] = details return disk_devices @@ -2791,24 +2957,26 @@ def get_first_disk_devices(self): """ disk = {} options = "--details" - result = virsh.domblklist(self.name, options, ignore_status=True, - uri=self.connect_uri) + result = virsh.domblklist( + self.name, options, ignore_status=True, uri=self.connect_uri + ) blklist = result.stdout_text.strip().splitlines() if result.exit_status != 0: LOG.info("Get vm devices failed.") else: blklist = blklist[2:] linesplit = blklist[0].split(None, 4) - disk = {'type': linesplit[0], - 'device': linesplit[1], - 'target': linesplit[2], - 'source': linesplit[3]} + disk = { + "type": linesplit[0], + "device": linesplit[1], + "target": linesplit[2], + "source": linesplit[3], + } return disk def get_device_details(self, device_target): device_details = {} - result = virsh.domblkinfo(self.name, device_target, - uri=self.connect_uri) + result = virsh.domblkinfo(self.name, device_target, uri=self.connect_uri) details = result.stdout_text.strip().splitlines() if result.exit_status != 0: LOG.info("Get vm device details failed.") @@ -2831,7 +2999,7 @@ def get_max_mem(self): Get vm's maximum memory(kilobytes). """ dominfo_dict = self.dominfo() - max_mem = dominfo_dict['Max memory'].split(' ')[0] # strip off 'kb' + max_mem = dominfo_dict["Max memory"].split(" ")[0] # strip off 'kb' return int(max_mem) def domjobabort(self): @@ -2850,18 +3018,21 @@ def dump(self, path, option=""): :raise: exceptions.TestFail if dump fail. """ - cmd_result = virsh.dump(self.name, path=path, option=option, - uri=self.connect_uri) + cmd_result = virsh.dump( + self.name, path=path, option=option, uri=self.connect_uri + ) if cmd_result.exit_status: - raise exceptions.TestFail("Failed to dump %s to %s.\n" - "Detail: %s." % (self.name, path, cmd_result)) + raise exceptions.TestFail( + "Failed to dump %s to %s.\n" + "Detail: %s." % (self.name, path, cmd_result) + ) def get_job_type(self): jobresult = virsh.domjobinfo(self.name, uri=self.connect_uri) if not jobresult.exit_status: for line in jobresult.stdout_text.splitlines(): - key = line.split(':')[0] - value = line.split(':')[-1] + key = line.split(":")[0] + value = line.split(":")[-1] if key.count("type"): return value.strip() else: @@ -2915,9 +3086,9 @@ def get_interfaces(self): session.close() interfaces = [] for line in lines.splitlines(): - if len(line.split(':')) != 2: + if len(line.split(":")) != 2: continue - interfaces.append(line.split(':')[0].strip()) + interfaces.append(line.split(":")[0].strip()) return interfaces def get_interface_mac(self, interface): @@ -2937,8 +3108,7 @@ def get_interface_mac(self, interface): session.close() return mac.strip() - def install_package(self, name, ignore_status=False, timeout=300, - serial=False): + def install_package(self, name, ignore_status=False, timeout=300, serial=False): """ Install a package on VM. ToDo: Support multiple package manager. @@ -2946,16 +3116,15 @@ def install_package(self, name, ignore_status=False, timeout=300, :param name: Name of package to be installed :param serial: Whether to use a serial connection """ - session = self.wait_for_login() if not serial \ - else self.wait_for_serial_login() + session = self.wait_for_login() if not serial else self.wait_for_serial_login() try: if not utils_package.package_install(name, session, timeout=timeout): - raise virt_vm.VMError("Installation of package %s failed" % - name) + raise virt_vm.VMError("Installation of package %s failed" % name) except Exception as exception_detail: if ignore_status: - LOG.error("When install: %s\nError happened: %s\n", - name, exception_detail) + LOG.error( + "When install: %s\nError happened: %s\n", name, exception_detail + ) else: raise exception_detail finally: @@ -2969,8 +3138,7 @@ def remove_package(self, name, ignore_status=False, serial=False): :param name: Name of package to be removed :param serial: Whether to use a serial connection """ - session = self.wait_for_login() if not serial \ - else self.wait_for_serial_login() + session = self.wait_for_login() if not serial else self.wait_for_serial_login() if not utils_package.package_remove(name, session): if not ignore_status: session.close() @@ -2978,9 +3146,16 @@ def remove_package(self, name, ignore_status=False, serial=False): LOG.error("Removal of package %s failed", name) session.close() - def prepare_guest_agent(self, prepare_xml=True, channel=True, start=True, - source_path=None, target_name='org.qemu.guest_agent.0', - with_pm_utils=False, serial=False): + def prepare_guest_agent( + self, + prepare_xml=True, + channel=True, + start=True, + source_path=None, + target_name="org.qemu.guest_agent.0", + with_pm_utils=False, + serial=False, + ): """ Prepare qemu guest agent on the VM. @@ -2998,25 +3173,25 @@ def prepare_guest_agent(self, prepare_xml=True, channel=True, start=True, if self.is_alive(): self.destroy() vmxml = libvirt_xml.VMXML.new_from_inactive_dumpxml( - self.name, virsh_instance=virsh_inst) + self.name, virsh_instance=virsh_inst + ) # Check if we need to change XML of VM by checking # whether the agent is existing. is_existing = False ga_channels = vmxml.get_agent_channels() for chnl in ga_channels: - name = chnl.find('./target').get('name') + name = chnl.find("./target").get("name") try: - path = chnl.find('./source').get('path') + path = chnl.find("./source").get("path") except AttributeError: path = None - if (name == target_name and path == source_path): + if name == target_name and path == source_path: is_existing = True break if channel != is_existing: if channel: - vmxml.set_agent_channel(src_path=source_path, - tgt_name=target_name) + vmxml.set_agent_channel(src_path=source_path, tgt_name=target_name) else: vmxml.remove_agent_channels() vmxml.sync(virsh_instance=virsh_inst) @@ -3025,8 +3200,8 @@ def prepare_guest_agent(self, prepare_xml=True, channel=True, start=True, self.start() if with_pm_utils: - self.install_package('pm-utils', ignore_status=True, timeout=15) - self.install_package('qemu-guest-agent', serial=serial) + self.install_package("pm-utils", ignore_status=True, timeout=15) + self.install_package("qemu-guest-agent", serial=serial) self.set_state_guest_agent(start, serial=serial) @@ -3038,14 +3213,13 @@ def set_state_guest_agent(self, start, serial=False): :param serial: Whether to use a serial connection """ - session = self.wait_for_login() if not serial \ - else self.wait_for_serial_login() + session = self.wait_for_login() if not serial else self.wait_for_serial_login() def _is_ga_running(): - return (not session.cmd_status("pgrep qemu-ga")) + return not session.cmd_status("pgrep qemu-ga") def _is_ga_finished(): - return (session.cmd_status("pgrep qemu-ga") == 1) + return session.cmd_status("pgrep qemu-ga") == 1 def _start_ga(): if not _is_ga_running(): @@ -3055,15 +3229,16 @@ def _start_ga(): # filesystem due to the guest being destroyed and cause service # masked, so need to reinstall agent to fix it if status and "is masked" in output: - self.remove_package('qemu-guest-agent') - self.install_package('qemu-guest-agent') + self.remove_package("qemu-guest-agent") + self.install_package("qemu-guest-agent") status, output = session.cmd_status_output(cmd) if status and "unrecognized service" in output: cmd = "service qemu-ga start" status, output = session.cmd_status_output(cmd) if status: - raise virt_vm.VMError("Start qemu-guest-agent failed:" - "\n%s" % output) + raise virt_vm.VMError( + "Start qemu-guest-agent failed:" "\n%s" % output + ) def _stop_ga(): if _is_ga_running(): @@ -3073,8 +3248,9 @@ def _stop_ga(): cmd = "service qemu-ga stop" status, output = session.cmd_status_output(cmd) if status: - raise virt_vm.VMError("Stop qemu-guest-agent failed:" - "\n%s" % output) + raise virt_vm.VMError( + "Stop qemu-guest-agent failed:" "\n%s" % output + ) try: # Start/stop qemu-guest-agent @@ -3098,7 +3274,7 @@ def getenforce(self): :return: SELinux mode [Enforcing|Permissive|Disabled] """ - self.install_package('libselinux-utils') + self.install_package("libselinux-utils") session = self.wait_for_login() try: status, output = session.cmd_status_output("getenforce") @@ -3117,32 +3293,31 @@ def setenforce(self, mode): # SELinux is not supported by Ubuntu by default selinux_force = self.params.get("selinux_force", "no") == "yes" vm_distro = self.get_distro() - if vm_distro.lower() == 'ubuntu' and not selinux_force: + if vm_distro.lower() == "ubuntu" and not selinux_force: LOG.warning("Ubuntu doesn't support selinux by default") return - self.install_package('selinux-policy') - self.install_package('selinux-policy-targeted') - self.install_package('libselinux-utils') + self.install_package("selinux-policy") + self.install_package("selinux-policy-targeted") + self.install_package("libselinux-utils") try: if int(mode) == 1: - target_mode = 'Enforcing' + target_mode = "Enforcing" elif int(mode) == 0: - target_mode = 'Permissive' + target_mode = "Permissive" except ValueError: pass session = self.wait_for_login() try: current_mode = self.getenforce() - if current_mode == 'Disabled': + if current_mode == "Disabled": LOG.warning("VM SELinux disabled. Can't set mode.") return elif current_mode != target_mode: cmd = "setenforce %s" % mode status, output = session.cmd_status_output(cmd) if status != 0: - raise virt_vm.VMError( - "Set SELinux mode failed:\n%s" % output) + raise virt_vm.VMError("Set SELinux mode failed:\n%s" % output) else: LOG.debug("VM SELinux mode don't need change.") finally: diff --git a/virttest/libvirt_xml/accessors.py b/virttest/libvirt_xml/accessors.py index 30b4431de3..253ef68c33 100644 --- a/virttest/libvirt_xml/accessors.py +++ b/virttest/libvirt_xml/accessors.py @@ -26,8 +26,9 @@ def type_check(name, thing, expected): it_is = isinstance(thing, e) if it_is: return - raise ValueError('%s value is not any of %s, it is a %s' - % (name, expected, is_a_name)) + raise ValueError( + "%s value is not any of %s, it is a %s" % (name, expected, is_a_name) + ) def add_to_slots(*args): @@ -35,7 +36,7 @@ def add_to_slots(*args): Return list of AccessorBase.__all_slots__ + args """ for slot in args: - type_check('slot name', slot, str) + type_check("slot name", slot, str) return AccessorBase.__all_slots__ + args @@ -46,7 +47,7 @@ class AccessorBase(PropCanBase): """ # Gets AccessorGeneratorBase subclass's required_accessor_data_keys added - __slots__ = ('operation', 'property_name', 'libvirtxml') + __slots__ = ("operation", "property_name", "libvirtxml") def __init__(self, operation, property_name, libvirtxml, **dargs): """ @@ -57,33 +58,35 @@ def __init__(self, operation, property_name, libvirtxml, **dargs): :param libvirtxml: An instance of a LibvirtXMLBase subclass :param dargs: Necessary for subclasses to extend required parameters """ - type_check('Parameter property_name', property_name, str) - type_check('Operation attribute', operation, str) - type_check('__slots__ attribute', self.__all_slots__, [tuple, list]) - type_check('Parameter libvirtxml', libvirtxml, base.LibvirtXMLBase) + type_check("Parameter property_name", property_name, str) + type_check("Operation attribute", operation, str) + type_check("__slots__ attribute", self.__all_slots__, [tuple, list]) + type_check("Parameter libvirtxml", libvirtxml, base.LibvirtXMLBase) super(AccessorBase, self).__init__() - self.__dict_set__('operation', operation) - self.__dict_set__('property_name', property_name) - self.__dict_set__('libvirtxml', libvirtxml) + self.__dict_set__("operation", operation) + self.__dict_set__("property_name", property_name) + self.__dict_set__("libvirtxml", libvirtxml) for slot in self.__all_slots__: if slot in AccessorBase.__all_slots__: continue # already checked these # Don't care about value type if slot not in dargs: - raise ValueError('Required accessor generator parameter %s' - % slot) + raise ValueError("Required accessor generator parameter %s" % slot) self.__dict_set__(slot, dargs[slot]) # Subclass expected to override this and specify parameters __call__ = NotImplementedError def __repr__(self): - return ("%s's %s for %s with %s" - % (self.libvirtxml.__class__.__name__, self.operation, - self.property_name, str(dict(self)))) + return "%s's %s for %s with %s" % ( + self.libvirtxml.__class__.__name__, + self.operation, + self.property_name, + str(dict(self)), + ) def xmltreefile(self): """ @@ -91,8 +94,7 @@ def xmltreefile(self): """ return self.libvirtxml.xmltreefile - def element_by_parent(self, parent_xpath, tag_name, create=True, - nested=False): + def element_by_parent(self, parent_xpath, tag_name, create=True, nested=False): """ Retrieve/create an element instance at parent_xpath/tag_name @@ -103,17 +105,26 @@ def element_by_parent(self, parent_xpath, tag_name, create=True, :return: ElementTree.Element instance :raise: LibvirtXMLError: If element not exist & create=False """ - type_check('parent_xpath', parent_xpath, str) - type_check('tag_name', tag_name, str) + type_check("parent_xpath", parent_xpath, str) + type_check("tag_name", tag_name, str) parent_element = self.xmltreefile().find(parent_xpath) - if (parent_element == self.xmltreefile().getroot() and - parent_element.tag == tag_name) and nested is False: + if ( + parent_element == self.xmltreefile().getroot() + and parent_element.tag == tag_name + ) and nested is False: return parent_element - excpt_str = ('Exception thrown from %s for property "%s" while' - ' looking for element tag "%s", on parent at xpath' - ' "%s", in XML\n%s\n' % (self.operation, - self.property_name, tag_name, parent_xpath, - str(self.xmltreefile()))) + excpt_str = ( + 'Exception thrown from %s for property "%s" while' + ' looking for element tag "%s", on parent at xpath' + ' "%s", in XML\n%s\n' + % ( + self.operation, + self.property_name, + tag_name, + parent_xpath, + str(self.xmltreefile()), + ) + ) if parent_element is None: if create: # This will only work for simple XPath strings @@ -129,17 +140,21 @@ def element_by_parent(self, parent_xpath, tag_name, create=True, raise if element is None: if create: # Create the element - element = xml_utils.ElementTree.SubElement(parent_element, - tag_name) + element = xml_utils.ElementTree.SubElement(parent_element, tag_name) else: # create is False - raise xcepts.LibvirtXMLNotFoundError('Error in %s for property ' - '"%s", element tag "%s" not ' - 'found on parent at xpath "%s"' - ' in XML\n%s\n' - % (self.operation, - self.property_name, - tag_name, parent_xpath, - str(self.xmltreefile()))) + raise xcepts.LibvirtXMLNotFoundError( + "Error in %s for property " + '"%s", element tag "%s" not ' + 'found on parent at xpath "%s"' + " in XML\n%s\n" + % ( + self.operation, + self.property_name, + tag_name, + parent_xpath, + str(self.xmltreefile()), + ) + ) return element @@ -153,18 +168,16 @@ class ForbiddenBase(AccessorBase): def __call__(self, value=None): if value: - raise xcepts.LibvirtXMLForbiddenError("%s %s to '%s' on %s " - "forbidden" - % (self.operation, - self.property_name, - str(value), - str(self))) + raise xcepts.LibvirtXMLForbiddenError( + "%s %s to '%s' on %s " + "forbidden" + % (self.operation, self.property_name, str(value), str(self)) + ) else: - raise xcepts.LibvirtXMLForbiddenError("%s %s on %s " - "forbidden" - % (self.operation, - self.property_name, - str(self))) + raise xcepts.LibvirtXMLForbiddenError( + "%s %s on %s " + "forbidden" % (self.operation, self.property_name, str(self)) + ) class AccessorGeneratorBase(object): @@ -184,19 +197,19 @@ def __init__(self, property_name, libvirtxml, forbidden=None, **dargs): """ if forbidden is None: forbidden = [] - type_check('forbidden', forbidden, list) + type_check("forbidden", forbidden, list) self.forbidden = forbidden - type_check('libvirtxml', libvirtxml, base.LibvirtXMLBase) + type_check("libvirtxml", libvirtxml, base.LibvirtXMLBase) self.libvirtxml = libvirtxml - type_check('property_name', property_name, str) + type_check("property_name", property_name, str) self.property_name = property_name self.dargs = dargs # Lookup all property names possibly needing accessors - for operation in ('get', 'set', 'del'): + for operation in ("get", "set", "del"): self.set_if_not_defined(operation) def set_if_not_defined(self, operation): @@ -221,7 +234,7 @@ def callable_name(operation): """ Return class name for operation (i.e. 'Getter'), defined by subclass. """ - return operation.capitalize() + 'ter' + return operation.capitalize() + "ter" def make_callable(self, operation): """ @@ -229,8 +242,11 @@ def make_callable(self, operation): """ callable_class = getattr(self, self.callable_name(operation)) return callable_class( - self.callable_name(operation), self.property_name, - self.libvirtxml, **self.dargs) + self.callable_name(operation), + self.property_name, + self.libvirtxml, + **self.dargs + ) def make_forbidden(self, operation): """ @@ -242,8 +258,7 @@ def assign_callable(self, operation, callable_inst): """ Set reference on objectified libvirtxml instance to callable_inst """ - self.libvirtxml.__super_set__(self.accessor_name(operation), - callable_inst) + self.libvirtxml.__super_set__(self.accessor_name(operation), callable_inst) # Implementation of specific accessor generator subclasses follows @@ -262,9 +277,11 @@ def __init__(self, property_name, libvirtxml): :param property_name: String name of property (for exception detail) :param libvirtxml: An instance of a LibvirtXMLBase subclass """ - super(AllForbidden, self).__init__(property_name=property_name, - libvirtxml=libvirtxml, - forbidden=['get', 'set', 'del']) + super(AllForbidden, self).__init__( + property_name=property_name, + libvirtxml=libvirtxml, + forbidden=["get", "set", "del"], + ) class XMLElementText(AccessorGeneratorBase): @@ -273,10 +290,16 @@ class XMLElementText(AccessorGeneratorBase): Class of accessor classes operating on element.text """ - required_dargs = ('parent_xpath', 'tag_name') + required_dargs = ("parent_xpath", "tag_name") - def __init__(self, property_name, libvirtxml, forbidden=None, - parent_xpath=None, tag_name=None): + def __init__( + self, + property_name, + libvirtxml, + forbidden=None, + parent_xpath=None, + tag_name=None, + ): """ Create undefined accessors on libvirt instance @@ -286,10 +309,13 @@ def __init__(self, property_name, libvirtxml, forbidden=None, :param parent_xpath: XPath string of parent element :param tag_name: element tag name to manipulate text attribute on. """ - super(XMLElementText, self).__init__(property_name, libvirtxml, - forbidden, - parent_xpath=parent_xpath, - tag_name=tag_name) + super(XMLElementText, self).__init__( + property_name, + libvirtxml, + forbidden, + parent_xpath=parent_xpath, + tag_name=tag_name, + ) class Getter(AccessorBase): @@ -297,11 +323,12 @@ class Getter(AccessorBase): Retrieve text on element """ - __slots__ = add_to_slots('parent_xpath', 'tag_name') + __slots__ = add_to_slots("parent_xpath", "tag_name") def __call__(self): - return self.element_by_parent(self.parent_xpath, - self.tag_name, create=False).text + return self.element_by_parent( + self.parent_xpath, self.tag_name, create=False + ).text class Setter(AccessorBase): @@ -309,11 +336,12 @@ class Setter(AccessorBase): Set text to value on element """ - __slots__ = add_to_slots('parent_xpath', 'tag_name') + __slots__ = add_to_slots("parent_xpath", "tag_name") def __call__(self, value): - element = self.element_by_parent(self.parent_xpath, - self.tag_name, create=True) + element = self.element_by_parent( + self.parent_xpath, self.tag_name, create=True + ) element.text = str(value) self.xmltreefile().write() @@ -323,14 +351,17 @@ class Delter(AccessorBase): Remove element and ignore if it doesn't exist (same as False) """ - __slots__ = add_to_slots('parent_xpath', 'tag_name') + __slots__ = add_to_slots("parent_xpath", "tag_name") def __call__(self): try: - element = self.element_by_parent(self.parent_xpath, - self.tag_name, create=False) - except (xcepts.LibvirtXMLNotFoundError, # element doesn't exist - xcepts.LibvirtXMLAccessorError): # parent doesn't exist + element = self.element_by_parent( + self.parent_xpath, self.tag_name, create=False + ) + except ( + xcepts.LibvirtXMLNotFoundError, # element doesn't exist + xcepts.LibvirtXMLAccessorError, + ): # parent doesn't exist pass # already gone else: parent = self.xmltreefile().find(self.parent_xpath) @@ -344,16 +375,20 @@ class XMLElementInt(AccessorGeneratorBase): """ Class of accessor classes operating on element.text as an integer """ - __radix2func_dict__ = {0: int, - 2: bin, - 8: oct, - 10: int, - 16: hex} - required_dargs = ('parent_xpath', 'tag_name', 'radix') + __radix2func_dict__ = {0: int, 2: bin, 8: oct, 10: int, 16: hex} - def __init__(self, property_name, libvirtxml, forbidden=None, - parent_xpath=None, tag_name=None, radix=10): + required_dargs = ("parent_xpath", "tag_name", "radix") + + def __init__( + self, + property_name, + libvirtxml, + forbidden=None, + parent_xpath=None, + tag_name=None, + radix=10, + ): """ Create undefined accessors on libvirt instance @@ -366,13 +401,17 @@ def __init__(self, property_name, libvirtxml, forbidden=None, try: self.__radix2func_dict__[radix] except KeyError: - raise xcepts.LibvirtXMLError("Param radix=%s for XMLElementInt " - "is not accepted." % radix) - super(XMLElementInt, self).__init__(property_name, libvirtxml, - forbidden, - parent_xpath=parent_xpath, - tag_name=tag_name, - radix=radix) + raise xcepts.LibvirtXMLError( + "Param radix=%s for XMLElementInt " "is not accepted." % radix + ) + super(XMLElementInt, self).__init__( + property_name, + libvirtxml, + forbidden, + parent_xpath=parent_xpath, + tag_name=tag_name, + radix=radix, + ) class Getter(AccessorBase): @@ -380,17 +419,19 @@ class Getter(AccessorBase): Retrieve text on element and convert to int """ - __slots__ = add_to_slots('parent_xpath', 'tag_name', 'radix') + __slots__ = add_to_slots("parent_xpath", "tag_name", "radix") def __call__(self): - element = self.element_by_parent(self.parent_xpath, - self.tag_name, create=False) + element = self.element_by_parent( + self.parent_xpath, self.tag_name, create=False + ) try: result = int(element.text, self.radix) except ValueError: - raise xcepts.LibvirtXMLError("Value of %s in %s is %s," - "not a Integer." % (self.tag_name, - self.parent_xpath, element.text)) + raise xcepts.LibvirtXMLError( + "Value of %s in %s is %s," + "not a Integer." % (self.tag_name, self.parent_xpath, element.text) + ) return result class Setter(AccessorBase): @@ -399,12 +440,13 @@ class Setter(AccessorBase): Set text on element after converting to int then to str """ - __slots__ = add_to_slots('parent_xpath', 'tag_name', 'radix') + __slots__ = add_to_slots("parent_xpath", "tag_name", "radix") def __call__(self, value): - type_check(self.property_name + ' value', value, int) - element = self.element_by_parent(self.parent_xpath, - self.tag_name, create=True) + type_check(self.property_name + " value", value, int) + element = self.element_by_parent( + self.parent_xpath, self.tag_name, create=True + ) convertFunc = XMLElementInt.__radix2func_dict__[self.radix] element.text = str(convertFunc(value)) self.xmltreefile().write() @@ -418,10 +460,16 @@ class XMLElementBool(AccessorGeneratorBase): Class of accessor classes operating purely element existence """ - required_dargs = ('parent_xpath', 'tag_name') + required_dargs = ("parent_xpath", "tag_name") - def __init__(self, property_name, libvirtxml, forbidden=None, - parent_xpath=None, tag_name=None): + def __init__( + self, + property_name, + libvirtxml, + forbidden=None, + parent_xpath=None, + tag_name=None, + ): """ Create undefined accessors on libvirt instance @@ -431,10 +479,13 @@ def __init__(self, property_name, libvirtxml, forbidden=None, :param parent_xpath: XPath string of parent element :param tag_name: element tag name to manipulate text attribute on. """ - super(XMLElementBool, self).__init__(property_name, libvirtxml, - forbidden, - parent_xpath=parent_xpath, - tag_name=tag_name) + super(XMLElementBool, self).__init__( + property_name, + libvirtxml, + forbidden, + parent_xpath=parent_xpath, + tag_name=tag_name, + ) class Getter(AccessorBase): @@ -442,16 +493,14 @@ class Getter(AccessorBase): Retrieve text on element """ - __slots__ = add_to_slots('parent_xpath', 'tag_name') + __slots__ = add_to_slots("parent_xpath", "tag_name") def __call__(self): try: # Throws exception if parent path or element not exist - self.element_by_parent(self.parent_xpath, self.tag_name, - create=False) + self.element_by_parent(self.parent_xpath, self.tag_name, create=False) return True - except (xcepts.LibvirtXMLAccessorError, - xcepts.LibvirtXMLNotFoundError): + except (xcepts.LibvirtXMLAccessorError, xcepts.LibvirtXMLNotFoundError): return False class Setter(AccessorBase): @@ -460,12 +509,11 @@ class Setter(AccessorBase): Create element when True, delete when false """ - __slots__ = add_to_slots('parent_xpath', 'tag_name') + __slots__ = add_to_slots("parent_xpath", "tag_name") def __call__(self, value): if bool(value) is True: - self.element_by_parent(self.parent_xpath, self.tag_name, - create=True) + self.element_by_parent(self.parent_xpath, self.tag_name, create=True) else: delattr(self.libvirtxml, self.property_name) self.xmltreefile().write() @@ -479,8 +527,15 @@ class XMLAttribute(AccessorGeneratorBase): Class of accessor classes operating on an attribute of an element """ - def __init__(self, property_name, libvirtxml, forbidden=None, - parent_xpath=None, tag_name=None, attribute=None): + def __init__( + self, + property_name, + libvirtxml, + forbidden=None, + parent_xpath=None, + tag_name=None, + attribute=None, + ): """ Create undefined accessors on libvirt instance @@ -491,9 +546,14 @@ def __init__(self, property_name, libvirtxml, forbidden=None, :param tag_name: element tag name to manipulate text attribute on. :param attribute: Attribute name to manipulate """ - super(XMLAttribute, self).__init__(property_name, libvirtxml, - forbidden, parent_xpath=parent_xpath, - tag_name=tag_name, attribute=attribute) + super(XMLAttribute, self).__init__( + property_name, + libvirtxml, + forbidden, + parent_xpath=parent_xpath, + tag_name=tag_name, + attribute=attribute, + ) class Getter(AccessorBase): @@ -501,17 +561,18 @@ class Getter(AccessorBase): Get attribute value """ - __slots__ = add_to_slots('parent_xpath', 'tag_name', 'attribute') + __slots__ = add_to_slots("parent_xpath", "tag_name", "attribute") def __call__(self): - element = self.element_by_parent(self.parent_xpath, - self.tag_name, create=False) + element = self.element_by_parent( + self.parent_xpath, self.tag_name, create=False + ) value = element.get(self.attribute, None) if value is None: - raise xcepts.LibvirtXMLNotFoundError("Attribute %s not found " - "on element %s" - % (self.attribute, - element.tag)) + raise xcepts.LibvirtXMLNotFoundError( + "Attribute %s not found " + "on element %s" % (self.attribute, element.tag) + ) return value class Setter(AccessorBase): @@ -520,11 +581,12 @@ class Setter(AccessorBase): Set attribute value """ - __slots__ = add_to_slots('parent_xpath', 'tag_name', 'attribute') + __slots__ = add_to_slots("parent_xpath", "tag_name", "attribute") def __call__(self, value): - element = self.element_by_parent(self.parent_xpath, - self.tag_name, create=True) + element = self.element_by_parent( + self.parent_xpath, self.tag_name, create=True + ) element.set(self.attribute, str(value)) self.xmltreefile().write() @@ -534,11 +596,12 @@ class Delter(AccessorBase): Remove attribute """ - __slots__ = add_to_slots('parent_xpath', 'tag_name', 'attribute') + __slots__ = add_to_slots("parent_xpath", "tag_name", "attribute") def __call__(self): - element = self.element_by_parent(self.parent_xpath, - self.tag_name, create=False) + element = self.element_by_parent( + self.parent_xpath, self.tag_name, create=False + ) try: del element.attrib[self.attribute] except KeyError: @@ -552,8 +615,14 @@ class XMLElementDict(AccessorGeneratorBase): Class of accessor classes operating as a dictionary of attributes """ - def __init__(self, property_name, libvirtxml, forbidden=None, - parent_xpath=None, tag_name=None): + def __init__( + self, + property_name, + libvirtxml, + forbidden=None, + parent_xpath=None, + tag_name=None, + ): """ Create undefined accessors on libvirt instance @@ -563,10 +632,13 @@ def __init__(self, property_name, libvirtxml, forbidden=None, :param parent_xpath: XPath string of parent element :param tag_name: element tag name to manipulate text attribute on. """ - super(XMLElementDict, self).__init__(property_name, libvirtxml, - forbidden, - parent_xpath=parent_xpath, - tag_name=tag_name) + super(XMLElementDict, self).__init__( + property_name, + libvirtxml, + forbidden, + parent_xpath=parent_xpath, + tag_name=tag_name, + ) class Getter(AccessorBase): @@ -574,11 +646,12 @@ class Getter(AccessorBase): Retrieve attributes on element """ - __slots__ = add_to_slots('parent_xpath', 'tag_name') + __slots__ = add_to_slots("parent_xpath", "tag_name") def __call__(self): - element = self.element_by_parent(self.parent_xpath, - self.tag_name, create=False) + element = self.element_by_parent( + self.parent_xpath, self.tag_name, create=False + ) return dict(list(element.items())) class Setter(AccessorBase): @@ -587,12 +660,13 @@ class Setter(AccessorBase): Set attributes to value on element """ - __slots__ = add_to_slots('parent_xpath', 'tag_name') + __slots__ = add_to_slots("parent_xpath", "tag_name") def __call__(self, value): - type_check(self.property_name + ' value', value, dict) - element = self.element_by_parent(self.parent_xpath, - self.tag_name, create=True) + type_check(self.property_name + " value", value, dict) + element = self.element_by_parent( + self.parent_xpath, self.tag_name, create=True + ) for attr_key, attr_value in list(value.items()): element.set(str(attr_key), str(attr_value)) self.xmltreefile().write() @@ -607,11 +681,18 @@ class XMLElementNest(AccessorGeneratorBase): Class of accessor classes operating on a LibvirtXMLBase subclass """ - required_dargs = ('parent_xpath', 'tag_name', 'subclass', 'subclass_dargs') + required_dargs = ("parent_xpath", "tag_name", "subclass", "subclass_dargs") - def __init__(self, property_name, libvirtxml, forbidden=None, - parent_xpath=None, tag_name=None, subclass=None, - subclass_dargs=None): + def __init__( + self, + property_name, + libvirtxml, + forbidden=None, + parent_xpath=None, + tag_name=None, + subclass=None, + subclass_dargs=None, + ): """ Create undefined accessors on libvirt instance @@ -625,14 +706,17 @@ def __init__(self, property_name, libvirtxml, forbidden=None, N/B: Works ONLY if tag_name is unique within parent element """ - type_check('subclass', subclass, base.LibvirtXMLBase) - type_check('subclass_dargs', subclass_dargs, dict) - super(XMLElementNest, self).__init__(property_name, libvirtxml, - forbidden, - parent_xpath=parent_xpath, - tag_name=tag_name, - subclass=subclass, - subclass_dargs=subclass_dargs) + type_check("subclass", subclass, base.LibvirtXMLBase) + type_check("subclass_dargs", subclass_dargs, dict) + super(XMLElementNest, self).__init__( + property_name, + libvirtxml, + forbidden, + parent_xpath=parent_xpath, + tag_name=tag_name, + subclass=subclass, + subclass_dargs=subclass_dargs, + ) class Getter(AccessorBase): @@ -640,16 +724,16 @@ class Getter(AccessorBase): Retrieve instance of subclass with it's xml set to rerooted xpath/tag """ - __slots__ = add_to_slots('parent_xpath', 'tag_name', 'subclass', - 'subclass_dargs') + __slots__ = add_to_slots( + "parent_xpath", "tag_name", "subclass", "subclass_dargs" + ) def __call__(self): xmltreefile = self.xmltreefile() # Don't re-invent XPath generation method/behavior - nested_root_element = self.element_by_parent(self.parent_xpath, - self.tag_name, - create=False, - nested=True) + nested_root_element = self.element_by_parent( + self.parent_xpath, self.tag_name, create=False, nested=True + ) nested_root_xpath = xmltreefile.get_xpath(nested_root_element) # Try to make XMLTreeFile copy, rooted at nested_root_xpath # with copies of any/all child elements also @@ -667,17 +751,14 @@ class Setter(AccessorBase): Set attributes to value on element """ - __slots__ = add_to_slots('parent_xpath', 'tag_name', 'subclass') + __slots__ = add_to_slots("parent_xpath", "tag_name", "subclass") def __call__(self, value): - type_check('Instance of %s' % self.subclass.__name__, - value, - self.subclass) + type_check("Instance of %s" % self.subclass.__name__, value, self.subclass) # Will overwrite if exists - existing_element = self.element_by_parent(self.parent_xpath, - self.tag_name, - create=True, - nested=True) + existing_element = self.element_by_parent( + self.parent_xpath, self.tag_name, create=True, nested=True + ) existing_parent = self.xmltreefile().get_parent(existing_element) self.xmltreefile().remove(existing_element) existing_parent.append(value.xmltreefile.getroot()) @@ -699,11 +780,18 @@ class XMLElementList(AccessorGeneratorBase): the conversion to/from the format described in __init__. """ - required_dargs = ('parent_xpath', 'tag_name', 'marshal_from', 'marshal_to') + required_dargs = ("parent_xpath", "tag_name", "marshal_from", "marshal_to") - def __init__(self, property_name, libvirtxml, forbidden=None, - parent_xpath=None, marshal_from=None, marshal_to=None, - has_subclass=None): + def __init__( + self, + property_name, + libvirtxml, + forbidden=None, + parent_xpath=None, + marshal_from=None, + marshal_to=None, + has_subclass=None, + ): """ Create undefined accessors on libvirt instance @@ -720,14 +808,16 @@ def __init__(self, property_name, libvirtxml, forbidden=None, item value accepted by marshal_from or None to skip """ if not callable(marshal_from) or not callable(marshal_to): - raise ValueError("Both marshal_from and marshal_to must be " - "callable") - super(XMLElementList, self).__init__(property_name, libvirtxml, - forbidden, - parent_xpath=parent_xpath, - marshal_from=marshal_from, - marshal_to=marshal_to, - has_subclass=has_subclass) + raise ValueError("Both marshal_from and marshal_to must be " "callable") + super(XMLElementList, self).__init__( + property_name, + libvirtxml, + forbidden, + parent_xpath=parent_xpath, + marshal_from=marshal_from, + marshal_to=marshal_to, + has_subclass=has_subclass, + ) class Getter(AccessorBase): @@ -735,7 +825,7 @@ class Getter(AccessorBase): Retrieve list of values as returned by the marshal_to callable """ - __slots__ = add_to_slots('parent_xpath', 'marshal_to', 'has_subclass') + __slots__ = add_to_slots("parent_xpath", "marshal_to", "has_subclass") def __call__(self): # Parent structure cannot be pre-determined as in other classes @@ -760,12 +850,15 @@ def __call__(self): # xml objects, first create xmltreefile for new object if self.has_subclass: new_xmltreefile = xml_utils.XMLTreeFile( - tostring(child, encoding='unicode')) - item = self.marshal_to(child.tag, new_xmltreefile, - index, self.libvirtxml) + tostring(child, encoding="unicode") + ) + item = self.marshal_to( + child.tag, new_xmltreefile, index, self.libvirtxml + ) else: - item = self.marshal_to(child.tag, dict(list(child.items())), - index, self.libvirtxml) + item = self.marshal_to( + child.tag, dict(list(child.items())), index, self.libvirtxml + ) if item is not None: result.append(item) # Always use absolute index (even if item was None) @@ -778,10 +871,10 @@ class Setter(AccessorBase): Set child elements as returned by the marshal_to callable """ - __slots__ = add_to_slots('parent_xpath', 'marshal_from', 'has_subclass') + __slots__ = add_to_slots("parent_xpath", "marshal_from", "has_subclass") def __call__(self, value): - type_check('value', value, list) + type_check("value", value, list) # Allow other classes to generate parent structure parent = self.xmltreefile().find(self.parent_xpath) if parent is None: @@ -790,7 +883,8 @@ def __call__(self, value): parent = self.xmltreefile().find(self.parent_xpath) if parent is None: raise xcepts.LibvirtXMLNotFoundError( - 'Parent xpath %s not found.' % self.parent_xpath) + "Parent xpath %s not found." % self.parent_xpath + ) # Remove existing by calling accessor method, allowing # any "untouchable" or "filtered" elements (by marshal) # to be ignored and left as-is. @@ -803,18 +897,22 @@ def __call__(self, value): try: # Call user-defined conversion from simple # format, back to Element instances. - element_tuple = self.marshal_from(item, index, - self.libvirtxml) + element_tuple = self.marshal_from(item, index, self.libvirtxml) except ValueError: # Defined in marshal API, to help with error reporting # and debugging with more rich message. - msg = ("Call to %s by set accessor method for property %s " - "with unsupported item type %s, at index %d, " - " with value %s." % (str(self.marshal_from), - self.property_name, - str(type(item)), - index, - str(item))) + msg = ( + "Call to %s by set accessor method for property %s " + "with unsupported item type %s, at index %d, " + " with value %s." + % ( + str(self.marshal_from), + self.property_name, + str(type(item)), + index, + str(item), + ) + ) raise xcepts.LibvirtXMLAccessorError(msg) # Handle xml sub-element items @@ -823,9 +921,9 @@ def __call__(self, value): self.xmltreefile().write() continue - xml_utils.ElementTree.SubElement(parent, - element_tuple[0], - element_tuple[1]) + xml_utils.ElementTree.SubElement( + parent, element_tuple[0], element_tuple[1] + ) index += 1 self.xmltreefile().write() @@ -835,13 +933,14 @@ class Delter(AccessorBase): Remove ALL child elements for which marshal_to does NOT return None """ - __slots__ = add_to_slots('parent_xpath', 'marshal_to', 'has_subclass') + __slots__ = add_to_slots("parent_xpath", "marshal_to", "has_subclass") def __call__(self): parent = self.xmltreefile().find(self.parent_xpath) if parent is None: - raise xcepts.LibvirtXMLNotFoundError("Parent element %s not " - "found" % self.parent_xpath) + raise xcepts.LibvirtXMLNotFoundError( + "Parent element %s not " "found" % self.parent_xpath + ) # Don't delete while traversing list todel = [] index = 0 @@ -850,12 +949,15 @@ def __call__(self): # first create xmltreefile for new object if self.has_subclass: new_xmltreefile = xml_utils.XMLTreeFile( - tostring(child, encoding='unicode')) - item = self.marshal_to(child.tag, new_xmltreefile, - index, self.libvirtxml) + tostring(child, encoding="unicode") + ) + item = self.marshal_to( + child.tag, new_xmltreefile, index, self.libvirtxml + ) else: - item = self.marshal_to(child.tag, dict(list(child.items())), - index, self.libvirtxml) + item = self.marshal_to( + child.tag, dict(list(child.items())), index, self.libvirtxml + ) # Always use absolute index (even if item was None) index += 1 # Account for case where child elements are mixed in diff --git a/virttest/libvirt_xml/backup_xml.py b/virttest/libvirt_xml/backup_xml.py index 8f6de2a4e6..d2225183fe 100644 --- a/virttest/libvirt_xml/backup_xml.py +++ b/virttest/libvirt_xml/backup_xml.py @@ -50,25 +50,30 @@ class BackupXML(base.LibvirtXMLBase): dict, keys: transport, socket, name, port """ - __slots__ = ('mode', 'incremental', 'server', 'disks') + __slots__ = ("mode", "incremental", "server", "disks") __uncompareable__ = base.LibvirtXMLBase.__uncompareable__ __schema_name__ = "domainbackup" def __init__(self, virsh_instance=base.virsh): - accessors.XMLAttribute('mode', self, parent_xpath='/', - tag_name='domainbackup', attribute='mode') - accessors.XMLElementText('incremental', self, parent_xpath='/', - tag_name='incremental') - accessors.XMLElementDict('server', self, parent_xpath='/', - tag_name='server') - accessors.XMLElementList('disks', self, parent_xpath='/disks', - marshal_from=self.marshal_from_disks, - marshal_to=self.marshal_to_disks, - has_subclass=True) + accessors.XMLAttribute( + "mode", self, parent_xpath="/", tag_name="domainbackup", attribute="mode" + ) + accessors.XMLElementText( + "incremental", self, parent_xpath="/", tag_name="incremental" + ) + accessors.XMLElementDict("server", self, parent_xpath="/", tag_name="server") + accessors.XMLElementList( + "disks", + self, + parent_xpath="/disks", + marshal_from=self.marshal_from_disks, + marshal_to=self.marshal_to_disks, + has_subclass=True, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" @staticmethod def marshal_from_disks(item, index, libvirtxml): @@ -76,21 +81,22 @@ def marshal_from_disks(item, index, libvirtxml): Convert an xml object to disk tag and xml element. """ if isinstance(item, BackupXML.DiskXML): - return 'disk', item + return "disk", item elif isinstance(item, dict): disk = BackupXML.DiskXML() disk.setup_attrs(**item) - return 'disk', disk + return "disk", disk else: - raise xcepts.LibvirtXMLError("Expected a list of DiskXML " - "instances, not a %s" % str(item)) + raise xcepts.LibvirtXMLError( + "Expected a list of DiskXML " "instances, not a %s" % str(item) + ) @staticmethod def marshal_to_disks(tag, new_treefile, index, libvirtxml): """ Convert a disk tag xml element to an object of DiskXML. """ - if tag != 'disk': + if tag != "disk": return None newone = BackupXML.DiskXML(virsh_instance=libvirtxml.virsh) newone.xmltreefile = new_treefile @@ -126,37 +132,76 @@ class DiskXML(base.LibvirtXMLBase): libvirt_xml.backup_xml.DiskXML.DiskScratch instance """ - __slots__ = ('name', 'backup', 'exportname', 'exportbitmap', - 'type', 'backupmode', 'incremental', 'target', - 'driver', 'scratch') + __slots__ = ( + "name", + "backup", + "exportname", + "exportbitmap", + "type", + "backupmode", + "incremental", + "target", + "driver", + "scratch", + ) def __init__(self, virsh_instance=base.virsh): - accessors.XMLAttribute('name', self, parent_xpath='/', - tag_name='disk', attribute='name') - accessors.XMLAttribute('backup', self, parent_xpath='/', - tag_name='disk', attribute='backup') - accessors.XMLAttribute('exportname', self, parent_xpath='/', - tag_name='disk', attribute='exportname') - accessors.XMLAttribute('exportbitmap', self, parent_xpath='/', - tag_name='disk', attribute='exportbitmap') - accessors.XMLAttribute('type', self, parent_xpath='/', - tag_name='disk', attribute='type') - accessors.XMLAttribute('backupmode', self, parent_xpath='/', - tag_name='disk', attribute='backupmode') - accessors.XMLAttribute('incremental', self, parent_xpath='/', - tag_name='disk', attribute='incremental') - accessors.XMLElementNest('target', self, parent_xpath='/', - tag_name='target', - subclass=self.DiskTarget, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementDict('driver', self, parent_xpath='/', - tag_name='driver') - accessors.XMLElementNest('scratch', self, parent_xpath='/', - tag_name='scratch', - subclass=self.DiskScratch, - subclass_dargs={ - 'virsh_instance': virsh_instance}) + accessors.XMLAttribute( + "name", self, parent_xpath="/", tag_name="disk", attribute="name" + ) + accessors.XMLAttribute( + "backup", self, parent_xpath="/", tag_name="disk", attribute="backup" + ) + accessors.XMLAttribute( + "exportname", + self, + parent_xpath="/", + tag_name="disk", + attribute="exportname", + ) + accessors.XMLAttribute( + "exportbitmap", + self, + parent_xpath="/", + tag_name="disk", + attribute="exportbitmap", + ) + accessors.XMLAttribute( + "type", self, parent_xpath="/", tag_name="disk", attribute="type" + ) + accessors.XMLAttribute( + "backupmode", + self, + parent_xpath="/", + tag_name="disk", + attribute="backupmode", + ) + accessors.XMLAttribute( + "incremental", + self, + parent_xpath="/", + tag_name="disk", + attribute="incremental", + ) + accessors.XMLElementNest( + "target", + self, + parent_xpath="/", + tag_name="target", + subclass=self.DiskTarget, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementDict( + "driver", self, parent_xpath="/", tag_name="driver" + ) + accessors.XMLElementNest( + "scratch", + self, + parent_xpath="/", + tag_name="scratch", + subclass=self.DiskScratch, + subclass_dargs={"virsh_instance": virsh_instance}, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) self.xml = "" @@ -171,18 +216,22 @@ class DiskTarget(base.LibvirtXMLBase): encryption: libvirt_xml.backup_xml.Disk.DiskTarget.Encryption instances """ - __slots__ = ('attrs', 'encryption') + __slots__ = ("attrs", "encryption") def __init__(self, virsh_instance=base.virsh): - accessors.XMLElementDict('attrs', self, parent_xpath='/', - tag_name='target') - accessors.XMLElementNest('encryption', self, parent_xpath='/', - tag_name='encryption', - subclass=self.Encryption, - subclass_dargs={ - 'virsh_instance': virsh_instance}) + accessors.XMLElementDict( + "attrs", self, parent_xpath="/", tag_name="target" + ) + accessors.XMLElementNest( + "encryption", + self, + parent_xpath="/", + tag_name="encryption", + subclass=self.Encryption, + subclass_dargs={"virsh_instance": virsh_instance}, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" def new_encryption(self, **dargs): """ @@ -204,16 +253,21 @@ class Encryption(base.LibvirtXMLBase): dict, keys: type, usage, uuid """ - __slots__ = ('encryption', 'secret') + __slots__ = ("encryption", "secret") def __init__(self, virsh_instance=base.virsh): - accessors.XMLAttribute('encryption', self, parent_xpath='/', - tag_name='encryption', attribute='format') - accessors.XMLElementDict('secret', self, parent_xpath='/', - tag_name='secret') - super(self.__class__, self).__init__( - virsh_instance=virsh_instance) - self.xml = '' + accessors.XMLAttribute( + "encryption", + self, + parent_xpath="/", + tag_name="encryption", + attribute="format", + ) + accessors.XMLElementDict( + "secret", self, parent_xpath="/", tag_name="secret" + ) + super(self.__class__, self).__init__(virsh_instance=virsh_instance) + self.xml = "" class DiskScratch(DiskTarget): """ @@ -226,15 +280,19 @@ class DiskScratch(DiskTarget): encryption: libvirt_xml.backup_xml.Disk.DiskTarget.Encryption instances """ - __slots__ = ('attrs', 'encryption') + __slots__ = ("attrs", "encryption") def __init__(self, virsh_instance=base.virsh): - accessors.XMLElementDict('attrs', self, parent_xpath='/', - tag_name='scratch') - accessors.XMLElementNest('encryption', self, parent_xpath='/', - tag_name='encryption', - subclass=self.Encryption, - subclass_dargs={ - 'virsh_instance': virsh_instance}) + accessors.XMLElementDict( + "attrs", self, parent_xpath="/", tag_name="scratch" + ) + accessors.XMLElementNest( + "encryption", + self, + parent_xpath="/", + tag_name="encryption", + subclass=self.Encryption, + subclass_dargs={"virsh_instance": virsh_instance}, + ) base.LibvirtXMLBase.__init__(self, virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" diff --git a/virttest/libvirt_xml/base.py b/virttest/libvirt_xml/base.py index f4b83c249c..1c05b254da 100644 --- a/virttest/libvirt_xml/base.py +++ b/virttest/libvirt_xml/base.py @@ -9,6 +9,7 @@ # lazy imports for dependencies that are not needed in all modes of use from virttest._wrappers import lazy_import + virsh = lazy_import("virttest.virsh") @@ -40,7 +41,7 @@ class LibvirtXMLBase(propcan.PropCanBase): virtual boolean, read-only, True/False from virt-xml-validate """ - __slots__ = ('xml', 'virsh', 'xmltreefile', 'validates') + __slots__ = ("xml", "virsh", "xmltreefile", "validates") __uncompareable__ = __slots__ __schema_name__ = None @@ -50,17 +51,16 @@ def __init__(self, virsh_instance=virsh): :param virsh_instance: virsh module or instance to use """ - self.__dict_set__('xmltreefile', None) - self.__dict_set__('validates', None) - super(LibvirtXMLBase, self).__init__({'virsh': virsh_instance, - 'xml': None}) + self.__dict_set__("xmltreefile", None) + self.__dict_set__("validates", None) + super(LibvirtXMLBase, self).__init__({"virsh": virsh_instance, "xml": None}) # Can't use accessors module here, would make circular dep. def __str__(self): """ Returns raw XML as a string """ - return str(self.__dict_get__('xml')) + return str(self.__dict_get__("xml")) def __eq__(self, other): # Dynamic accessor methods mean we cannot compare class objects @@ -76,15 +76,19 @@ def __eq__(self, other): for slot in slots - uncomparable: try: dict_1[slot] = getattr(self, slot) - except (xcepts.LibvirtXMLNotFoundError, - xcepts.LibvirtXMLAccessorError, - AttributeError): + except ( + xcepts.LibvirtXMLNotFoundError, + xcepts.LibvirtXMLAccessorError, + AttributeError, + ): pass # Unset virtual values won't have keys try: dict_2[slot] = getattr(other, slot) - except (xcepts.LibvirtXMLNotFoundError, - xcepts.LibvirtXMLAccessorError, - AttributeError): + except ( + xcepts.LibvirtXMLNotFoundError, + xcepts.LibvirtXMLAccessorError, + AttributeError, + ): pass # Unset virtual values won't have keys return dict_1 == dict_2 @@ -105,28 +109,30 @@ def set_virsh(self, value): """Accessor method for virsh property, make sure it's right type""" value_type = type(value) # issubclass can't work for classes using __slots__ (i.e. no __bases__) - if hasattr(value, 'VIRSH_EXEC') or hasattr(value, 'virsh_exec'): - self.__dict_set__('virsh', value) + if hasattr(value, "VIRSH_EXEC") or hasattr(value, "virsh_exec"): + self.__dict_set__("virsh", value) else: - raise xcepts.LibvirtXMLError("virsh parameter must be a module " - "named virsh or subclass of virsh.VirshBase " - "not a %s" % str(value_type)) + raise xcepts.LibvirtXMLError( + "virsh parameter must be a module " + "named virsh or subclass of virsh.VirshBase " + "not a %s" % str(value_type) + ) def set_xml(self, value): """ Accessor method for 'xml' property to load using xml_utils.XMLTreeFile """ # Always check to see if a "set" accessor is being called from __init__ - if not self.__super_get__('INITIALIZED'): - self.__dict_set__('xml', value) + if not self.__super_get__("INITIALIZED"): + self.__dict_set__("xml", value) else: try: - if self.__dict_get__('xml') is not None: - del self['xml'] # clean up old temporary files + if self.__dict_get__("xml") is not None: + del self["xml"] # clean up old temporary files except KeyError: pass # Allow other exceptions through # value could be filename or a string full of XML - self.__dict_set__('xml', xml_utils.XMLTreeFile(value)) + self.__dict_set__("xml", xml_utils.XMLTreeFile(value)) def get_xml(self): """ @@ -140,7 +146,7 @@ def get_xmltreefile(self): """ try: # don't call get_xml() recursively - xml = self.__dict_get__('xml') + xml = self.__dict_get__("xml") if xml is None: raise KeyError except (KeyError, AttributeError): @@ -152,16 +158,17 @@ def set_xmltreefile(self, value): Point instance directly at an already initialized XMLTreeFile instance """ if not issubclass(type(value), xml_utils.XMLTreeFile): - raise xcepts.LibvirtXMLError("xmltreefile value must be XMLTreefile" - " type or subclass, not a %s" - % type(value)) - self.__dict_set__('xml', value) + raise xcepts.LibvirtXMLError( + "xmltreefile value must be XMLTreefile" + " type or subclass, not a %s" % type(value) + ) + self.__dict_set__("xml", value) def del_xmltreefile(self): """ Remove all backing XML """ - self.__dict_del__('xml') + self.__dict_del__("xml") def copy(self): """ @@ -171,10 +178,10 @@ def copy(self): the_copy = self.__class__(virsh_instance=self.virsh) try: # file may not be accessible, obtain XML string value - xmlstr = str(self.__dict_get__('xml')) + xmlstr = str(self.__dict_get__("xml")) # Create fresh/new XMLTreeFile along with tmp files from XML content # content - the_copy.__dict_set__('xml', xml_utils.XMLTreeFile(xmlstr)) + the_copy.__dict_set__("xml", xml_utils.XMLTreeFile(xmlstr)) except xcepts.LibvirtXMLError: # Allow other exceptions through pass # no XML was loaded yet return the_copy @@ -188,8 +195,7 @@ def get_section_string(self, xpath, index=0): """ section = self.xmltreefile.find(xpath) if section is None: - raise xcepts.LibvirtXMLNotFoundError( - "Path %s is not found." % xpath) + raise xcepts.LibvirtXMLNotFoundError("Path %s is not found." % xpath) return self.xmltreefile.get_element_string(xpath, index=index) @@ -198,8 +204,7 @@ def get_validates(self): Accessor method for 'validates' property returns virt-xml-validate T/F """ # self.xml is the filename - ret = self.virt_xml_validate(self.xml, - self.__super_get__('__schema_name__')) + ret = self.virt_xml_validate(self.xml, self.__super_get__("__schema_name__")) if ret.exit_status == 0: return True else: @@ -230,9 +235,9 @@ def virt_xml_validate(filename, schema_name=None): """ Return CmdResult from running virt-xml-validate on backing XML """ - command = 'virt-xml-validate %s' % filename + command = "virt-xml-validate %s" % filename if schema_name: - command += ' %s' % schema_name + command += " %s" % schema_name cmdresult = process.run(command, ignore_status=True) cmdresult.stdout = cmdresult.stdout_text cmdresult.stderr = cmdresult.stderr_text @@ -296,32 +301,33 @@ def setup_attrs(self, **attrs): for key, value in attrs.items(): if key not in self.__all_slots__: - raise AttributeError('Cannot set attribute "%s" to %s object.' - 'There is no such attribute.' - % (key, self.__class__)) - get_func = eval('self.get_%s' % key) + raise AttributeError( + 'Cannot set attribute "%s" to %s object.' + "There is no such attribute." % (key, self.__class__) + ) + get_func = eval("self.get_%s" % key) # Skip Getters if they are customized if not isinstance(get_func, propcan.PropCanBase): continue # Is XMLElementNest or not - subclass = get_func.get('subclass') + subclass = get_func.get("subclass") if subclass is None: setattr(self, key, value) else: # Whether to keep the sub-xml instance and modify it # or completely re-create one - reset_all = value.get('reset_all') is True - if 'reset_all' in value: - value.pop('reset_all') + reset_all = value.get("reset_all") is True + if "reset_all" in value: + value.pop("reset_all") # If Element tag is not found, we need to create a new instance # to set the attributes. # If reset_all, it means we will discard the existing instance # of current sub-xml and create a new one to replace it. if reset_all or self.xmltreefile.find(key) is None: # Get args to create an instance of subclass - subclass_dargs = get_func.get('subclass_dargs') + subclass_dargs = get_func.get("subclass_dargs") # Create an instance of subclass with given args target_obj = subclass(**subclass_dargs) else: @@ -364,15 +370,17 @@ def fetch_attrs(self): """ attrs = {} - slots = set(self.__all_slots__) - set(self.__uncompareable__) - {'device_tag'} + slots = set(self.__all_slots__) - set(self.__uncompareable__) - {"device_tag"} for key in slots: try: # Try to get values of each attribute, slot by slot value = self[key] - except (xcepts.LibvirtXMLAccessorError, - xcepts.LibvirtXMLNotFoundError, - AttributeError, - KeyError): + except ( + xcepts.LibvirtXMLAccessorError, + xcepts.LibvirtXMLNotFoundError, + AttributeError, + KeyError, + ): continue else: # Got value and check the type of value. If it's an nested xml @@ -408,8 +416,10 @@ def load_xml_module(path, name, type_list): """ # Module names and tags are always all lower-case name = str(name).lower() - errmsg = ("Unknown/unsupported type '%s', supported types %s" - % (str(name), type_list)) + errmsg = "Unknown/unsupported type '%s', supported types %s" % ( + str(name), + type_list, + ) if name not in type_list: raise xcepts.LibvirtXMLError(errmsg) try: @@ -417,12 +427,13 @@ def load_xml_module(path, name, type_list): # Enforce capitalized class names return getattr(mod_obj, name.capitalize()) except TypeError as detail: - raise xcepts.LibvirtXMLError(errmsg + ': %s' % str(detail)) + raise xcepts.LibvirtXMLError(errmsg + ": %s" % str(detail)) except ImportError as detail: - raise xcepts.LibvirtXMLError("Can't find module %s in %s: %s" - % (name, path, str(detail))) + raise xcepts.LibvirtXMLError( + "Can't find module %s in %s: %s" % (name, path, str(detail)) + ) except AttributeError as detail: - raise xcepts.LibvirtXMLError("Can't find class %s in %s module in " - "%s: %s" - % (name.capitalize(), name, path, - str(detail))) + raise xcepts.LibvirtXMLError( + "Can't find class %s in %s module in " + "%s: %s" % (name.capitalize(), name, path, str(detail)) + ) diff --git a/virttest/libvirt_xml/capability_xml.py b/virttest/libvirt_xml/capability_xml.py index 4043c46b01..85876ba142 100755 --- a/virttest/libvirt_xml/capability_xml.py +++ b/virttest/libvirt_xml/capability_xml.py @@ -24,70 +24,92 @@ class CapabilityXML(base.LibvirtXMLBase): # TODO: Add more __slots__ and accessors to get some useful stats # e.g. guest_count etc. - __slots__ = ('uuid', 'guest_capabilities', 'cpu_count', 'arch', 'model', - 'vendor', 'feature_list', 'power_management_list', 'cpu_pages', - 'cpu_topology', 'cells_topology', 'iommu') + __slots__ = ( + "uuid", + "guest_capabilities", + "cpu_count", + "arch", + "model", + "vendor", + "feature_list", + "power_management_list", + "cpu_pages", + "cpu_topology", + "cells_topology", + "iommu", + ) __schema_name__ = "capability" def __init__(self, virsh_instance=base.virsh): - accessors.XMLElementText(property_name="uuid", - libvirtxml=self, - forbidden=['set', 'del'], - parent_xpath='/host', - tag_name='uuid') + accessors.XMLElementText( + property_name="uuid", + libvirtxml=self, + forbidden=["set", "del"], + parent_xpath="/host", + tag_name="uuid", + ) # This will skip self.get_guest_capabilities() defined below - accessors.AllForbidden(property_name="guest_capabilities", - libvirtxml=self) + accessors.AllForbidden(property_name="guest_capabilities", libvirtxml=self) # This will skip self.get_cpu_count() defined below - accessors.AllForbidden(property_name="cpu_count", - libvirtxml=self) + accessors.AllForbidden(property_name="cpu_count", libvirtxml=self) # The set action is for test. - accessors.XMLElementText(property_name="arch", - libvirtxml=self, - forbidden=['del'], - parent_xpath='/host/cpu', - tag_name='arch') - accessors.XMLElementText(property_name="model", - libvirtxml=self, - forbidden=['del'], - parent_xpath='/host/cpu', - tag_name='model') - accessors.XMLElementText(property_name="vendor", - libvirtxml=self, - forbidden=['del'], - parent_xpath='/host/cpu', - tag_name='vendor') - accessors.XMLElementDict(property_name="cpu_topology", - libvirtxml=self, - forbidden=['del'], - parent_xpath='/host/cpu', - tag_name='topology') - accessors.XMLElementList(property_name="cpu_pages", - libvirtxml=self, - parent_xpath='/host/cpu', - marshal_from=self.marshal_from_pages, - marshal_to=self.marshal_to_pages) - accessors.XMLElementDict(property_name="iommu", - libvirtxml=self, - forbidden=['del'], - parent_xpath='/host', - tag_name='iommu') - accessors.XMLElementNest(property_name='cells_topology', - libvirtxml=self, - parent_xpath='/host', - tag_name='topology', - subclass=TopologyXML, - subclass_dargs={ - 'virsh_instance': virsh_instance}) + accessors.XMLElementText( + property_name="arch", + libvirtxml=self, + forbidden=["del"], + parent_xpath="/host/cpu", + tag_name="arch", + ) + accessors.XMLElementText( + property_name="model", + libvirtxml=self, + forbidden=["del"], + parent_xpath="/host/cpu", + tag_name="model", + ) + accessors.XMLElementText( + property_name="vendor", + libvirtxml=self, + forbidden=["del"], + parent_xpath="/host/cpu", + tag_name="vendor", + ) + accessors.XMLElementDict( + property_name="cpu_topology", + libvirtxml=self, + forbidden=["del"], + parent_xpath="/host/cpu", + tag_name="topology", + ) + accessors.XMLElementList( + property_name="cpu_pages", + libvirtxml=self, + parent_xpath="/host/cpu", + marshal_from=self.marshal_from_pages, + marshal_to=self.marshal_to_pages, + ) + accessors.XMLElementDict( + property_name="iommu", + libvirtxml=self, + forbidden=["del"], + parent_xpath="/host", + tag_name="iommu", + ) + accessors.XMLElementNest( + property_name="cells_topology", + libvirtxml=self, + parent_xpath="/host", + tag_name="topology", + subclass=TopologyXML, + subclass_dargs={"virsh_instance": virsh_instance}, + ) # This will skip self.get_feature_list() defined below - accessors.AllForbidden(property_name="feature_list", - libvirtxml=self) + accessors.AllForbidden(property_name="feature_list", libvirtxml=self) # This will skip self.get_power_management_list() defined below - accessors.AllForbidden(property_name="power_management_list", - libvirtxml=self) + accessors.AllForbidden(property_name="power_management_list", libvirtxml=self) super(CapabilityXML, self).__init__(virsh_instance) # calls set_xml accessor method - self['xml'] = self.__dict_get__('virsh').capabilities() + self["xml"] = self.__dict_get__("virsh").capabilities() @staticmethod def marshal_from_pages(item, index, libvirtxml): @@ -97,10 +119,10 @@ def marshal_from_pages(item, index, libvirtxml): del index del libvirtxml if not isinstance(item, dict): - raise xcepts.LibvirtXMLError("Expected a dictionary of pages " - "attributes, not a %s" - % str(item)) - return ('pages', dict(item)) + raise xcepts.LibvirtXMLError( + "Expected a dictionary of pages " "attributes, not a %s" % str(item) + ) + return ("pages", dict(item)) @staticmethod def marshal_to_pages(tag, attr_dict, index, libvirtxml): @@ -109,7 +131,7 @@ def marshal_to_pages(tag, attr_dict, index, libvirtxml): """ del index del libvirtxml - if tag != 'pages': + if tag != "pages": return None return dict(attr_dict) @@ -121,37 +143,37 @@ def get_guest_capabilities(self): 'machine': [, ...], 'domaini_': {'emulator': ''}}}} """ guest_capa = {} - xmltreefile = self.__dict_get__('xml') - for guest in xmltreefile.findall('guest'): - os_type_name = guest.find('os_type').text + xmltreefile = self.__dict_get__("xml") + for guest in xmltreefile.findall("guest"): + os_type_name = guest.find("os_type").text # Multiple guest definitions can share same os_type (e.g. hvm, pvm) - if os_type_name == 'xen': - os_type_name = 'pv' + if os_type_name == "xen": + os_type_name = "pv" guest_arch = guest_capa.get(os_type_name, {}) - for arch in guest.findall('arch'): - arch_name = arch.get('name') + for arch in guest.findall("arch"): + arch_name = arch.get("name") arch_prop = guest_arch.get(arch_name, {}) - arch_prop['wordsize'] = arch.find('wordsize').text - arch_prop['emulator'] = arch.find('emulator').text + arch_prop["wordsize"] = arch.find("wordsize").text + arch_prop["emulator"] = arch.find("emulator").text # TODO: maxcpus differs for machine in a given arch. # Right now takes first machine as default for the given # arch and assigns maxcpu for the arch, ideally below machine # list to get populated with maxcpu for each machine # not modifying as no requirement for now and as it would break # other places where machine list is used. - arch_prop['maxcpus'] = arch.findall('machine')[0].get('maxCpus') + arch_prop["maxcpus"] = arch.findall("machine")[0].get("maxCpus") m_list = [] - for machine in arch.findall('machine'): + for machine in arch.findall("machine"): machine_text = machine.text # Don't add duplicate entries if not m_list.count(machine_text): m_list.append(machine_text) - arch_prop['machine'] = m_list - for domain in arch.findall('domain'): - domain_name = "domain_" + domain.get('type') + arch_prop["machine"] = m_list + for domain in arch.findall("domain"): + domain_name = "domain_" + domain.get("type") dom_prop = {} - if domain.find('emulator') is not None: - dom_prop['emulator'] = domain.find('emulator').text + if domain.find("emulator") is not None: + dom_prop["emulator"] = domain.find("emulator").text arch_prop[domain_name] = dom_prop guest_arch[arch_name] = arch_prop guest_capa[os_type_name] = guest_arch @@ -161,8 +183,8 @@ def get_power_management_list(self): """ Accessor method for power_management_list property (in __slots__) """ - xmltreefile = self.__dict_get__('xml') - pms = list(xmltreefile.find('host').find('power_management')) + xmltreefile = self.__dict_get__("xml") + pms = list(xmltreefile.find("host").find("power_management")) return [pm.tag for pm in pms] def get_feature_list(self): @@ -170,8 +192,8 @@ def get_feature_list(self): Accessor method for feature_list property (in __slots__) """ feature_list = [] # [, , ...] - xmltreefile = self.__dict_get__('xml') - for feature_node in xmltreefile.findall('/host/cpu/feature'): + xmltreefile = self.__dict_get__("xml") + for feature_node in xmltreefile.findall("/host/cpu/feature"): feature_list.append(feature_node) return feature_list @@ -197,16 +219,16 @@ def get_feature_name(self, num): :param num: Assigned feature number :return: Assigned feature name """ - return self.get_feature(num).get('name') + return self.get_feature(num).get("name") def get_cpu_count(self): """ Accessor method for cpu_count property (in __slots__) """ cpu_count = 0 - xmltreefile = self.__dict_get__('xml') - for cpus in xmltreefile.findall('/host/topology/cells/cell/cpus'): - cpu_num = cpus.get('num') + xmltreefile = self.__dict_get__("xml") + for cpus in xmltreefile.findall("/host/topology/cells/cell/cpus"): + cpu_num = cpus.get("num") cpu_count += int(cpu_num) return cpu_count @@ -216,9 +238,9 @@ def remove_feature(self, num): :param num: Assigned feature number """ - xmltreefile = self.__dict_get__('xml') + xmltreefile = self.__dict_get__("xml") feature_remove_node = self.get_feature(num) - cpu_node = xmltreefile.find('/host/cpu') + cpu_node = xmltreefile.find("/host/cpu") cpu_node.remove(feature_remove_node) def check_feature_name(self, name): @@ -229,14 +251,14 @@ def check_feature_name(self, name): :return: True if check pass """ sys_feature = [] - cpu_xml_file = open('/proc/cpuinfo', 'r') + cpu_xml_file = open("/proc/cpuinfo", "r") for line in cpu_xml_file.readlines(): - if line.find('flags') != -1: - feature_names = line.split(':')[1].strip() - sys_sub_feature = feature_names.split(' ') + if line.find("flags") != -1: + feature_names = line.split(":")[1].strip() + sys_sub_feature = feature_names.split(" ") sys_feature = list(set(sys_feature + sys_sub_feature)) cpu_xml_file.close() - return (name in sys_feature) + return name in sys_feature def set_feature(self, num, value): """ @@ -246,7 +268,7 @@ def set_feature(self, num, value): :param value: The feature name modified to """ feature_set_node = self.get_feature(num) - feature_set_node.set('name', value) + feature_set_node.set("name", value) def add_feature(self, value): """ @@ -254,9 +276,9 @@ def add_feature(self, value): :param value: The added feature name """ - xmltreefile = self.__dict_get__('xml') - cpu_node = xmltreefile.find('/host/cpu') - xml_utils.ElementTree.SubElement(cpu_node, 'feature', {'name': value}) + xmltreefile = self.__dict_get__("xml") + cpu_node = xmltreefile.find("/host/cpu") + xml_utils.ElementTree.SubElement(cpu_node, "feature", {"name": value}) class TopologyXML(base.LibvirtXMLBase): @@ -271,21 +293,22 @@ class TopologyXML(base.LibvirtXMLBase): list of cpu dict """ - __slots__ = ('num', 'cell') + __slots__ = ("num", "cell") def __init__(self, virsh_instance=base.virsh): """ Create new cells topology XML instance """ - accessors.XMLAttribute(property_name="num", - libvirtxml=self, - parent_xpath='/', - tag_name='cells', - attribute='num') - accessors.AllForbidden(property_name="cell", - libvirtxml=self) + accessors.XMLAttribute( + property_name="num", + libvirtxml=self, + parent_xpath="/", + tag_name="cells", + attribute="num", + ) + accessors.AllForbidden(property_name="cell", libvirtxml=self) super(TopologyXML, self).__init__(virsh_instance) - self.xml = self.__dict_get__('virsh').capabilities() + self.xml = self.__dict_get__("virsh").capabilities() self.xmltreefile.reroot("/host/topology") self.xmltreefile.write() @@ -294,11 +317,10 @@ def get_cell(self, withmem=False): Return CellXML instances list """ cell_list = [] - for cell_node in self.xmltreefile.findall('/cells/cell'): - if cell_node.find('memory') is None and withmem: + for cell_node in self.xmltreefile.findall("/cells/cell"): + if cell_node.find("memory") is None and withmem: continue - xml_str = xml_utils.ElementTree.tostring( - cell_node, encoding='unicode') + xml_str = xml_utils.ElementTree.tostring(cell_node, encoding="unicode") new_cell = CellXML() new_cell.xml = xml_str cell_list.append(new_cell) @@ -327,50 +349,60 @@ class CellXML(base.LibvirtXMLBase): list of cpu dict """ - __slots__ = ('cell_id', 'memory', 'mem_unit', 'pages', 'sibling', - 'cpus_num', 'cpu') + __slots__ = ("cell_id", "memory", "mem_unit", "pages", "sibling", "cpus_num", "cpu") def __init__(self, virsh_instance=base.virsh): """ Create new cpus XML instance """ - accessors.XMLAttribute(property_name="cell_id", - libvirtxml=self, - parent_xpath='/', - tag_name='cell', - attribute='id') - accessors.XMLElementInt(property_name="memory", - libvirtxml=self, - parent_xpath='/', - tag_name='memory') - accessors.XMLAttribute(property_name="mem_unit", - libvirtxml=self, - parent_xpath='/', - tag_name='memory', - attribute='unit') - accessors.XMLElementList(property_name="pages", - libvirtxml=self, - parent_xpath='/', - marshal_from=self.marshal_from_pages, - marshal_to=self.marshal_to_pages, - has_subclass=True) - accessors.XMLElementList(property_name="sibling", - libvirtxml=self, - parent_xpath='/distances', - marshal_from=self.marshal_from_sibling, - marshal_to=self.marshal_to_sibling) - accessors.XMLAttribute(property_name="cpus_num", - libvirtxml=self, - parent_xpath='/', - tag_name='cpus', - attribute='num') - accessors.XMLElementList(property_name="cpu", - libvirtxml=self, - parent_xpath='/cpus', - marshal_from=self.marshal_from_cpu, - marshal_to=self.marshal_to_cpu) + accessors.XMLAttribute( + property_name="cell_id", + libvirtxml=self, + parent_xpath="/", + tag_name="cell", + attribute="id", + ) + accessors.XMLElementInt( + property_name="memory", libvirtxml=self, parent_xpath="/", tag_name="memory" + ) + accessors.XMLAttribute( + property_name="mem_unit", + libvirtxml=self, + parent_xpath="/", + tag_name="memory", + attribute="unit", + ) + accessors.XMLElementList( + property_name="pages", + libvirtxml=self, + parent_xpath="/", + marshal_from=self.marshal_from_pages, + marshal_to=self.marshal_to_pages, + has_subclass=True, + ) + accessors.XMLElementList( + property_name="sibling", + libvirtxml=self, + parent_xpath="/distances", + marshal_from=self.marshal_from_sibling, + marshal_to=self.marshal_to_sibling, + ) + accessors.XMLAttribute( + property_name="cpus_num", + libvirtxml=self, + parent_xpath="/", + tag_name="cpus", + attribute="num", + ) + accessors.XMLElementList( + property_name="cpu", + libvirtxml=self, + parent_xpath="/cpus", + marshal_from=self.marshal_from_cpu, + marshal_to=self.marshal_to_cpu, + ) super(CellXML, self).__init__(virsh_instance) - self.xml = u"" + self.xml = "" @staticmethod def marshal_from_pages(item, index, libvirtxml): @@ -378,22 +410,23 @@ def marshal_from_pages(item, index, libvirtxml): Convert a xml object to pages tag and xml element. """ if isinstance(item, CellPagesXML): - return 'pages', item + return "pages", item elif isinstance(item, dict): cell_page = CellPagesXML() cell_page.setup_attrs(**item) - return 'pages', cell_page + return "pages", cell_page else: - raise xcepts.LibvirtXMLError("Expected a list of CellPagesXML " - "instances, not a %s" % str(item)) + raise xcepts.LibvirtXMLError( + "Expected a list of CellPagesXML " "instances, not a %s" % str(item) + ) @staticmethod def marshal_to_pages(tag, new_treefile, index, libvirtxml): """ Convert a pages tag xml element to an object of CellPagesXML. """ - if tag != 'pages': - return None # Don't convert this item + if tag != "pages": + return None # Don't convert this item newone = CellPagesXML(virsh_instance=libvirtxml.virsh) newone.xmltreefile = new_treefile return newone @@ -406,10 +439,10 @@ def marshal_from_sibling(item, index, libvirtxml): del index del libvirtxml if not isinstance(item, dict): - raise xcepts.LibvirtXMLError("Expected a dictionary of sibling " - "attributes, not a %s" - % str(item)) - return ('sibling', dict(item)) + raise xcepts.LibvirtXMLError( + "Expected a dictionary of sibling " "attributes, not a %s" % str(item) + ) + return ("sibling", dict(item)) @staticmethod def marshal_to_sibling(tag, attr_dict, index, libvirtxml): @@ -418,7 +451,7 @@ def marshal_to_sibling(tag, attr_dict, index, libvirtxml): """ del index del libvirtxml - if tag != 'sibling': + if tag != "sibling": return None return dict(attr_dict) @@ -430,10 +463,10 @@ def marshal_from_cpu(item, index, libvirtxml): del index del libvirtxml if not isinstance(item, dict): - raise xcepts.LibvirtXMLError("Expected a dictionary of cpu " - "attributes, not a %s" - % str(item)) - return ('cpu', dict(item)) + raise xcepts.LibvirtXMLError( + "Expected a dictionary of cpu " "attributes, not a %s" % str(item) + ) + return ("cpu", dict(item)) @staticmethod def marshal_to_cpu(tag, attr_dict, index, libvirtxml): @@ -442,7 +475,7 @@ def marshal_to_cpu(tag, attr_dict, index, libvirtxml): """ del index del libvirtxml - if tag != 'cpu': + if tag != "cpu": return None return dict(attr_dict) @@ -461,25 +494,31 @@ class CellPagesXML(base.LibvirtXMLBase): text, number of the pages """ - __slots__ = ('pages', 'unit', 'size') + __slots__ = ("pages", "unit", "size") def __init__(self, virsh_instance=base.virsh): """ Create new cpus XML instance """ - accessors.XMLElementText(property_name="pages", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='pages') - accessors.XMLAttribute(property_name="unit", - libvirtxml=self, - parent_xpath='/', - tag_name='pages', - attribute='unit') - accessors.XMLAttribute(property_name="size", - libvirtxml=self, - parent_xpath='/', - tag_name='pages', - attribute='size') - self.xml = u"" + accessors.XMLElementText( + property_name="pages", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="pages", + ) + accessors.XMLAttribute( + property_name="unit", + libvirtxml=self, + parent_xpath="/", + tag_name="pages", + attribute="unit", + ) + accessors.XMLAttribute( + property_name="size", + libvirtxml=self, + parent_xpath="/", + tag_name="pages", + attribute="size", + ) + self.xml = "" diff --git a/virttest/libvirt_xml/checkpoint_xml.py b/virttest/libvirt_xml/checkpoint_xml.py index 87c7d2e168..a09859b870 100644 --- a/virttest/libvirt_xml/checkpoint_xml.py +++ b/virttest/libvirt_xml/checkpoint_xml.py @@ -21,22 +21,26 @@ class CheckpointXML(base.LibvirtXMLBase): bitmap: name of the bitmap to be created """ - __slots__ = ('name', 'description', 'disks') + __slots__ = ("name", "description", "disks") __uncompareable__ = base.LibvirtXMLBase.__uncompareable__ __schema_name__ = "domaincheckpoint" def __init__(self, virsh_instance=base.virsh): - accessors.XMLElementText('name', self, parent_xpath='/', - tag_name='name') - accessors.XMLElementText('description', self, parent_xpath='/', - tag_name='description') - accessors.XMLElementList('disks', self, parent_xpath='/disks', - marshal_from=self.marshal_from_disks, - marshal_to=self.marshal_to_disks) + accessors.XMLElementText("name", self, parent_xpath="/", tag_name="name") + accessors.XMLElementText( + "description", self, parent_xpath="/", tag_name="description" + ) + accessors.XMLElementList( + "disks", + self, + parent_xpath="/disks", + marshal_from=self.marshal_from_disks, + marshal_to=self.marshal_to_disks, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" @staticmethod def marshal_from_disks(item, index, libvirtxml): @@ -45,7 +49,7 @@ def marshal_from_disks(item, index, libvirtxml): """ del index del libvirtxml - return ('disk', dict(item)) + return ("disk", dict(item)) @staticmethod def marshal_to_disks(tag, attr_dict, index, libvirtxml): @@ -54,13 +58,14 @@ def marshal_to_disks(tag, attr_dict, index, libvirtxml): """ del index del libvirtxml - if tag != 'disk': + if tag != "disk": return None return dict(attr_dict) @staticmethod - def new_from_checkpoint_dumpxml(name, checkpoint_name, options="", - virsh_instance=base.virsh): + def new_from_checkpoint_dumpxml( + name, checkpoint_name, options="", virsh_instance=base.virsh + ): """ Return new CheckpointXML instance from virsh checkpoint-dumpxml cmd @@ -72,5 +77,5 @@ def new_from_checkpoint_dumpxml(name, checkpoint_name, options="", """ checkpoint_xml = CheckpointXML(virsh_instance=virsh_instance) result = virsh_instance.checkpoint_dumpxml(name, checkpoint_name, options) - checkpoint_xml['xml'] = result.stdout_text.strip() + checkpoint_xml["xml"] = result.stdout_text.strip() return checkpoint_xml diff --git a/virttest/libvirt_xml/devices/address.py b/virttest/libvirt_xml/devices/address.py index 8bd33188a0..506aeb2186 100644 --- a/virttest/libvirt_xml/devices/address.py +++ b/virttest/libvirt_xml/devices/address.py @@ -10,26 +10,25 @@ class Address(base.TypedDeviceBase): - __slots__ = ('attrs',) + __slots__ = ("attrs",) def __init__(self, type_name, virsh_instance=base.base.virsh): # Blindly accept any/all attributes as simple dictionary - accessors.XMLElementDict('attrs', self, parent_xpath='/', - tag_name='address') - super(self.__class__, self).__init__(device_tag='address', - type_name=type_name, - virsh_instance=virsh_instance) + accessors.XMLElementDict("attrs", self, parent_xpath="/", tag_name="address") + super(self.__class__, self).__init__( + device_tag="address", type_name=type_name, virsh_instance=virsh_instance + ) @classmethod def new_from_dict(cls, attributes, virsh_instance=base.base.virsh): # type_name is mandatory, throw exception if doesn't exist try: # pop() so don't process again in loop below - instance = cls(type_name=attributes.pop('type_name'), - virsh_instance=virsh_instance) + instance = cls( + type_name=attributes.pop("type_name"), virsh_instance=virsh_instance + ) except (KeyError, AttributeError): - raise xcepts.LibvirtXMLError("type_name is manditory for " - "Address class") + raise xcepts.LibvirtXMLError("type_name is manditory for " "Address class") # Stick property values in as attributes xtfroot = instance.xmltreefile.getroot() for key, value in list(attributes.items()): @@ -41,8 +40,9 @@ def new_from_element(cls, element, virsh_instance=base.base.virsh): # element uses type attribute, class uses type_name edict = dict(list(element.items())) try: - edict['type_name'] = edict.pop('type') + edict["type_name"] = edict.pop("type") except (KeyError, AttributeError): - raise xcepts.LibvirtXMLError("type attribute is manditory for " - "Address class") + raise xcepts.LibvirtXMLError( + "type attribute is manditory for " "Address class" + ) return cls.new_from_dict(edict, virsh_instance=virsh_instance) diff --git a/virttest/libvirt_xml/devices/audio.py b/virttest/libvirt_xml/devices/audio.py index 25f630a5bc..a41bd136c6 100644 --- a/virttest/libvirt_xml/devices/audio.py +++ b/virttest/libvirt_xml/devices/audio.py @@ -10,32 +10,34 @@ class Audio(base.UntypedDeviceBase): - __slots__ = ('id', 'type', 'attrs', 'input_attrs', - 'input_settings', 'output_attrs', 'output_settings') + __slots__ = ( + "id", + "type", + "attrs", + "input_attrs", + "input_settings", + "output_attrs", + "output_settings", + ) def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute('id', self, - parent_xpath='/', - tag_name='audio', - attribute='id') - accessors.XMLAttribute('type', self, - parent_xpath='/', - tag_name='audio', - attribute='type') - accessors.XMLElementDict('attrs', self, - parent_xpath='/', - tag_name='audio') - accessors.XMLElementDict('input_attrs', self, - parent_xpath='/', - tag_name='input') - accessors.XMLElementDict('input_settings', self, - parent_xpath='/input', - tag_name='settings') - accessors.XMLElementDict('output_attrs', self, - parent_xpath='/', - tag_name='output') - accessors.XMLElementDict('output_settings', self, - parent_xpath='/output', - tag_name='settings') - super(Audio, self).__init__(device_tag='audio', - virsh_instance=virsh_instance) + accessors.XMLAttribute( + "id", self, parent_xpath="/", tag_name="audio", attribute="id" + ) + accessors.XMLAttribute( + "type", self, parent_xpath="/", tag_name="audio", attribute="type" + ) + accessors.XMLElementDict("attrs", self, parent_xpath="/", tag_name="audio") + accessors.XMLElementDict( + "input_attrs", self, parent_xpath="/", tag_name="input" + ) + accessors.XMLElementDict( + "input_settings", self, parent_xpath="/input", tag_name="settings" + ) + accessors.XMLElementDict( + "output_attrs", self, parent_xpath="/", tag_name="output" + ) + accessors.XMLElementDict( + "output_settings", self, parent_xpath="/output", tag_name="settings" + ) + super(Audio, self).__init__(device_tag="audio", virsh_instance=virsh_instance) diff --git a/virttest/libvirt_xml/devices/base.py b/virttest/libvirt_xml/devices/base.py index c2d0cfffe8..4d19effeff 100644 --- a/virttest/libvirt_xml/devices/base.py +++ b/virttest/libvirt_xml/devices/base.py @@ -16,7 +16,7 @@ class UntypedDeviceBase(base.LibvirtXMLBase): Base class implementing common functions for all device XML w/o a type attr. """ - __slots__ = ('device_tag',) + __slots__ = ("device_tag",) # Subclasses are expected to hide device_tag def __init__(self, device_tag, virsh_instance=base.virsh): @@ -26,9 +26,9 @@ def __init__(self, device_tag, virsh_instance=base.virsh): super(UntypedDeviceBase, self).__init__(virsh_instance=virsh_instance) # Just a regular dictionary value # (Using a property to change element tag won't work) - self['device_tag'] = device_tag + self["device_tag"] = device_tag # setup bare-bones XML - self.xml = u"<%s/>" % device_tag + self.xml = "<%s/>" % device_tag def from_element(self, element): """ @@ -36,9 +36,10 @@ def from_element(self, element): """ class_name = self.__class__.__name__ if element.tag != class_name.lower(): - raise xcepts.LibvirtXMLError('Refusing to create %s instance' - 'from %s tagged element' - % (class_name, element.tag)) + raise xcepts.LibvirtXMLError( + "Refusing to create %s instance" + "from %s tagged element" % (class_name, element.tag) + ) # XMLTreeFile only supports element trees etree = xml_utils.ElementTree.ElementTree(element) # ET only writes to open file-like objects @@ -84,9 +85,10 @@ def _set_list(self, tag_name, value): Set all elements to the value list of dictionaries of element's attributes. """ - xcept = xcepts.LibvirtXMLError("Must set %s child %s elements from" - " a list of dictionary" - % (self.device_tag, tag_name)) + xcept = xcepts.LibvirtXMLError( + "Must set %s child %s elements from" + " a list of dictionary" % (self.device_tag, tag_name) + ) if not isinstance(value, list): raise xcept # Start with clean slate @@ -94,8 +96,7 @@ def _set_list(self, tag_name, value): for dict_item in value: if not isinstance(dict_item, dict): raise xcept - ElementTree.SubElement(self.xmltreefile.getroot(), - tag_name, dict_item) + ElementTree.SubElement(self.xmltreefile.getroot(), tag_name, dict_item) self.xmltreefile.write() def _del_list(self, tag_filter): @@ -133,7 +134,7 @@ class TypedDeviceBase(UntypedDeviceBase): Base class implementing common functions for all device XML w/o a type attr. """ - __slots__ = ('type_name',) + __slots__ = ("type_name",) # Subclasses are expected to hide device_tag def __init__(self, device_tag, type_name, virsh_instance=base.virsh): @@ -141,14 +142,18 @@ def __init__(self, device_tag, type_name, virsh_instance=base.virsh): Initialize Typed device instance's basic XML with type_name & device_tag """ # generate getter, setter, deleter for 'type_name' property - accessors.XMLAttribute('type_name', self, - # each device is it's own XML "document" - # because python 2.6 ElementPath is broken - parent_xpath='/', - tag_name=device_tag, - attribute='type') - super(TypedDeviceBase, self).__init__(device_tag=device_tag, - virsh_instance=virsh_instance) + accessors.XMLAttribute( + "type_name", + self, + # each device is it's own XML "document" + # because python 2.6 ElementPath is broken + parent_xpath="/", + tag_name=device_tag, + attribute="type", + ) + super(TypedDeviceBase, self).__init__( + device_tag=device_tag, virsh_instance=virsh_instance + ) # Calls accessor to modify xml self.type_name = type_name @@ -157,10 +162,9 @@ def new_from_element(cls, element, virsh_instance=base.virsh): """ Hides type_name from superclass new_from_element(). """ - type_name = element.get('type', None) + type_name = element.get("type", None) # subclasses must hide device_tag parameter - instance = cls(type_name=type_name, - virsh_instance=virsh_instance) + instance = cls(type_name=type_name, virsh_instance=virsh_instance) instance.from_element(element) return instance @@ -186,6 +190,7 @@ def new_from_element(cls, element, virsh_instance=base.virsh): # virsh_instance=virsh_instance) # + class StubDeviceMeta(type): """ @@ -210,15 +215,16 @@ def __init__(mcs, name, bases, dct): # Needed for UntypedDeviceBase __init__'s default argument value # i.e. device_tag='disk' as specified by specific device class - if not hasattr(mcs, '_device_tag'): - raise ValueError( - "Class %s requires a _device_tag attribute" % name) + if not hasattr(mcs, "_device_tag"): + raise ValueError("Class %s requires a _device_tag attribute" % name) # Same message for both TypedDeviceBase & UntypedDeviceBase subclasses - message = ("Detected use of a stub device XML for a %s class. These " - "only implement a minimal interface that is very likely to " - "change in future versions. This warning will only be " - " logged once." % name) + message = ( + "Detected use of a stub device XML for a %s class. These " + "only implement a minimal interface that is very likely to " + "change in future versions. This warning will only be " + " logged once." % name + ) def issue_warning(): """ @@ -236,13 +242,18 @@ def issue_warning(): if TypedDeviceBase in bases: # Needed for TypedDeviceBase __init__'s default argument value # i.e. type_name='pci' as specified by specific device class. - if not hasattr(mcs, '_def_type_name'): - raise ValueError("TypedDevice sub-Class %s must define a " - "_def_type_name attribute" % name) + if not hasattr(mcs, "_def_type_name"): + raise ValueError( + "TypedDevice sub-Class %s must define a " + "_def_type_name attribute" % name + ) # form __init__() and it's arguments for generated class - def stub_init(self, type_name=getattr(mcs, '_def_type_name'), - virsh_instance=base.virsh): + def stub_init( + self, + type_name=getattr(mcs, "_def_type_name"), + virsh_instance=base.virsh, + ): """ Initialize stub typed device instance """ @@ -251,10 +262,13 @@ def stub_init(self, type_name=getattr(mcs, '_def_type_name'), issue_warning() # Created class __init__ still needs to call superclass # __init__ (i.e. UntypedDeviceBase or TypedDeviceBase) - TypedDeviceBase.__init__(self, device_tag=getattr(mcs, - '_device_tag'), - type_name=type_name, - virsh_instance=virsh_instance) + TypedDeviceBase.__init__( + self, + device_tag=getattr(mcs, "_device_tag"), + type_name=type_name, + virsh_instance=virsh_instance, + ) + elif UntypedDeviceBase in bases: # generate __init__() for untyped devices (similar to above) def stub_init(self, virsh_instance=base.virsh): @@ -262,12 +276,16 @@ def stub_init(self, virsh_instance=base.virsh): Initialize stub un-typed device instance """ issue_warning() - UntypedDeviceBase.__init__(self, device_tag=getattr(mcs, - '_device_tag'), - virsh_instance=virsh_instance) + UntypedDeviceBase.__init__( + self, + device_tag=getattr(mcs, "_device_tag"), + virsh_instance=virsh_instance, + ) + else: # unexpected usage - raise TypeError("Class %s is not a subclass of TypedDeviceBase or " - "UntypedDeviceBase") + raise TypeError( + "Class %s is not a subclass of TypedDeviceBase or " "UntypedDeviceBase" + ) # Point the generated class's __init__ at the generated function above - setattr(mcs, '__init__', stub_init) + setattr(mcs, "__init__", stub_init) diff --git a/virttest/libvirt_xml/devices/channel.py b/virttest/libvirt_xml/devices/channel.py index 6c68b60b3d..7c24cb629b 100644 --- a/virttest/libvirt_xml/devices/channel.py +++ b/virttest/libvirt_xml/devices/channel.py @@ -11,15 +11,18 @@ class Channel(CharacterBase): - __slots__ = ('sources', 'target', 'alias', 'address') + __slots__ = ("sources", "target", "alias", "address") - def __init__(self, type_name='unix', virsh_instance=base.virsh): - accessors.XMLElementList('sources', self, parent_xpath='/', - marshal_from=self.marshal_from_sources, - marshal_to=self.marshal_to_sources, - has_subclass=True) - accessors.XMLElementDict('target', self, parent_xpath='/', - tag_name='target') + def __init__(self, type_name="unix", virsh_instance=base.virsh): + accessors.XMLElementList( + "sources", + self, + parent_xpath="/", + marshal_from=self.marshal_from_sources, + marshal_to=self.marshal_to_sources, + has_subclass=True, + ) + accessors.XMLElementDict("target", self, parent_xpath="/", tag_name="target") # example for new slots : alias and address # # @@ -29,10 +32,8 @@ def __init__(self, type_name='unix', virsh_instance=base.virsh): # #
# - accessors.XMLElementDict('alias', self, parent_xpath='/', - tag_name='alias') - accessors.XMLElementDict('address', self, parent_xpath='/', - tag_name='address') - super( - Channel, self).__init__(device_tag='channel', type_name=type_name, - virsh_instance=virsh_instance) + accessors.XMLElementDict("alias", self, parent_xpath="/", tag_name="alias") + accessors.XMLElementDict("address", self, parent_xpath="/", tag_name="address") + super(Channel, self).__init__( + device_tag="channel", type_name=type_name, virsh_instance=virsh_instance + ) diff --git a/virttest/libvirt_xml/devices/character.py b/virttest/libvirt_xml/devices/character.py index 153030ab83..1b3fb419b9 100644 --- a/virttest/libvirt_xml/devices/character.py +++ b/virttest/libvirt_xml/devices/character.py @@ -11,7 +11,7 @@ class CharacterBase(base.TypedDeviceBase): - __slots__ = ('sources', 'targets') + __slots__ = ("sources", "targets") # Not overriding __init__ because ABC cannot hide device_tag as expected @@ -21,19 +21,19 @@ def get_targets(self): """ Return a list of dictionaries containing each target's attributes. """ - return self._get_list('target') + return self._get_list("target") def set_targets(self, value): """ Set all sources to the value list of dictionaries of target attributes. """ - self._set_list('target', value) + self._set_list("target", value) def del_targets(self): """ Remove the list of dictionaries containing each target's attributes. """ - self._del_list('target') + self._del_list("target") # Some convenience methods so appending to sources/targets is easier def add_source(self, **attributes): @@ -50,19 +50,19 @@ def add_target(self, **attributes): """ Convenience method for appending a target from dictionary of attributes """ - self._add_item('targets', **attributes) + self._add_item("targets", **attributes) def update_source(self, index, **attributes): """ Convenience method for merging values into a source's attributes """ - self._update_item('sources', index, **attributes) + self._update_item("sources", index, **attributes) def update_target(self, index, **attributes): """ Convenience method for merging values into a target's attributes """ - self._update_item('targets', index, **attributes) + self._update_item("targets", index, **attributes) @staticmethod def marshal_from_sources(item, index, libvirtxml): @@ -70,21 +70,22 @@ def marshal_from_sources(item, index, libvirtxml): Convert an xml object to source tag and xml element. """ if isinstance(item, CharacterBase.Source): - return 'source', item + return "source", item elif isinstance(item, dict): source = CharacterBase.Source() source.setup_attrs(**item) - return 'source', source + return "source", source else: - raise xcepts.LibvirtXMLError("Expected a list of Source " - "instances, not a %s" % str(item)) + raise xcepts.LibvirtXMLError( + "Expected a list of Source " "instances, not a %s" % str(item) + ) @staticmethod def marshal_to_sources(tag, new_treefile, index, libvirtxml): """ Convert a source tag xml element to an object of Source. """ - if tag != 'source': + if tag != "source": return None newone = CharacterBase.Source(virsh_instance=libvirtxml.virsh) newone.xmltreefile = new_treefile @@ -92,17 +93,20 @@ def marshal_to_sources(tag, new_treefile, index, libvirtxml): class Source(base.base.LibvirtXMLBase): - __slots__ = ('attrs', 'seclabels') + __slots__ = ("attrs", "seclabels") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLElementDict('attrs', self, parent_xpath='/', - tag_name='source') - accessors.XMLElementList('seclabels', self, parent_xpath='/', - marshal_from=self.marshal_from_seclabels, - marshal_to=self.marshal_to_seclabels, - has_subclass=True) + accessors.XMLElementDict("attrs", self, parent_xpath="/", tag_name="source") + accessors.XMLElementList( + "seclabels", + self, + parent_xpath="/", + marshal_from=self.marshal_from_seclabels, + marshal_to=self.marshal_to_seclabels, + has_subclass=True, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" @staticmethod def marshal_from_seclabels(item, index, libvirtxml): @@ -110,21 +114,22 @@ def marshal_from_seclabels(item, index, libvirtxml): Convert an xml object to seclabel tag and xml element. """ if isinstance(item, Seclabel): - return 'seclabel', item + return "seclabel", item elif isinstance(item, dict): seclabel = Seclabel() seclabel.setup_attrs(**item) - return 'seclabel', seclabel + return "seclabel", seclabel else: - raise xcepts.LibvirtXMLError("Expected a list of Seclabel " - "instances, not a %s" % str(item)) + raise xcepts.LibvirtXMLError( + "Expected a list of Seclabel " "instances, not a %s" % str(item) + ) @staticmethod def marshal_to_seclabels(tag, new_treefile, index, libvirtxml): """ Convert a seclabel tag xml element to an object of Seclabel. """ - if tag != 'seclabel': + if tag != "seclabel": return None newone = Seclabel(virsh_instance=libvirtxml.virsh) newone.xmltreefile = new_treefile diff --git a/virttest/libvirt_xml/devices/console.py b/virttest/libvirt_xml/devices/console.py index c261422e3e..d9e34d3d23 100644 --- a/virttest/libvirt_xml/devices/console.py +++ b/virttest/libvirt_xml/devices/console.py @@ -10,24 +10,39 @@ class Console(CharacterBase): - __slots__ = ('protocol_type', 'target_port', 'target_type', 'sources', - 'alias', 'log') + __slots__ = ( + "protocol_type", + "target_port", + "target_type", + "sources", + "alias", + "log", + ) - def __init__(self, type_name='pty', virsh_instance=base.virsh): - accessors.XMLAttribute('protocol_type', self, parent_xpath='/', - tag_name='protocol', attribute='type') - accessors.XMLAttribute('target_port', self, parent_xpath='/', - tag_name='target', attribute='port') - accessors.XMLAttribute('target_type', self, parent_xpath='/', - tag_name='target', attribute='type') - accessors.XMLElementList('sources', self, parent_xpath='/', - marshal_from=self.marshal_from_sources, - marshal_to=self.marshal_to_sources, - has_subclass=True) - accessors.XMLElementDict('alias', self, parent_xpath='/', - tag_name='alias') - accessors.XMLElementDict('log', self, parent_xpath='/', - tag_name='log') - super( - Console, self).__init__(device_tag='console', type_name=type_name, - virsh_instance=virsh_instance) + def __init__(self, type_name="pty", virsh_instance=base.virsh): + accessors.XMLAttribute( + "protocol_type", + self, + parent_xpath="/", + tag_name="protocol", + attribute="type", + ) + accessors.XMLAttribute( + "target_port", self, parent_xpath="/", tag_name="target", attribute="port" + ) + accessors.XMLAttribute( + "target_type", self, parent_xpath="/", tag_name="target", attribute="type" + ) + accessors.XMLElementList( + "sources", + self, + parent_xpath="/", + marshal_from=self.marshal_from_sources, + marshal_to=self.marshal_to_sources, + has_subclass=True, + ) + accessors.XMLElementDict("alias", self, parent_xpath="/", tag_name="alias") + accessors.XMLElementDict("log", self, parent_xpath="/", tag_name="log") + super(Console, self).__init__( + device_tag="console", type_name=type_name, virsh_instance=virsh_instance + ) diff --git a/virttest/libvirt_xml/devices/controller.py b/virttest/libvirt_xml/devices/controller.py index 82067afe3b..444844ca73 100644 --- a/virttest/libvirt_xml/devices/controller.py +++ b/virttest/libvirt_xml/devices/controller.py @@ -10,43 +10,63 @@ class Controller(base.TypedDeviceBase): - __slots__ = ('type', 'index', 'model', 'ports', 'vectors', 'driver', - 'address', 'pcihole64', 'target', 'alias', 'model_name', - 'node') - - def __init__(self, type_name='pci', virsh_instance=base.base.virsh): - super(Controller, self).__init__(device_tag='controller', - type_name=type_name, - virsh_instance=virsh_instance) + __slots__ = ( + "type", + "index", + "model", + "ports", + "vectors", + "driver", + "address", + "pcihole64", + "target", + "alias", + "model_name", + "node", + ) + + def __init__(self, type_name="pci", virsh_instance=base.base.virsh): + super(Controller, self).__init__( + device_tag="controller", type_name=type_name, virsh_instance=virsh_instance + ) # TODO: Remove 'type' since it's duplicated with 'type_name' - accessors.XMLAttribute('type', self, parent_xpath='/', - tag_name='controller', attribute='type') - accessors.XMLAttribute('index', self, parent_xpath='/', - tag_name='controller', attribute='index') - accessors.XMLAttribute('model', self, parent_xpath='/', - tag_name='controller', attribute='model') - accessors.XMLAttribute('ports', self, parent_xpath='/', - tag_name='controller', attribute='ports') - accessors.XMLAttribute('vectors', self, parent_xpath='/', - tag_name='controller', attribute='vectors') - accessors.XMLElementText('pcihole64', self, parent_xpath='/', - tag_name='pcihole64') - accessors.XMLElementDict('driver', self, parent_xpath='/', - tag_name='driver') - accessors.XMLElementNest('address', self, parent_xpath='/', - tag_name='address', subclass=self.Address, - subclass_dargs={'type_name': 'pci', - 'virsh_instance': virsh_instance}) - accessors.XMLElementDict('target', self, parent_xpath='/', - tag_name='target') - accessors.XMLElementText('node', self, parent_xpath='/target', - tag_name='node') - accessors.XMLElementDict('alias', self, parent_xpath='/', - tag_name='alias') - accessors.XMLElementDict('model_name', self, parent_xpath='/', - tag_name='model') - - Address = librarian.get('address') + accessors.XMLAttribute( + "type", self, parent_xpath="/", tag_name="controller", attribute="type" + ) + accessors.XMLAttribute( + "index", self, parent_xpath="/", tag_name="controller", attribute="index" + ) + accessors.XMLAttribute( + "model", self, parent_xpath="/", tag_name="controller", attribute="model" + ) + accessors.XMLAttribute( + "ports", self, parent_xpath="/", tag_name="controller", attribute="ports" + ) + accessors.XMLAttribute( + "vectors", + self, + parent_xpath="/", + tag_name="controller", + attribute="vectors", + ) + accessors.XMLElementText( + "pcihole64", self, parent_xpath="/", tag_name="pcihole64" + ) + accessors.XMLElementDict("driver", self, parent_xpath="/", tag_name="driver") + accessors.XMLElementNest( + "address", + self, + parent_xpath="/", + tag_name="address", + subclass=self.Address, + subclass_dargs={"type_name": "pci", "virsh_instance": virsh_instance}, + ) + accessors.XMLElementDict("target", self, parent_xpath="/", tag_name="target") + accessors.XMLElementText("node", self, parent_xpath="/target", tag_name="node") + accessors.XMLElementDict("alias", self, parent_xpath="/", tag_name="alias") + accessors.XMLElementDict("model_name", self, parent_xpath="/", tag_name="model") + + Address = librarian.get("address") def new_controller_address(self, **dargs): """ diff --git a/virttest/libvirt_xml/devices/disk.py b/virttest/libvirt_xml/devices/disk.py index 0630c3cab8..a0afe501d2 100644 --- a/virttest/libvirt_xml/devices/disk.py +++ b/virttest/libvirt_xml/devices/disk.py @@ -72,98 +72,162 @@ class Disk(base.TypedDeviceBase): libvirt_xml.devices.Disk.Reservations instance. drivermetadata: libvirt_xml.devices.Disk.DriverMetadata instance. - """ - - __slots__ = ('device', 'rawio', 'sgio', 'snapshot', 'driver', 'target', 'alias', - 'address', 'boot', 'loadparm', 'readonly', 'transient', 'share', 'model', - 'mirror', 'ready', 'iotune', 'source', 'blockio', 'geometry', - 'wwn', 'serial', 'vendor', 'product', 'encryption', 'auth', - 'reservations', 'backingstore', 'driver_metadatacache', 'sharebacking') - - def __init__(self, type_name='file', virsh_instance=base.base.virsh): - accessors.XMLAttribute('device', self, parent_xpath='/', - tag_name='disk', attribute='device') - accessors.XMLAttribute('model', self, parent_xpath='/', - tag_name='disk', attribute='model') - accessors.XMLAttribute('rawio', self, parent_xpath='/', - tag_name='disk', attribute='rawio') - accessors.XMLAttribute('sharebacking', self, parent_xpath='/', - tag_name='transient', attribute='shareBacking') - accessors.XMLAttribute('sgio', self, parent_xpath='/', - tag_name='disk', attribute='sgio') - accessors.XMLAttribute('snapshot', self, parent_xpath='/', - tag_name='disk', attribute='snapshot') - accessors.XMLElementText('wwn', self, parent_xpath='/', - tag_name='wwn') - accessors.XMLElementText('serial', self, parent_xpath='/', - tag_name='serial') - accessors.XMLElementText('vendor', self, parent_xpath='/', - tag_name='vendor') - accessors.XMLElementText('product', self, parent_xpath='/', - tag_name='product') - accessors.XMLElementDict('driver', self, parent_xpath='/', - tag_name='driver') - accessors.XMLElementNest('driver_metadatacache', self, - parent_xpath='/driver', - tag_name='metadata_cache', - subclass=self.MetadataCache, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementDict('target', self, parent_xpath='/', - tag_name='target') - accessors.XMLElementDict('alias', self, parent_xpath='/', - tag_name='alias') - accessors.XMLElementDict('blockio', self, parent_xpath='/', - tag_name='blockio') - accessors.XMLElementDict('geometry', self, parent_xpath='/', - tag_name='geometry') - accessors.XMLElementNest('address', self, parent_xpath='/', - tag_name='address', subclass=self.Address, - subclass_dargs={'type_name': 'drive', - 'virsh_instance': virsh_instance}) - accessors.XMLAttribute('boot', self, parent_xpath='/', - tag_name='boot', attribute='order') - accessors.XMLAttribute('loadparm', self, parent_xpath='/', - tag_name='boot', attribute='loadparm') - accessors.XMLElementBool('readonly', self, parent_xpath='/', - tag_name='readonly') - accessors.XMLElementBool('transient', self, parent_xpath='/', - tag_name='transient') - accessors.XMLElementBool('share', self, parent_xpath='/', - tag_name='shareable') - accessors.XMLElementNest('source', self, parent_xpath='/', - tag_name='source', subclass=self.DiskSource, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - ro = ['set', 'del'] - accessors.XMLElementBool('mirror', self, forbidden=ro, - parent_xpath='/', tag_name='mirror') - accessors.XMLElementBool('ready', self, forbidden=ro, - parent_xpath='/', tag_name='ready') - accessors.XMLElementNest('iotune', self, parent_xpath='/', - tag_name='iotune', subclass=self.IOTune, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest('encryption', self, parent_xpath='/', - tag_name='encryption', subclass=self.Encryption, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest('auth', self, parent_xpath='/', - tag_name='auth', subclass=self.Auth, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest('reservations', self, parent_xpath='/', - tag_name='reservations', - subclass=Disk.Reservations, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest('backingstore', self, parent_xpath='/', - tag_name='backingStore', - subclass=self.BackingStore, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - super(Disk, self).__init__(device_tag='disk', type_name=type_name, - virsh_instance=virsh_instance) + """ + + __slots__ = ( + "device", + "rawio", + "sgio", + "snapshot", + "driver", + "target", + "alias", + "address", + "boot", + "loadparm", + "readonly", + "transient", + "share", + "model", + "mirror", + "ready", + "iotune", + "source", + "blockio", + "geometry", + "wwn", + "serial", + "vendor", + "product", + "encryption", + "auth", + "reservations", + "backingstore", + "driver_metadatacache", + "sharebacking", + ) + + def __init__(self, type_name="file", virsh_instance=base.base.virsh): + accessors.XMLAttribute( + "device", self, parent_xpath="/", tag_name="disk", attribute="device" + ) + accessors.XMLAttribute( + "model", self, parent_xpath="/", tag_name="disk", attribute="model" + ) + accessors.XMLAttribute( + "rawio", self, parent_xpath="/", tag_name="disk", attribute="rawio" + ) + accessors.XMLAttribute( + "sharebacking", + self, + parent_xpath="/", + tag_name="transient", + attribute="shareBacking", + ) + accessors.XMLAttribute( + "sgio", self, parent_xpath="/", tag_name="disk", attribute="sgio" + ) + accessors.XMLAttribute( + "snapshot", self, parent_xpath="/", tag_name="disk", attribute="snapshot" + ) + accessors.XMLElementText("wwn", self, parent_xpath="/", tag_name="wwn") + accessors.XMLElementText("serial", self, parent_xpath="/", tag_name="serial") + accessors.XMLElementText("vendor", self, parent_xpath="/", tag_name="vendor") + accessors.XMLElementText("product", self, parent_xpath="/", tag_name="product") + accessors.XMLElementDict("driver", self, parent_xpath="/", tag_name="driver") + accessors.XMLElementNest( + "driver_metadatacache", + self, + parent_xpath="/driver", + tag_name="metadata_cache", + subclass=self.MetadataCache, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementDict("target", self, parent_xpath="/", tag_name="target") + accessors.XMLElementDict("alias", self, parent_xpath="/", tag_name="alias") + accessors.XMLElementDict("blockio", self, parent_xpath="/", tag_name="blockio") + accessors.XMLElementDict( + "geometry", self, parent_xpath="/", tag_name="geometry" + ) + accessors.XMLElementNest( + "address", + self, + parent_xpath="/", + tag_name="address", + subclass=self.Address, + subclass_dargs={"type_name": "drive", "virsh_instance": virsh_instance}, + ) + accessors.XMLAttribute( + "boot", self, parent_xpath="/", tag_name="boot", attribute="order" + ) + accessors.XMLAttribute( + "loadparm", self, parent_xpath="/", tag_name="boot", attribute="loadparm" + ) + accessors.XMLElementBool( + "readonly", self, parent_xpath="/", tag_name="readonly" + ) + accessors.XMLElementBool( + "transient", self, parent_xpath="/", tag_name="transient" + ) + accessors.XMLElementBool("share", self, parent_xpath="/", tag_name="shareable") + accessors.XMLElementNest( + "source", + self, + parent_xpath="/", + tag_name="source", + subclass=self.DiskSource, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + ro = ["set", "del"] + accessors.XMLElementBool( + "mirror", self, forbidden=ro, parent_xpath="/", tag_name="mirror" + ) + accessors.XMLElementBool( + "ready", self, forbidden=ro, parent_xpath="/", tag_name="ready" + ) + accessors.XMLElementNest( + "iotune", + self, + parent_xpath="/", + tag_name="iotune", + subclass=self.IOTune, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "encryption", + self, + parent_xpath="/", + tag_name="encryption", + subclass=self.Encryption, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "auth", + self, + parent_xpath="/", + tag_name="auth", + subclass=self.Auth, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "reservations", + self, + parent_xpath="/", + tag_name="reservations", + subclass=Disk.Reservations, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "backingstore", + self, + parent_xpath="/", + tag_name="backingStore", + subclass=self.BackingStore, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + super(Disk, self).__init__( + device_tag="disk", type_name=type_name, virsh_instance=virsh_instance + ) def new_disk_source(self, **dargs): """ @@ -192,7 +256,7 @@ def new_encryption(self, **dargs): setattr(new_one, key, value) return new_one - def new_disk_address(self, type_name='drive', **dargs): + def new_disk_address(self, type_name="drive", **dargs): """ Return a new disk Address instance and set properties from dargs """ @@ -247,7 +311,7 @@ def get_backingstore_list(self): :return: a disk backingstore list where each element is primitive virttest.element_tree._ElementInterface object """ backingstore_list = [] - for elem in self.xmltreefile.iter('backingStore'): + for elem in self.xmltreefile.iter("backingStore"): backingstore_list.append(elem) return backingstore_list @@ -264,7 +328,7 @@ def _get_next_backingstore(elem): :param elem: root element to get backingstore object """ - if elem.xmltreefile.find('/backingStore') is None: + if elem.xmltreefile.find("/backingStore") is None: return bs = elem.backingstore backingstore_list.append(bs) @@ -276,7 +340,7 @@ def _get_next_backingstore(elem): return backingstore_list # For convenience - Address = librarian.get('address') + Address = librarian.get("address") class DiskSource(base.base.LibvirtXMLBase): @@ -290,66 +354,108 @@ class DiskSource(base.base.LibvirtXMLBase): hosts: list of dictionaries describing network host properties """ - __slots__ = ('attrs', 'seclabels', 'hosts', 'encryption', 'auth', - 'reservations', 'slices', 'config_file', 'snapshot_name', 'address') + __slots__ = ( + "attrs", + "seclabels", + "hosts", + "encryption", + "auth", + "reservations", + "slices", + "config_file", + "snapshot_name", + "address", + ) def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLElementDict('attrs', self, parent_xpath='/', - tag_name='source') - accessors.XMLElementList('seclabels', self, parent_xpath='/', - marshal_from=self.marshal_from_seclabel, - marshal_to=self.marshal_to_seclabel, - has_subclass=True) - accessors.XMLElementList('hosts', self, parent_xpath='/', - marshal_from=self.marshal_from_host, - marshal_to=self.marshal_to_host) - accessors.XMLElementNest('encryption', self, parent_xpath='/', - tag_name='encryption', - subclass=Disk.Encryption, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest('auth', self, parent_xpath='/', - tag_name='auth', subclass=Disk.Auth, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest('reservations', self, parent_xpath='/', - tag_name='reservations', - subclass=Disk.Reservations, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest('slices', self, parent_xpath='/', - tag_name='slices', - subclass=Disk.Slices, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLAttribute('config_file', self, parent_xpath='/', - tag_name='config', attribute='file') - accessors.XMLAttribute('snapshot_name', self, parent_xpath='/', - tag_name='snapshot', attribute='name') - accessors.XMLElementDict('address', self, parent_xpath='/', - tag_name='address') + accessors.XMLElementDict("attrs", self, parent_xpath="/", tag_name="source") + accessors.XMLElementList( + "seclabels", + self, + parent_xpath="/", + marshal_from=self.marshal_from_seclabel, + marshal_to=self.marshal_to_seclabel, + has_subclass=True, + ) + accessors.XMLElementList( + "hosts", + self, + parent_xpath="/", + marshal_from=self.marshal_from_host, + marshal_to=self.marshal_to_host, + ) + accessors.XMLElementNest( + "encryption", + self, + parent_xpath="/", + tag_name="encryption", + subclass=Disk.Encryption, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "auth", + self, + parent_xpath="/", + tag_name="auth", + subclass=Disk.Auth, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "reservations", + self, + parent_xpath="/", + tag_name="reservations", + subclass=Disk.Reservations, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "slices", + self, + parent_xpath="/", + tag_name="slices", + subclass=Disk.Slices, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLAttribute( + "config_file", + self, + parent_xpath="/", + tag_name="config", + attribute="file", + ) + accessors.XMLAttribute( + "snapshot_name", + self, + parent_xpath="/", + tag_name="snapshot", + attribute="name", + ) + accessors.XMLElementDict( + "address", self, parent_xpath="/", tag_name="address" + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" @staticmethod def marshal_from_seclabel(item, index, libvirtxml): """Convert a Seclabel instance into tag + attributes""" if isinstance(item, Seclabel): - return 'seclabel', item + return "seclabel", item elif isinstance(item, dict): seclabel = Seclabel() seclabel.setup_attrs(**item) - return 'seclabel', seclabel + return "seclabel", seclabel else: - raise xcepts.LibvirtXMLError("Expected a list of seclabel " - "instances, not a %s" % str(item)) + raise xcepts.LibvirtXMLError( + "Expected a list of seclabel " "instances, not a %s" % str(item) + ) @staticmethod def marshal_to_seclabel(tag, new_treefile, index, libvirtxml): """Convert a tag + attributes into a Seclabel instance""" - del index # not used - if tag != 'seclabel': - return None # Don't convert this item + del index # not used + if tag != "seclabel": + return None # Don't convert this item newone = Seclabel(virsh_instance=libvirtxml.virsh) newone.xmltreefile = new_treefile return newone @@ -357,22 +463,22 @@ def marshal_to_seclabel(tag, new_treefile, index, libvirtxml): @staticmethod def marshal_from_host(item, index, libvirtxml): """Convert a dictionary into a tag + attributes""" - del index # not used - del libvirtxml # not used + del index # not used + del libvirtxml # not used if not isinstance(item, dict): - raise xcepts.LibvirtXMLError("Expected a dictionary of host " - "attributes, not a %s" - % str(item)) - return ('host', dict(item)) # return copy of dict, not reference + raise xcepts.LibvirtXMLError( + "Expected a dictionary of host " "attributes, not a %s" % str(item) + ) + return ("host", dict(item)) # return copy of dict, not reference @staticmethod def marshal_to_host(tag, attr_dict, index, libvirtxml): """Convert a tag + attributes into a dictionary""" - del index # not used - del libvirtxml # not used - if tag != 'host': - return None # skip this one - return dict(attr_dict) # return copy of dict, not reference + del index # not used + del libvirtxml # not used + if tag != "host": + return None # skip this one + return dict(attr_dict) # return copy of dict, not reference class IOTune(base.base.LibvirtXMLBase): @@ -389,19 +495,24 @@ class IOTune(base.base.LibvirtXMLBase): write_iops_sec: str(int) """ - __slots__ = ('total_bytes_sec', 'read_bytes_sec', 'write_bytes_sec', - 'total_iops_sec', 'read_iops_sec', 'write_iops_sec') + __slots__ = ( + "total_bytes_sec", + "read_bytes_sec", + "write_bytes_sec", + "total_iops_sec", + "read_iops_sec", + "write_iops_sec", + ) def __init__(self, virsh_instance=base.base.virsh): # pylint: disable=E1133,E1135 for slot in self.__all_slots__: if slot in base.base.LibvirtXMLBase.__all_slots__: - continue # don't add these + continue # don't add these else: - accessors.XMLElementInt(slot, self, parent_xpath='/', - tag_name=slot) + accessors.XMLElementInt(slot, self, parent_xpath="/", tag_name=slot) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" class Encryption(base.base.LibvirtXMLBase): @@ -416,16 +527,21 @@ class Encryption(base.base.LibvirtXMLBase): dict, keys: type, uuid """ - __slots__ = ('encryption', 'secret') + __slots__ = ("encryption", "secret") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute('encryption', self, parent_xpath='/', - tag_name='encryption', attribute='format') - accessors.XMLElementDict('secret', self, parent_xpath='/', - tag_name='secret') - super(self.__class__, self).__init__( - virsh_instance=virsh_instance) - self.xml = '' + accessors.XMLAttribute( + "encryption", + self, + parent_xpath="/", + tag_name="encryption", + attribute="format", + ) + accessors.XMLElementDict( + "secret", self, parent_xpath="/", tag_name="secret" + ) + super(self.__class__, self).__init__(virsh_instance=virsh_instance) + self.xml = "" class Auth(base.base.LibvirtXMLBase): @@ -444,19 +560,39 @@ class Auth(base.base.LibvirtXMLBase): string, attribute of secret tag, sub-tag of the auth tag """ - __slots__ = ('auth_user', 'secret_type', 'secret_uuid', 'secret_usage') + __slots__ = ("auth_user", "secret_type", "secret_uuid", "secret_usage") def __init__(self, virsh_instance=base.base.virsh, auth_user=""): - accessors.XMLAttribute('auth_user', self, parent_xpath='/', - tag_name='auth', attribute='username') - accessors.XMLAttribute('secret_type', self, parent_xpath='/', - tag_name='secret', attribute='type') - accessors.XMLAttribute('secret_uuid', self, parent_xpath='/', - tag_name='secret', attribute='uuid') - accessors.XMLAttribute('secret_usage', self, parent_xpath='/', - tag_name='secret', attribute='usage') + accessors.XMLAttribute( + "auth_user", + self, + parent_xpath="/", + tag_name="auth", + attribute="username", + ) + accessors.XMLAttribute( + "secret_type", + self, + parent_xpath="/", + tag_name="secret", + attribute="type", + ) + accessors.XMLAttribute( + "secret_uuid", + self, + parent_xpath="/", + tag_name="secret", + attribute="uuid", + ) + accessors.XMLAttribute( + "secret_usage", + self, + parent_xpath="/", + tag_name="secret", + attribute="usage", + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" class Slices(base.base.LibvirtXMLBase): @@ -476,17 +612,24 @@ class Slices(base.base.LibvirtXMLBase): string, size attribute of slice tag """ - __slots__ = ('slice_type', 'slice_offset', 'slice_size') + __slots__ = ("slice_type", "slice_offset", "slice_size") def __init__(self, virsh_instance=base.base.virsh, auth_user=""): - accessors.XMLAttribute('slice_type', self, parent_xpath='/', - tag_name='slice', attribute='type') - accessors.XMLAttribute('slice_offset', self, parent_xpath='/', - tag_name='slice', attribute='offset') - accessors.XMLAttribute('slice_size', self, parent_xpath='/', - tag_name='slice', attribute='size') + accessors.XMLAttribute( + "slice_type", self, parent_xpath="/", tag_name="slice", attribute="type" + ) + accessors.XMLAttribute( + "slice_offset", + self, + parent_xpath="/", + tag_name="slice", + attribute="offset", + ) + accessors.XMLAttribute( + "slice_size", self, parent_xpath="/", tag_name="slice", attribute="size" + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" class Reservations(base.base.LibvirtXMLBase): @@ -505,20 +648,44 @@ class Reservations(base.base.LibvirtXMLBase): string, attribute of source tag, sub-tag of the reservations tag """ - __slots__ = ('reservations_managed', 'reservations_source_type', - 'reservations_source_path', 'reservations_source_mode') + __slots__ = ( + "reservations_managed", + "reservations_source_type", + "reservations_source_path", + "reservations_source_mode", + ) def __init__(self, virsh_instance=base.base.virsh, reservations_managed=""): - accessors.XMLAttribute('reservations_managed', self, parent_xpath='/', - tag_name='reservations', attribute='managed') - accessors.XMLAttribute('reservations_source_type', self, parent_xpath='/', - tag_name='source', attribute='type') - accessors.XMLAttribute('reservations_source_path', self, parent_xpath='/', - tag_name='source', attribute='path') - accessors.XMLAttribute('reservations_source_mode', self, parent_xpath='/', - tag_name='source', attribute='mode') + accessors.XMLAttribute( + "reservations_managed", + self, + parent_xpath="/", + tag_name="reservations", + attribute="managed", + ) + accessors.XMLAttribute( + "reservations_source_type", + self, + parent_xpath="/", + tag_name="source", + attribute="type", + ) + accessors.XMLAttribute( + "reservations_source_path", + self, + parent_xpath="/", + tag_name="source", + attribute="path", + ) + accessors.XMLAttribute( + "reservations_source_mode", + self, + parent_xpath="/", + tag_name="source", + attribute="mode", + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" class BackingStore(base.base.LibvirtXMLBase): """ @@ -533,34 +700,46 @@ class BackingStore(base.base.LibvirtXMLBase): source: nested xml of backingStore tag """ - __slots__ = ('type', 'index', 'format', 'source', 'backingstore') + + __slots__ = ("type", "index", "format", "source", "backingstore") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute('type', self, - parent_xpath='/', - tag_name='backingStore', - attribute='type') - accessors.XMLAttribute('index', self, - parent_xpath='/', - tag_name='backingStore', - attribute='index') - accessors.XMLElementDict('format', self, - parent_xpath='/', - tag_name='format') - accessors.XMLElementNest('source', self, - parent_xpath='/', - tag_name='source', - subclass=self.Source, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest('backingstore', self, parent_xpath='/', - tag_name='backingStore', - subclass=Disk.BackingStore, - subclass_dargs={ - 'virsh_instance': virsh_instance}) + accessors.XMLAttribute( + "type", + self, + parent_xpath="/", + tag_name="backingStore", + attribute="type", + ) + accessors.XMLAttribute( + "index", + self, + parent_xpath="/", + tag_name="backingStore", + attribute="index", + ) + accessors.XMLElementDict( + "format", self, parent_xpath="/", tag_name="format" + ) + accessors.XMLElementNest( + "source", + self, + parent_xpath="/", + tag_name="source", + subclass=self.Source, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "backingstore", + self, + parent_xpath="/", + tag_name="backingStore", + subclass=Disk.BackingStore, + subclass_dargs={"virsh_instance": virsh_instance}, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" def new_source(self, **dargs): """ @@ -588,40 +767,42 @@ class Source(base.base.LibvirtXMLBase): string, attribute of backingStore/source tag """ - __slots__ = ('attrs', 'dev', 'protocol', 'name', 'host', 'file', 'auth') + __slots__ = ("attrs", "dev", "protocol", "name", "host", "file", "auth") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLElementDict('attrs', self, - parent_xpath='/', - tag_name='source') - accessors.XMLAttribute('dev', self, - parent_xpath='/', - tag_name='source', - attribute='dev') - accessors.XMLAttribute('protocol', self, - parent_xpath='/', - tag_name='source', - attribute='protocol') - accessors.XMLAttribute('name', self, - parent_xpath='/', - tag_name='source', - attribute='name') - accessors.XMLElementDict('host', self, - parent_xpath='/', - tag_name='host') - accessors.XMLAttribute('file', self, - parent_xpath='/', - tag_name='source', - attribute='file') - accessors.XMLElementNest('auth', self, - parent_xpath='/', - tag_name='auth', - subclass=Disk.Auth, - subclass_dargs={ - 'virsh_instance': virsh_instance}) + accessors.XMLElementDict( + "attrs", self, parent_xpath="/", tag_name="source" + ) + accessors.XMLAttribute( + "dev", self, parent_xpath="/", tag_name="source", attribute="dev" + ) + accessors.XMLAttribute( + "protocol", + self, + parent_xpath="/", + tag_name="source", + attribute="protocol", + ) + accessors.XMLAttribute( + "name", self, parent_xpath="/", tag_name="source", attribute="name" + ) + accessors.XMLElementDict( + "host", self, parent_xpath="/", tag_name="host" + ) + accessors.XMLAttribute( + "file", self, parent_xpath="/", tag_name="source", attribute="file" + ) + accessors.XMLElementNest( + "auth", + self, + parent_xpath="/", + tag_name="auth", + subclass=Disk.Auth, + subclass_dargs={"virsh_instance": virsh_instance}, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" class MetadataCache(base.base.LibvirtXMLBase): """ @@ -633,17 +814,21 @@ class MetadataCache(base.base.LibvirtXMLBase): string, attribute of MetadataCache max size unit """ - __slots__ = ('max_size', 'max_size_unit') + __slots__ = ("max_size", "max_size_unit") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLElementInt(property_name='max_size', - libvirtxml=self, - parent_xpath='/', - tag_name='max_size') - accessors.XMLAttribute(property_name="max_size_unit", - libvirtxml=self, - parent_xpath='/', - tag_name='max_size', - attribute='unit') + accessors.XMLElementInt( + property_name="max_size", + libvirtxml=self, + parent_xpath="/", + tag_name="max_size", + ) + accessors.XMLAttribute( + property_name="max_size_unit", + libvirtxml=self, + parent_xpath="/", + tag_name="max_size", + attribute="unit", + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" diff --git a/virttest/libvirt_xml/devices/emulator.py b/virttest/libvirt_xml/devices/emulator.py index 6eea2d8900..d8e417105c 100644 --- a/virttest/libvirt_xml/devices/emulator.py +++ b/virttest/libvirt_xml/devices/emulator.py @@ -10,10 +10,10 @@ class Emulator(base.UntypedDeviceBase): - __slots__ = ('path',) + __slots__ = ("path",) def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLElementText('path', self, parent_xpath='/', - tag_name='emulator') - super(Emulator, self).__init__(device_tag='emulator', - virsh_instance=virsh_instance) + accessors.XMLElementText("path", self, parent_xpath="/", tag_name="emulator") + super(Emulator, self).__init__( + device_tag="emulator", virsh_instance=virsh_instance + ) diff --git a/virttest/libvirt_xml/devices/filesystem.py b/virttest/libvirt_xml/devices/filesystem.py index 7d6ce80197..f3acef972b 100644 --- a/virttest/libvirt_xml/devices/filesystem.py +++ b/virttest/libvirt_xml/devices/filesystem.py @@ -10,30 +10,34 @@ class Filesystem(base.TypedDeviceBase): - __slots__ = ('accessmode', 'source', 'target', 'driver', 'binary', 'alias', 'boot') + __slots__ = ("accessmode", "source", "target", "driver", "binary", "alias", "boot") - def __init__(self, type_name='mount', virsh_instance=base.base.virsh): - accessors.XMLAttribute('accessmode', self, parent_xpath='/', - tag_name='filesystem', attribute='accessmode') - accessors.XMLElementDict('source', self, parent_xpath='/', - tag_name='source') - accessors.XMLElementDict('target', self, parent_xpath='/', - tag_name='target') - accessors.XMLElementDict('driver', self, - parent_xpath='/', - tag_name='driver') - accessors.XMLElementNest('binary', self, parent_xpath='/', - tag_name='binary', subclass=self.Binary, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementDict('alias', self, - parent_xpath='/', - tag_name='alias') - accessors.XMLAttribute('boot', self, parent_xpath='/', - tag_name='boot', attribute='order') - super(Filesystem, self).__init__(device_tag='filesystem', - type_name=type_name, - virsh_instance=virsh_instance) + def __init__(self, type_name="mount", virsh_instance=base.base.virsh): + accessors.XMLAttribute( + "accessmode", + self, + parent_xpath="/", + tag_name="filesystem", + attribute="accessmode", + ) + accessors.XMLElementDict("source", self, parent_xpath="/", tag_name="source") + accessors.XMLElementDict("target", self, parent_xpath="/", tag_name="target") + accessors.XMLElementDict("driver", self, parent_xpath="/", tag_name="driver") + accessors.XMLElementNest( + "binary", + self, + parent_xpath="/", + tag_name="binary", + subclass=self.Binary, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementDict("alias", self, parent_xpath="/", tag_name="alias") + accessors.XMLAttribute( + "boot", self, parent_xpath="/", tag_name="boot", attribute="order" + ) + super(Filesystem, self).__init__( + device_tag="filesystem", type_name=type_name, virsh_instance=virsh_instance + ) class Binary(base.base.LibvirtXMLBase): @@ -46,33 +50,38 @@ class Binary(base.base.LibvirtXMLBase): """ - __slots__ = ('path', 'xattr', 'cache_mode', 'lock_posix', - 'flock', 'thread_pool_size') + + __slots__ = ( + "path", + "xattr", + "cache_mode", + "lock_posix", + "flock", + "thread_pool_size", + ) def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute('path', self, - parent_xpath='/', - tag_name='binary', - attribute='path') - accessors.XMLAttribute('xattr', self, - parent_xpath='/', - tag_name='binary', - attribute='xattr') - accessors.XMLAttribute('cache_mode', self, - parent_xpath='/', - tag_name='cache', - attribute='mode') - accessors.XMLAttribute('lock_posix', self, - parent_xpath='/', - tag_name='lock', - attribute='posix') - accessors.XMLAttribute('flock', self, - parent_xpath='/', - tag_name='lock', - attribute='flock') - accessors.XMLAttribute('thread_pool_size', self, - parent_xpath='/', - tag_name='thread_pool', - attribute='size') + accessors.XMLAttribute( + "path", self, parent_xpath="/", tag_name="binary", attribute="path" + ) + accessors.XMLAttribute( + "xattr", self, parent_xpath="/", tag_name="binary", attribute="xattr" + ) + accessors.XMLAttribute( + "cache_mode", self, parent_xpath="/", tag_name="cache", attribute="mode" + ) + accessors.XMLAttribute( + "lock_posix", self, parent_xpath="/", tag_name="lock", attribute="posix" + ) + accessors.XMLAttribute( + "flock", self, parent_xpath="/", tag_name="lock", attribute="flock" + ) + accessors.XMLAttribute( + "thread_pool_size", + self, + parent_xpath="/", + tag_name="thread_pool", + attribute="size", + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" diff --git a/virttest/libvirt_xml/devices/filterref.py b/virttest/libvirt_xml/devices/filterref.py index b957aa9670..d7832b3f85 100644 --- a/virttest/libvirt_xml/devices/filterref.py +++ b/virttest/libvirt_xml/devices/filterref.py @@ -19,40 +19,45 @@ class Filterref(base.base.LibvirtXMLBase): parameters: list. parameters element dict list """ + __slots__ = ("name", "parameters") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute(property_name="name", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='filterref', - attribute='filter') - accessors.XMLElementList(property_name='parameters', - libvirtxml=self, - parent_xpath='/', - marshal_from=self.marshal_from_parameter, - marshal_to=self.marshal_to_parameter) + accessors.XMLAttribute( + property_name="name", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="filterref", + attribute="filter", + ) + accessors.XMLElementList( + property_name="parameters", + libvirtxml=self, + parent_xpath="/", + marshal_from=self.marshal_from_parameter, + marshal_to=self.marshal_to_parameter, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" @staticmethod def marshal_from_parameter(item, index, libvirtxml): """Convert a dictionary into a tag + attributes""" - del index # not used - del libvirtxml # not used + del index # not used + del libvirtxml # not used if not isinstance(item, dict): - raise xcepts.LibvirtXMLError("Expected a dictionary of parameter " - "attributes, not a %s" - % str(item)) + raise xcepts.LibvirtXMLError( + "Expected a dictionary of parameter " "attributes, not a %s" % str(item) + ) # return copy of dict, not reference - return ('parameter', dict(item)) + return ("parameter", dict(item)) @staticmethod def marshal_to_parameter(tag, attr_dict, index, libvirtxml): """Convert a tag + attributes into a dictionary""" - del index # not used - del libvirtxml # not used - if tag != 'parameter': - return None # skip this one - return dict(attr_dict) # return copy of dict, not reference@ + del index # not used + del libvirtxml # not used + if tag != "parameter": + return None # skip this one + return dict(attr_dict) # return copy of dict, not reference@ diff --git a/virttest/libvirt_xml/devices/graphics.py b/virttest/libvirt_xml/devices/graphics.py index 6d6ab5a3ad..ae5609db5b 100644 --- a/virttest/libvirt_xml/devices/graphics.py +++ b/virttest/libvirt_xml/devices/graphics.py @@ -12,64 +12,137 @@ class Graphics(base.TypedDeviceBase): - __slots__ = ('passwd', 'channels', 'listen', 'autoport', 'port', - 'tlsPort', 'defaultMode', 'image_compression', - 'jpeg_compression', 'zlib_compression', 'playback_compression', - 'listen_attrs', 'passwdValidTo', 'clipboard_copypaste', - 'filetransfer_enable', 'streaming_mode') + __slots__ = ( + "passwd", + "channels", + "listen", + "autoport", + "port", + "tlsPort", + "defaultMode", + "image_compression", + "jpeg_compression", + "zlib_compression", + "playback_compression", + "listen_attrs", + "passwdValidTo", + "clipboard_copypaste", + "filetransfer_enable", + "streaming_mode", + ) - def __init__(self, type_name='vnc', virsh_instance=base.base.virsh): + def __init__(self, type_name="vnc", virsh_instance=base.base.virsh): # Add additional attribute 'passwd' for security - accessors.XMLAttribute('passwd', self, parent_xpath='/', - tag_name='graphics', attribute='passwd') - accessors.XMLAttribute('passwdValidTo', self, parent_xpath='/', - tag_name='graphics', attribute='passwdValidTo') - accessors.XMLAttribute('listen', self, parent_xpath='/', - tag_name='graphics', attribute='listen') - accessors.XMLAttribute('autoport', self, parent_xpath='/', - tag_name='graphics', attribute='autoport') - accessors.XMLAttribute('port', self, parent_xpath='/', - tag_name='graphics', attribute='port') - accessors.XMLAttribute('tlsPort', self, parent_xpath='/', - tag_name='graphics', attribute='tlsPort') - accessors.XMLAttribute('type', self, parent_xpath='/', - tag_name='graphics', attribute='type') - accessors.XMLAttribute('defaultMode', self, parent_xpath='/', - tag_name='graphics', attribute='defaultMode') - accessors.XMLAttribute('image_compression', self, parent_xpath='/', - tag_name='image', attribute='compression') - accessors.XMLAttribute('jpeg_compression', self, parent_xpath='/', - tag_name='jpeg', attribute='compression') - accessors.XMLAttribute('zlib_compression', self, parent_xpath='/', - tag_name='zlib', attribute='compression') - accessors.XMLAttribute('playback_compression', self, parent_xpath='/', - tag_name='playback', attribute='compression') - accessors.XMLAttribute('clipboard_copypaste', self, parent_xpath='/', - tag_name='clipboard', attribute='copypaste') - accessors.XMLAttribute('filetransfer_enable', self, parent_xpath='/', - tag_name='filetransfer', attribute='enable') - accessors.XMLAttribute('streaming_mode', self, parent_xpath='/', - tag_name='streaming', attribute='mode') - accessors.XMLElementDict('listen_attrs', self, parent_xpath='/', - tag_name='listen') - accessors.XMLElementList('channels', self, parent_xpath='/', - marshal_from=self.marshal_from_channels, - marshal_to=self.marshal_to_channels) - super(Graphics, self).__init__(device_tag='graphics', - type_name=type_name, - virsh_instance=virsh_instance) + accessors.XMLAttribute( + "passwd", self, parent_xpath="/", tag_name="graphics", attribute="passwd" + ) + accessors.XMLAttribute( + "passwdValidTo", + self, + parent_xpath="/", + tag_name="graphics", + attribute="passwdValidTo", + ) + accessors.XMLAttribute( + "listen", self, parent_xpath="/", tag_name="graphics", attribute="listen" + ) + accessors.XMLAttribute( + "autoport", + self, + parent_xpath="/", + tag_name="graphics", + attribute="autoport", + ) + accessors.XMLAttribute( + "port", self, parent_xpath="/", tag_name="graphics", attribute="port" + ) + accessors.XMLAttribute( + "tlsPort", self, parent_xpath="/", tag_name="graphics", attribute="tlsPort" + ) + accessors.XMLAttribute( + "type", self, parent_xpath="/", tag_name="graphics", attribute="type" + ) + accessors.XMLAttribute( + "defaultMode", + self, + parent_xpath="/", + tag_name="graphics", + attribute="defaultMode", + ) + accessors.XMLAttribute( + "image_compression", + self, + parent_xpath="/", + tag_name="image", + attribute="compression", + ) + accessors.XMLAttribute( + "jpeg_compression", + self, + parent_xpath="/", + tag_name="jpeg", + attribute="compression", + ) + accessors.XMLAttribute( + "zlib_compression", + self, + parent_xpath="/", + tag_name="zlib", + attribute="compression", + ) + accessors.XMLAttribute( + "playback_compression", + self, + parent_xpath="/", + tag_name="playback", + attribute="compression", + ) + accessors.XMLAttribute( + "clipboard_copypaste", + self, + parent_xpath="/", + tag_name="clipboard", + attribute="copypaste", + ) + accessors.XMLAttribute( + "filetransfer_enable", + self, + parent_xpath="/", + tag_name="filetransfer", + attribute="enable", + ) + accessors.XMLAttribute( + "streaming_mode", + self, + parent_xpath="/", + tag_name="streaming", + attribute="mode", + ) + accessors.XMLElementDict( + "listen_attrs", self, parent_xpath="/", tag_name="listen" + ) + accessors.XMLElementList( + "channels", + self, + parent_xpath="/", + marshal_from=self.marshal_from_channels, + marshal_to=self.marshal_to_channels, + ) + super(Graphics, self).__init__( + device_tag="graphics", type_name=type_name, virsh_instance=virsh_instance + ) @staticmethod def marshal_from_channels(item, index, libvirtxml): if not isinstance(item, dict): - raise xcepts.LibvirtXMLError("Expected a dictionary of channel " - "attributes, not a %s" - % str(item)) - return 'channel', dict(item) + raise xcepts.LibvirtXMLError( + "Expected a dictionary of channel " "attributes, not a %s" % str(item) + ) + return "channel", dict(item) @staticmethod def marshal_to_channels(tag, attr_dict, index, libvirtxml): - if tag != 'channel': + if tag != "channel": return None return dict(attr_dict) @@ -92,7 +165,7 @@ def change_graphic_type_passwd(vm_name, graphic, passwd=None): """ vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) devices = vmxml.devices - graphics = devices.by_device_tag('graphics')[0] + graphics = devices.by_device_tag("graphics")[0] graphics.type_name = graphic if passwd is not None: graphics.passwd = passwd @@ -100,8 +173,7 @@ def change_graphic_type_passwd(vm_name, graphic, passwd=None): vmxml.sync() @staticmethod - def add_graphic(vm_name, passwd=None, graphic="vnc", - add_channel=False): + def add_graphic(vm_name, passwd=None, graphic="vnc", add_channel=False): """ Add spice ssl or vnc graphic with passwd @@ -111,13 +183,13 @@ def add_graphic(vm_name, passwd=None, graphic="vnc", :param add_channel: add channel for spice """ vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) - grap = vmxml.get_device_class('graphics')(type_name=graphic) + grap = vmxml.get_device_class("graphics")(type_name=graphic) if passwd is not None: grap.passwd = passwd grap.autoport = "yes" if graphic == "spice" and add_channel: - grap.add_channel(name='main', mode='secure') - grap.add_channel(name='inputs', mode='secure') + grap.add_channel(name="main", mode="secure") + grap.add_channel(name="inputs", mode="secure") vmxml.devices = vmxml.devices.append(grap) vmxml.sync() @@ -129,6 +201,6 @@ def del_graphic(vm_name): :param vm_name: name of vm """ vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) - vmxml.xmltreefile.remove_by_xpath('/devices/graphics', remove_all=True) + vmxml.xmltreefile.remove_by_xpath("/devices/graphics", remove_all=True) vmxml.xmltreefile.write() vmxml.sync() diff --git a/virttest/libvirt_xml/devices/hostdev.py b/virttest/libvirt_xml/devices/hostdev.py index 0053913a05..cbcca4ef47 100644 --- a/virttest/libvirt_xml/devices/hostdev.py +++ b/virttest/libvirt_xml/devices/hostdev.py @@ -10,59 +10,86 @@ class Hostdev(base.TypedDeviceBase): - __slots__ = ('type', 'mode', 'managed', 'sgio', 'rawio', - 'source', 'boot_order', 'readonly', 'shareable', - 'alias', 'model', 'teaming', 'rom', 'address') + __slots__ = ( + "type", + "mode", + "managed", + "sgio", + "rawio", + "source", + "boot_order", + "readonly", + "shareable", + "alias", + "model", + "teaming", + "rom", + "address", + ) def __init__(self, type_name="hostdev", virsh_instance=base.base.virsh): - accessors.XMLAttribute('type', self, parent_xpath='/', - tag_name='hostdev', attribute='type') - accessors.XMLAttribute('mode', self, parent_xpath='/', - tag_name='hostdev', attribute='mode') - accessors.XMLAttribute('model', self, parent_xpath='/', - tag_name='hostdev', attribute='model') - accessors.XMLAttribute('managed', self, parent_xpath='/', - tag_name='hostdev', attribute='managed') - accessors.XMLAttribute('sgio', self, parent_xpath='/', - tag_name='hostdev', attribute='sgio') - accessors.XMLAttribute('rawio', self, parent_xpath='/', - tag_name='hostdev', attribute='rawio') - accessors.XMLElementNest('source', self, parent_xpath='/', - tag_name='source', subclass=self.Source, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLAttribute('boot_order', self, parent_xpath='/', - tag_name='boot', attribute='order') - accessors.XMLElementBool('readonly', self, parent_xpath='/', - tag_name='readonly') - accessors.XMLElementBool('shareable', self, parent_xpath='/', - tag_name='shareable') - accessors.XMLElementDict('alias', self, parent_xpath='/', - tag_name='alias') - accessors.XMLElementDict('rom', self, parent_xpath='/', - tag_name='rom') - accessors.XMLElementNest('address', self, parent_xpath='/', - tag_name='address', subclass=self.Address, - subclass_dargs={'type_name': 'drive', - 'virsh_instance': virsh_instance}) - accessors.XMLElementDict("teaming", self, parent_xpath='/', - tag_name='teaming') - super(self.__class__, self).__init__(device_tag='hostdev', - type_name=type_name, - virsh_instance=virsh_instance) - - Address = librarian.get('address') + accessors.XMLAttribute( + "type", self, parent_xpath="/", tag_name="hostdev", attribute="type" + ) + accessors.XMLAttribute( + "mode", self, parent_xpath="/", tag_name="hostdev", attribute="mode" + ) + accessors.XMLAttribute( + "model", self, parent_xpath="/", tag_name="hostdev", attribute="model" + ) + accessors.XMLAttribute( + "managed", self, parent_xpath="/", tag_name="hostdev", attribute="managed" + ) + accessors.XMLAttribute( + "sgio", self, parent_xpath="/", tag_name="hostdev", attribute="sgio" + ) + accessors.XMLAttribute( + "rawio", self, parent_xpath="/", tag_name="hostdev", attribute="rawio" + ) + accessors.XMLElementNest( + "source", + self, + parent_xpath="/", + tag_name="source", + subclass=self.Source, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLAttribute( + "boot_order", self, parent_xpath="/", tag_name="boot", attribute="order" + ) + accessors.XMLElementBool( + "readonly", self, parent_xpath="/", tag_name="readonly" + ) + accessors.XMLElementBool( + "shareable", self, parent_xpath="/", tag_name="shareable" + ) + accessors.XMLElementDict("alias", self, parent_xpath="/", tag_name="alias") + accessors.XMLElementDict("rom", self, parent_xpath="/", tag_name="rom") + accessors.XMLElementNest( + "address", + self, + parent_xpath="/", + tag_name="address", + subclass=self.Address, + subclass_dargs={"type_name": "drive", "virsh_instance": virsh_instance}, + ) + accessors.XMLElementDict("teaming", self, parent_xpath="/", tag_name="teaming") + super(self.__class__, self).__init__( + device_tag="hostdev", type_name=type_name, virsh_instance=virsh_instance + ) + + Address = librarian.get("address") def new_source(self, **dargs): new_one = self.Source(virsh_instance=self.virsh) - if self.type == 'pci': + if self.type == "pci": pass - elif self.type == 'usb': + elif self.type == "usb": new_one.vendor_id = dargs.pop("vendor_id", None) new_one.product_id = dargs.pop("product_id", None) new_one.address_bus = dargs.pop("address_bus", None) new_one.address_device = dargs.pop("address_device", None) - elif self.type == 'scsi': + elif self.type == "scsi": if dargs.get("adapter_name"): new_one.adapter_name = dargs.pop("adapter_name") if dargs.get("protocol"): @@ -73,16 +100,17 @@ def new_source(self, **dargs): new_one.host_name = dargs.pop("host_name") if dargs.get("host_port"): new_one.host_port = dargs.pop("host_port") - auth_args = {'auth_user': dargs.pop('auth_user', None), - 'secret_type': dargs.pop('secret_type', None), - 'secret_uuid': dargs.pop('secret_uuid', None), - 'secret_usage': dargs.pop('secret_usage', None) - } - if auth_args['auth_user']: + auth_args = { + "auth_user": dargs.pop("auth_user", None), + "secret_type": dargs.pop("secret_type", None), + "secret_uuid": dargs.pop("secret_uuid", None), + "secret_usage": dargs.pop("secret_usage", None), + } + if auth_args["auth_user"]: new_auth = new_one.new_auth(**auth_args) new_one.auth = new_auth - initiator_args = {'iqn_id': dargs.pop('iqn_id', None)} - if initiator_args['iqn_id']: + initiator_args = {"iqn_id": dargs.pop("iqn_id", None)} + if initiator_args["iqn_id"]: new_initiator = new_one.new_initiator(**initiator_args) new_one.initiator = new_initiator if dargs: @@ -92,44 +120,95 @@ def new_source(self, **dargs): class Source(base.base.LibvirtXMLBase): - __slots__ = ('untyped_address', 'vendor_id', 'product_id', - 'adapter_name', 'protocol', 'source_name', - 'host_name', 'host_port', 'auth', 'address_bus', - 'address_device', 'initiator') + __slots__ = ( + "untyped_address", + "vendor_id", + "product_id", + "adapter_name", + "protocol", + "source_name", + "host_name", + "host_port", + "auth", + "address_bus", + "address_device", + "initiator", + ) def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute('vendor_id', self, parent_xpath='/', - tag_name='vendor', attribute='id') - accessors.XMLAttribute('product_id', self, parent_xpath='/', - tag_name='product', attribute='id') - accessors.XMLElementNest('untyped_address', self, parent_xpath='/', - tag_name='address', subclass=self.UntypedAddress, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLAttribute('adapter_name', self, parent_xpath='/', - tag_name='adapter', attribute='name') - accessors.XMLAttribute('protocol', self, parent_xpath='/', - tag_name='source', attribute='protocol') - accessors.XMLAttribute('source_name', self, parent_xpath='/', - tag_name='source', attribute='name') - accessors.XMLAttribute('host_name', self, parent_xpath='/', - tag_name='host', attribute='name') - accessors.XMLAttribute('host_port', self, parent_xpath='/', - tag_name='host', attribute='port') - accessors.XMLAttribute('address_bus', self, parent_xpath='/', - tag_name='address', attribute='bus') - accessors.XMLAttribute('address_device', self, parent_xpath='/', - tag_name='address', attribute='device') - accessors.XMLElementNest('auth', self, parent_xpath='/', - tag_name='auth', subclass=self.Auth, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest('initiator', self, parent_xpath='/', - tag_name='initiator', subclass=self.Initiator, - subclass_dargs={ - 'virsh_instance': virsh_instance}) + accessors.XMLAttribute( + "vendor_id", self, parent_xpath="/", tag_name="vendor", attribute="id" + ) + accessors.XMLAttribute( + "product_id", self, parent_xpath="/", tag_name="product", attribute="id" + ) + accessors.XMLElementNest( + "untyped_address", + self, + parent_xpath="/", + tag_name="address", + subclass=self.UntypedAddress, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLAttribute( + "adapter_name", + self, + parent_xpath="/", + tag_name="adapter", + attribute="name", + ) + accessors.XMLAttribute( + "protocol", + self, + parent_xpath="/", + tag_name="source", + attribute="protocol", + ) + accessors.XMLAttribute( + "source_name", + self, + parent_xpath="/", + tag_name="source", + attribute="name", + ) + accessors.XMLAttribute( + "host_name", self, parent_xpath="/", tag_name="host", attribute="name" + ) + accessors.XMLAttribute( + "host_port", self, parent_xpath="/", tag_name="host", attribute="port" + ) + accessors.XMLAttribute( + "address_bus", + self, + parent_xpath="/", + tag_name="address", + attribute="bus", + ) + accessors.XMLAttribute( + "address_device", + self, + parent_xpath="/", + tag_name="address", + attribute="device", + ) + accessors.XMLElementNest( + "auth", + self, + parent_xpath="/", + tag_name="auth", + subclass=self.Auth, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "initiator", + self, + parent_xpath="/", + tag_name="initiator", + subclass=self.Initiator, + subclass_dargs={"virsh_instance": virsh_instance}, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" def new_untyped_address(self, **dargs): new_one = self.UntypedAddress(virsh_instance=self.virsh) @@ -154,52 +233,106 @@ def new_initiator(self, **dargs): class UntypedAddress(base.UntypedDeviceBase): - __slots__ = ('device', 'domain', 'bus', 'slot', 'function', - 'target', 'unit', 'uuid') + __slots__ = ( + "device", + "domain", + "bus", + "slot", + "function", + "target", + "unit", + "uuid", + ) def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute('domain', self, parent_xpath='/', - tag_name='address', attribute='domain') - accessors.XMLAttribute('slot', self, parent_xpath='/', - tag_name='address', attribute='slot') - accessors.XMLAttribute('bus', self, parent_xpath='/', - tag_name='address', attribute='bus') - accessors.XMLAttribute('device', self, parent_xpath='/', - tag_name='address', attribute='device') - accessors.XMLAttribute('function', self, parent_xpath='/', - tag_name='address', attribute='function') - accessors.XMLAttribute('target', self, parent_xpath='/', - tag_name='address', attribute='target') - accessors.XMLAttribute('unit', self, parent_xpath='/', - tag_name='address', attribute='unit') - accessors.XMLAttribute('uuid', self, parent_xpath='/', - tag_name='address', attribute='uuid') + accessors.XMLAttribute( + "domain", + self, + parent_xpath="/", + tag_name="address", + attribute="domain", + ) + accessors.XMLAttribute( + "slot", self, parent_xpath="/", tag_name="address", attribute="slot" + ) + accessors.XMLAttribute( + "bus", self, parent_xpath="/", tag_name="address", attribute="bus" + ) + accessors.XMLAttribute( + "device", + self, + parent_xpath="/", + tag_name="address", + attribute="device", + ) + accessors.XMLAttribute( + "function", + self, + parent_xpath="/", + tag_name="address", + attribute="function", + ) + accessors.XMLAttribute( + "target", + self, + parent_xpath="/", + tag_name="address", + attribute="target", + ) + accessors.XMLAttribute( + "unit", self, parent_xpath="/", tag_name="address", attribute="unit" + ) + accessors.XMLAttribute( + "uuid", self, parent_xpath="/", tag_name="address", attribute="uuid" + ) super(self.__class__, self).__init__( - "address", virsh_instance=virsh_instance) + "address", virsh_instance=virsh_instance + ) self.xml = "
" class Auth(base.base.LibvirtXMLBase): - __slots__ = ('auth_user', 'secret_type', 'secret_uuid', 'secret_usage') + __slots__ = ("auth_user", "secret_type", "secret_uuid", "secret_usage") def __init__(self, virsh_instance=base.base.virsh, auth_user=""): - accessors.XMLAttribute('auth_user', self, parent_xpath='/', - tag_name='auth', attribute='username') - accessors.XMLAttribute('secret_type', self, parent_xpath='/', - tag_name='secret', attribute='type') - accessors.XMLAttribute('secret_uuid', self, parent_xpath='/', - tag_name='secret', attribute='uuid') - accessors.XMLAttribute('secret_usage', self, parent_xpath='/', - tag_name='secret', attribute='usage') + accessors.XMLAttribute( + "auth_user", + self, + parent_xpath="/", + tag_name="auth", + attribute="username", + ) + accessors.XMLAttribute( + "secret_type", + self, + parent_xpath="/", + tag_name="secret", + attribute="type", + ) + accessors.XMLAttribute( + "secret_uuid", + self, + parent_xpath="/", + tag_name="secret", + attribute="uuid", + ) + accessors.XMLAttribute( + "secret_usage", + self, + parent_xpath="/", + tag_name="secret", + attribute="usage", + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) self.xml = "" class Initiator(base.base.LibvirtXMLBase): - __slots__ = ('iqn_id',) + __slots__ = ("iqn_id",) def __init__(self, virsh_instance=base.base.virsh, auth_user=""): - accessors.XMLAttribute('iqn_id', self, parent_xpath='/', - tag_name='iqn', attribute='name') + accessors.XMLAttribute( + "iqn_id", self, parent_xpath="/", tag_name="iqn", attribute="name" + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) self.xml = "" diff --git a/virttest/libvirt_xml/devices/hub.py b/virttest/libvirt_xml/devices/hub.py index 5b8028039f..1906660405 100644 --- a/virttest/libvirt_xml/devices/hub.py +++ b/virttest/libvirt_xml/devices/hub.py @@ -9,20 +9,25 @@ class Hub(base.TypedDeviceBase): - __slots__ = ('address',) + __slots__ = ("address",) def __init__(self, type_name, virsh_instance=base.base.virsh): - super(Hub, self).__init__(device_tag='hub', - type_name=type_name, - virsh_instance=virsh_instance) - accessors.XMLElementNest('address', self, parent_xpath='/', - tag_name='address', subclass=self.Address, - subclass_dargs={'type_name': 'usb', - 'virsh_instance': virsh_instance}) + super(Hub, self).__init__( + device_tag="hub", type_name=type_name, virsh_instance=virsh_instance + ) + accessors.XMLElementNest( + "address", + self, + parent_xpath="/", + tag_name="address", + subclass=self.Address, + subclass_dargs={"type_name": "usb", "virsh_instance": virsh_instance}, + ) + # For convenience - Address = librarian.get('address') + Address = librarian.get("address") - def new_hub_address(self, type_name='usb', **dargs): + def new_hub_address(self, type_name="usb", **dargs): """ Return a new hub Address instance and set properties from dargs """ diff --git a/virttest/libvirt_xml/devices/input.py b/virttest/libvirt_xml/devices/input.py index 39bfd38bdc..59fbeb5c58 100644 --- a/virttest/libvirt_xml/devices/input.py +++ b/virttest/libvirt_xml/devices/input.py @@ -9,37 +9,48 @@ class Input(base.TypedDeviceBase): - __slots__ = ('input_bus', 'model', 'address', 'source_evdev', 'driver', 'alias') + __slots__ = ("input_bus", "model", "address", "source_evdev", "driver", "alias") def __init__(self, type_name, virsh_instance=base.base.virsh): - super(Input, self).__init__(device_tag='input', - type_name=type_name, - virsh_instance=virsh_instance) - accessors.XMLAttribute(property_name="input_bus", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='input', - attribute='bus') - accessors.XMLAttribute(property_name="model", - libvirtxml=self, - parent_xpath='/', - tag_name='input', - attribute='model') - accessors.XMLElementNest('address', self, parent_xpath='/', - tag_name='address', subclass=self.Address, - subclass_dargs={'type_name': 'usb', - 'virsh_instance': virsh_instance}) - accessors.XMLAttribute('source_evdev', self, parent_xpath='/', - tag_name='source', attribute='evdev') - accessors.XMLElementDict('driver', self, parent_xpath='/', - tag_name='driver') - accessors.XMLElementDict('alias', self, parent_xpath='/', - tag_name='alias') + super(Input, self).__init__( + device_tag="input", type_name=type_name, virsh_instance=virsh_instance + ) + accessors.XMLAttribute( + property_name="input_bus", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="input", + attribute="bus", + ) + accessors.XMLAttribute( + property_name="model", + libvirtxml=self, + parent_xpath="/", + tag_name="input", + attribute="model", + ) + accessors.XMLElementNest( + "address", + self, + parent_xpath="/", + tag_name="address", + subclass=self.Address, + subclass_dargs={"type_name": "usb", "virsh_instance": virsh_instance}, + ) + accessors.XMLAttribute( + "source_evdev", self, parent_xpath="/", tag_name="source", attribute="evdev" + ) + accessors.XMLElementDict("driver", self, parent_xpath="/", tag_name="driver") + accessors.XMLElementDict("alias", self, parent_xpath="/", tag_name="alias") + + # For convenience + Address = librarian.get("address") + # For convenience - Address = librarian.get('address') + Address = librarian.get("address") - def new_input_address(self, type_name='usb', **dargs): + def new_input_address(self, type_name="usb", **dargs): """ Return a new input Address instance and set properties from dargs """ diff --git a/virttest/libvirt_xml/devices/interface.py b/virttest/libvirt_xml/devices/interface.py index 5ecd8b27c2..a519a2a9a0 100644 --- a/virttest/libvirt_xml/devices/interface.py +++ b/virttest/libvirt_xml/devices/interface.py @@ -12,188 +12,266 @@ class Interface(base.TypedDeviceBase): - __slots__ = ('source', 'hostdev_address', 'managed', 'mac_address', - 'bandwidth', 'model', 'coalesce', 'link_state', 'target', 'driver', - 'address', 'boot', 'rom', 'mtu', 'filterref', 'backend', - 'virtualport', 'alias', "ips", "teaming", "vlan", "port", - 'acpi', 'portForwards', 'trustGuestRxFilters', 'source_local', - 'tune') - - def __init__(self, type_name='network', virsh_instance=base.base.virsh): - super(Interface, self).__init__(device_tag='interface', - type_name=type_name, - virsh_instance=virsh_instance) - accessors.XMLElementDict(property_name="source", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='source') - accessors.XMLElementNest('hostdev_address', self, parent_xpath='/source', - tag_name='address', subclass=self.Address, - subclass_dargs={'type_name': 'pci', - 'virsh_instance': virsh_instance}) - accessors.XMLElementDict(property_name="source_local", - libvirtxml=self, - forbidden=None, - parent_xpath='/source', - tag_name='local') - accessors.XMLAttribute(property_name="managed", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='interface', - attribute='managed') - accessors.XMLAttribute(property_name="trustGuestRxFilters", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='interface', - attribute='trustGuestRxFilters') - accessors.XMLElementDict(property_name="target", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='target') - accessors.XMLElementDict(property_name="backend", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='backend') - accessors.XMLAttribute(property_name="mac_address", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='mac', - attribute='address') - accessors.XMLAttribute(property_name="link_state", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='link', - attribute='state') - accessors.XMLAttribute(property_name="boot", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='boot', - attribute='order') - accessors.XMLElementNest("bandwidth", self, - parent_xpath='/', - tag_name='bandwidth', - subclass=self.Bandwidth, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest("driver", self, - parent_xpath='/', - tag_name='driver', - subclass=self.Driver, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest("filterref", self, - parent_xpath='/', - tag_name='filterref', - subclass=self.Filterref, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLAttribute(property_name="model", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='model', - attribute='type') - accessors.XMLElementDict(property_name="coalesce", - libvirtxml=self, - forbidden=None, - parent_xpath='/coalesce/rx', - tag_name='frames') - accessors.XMLElementDict(property_name="rom", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='rom') - accessors.XMLElementDict(property_name="mtu", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='mtu') - accessors.XMLElementNest('address', self, parent_xpath='/', - tag_name='address', subclass=self.Address, - subclass_dargs={'type_name': 'pci', - 'virsh_instance': virsh_instance}) - accessors.XMLElementDict('alias', self, parent_xpath='/', - tag_name='alias') - accessors.XMLElementDict(property_name="acpi", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='acpi') - accessors.XMLElementList(property_name='ips', - libvirtxml=self, - forbidden=None, - parent_xpath='/', - marshal_from=self.marshal_from_ips, - marshal_to=self.marshal_to_ips) - accessors.XMLElementDict(property_name="teaming", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='teaming') - accessors.XMLElementNest("vlan", self, - parent_xpath='/', - tag_name='vlan', - subclass=self.Vlan, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest("virtualport", self, - parent_xpath='/', - tag_name='virtualport', - subclass=self.VirtualPort, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementDict(property_name='port', - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='port') - accessors.XMLElementList(property_name='portForwards', - libvirtxml=self, - forbidden=None, - parent_xpath='/', - marshal_from=self.marshal_from_portForwards, - marshal_to=self.marshal_to_portForwards, - has_subclass=True) - accessors.XMLElementNest('tune', self, - parent_xpath='/', - tag_name='tune', - subclass=self.Tune, - subclass_dargs={ - 'virsh_instance': virsh_instance}) + __slots__ = ( + "source", + "hostdev_address", + "managed", + "mac_address", + "bandwidth", + "model", + "coalesce", + "link_state", + "target", + "driver", + "address", + "boot", + "rom", + "mtu", + "filterref", + "backend", + "virtualport", + "alias", + "ips", + "teaming", + "vlan", + "port", + "acpi", + "portForwards", + "trustGuestRxFilters", + "source_local", + "tune", + ) + + def __init__(self, type_name="network", virsh_instance=base.base.virsh): + super(Interface, self).__init__( + device_tag="interface", type_name=type_name, virsh_instance=virsh_instance + ) + accessors.XMLElementDict( + property_name="source", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="source", + ) + accessors.XMLElementNest( + "hostdev_address", + self, + parent_xpath="/source", + tag_name="address", + subclass=self.Address, + subclass_dargs={"type_name": "pci", "virsh_instance": virsh_instance}, + ) + accessors.XMLElementDict( + property_name="source_local", + libvirtxml=self, + forbidden=None, + parent_xpath="/source", + tag_name="local", + ) + accessors.XMLAttribute( + property_name="managed", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="interface", + attribute="managed", + ) + accessors.XMLAttribute( + property_name="trustGuestRxFilters", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="interface", + attribute="trustGuestRxFilters", + ) + accessors.XMLElementDict( + property_name="target", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="target", + ) + accessors.XMLElementDict( + property_name="backend", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="backend", + ) + accessors.XMLAttribute( + property_name="mac_address", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="mac", + attribute="address", + ) + accessors.XMLAttribute( + property_name="link_state", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="link", + attribute="state", + ) + accessors.XMLAttribute( + property_name="boot", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="boot", + attribute="order", + ) + accessors.XMLElementNest( + "bandwidth", + self, + parent_xpath="/", + tag_name="bandwidth", + subclass=self.Bandwidth, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "driver", + self, + parent_xpath="/", + tag_name="driver", + subclass=self.Driver, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "filterref", + self, + parent_xpath="/", + tag_name="filterref", + subclass=self.Filterref, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLAttribute( + property_name="model", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="model", + attribute="type", + ) + accessors.XMLElementDict( + property_name="coalesce", + libvirtxml=self, + forbidden=None, + parent_xpath="/coalesce/rx", + tag_name="frames", + ) + accessors.XMLElementDict( + property_name="rom", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="rom", + ) + accessors.XMLElementDict( + property_name="mtu", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="mtu", + ) + accessors.XMLElementNest( + "address", + self, + parent_xpath="/", + tag_name="address", + subclass=self.Address, + subclass_dargs={"type_name": "pci", "virsh_instance": virsh_instance}, + ) + accessors.XMLElementDict("alias", self, parent_xpath="/", tag_name="alias") + accessors.XMLElementDict( + property_name="acpi", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="acpi", + ) + accessors.XMLElementList( + property_name="ips", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + marshal_from=self.marshal_from_ips, + marshal_to=self.marshal_to_ips, + ) + accessors.XMLElementDict( + property_name="teaming", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="teaming", + ) + accessors.XMLElementNest( + "vlan", + self, + parent_xpath="/", + tag_name="vlan", + subclass=self.Vlan, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "virtualport", + self, + parent_xpath="/", + tag_name="virtualport", + subclass=self.VirtualPort, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementDict( + property_name="port", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="port", + ) + accessors.XMLElementList( + property_name="portForwards", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + marshal_from=self.marshal_from_portForwards, + marshal_to=self.marshal_to_portForwards, + has_subclass=True, + ) + accessors.XMLElementNest( + "tune", + self, + parent_xpath="/", + tag_name="tune", + subclass=self.Tune, + subclass_dargs={"virsh_instance": virsh_instance}, + ) # For convenience - Address = librarian.get('address') + Address = librarian.get("address") - Filterref = librarian.get('filterref') + Filterref = librarian.get("filterref") @staticmethod def marshal_from_ips(item, index, libvirtxml): """Convert an Address instance into tag + attributes""" """Convert a dictionary into a tag + attributes""" - del index # not used - del libvirtxml # not used + del index # not used + del libvirtxml # not used if not isinstance(item, dict): - raise xcepts.LibvirtXMLError("Expected a dictionary of ip" - "attributes, not a %s" - % str(item)) - return ('ip', dict(item)) # return copy of dict, not reference + raise xcepts.LibvirtXMLError( + "Expected a dictionary of ip" "attributes, not a %s" % str(item) + ) + return ("ip", dict(item)) # return copy of dict, not reference @staticmethod def marshal_to_ips(tag, attr_dict, index, libvirtxml): - """Convert a tag + attributes into an Address instance """ - del index # not used - del libvirtxml # not used - if not tag == 'ip': - return None # skip this one - return dict(attr_dict) # return copy of dict, not reference + """Convert a tag + attributes into an Address instance""" + del index # not used + del libvirtxml # not used + if not tag == "ip": + return None # skip this one + return dict(attr_dict) # return copy of dict, not reference @staticmethod def marshal_from_portForwards(item, index, libvirtxml): @@ -201,21 +279,22 @@ def marshal_from_portForwards(item, index, libvirtxml): Convert an PortForward object to portForward tag and xml element. """ if isinstance(item, Interface.PortForward): - return 'portForward', item + return "portForward", item elif isinstance(item, dict): portForward = Interface.PortForward() portForward.setup_attrs(**item) - return 'portForward', portForward + return "portForward", portForward else: - raise xcepts.LibvirtXMLError("Expected a list of PortForward " - "instances, not a %s" % str(item)) + raise xcepts.LibvirtXMLError( + "Expected a list of PortForward " "instances, not a %s" % str(item) + ) @staticmethod def marshal_to_portForwards(tag, new_treefile, index, libvirtxml): """ Convert a portForward tag xml element to an object of PortForward. """ - if tag != 'portForward': + if tag != "portForward": return None # Don't convert this item newone = Interface.PortForward(virsh_instance=libvirtxml.virsh) newone.xmltreefile = new_treefile @@ -282,15 +361,18 @@ class Bandwidth(base.base.LibvirtXMLBase): outbound: dict. Keys: average, peak, floor, burst """ + __slots__ = ("inbound", "outbound") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLElementDict("inbound", self, parent_xpath="/", - tag_name="inbound") - accessors.XMLElementDict("outbound", self, parent_xpath="/", - tag_name="outbound") + accessors.XMLElementDict( + "inbound", self, parent_xpath="/", tag_name="inbound" + ) + accessors.XMLElementDict( + "outbound", self, parent_xpath="/", tag_name="outbound" + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" class Driver(base.base.LibvirtXMLBase): @@ -306,17 +388,21 @@ class Driver(base.base.LibvirtXMLBase): guest: dict. Keys: csum, gso, tso4, tso6, ecn, ufo """ + __slots__ = ("driver_attr", "driver_host", "driver_guest") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLElementDict("driver_attr", self, parent_xpath="/", - tag_name="driver") - accessors.XMLElementDict("driver_host", self, parent_xpath="/", - tag_name="host") - accessors.XMLElementDict("driver_guest", self, parent_xpath="/", - tag_name="guest") + accessors.XMLElementDict( + "driver_attr", self, parent_xpath="/", tag_name="driver" + ) + accessors.XMLElementDict( + "driver_host", self, parent_xpath="/", tag_name="host" + ) + accessors.XMLElementDict( + "driver_guest", self, parent_xpath="/", tag_name="guest" + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" class Vlan(base.base.LibvirtXMLBase): @@ -329,23 +415,28 @@ class Vlan(base.base.LibvirtXMLBase): attribute. tags: list. tags element dict list - """ + """ + __slots__ = ("trunk", "tags") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute(property_name="trunk", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='vlan', - attribute='trunk') - accessors.XMLElementList(property_name='tags', - libvirtxml=self, - parent_xpath='/', - marshal_from=self.marshal_from_tag, - marshal_to=self.marshal_to_tag) + accessors.XMLAttribute( + property_name="trunk", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="vlan", + attribute="trunk", + ) + accessors.XMLElementList( + property_name="tags", + libvirtxml=self, + parent_xpath="/", + marshal_from=self.marshal_from_tag, + marshal_to=self.marshal_to_tag, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" @staticmethod def marshal_from_tag(item, index, libvirtxml): @@ -353,18 +444,18 @@ def marshal_from_tag(item, index, libvirtxml): del index # not used del libvirtxml # not used if not isinstance(item, dict): - raise xcepts.LibvirtXMLError("Expected a dictionary of tag " - "attributes, not a %s" - % str(item)) + raise xcepts.LibvirtXMLError( + "Expected a dictionary of tag " "attributes, not a %s" % str(item) + ) # return copy of dict, not reference - return ('tag', dict(item)) + return ("tag", dict(item)) @staticmethod def marshal_to_tag(tag, attr_dict, index, libvirtxml): """Convert a tag + attributes into a dictionary""" del index # not used del libvirtxml # not used - if tag != 'tag': + if tag != "tag": return None # skip this one return dict(attr_dict) # return copy of dict, not reference@ @@ -379,19 +470,23 @@ class VirtualPort(base.base.LibvirtXMLBase): parameters: dict. """ + __slots__ = ("type", "parameters") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute(property_name="type", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='virtualport', - attribute='type') - accessors.XMLElementDict("parameters", self, parent_xpath="/", - tag_name="parameters") + accessors.XMLAttribute( + property_name="type", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="virtualport", + attribute="type", + ) + accessors.XMLElementDict( + "parameters", self, parent_xpath="/", tag_name="parameters" + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" class PortForward(base.base.LibvirtXMLBase): """ @@ -404,34 +499,39 @@ class PortForward(base.base.LibvirtXMLBase): ranges: list. """ + __slots__ = ("attrs", "ranges") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLElementDict(property_name='attrs', - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='portForward') - accessors.XMLElementList(property_name='ranges', - libvirtxml=self, - forbidden=None, - parent_xpath='/', - marshal_from=self.marshal_from_ranges, - marshal_to=self.marshal_to_ranges) + accessors.XMLElementDict( + property_name="attrs", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="portForward", + ) + accessors.XMLElementList( + property_name="ranges", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + marshal_from=self.marshal_from_ranges, + marshal_to=self.marshal_to_ranges, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" @staticmethod def marshal_from_ranges(item, index, libvirtxml): if not isinstance(item, dict): - raise xcepts.LibvirtXMLError("Expected a dictionary of range" - "attributes, not a %s" - % str(item)) - return ('range', dict(item)) + raise xcepts.LibvirtXMLError( + "Expected a dictionary of range" "attributes, not a %s" % str(item) + ) + return ("range", dict(item)) @staticmethod def marshal_to_ranges(tag, attr_dict, index, libvirtxml): - if not tag == 'range': + if not tag == "range": return None return dict(attr_dict) @@ -444,13 +544,16 @@ class Tune(base.base.LibvirtXMLBase): sndbuf: int """ - __slots__ = ('sndbuf',) + + __slots__ = ("sndbuf",) def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLElementInt(property_name='sndbuf', - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='sndbuf') + accessors.XMLElementInt( + property_name="sndbuf", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="sndbuf", + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" diff --git a/virttest/libvirt_xml/devices/iommu.py b/virttest/libvirt_xml/devices/iommu.py index ed61307236..415b618fd0 100644 --- a/virttest/libvirt_xml/devices/iommu.py +++ b/virttest/libvirt_xml/devices/iommu.py @@ -10,21 +10,19 @@ class Iommu(base.UntypedDeviceBase): - __slots__ = ('model', 'driver', 'alias', 'address') + __slots__ = ("model", "driver", "alias", "address") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute(property_name='model', - libvirtxml=self, - parent_xpath='/', - tag_name='iommu', - attribute='model') - accessors.XMLElementDict(property_name='driver', - libvirtxml=self, - parent_xpath='/', - tag_name='driver') - accessors.XMLElementDict('alias', self, parent_xpath='/', - tag_name='alias') - accessors.XMLElementDict('address', self, parent_xpath='/', - tag_name='address') - super(Iommu, self).__init__(device_tag='iommu', - virsh_instance=virsh_instance) + accessors.XMLAttribute( + property_name="model", + libvirtxml=self, + parent_xpath="/", + tag_name="iommu", + attribute="model", + ) + accessors.XMLElementDict( + property_name="driver", libvirtxml=self, parent_xpath="/", tag_name="driver" + ) + accessors.XMLElementDict("alias", self, parent_xpath="/", tag_name="alias") + accessors.XMLElementDict("address", self, parent_xpath="/", tag_name="address") + super(Iommu, self).__init__(device_tag="iommu", virsh_instance=virsh_instance) diff --git a/virttest/libvirt_xml/devices/lease.py b/virttest/libvirt_xml/devices/lease.py index 6ae2c1515a..7b1ac440f8 100644 --- a/virttest/libvirt_xml/devices/lease.py +++ b/virttest/libvirt_xml/devices/lease.py @@ -9,7 +9,7 @@ class Lease(base.UntypedDeviceBase): - __slots__ = ('lockspace', 'key', 'target') + __slots__ = ("lockspace", "key", "target") def __init__(self, virsh_instance=base.base.virsh): # example for lease : lockspace, key and target @@ -19,13 +19,9 @@ def __init__(self, virsh_instance=base.base.virsh): # somekey # # - accessors.XMLElementText('lockspace', self, - parent_xpath='/', - tag_name='lockspace') - accessors.XMLElementText('key', self, - parent_xpath='/', - tag_name='key') - accessors.XMLElementDict('target', self, parent_xpath='/', - tag_name='target') - super(Lease, self).__init__(device_tag='lease', - virsh_instance=virsh_instance) + accessors.XMLElementText( + "lockspace", self, parent_xpath="/", tag_name="lockspace" + ) + accessors.XMLElementText("key", self, parent_xpath="/", tag_name="key") + accessors.XMLElementDict("target", self, parent_xpath="/", tag_name="target") + super(Lease, self).__init__(device_tag="lease", virsh_instance=virsh_instance) diff --git a/virttest/libvirt_xml/devices/librarian.py b/virttest/libvirt_xml/devices/librarian.py index 1c67f6d520..12db5e9e6f 100644 --- a/virttest/libvirt_xml/devices/librarian.py +++ b/virttest/libvirt_xml/devices/librarian.py @@ -8,13 +8,41 @@ # Avoid accidental names like __init__, librarian, and/or other support modules -DEVICE_TYPES = ['disk', 'filesystem', 'controller', 'lease', - 'hostdev', 'redirdev', 'smartcard', 'interface', 'input', - 'hub', 'graphics', 'video', 'audio', 'parallel', 'serial', 'console', - 'nvram', 'redirfilter', 'shmem', - 'channel', 'sound', 'watchdog', 'memballoon', 'rng', 'vsock', - 'seclabel', 'address', 'emulator', 'panic', 'memory', 'filterref', - 'iommu', 'tpm'] +DEVICE_TYPES = [ + "disk", + "filesystem", + "controller", + "lease", + "hostdev", + "redirdev", + "smartcard", + "interface", + "input", + "hub", + "graphics", + "video", + "audio", + "parallel", + "serial", + "console", + "nvram", + "redirfilter", + "shmem", + "channel", + "sound", + "watchdog", + "memballoon", + "rng", + "vsock", + "seclabel", + "address", + "emulator", + "panic", + "memory", + "filterref", + "iommu", + "tpm", +] def get(name): diff --git a/virttest/libvirt_xml/devices/memballoon.py b/virttest/libvirt_xml/devices/memballoon.py index ffdddaabcc..3a542a42f6 100644 --- a/virttest/libvirt_xml/devices/memballoon.py +++ b/virttest/libvirt_xml/devices/memballoon.py @@ -10,24 +10,42 @@ class Memballoon(base.UntypedDeviceBase): - __slots__ = ('model', 'autodeflate', 'stats_period', 'address', 'alias_name', - 'driver', 'freepage_reporting') + __slots__ = ( + "model", + "autodeflate", + "stats_period", + "address", + "alias_name", + "driver", + "freepage_reporting", + ) def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute('model', self, parent_xpath='/', - tag_name='memballoon', attribute='model') - accessors.XMLAttribute('autodeflate', self, parent_xpath='/', - tag_name='memballoon', attribute='autodeflate') - accessors.XMLAttribute('stats_period', self, parent_xpath='/', - tag_name='stats', attribute='period') - accessors.XMLElementDict('address', self, parent_xpath='/', - tag_name='address') - accessors.XMLAttribute('alias_name', self, parent_xpath='/', - tag_name='alias', attribute='name') - accessors.XMLElementDict('driver', self, parent_xpath='/', - tag_name='driver') - accessors.XMLAttribute('freepage_reporting', self, parent_xpath='/', - tag_name='memballoon', - attribute='freePageReporting') - super(Memballoon, self).__init__(device_tag='memballoon', - virsh_instance=virsh_instance) + accessors.XMLAttribute( + "model", self, parent_xpath="/", tag_name="memballoon", attribute="model" + ) + accessors.XMLAttribute( + "autodeflate", + self, + parent_xpath="/", + tag_name="memballoon", + attribute="autodeflate", + ) + accessors.XMLAttribute( + "stats_period", self, parent_xpath="/", tag_name="stats", attribute="period" + ) + accessors.XMLElementDict("address", self, parent_xpath="/", tag_name="address") + accessors.XMLAttribute( + "alias_name", self, parent_xpath="/", tag_name="alias", attribute="name" + ) + accessors.XMLElementDict("driver", self, parent_xpath="/", tag_name="driver") + accessors.XMLAttribute( + "freepage_reporting", + self, + parent_xpath="/", + tag_name="memballoon", + attribute="freePageReporting", + ) + super(Memballoon, self).__init__( + device_tag="memballoon", virsh_instance=virsh_instance + ) diff --git a/virttest/libvirt_xml/devices/memory.py b/virttest/libvirt_xml/devices/memory.py index de76261244..b42277ec34 100644 --- a/virttest/libvirt_xml/devices/memory.py +++ b/virttest/libvirt_xml/devices/memory.py @@ -10,45 +10,61 @@ class Memory(base.UntypedDeviceBase): - __slots__ = ('mem_model', 'target', 'source', 'address', 'mem_discard', - 'mem_access', 'alias', 'uuid') + __slots__ = ( + "mem_model", + "target", + "source", + "address", + "mem_discard", + "mem_access", + "alias", + "uuid", + ) def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLAttribute('mem_model', self, - parent_xpath='/', - tag_name='memory', - attribute='model') - accessors.XMLAttribute('mem_discard', self, - parent_xpath='/', - tag_name='memory', - attribute='discard') - accessors.XMLAttribute('mem_access', self, - parent_xpath='/', - tag_name='memory', - attribute='access') - accessors.XMLElementText("uuid", self, - parent_xpath='/', - tag_name='uuid') - accessors.XMLElementNest('target', self, parent_xpath='/', - tag_name='target', subclass=self.Target, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest('source', self, parent_xpath='/', - tag_name='source', subclass=self.Source, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementNest('address', self, parent_xpath='/', - tag_name='address', subclass=self.Address, - subclass_dargs={ - 'type_name': 'dimm', - 'virsh_instance': virsh_instance}) - accessors.XMLElementDict('alias', self, parent_xpath='/', - tag_name='alias') - super(Memory, self).__init__(device_tag='memory', - virsh_instance=virsh_instance) - self.xml = '' - - Address = librarian.get('address') + accessors.XMLAttribute( + "mem_model", self, parent_xpath="/", tag_name="memory", attribute="model" + ) + accessors.XMLAttribute( + "mem_discard", + self, + parent_xpath="/", + tag_name="memory", + attribute="discard", + ) + accessors.XMLAttribute( + "mem_access", self, parent_xpath="/", tag_name="memory", attribute="access" + ) + accessors.XMLElementText("uuid", self, parent_xpath="/", tag_name="uuid") + accessors.XMLElementNest( + "target", + self, + parent_xpath="/", + tag_name="target", + subclass=self.Target, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "source", + self, + parent_xpath="/", + tag_name="source", + subclass=self.Source, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementNest( + "address", + self, + parent_xpath="/", + tag_name="address", + subclass=self.Address, + subclass_dargs={"type_name": "dimm", "virsh_instance": virsh_instance}, + ) + accessors.XMLElementDict("alias", self, parent_xpath="/", tag_name="alias") + super(Memory, self).__init__(device_tag="memory", virsh_instance=virsh_instance) + self.xml = "" + + Address = librarian.get("address") class Target(base.base.LibvirtXMLBase): @@ -62,65 +78,87 @@ class Target(base.base.LibvirtXMLBase): size_unit, requested_unit, current_unit, block_unit: string. """ - __slots__ = ('size', 'size_unit', 'node', 'label', - 'requested_size', 'requested_unit', - 'current_size', 'current_unit', - 'block_size', 'block_unit', 'readonly', - 'address') + + __slots__ = ( + "size", + "size_unit", + "node", + "label", + "requested_size", + "requested_unit", + "current_size", + "current_unit", + "block_size", + "block_unit", + "readonly", + "address", + ) def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLElementInt('size', - self, parent_xpath='/', - tag_name='size') - accessors.XMLAttribute(property_name="size_unit", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='size', - attribute='unit') - accessors.XMLElementInt('requested_size', - self, parent_xpath='/', - tag_name='requested') - accessors.XMLAttribute(property_name="requested_unit", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='requested', - attribute='unit') - accessors.XMLElementInt('current_size', - self, parent_xpath='/', - tag_name='current') - accessors.XMLAttribute(property_name="current_unit", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='current', - attribute='unit') - accessors.XMLElementInt('block_size', - self, parent_xpath='/', - tag_name='block') - accessors.XMLAttribute(property_name="block_unit", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='block', - attribute='unit') - accessors.XMLElementInt('node', - self, parent_xpath='/', - tag_name='node') - accessors.XMLElementNest('label', self, parent_xpath='/', - tag_name='label', subclass=self.Label, - subclass_dargs={ - 'virsh_instance': virsh_instance}) - accessors.XMLElementBool('readonly', self, parent_xpath='/', - tag_name='readonly') - accessors.XMLElementNest('address', self, parent_xpath='/', - tag_name='address', subclass=Memory.Address, - subclass_dargs={ - 'type_name': 'pci', - 'virsh_instance': virsh_instance}) + accessors.XMLElementInt("size", self, parent_xpath="/", tag_name="size") + accessors.XMLAttribute( + property_name="size_unit", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="size", + attribute="unit", + ) + accessors.XMLElementInt( + "requested_size", self, parent_xpath="/", tag_name="requested" + ) + accessors.XMLAttribute( + property_name="requested_unit", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="requested", + attribute="unit", + ) + accessors.XMLElementInt( + "current_size", self, parent_xpath="/", tag_name="current" + ) + accessors.XMLAttribute( + property_name="current_unit", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="current", + attribute="unit", + ) + accessors.XMLElementInt( + "block_size", self, parent_xpath="/", tag_name="block" + ) + accessors.XMLAttribute( + property_name="block_unit", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="block", + attribute="unit", + ) + accessors.XMLElementInt("node", self, parent_xpath="/", tag_name="node") + accessors.XMLElementNest( + "label", + self, + parent_xpath="/", + tag_name="label", + subclass=self.Label, + subclass_dargs={"virsh_instance": virsh_instance}, + ) + accessors.XMLElementBool( + "readonly", self, parent_xpath="/", tag_name="readonly" + ) + accessors.XMLElementNest( + "address", + self, + parent_xpath="/", + tag_name="address", + subclass=Memory.Address, + subclass_dargs={"type_name": "pci", "virsh_instance": virsh_instance}, + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '' + self.xml = "" class Label(base.base.LibvirtXMLBase): @@ -134,20 +172,21 @@ class Label(base.base.LibvirtXMLBase): size_unit: string. """ - __slots__ = ('size', 'size_unit') + + __slots__ = ("size", "size_unit") def __init__(self, virsh_instance=base.base.virsh): - accessors.XMLElementInt('size', - self, parent_xpath='/', - tag_name='size') - accessors.XMLAttribute(property_name="size_unit", - libvirtxml=self, - forbidden=None, - parent_xpath='/', - tag_name='size', - attribute='unit') + accessors.XMLElementInt("size", self, parent_xpath="/", tag_name="size") + accessors.XMLAttribute( + property_name="size_unit", + libvirtxml=self, + forbidden=None, + parent_xpath="/", + tag_name="size", + attribute="unit", + ) super(self.__class__, self).__init__(virsh_instance=virsh_instance) - self.xml = '
%s%s