def __init__(self, **kwargs): # reset the suite variables self._set_suite_defaults() # test suite name is the name of the suite class # or the suite_name arg if passed if 'suite_name' in kwargs: if assert_variable_type(kwargs['suite_name'], str): self.suite_name = kwargs['suite_name'] else: # suite name defaults to class name self.suite_name = self.__class__.__name__ # add test suite to class static total list try: # make sure a suite with the same # name does not already exist if self.suite_name not in ExternalProgramTestSuite._test_suites: ExternalProgramTestSuite._test_suites[self.suite_name] = {'self': self, 'name': self.suite_name, 'description': self.suite_description, 'args': kwargs, 'num_passed': 0, 'num_tests': 0, 'num_checks': 0, 'num_checks_passed': 0, 'execution_time': 0, 'has_run': False, 'pass_threshold': 100, 'passed': False} else: raise ValueError('A suite with the name "%s" already exists. ' 'Please rename one of suite classes or pass a unique "suite_name" argument to one or both of the constructors.') except ValueError as e: raise Exception('[%s] %s' %(type(e).__name__, e))
def __init__(self, stream, print_stream=True, log_file=None): """Initialize the stream reader/writer Positional arguments: stream -- the stream to read from. Usually a process' stdout or stderr. log_file -- the file to write the stream output to """ # Queue to hold stream self._q = Queue() # string to hold cumulative output self._output = "" # verify arguments assert_variable_type(log_file, [str, NoneType]) assert_variable_type(stream, FileType) def _populate_queue(stream, queue, log_file): """ Collect lines from 'stream', put them in 'queue'. Write the stream output to the log_file if it was supplied. """ while True: line = stream.readline() if line: queue.put(line) if print_stream: print(line) self._output += line + "\r\n" if log_file is not None: with open(log_file, 'a') as f: f.write(line) else: return self._t = Thread(target=_populate_queue, args=(stream, self._q, log_file)) self._t.daemon = True self._t.start() #start collecting lines from the stream
def __init__(self, stream, print_stream=True, log_file=None): """Initialize the stream reader/writer Positional arguments: stream -- the stream to read from. Usually a process' stdout or stderr. log_file -- the file to write the stream output to """ # Queue to hold stream self._q = Queue() # string to hold cumulative output self._output = "" # verify arguments assert_variable_type(log_file, [str, NoneType]) assert_variable_type(stream, FileType) def _populate_queue(stream, queue, log_file): """ Collect lines from 'stream', put them in 'queue'. Write the stream output to the log_file if it was supplied. """ while True: line = stream.readline() if line: queue.put(line) if print_stream: print(line) self._output += line + "\r\n" if log_file is not None: with open(log_file, 'a') as f: f.write(line) else: return self._t = Thread(target = _populate_queue, args = (stream, self._q, log_file)) self._t.daemon = True self._t.start() #start collecting lines from the stream
def __init__(self, **kwargs): # reset the suite variables self._set_suite_defaults() # test suite name is the name of the suite class # or the suite_name arg if passed if 'suite_name' in kwargs: if assert_variable_type(kwargs['suite_name'], str): self.suite_name = kwargs['suite_name'] else: # suite name defaults to class name self.suite_name = self.__class__.__name__ # add test suite to class static total list try: # make sure a suite with the same # name does not already exist if self.suite_name not in ExternalProgramTestSuite._test_suites: ExternalProgramTestSuite._test_suites[self.suite_name] = { 'self': self, 'name': self.suite_name, 'description': self.suite_description, 'args': kwargs, 'num_passed': 0, 'num_tests': 0, 'num_checks': 0, 'num_checks_passed': 0, 'execution_time': 0, 'has_run': False, 'pass_threshold': 100, 'passed': False } else: raise ValueError( 'A suite with the name "%s" already exists. ' 'Please rename one of suite classes or pass a unique "suite_name" argument to one or both of the constructors.' ) except ValueError as e: raise Exception('[%s] %s' % (type(e).__name__, e))
def run_subprocess(executable_command, command_arguments = [], timeout=None, print_process_output=True, stdout_file=None, stderr_file=None, poll_seconds=.100, buffer_size=-1): """Create and run a subprocess and return the process and execution time after it has completed. The execution time does not include the time taken for file i/o when logging the output if stdout_file and stderr_file arguments are given. Positional arguments: executable_command (str) -- executable command to run command_arguments (list) -- command line arguments timeout (int/float) -- how many seconds to allow for process completion print_process_output (bool) -- whether to print the process' live output stdout_file (str) -- file to log stdout to stderr_file (str) -- file to log stderr to poll_seconds(int/float) -- how often in seconds to poll the subprocess to check for completion """ # validate arguments # list assert_variable_type(command_arguments, list) # strings assert_variable_type(executable_command, str) _string_vars = [stdout_file, stderr_file] [assert_variable_type(x, [str, NoneType]) for x in _string_vars + command_arguments] # bools assert_variable_type(print_process_output, bool) # floats _float_vars = [timeout, poll_seconds] [assert_variable_type(x, [int, float, NoneType]) for x in _float_vars] global process, _nbsr_stdout, _nbsr_stderr process = None _nbsr_stdout = None _nbsr_stderr = None def _exec_subprocess(): # create the subprocess to run the external program global process, _nbsr_stdout, _nbsr_stderr process = subprocess.Popen([executable_command] + command_arguments, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=buffer_size) # wrap p.stdout with a NonBlockingStreamReader object: _nbsr_stdout = NBSRW(process.stdout, print_process_output, stdout_file) _nbsr_stderr = NBSRW(process.stderr, print_process_output, stderr_file) # set deadline if timeout was set _deadline = None if timeout is not None: _deadline = timeit.default_timer() + timeout # poll process while it runs while process.poll() is None: # throw TimeoutError if timeout was specified and deadline has passed if _deadline is not None and timeit.default_timer() > _deadline and process.poll() is None: process.terminate() raise TimeoutError("Sub-process did not complete before %.4f seconds elapsed" %(timeout)) # sleep to yield for other processes time.sleep(poll_seconds) execution_time = timeit.timeit(_exec_subprocess, number=1) # return process to allow application to communicate with it # and extract whatever info like stdout, stderr, returncode # also return execution_time to allow return process, execution_time
def _validate_argument(self, argument, types): key = argument.keys()[0] valid, message = assert_variable_type(argument[key], types, False) if not valid: self._invalid_args.append("%s: %s" %(key, message))
def _validate_argument(self, argument, types): key = argument.keys()[0] valid, message = assert_variable_type(argument[key], types, False) if not valid: self._invalid_args.append("%s: %s" % (key, message))