2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-08-31 06:16:03 +00:00

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 1259319508
("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 <steve.beattie@canonical.com>
Approved-by: John Johansen <john@jjmx.net>
This commit is contained in:
Steve Beattie
2023-07-11 14:20:55 -05:00
parent a271b2474c
commit 12cf66ff0b

View File

@@ -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