From 60254a4fd2bcb619ccf394e143583c6c226a21b9 Mon Sep 17 00:00:00 2001 From: SolKuczala Date: Wed, 31 Jul 2019 20:47:36 +0000 Subject: [PATCH] Fix _list_view function for count When doing `manila list --count True` without shares, the user gets an error. This happens because _list_view do not handle count properly for 0 shares. This patch-set adds a fix for this case. Change-Id: Ic6b45260ae39da9ec2c29d05c76d85be1e20635d Closes-bug: #1822815 --- manila/api/views/shares.py | 2 +- manila/tests/api/v2/test_shares.py | 39 +++++++++++++++++++ .../bug-667744-fix-c64071e6e5a098f7.yaml | 4 ++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/bug-667744-fix-c64071e6e5a098f7.yaml diff --git a/manila/api/views/shares.py b/manila/api/views/shares.py index 0bfa914770..eb63f54e2f 100644 --- a/manila/api/views/shares.py +++ b/manila/api/views/shares.py @@ -178,7 +178,7 @@ class ViewBuilder(common.ViewBuilder): self._collection_name) shares_dict = dict(shares=shares_list) - if count: + if count is not None: shares_dict['count'] = count if shares_links: shares_dict['shares_links'] = shares_links diff --git a/manila/tests/api/v2/test_shares.py b/manila/tests/api/v2/test_shares.py index 4d98b3971a..038242f5f2 100644 --- a/manila/tests/api/v2/test_shares.py +++ b/manila/tests/api/v2/test_shares.py @@ -1634,6 +1634,45 @@ class ShareAPITest(test.TestCase): api_version.APIVersionRequest('2.42')): self.assertEqual(3, result['count']) + @ddt.data({'use_admin_context': True, 'version': '2.42'}, + {'use_admin_context': False, 'version': '2.42'}) + @ddt.unpack + def test_share_list_summary_with_search_opt_count_0(self, + use_admin_context, + version): + search_opts = { + 'sort_key': 'fake_sort_key', + 'sort_dir': 'fake_sort_dir', + 'with_count': 'true' + } + if use_admin_context: + search_opts['host'] = 'fake_host' + # fake_key should be filtered + url = '/shares?fake_key=fake_value' + for k, v in search_opts.items(): + url = url + '&' + k + '=' + v + req = fakes.HTTPRequest.blank(url, version=version, + use_admin_context=use_admin_context) + + self.mock_object(share_api.API, 'get_all', + mock.Mock(return_value=[])) + + result = self.controller.index(req) + + search_opts_expected = {} + + if use_admin_context: + search_opts_expected.update({'fake_key': 'fake_value'}) + search_opts_expected['host'] = search_opts['host'] + share_api.API.get_all.assert_called_once_with( + req.environ['manila.context'], + sort_key=search_opts['sort_key'], + sort_dir=search_opts['sort_dir'], + search_opts=search_opts_expected, + ) + self.assertEqual(0, len(result['shares'])) + self.assertEqual(0, result['count']) + def test_share_list_summary(self): self.mock_object(share_api.API, 'get_all', stubs.stub_share_get_all_by_project) diff --git a/releasenotes/notes/bug-667744-fix-c64071e6e5a098f7.yaml b/releasenotes/notes/bug-667744-fix-c64071e6e5a098f7.yaml new file mode 100644 index 0000000000..ad4a0a0140 --- /dev/null +++ b/releasenotes/notes/bug-667744-fix-c64071e6e5a098f7.yaml @@ -0,0 +1,4 @@ +fixes: + - Launchpad bug `1822815 `_ + has been fixed. The user no longer gets an error if the list command has + no rows when executing `manila list --count True`.