From 064046a47f4a6340f1be00e7121c1c35ab985ff3 Mon Sep 17 00:00:00 2001 From: huwenhui Date: Sat, 29 Jan 2022 14:50:08 +0800 Subject: [PATCH] Fix the bug of TypeError with JsonFilter The default scheduler_hints value is None, and if we don't pass the scheduler_hints , It will report TypeError when creating share with JsonFilter with Rest API. The TypeError exception is added to solve this problem. Change-Id: Iad89491cbbaccac8df161f8f1157c7ebf3291458 Closes-Bug: #1959472 --- manila/scheduler/filters/json.py | 4 +++- manila/tests/scheduler/filters/test_json.py | 8 ++++++++ ...-1959472-fix-type-error-jsonfilter-fc7f87c288cc69.yaml | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/bug-1959472-fix-type-error-jsonfilter-fc7f87c288cc69.yaml diff --git a/manila/scheduler/filters/json.py b/manila/scheduler/filters/json.py index e654ae749d..1ff92f9c6d 100644 --- a/manila/scheduler/filters/json.py +++ b/manila/scheduler/filters/json.py @@ -137,7 +137,9 @@ class JsonFilter(base_host.BaseHostFilter): # and scheduler_hints. try: query = filter_properties['scheduler_hints']['query'] - except KeyError: + # If filter_properties['scheduler_hints'] is None, and TypeError + # will occur, add TypeError exception here. + except (KeyError, TypeError): query = None if not query: return True diff --git a/manila/tests/scheduler/filters/test_json.py b/manila/tests/scheduler/filters/test_json.py index 9b506825ed..ba90c523b5 100644 --- a/manila/tests/scheduler/filters/test_json.py +++ b/manila/tests/scheduler/filters/test_json.py @@ -264,6 +264,14 @@ class HostFiltersTestCase(test.TestCase): self.assertRaises(KeyError, self.filter.host_passes, host, filter_properties) + def test_json_filter_type_errror_passes(self): + filter_properties = { + 'scheduler_hints': None + } + host = fakes.FakeHostState('host1', + {'capabilities': {'enabled': True}}) + self.assertTrue(self.filter.host_passes, (host, filter_properties)) + def test_json_filter_empty_filters_pass(self): host = fakes.FakeHostState('host1', {'capabilities': {'enabled': True}}) diff --git a/releasenotes/notes/bug-1959472-fix-type-error-jsonfilter-fc7f87c288cc69.yaml b/releasenotes/notes/bug-1959472-fix-type-error-jsonfilter-fc7f87c288cc69.yaml new file mode 100644 index 0000000000..65a29a413a --- /dev/null +++ b/releasenotes/notes/bug-1959472-fix-type-error-jsonfilter-fc7f87c288cc69.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - Fix the bug of TypeError with JsonFilter. If the scheduler_hints + value is None, the TypeError exception may occur when creating share + with JsonFilter. The TypeError exception is added to solve this problem.