test_streams.py 6.1 KB
Newer Older
1 2 3
import os
import shutil
import tempfile
4
import unittest
5 6 7 8 9 10 11 12

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)

13 14
AVOCADO = os.environ.get("UNITTEST_AVOCADO_CMD", "./scripts/avocado")

15 16 17 18 19

class StreamsTest(unittest.TestCase):

    def setUp(self):
        self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
20
        os.chdir(basedir)
21 22 23 24 25

    def test_app_info_stdout(self):
        """
        Checks that the application output (<= level info) goes to stdout
        """
26
        result = process.run('%s distro' % AVOCADO)
27
        self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
28 29
        self.assertIn(b'Detected distribution', result.stdout)
        self.assertEqual(b'', result.stderr)
30 31 32 33 34

    def test_app_error_stderr(self):
        """
        Checks that the application error (> level info) goes to stderr
        """
35
        result = process.run('%s unknown-whacky-command' % AVOCADO,
36 37
                             ignore_status=True)
        self.assertEqual(result.exit_status, exit_codes.AVOCADO_FAIL)
38
        self.assertIn(b"invalid choice: 'unknown-whacky-command'",
39
                      result.stderr)
40
        self.assertNotIn(b"invalid choice: 'unknown-whacky-command'",
41
                         result.stdout)
42 43
        self.assertIn(b"Avocado Test Runner", result.stdout)
        self.assertNotIn(b"Avocado Test Runner", result.stderr)
44 45 46 47 48 49 50 51

    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.
        """
52 53 54 55 56 57
        cmds = (('%s --show early run --sysinfo=off '
                 '--job-results-dir %s passtest.py' % (AVOCADO, self.tmpdir),
                 {}),
                ('%s run --sysinfo=off --job-results-dir'
                 ' %s passtest.py' % (AVOCADO, self.tmpdir),
                 {'AVOCADO_LOG_EARLY': 'y'}))
58 59 60
        for cmd, env in cmds:
            result = process.run(cmd, env=env, shell=True)
            self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
61
            self.assertIn(b"stevedore.extension: found extension EntryPoint.parse",
62 63
                          result.stdout)
            self.assertIn("avocado.test: Command line: %s" % cmd,
64 65
                          result.stdout_text)
            self.assertEqual(b'', result.stderr)
66 67 68 69 70 71 72

    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`
        """
73 74 75 76
        for cmd in (('%s --show test run --sysinfo=off --job-results-dir %s '
                     'passtest.py' % (AVOCADO, self.tmpdir)),
                    ('%s run --show-job-log --sysinfo=off --job-results-dir %s'
                     ' passtest.py' % (AVOCADO, self.tmpdir))):
77 78
            result = process.run(cmd)
            self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
79
            self.assertNotIn(b"stevedore.extension: found extension EntryPoint.parse",
80
                             result.stdout)
81
            self.assertNotIn(b"stevedore.extension: found extension EntryPoint.parse",
82 83
                             result.stderr)
            self.assertIn("Command line: %s" % cmd,
84 85
                          result.stdout_text)
            self.assertIn(b"\nSTART 1-passtest.py:PassTest.test",
86
                          result.stdout)
87 88
            self.assertIn(b"PASS 1-passtest.py:PassTest.test", result.stdout)
            self.assertEqual(b'', result.stderr)
89 90 91 92 93 94 95

    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`
        """
96 97 98 99
        for cmd in (('%s --show none run --sysinfo=off --job-results-dir %s '
                     'passtest.py' % (AVOCADO, self.tmpdir)),
                    ('%s --silent run --sysinfo=off --job-results-dir %s '
                     'passtest.py' % (AVOCADO, self.tmpdir))):
100 101
            result = process.run(cmd)
            self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
102 103
            self.assertEqual(b'', result.stdout)
            self.assertEqual(b'', result.stderr)
104 105 106 107 108 109 110

    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`
        """
111 112
        for cmd in ('%s --show none unknown-whacky-command' % AVOCADO,
                    '%s --silent unknown-whacky-command' % AVOCADO):
113 114
            result = process.run(cmd, ignore_status=True)
            self.assertEqual(result.exit_status, exit_codes.AVOCADO_FAIL)
115 116
            self.assertEqual(b'', result.stdout)
            self.assertNotEqual(b'', result.stderr)
117

118 119 120 121 122
    def test_custom_stream_and_level(self):
        """
        Checks if "--show stream:level" works for non-built-in-streams
        """
        def run(show, no_lines):
123
            result = process.run("%s --show %s config" % (AVOCADO, show))
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            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()