[apparmor] [patch] convert test-regex_matches.py to new tests[] loop
Steve Beattie
steve at nxnw.org
Tue Mar 31 10:00:51 UTC 2015
On Fri, Mar 06, 2015 at 11:58:00PM +0100, Christian Boltz wrote:
> Am Mittwoch, 4. März 2015 schrieb Christian Boltz:
> > this patch converts test-regex_matches.py to the new tests[] loop.
> >
> > Needless to say that this patch depends on
> > [patch] add better loop support to common_test.py
> >
> > The test behaviour is the same with and without this patch - 166 tests
> > run, all successful.
> >
> > BTW: I don't care too much for getting rid of
> > setup_has_comma_testcases() and setup_split_comment_testcases() - the
> > important point of this patch is to get rid of the list of test
> > classes and the risk of forgetting to add new classes there.
> >
> > Oh, just in case you wonder - some parts of this patch are
> > hand-formatted ;-)
> >
> >
> > As usual, I propose this patch for trunk and 2.9.
>
> Here's v2 that includes the simplified setup_all_tests() syntax:
This suffers (unsurprisingly) from the same python2.7 problem as the
07-common_test_better_loop_support.diff patch does:
$ PYTHONPATH=.. python2.7 ./test-regex_matches.py -v
Traceback (most recent call last):
File "./test-regex_matches.py", line 395, in <module>
setup_all_tests()
File "/home/steve/bzr/apparmor-master/utils/test/common_test.py", line 60, in setup_all_tests
obj_instance = obj() # will fail for (non-test) classes with additional __init__ parameters
File "/usr/lib/python2.7/unittest/case.py", line 189, in __init__
(self.__class__, methodName))
ValueError: no such test method in <class '__main__.AARegexBareFile'>: runTest
That said, I'm assuming the solution for the issue will land in
common_test.py, so once 07-common_test_better_loop_support.diff is in
good shape, this would be Acked-by: Steve Beattie <steve at nxnw.org>,
too.
> [ 08-convert-test-regex_matches.diff ]
>
> === modified file 'utils/test/test-regex_matches.py'
> --- utils/test/test-regex_matches.py 2015-03-03 19:15:00 +0000
> +++ utils/test/test-regex_matches.py 2015-03-03 23:45:38 +0000
> @@ -11,10 +11,16 @@
>
> import apparmor.aa as aa
> import unittest
> +from common_test import AATest, setup_all_tests
>
> from apparmor.regex import strip_quotes
>
> +class AARegexTest(AATest):
> + def _run_test(self, params, expected):
> + return regex_test(self, params, expected)
> +
> +
> -class AARegexHasComma(unittest.TestCase):
> +class AARegexHasComma(AATest):
> '''Tests for apparmor.aa.RE_RULE_HAS_COMMA'''
>
> def _check(self, line, expected=True):
> @@ -94,7 +101,7 @@
> setattr(AARegexHasComma, 'test_comma_%d' % (i), stub_test_comma)
> setattr(AARegexHasComma, 'test_no_comma_%d' % (i), stub_test_no_comma)
>
> -class AARegexSplitComment(unittest.TestCase):
> +class AARegexSplitComment(AATest):
> '''Tests for RE_HAS_COMMENT_SPLIT'''
>
> def _check(self, line, expected, comment=None, not_comment=None):
> @@ -166,23 +173,10 @@
> self.assertEqual(group, expected[i], 'Group %d mismatch in rule %s' % (i,line))
>
>
> -def setup_regex_tests(test_class):
> - '''Create tests in test_class using test_class.tests and regex_tests()
> -
> - test_class.tests should be tuples of (line, expected_results) where
> - expected_results is False if test_class.regex.search(line) should not
> - match. If the search should match, expected_results should be a tuple of
> - the expected groups, with all of the strings stripped.
> - '''
> - for (i, (line, expected)) in enumerate(test_class.tests):
> - def stub_test(self, line=line, expected=expected):
> - regex_test(self, line, expected)
> -
> - stub_test.__doc__ = "test '%s'" % (line)
> - setattr(test_class, 'test_%d' % (i), stub_test)
>
>
> +
> -class AARegexCapability(unittest.TestCase):
> +class AARegexCapability(AARegexTest):
> '''Tests for RE_PROFILE_CAP'''
>
> def setUp(self):
> @@ -197,7 +191,7 @@
> ]
>
>
> -class AARegexPath(unittest.TestCase):
> +class AARegexPath(AARegexTest):
> '''Tests for RE_PROFILE_PATH_ENTRY'''
>
> def setUp(self):
> @@ -216,7 +210,7 @@
> ]
>
>
> -class AARegexBareFile(unittest.TestCase):
> +class AARegexBareFile(AARegexTest):
> '''Tests for RE_PROFILE_BARE_FILE_ENTRY'''
>
> def setUp(self):
> @@ -234,7 +228,7 @@
> ]
>
>
> -class AARegexDbus(unittest.TestCase):
> +class AARegexDbus(AARegexTest):
> '''Tests for RE_PROFILE_DBUS'''
>
> def setUp(self):
> @@ -250,7 +244,7 @@
> (' audit dbusdriver,', False),
> ]
>
> -class AARegexMount(unittest.TestCase):
> +class AARegexMount(AARegexTest):
> '''Tests for RE_PROFILE_MOUNT'''
>
> def setUp(self):
> @@ -273,7 +267,7 @@
> ]
>
>
> -class AARegexSignal(unittest.TestCase):
> +class AARegexSignal(AARegexTest):
> '''Tests for RE_PROFILE_SIGNAL'''
>
> def setUp(self):
> @@ -300,7 +294,7 @@
> ]
>
>
> -class AARegexPtrace(unittest.TestCase):
> +class AARegexPtrace(AARegexTest):
> '''Tests for RE_PROFILE_PTRACE'''
>
> def setUp(self):
> @@ -322,7 +316,7 @@
> ]
>
>
> -class AARegexPivotRoot(unittest.TestCase):
> +class AARegexPivotRoot(AARegexTest):
> '''Tests for RE_PROFILE_PIVOT_ROOT'''
>
> def setUp(self):
> @@ -349,7 +343,7 @@
> ('pivot_rootbeer /new, # comment', False),
> ]
>
> -class AARegexUnix(unittest.TestCase):
> +class AARegexUnix(AARegexTest):
> '''Tests for RE_PROFILE_UNIX'''
>
> def setUp(self):
> @@ -374,7 +368,7 @@
> ('deny unixlike,', False),
> ]
>
> -class TestStripQuotes(unittest.TestCase):
> +class TestStripQuotes(AATest):
> def test_strip_quotes_01(self):
> self.assertEqual('foo', strip_quotes('foo'))
> def test_strip_quotes_02(self):
> @@ -394,22 +388,9 @@
>
>
> if __name__ == '__main__':
> - verbosity = 2
> -
> + # these two are not converted to a tests[] loop yet
> setup_has_comma_testcases()
> setup_split_comment_testcases()
>
> - test_suite = unittest.TestSuite()
> - test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AARegexHasComma))
> - test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AARegexSplitComment))
> - test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestStripQuotes))
> -
> - for tests in (AARegexCapability, AARegexPath, AARegexBareFile,
> - AARegexDbus, AARegexMount, AARegexUnix,
> - AARegexSignal, AARegexPtrace, AARegexPivotRoot):
> - setup_regex_tests(tests)
> - test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(tests))
> -
> - result = unittest.TextTestRunner(verbosity=verbosity).run(test_suite)
> - if not result.wasSuccessful():
> - exit(1)
> + setup_all_tests()
> + unittest.main(verbosity=2)
>
>
>
> Regards,
>
> Christian Boltz
> --
> - Macs sind für die, die nicht wissen wollen, warum Ihr Rechner funzt.
> - Linux ist für die, die wissen wollen, warum er funzt.
> - DOS ist für die, die wissen wollen, warum er nicht funzt, und
> - Windows ist für die, die nicht wissen wollen, warum er nicht funzt.
>
>
> --
> AppArmor mailing list
> AppArmor at lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/apparmor
--
Steve Beattie
<sbeattie at ubuntu.com>
http://NxNW.org/~steve/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20150331/13f07b85/attachment.pgp>
More information about the AppArmor
mailing list