Пример #1
0
    def RunCommand(self,
                   command_name,
                   args=None,
                   headers=None,
                   debug=0,
                   return_stdout=False,
                   return_stderr=False,
                   return_log_handler=False,
                   cwd=None):
        """Method for calling gslib.command_runner.CommandRunner.

    Passes parallel_operations=False for all tests, optionally saving/returning
    stdout output. We run all tests multi-threaded, to exercise those more
    complicated code paths.
    TODO: Change to run with parallel_operations=True for all tests. At
    present when you do this it causes many test failures.

    Args:
      command_name: The name of the command being run.
      args: Command-line args (arg0 = actual arg, not command name ala bash).
      headers: Dictionary containing optional HTTP headers to pass to boto.
      debug: Debug level to pass in to boto connection (range 0..3).
      return_stdout: If True, will save and return stdout produced by command.
      return_stderr: If True, will save and return stderr produced by command.
      return_log_handler: If True, will return a MockLoggingHandler instance
           that was attached to the command's logger while running.
      cwd: The working directory that should be switched to before running the
           command. The working directory will be reset back to its original
           value after running the command. If not specified, the working
           directory is left unchanged.

    Returns:
      One or a tuple of requested return values, depending on whether
      return_stdout, return_stderr, and/or return_log_handler were specified.
      Return Types:
        stdout - str (binary in Py2, text in Py3)
        stderr - str (binary in Py2, text in Py3)
        log_handler - MockLoggingHandler
    """
        args = args or []

        command_line = six.ensure_text(' '.join([command_name] + args))
        if self.is_debugging:
            print_to_fd('\nRunCommand of {}\n'.format(command_line),
                        file=self.stderr_save)

        # Save and truncate stdout and stderr for the lifetime of RunCommand. This
        # way, we can return just the stdout and stderr that was output during the
        # RunNamedCommand call below.
        sys.stdout.seek(0)
        sys.stderr.seek(0)
        stdout = sys.stdout.read()
        stderr = sys.stderr.read()
        if stdout:
            self.accumulated_stdout.append(stdout)
        if stderr:
            self.accumulated_stderr.append(stderr)
        sys.stdout.seek(0)
        sys.stderr.seek(0)
        sys.stdout.truncate()
        sys.stderr.truncate()

        mock_log_handler = MockLoggingHandler()
        logging.getLogger(command_name).addHandler(mock_log_handler)
        if debug:
            logging.getLogger(command_name).setLevel(logging.DEBUG)

        try:
            with WorkingDirectory(cwd):
                self.command_runner.RunNamedCommand(command_name,
                                                    args=args,
                                                    headers=headers,
                                                    debug=debug,
                                                    parallel_operations=False,
                                                    do_shutdown=False)
        finally:
            sys.stdout.seek(0)
            sys.stderr.seek(0)
            if six.PY2:
                stdout = sys.stdout.read()
                stderr = sys.stderr.read()
            else:
                try:
                    stdout = sys.stdout.read()
                    stderr = sys.stderr.read()
                except UnicodeDecodeError:
                    sys.stdout.seek(0)
                    sys.stderr.seek(0)
                    stdout = sys.stdout.buffer.read()
                    stderr = sys.stderr.buffer.read()
            logging.getLogger(command_name).removeHandler(mock_log_handler)
            mock_log_handler.close()

            log_output = '\n'.join(
                '%s:\n  ' % level + '\n  '.join(records)
                for level, records in six.iteritems(mock_log_handler.messages)
                if records)

            _id = six.ensure_text(self.id())
            if self.is_debugging and log_output:
                print_to_fd('==== logging RunCommand {} {} ====\n'.format(
                    _id, command_line),
                            file=self.stderr_save)
                print_to_fd(log_output, file=self.stderr_save)
                print_to_fd('\n==== end logging ====\n', file=self.stderr_save)
            if self.is_debugging and stdout:
                print_to_fd('==== stdout RunCommand {} {} ====\n'.format(
                    _id, command_line),
                            file=self.stderr_save)
                print_to_fd(stdout, file=self.stderr_save)
                print_to_fd('==== end stdout ====\n', file=self.stderr_save)
            if self.is_debugging and stderr:
                print_to_fd('==== stderr RunCommand {} {} ====\n'.format(
                    _id, command_line),
                            file=self.stderr_save)
                print_to_fd(stderr, file=self.stderr_save)
                print_to_fd('==== end stderr ====\n', file=self.stderr_save)

            # Reset stdout and stderr files, so that we won't print them out again
            # in tearDown if debugging is enabled.
            sys.stdout.seek(0)
            sys.stderr.seek(0)
            sys.stdout.truncate()
            sys.stderr.truncate()

        to_return = []
        if return_stdout:
            to_return.append(stdout)
        if return_stderr:
            to_return.append(stderr)
        if return_log_handler:
            to_return.append(mock_log_handler)
        if len(to_return) == 1:
            return to_return[0]
        return tuple(to_return)
Пример #2
0
    def RunCommand(self,
                   command_name,
                   args=None,
                   headers=None,
                   debug=0,
                   test_method=None,
                   return_stdout=False,
                   return_stderr=False,
                   return_log_handler=False,
                   cwd=None):
        """Method for calling gslib.command_runner.CommandRunner.

    Passes parallel_operations=False for all tests, optionally saving/returning
    stdout output. We run all tests multi-threaded, to exercise those more
    complicated code paths.
    TODO: Change to run with parallel_operations=True for all tests. At
    present when you do this it causes many test failures.

    Args:
      command_name: The name of the command being run.
      args: Command-line args (arg0 = actual arg, not command name ala bash).
      headers: Dictionary containing optional HTTP headers to pass to boto.
      debug: Debug level to pass in to boto connection (range 0..3).
      test_method: Optional general purpose method for testing purposes.
                   Application and semantics of this method will vary by
                   command and test type.
      return_stdout: If True, will save and return stdout produced by command.
      return_stderr: If True, will save and return stderr produced by command.
      return_log_handler: If True, will return a MockLoggingHandler instance
           that was attached to the command's logger while running.
      cwd: The working directory that should be switched to before running the
           command. The working directory will be reset back to its original
           value after running the command. If not specified, the working
           directory is left unchanged.

    Returns:
      One or a tuple of requested return values, depending on whether
      return_stdout, return_stderr, and/or return_log_handler were specified.
    """
        args = args or []

        command_line = ' '.join([command_name] + args)
        if self.is_debugging:
            self.stderr_save.write('\nRunCommand of %s\n' % command_line)

        # Save and truncate stdout and stderr for the lifetime of RunCommand. This
        # way, we can return just the stdout and stderr that was output during the
        # RunNamedCommand call below.
        sys.stdout.seek(0)
        sys.stderr.seek(0)
        stdout = sys.stdout.read()
        stderr = sys.stderr.read()
        if stdout:
            self.accumulated_stdout.append(stdout)
        if stderr:
            self.accumulated_stderr.append(stderr)
        sys.stdout.seek(0)
        sys.stderr.seek(0)
        sys.stdout.truncate()
        sys.stderr.truncate()

        cwd_sav = None
        try:
            cwd_sav = os.getcwd()
        except OSError:
            # This can happen if the current working directory no longer exists.
            pass

        mock_log_handler = MockLoggingHandler()
        logging.getLogger(command_name).addHandler(mock_log_handler)

        try:
            if cwd:
                os.chdir(cwd)
            self.command_runner.RunNamedCommand(command_name,
                                                args=args,
                                                headers=headers,
                                                debug=debug,
                                                parallel_operations=False,
                                                test_method=test_method,
                                                do_shutdown=False)
        finally:
            if cwd and cwd_sav:
                os.chdir(cwd_sav)

            sys.stdout.seek(0)
            stdout = sys.stdout.read()
            sys.stderr.seek(0)
            stderr = sys.stderr.read()
            logging.getLogger(command_name).removeHandler(mock_log_handler)
            mock_log_handler.close()

            log_output = '\n'.join(
                '%s:\n  ' % level + '\n  '.join(records)
                for level, records in mock_log_handler.messages.iteritems()
                if records)
            if self.is_debugging and log_output:
                self.stderr_save.write('==== logging RunCommand %s %s ====\n' %
                                       (self.id(), command_line))
                self.stderr_save.write(log_output)
                self.stderr_save.write('\n==== end logging ====\n')
            if self.is_debugging and stdout:
                self.stderr_save.write('==== stdout RunCommand %s %s ====\n' %
                                       (self.id(), command_line))
                self.stderr_save.write(stdout)
                self.stderr_save.write('==== end stdout ====\n')
            if self.is_debugging and stderr:
                self.stderr_save.write('==== stderr RunCommand %s %s ====\n' %
                                       (self.id(), command_line))
                self.stderr_save.write(stderr)
                self.stderr_save.write('==== end stderr ====\n')

            # Reset stdout and stderr files, so that we won't print them out again
            # in tearDown if debugging is enabled.
            sys.stdout.seek(0)
            sys.stderr.seek(0)
            sys.stdout.truncate()
            sys.stderr.truncate()

        to_return = []
        if return_stdout:
            to_return.append(stdout)
        if return_stderr:
            to_return.append(stderr)
        if return_log_handler:
            to_return.append(mock_log_handler)
        if len(to_return) == 1:
            return to_return[0]
        return tuple(to_return)