ProfileList: add __getitem__()

... and add some tests for it.
This commit is contained in:
Christian Boltz 2024-10-27 16:28:23 +01:00
parent b66dfd8bfb
commit a37c65957f
Failed to generate hash of commit
2 changed files with 11 additions and 0 deletions

View file

@ -52,6 +52,12 @@ class ProfileList:
name = type(self).__name__
return '\n<%s>\n%s\n</%s>\n' % (name, '\n'.join(self.files), name)
def __getitem__(self, key):
if key in self.profiles:
return self.profiles[key]
else:
raise AppArmorBug('attempt to read unknown profile %s' % key)
def init_file(self, filename):
if self.files.get(filename):
return # don't re-initialize / overwrite existing data

View file

@ -47,6 +47,11 @@ class TestAdd_profile(AATest):
self.assertEqual(self.pl.profiles_in_file('/etc/apparmor.d/bin.foo'), ['foo'])
self.assertEqual(str(self.pl), '\n<ProfileList>\n/etc/apparmor.d/bin.foo\n</ProfileList>\n')
# test __getitem__()
self.assertEqual(self.pl['foo'], self.dummy_profile)
with self.assertRaises(AppArmorBug):
self.pl['does_not_exist']
def testAdd_profile_2(self):
self.assertFalse(self.pl.profile_exists('foo'))
self.assertFalse(self.pl.profile_exists('/bin/foo'))