def __call__(self, test, file, command=None, success=None, stderr_out=None): # where to redirect stderr if stderr_out == 'null': err = open('/dev/null', 'wb') else: err = subprocess.STDOUT # get output if success == "output": try: cmd_result = subprocess.check_output( command, stderr=err, shell=True).decode("utf-8") cmd_result = str(cmd_result) return cmd_result except: raise result_parsers.ResultParserError( "Command cannot be executed.") # get return value else: try: cmd_result = str( subprocess.call(command, stderr=err, shell=True)) return cmd_result except: raise result_parsers.ResultParserError( "Command cannot be executed.")
def _check_args(self, delimiter=None, col_num=None, has_header=None, col_names=[], by_column=True): try: if len(col_names) is not 0: if len(col_names) != int(col_num): raise result_parsers.ResultParserError( "Length of `col_names` does not match `col_num`." ) except ValueError: raise result_parsers.ResultParserError( "`col_names` needs to be an integer." )
def _check_args(self, delimiter=None, col_num=None, has_header=None, col_names=[]): if len(col_names) is not 0: if len(col_names) is not col_num: raise result_parsers.ResultParserError( "Length of `col_names` does not match `col_num`.") if delimiter == "": raise result_parsers.ResultParserError("Delimiter required.")
def _check_args(self, regex=None, match_type=None, threshold=None, expected=None): try: re.compile(regex) except (ValueError, sre_constants.error) as err: raise result_parsers.ResultParserError( "Invalid regular expression: {}".format(err)) if not isinstance(expected, list): raise result_parsers.ResultParserError( "Expected should be a list.") if threshold is not None: try: int(threshold) except ValueError as err: raise result_parsers.ResultParserError( "Non-integer value provided for 'threshold'.") if int(threshold) < 0: raise result_parsers.ResultParserError( "'threshold' must be a non-negative integer.") if expected: raise result_parsers.ResultParserError( "'threshold' and 'expected' cannot be used at the same " "time.") for item in expected: test_list = [] if '-' in item[1:]: test_list = list(self.range_re.search(item).groups()) # Check for valid second part of range. if '-' in test_list[1][1:]: raise result_parsers.ResultParserError( "Invalid range: {}".format(item)) else: test_list = [item] for test_item in test_list: # Check for values as integers. try: float(test_item) except ValueError as err: raise result_parsers.ResultParserError( "Invalid value: {}".format(test_item)) if len(test_list) > 1: # Check for range specification as # (<lesser value>-<greater value>) if float(test_list[1]) < float(test_list[0]): raise result_parsers.ResultParserError( "Invalid range: {}".format(item))
def __call__(self, test, file=None, regex=None, results=None): regex = re.compile(regex) matches = [] try: with open(file, "r") as infile: for line in infile.readlines(): match = regex.search(line) if match is not None: matches.append(match.group()) except (IOError, OSError) as err: raise result_parsers.ResultParserError( "Regex result parser could not read input file '{}': {}". format(file, err)) if results in ['first', 'last'] and not matches: return None if results == 'first': return matches[0] elif results == 'last': return matches[-1] else: return matches
def check_args(self, test, file=None, regex=None, results=None): try: re.compile(regex) except ValueError as err: raise result_parsers.ResultParserError( "Invalid regular expression: {}".format(err))
def __call__(self, test, file=None, regex=None, rtype=None): regex = re.compile(regex) matches = [] try: with open(file, "r") as infile: for line in infile.readlines(): match = regex.search(line) if match is not None: matches.append(match.group()) except (IOError, OSError) as err: raise result_parsers.ResultParserError( "Regex result parser could not read input file '{}': {}". format(file, err)) if rtype == 'first': return matches[0] if matches else None elif rtype == 'last': return matches[-1] if matches else None elif rtype == 'all': return matches elif rtype in ['PASS', 'FAIL']: if matches: return rtype else: return 'PASS' if rtype == 'FAIL' else 'FAIL' else: raise RuntimeError("Invalid 'results' argument in regex parser: " "'{}'".format(rtype))
def __call__(self, test, file, regex=None, match_type=None, threshold=None, expected=None): regex = re.compile(regex) matches = [] for line in file.readlines(): # Find all non-overlapping matches and return them as a list. # if more than one capture is used, list contains tuples of # captured strings. matches.extend(regex.findall(line)) # Test if the number of matches meets the specified threshold if int(threshold) > 0: return (len(matches) >= int(threshold)) elif match_type == result_parsers.MATCH_FIRST: matches = None if not matches else matches[0] elif match_type == result_parsers.MATCH_LAST: matches = None if not matches else matches[-1] elif match_type == result_parsers.MATCH_ALL: pass else: raise result_parsers.ResultParserError( "Invalid 'matches' value '{}'".format('matches')) # Test if the found values are within any of the specified expected # ranges. if not expected: return matches # if matches else None else: if not isinstance(matches, list): matches = [matches] ret_vals = [] for i in range(0, len(matches)): for j in range(0, len(expected)): # Not a range, checking for exact match. if '-' not in expected[j][1:] and \ float(matches[i]) == float(expected[j]): ret_vals.append(True) # Checking if found value is in this range. elif '-' in expected[j][1:]: low, high = self.range_re.search(expected[j]).groups() if float(low) <= float(matches[i]) <= float(high): ret_vals.append(True) return ret_vals
def _check_args(self, command=None, success=None, stderr_out=None): if not command: raise result_parsers.ResultParserError("Command required.")
def _check_args(self, const=None): if const == "": raise result_parsers.ResultParserError("Constant required.")
def _check_args(self, regex=None, match_type=None, threshold=None, expected=None): try: re.compile(regex) except (ValueError, sre_constants.error) as err: raise result_parsers.ResultParserError( "Invalid regular expression: {}".format(err)) if not isinstance(expected, list): raise result_parsers.ResultParserError( "Expected should be a list.") if threshold: try: int(threshold) except ValueError as err: raise result_parsers.ResultParserError( "Non-integer value provided for 'threshold'.") if int(threshold) < 0: raise result_parsers.ResultParserError( "'threshold' must be a non-negative integer.") if expected: raise result_parsers.ResultParserError( "'threshold' and 'expected' cannot be used at the same " "time.") for item in expected: test_list = [] if ':' in item: test_list = list(self.range_re.search(item).groups()) else: test_list = [ item ] none_used = False for test_item in test_list: if test_item is '': if not none_used: none_used = True else: raise result_parsers.ResultParserError( "No values provided in range: {}" .format(test_list)) else: try: # If the value is an int, it seems to work better to # cast it as a float first, just in case it is a float. float(test_item) except ValueError as err: raise result_parsers.ResultParserError( "Invalid value: {}".format(test_item) ) if len(test_list) > 1: if '.' in test_list[0]: low = float(test_list[0]) elif test_list[0] != '': low = int(test_list[0]) if '.' in test_list[1]: high = float(test_list[1]) elif test_list[1] != '': high = int(test_list[1]) # Check for range specification as # (<lesser value>:<greater value>) if '' not in test_list and high < low: raise result_parsers.ResultParserError( "Invalid range: {}".format(item))