From 12cf66ff0bacdbf990b81731c9d99d403a11e9dc Mon Sep 17 00:00:00 2001 From: Steve Beattie Date: Tue, 11 Jul 2023 14:20:55 -0500 Subject: [PATCH 1/2] parser/errors.py: check error message + error code for non-existent profiles Add tests for passing the parser a file that doesn't exist, a symlink to a file that doesn't exist, and a directory that contains that latter. Also include tests for different levels of -j passed as an argument. These tests are based on the fixing commit 125931950838 ("parser: Fix parser failing to handle errors when setting up work") MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1070 Signed-off-by: Steve Beattie Approved-by: John Johansen --- parser/tst/errors.py | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/parser/tst/errors.py b/parser/tst/errors.py index ad4900c55..7a7cfc2d8 100755 --- a/parser/tst/errors.py +++ b/parser/tst/errors.py @@ -13,10 +13,13 @@ # # ------------------------------------------------------------------ +import os import subprocess import sys import unittest from argparse import ArgumentParser +from shutil import rmtree +from tempfile import mkdtemp import testlib @@ -28,6 +31,15 @@ class AAErrorTests(testlib.AATestTemplate): self.maxDiff = None self.cmd_prefix = [config.parser, '--config-file=./parser.conf', '-S', '-I', 'errors'] + self.tmpdir = os.path.realpath(mkdtemp(prefix='test-aa-parser-errors-')) + self.profile_dir = os.path.join(self.tmpdir, 'profile') + os.mkdir(self.profile_dir) + + + def tearDown(self): + if os.path.exists(self.tmpdir): + rmtree(self.tmpdir) + def _run_test(self, profile, message='', is_error=True): cmd = self.cmd_prefix + [profile] @@ -90,6 +102,56 @@ class AAErrorTests(testlib.AATestTemplate): is_error=False ) + def test_non_existant_profile(self): + test_profile = os.path.join(self.profile_dir, "does-not-exist.sd") + self._run_test( + test_profile, + "File {} not found, skipping...\n".format(test_profile), + ) + + # We can run this test with multiple different arguments + def _test_non_existant_symlink_target(self): + """Helper Function to test the parser on a symlink with a non-existent target""" + + test_profile = os.path.join(self.profile_dir, "non-existant-target.sd") + os.symlink('does-not-exist.sd', test_profile) + self._run_test( + test_profile, + "File {} not found, skipping...\n".format(test_profile), + ) + + def test_non_existant_symlink_target(self): + '''Basic symlink test that goes nowhere''' + self._test_non_existant_symlink_target() + + def test_non_existant_symlink_target_j0(self): + '''Basic symlink test that goes nowhere with 0 jobs''' + self.cmd_prefix.append('-j0') + self._test_non_existant_symlink_target() + + def test_non_existant_symlink_target_j1(self): + '''Basic symlink test that goes nowhere with 1 job arg''' + self.cmd_prefix.append('-j1') + self._test_non_existant_symlink_target() + + def test_non_existant_symlink_target_j8(self): + '''Basic symlink test that goes nowhere with 8 job arg''' + self.cmd_prefix.append('-j8') + self._test_non_existant_symlink_target() + + def test_non_existant_symlink_target_jauto(self): + '''Basic symlink test that goes nowhere with auto job arg''' + self.cmd_prefix.append('-jauto') + self._test_non_existant_symlink_target() + + def test_non_existant_symlink_target_in_directory(self): + '''Symlink test passing a directory to the parser''' + test_profile = os.path.join(self.profile_dir, "non-existant-target.sd") + os.symlink('does-not-exist.sd', test_profile) + self._run_test( + self.profile_dir, + "There was an error while loading profiles from {}\n".format(self.profile_dir), + ) def main(): global config From 87896b949641d026ca95cb033cdaf273af811b66 Mon Sep 17 00:00:00 2001 From: Steve Beattie Date: Thu, 13 Jul 2023 12:31:42 -0500 Subject: [PATCH 2/2] parser/errors.py: convert to unittest.main() Do this to simplify test identification, and also support the different invocation mechanisms of unittest, like running individual tests. MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/1070 Signed-off-by: Steve Beattie Approved-by: John Johansen --- parser/tst/errors.py | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/parser/tst/errors.py b/parser/tst/errors.py index 7a7cfc2d8..28a3b19bc 100755 --- a/parser/tst/errors.py +++ b/parser/tst/errors.py @@ -158,22 +158,9 @@ def main(): p = ArgumentParser() p.add_argument('-p', '--parser', default=testlib.DEFAULT_PARSER, action="store", dest='parser', help="Specify path of apparmor parser to use [default = %(default)s]") - p.add_argument('-v', '--verbose', action="store_true", dest="verbose") - config = p.parse_args() - - verbosity = 2 if config.verbose else 1 - - test_suite = unittest.TestSuite() - test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AAErrorTests)) - try: - result = unittest.TextTestRunner(verbosity=verbosity).run(test_suite) - except Exception: - rc = 1 - else: - rc = 0 if result.wasSuccessful() else 1 - - return rc + config, args = p.parse_known_args() + unittest.main(argv=sys.argv[:1] + args) if __name__ == "__main__": - sys.exit(main()) + main()