Merge "Use oslo.polcy DocumentedRuleDefault"

This commit is contained in:
Jenkins 2017-07-14 12:32:10 +00:00 committed by Gerrit Code Review
commit 57360fb72a
3 changed files with 8 additions and 97 deletions

View File

@ -28,39 +28,10 @@ rules = [
] ]
def _unwrap_text(text): # TODO(johngarbutt) we can remove this now
def _split_paragraphs(lines):
output = []
for line in lines.split('\n'):
if line:
output.append(line.strip())
elif output:
yield ' '.join(output)
output = []
if output:
yield ' '.join(output)
return '\n\n'.join([x for x in _split_paragraphs(text)])
def create_rule_default(name, check_str, description, operations): def create_rule_default(name, check_str, description, operations):
# TODO(sneti): use DocumentedRuleDefault instead of RuleDefault when return policy.DocumentedRuleDefault(name, check_str,
# oslo.policy library is bumped to 1.21.0. The formatted_description hack description, operations)
# can be removed then.
ops = ""
for operation in operations:
ops += ('%(method)s %(path)s\n' %
{'method': operation['method'],
'path': operation['path']})
template = """%(description)s\n\n%(operations)s\n"""
formatted_description = template % {
"description": _unwrap_text(description),
"operations": ops,
}
return policy.RuleDefault(name, check_str, formatted_description)
def list_rules(): def list_rules():

View File

@ -13,29 +13,27 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from nova.policies import base from oslo_policy import policy
POLICY_ROOT = 'cells_scheduler_filter:%s' POLICY_ROOT = 'cells_scheduler_filter:%s'
cells_scheduler_policies = [ cells_scheduler_policies = [
base.create_rule_default( policy.RuleDefault(
POLICY_ROOT % 'DifferentCellFilter', POLICY_ROOT % 'DifferentCellFilter',
'is_admin:True', 'is_admin:True',
"""Different cell filter to route a build away from a particular cell """Different cell filter to route a build away from a particular cell
This policy is read by nova-scheduler process. This policy is read by nova-scheduler process.
""", """),
[]), policy.RuleDefault(
base.create_rule_default(
POLICY_ROOT % 'TargetCellFilter', POLICY_ROOT % 'TargetCellFilter',
'is_admin:True', 'is_admin:True',
"""Target cell filter to route a build to a particular cell """Target cell filter to route a build to a particular cell
This policy is read by nova-scheduler process. This policy is read by nova-scheduler process.
""", """)
[])
] ]

View File

@ -25,7 +25,6 @@ import requests_mock
import nova.conf import nova.conf
from nova import context from nova import context
from nova import exception from nova import exception
from nova.policies import base
from nova import policy from nova import policy
from nova import test from nova import test
from nova.tests.unit import fake_policy from nova.tests.unit import fake_policy
@ -236,63 +235,6 @@ class AdminRolePolicyTestCase(test.NoDBTestCase):
self.context, action, self.target) self.context, action, self.target)
class PolicyDocsTestCase(test.NoDBTestCase):
def test_create_rule_default(self):
rule_default = base.create_rule_default(
"name", "check_str", "description goes in here",
[{'method': 'GET', 'path': '/test_url'},
{'method': 'POST', 'path': '/test_url'}])
expected = """description goes in here
GET /test_url
POST /test_url
"""
self.assertEqual(expected, rule_default.description)
self.assertEqual("name", rule_default.name)
self.assertEqual("check_str", rule_default.check_str)
def test_create_rule_default_improper_formatting(self):
"""Ensure we correctly handle various formatting misdemeanours."""
rule_default = base.create_rule_default(
"name",
"check_str",
"""Joe's special policy.
This is Joe's special policy. Joe writes
really, really long sentences that should really be wrapped better than \
they are.
Unfortunately we can't expect him
to just do
things
right, so we must fix the problem ourselves.
Oh, Joe. You so silly.
""",
[{'method': 'GET', 'path': '/test_url'},
{'method': 'POST', 'path': '/test_url'}])
expected = """Joe's special policy.
This is Joe's special policy. Joe writes really, really long sentences \
that should really be wrapped better than they are. Unfortunately we \
can't expect him to just do things right, so we must fix the problem \
ourselves.
Oh, Joe. You so silly.
GET /test_url
POST /test_url
"""
self.assertEqual(expected, rule_default.description)
self.assertEqual("name", rule_default.name)
self.assertEqual("check_str", rule_default.check_str)
class RealRolePolicyTestCase(test.NoDBTestCase): class RealRolePolicyTestCase(test.NoDBTestCase):
def setUp(self): def setUp(self):
super(RealRolePolicyTestCase, self).setUp() super(RealRolePolicyTestCase, self).setUp()