test_streams.py 6.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
import os
import shutil
import sys
import tempfile

if sys.version_info[:2] == (2, 6):
    import unittest2 as unittest
else:
    import unittest

from avocado.core import exit_codes
from avocado.utils import process


basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..')
basedir = os.path.abspath(basedir)


class StreamsTest(unittest.TestCase):

    def setUp(self):
        self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)

    def test_app_info_stdout(self):
        """
        Checks that the application output (<= level info) goes to stdout
        """
        result = process.run('./scripts/avocado distro')
        self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
        self.assertIn('Detected distribution', result.stdout)
        self.assertEqual('', result.stderr)

    def test_app_error_stderr(self):
        """
        Checks that the application error (> level info) goes to stderr
        """
        result = process.run('./scripts/avocado unknown-whacky-command',
                             ignore_status=True)
        self.assertEqual(result.exit_status, exit_codes.AVOCADO_FAIL)
        self.assertIn("invalid choice: 'unknown-whacky-command'",
                      result.stderr)
        self.assertNotIn("invalid choice: 'unknown-whacky-command'",
                         result.stdout)
        self.assertIn("Avocado Test Runner", result.stdout)
        self.assertNotIn("Avocado Test Runner", result.stderr)

    def test_other_stream_early_stdout(self):
        """
        Checks that other streams (early in this case) goes to stdout

        Also checks the symmetry between `--show early` and the environment
        variable `AVOCADO_LOG_EARLY` being set.
        """
        cmds = (('./scripts/avocado --show early run --sysinfo=off '
55
                 '--job-results-dir %s passtest.py' % self.tmpdir, {}),
56
                ('./scripts/avocado run --sysinfo=off --job-results-dir'
57
                 ' %s passtest.py' % self.tmpdir, {'AVOCADO_LOG_EARLY': 'y'}))
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        for cmd, env in cmds:
            result = process.run(cmd, env=env, shell=True)
            self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
            self.assertIn("stevedore.extension: found extension EntryPoint.parse",
                          result.stdout)
            self.assertIn("avocado.test: Command line: %s" % cmd,
                          result.stdout)
            self.assertEqual('', result.stderr)

    def test_test(self):
        """
        Checks that the test stream (early in this case) goes to stdout

        Also checks the symmetry between `--show test` and `--show-job-log`
        """
        for cmd in (('./scripts/avocado --show test run --sysinfo=off '
74
                     '--job-results-dir %s passtest.py' % self.tmpdir),
75
                    ('./scripts/avocado run --show-job-log --sysinfo=off '
76
                     '--job-results-dir %s passtest.py' % self.tmpdir)):
77 78 79 80 81 82 83 84
            result = process.run(cmd)
            self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
            self.assertNotIn("stevedore.extension: found extension EntryPoint.parse",
                             result.stdout)
            self.assertNotIn("stevedore.extension: found extension EntryPoint.parse",
                             result.stderr)
            self.assertIn("Command line: %s" % cmd,
                          result.stdout)
85 86 87
            self.assertIn("\nSTART 1-passtest.py:PassTest.test",
                          result.stdout)
            self.assertIn("PASS 1-passtest.py:PassTest.test", result.stdout)
88 89 90 91 92 93 94 95 96
            self.assertEqual('', result.stderr)

    def test_none_success(self):
        """
        Checks that only errors are output, and that they go to stderr

        Also checks the symmetry between `--show none` and `--silent`
        """
        for cmd in (('./scripts/avocado --show none run --sysinfo=off '
97
                     '--job-results-dir %s passtest.py' % self.tmpdir),
98
                    ('./scripts/avocado --silent run --sysinfo=off '
99
                     '--job-results-dir %s passtest.py' % self.tmpdir)):
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            result = process.run(cmd)
            self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
            self.assertEqual('', result.stdout)
            self.assertEqual('', result.stderr)

    def test_none_error(self):
        """
        Checks that only errors are output, and that they go to stderr

        Also checks the symmetry between `--show none` and `--silent`
        """
        for cmd in ('./scripts/avocado --show none unknown-whacky-command',
                    './scripts/avocado --silent unknown-whacky-command'):
            result = process.run(cmd, ignore_status=True)
            self.assertEqual(result.exit_status, exit_codes.AVOCADO_FAIL)
            self.assertEqual('', result.stdout)
            self.assertNotEqual('', result.stderr)

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    def test_custom_stream_and_level(self):
        """
        Checks if "--show stream:level" works for non-built-in-streams
        """
        def run(show, no_lines):
            result = process.run("./scripts/avocado --show %s config" % show)
            out = (result.stdout + result.stderr).splitlines()
            if no_lines == "more_than_one":
                self.assertGreater(len(out), 1, "Output of %s should contain "
                                   "more than 1 line, contains only %s\n%s"
                                   % (result.command, len(out), result))
            else:
                self.assertEqual(len(out), no_lines, "Output of %s should "
                                 "contain %s lines, contains %s instead\n%s"
                                 % (result.command, no_lines, len(out),
                                    result))
        run("avocado.app:dEbUg", "more_than_one")
        run("avocado.app:0", "more_than_one")
        run("avocado.app:InFo", 1)
        run("avocado.app:20", 1)
        run("avocado.app:wARn", 0)
        run("avocado.app:30", 0)

141 142 143 144 145 146
    def tearDown(self):
        shutil.rmtree(self.tmpdir)


if __name__ == '__main__':
    unittest.main()