Ejemplo n.º 1
0
def get_odcs_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 = os.environ.get('MTF_ODCS')

    # in case you dont have token enabled, try to ask for openidc via web browser
    if odcstoken in TRUE_VALUES_DICT:
        # to not have hard dependency on openidc (use just when using ODCS without defined token)
        import openidc_client
        id_provider = 'https://id.fedoraproject.org/openidc/'
        # Get the auth token using the OpenID client.
        oidc = openidc_client.OpenIDCClient(
            'odcs',
            id_provider,
            {
                'Token': 'Token',
                'Authorization': 'Authorization'
            },
            'odcs-authorizer',
            'notsecret',
        )

        scopes = [
            'openid',
            'https://id.fedoraproject.org/scope/groups',
            'https://pagure.io/odcs/new-compose',
            'https://pagure.io/odcs/renew-compose',
            'https://pagure.io/odcs/delete-compose',
        ]
        try:
            odcstoken = oidc.get_token(scopes, new_token=True)
        except requests.exceptions.HTTPError as e:
            print_info(e.response.text)
            raise ModuleFrameworkException(
                "Unable to get token via OpenIDC for your user")
    if odcstoken and len(odcstoken) < 10:
        raise ModuleFrameworkException(
            "Unable to parse token for ODCS, token is too short: %s" %
            odcstoken)
    return odcstoken
Ejemplo n.º 2
0
def get_config():
    """
    Read the module's configuration file.

    :default: ``./config.yaml`` in the ``tests`` directory of the module's root
     directory
    :envvar: **CONFIG=path/to/file** overrides default value.
    :return: str
    """
    global __persistent_config
    if not __persistent_config:
        cfgfile = os.environ.get('CONFIG')
        if cfgfile:
            if os.path.exists(cfgfile):
                print_debug("Config file defined via envvar: %s" % cfgfile)
            else:
                raise ConfigExc(
                    "File does not exist although defined CONFIG envvar: %s" %
                    cfgfile)
        else:
            cfgfile = "./config.yaml"
            if os.path.exists(cfgfile):
                print_debug("Using module config file: %s" % cfgfile)
            else:
                cfgfile = "/usr/share/moduleframework/docs/example-config-minimal.yaml"
                print_debug("Using default minimal config: %s" % cfgfile)
                if not get_url():
                    raise ModuleFrameworkException(
                        "You have to use URL envvar for testing your images or repos"
                    )

        try:
            with open(cfgfile, 'r') as ymlfile:
                xcfg = yaml.load(ymlfile.read())
            doc_name = ['modularity-testing', 'meta-test-family', 'meta-test']
            if xcfg.get('document') not in doc_name:
                raise ConfigExc("bad yaml file: item (%s)" % doc_name,
                                xcfg.get('document'))
            if not xcfg.get('name'):
                raise ConfigExc("Missing (name:) in config file")
            if not xcfg.get("module"):
                raise ConfigExc("No module in yaml config defined")
            # copy rpm section to nspawn, in case not defined explicitly
            # make it backward compatible
            if xcfg.get("module", {}).get("rpm") and not xcfg.get(
                    "module", {}).get("nspawn"):
                xcfg["module"]["nspawn"] = copy.deepcopy(
                    xcfg.get("module", {}).get("rpm"))
            __persistent_config = xcfg
            return xcfg
        except IOError:
            raise ConfigExc(
                "Error: File '%s' doesn't appear to exist or it's not a YAML file. "
                "Tip: If the CONFIG envvar is not set, mtf-generator looks for './config'."
                % cfgfile)
    else:
        return __persistent_config
Ejemplo n.º 3
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 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 ModuleFrameworkException(
            "As parent is allowed just base type: %s" % get_backend_list)
    return parent
Ejemplo n.º 4
0
def translate_cmd(cmd, translation_dict=None):
    if not translation_dict:
        return cmd
    try:
        formattedcommand = cmd.format(**translation_dict)
    except KeyError:
        raise 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
Ejemplo n.º 5
0
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 ModuleFrameworkException(
            "DEPRECATED, don't use this skip, use self.cancel() inside test function, or self.skip() in setUp()"
        )
Ejemplo n.º 6
0
def get_module_type():
    """
    Get which module are you actually using.

    :return: str
    """
    amodule = os.environ.get('MODULE')
    readconfig = get_config()
    if "default_module" in readconfig and readconfig[
            "default_module"] is not None and amodule is None:
        amodule = readconfig["default_module"]
    if amodule in list_modules_from_config():
        return amodule
    else:
        raise ModuleFrameworkException(
            "Unsupported MODULE={0}".format(amodule),
            "supported are: %s" % list_modules_from_config())
Ejemplo n.º 7
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

    common.print_debug("Options: linter={0}, setup={1}, action={2}, module={3}".format(
        args.linter, args.setup, args.action, args.module))
    common.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. common.get_module_type() ... it reads config.yaml and treceback if it doesn't exist
    #   2. environment ... MODULE=docker etc
    #   3. argument ... --module=docker
    try:
        args.module = common.get_module_type()
        # TODO it wrongly writes: 'Using default minimal config ...', change in common.py
    except moduleframework.mtfexceptions.ModuleFrameworkException:
        pass

    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 args.module:
        # setup also environment
        os.environ['MODULE'] = args.module
    if not os.environ.get('URL'):
        try:
            common.get_config(reload=True)
        except DefaultConfigExc:
            raise 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
        common.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 ModuleFrameworkException("MODULE={0} ; we support {1}".format(
            os.environ.get('MODULE'), supported_modules))

    common.print_debug("MODULE={0}".format(os.environ.get('MODULE')))
    return args, unknown