[apparmor] [patch] convert test-regex_matches.py to new tests[] loop

Christian Boltz apparmor at cboltz.de
Fri Mar 6 22:58:00 UTC 2015


Hello,

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:


[ 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.




More information about the AppArmor mailing list