Skip to content

Commit 136fc7a

Browse files
committed
Add test coverage of volumes policies
Current tests do not have good test coverage of existing policies. Either tests for policies do not exist or if they exist then they do not cover the actual negative and positive testing. To adopt the keystone's scope_type and new defaults in deprecated API policies, we need to first write test coverage for the same to know the complete effect of policies changes. Partial implement blueprint policy-defaults-refresh-deprecated-apis Change-Id: Id456ec736e22361362afb33c4254b20bfd7671aa
1 parent 0a51759 commit 136fc7a

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

nova/tests/unit/policies/test_volumes.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,140 @@ def setUp(self):
312312
self.project_foo_context,
313313
self.other_project_member_context
314314
]
315+
316+
317+
class VolumesPolicyTest(base.BasePolicyTest):
318+
"""Test Volumes APIs policies with all possible context.
319+
320+
This class defines the set of context with different roles
321+
which are allowed and not allowed to pass the policy checks.
322+
With those set of context, it will call the API operation and
323+
verify the expected behaviour.
324+
"""
325+
326+
def setUp(self):
327+
super(VolumesPolicyTest, self).setUp()
328+
self.controller = volumes_v21.VolumeController()
329+
self.snapshot_ctlr = volumes_v21.SnapshotController()
330+
self.req = fakes.HTTPRequest.blank('')
331+
self.controller._translate_volume_summary_view = mock.MagicMock()
332+
# Check that everyone is able to perform crud operations
333+
# on volume and volume snapshots.
334+
# NOTE: Nova cannot verify the volume/snapshot owner during nova policy
335+
# enforcement so will be passing context's project_id as target to
336+
# policy and always pass. If requester is not admin or owner
337+
# of volume/snapshot then cinder will be returning the appropriate
338+
# error.
339+
self.everyone_authorized_contexts = [
340+
self.legacy_admin_context, self.system_admin_context,
341+
self.project_admin_context, self.project_member_context,
342+
self.project_reader_context, self.project_foo_context,
343+
self.other_project_reader_context,
344+
self.system_member_context, self.system_reader_context,
345+
self.system_foo_context,
346+
self.other_project_member_context
347+
]
348+
self.everyone_unauthorized_contexts = []
349+
350+
@mock.patch('nova.volume.cinder.API.get_all')
351+
def test_list_volumes_policy(self, mock_get):
352+
rule_name = "os_compute_api:os-volumes"
353+
self.common_policy_check(self.everyone_authorized_contexts,
354+
self.everyone_unauthorized_contexts,
355+
rule_name, self.controller.index,
356+
self.req)
357+
358+
@mock.patch('nova.volume.cinder.API.get_all')
359+
def test_list_detail_volumes_policy(self, mock_get):
360+
rule_name = "os_compute_api:os-volumes"
361+
self.common_policy_check(self.everyone_authorized_contexts,
362+
self.everyone_unauthorized_contexts,
363+
rule_name, self.controller.detail,
364+
self.req)
365+
366+
@mock.patch('nova.volume.cinder.API.get')
367+
def test_show_volume_policy(self, mock_get):
368+
rule_name = "os_compute_api:os-volumes"
369+
self.common_policy_check(self.everyone_authorized_contexts,
370+
self.everyone_unauthorized_contexts,
371+
rule_name, self.controller.show,
372+
self.req, uuids.fake_id)
373+
374+
@mock.patch('nova.api.openstack.compute.volumes.'
375+
'_translate_volume_detail_view')
376+
@mock.patch('nova.volume.cinder.API.create')
377+
def test_create_volumes_policy(self, mock_create, mock_view):
378+
rule_name = "os_compute_api:os-volumes"
379+
body = {"volume": {"size": 100,
380+
"display_name": "Volume Test Name",
381+
"display_description": "Volume Test Desc",
382+
"availability_zone": "zone1:host1"}}
383+
self.common_policy_check(self.everyone_authorized_contexts,
384+
self.everyone_unauthorized_contexts,
385+
rule_name, self.controller.create,
386+
self.req, body=body)
387+
388+
@mock.patch('nova.volume.cinder.API.delete')
389+
def test_delete_volume_policy(self, mock_delete):
390+
rule_name = "os_compute_api:os-volumes"
391+
self.common_policy_check(self.everyone_authorized_contexts,
392+
self.everyone_unauthorized_contexts,
393+
rule_name, self.controller.delete,
394+
self.req, uuids.fake_id)
395+
396+
@mock.patch('nova.volume.cinder.API.get_all_snapshots')
397+
def test_list_snapshots_policy(self, mock_get):
398+
rule_name = "os_compute_api:os-volumes"
399+
self.common_policy_check(self.everyone_authorized_contexts,
400+
self.everyone_unauthorized_contexts,
401+
rule_name, self.snapshot_ctlr.index,
402+
self.req)
403+
404+
@mock.patch('nova.volume.cinder.API.get_all_snapshots')
405+
def test_list_detail_snapshots_policy(self, mock_get):
406+
rule_name = "os_compute_api:os-volumes"
407+
self.common_policy_check(self.everyone_authorized_contexts,
408+
self.everyone_unauthorized_contexts,
409+
rule_name, self.snapshot_ctlr.detail,
410+
self.req)
411+
412+
@mock.patch('nova.volume.cinder.API.get_snapshot')
413+
def test_show_snapshot_policy(self, mock_get):
414+
rule_name = "os_compute_api:os-volumes"
415+
self.common_policy_check(self.everyone_authorized_contexts,
416+
self.everyone_unauthorized_contexts,
417+
rule_name, self.snapshot_ctlr.show,
418+
self.req, uuids.fake_id)
419+
420+
@mock.patch('nova.volume.cinder.API.create_snapshot')
421+
def test_create_snapshot_policy(self, mock_create):
422+
rule_name = "os_compute_api:os-volumes"
423+
body = {"snapshot": {"volume_id": uuids.fake_id}}
424+
self.common_policy_check(self.everyone_authorized_contexts,
425+
self.everyone_unauthorized_contexts,
426+
rule_name, self.snapshot_ctlr.create,
427+
self.req, body=body)
428+
429+
@mock.patch('nova.volume.cinder.API.delete_snapshot')
430+
def test_delete_snapshot_policy(self, mock_delete):
431+
rule_name = "os_compute_api:os-volumes"
432+
self.common_policy_check(self.everyone_authorized_contexts,
433+
self.everyone_unauthorized_contexts,
434+
rule_name, self.snapshot_ctlr.delete,
435+
self.req, uuids.fake_id)
436+
437+
438+
class VolumesScopeTypePolicyTest(VolumesPolicyTest):
439+
"""Test Volumes APIs policies with system scope enabled.
440+
441+
This class set the nova.conf [oslo_policy] enforce_scope to True
442+
so that we can switch on the scope checking on oslo policy side.
443+
It defines the set of context with scoped token
444+
which are allowed and not allowed to pass the policy checks.
445+
With those set of context, it will run the API operation and
446+
verify the expected behaviour.
447+
"""
448+
449+
def setUp(self):
450+
super(VolumesScopeTypePolicyTest, self).setUp()
451+
self.flags(enforce_scope=True, group="oslo_policy")

0 commit comments

Comments
 (0)