Exemple #1
0
    def getUserParam(self,
                     param_name,
                     error_if_not_found=False,
                     log_warning_and_continue_if_not_found=False,
                     default_value=None,
                     to_str=False):
        """Get the value of a single user parameter.

        This method returns the value of specified user parameter.
        Note: this method will not automatically set attribute using the parameter name and value.

        Args:
            param_name: string or list of string, denoting user parameter names. If provided
                        a single string, self.user_params["<param_name>"] will be accessed.
                        If provided multiple strings,
                        self.user_params["<param_name1>"]["<param_name2>"]["<param_name3>"]...
                        will be accessed.
            error_if_not_found: bool, whether to raise error if parameter not exists. Default:
                                False
            log_warning_and_continue_if_not_found: bool, log a warning message if parameter value
                                                   not found.
            default_value: object, default value to return if not found. If error_if_not_found is
                           True, this parameter has no effect. Default: None
            to_str: boolean, whether to convert the result object to string if not None.
                    Note, strings passing in from java json config are usually unicode.

        Returns:
            object, value of the specified parameter name chain if exists;
            <default_value> if not exists.
        """

        def ToStr(return_value):
            """Check to_str option and convert to string if not None"""
            if to_str and return_value is not None:
                return str(return_value)
            return return_value

        if not param_name:
            if error_if_not_found:
                raise errors.BaseTestError("empty param_name provided")
            logging.error("empty param_name")
            return ToStr(default_value)

        if not isinstance(param_name, list):
            param_name = [param_name]

        curr_obj = self.user_params
        for param in param_name:
            if param not in curr_obj:
                msg = "Missing user param '%s' in test configuration." % param_name
                if error_if_not_found:
                    raise errors.BaseTestError(msg)
                elif log_warning_and_continue_if_not_found:
                    logging.warn(msg)
                return ToStr(default_value)
            curr_obj = curr_obj[param]

        return ToStr(curr_obj)
Exemple #2
0
    def getUserParams(self, req_param_names=[], opt_param_names=[], **kwargs):
        """Unpacks user defined parameters in test config into individual
        variables.

        Instead of accessing the user param with self.user_params["xxx"], the
        variable can be directly accessed with self.xxx.

        A missing required param will raise an exception. If an optional param
        is missing, an INFO line will be logged.

        Args:
            req_param_names: A list of names of the required user params.
            opt_param_names: A list of names of the optional user params.
            **kwargs: Arguments that provide default values.
                e.g. getUserParams(required_list, opt_list, arg_a="hello")
                     self.arg_a will be "hello" unless it is specified again in
                     required_list or opt_list.

        Raises:
            BaseTestError is raised if a required user params is missing from
            test config.
        """
        for k, v in kwargs.items():
            setattr(self, k, v)
        for name in req_param_names:
            if name not in self.user_params:
                raise errors.BaseTestError(("Missing required user param '%s' "
                                            "in test configuration.") % name)
            setattr(self, name, self.user_params[name])
        for name in opt_param_names:
            if name not in self.user_params:
                logging.info(("Missing optional user param '%s' in "
                              "configuration, continue."), name)
            else:
                setattr(self, name, self.user_params[name])
Exemple #3
0
    def _getUserConfig(self,
                       config_type,
                       key,
                       default_value=None,
                       error_if_not_found=False,
                       warn_if_not_found=False,
                       to_str=False):
        """Get the value of a user config given the key.

        This method returns the value of specified user config type.

        Args:
            config_type: string, type of user config
            key: string, key of the value string in string config map.
            default_value: object, default value to return if not found.
                           If error_if_not_found is true, this parameter has no
                           effect. Default: None
            error_if_not_found: bool, whether to raise error if parameter not
                                exists. Default: False
            warn_if_not_found: bool, log a warning message if parameter value
                               not found. Default: False
            to_str: boolean, whether to apply str() method to result value
                    if result is not None.
                    Note, strings passing in from java json config are ofen
                    unicode.

        Returns:
            Value in config matching the given key and type if exists;
            <default_value> otherwise.
        """
        dic = self.getUserParam(config_type,
                                error_if_not_found=False,
                                warn_if_not_found=False,
                                default_value=None,
                                to_str=False)

        if dic is None or key not in dic:
            msg = ("Config key %s not found in user config type %s.\n"
                   "User params: %s") % (key, config_type, self.user_params)
            if error_if_not_found:
                raise errors.BaseTestError(msg)
            elif warn_if_not_found:
                logging.warn(msg)

            return default_value

        return dic[key] if not to_str else str(dic[key])