Merge "Improve policy sample generation testing"
This commit is contained in:
commit
714180716b
@ -48,17 +48,23 @@ class GenerateSampleTestCase(base.PolicyBaseTestCase):
|
|||||||
self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO()))
|
self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO()))
|
||||||
return sys.stdout
|
return sys.stdout
|
||||||
|
|
||||||
@mock.patch('stevedore.named.NamedExtensionManager')
|
def test_generate_loadable_yaml(self):
|
||||||
def test_generate_loadable_yaml(self, mock_named_mgr):
|
extensions = []
|
||||||
mock_eps = []
|
|
||||||
for name, opts in OPTS.items():
|
for name, opts in OPTS.items():
|
||||||
mock_ep = mock.Mock()
|
ext = stevedore.extension.Extension(name=name, entry_point=None,
|
||||||
mock_ep.configure_mock(name=name, obj=opts)
|
plugin=None, obj=opts)
|
||||||
mock_eps.append(mock_ep)
|
extensions.append(ext)
|
||||||
mock_named_mgr.return_value = mock_eps
|
test_mgr = stevedore.named.NamedExtensionManager.make_test_instance(
|
||||||
|
extensions=extensions, namespace=['base_rules', 'rules'])
|
||||||
|
|
||||||
output_file = self.get_config_file_fullname('policy.yaml')
|
output_file = self.get_config_file_fullname('policy.yaml')
|
||||||
generator._generate_sample(['base_rules', 'rules'], output_file)
|
with mock.patch('stevedore.named.NamedExtensionManager',
|
||||||
|
return_value=test_mgr) as mock_ext_mgr:
|
||||||
|
generator._generate_sample(['base_rules', 'rules'], output_file)
|
||||||
|
mock_ext_mgr.assert_called_once_with(
|
||||||
|
'oslo.policy.policies', names=['base_rules', 'rules'],
|
||||||
|
on_load_failure_callback=generator.on_load_failure_callback,
|
||||||
|
invoke_on_load=True)
|
||||||
|
|
||||||
self.enforcer.load_rules()
|
self.enforcer.load_rules()
|
||||||
|
|
||||||
@ -71,14 +77,14 @@ class GenerateSampleTestCase(base.PolicyBaseTestCase):
|
|||||||
self.assertEqual('(rule:admin or rule:owner)',
|
self.assertEqual('(rule:admin or rule:owner)',
|
||||||
str(self.enforcer.rules['admin_or_owner']))
|
str(self.enforcer.rules['admin_or_owner']))
|
||||||
|
|
||||||
@mock.patch('stevedore.named.NamedExtensionManager')
|
def test_expected_content(self):
|
||||||
def test_expected_content(self, mock_named_mgr):
|
extensions = []
|
||||||
mock_eps = []
|
|
||||||
for name, opts in OPTS.items():
|
for name, opts in OPTS.items():
|
||||||
mock_ep = mock.Mock()
|
ext = stevedore.extension.Extension(name=name, entry_point=None,
|
||||||
mock_ep.configure_mock(name=name, obj=opts)
|
plugin=None, obj=opts)
|
||||||
mock_eps.append(mock_ep)
|
extensions.append(ext)
|
||||||
mock_named_mgr.return_value = mock_eps
|
test_mgr = stevedore.named.NamedExtensionManager.make_test_instance(
|
||||||
|
extensions=extensions, namespace=['base_rules', 'rules'])
|
||||||
|
|
||||||
expected = '''# Basic admin check
|
expected = '''# Basic admin check
|
||||||
"admin": "is_admin:True"
|
"admin": "is_admin:True"
|
||||||
@ -91,21 +97,27 @@ class GenerateSampleTestCase(base.PolicyBaseTestCase):
|
|||||||
"admin_or_owner": "rule:admin or rule:owner"
|
"admin_or_owner": "rule:admin or rule:owner"
|
||||||
'''
|
'''
|
||||||
output_file = self.get_config_file_fullname('policy.yaml')
|
output_file = self.get_config_file_fullname('policy.yaml')
|
||||||
generator._generate_sample(['base_rules', 'rules'], output_file)
|
with mock.patch('stevedore.named.NamedExtensionManager',
|
||||||
|
return_value=test_mgr) as mock_ext_mgr:
|
||||||
|
generator._generate_sample(['base_rules', 'rules'], output_file)
|
||||||
|
mock_ext_mgr.assert_called_once_with(
|
||||||
|
'oslo.policy.policies', names=['base_rules', 'rules'],
|
||||||
|
on_load_failure_callback=generator.on_load_failure_callback,
|
||||||
|
invoke_on_load=True)
|
||||||
|
|
||||||
with open(output_file, 'r') as written_file:
|
with open(output_file, 'r') as written_file:
|
||||||
written_policy = written_file.read()
|
written_policy = written_file.read()
|
||||||
|
|
||||||
self.assertEqual(expected, written_policy)
|
self.assertEqual(expected, written_policy)
|
||||||
|
|
||||||
@mock.patch('stevedore.named.NamedExtensionManager')
|
def test_expected_content_stdout(self):
|
||||||
def test_expected_content_stdout(self, mock_named_mgr):
|
extensions = []
|
||||||
mock_eps = []
|
|
||||||
for name, opts in OPTS.items():
|
for name, opts in OPTS.items():
|
||||||
mock_ep = mock.Mock()
|
ext = stevedore.extension.Extension(name=name, entry_point=None,
|
||||||
mock_ep.configure_mock(name=name, obj=opts)
|
plugin=None, obj=opts)
|
||||||
mock_eps.append(mock_ep)
|
extensions.append(ext)
|
||||||
mock_named_mgr.return_value = mock_eps
|
test_mgr = stevedore.named.NamedExtensionManager.make_test_instance(
|
||||||
|
extensions=extensions, namespace=['base_rules', 'rules'])
|
||||||
|
|
||||||
expected = '''# Basic admin check
|
expected = '''# Basic admin check
|
||||||
"admin": "is_admin:True"
|
"admin": "is_admin:True"
|
||||||
@ -118,7 +130,14 @@ class GenerateSampleTestCase(base.PolicyBaseTestCase):
|
|||||||
"admin_or_owner": "rule:admin or rule:owner"
|
"admin_or_owner": "rule:admin or rule:owner"
|
||||||
'''
|
'''
|
||||||
stdout = self._capture_stdout()
|
stdout = self._capture_stdout()
|
||||||
generator._generate_sample(['base_rules', 'rules'], output_file=None)
|
with mock.patch('stevedore.named.NamedExtensionManager',
|
||||||
|
return_value=test_mgr) as mock_ext_mgr:
|
||||||
|
generator._generate_sample(['base_rules', 'rules'],
|
||||||
|
output_file=None)
|
||||||
|
mock_ext_mgr.assert_called_once_with(
|
||||||
|
'oslo.policy.policies', names=['base_rules', 'rules'],
|
||||||
|
on_load_failure_callback=generator.on_load_failure_callback,
|
||||||
|
invoke_on_load=True)
|
||||||
|
|
||||||
self.assertEqual(expected, stdout.getvalue())
|
self.assertEqual(expected, stdout.getvalue())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user