Пример #1
0
    def test_no_writers(self):
        test_string = 'some_string'

        ccstring = utils.CCStringIO()
        ccstring.write(test_string)

        self.assertEqual(ccstring.getvalue(), test_string)
Пример #2
0
    def command(self, command):
        out = CommandOut()

        raw_stdout = utils.CCStringIO(writers=sys.stdout)
        try:
            if not self.local:
                with fabric.api.settings(
                        host_string=self.host,  # destination host
                        key_filename=self.ssh_key,  # a path to ssh key
                        timeout=2,  # a network connection timeout
                        command_timeout=10,  # a command execution timeout
                        warn_only=True,  # don't exit on error
                        abort_on_prompts=True,  # non-interactive mode
                ):
                    logger.debug(
                        "Running remote command: "
                        "host: %s command: %s", self.host, command)
                    try:
                        output = fabric.api.run(command, stdout=raw_stdout)
                    except SystemExit:
                        logger.error("Fabric aborted this iteration")
                    # NOTE(prmtl): because of pty=True (default) and
                    # combine_stderr=True (default) stderr is combined
                    # with stdout
                    out.stdout = raw_stdout.getvalue()
                    out.return_code = output.return_code
            else:
                logger.debug("Running local command: %s", command)
                out.return_code, out.stdout, out.stderr = utils.execute(
                    command)
        except Exception as e:
            logger.error("Error occured: %s", str(e))
            out.stdout = raw_stdout.getvalue()
        return out
Пример #3
0
    def test_with_writer_and_buffer(self):
        buffer = 'I am here already'

        writer = StringIO.StringIO()
        ccstring = utils.CCStringIO(buffer, writers=writer)

        self.assertEqual(ccstring.getvalue(), buffer)
        self.assertEqual(writer.getvalue(), '')
Пример #4
0
    def test_with_one_writer(self):
        test_string = 'some_string'

        writer = StringIO.StringIO()
        ccstring = utils.CCStringIO(writers=writer)
        ccstring.write(test_string)

        self.assertEqual(ccstring.getvalue(), test_string)
        self.assertEqual(writer.getvalue(), test_string)
Пример #5
0
    def test_with_multiple_writers(self):
        test_string = 'some_string'

        writer_a = StringIO.StringIO()
        writer_b = StringIO.StringIO()
        ccstring = utils.CCStringIO(writers=[writer_a, writer_b])
        ccstring.write(test_string)

        self.assertEqual(ccstring.getvalue(), test_string)
        self.assertEqual(writer_a.getvalue(), test_string)
        self.assertEqual(writer_b.getvalue(), test_string)
Пример #6
0
    def test_non_ascii_output_with_unicode(self):
        ccstring = utils.CCStringIO()
        ccstring.write('привет')
        ccstring.write(u'test')

        self.assertEqual(ccstring.getvalue(), 'приветtest')