mirror of
https://gitlab.com/apparmor/apparmor.git
synced 2025-03-04 16:35:02 +01:00

This patch rewrites the caching test in python, using python's unittest framework. It has been used with python 2.7 and python 3.3; python2.6 may have issues. It covers the tests in the existing caching.sh test script (with the exception of the test that checks for when the parser in $PATH is newer), as well as adding additional tests that more extensively cover using a cache in an alternate location from basedir. It also adds simple tests for the --create-cache-dir option (along with that option's interaction with the alt-cache option). (Some further work to be done is listed under TODO.) Patch history: v1: - initial version v2: - create template base class - add keep_on_fail() decorator to keep temporary test files around after a test fails - don't dump raw cache file to failure output in test_cache_writing_updates_cache_file() - push run_cmd into template class - create run_cmd_check wrapper to run_cmd that adds an assertion check based on whether return code matches the expected rc (the valgrind tests only want to verify that the rc is not a specific set of values, hence the separate wrapper function) - similarly, add a check to run_cmd_check for verifying the output contains a specific string, also simplifying many of the caching tests. - create testlib.write_file() to simplify writing file Signed-off-by: Steve Beattie <steve@nxnw.org> Acked-by: Christian Boltz <apparmor@cboltz.de>
146 lines
4.2 KiB
Python
Executable file
146 lines
4.2 KiB
Python
Executable file
#!/usr/bin/env python
|
|
# ------------------------------------------------------------------
|
|
#
|
|
# Copyright (C) 2013 Canonical Ltd.
|
|
# Author: Steve Beattie <steve@nxnw.org>
|
|
#
|
|
# This program is free software; you can redistribute it and/or
|
|
# modify it under the terms of version 2 of the GNU General Public
|
|
# License published by the Free Software Foundation.
|
|
#
|
|
# ------------------------------------------------------------------
|
|
|
|
# TODO
|
|
# - finish adding suppressions for valgrind false positives
|
|
|
|
from argparse import ArgumentParser # requires python 2.7 or newer
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
import testlib
|
|
|
|
DEFAULT_TESTDIR = "./simple_tests/vars"
|
|
VALGRIND_ERROR_CODE = 151
|
|
VALGRIND_ARGS = ['--leak-check=full', '--error-exitcode=%d' % (VALGRIND_ERROR_CODE)]
|
|
|
|
VALGRIND_SUPPRESSIONS = '''
|
|
{
|
|
valgrind-add_search_dir-obsessive-overreads
|
|
Memcheck:Addr4
|
|
fun:_Z*add_search_dir*
|
|
fun:_Z*process_arg*
|
|
fun:main
|
|
}
|
|
|
|
{
|
|
valgrind-yylex-obsessive-overreads
|
|
Memcheck:Addr4
|
|
fun:_Z?yylex?
|
|
fun:_Z*yyparse*
|
|
fun:_Z*process_profile*
|
|
fun:main
|
|
}
|
|
|
|
{
|
|
valgrind-serialize_profile-obsessive-overreads
|
|
Memcheck:Addr4
|
|
fun:_Z*sd_serialize_profile*
|
|
fun:_Z*sd_serialize_codomain*
|
|
fun:_Z*load_codomain*
|
|
fun:_Z*__load_flattened_hat*
|
|
...
|
|
fun:twalk
|
|
fun:_Z*load_flattened_hats*
|
|
fun:_Z*sd_serialize_codomain*
|
|
fun:_Z*load_codomain*
|
|
fun:_Z*__load_policy*
|
|
}'''
|
|
|
|
|
|
class AAParserValgrindTests(testlib.AATestTemplate):
|
|
def setUp(self):
|
|
# REPORT ALL THE OUTPUT
|
|
self.maxDiff = None
|
|
|
|
def _runtest(self, testname, config):
|
|
parser_args = ['-Q', '-I', config.testdir]
|
|
failure_rc = [VALGRIND_ERROR_CODE, testlib.TIMEOUT_ERROR_CODE]
|
|
command = ['valgrind']
|
|
command.extend(VALGRIND_ARGS)
|
|
command.append(config.parser)
|
|
command.extend(parser_args)
|
|
command.append(testname)
|
|
rc, output = self.run_cmd(command, timeout=120)
|
|
self.assertNotIn(rc, failure_rc,
|
|
"valgrind returned error code %d, gave the following output\n%s" % (rc, output))
|
|
|
|
|
|
def find_testcases(testdir):
|
|
'''dig testcases out of passed directory'''
|
|
|
|
for (fdir, direntries, files) in os.walk(testdir):
|
|
for f in files:
|
|
if f.endswith(".sd"):
|
|
yield os.path.join(fdir, f)
|
|
|
|
|
|
def create_suppressions():
|
|
'''generate valgrind suppressions file'''
|
|
|
|
handle, name = tempfile.mkstemp(suffix='.suppressions', prefix='aa-parser-valgrind')
|
|
os.close(handle)
|
|
handle = open(name,"w+")
|
|
handle.write(VALGRIND_SUPPRESSIONS)
|
|
handle.close()
|
|
return name
|
|
|
|
def main():
|
|
rc = 0
|
|
p = ArgumentParser()
|
|
p.add_argument('-p', '--parser', default=testlib.DEFAULT_PARSER, action="store", dest='parser')
|
|
p.add_argument('-v', '--verbose', action="store_true", dest="verbose")
|
|
p.add_argument('-s', '--skip-suppressions', action="store_true", dest="skip_suppressions",
|
|
help="Don't use valgrind suppressions to skip false positives")
|
|
p.add_argument('--dump-suppressions', action="store_true", dest="dump_suppressions",
|
|
help="Dump valgrind suppressions to stdout")
|
|
config, args = p.parse_args()
|
|
|
|
if config.dump_suppressions:
|
|
print(VALGRIND_SUPPRESSIONS)
|
|
return rc
|
|
|
|
verbosity = 1
|
|
if config.verbose:
|
|
verbosity = 2
|
|
|
|
if len(args) == 1:
|
|
config.testdir = args[0]
|
|
else:
|
|
config.testdir = DEFAULT_TESTDIR
|
|
|
|
if not config.skip_suppressions:
|
|
suppression_file = create_suppressions()
|
|
VALGRIND_ARGS.append('--suppressions=%s' % (suppression_file))
|
|
|
|
for f in find_testcases(config.testdir):
|
|
def stub_test(self, testname=f):
|
|
self._runtest(testname, config)
|
|
stub_test.__doc__ = "test %s" % (f)
|
|
setattr(AAParserValgrindTests, 'test_%s' % (f), stub_test)
|
|
test_suite = unittest.TestSuite()
|
|
test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(AAParserValgrindTests))
|
|
|
|
try:
|
|
result = unittest.TextTestRunner(verbosity=verbosity).run(test_suite)
|
|
if not result.wasSuccessful():
|
|
rc = 1
|
|
except:
|
|
rc = 1
|
|
finally:
|
|
os.remove(suppression_file)
|
|
|
|
return rc
|
|
|
|
if __name__ == "__main__":
|
|
rc = main()
|
|
exit(rc)
|