Enhance the db layer to verify filters

Now if we init filters=None, as call
dbapi.device_profile_list_by_filters(self.context, filters=None),
that will raise an NoneType error.

Mainly error info:
Traceback (most recent call last):
  File "/home/my_work/code/cyborg/cyborg/db/sqlalchemy/api.py", line
558, in device_profile_list_by_filters
    filters, exact_match_filter_names)
  File "/home/my_work/code/cyborg/cyborg/db/sqlalchemy/api.py", line
223, in _exact_filter
    if key not in filters:
TypeError: argument of type 'NoneType' is not iterable

This patch will add initial validation of the filters.

Change-Id: Icf711dc3621fb8d2e5b022ab1d1ce02b0885b055
This commit is contained in:
zhangbailin 2020-01-17 17:51:01 +08:00 committed by chenker
parent 70bc4b89a4
commit acbc64f3be
2 changed files with 17 additions and 0 deletions
cyborg
db/sqlalchemy
tests/unit/db

@ -211,6 +211,8 @@ class Connection(api.Connection):
:param legal_keys: list of keys to apply exact filtering to
"""
if filters is None:
filters = {}
if legal_keys is None:
legal_keys = []
filter_dict = {}

@ -86,6 +86,21 @@ class TestDbDeviceProfile(base.DbTestCase):
self.dbapi.device_profile_get_by_uuid,
self.context, random_uuid)
def test_list_filter_is_none(self):
"""The main test is filters=None. If filters=None,
it will be initialized to {}, that will return all device
profiles same as the List Device Profiles API response.
"""
utils.create_test_device_profile(
self.context,
id=1,
uuid=uuidutils.generate_uuid(),
name="foo_dp")
res = self.dbapi.device_profile_list_by_filters(
self.context, filters=None)
self.assertEqual(1, len(res))
self.assertEqual('foo_dp', res[0]['name'])
def test_update_with_uuid_not_exist(self):
utils.create_test_device_profile(self.context)
random_uuid = uuidutils.generate_uuid()