Files
archy/scripts/tests/test_exec_profile_fixture.py

62 lines
3.0 KiB
Python

import hashlib
import tempfile
from pathlib import Path
import unittest
from unittest.mock import patch
from with_exec_profile import loaded_profile
class ExecProfileFixtureTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.root = Path(self.tmp.name)
template = b'profile @PROFILE@ {}\n'
path = self.root / 'image-recipe/configs/archipelago-manager-exec.profile.in'
path.parent.mkdir(parents=True)
path.write_bytes(template)
self.name = 'archipelago-manager-exec-' + hashlib.sha256(template).hexdigest()
def snapshot(self, mode=None, bound=False):
profiles = {'unrelated': 'enforce'}
if mode:
profiles[self.name] = mode
return {'profiles': profiles, 'processes': {'test': [{'profile': self.name}] if bound else []}}
def test_adds_exact_policy_and_cleans_it_even_if_test_command_fails(self):
states = [self.snapshot(), self.snapshot('enforce'), self.snapshot('enforce'), self.snapshot()]
with patch('with_exec_profile.inventory', side_effect=states), patch('with_exec_profile.parser') as parser:
with self.assertRaisesRegex(ValueError, 'test command failed'):
with loaded_profile(self.root) as name:
self.assertEqual(name, self.name)
raise ValueError('test command failed')
self.assertEqual([call.args[0] for call in parser.call_args_list], ['-a', '-R'])
self.assertEqual(parser.call_args_list[0].args[1], parser.call_args_list[1].args[1])
self.assertIn(self.name.encode(), parser.call_args_list[0].args[1])
def test_borrows_existing_enforcing_policy_without_changing_it(self):
with patch('with_exec_profile.inventory', return_value=self.snapshot('enforce')), patch('with_exec_profile.parser') as parser:
with loaded_profile(self.root):
pass
parser.assert_not_called()
def test_refuses_to_replace_existing_non_enforcing_policy(self):
with patch('with_exec_profile.inventory', return_value=self.snapshot('complain')), patch('with_exec_profile.parser') as parser:
with self.assertRaisesRegex(RuntimeError, 'not enforcing'):
with loaded_profile(self.root):
self.fail('must not run tests under a non-enforcing policy')
parser.assert_not_called()
def test_never_unloads_policy_while_processes_are_bound(self):
states = [self.snapshot(), self.snapshot('enforce'), self.snapshot('enforce', bound=True)]
with patch('with_exec_profile.inventory', side_effect=states), patch('with_exec_profile.parser') as parser:
with self.assertRaisesRegex(RuntimeError, 'bound processes'):
with loaded_profile(self.root):
pass
self.assertEqual([call.args[0] for call in parser.call_args_list], ['-a'])
if __name__ == '__main__':
unittest.main()