From 82dfce9f65d6b0308ca6709416d178e0acbf6a87 Mon Sep 17 00:00:00 2001 From: manchandavishal Date: Thu, 10 Sep 2020 06:51:17 +0000 Subject: [PATCH] Add Unit Test for cinder-user-facing messages This patch is a follow-up patch of 64db185c7942721d32308245b1527f2367dceb21 which adds unit tests for the change. Add message_level to cinder message test data. Add third cinder message to make tests more robust. Co-Authored-By: Akihiro Motoki Change-Id: I6be88fbacf998a2ba36fea5a776cd4215cdaba0f --- .../dashboards/project/snapshots/tests.py | 39 ++++++++++++++++++ .../dashboards/project/volumes/tests.py | 41 +++++++++++++++++++ .../test/test_data/cinder_data.py | 16 +++++++- 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/openstack_dashboard/dashboards/project/snapshots/tests.py b/openstack_dashboard/dashboards/project/snapshots/tests.py index d2efc9f13b..c2eec55de4 100644 --- a/openstack_dashboard/dashboards/project/snapshots/tests.py +++ b/openstack_dashboard/dashboards/project/snapshots/tests.py @@ -26,6 +26,7 @@ from django.utils.http import urlunquote from openstack_dashboard import api from openstack_dashboard.dashboards.project.snapshots \ import tables as snapshot_tables +from openstack_dashboard.dashboards.project.snapshots import tabs from openstack_dashboard.test import helpers as test from openstack_dashboard.usage import quotas @@ -283,6 +284,44 @@ class VolumeSnapshotsViewTests(test.TestCase): self.mock_volume_snapshot_get.assert_called_once_with( test.IsHttpRequest(), snapshot.id) + @test.create_mocks({api.cinder: ('volume_snapshot_get', + 'message_list', + 'volume_get')}) + def test_volume_snapshot_detail_view_with_messages_tab(self): + volume = self.cinder_volumes.first() + snapshot = self.cinder_volume_snapshots.first() + messages = [msg for msg in self.cinder_messages.list() + if msg.resource_type == 'VOLUME_SNAPSHOT'] + + self.mock_volume_get.return_value = volume + self.mock_volume_snapshot_get.return_value = snapshot + self.mock_message_list.return_value = messages + + url = reverse('horizon:project:snapshots:detail', + args=[snapshot.id]) + detail_view = tabs.SnapshotDetailTabs(self.request) + messages_tab_link = "?%s=%s" % ( + detail_view.param_name, + detail_view.get_tab("messages_tab").get_id()) + url += messages_tab_link + res = self.client.get(url) + + self.assertTemplateUsed(res, 'horizon/common/_detail.html') + self.assertContains(res, messages[0].user_message) + self.assertNoMessages() + + self.mock_volume_get.assert_has_calls([ + mock.call(test.IsHttpRequest(), volume.id), + mock.call(test.IsHttpRequest(), snapshot.volume_id), + ]) + self.assertEqual(2, self.mock_volume_get.call_count) + self.mock_volume_snapshot_get.assert_called_once_with( + test.IsHttpRequest(), snapshot.id) + search_opts = {'resource_type': 'volume_snapshot', + 'resource_uuid': snapshot.id} + self.mock_message_list.assert_called_once_with( + test.IsHttpRequest(), search_opts=search_opts) + @test.create_mocks({api.cinder: ('volume_get', 'volume_snapshot_get')}) def test_volume_snapshot_detail_with_volume_get_exception(self): diff --git a/openstack_dashboard/dashboards/project/volumes/tests.py b/openstack_dashboard/dashboards/project/volumes/tests.py index 65b0ba0aac..13e2f478db 100644 --- a/openstack_dashboard/dashboards/project/volumes/tests.py +++ b/openstack_dashboard/dashboards/project/volumes/tests.py @@ -26,6 +26,7 @@ from openstack_dashboard import api from openstack_dashboard.api import cinder from openstack_dashboard.dashboards.project.volumes \ import tables as volume_tables +from openstack_dashboard.dashboards.project.volumes import tabs from openstack_dashboard.test import helpers as test from openstack_dashboard.usage import quotas @@ -1555,6 +1556,46 @@ class VolumeViewTests(test.ResetImageAPIVersionMixin, test.TestCase): test.IsHttpRequest(), search_opts={'volume_id': volume.id}) self.mock_tenant_absolute_limits.assert_called_once() + @test.create_mocks({cinder: ['volume_get', + 'message_list', + 'volume_snapshot_list', + 'tenant_absolute_limits']}) + def test_detail_view_with_messages_tab(self): + volume = self.cinder_volumes.first() + messages = [msg for msg in self.cinder_messages.list() + if msg.resource_type == 'VOLUME'] + snapshots = self.cinder_volume_snapshots.list() + + self.mock_volume_get.return_value = volume + self.mock_message_list.return_value = messages + self.mock_volume_snapshot_list.return_value = snapshots + self.mock_tenant_absolute_limits.return_value = \ + self.cinder_limits['absolute'] + + url = reverse(DETAIL_URL, args=[volume.id]) + detail_view = tabs.VolumeDetailTabs(self.request) + messages_tab_link = "?%s=%s" % ( + detail_view.param_name, + detail_view.get_tab("messages_tab").get_id()) + url += messages_tab_link + res = self.client.get(url) + + self.assertTemplateUsed(res, 'horizon/common/_detail.html') + self.assertContains(res, messages[0].user_message) + self.assertContains(res, messages[1].user_message) + self.assertNoMessages() + + self.mock_volume_get.assert_called_once_with(test.IsHttpRequest(), + volume.id) + self.mock_volume_snapshot_list.assert_called_once_with( + test.IsHttpRequest(), search_opts={'volume_id': volume.id}) + self.mock_tenant_absolute_limits.assert_called_once_with( + test.IsHttpRequest()) + search_opts = {'resource_type': 'volume', + 'resource_uuid': volume.id} + self.mock_message_list.assert_called_once_with( + test.IsHttpRequest(), search_opts=search_opts) + @mock.patch.object(cinder, 'volume_get') def test_detail_view_with_exception(self, mock_get): volume = self.cinder_volumes.first() diff --git a/openstack_dashboard/test/test_data/cinder_data.py b/openstack_dashboard/test/test_data/cinder_data.py index 7f67a160f1..a08273b668 100644 --- a/openstack_dashboard/test/test_data/cinder_data.py +++ b/openstack_dashboard/test/test_data/cinder_data.py @@ -559,9 +559,9 @@ def data(TEST): 'id': '2d2bb0d7-af28-4566-9a65-6d987c19093c', 'resource_type': 'VOLUME', 'resource_uuid': '6d53d143-e10f-440a-a65f-16a6b6d068f7', + 'message_level': 'ERROR', 'user_message': 'schedule allocate volume:An unknown error occurred.' }) - messages_2 = messages.Message( messages.MessageManager(None), {'created_at': '2020-07-12T12:56:43.000000', @@ -570,6 +570,20 @@ def data(TEST): 'id': 'd360b4e2-bda5-4289-b673-714a90cde80b', 'resource_type': 'VOLUME_SNAPSHOT', 'resource_uuid': '761634b0-fa1c-4e59-b8ad-d720807cb355', + 'message_level': 'ERROR', 'user_message': 'create snapshot:Snapshot is busy.'}) + messages_3 = messages.Message( + messages.MessageManager(None), + {'created_at': '2020-09-24T11:03:31.000000', + 'event_id': 'VOLUME_VOLUME_001_003', + 'guaranteed_until': '2020-10-24T11:03:31.000000', + 'id': '1f481885-9e1b-4d67-bdb5-b9db461391d3', + 'resource_type': 'VOLUME', + 'resource_uuid': 'f227c02d-91d7-4c3f-90d8-d8f261d0331a', + 'message_level': 'ERROR', + 'user_message': ('schedule allocate volume:' + 'Could not find any available weighted backend.'), + }) TEST.cinder_messages.add(api.cinder.Message(messages_1)) TEST.cinder_messages.add(api.cinder.Message(messages_2)) + TEST.cinder_messages.add(api.cinder.Message(messages_3))