Esempio n. 1
0
def get_openidc_auth():
    """
    use ODCS for creating composes as URL parameter
    It enables this feature in case MTF_ODCS envvar is set
    MTF_ODCS=yes -- use openidc and token for your user
    MTF_ODCS=OIDC_token_string -- use this token for authentication

    :envvar MTF_ODCS: yes or token
    :return:
    """
    odcstoken = get_odcs_envvar()

    # in case you dont have token enabled, try to ask for openidc via web browser
    if is_true(odcstoken):
        if conf.get("openidc").get("token"):
            # use value defined in config file if defined
            return conf["openidc"]["token"]
        # to not have hard dependency on openidc (use just when using ODCS without defined token)
        import openidc_client
        # Get the auth token using the OpenID client.
        oidc = openidc_client.OpenIDCClient(*conf["openidc"]["auth"])
        scopes = conf["openidc"]["scopes"]
        try:
            odcstoken = oidc.get_token(scopes, new_token=True)
        except requests.exceptions.HTTPError as e:
            core.print_info(e.response.text)
            raise mtfexceptions.ModuleFrameworkException(
                "Unable to get token via OpenIDC for your user")
    if odcstoken and len(odcstoken) < 10:
        raise mtfexceptions.ModuleFrameworkException(
            "Unable to parse token for ODCS, token is too short: %s" %
            odcstoken)
    return odcstoken
Esempio n. 2
0
def get_module_type_base():
    """
    Get which BASE module (parent) are you using

    :return: str
    """
    module_type = get_module_type()
    parent = module_type
    if module_type not in get_backend_list():
        parent = get_config().get("module", {}).get(module_type,
                                                    {}).get("parent")
        if not parent:
            raise mtfexceptions.ModuleFrameworkException(
                "Module (%s) does not provide parent backend parameter (there are: %s)"
                % (module_type, get_backend_list()))
    if parent not in get_backend_list():
        raise mtfexceptions.ModuleFrameworkException(
            "As parent is allowed just base type: %s" % get_backend_list)
    return parent
Esempio n. 3
0
def translate_cmd(cmd, translation_dict=None):
    if not translation_dict:
        return cmd
    try:
        formattedcommand = cmd.format(**translation_dict)
    except KeyError:
        raise mtfexceptions.ModuleFrameworkException(
            "Command is formatted by using trans_dict. If you want to use "
            "brackets { } in your code, please use {{ }}. Possible values "
            "in trans_dict are: %s. \nBAD COMMAND: %s" %
            (translation_dict, cmd))
    return formattedcommand
def skipTestIf(value, text="Test not intended for this module profile"):
    """
    function what solves troubles that it is not possible to call SKIP inside code
    You can use avocado decorators, it is preferred way.

    :param value: Boolean what is used for decision in case of True
    :param text: Error text what to raise
    :return: None
    """
    if value:
        raise mtfexceptions.ModuleFrameworkException(
            "DEPRECATED, don't use this skip, use self.cancel() inside test function, or self.skip() in setUp()")
Esempio n. 5
0
def get_module_type():
    """
    Get which module are you actually using.

    :return: str
    """
    amodule = os.environ.get('MODULE')
    readconfig = get_config(fail_without_url=False)
    if amodule is None and readconfig.get("default_module"):
        amodule = readconfig["default_module"]
    if amodule is None and conf.get("default_module"):
        amodule = conf["default_module"]
    if amodule in list_modules_from_config():
        return amodule
    else:
        raise mtfexceptions.ModuleFrameworkException(
            "Unsupported MODULE={0}".format(amodule),
            "supported are: %s" % list_modules_from_config())
Esempio n. 6
0
def cli():
    # unknown options are forwarded to avocado run
    args, unknown = mtfparser().parse_known_args()

    if args.version:
        print "0.7.7"
        exit(0)

    # uses additional arguments, set up variable asap, its used afterwards:
    if args.debug:
        os.environ['DEBUG'] = 'yes'
        os.environ['AVOCADO_LOG_DEBUG'] = 'yes'
    if args.config:
        os.environ['CONFIG'] = args.config
    if args.url:
        os.environ['URL'] = args.url
    if args.modulemdurl:
        os.environ['MODULEMDURL'] = args.modulemdurl

    core.print_debug(
        "Options: linter={0}, setup={1}, action={2}, module={3}".format(
            args.linter, args.setup, args.action, args.module))
    core.print_debug(
        "remaining options for avocado or test files: {0}".format(unknown))

    # environment usage:
    #   read: os.environ.get('MODULE')
    #   write: os.environ['MODULE']

    # MODULE could be from:
    #   1. environment ... MODULE=docker etc
    #   2. argument ... --module=docker
    #   3. from config.yaml in default_module
    #   4. default module stored in general mtf config yaml file

    if os.environ.get('MODULE') is not None:
        # environment is the highest priority because mtf uses environment (too much)
        args.module = os.environ['MODULE']
    if not args.module:
        args.module = common.get_module_type()

    os.environ['MODULE'] = args.module

    if not os.environ.get('URL'):
        try:
            common.get_config(reload=True)
        except mtfexceptions.DefaultConfigExc:
            raise mtfexceptions.DefaultConfigExc(
                "You have to set URL variable (via URL envar or --url parameter) in case of default config"
            )
    supported_modules = set(common.get_backend_list() +
                            common.list_modules_from_config())
    if args.module in supported_modules:
        # for debug purposes, to be sure about module variables or options
        core.print_debug("MODULE={0}, options={1}".format(
            os.environ.get('MODULE'), args.module))
    else:
        # TODO: what to do here? whats the defaults value for MODULE, do I know it?
        raise mtfexceptions.ModuleFrameworkException(
            "MODULE={0} ; we support {1}".format(os.environ.get('MODULE'),
                                                 supported_modules))

    core.print_debug("MODULE={0}".format(os.environ.get('MODULE')))
    return args, unknown
    def __init__(self, args, unknown):
        # choose between TESTS and ADDITIONAL ENVIRONMENT from options
        if args.linter:
            self.tests += glob.glob("{MTF_TOOLS}/{GENERIC_TEST}/*.py".format(
                MTF_TOOLS=metadata_common.MetadataLoaderMTF.MTF_LINTER_PATH,
                GENERIC_TEST=common.conf["generic"]["generic_tests"]))
            self.tests += glob.glob("{MTF_TOOLS}/{STATIC_LINTERS}/*.py".format(
                MTF_TOOLS=metadata_common.MetadataLoaderMTF.MTF_LINTER_PATH,
                STATIC_LINTERS=common.conf["generic"]["static_tests"]))
        self.args = args

        # parse unknow options and try to find what parameter is test
        while unknown:
            if unknown[0] in self.A_KNOWN_PARAMS_SIMPLE:
                self.additionalAvocadoArg.append(unknown[0])
                unknown = unknown[1:]
            elif unknown[0].startswith("-"):
                if "=" in unknown[0] or len(unknown) < 2:
                    self.additionalAvocadoArg.append(unknown[0])
                    unknown = unknown[1:]
                else:
                    self.additionalAvocadoArg += unknown[0:2]
                    unknown = unknown[2:]
            elif glob.glob(unknown[0]):
                # dereference filename via globs
                testlist = glob.glob(unknown[0])
                self.tests += testlist
                unknown = unknown[1:]
            else:
                self.tests.append(unknown[0])
                unknown = unknown[1:]

        if self.args.metadata:
            core.print_info("Using Metadata loader for tests and filtering")
            metadata_tests = filtertests(backend="mtf",
                                         location=os.getcwd(),
                                         linters=False,
                                         tests=[],
                                         tags=[],
                                         relevancy="")
            tests_dict = [x[metadata_common.SOURCE] for x in metadata_tests]
            self.tests += tests_dict
            core.print_debug("Loaded tests via metadata file: %s" % tests_dict)
        core.print_debug("tests = {0}".format(self.tests))
        core.print_debug("additionalAvocadoArg = {0}".format(
            self.additionalAvocadoArg))

        # Advanced filtering and testcases adding based on FMF metadata, see help
        if self.args.fmffilter or self.args.fmfpath:
            # if fmf path is empty, use actual directory
            if not self.args.fmfpath:
                self.args.fmfpath = ["."]
            try:
                import fmf
            except ImportError:
                mtfexceptions.ModuleFrameworkException(
                    "FMF metadata format not installed on your system,"
                    " see fmf.readthedocs.io/en/latest/"
                    "for more info (how to install)")
            core.print_debug(
                "Using FMF metadata: path - {} and filters - {}".format(
                    self.args.fmfpath,
                    common.conf["fmf"]["filters"] + self.args.fmffilter))
            for onepath in self.args.fmfpath:
                tree = fmf.Tree(onepath)
                for node in tree.prune(
                        False, common.conf["fmf"]["keys"],
                        common.conf["fmf"]["names"],
                        common.conf["fmf"]["filters"] + self.args.fmffilter):
                    testcase = node.show(False, common.conf["fmf"]["format"],
                                         common.conf["fmf"]["values"])
                    core.print_debug("added test by FMF: {}".format(testcase))
                    self.tests.append(testcase)