Improve policy sample generation testing
The tests for policy sample generation were replacing stevedore.named.NamedExtensionManager with a mock. This allows tests to form a mock with a different api than NamedExtensionManager and run code against that, which may pass the test and then fail when a real NamedExtensionManager is used. Instead a NamedExtensionManager.make_test_instance() should be returned which enforces the same api as NamedExtensionManager and would cause a test failure if used improperly. Change-Id: I6db1b92fd90956f727a6422524623aee73f8c416
This commit is contained in:
parent
474c120ae6
commit
9050c428d6
@ -15,6 +15,7 @@ import fixtures
|
|||||||
import mock
|
import mock
|
||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
from six import moves
|
from six import moves
|
||||||
|
import stevedore
|
||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
from oslo_policy import generator
|
from oslo_policy import generator
|
||||||
@ -46,17 +47,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()
|
||||||
|
|
||||||
@ -69,14 +76,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"
|
||||||
@ -89,21 +96,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"
|
||||||
@ -116,7 +129,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