diff --git a/cinder/tests/unit/api/v2/test_volumes.py b/cinder/tests/unit/api/v2/test_volumes.py index 6dafffeab8b..2652869488b 100644 --- a/cinder/tests/unit/api/v2/test_volumes.py +++ b/cinder/tests/unit/api/v2/test_volumes.py @@ -1362,20 +1362,46 @@ class VolumeApiTest(test.TestCase): body = {'volume': 'string'} self._create_volume_bad_request(body=body) - @mock.patch('cinder.volume.api.API.get_all') - def test_get_volumes_filter_with_string(self, get_all): + def _test_get_volumes_by_name(self, get_all, display_name): req = mock.MagicMock() context = mock.Mock() req.environ = {'cinder.context': context} - req.params = {'display_name': 'Volume-573108026'} + req.params = {'display_name': display_name} self.controller._view_builder.detail_list = mock.Mock() self.controller._get_volumes(req, True) get_all.assert_called_once_with( context, None, CONF.osapi_max_limit, sort_keys=['created_at'], sort_dirs=['desc'], - filters={'display_name': 'Volume-573108026'}, + filters={'display_name': display_name}, viewable_admin_meta=True, offset=0) + @mock.patch('cinder.volume.api.API.get_all') + def test_get_volumes_filter_with_string(self, get_all): + """Test to get a volume with an alpha-numeric display name.""" + self._test_get_volumes_by_name(get_all, 'Volume-573108026') + + @mock.patch('cinder.volume.api.API.get_all') + def test_get_volumes_filter_with_double_quoted_string(self, get_all): + """Test to get a volume with a double-quoted display name.""" + self._test_get_volumes_by_name(get_all, '"Volume-573108026"') + + @mock.patch('cinder.volume.api.API.get_all') + def test_get_volumes_filter_with_single_quoted_string(self, get_all): + """Test to get a volume with a single-quoted display name.""" + self._test_get_volumes_by_name(get_all, "'Volume-573108026'") + + @mock.patch('cinder.volume.api.API.get_all') + def test_get_volumes_filter_with_quote_in_between_string(self, get_all): + """Test to get a volume with a quote in between the display name.""" + self._test_get_volumes_by_name(get_all, 'Volu"me-573108026') + + @mock.patch('cinder.volume.api.API.get_all') + def test_get_volumes_filter_with_mixed_quoted_string(self, get_all): + """Test to get a volume with a mix of single and double quotes. """ + # The display name starts with a single quote and ends with a + # double quote + self._test_get_volumes_by_name(get_all, '\'Volume-573108026"') + @mock.patch('cinder.volume.api.API.get_all') def test_get_volumes_filter_with_true(self, get_all): req = mock.MagicMock() diff --git a/cinder/volume/api.py b/cinder/volume/api.py index 3d3f9429cb2..e25dc28627a 100644 --- a/cinder/volume/api.py +++ b/cinder/volume/api.py @@ -1667,6 +1667,13 @@ class API(base.Base): filters[k] = True else: filters[k] = bool(v) + elif k == 'display_name': + # Use the raw value of display name as is for the filter + # without passing it through ast.literal_eval(). If the + # display name is a properly quoted string (e.g. '"foo"') + # then literal_eval() strips the quotes (i.e. 'foo'), so + # the filter becomes different from the user input. + continue else: filters[k] = ast.literal_eval(v) except (ValueError, SyntaxError): diff --git a/releasenotes/notes/volume-filtering-for-quoted-display-name-7f5e8ac888a73001.yaml b/releasenotes/notes/volume-filtering-for-quoted-display-name-7f5e8ac888a73001.yaml new file mode 100644 index 00000000000..8192b265c57 --- /dev/null +++ b/releasenotes/notes/volume-filtering-for-quoted-display-name-7f5e8ac888a73001.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - Filtering volumes by their display name now + correctly handles display names with single and + double quotes.