Merge "Fix the unit tests about policy and the DocumentedRule description"

This commit is contained in:
Zuul 2017-10-16 07:05:04 +00:00 committed by Gerrit Code Review
commit 055235a850
14 changed files with 32 additions and 45 deletions

View File

@ -329,7 +329,7 @@ class PlansController(wsgi.Controller):
plan = self._plan_get(context, id) plan = self._plan_get(context, id)
except exception.PlanNotFound as error: except exception.PlanNotFound as error:
raise exc.HTTPNotFound(explanation=error.msg) raise exc.HTTPNotFound(explanation=error.msg)
context.can(plan_policy.UPDATE_POLICY, target_obj=plan)
self._plan_update(context, plan, update_dict) self._plan_update(context, plan, update_dict)
plan.update(update_dict) plan.update(update_dict)
@ -360,6 +360,7 @@ class PlansController(wsgi.Controller):
raise exception.InvalidPlan(reason=msg) raise exception.InvalidPlan(reason=msg)
# TODO(chenying) replication scene: need call rpc API when # TODO(chenying) replication scene: need call rpc API when
# the status of the plan is changed. # the status of the plan is changed.
context.can(plan_policy.UPDATE_POLICY, target_obj=plan)
if isinstance(plan, objects_base.KarborObject): if isinstance(plan, objects_base.KarborObject):
plan.update(fields) plan.update(fields)
plan.save() plan.save()

View File

@ -28,7 +28,7 @@ plans_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=CREATE_POLICY, name=CREATE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Create a plan.""", description='Create a plan.',
operations=[ operations=[
{ {
'method': 'POST', 'method': 'POST',
@ -38,7 +38,7 @@ plans_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=UPDATE_POLICY, name=UPDATE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Update a plan.""", description='Update a plan.',
operations=[ operations=[
{ {
'method': 'PUT', 'method': 'PUT',
@ -48,7 +48,7 @@ plans_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=DELETE_POLICY, name=DELETE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Delete a plan.""", description='Delete a plan.',
operations=[ operations=[
{ {
'method': 'DELETE', 'method': 'DELETE',
@ -58,7 +58,7 @@ plans_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_POLICY, name=GET_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Get a plan.""", description='Get a plan.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',
@ -68,7 +68,7 @@ plans_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_ALL_POLICY, name=GET_ALL_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Get plans.""", description='Get plans.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',

View File

@ -27,7 +27,7 @@ protectables_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_POLICY, name=GET_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Show a protectable type.""", description='Show a protectable type.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',
@ -37,7 +37,7 @@ protectables_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_ALL_POLICY, name=GET_ALL_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""List protectable types.""", description='List protectable types.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',
@ -47,7 +47,7 @@ protectables_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=INSTANCES_GET_POLICY, name=INSTANCES_GET_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Show a protectable instance.""", description='Show a protectable instance.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',
@ -58,7 +58,7 @@ protectables_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=INSTANCES_GET_ALL_POLICY, name=INSTANCES_GET_ALL_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""List protectable instances.""", description='List protectable instances.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',

View File

@ -30,7 +30,7 @@ providers_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_POLICY, name=GET_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Show a protection provider.""", description='Show a protection provider.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',
@ -40,7 +40,7 @@ providers_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_ALL_POLICY, name=GET_ALL_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""List protection providers.""", description='List protection providers.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',
@ -50,7 +50,7 @@ providers_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=CHECKPOINT_GET_POLICY, name=CHECKPOINT_GET_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Show a checkpoint.""", description='Show a checkpoint.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',
@ -60,7 +60,7 @@ providers_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=CHECKPOINT_GET_ALL_POLICY, name=CHECKPOINT_GET_ALL_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""List checkpoints.""", description='List checkpoints.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',
@ -70,7 +70,7 @@ providers_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=CHECKPOINT_CREATE_POLICY, name=CHECKPOINT_CREATE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Create checkpoint.""", description='Create checkpoint.',
operations=[ operations=[
{ {
'method': 'POST', 'method': 'POST',
@ -80,7 +80,7 @@ providers_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=CHECKPOINT_DELETE_POLICY, name=CHECKPOINT_DELETE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Delete checkpoint.""", description='Delete checkpoint.',
operations=[ operations=[
{ {
'method': 'DELETE', 'method': 'DELETE',

View File

@ -27,7 +27,7 @@ restores_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=CREATE_POLICY, name=CREATE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Create a restore.""", description='Create a restore.',
operations=[ operations=[
{ {
'method': 'POST', 'method': 'POST',
@ -37,7 +37,7 @@ restores_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=UPDATE_POLICY, name=UPDATE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Update a restore.""", description='Update a restore.',
operations=[ operations=[
{ {
'method': 'PUT', 'method': 'PUT',
@ -47,7 +47,7 @@ restores_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_POLICY, name=GET_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Get a restore.""", description='Get a restore.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',
@ -57,7 +57,7 @@ restores_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_ALL_POLICY, name=GET_ALL_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Get restores.""", description='Get restores.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',

View File

@ -27,7 +27,7 @@ scheduled_operations_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=CREATE_POLICY, name=CREATE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Create a scheduled_operation.""", description='Create a scheduled_operation.',
operations=[ operations=[
{ {
'method': 'POST', 'method': 'POST',
@ -37,7 +37,7 @@ scheduled_operations_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=DELETE_POLICY, name=DELETE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Delete a scheduled_operation.""", description='Delete a scheduled_operation.',
operations=[ operations=[
{ {
'method': 'DELETE', 'method': 'DELETE',
@ -47,7 +47,7 @@ scheduled_operations_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_POLICY, name=GET_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Get a scheduled_operation.""", description='Get a scheduled_operation.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',
@ -57,7 +57,7 @@ scheduled_operations_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_ALL_POLICY, name=GET_ALL_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Get scheduled_operations.""", description='Get scheduled_operations.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',

View File

@ -28,7 +28,7 @@ triggers_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=CREATE_POLICY, name=CREATE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Create a trigger.""", description='Create a trigger.',
operations=[ operations=[
{ {
'method': 'POST', 'method': 'POST',
@ -38,7 +38,7 @@ triggers_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=UPDATE_POLICY, name=UPDATE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Update a trigger.""", description='Update a trigger.',
operations=[ operations=[
{ {
'method': 'PUT', 'method': 'PUT',
@ -48,7 +48,7 @@ triggers_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=DELETE_POLICY, name=DELETE_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Delete a trigger.""", description='Delete a trigger.',
operations=[ operations=[
{ {
'method': 'DELETE', 'method': 'DELETE',
@ -58,7 +58,7 @@ triggers_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_POLICY, name=GET_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Get a trigger.""", description='Get a trigger.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',
@ -68,7 +68,7 @@ triggers_policies = [
policy.DocumentedRuleDefault( policy.DocumentedRuleDefault(
name=GET_ALL_POLICY, name=GET_ALL_POLICY,
check_str=base.RULE_ADMIN_OR_OWNER, check_str=base.RULE_ADMIN_OR_OWNER,
description="""Get triggerss.""", description='Get triggerss.',
operations=[ operations=[
{ {
'method': 'GET', 'method': 'GET',

View File

@ -28,8 +28,6 @@ class OperationLogTest(base.TestCase):
super(OperationLogTest, self).setUp() super(OperationLogTest, self).setUp()
self.controller = operation_logs.OperationLogsController() self.controller = operation_logs.OperationLogsController()
self.ctxt = context.RequestContext('demo', 'fakeproject', True) self.ctxt = context.RequestContext('demo', 'fakeproject', True)
self.mock_policy_check = self.mock_object(
context.RequestContext, 'can')
@mock.patch( @mock.patch(
'karbor.api.v1.operation_logs.' 'karbor.api.v1.operation_logs.'

View File

@ -37,9 +37,8 @@ class PlanApiTest(base.TestCase):
def setUp(self): def setUp(self):
super(PlanApiTest, self).setUp() super(PlanApiTest, self).setUp()
self.controller = plans.PlansController() self.controller = plans.PlansController()
self.ctxt = context.RequestContext('demo', 'fakeproject', True) self.ctxt = context.RequestContext('demo',
self.mock_policy_check = self.mock_object( DEFAULT_PROJECT_ID, True)
context.RequestContext, 'can')
@mock.patch( @mock.patch(
'karbor.services.protection.rpcapi.ProtectionAPI.show_provider') 'karbor.services.protection.rpcapi.ProtectionAPI.show_provider')
@ -52,7 +51,6 @@ class PlanApiTest(base.TestCase):
mock_provider.return_value = fakes.PROVIDER_OS mock_provider.return_value = fakes.PROVIDER_OS
self.controller.create(req, body) self.controller.create(req, body)
self.assertTrue(mock_plan_create.called) self.assertTrue(mock_plan_create.called)
self.assertTrue(self.mock_policy_check.called)
def test_plan_create_InvalidBody(self): def test_plan_create_InvalidBody(self):
plan = self._plan_in_request_body() plan = self._plan_in_request_body()

View File

@ -28,8 +28,6 @@ class ProtectablesApiTest(base.TestCase):
super(ProtectablesApiTest, self).setUp() super(ProtectablesApiTest, self).setUp()
self.controller = protectables.ProtectablesController() self.controller = protectables.ProtectablesController()
self.ctxt = context.RequestContext('demo', 'fakeproject', True) self.ctxt = context.RequestContext('demo', 'fakeproject', True)
self.mock_policy_check = self.mock_object(
context.RequestContext, 'can')
@mock.patch( @mock.patch(
'karbor.api.v1.protectables.ProtectablesController._get_all') 'karbor.api.v1.protectables.ProtectablesController._get_all')

View File

@ -29,8 +29,6 @@ class ProvidersApiTest(base.TestCase):
super(ProvidersApiTest, self).setUp() super(ProvidersApiTest, self).setUp()
self.controller = providers.ProvidersController() self.controller = providers.ProvidersController()
self.ctxt = context.RequestContext('demo', 'fakeproject', True) self.ctxt = context.RequestContext('demo', 'fakeproject', True)
self.mock_policy_check = self.mock_object(
context.RequestContext, 'can')
@mock.patch( @mock.patch(
'karbor.api.v1.providers.ProvidersController._get_all') 'karbor.api.v1.providers.ProvidersController._get_all')

View File

@ -42,8 +42,6 @@ class RestoreApiTest(base.TestCase):
super(RestoreApiTest, self).setUp() super(RestoreApiTest, self).setUp()
self.controller = restores.RestoresController() self.controller = restores.RestoresController()
self.ctxt = context.RequestContext('demo', 'fakeproject', True) self.ctxt = context.RequestContext('demo', 'fakeproject', True)
self.mock_policy_check = self.mock_object(
context.RequestContext, 'can')
@mock.patch( @mock.patch(
'karbor.services.protection.api.API.restore') 'karbor.services.protection.api.API.restore')

View File

@ -63,8 +63,6 @@ class ScheduledOperationApiTest(base.TestCase):
"provider_id": self._plan['provider_id'] "provider_id": self._plan['provider_id']
}, },
} }
self.mock_policy_check = self.mock_object(
context.RequestContext, 'can')
def test_create_operation_InvalidBody(self): def test_create_operation_InvalidBody(self):
self.assertRaises(exc.HTTPUnprocessableEntity, self.assertRaises(exc.HTTPUnprocessableEntity,

View File

@ -54,8 +54,6 @@ class TriggerApiTest(base.TestCase):
"pattern": "* * * * *" "pattern": "* * * * *"
}, },
} }
self.mock_policy_check = self.mock_object(
context.RequestContext, 'can')
def test_create_trigger_InvalidBody(self): def test_create_trigger_InvalidBody(self):
self.assertRaises(exc.HTTPUnprocessableEntity, self.assertRaises(exc.HTTPUnprocessableEntity,