def get_parser(self, show_restriction=False, show_dictionaries=False, show_dict_fullname=False, show_dict_contents=False, show_empty_cartesian_product=True): """ Get a basic parameters parser with its dictionaries. :param bool show_restriction: whether to show the restriction strings :param bool show_dictionaries: whether to show the obtained variants :param bool show_dict_fullname: whether to show the variant fullname rather than its shortname :param bool show_dict_contents: whether to show the obtained variant parameters :param bool show_empty_cartesian_product: whether to check and show the resulting cartesian product :returns: resulting parser :rtype: :py:class:`cartesian_config.Parser` :raises: :py:class:`EmptyCartesianProduct` if no combination of the restrictions exists """ parser = cartesian_config.Parser() hostname = os.environ.get("PREFIX", os.environ.get("HOSTNAME", "avocado")) parser.parse_string("hostname = %s\n" % hostname) for step in self.steps: if isinstance(step, ParsedFile): parser.parse_file(step.filename) if isinstance(step, ParsedStr): parser.parse_string(step.content) if isinstance(step, ParsedDict): parser.parse_string(step.parsable_form()) # log any required information and detect empty Cartesian product if show_restriction: logging.debug(self.print_parsed()) if show_dictionaries or show_empty_cartesian_product: options = collections.namedtuple( "options", ['repr_mode', 'fullname', 'contents']) peek_parser = self.get_parser(show_dictionaries=False, show_empty_cartesian_product=False) # break generator into first detectable entry and rest to reuse it better peek_generator = peek_parser.get_dicts() if show_empty_cartesian_product: try: peek_dict = peek_generator.__next__() if show_dictionaries: cartesian_config.print_dicts( options(False, show_dict_fullname, show_dict_contents), (peek_dict, )) cartesian_config.print_dicts( options(False, show_dict_fullname, show_dict_contents), peek_generator) except StopIteration: raise EmptyCartesianProduct(self.print_parsed()) from None else: cartesian_config.print_dicts( options(False, show_dict_fullname, show_dict_contents), peek_generator) return parser
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 = get_opt(self.options, 'vt_type') vt_config = get_opt(self.options, 'vt_config') if (not vt_type) and (not vt_config): 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))) 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.options, '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.options, 'vt_filter_default_filters'): self.cartesian_parser.only_filter(arg) if 'image_backend' not in get_opt(self.options, 'vt_filter_default_filters'): self.cartesian_parser.only_filter('(image_backend=' 'filesystem)') if 'multihost' not in get_opt(self.options, 'vt_filter_default_filters'): self.cartesian_parser.no_filter('multihost') else: cfg = data_dir.get_backend_cfg_path(vt_type, 'tests.cfg') self.cartesian_parser.parse_file(cfg) if vt_type != 'lvsb': self._process_general_options() if vt_type == 'qemu': self._process_qemu_specific_options() elif vt_type == 'lvsb': self._process_lvsb_specific_options() elif vt_type == 'openvswitch': self._process_qemu_specific_options() elif vt_type == 'libvirt': self._process_libvirt_specific_options() elif vt_type == 'spice': self._process_spice_specific_options() self._process_extra_params()
def get_cfg_as_dict(path, drop_only=True, drop_conditional_assigment=True): """ Gets a single config file as dict By putting the content of file within a "variants:" context. Optionally (default) also drops all instances of "only" statements, and optional assignments, ie: section_name: foo = bar since the files are evaluated individually and that can lead to an empty results. """ lines = open(path).readlines() if drop_only: 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.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)) assert len_dicts == 1 return dicts[0]
def copy_parser(parser): """ Copy a parser in the most efficient way possible. :param parser: source parser to copy from :type parser: Parser object :returns: new parser copy :rtype: Parser object """ new_parser = cartesian_config.Parser() new_parser.node.content = copy.copy(parser.node.content) new_parser.node.children = copy.copy(parser.node.children) new_parser.node.labels = copy.copy(parser.node.labels) return new_parser
def get_koji_qemu_kvm_tag_pkgs(self, config_file): tag = None pkgs = None parser = cartesian_config.Parser(config_file) for d in parser.get_dicts(): if tag is not None and pkgs is not None: break 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: if pkgs is None: pkgs = d.get('koji_qemu_kvm_pkgs') return (tag, pkgs)
def _checkConfigDump(self, config, dump): """Check if the parser output matches a config file dump""" configpath = os.path.join(testdatadir, config) dumppath = os.path.join(testdatadir, dump) if dumppath.endswith('.gz'): df = gzip.GzipFile(dumppath, 'r') else: df = open(dumppath, 'r') # we could have used pickle, but repr()-based dumps are easier to # enerate, debug, and edit dumpdata = eval(df.read()) p = cartesian_config.Parser(configpath) self._checkDictionaries(p, dumpdata)
def test_git_repo_param_helper(self): config = """git_repo_foo_uri = git://git.foo.org/foo.git git_repo_foo_branch = next git_repo_foo_lbranch = local git_repo_foo_commit = bc732ad8b2ed8be52160b893735417b43a1e91a8 """ config_parser = cartesian_config.Parser() config_parser.parse_string(config) params = config_parser.get_dicts().next() 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_make_installer(self): config = """install_mode = test_install_mode vm_type = test""" class Installer: def __init__(self, mode, name, test, params): pass installer.INSTALLER_REGISTRY.register('test_install_mode', Installer, 'test') config_parser = cartesian_config.Parser() config_parser.parse_string(config) params = config_parser.get_dicts().next() instance = installer.make_installer("test_install_mode_test", params) self.assertTrue(isinstance(instance, Installer))
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' if (not self.options.vt_type) and (not self.options.vt_config): raise ValueError("No %s or %s specified" % (vt_type_setting, vt_config_setting)) if self.options.vt_type: if self.options.vt_type not in SUPPORTED_TEST_TYPES: raise ValueError("Invalid %s %s. Valid values: %s. " % (vt_type_setting, self.options.vt_type, " ".join(SUPPORTED_TEST_TYPES))) self.cartesian_parser = cartesian_config.Parser(debug=False) if self.options.vt_config: cfg = os.path.abspath(self.options.vt_config) if not self.options.vt_config: cfg = data_dir.get_backend_cfg_path(self.options.vt_type, 'tests.cfg') self.cartesian_parser.parse_file(cfg) if self.options.vt_type != 'lvsb': self._process_general_options() if self.options.vt_type == 'qemu': self._process_qemu_specific_options() elif self.options.vt_type == 'lvsb': self._process_lvsb_specific_options() elif self.options.vt_type == 'openvswitch': self._process_qemu_specific_options() elif self.options.vt_type == 'libvirt': self._process_libvirt_specific_options() elif self.options.vt_type == 'spice': self._process_spice_specific_options() self._process_extra_params()
def setUp(self): """ Runs before every test """ logging.disable(logging.INFO) logging.disable(logging.WARNING) # MAC generator produces from incrementing byte list # at random starting point (class property). # make sure it starts counting at zero before every test utils_net.VirtIface.LASTBYTE = -1 # These warnings are annoying during testing utils_net.VMNet.DISCARD_WARNINGS - 1 parser = cartesian_config.Parser() parser.parse_string(self.nettests_cartesian) self.CartesianResult = [] for d in parser.get_dicts(): params = utils_params.Params(d) self.CartesianResult.append(params) for vm_name in params.objects('vms'): vm = params.object_params(vm_name) nics = vm.get('nics') if nics and len(nics.split()) > 0: self.db_item_count += 1
def _checkStringDump(self, string, dump, defaults=False): p = cartesian_config.Parser(defaults=defaults) p.parse_string(string) self._checkDictionaries(p, dump)
def _checkStringConfig(self, string, reference): p = cartesian_config.Parser() p.parse_string(string) self._checkDictionaries(p, reference)
def prepare_parser(base_dict=None, base_str="", base_file=None, ovrwrt_dict=None, ovrwrt_str="", ovrwrt_file=None, show_restriction=False, show_dictionaries=False, show_dict_fullname=False, show_dict_contents=False): """ Get a basic parameters parser with its dictionaries. :param base_file: file to be parsed first :type base_file: str or None :param str base_str: string to be parsed first :param base_dict: params to be added first :type base_dict: {str, str} or None :param ovrwrt_file: file to be parsed last :type ovrwrt_file: str or None :param str ovrwrt_str: string to be parsed last :param ovrwrt_dict: params to be added last :type ovrwrt_dict: {str, str} or None :param bool show_restriction: whether to show the restriction strings :param bool show_dictionaries: whether to show the obtained variants :param bool show_dict_fullname: whether to show the variant fullname rather than its shortname :param bool show_dict_contents: whether to show the obtained variant parameters :returns: resulting parser :rtype: Parser object :raises: :py:class:`EmptyCartesianProduct` if no combination of the restrictions exists The priority of the setting follows the order of the arguments: Dictionary with some parameters is topmost, string with some parameters is next and the file with parameters is taken as a base. The overwriting version is taken last, the base version first. """ parser = cartesian_config.Parser() # configuration base if base_file is not None: parser.parse_file(os.path.join(custom_configs_dir, base_file)) if base_dict is not None: base_str += dict_to_str(base_dict) parser.parse_string(base_str) # configuration top if ovrwrt_file is not None: parser.parse_file(os.path.join(os.environ['HOME'], ovrwrt_file)) if ovrwrt_dict is not None: ovrwrt_str += dict_to_str(ovrwrt_dict) parser.parse_string(ovrwrt_str) # log any required information if show_restriction: logging.debug( print_restriction(base_file=base_file, base_str=base_str, ovrwrt_file=ovrwrt_file, ovrwrt_str=ovrwrt_str)) if show_dictionaries: options = collections.namedtuple("options", ['repr_mode', 'fullname', 'contents'], verbose=False) cartesian_config.print_dicts( options(False, show_dict_fullname, show_dict_contents), parser.get_dicts()) # detect empty Cartesian product try: parser.get_dicts().__next__() except StopIteration: raise EmptyCartesianProduct( print_restriction(base_file=base_file, base_str=base_str, ovrwrt_file=ovrwrt_file, ovrwrt_str=ovrwrt_str)) from None return parser