diff options
author | Jake Hunsaker <jhunsake@redhat.com> | 2021-01-27 09:55:39 -0500 |
---|---|---|
committer | Jake Hunsaker <jhunsake@redhat.com> | 2021-04-15 11:33:03 -0400 |
commit | f568fb219fff5b502882d591ddcc6bd5cc84c7a2 (patch) | |
tree | a37485633c469bfd4d166116da86a0b574446753 /tests/unittests/utilities_tests.py | |
parent | 9191c690a3311b81eeb3480d4f054e93d1ce75e3 (diff) | |
download | sos-f568fb219fff5b502882d591ddcc6bd5cc84c7a2.tar.gz |
[tests] Start using avocado for test suite
This commit represents the start of an overhaul of the test suite used
by sos. Note that several more commits to follow will be required in
order for the test suite to be considered stable.
The new test suite will use the avocado-framework to build out new
tests.
This first part adopts a new 'stageX' naming scheme for our tests as
follows:
stage0 -> Unittests
stage1 -> Basic function tests, no mocking allowed
stage2 -> Mocked tests for specific scenarios/regressions
stage3 -> Complex setups for layered products/environments
At the moment, these unittests are not updated for avocado, though most
should still work with `nosetest` directly.
A new set of base classes is defined in tests/sos_tests.py which provide
the foundation for actual tests cases. This approach entails new test
cases subclassing a base class, such as the new `StageOneReportTest`,
and setting the `sos_cmd` class attr to the _options_ for an sos report
run. By default `sos report --batch` will be run, and targeted to the
test job's directory as a tmpdir.
Each sos command will be executed once, and all test_* methods within a
test case that subclasses `StageOneReportTest` will be checked against
the output of that execution. Note that this diverges from avocado's
typical approach where each test_* method is performed against a brand
new instance of the class (thus meaning any setup including our sos
report run would normally be run fresh). However, after speaking with
the avocado devel team, this is still seen as a valid pattern for the
framework.
The current organizational approach is to separate the tests by
component rather than stage. For example. `tests/report_tests/` should
hold any report-centric tests, and the `plugin_tests` directory therein
should be used for plugin-specific tests. As of this commit, there are
basic functionality tests under `tests/report_tests/` and a single
plugin test under `tests/report_tests/plugin_tests/` to act as a POC.
Further, there is a `tests/vendor_tests/` directory for organizing
vendor-specific bug/feature tests that are not covered by the generic
project-wide tests. A POC test from RHBZ1928628 is available with this
commit.
Note that in order for these tests to be run properly _without_
installing the current branch to the local system, you will need to run
the following command:
`PYTHONPATH=tests/ avocado run -t stageone tests/`
Related: #2431
Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
Diffstat (limited to 'tests/unittests/utilities_tests.py')
-rw-r--r-- | tests/unittests/utilities_tests.py | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/tests/unittests/utilities_tests.py b/tests/unittests/utilities_tests.py new file mode 100644 index 00000000..64be9f1e --- /dev/null +++ b/tests/unittests/utilities_tests.py @@ -0,0 +1,103 @@ +# This file is part of the sos project: https://github.com/sosreport/sos +# +# This copyrighted material is made available to anyone wishing to use, +# modify, copy, or redistribute it subject to the terms and conditions of +# version 2 of the GNU General Public License. +# +# See the LICENSE file in the source distribution for further information. +import os.path +import unittest + +# PYCOMPAT +from io import StringIO + +from sos.utilities import (grep, is_executable, sos_get_command_output, + find, tail, shell_out) + +TEST_DIR = os.path.dirname(__file__) + + +class GrepTest(unittest.TestCase): + + def test_file_obj(self): + test_s = u"\n".join( + ['this is only a test', 'there are only two lines']) + test_fo = StringIO(test_s) + matches = grep(".*test$", test_fo) + self.assertEquals(matches, ['this is only a test\n']) + + def test_real_file(self): + matches = grep(".*unittest$", __file__.replace(".pyc", ".py")) + self.assertEquals(matches, ['import unittest\n']) + + def test_open_file(self): + matches = grep(".*unittest$", open(__file__.replace(".pyc", ".py"))) + self.assertEquals(matches, ['import unittest\n']) + + def test_grep_multiple_files(self): + matches = grep(".*unittest$", + __file__.replace(".pyc", ".py"), "does_not_exist.txt") + self.assertEquals(matches, ['import unittest\n']) + + +class TailTest(unittest.TestCase): + + def test_tail(self): + t = tail("tests/tail_test.txt", 10) + self.assertEquals(t, b"last line\n") + + def test_tail_too_many(self): + t = tail("tests/tail_test.txt", 200) + expected = open("tests/tail_test.txt", "r").read() + self.assertEquals(t, str.encode(expected)) + + +class ExecutableTest(unittest.TestCase): + + def test_nonexe_file(self): + path = os.path.join(TEST_DIR, 'utility_tests.py') + self.assertFalse(is_executable(path)) + + def test_exe_file(self): + self.assertTrue(is_executable('true')) + + def test_exe_file_abs_path(self): + self.assertTrue(is_executable("/usr/bin/timeout")) + + def test_output(self): + result = sos_get_command_output("echo executed") + self.assertEquals(result['status'], 0) + self.assertEquals(result['output'], "executed\n") + + def test_output_non_exe(self): + path = os.path.join(TEST_DIR, 'utility_tests.py') + result = sos_get_command_output(path) + self.assertEquals(result['status'], 127) + self.assertEquals(result['output'], b"") + + def test_output_chdir(self): + cmd = "/bin/bash -c 'echo $PWD'" + result = sos_get_command_output(cmd, chdir=TEST_DIR) + print(result) + self.assertEquals(result['status'], 0) + self.assertEquals(result['output'].strip(), TEST_DIR) + + def test_shell_out(self): + self.assertEquals("executed\n", shell_out('echo executed')) + + +class FindTest(unittest.TestCase): + + def test_find_leaf(self): + leaves = find("leaf", TEST_DIR) + self.assertTrue(any(name.endswith("leaf") for name in leaves)) + + def test_too_shallow(self): + leaves = find("leaf", TEST_DIR, max_depth=1) + self.assertFalse(any(name.endswith("leaf") for name in leaves)) + + def test_not_in_pattern(self): + leaves = find("leaf", TEST_DIR, path_pattern="tests/path") + self.assertFalse(any(name.endswith("leaf") for name in leaves)) + +# vim: set et ts=4 sw=4 : |