コード例 #1
0
def ValidateRoboDirectivesList(args):
    """Validates key-value pairs for 'robo_directives' flag."""
    resource_names = set()
    duplicates = set()
    for key, value in six.iteritems((args.robo_directives or {})):
        (action_type, resource_name) = util.ParseRoboDirectiveKey(key)
        if action_type == 'click' and value:
            raise test_exceptions.InvalidArgException(
                'robo_directives',
                'Input value not allowed for click action: [{0}={1}]'.format(
                    key, value))

        # Validate resource_name validity
        if not resource_name:
            raise test_exceptions.InvalidArgException(
                'robo_directives',
                'Missing resource_name for key [{0}].'.format(key))

        # Validate resource name uniqueness
        if resource_name in resource_names:
            duplicates.add(resource_name)
        else:
            resource_names.add(resource_name)

    if duplicates:
        raise test_exceptions.InvalidArgException(
            'robo_directives',
            'Duplicate resource names are not allowed: [{0}].'.format(
                ', '.join(duplicates)))
コード例 #2
0
def _ValidateNonNegativeInteger(arg_internal_name, arg_value):
    """Validates an argument which should be an integer >= 0."""
    try:
        if isinstance(arg_value, int):
            return NONNEGATIVE_INT_PARSER(str(arg_value))
    except arg_parsers.ArgumentTypeError as e:
        raise test_exceptions.InvalidArgException(arg_internal_name, e.message)
    raise test_exceptions.InvalidArgException(arg_internal_name, arg_value)
コード例 #3
0
ファイル: arg_validate.py プロジェクト: saranraju90/multik8s
def _ValidateAdditionalIpasList(arg_internal_name, arg_value):
    """Validates that 'additional-ipas' contains [1, 100] entries."""
    if len(arg_value) < 1:
        raise test_exceptions.InvalidArgException(
            arg_internal_name, 'At least 1 additional ipa must be specified.')
    if len(arg_value) > 100:
        raise test_exceptions.InvalidArgException(
            arg_internal_name, 'At most 100 additional ipas may be specified.')
    return arg_value
コード例 #4
0
def _ValidatePositiveInteger(arg_internal_name, arg_value):
    """Validates an argument which should be an integer > 0."""
    try:
        if isinstance(arg_value, int):
            return POSITIVE_INT_PARSER(str(arg_value))
    except arg_parsers.ArgumentTypeError as e:
        raise test_exceptions.InvalidArgException(arg_internal_name,
                                                  six.text_type(e))
    raise test_exceptions.InvalidArgException(arg_internal_name, arg_value)
コード例 #5
0
def _ValidateDuration(arg_internal_name, arg_value):
    """Validates an argument which should have a Duration value."""
    try:
        if isinstance(arg_value, basestring):
            return TIMEOUT_PARSER(arg_value)
        elif isinstance(arg_value, int):
            return TIMEOUT_PARSER(str(arg_value))
    except arg_parsers.ArgumentTypeError as e:
        raise test_exceptions.InvalidArgException(arg_internal_name, e.message)
    raise test_exceptions.InvalidArgException(arg_internal_name, arg_value)
コード例 #6
0
def _ValidateDurationUs(arg_internal_name, arg_value):
    """Validates an argument which should have Duration value in microseconds."""
    try:
        if isinstance(arg_value, six.string_types):
            return TIMEOUT_PARSER_US(arg_value)
        elif isinstance(arg_value, int):
            return TIMEOUT_PARSER_US(str(arg_value))
    except arg_parsers.ArgumentTypeError as e:
        raise test_exceptions.InvalidArgException(arg_internal_name,
                                                  six.text_type(e))
    raise test_exceptions.InvalidArgException(arg_internal_name, arg_value)
コード例 #7
0
def ValidateArgFromFile(arg_internal_name, arg_value):
    """Do checks/mutations on arg values parsed from YAML which need validation.

  Any arg not appearing in the _FILE_ARG_VALIDATORS dictionary is assumed to be
  a simple string to be validated by the default _ValidateString() function.

  Mutations of the args are done in limited cases to improve ease-of-use.
  This includes:
  1) The YAML parser automatically converts attribute values into numeric types
  where possible. The os-version-ids for Android devices happen to be integers,
  but the Testing service expects them to be strings, so we automatically
  convert them to strings so users don't have to quote each one.
  2) The include: keyword, plus all test args that normally expect lists (e.g.
  device-ids, os-version-ids, locales, orientations...), will also accept a
  single value which is not specified using YAML list notation (e.g not enclosed
  in []). Such single values are automatically converted into a list containing
  one element.

  Args:
    arg_internal_name: the internal form of the arg name.
    arg_value: the argument's value as parsed from the yaml file.

  Returns:
    The validated argument value.

  Raises:
    InvalidArgException: If the arg value is missing or is not valid.
  """
    if arg_value is None:
        raise test_exceptions.InvalidArgException(arg_internal_name,
                                                  'no argument value found.')
    if arg_internal_name in _FILE_ARG_VALIDATORS:
        return _FILE_ARG_VALIDATORS[arg_internal_name](arg_internal_name,
                                                       arg_value)
    return _ValidateString(arg_internal_name, arg_value)
コード例 #8
0
ファイル: arg_file.py プロジェクト: jphalip/google-cloud-sdk
def _SplitArgFileAndGroup(file_and_group_str):
    """Parses an ARGSPEC and returns the arg filename and arg group name."""
    index = file_and_group_str.rfind(':')
    if index < 0 or (index == 2 and file_and_group_str.startswith('gs://')):
        raise exceptions.InvalidArgException(
            'arg-spec', 'Format must be ARG_FILE:ARG_GROUP_NAME')
    return file_and_group_str[:index], file_and_group_str[index + 1:]
コード例 #9
0
ファイル: arg_validate.py プロジェクト: saranraju90/multik8s
def ValidateIosDirectoriesToPullList(args):
    if not getattr(args, 'directories_to_pull', []):
        return
    for file_path in args.directories_to_pull:
        if not _IOS_DIRECTORIES_TO_PULL_PATH_REGEX.match(file_path):
            raise test_exceptions.InvalidArgException(
                'directories_to_pull', 'Invalid path [{0}]'.format(file_path))
コード例 #10
0
ファイル: arg_validate.py プロジェクト: saranraju90/multik8s
def ValidateTestTargetsForShard(args):
    """Validates --test-targets-for-shard uses proper delimiter."""
    if not getattr(args, 'test_targets_for_shard', {}):
        return
    for test_target in args.test_targets_for_shard:
        if _PACKAGE_OR_CLASS_FOLLOWED_BY_COMMA.match(test_target):
            raise test_exceptions.InvalidArgException(
                'test_targets_for_shard',
                '[{0}] is not a valid test_targets_for_shard argument. Multiple '
                '"package" and "class" specifications should be separated by '
                'a semicolon instead of a comma.'.format(test_target))
        if _ANY_SPACE_AFTER_COMMA.match(test_target):
            raise test_exceptions.InvalidArgException(
                'test_targets_for_shard',
                '[{0}] is not a valid test_targets_for_shard argument. No white '
                'space is allowed after a comma.'.format(test_target))
コード例 #11
0
def _ValidateObbFileList(arg_internal_name, arg_value):
    """Validates that 'obb-files' contains at most 2 entries."""
    arg_value = ValidateStringList(arg_internal_name, arg_value)
    if len(arg_value) > 2:
        raise test_exceptions.InvalidArgException(
            arg_internal_name, 'At most two OBB files may be specified.')
    return arg_value
コード例 #12
0
def _ValidateString(arg_internal_name, arg_value):
  """Validates an arg whose value should be a simple string."""
  if isinstance(arg_value, basestring):
    return arg_value
  if isinstance(arg_value, int):  # convert int->str if str is really expected
    return str(arg_value)
  raise test_exceptions.InvalidArgException(arg_internal_name, arg_value)
コード例 #13
0
def ValidateDirectoriesToPullList(args):
    """Validates list of file paths for 'directories-to-pull' flag."""
    for file_path in (args.directories_to_pull or []):
        # Check for correct file path format.
        if not _DIRECTORIES_TO_PULL_PATH_REGEX.match(file_path):
            raise test_exceptions.InvalidArgException(
                'directories_to_pull', 'Invalid path [{0}]'.format(file_path))
コード例 #14
0
def _ValidateBool(arg_internal_name, arg_value):
    """Validates an argument which should have a boolean value."""
    # Note: the python yaml parser automatically does string->bool conversion for
    # true/True/TRUE/false/False/FALSE and also for variations of on/off/yes/no.
    if isinstance(arg_value, bool):
        return arg_value
    raise test_exceptions.InvalidArgException(arg_internal_name, arg_value)
コード例 #15
0
def _ValidateListOfStringToStringDicts(arg_internal_name, arg_value):
    """Validates that an argument is a list of dicts of key=value string pairs."""
    if not isinstance(arg_value, list):
        raise test_exceptions.InvalidArgException(
            arg_internal_name, 'is not a list of maps of key-value pairs.')
    new_list = []
    for a_dict in arg_value:
        if not isinstance(a_dict, dict):
            raise test_exceptions.InvalidArgException(
                arg_internal_name,
                'Each list item must be a map of key-value string pairs.')
        new_dict = {}
        for (key, value) in a_dict.items():
            new_dict[str(key)] = _ValidateString(key, value)
        new_list.append(new_dict)
    return new_list
コード例 #16
0
def _MergeArgGroupIntoArgs(
    args_from_file, group_name, all_arg_groups, all_test_args_set,
    already_included_set=None):
  """Merges args from an arg group into the given args_from_file dictionary.

  Args:
    args_from_file: dict of arg:value pairs already loaded from the arg-file.
    group_name: str, the name of the arg-group to merge into args_from_file.
    all_arg_groups: dict containing all arg-groups loaded from the arg-file.
    all_test_args_set: set of str, all possible test arg names.
    already_included_set: set of str, all group names which were already
      included. Used to detect 'include:' cycles.

  Raises:
    BadFileException: an undefined arg-group name was encountered.
    InvalidArgException: a valid argument name has an invalid value, or
      use of include: led to cyclic references.
    InvalidTestArgError: an undefined argument name was encountered.
  """
  if already_included_set is None:
    already_included_set = set()
  elif group_name in already_included_set:
    raise exceptions.InvalidArgException(
        _INCLUDE,
        'Detected cyclic reference to arg group [{g}]'.format(g=group_name))
  if group_name not in all_arg_groups:
    raise calliope_exceptions.BadFileException(
        'Could not find argument group [{g}] in argument file.'
        .format(g=group_name))

  arg_group = all_arg_groups[group_name]
  if not arg_group:
    log.warning('Argument group [{0}] is empty.'.format(group_name))
    return

  for arg_name in arg_group:
    arg = arg_validate.InternalArgNameFrom(arg_name)
    # Must process include: groups last in order to follow precedence rules.
    if arg == _INCLUDE:
      continue

    if arg not in all_test_args_set:
      raise exceptions.InvalidTestArgError(arg_name)
    if arg in args_from_file:
      log.info(
          'Skipping include: of arg [{0}] because it already had value [{1}].'
          .format(arg_name, args_from_file[arg]))
    else:
      args_from_file[arg] = arg_validate.ValidateArgFromFile(
          arg, arg_group[arg_name])

  already_included_set.add(group_name)  # Prevent "include:" cycles

  if _INCLUDE in arg_group:
    included_groups = arg_validate.ValidateStringList(_INCLUDE,
                                                      arg_group[_INCLUDE])
    for included_group in included_groups:
      _MergeArgGroupIntoArgs(args_from_file, included_group, all_arg_groups,
                             all_test_args_set, already_included_set)
コード例 #17
0
def ValidateEnvironmentVariablesList(args):
    """Validates key-value pairs for 'environment-variables' flag."""
    for key in (args.environment_variables or []):
        # Check for illegal characters in the key.
        if not _ENVIRONMENT_VARIABLE_REGEX.match(key):
            raise test_exceptions.InvalidArgException(
                'environment_variables',
                'Invalid environment variable [{0}]'.format(key))
コード例 #18
0
def _ValidateOrientationList(arg_internal_name, arg_value):
    """Validates that 'orientations' only contains allowable values."""
    arg_value = ValidateStringList(arg_internal_name, arg_value)
    for orientation in arg_value:
        _ValidateOrientation(orientation)
    if len(arg_value) != len(set(arg_value)):
        raise test_exceptions.InvalidArgException(
            arg_internal_name, 'orientations may not be repeated.')
    return arg_value
コード例 #19
0
def ValidateObbFileNames(obb_files):
    """Confirm that any OBB file names follow the required Android pattern."""
    for obb_file in (obb_files or []):
        if not _OBB_FILE_REGEX.match(obb_file):
            raise test_exceptions.InvalidArgException(
                'obb_files',
                '[{0}] is not a valid OBB file name, which must have the format: '
                '(main|patch).<versionCode>.<package.name>.obb'.format(
                    obb_file))
コード例 #20
0
def _ValidateKeyValueStringPairs(arg_internal_name, arg_value):
    """Validates that an argument is a dict of string-type key-value pairs."""
    if isinstance(arg_value, dict):
        new_dict = {}
        # Cannot use dict comprehension since it's not supported in Python 2.6.
        for (key, value) in arg_value.items():
            new_dict[str(key)] = _ValidateString(arg_internal_name, value)
        return new_dict
    else:
        raise test_exceptions.InvalidArgException(
            arg_internal_name, 'Malformed key-value pairs.')
コード例 #21
0
ファイル: util.py プロジェクト: bopopescu/dotfiles-2
def ParseRoboDirectiveKey(key):
    """Returns a tuple representing a directive's type and resource name.

  Args:
    key: the directive key, which can be "<type>:<resource>" or "<resource>"

  Returns:
    A tuple of the directive's parsed type and resource name. If no type is
    specified, "text" will be returned as the default type.

  Raises:
    InvalidArgException: if the input format is incorrect or if the specified
    type is unsupported.
  """

    parts = key.split(':')
    resource_name = parts[-1]
    if len(parts) > 2:
        # Invalid format: at most one ':' is allowed.
        raise exceptions.InvalidArgException(
            'robo_directives', 'Invalid format for key [{0}]. '
            'Use a colon only to separate action type and resource name.'.
            format(key))

    if len(parts) == 1:
        # Format: '<resource_name>=<input_text>' defaults to 'text'
        action_type = 'text'
    else:
        # Format: '<type>:<resource_name>=<input_value>'
        action_type = parts[0]
        supported_action_types = ['text', 'click']
        if action_type not in supported_action_types:
            raise exceptions.InvalidArgException(
                'robo_directives',
                'Unsupported action type [{0}]. Please choose one of [{1}]'.
                format(action_type, ', '.join(supported_action_types)))

    return (action_type, resource_name)
コード例 #22
0
def NormalizeAndValidateDirectoriesToPullList(dirs):
    """Validate list of file paths for 'directories-to-pull' flag.

  Also collapse paths to remove "." ".." and "//".

  Args:
    dirs: list of directory names to pull from the device.
  """
    if dirs:
        # Expand posix paths (Note: blank entries will fail in the next loop)
        dirs[:] = [posixpath.abspath(path) if path else path for path in dirs]

    for file_path in (dirs or []):
        # Check for correct file path format.
        if not _DIRECTORIES_TO_PULL_PATH_REGEX.match(file_path):
            raise test_exceptions.InvalidArgException(
                'directories_to_pull', 'Invalid path [{0}]'.format(file_path))
コード例 #23
0
def _ValidatePositiveIntList(arg_internal_name, arg_value):
  """Validates an arg whose value should be a list of ints > 0.

  Args:
    arg_internal_name: the internal form of the arg name.
    arg_value: the argument's value parsed from yaml file.

  Returns:
    The validated argument value.

  Raises:
    InvalidArgException: the argument's value is not valid.
  """
  if isinstance(arg_value, int):  # convert single int to an int list
    arg_value = [arg_value]
  if isinstance(arg_value, list):
    return [_ValidatePositiveInteger(arg_internal_name, v) for v in arg_value]
  raise test_exceptions.InvalidArgException(arg_internal_name, arg_value)
コード例 #24
0
def ValidateStringList(arg_internal_name, arg_value):
  """Validates an arg whose value should be a list of strings.

  Args:
    arg_internal_name: the internal form of the arg name.
    arg_value: the argument's value parsed from yaml file.

  Returns:
    The validated argument value.

  Raises:
    InvalidArgException: the argument's value is not valid.
  """
  if isinstance(arg_value, basestring):  # convert single str to a str list
    return [arg_value]
  if isinstance(arg_value, int):  # convert single int to a str list
    return [str(arg_value)]
  if isinstance(arg_value, list):
    return [_ValidateString(arg_internal_name, value) for value in arg_value]
  raise test_exceptions.InvalidArgException(arg_internal_name, arg_value)
コード例 #25
0
def NormalizeAndValidateObbFileNames(obb_files):
  """Confirm that any OBB file names follow the required Android pattern.

  Also expand local paths with "~"

  Args:
    obb_files: list of obb file references. Each one is either a filename on the
      local FS or a gs:// reference.
  """
  if obb_files:
    obb_files[:] = [
        obb_file if not obb_file or
        obb_file.startswith(storage_util.GSUTIL_BUCKET_PREFIX) else
        os.path.expanduser(obb_file) for obb_file in obb_files
    ]
  for obb_file in (obb_files or []):
    if not _OBB_FILE_REGEX.match(obb_file):
      raise test_exceptions.InvalidArgException(
          'obb_files',
          '[{0}] is not a valid OBB file name, which must have the format: '
          '(main|patch).<versionCode>.<package.name>.obb'.format(obb_file))
コード例 #26
0
def ValidateArgsForTestType(args, test_type, type_rules, shared_rules,
                            all_test_args_set):
    """Raise errors if required args are missing or invalid args are present.

  Args:
    args: an argparse.Namespace object which contains attributes for all the
      arguments that were provided to the command invocation (i.e. command
      group and command arguments combined).
    test_type: string containing the type of test to run.
    type_rules: a nested dictionary defining the required and optional args
      per type of test, plus any default values.
    shared_rules: a nested dictionary defining the required and optional args
      shared among all test types, plus any default values.
    all_test_args_set: a set of strings for every gcloud-test argument to use
      for validation.

  Raises:
    InvalidArgException: If an arg doesn't pair with the test type.
    RequiredArgumentException: If a required arg for the test type is missing.
  """
    required_args = type_rules[test_type]['required'] + shared_rules['required']
    optional_args = type_rules[test_type]['optional'] + shared_rules['optional']
    allowable_args_for_type = required_args + optional_args

    # Raise an error if an optional test arg is not allowed with this test_type.
    for arg in all_test_args_set:
        if getattr(args, arg, None) is not None:  # Ignore args equal to None
            if arg not in allowable_args_for_type:
                raise test_exceptions.InvalidArgException(
                    arg,
                    'may not be used with test type [{0}].'.format(test_type))
    # Raise an error if a required test arg is missing or equal to None.
    for arg in required_args:
        if getattr(args, arg, None) is None:
            raise exceptions.RequiredArgumentException(
                '{0}'.format(test_exceptions.ExternalArgNameFrom(arg)),
                'must be specified with test type [{0}].'.format(test_type))
コード例 #27
0
ファイル: arg_validate.py プロジェクト: saranraju90/multik8s
def _ValidatePermissions(arg_internal_name, arg_value):
    if arg_value not in PERMISSIONS_LIST:
        raise test_exceptions.InvalidArgException(
            arg_internal_name,
            'Invalid permissions specified. Must be either "all" or "none"')
    return arg_value
コード例 #28
0
def _ValidateInteger(arg_internal_name, arg_value):
    """Validates an argument which should have any integer value."""
    if isinstance(arg_value, int):
        return arg_value
    raise test_exceptions.InvalidArgException(arg_internal_name, arg_value)