Merge "Changed filter_by() to filter() during filtering instances in db API"

This commit is contained in:
Jenkins 2016-01-19 13:36:19 +00:00 committed by Gerrit Code Review
commit 806113e4f4
2 changed files with 29 additions and 2 deletions

View File

@ -2252,8 +2252,8 @@ def _exact_instance_filter(query, filters, legal_keys):
# Apply simple exact matches
if filter_dict:
query = query.filter_by(**filter_dict)
query = query.filter(*[getattr(models.Instance, k) == v
for k, v in filter_dict.items()])
return query

View File

@ -2589,6 +2589,33 @@ class InstanceTestCase(test.TestCase, ModelsObjectComparatorMixin):
ignored_keys=['deleted', 'deleted_at', 'metadata', 'extra',
'system_metadata', 'info_cache', 'pci_devices'])
def test_instance_get_all_by_filters_tags_and_project_id(self):
context1 = context.RequestContext('user1', 'p1')
context2 = context.RequestContext('user2', 'p2')
inst1 = self.create_instance_with_args(context=context1,
project_id='p1')
inst2 = self.create_instance_with_args(context=context1,
project_id='p1')
inst3 = self.create_instance_with_args(context=context2,
project_id='p2')
t1 = u'tag1'
t2 = u'tag2'
t3 = u'tag3'
t4 = u'tag4'
db.instance_tag_set(context1, inst1.uuid, [t1, t2])
db.instance_tag_set(context1, inst2.uuid, [t1, t2, t4])
db.instance_tag_set(context2, inst3.uuid, [t1, t2, t3, t4])
result = db.instance_get_all_by_filters(self.ctxt,
{'tags': [t1, t2],
'tags-any': [t3, t4],
'project_id': 'p1'})
self._assertEqualListsOfObjects([inst2], result,
ignored_keys=['deleted', 'deleted_at', 'metadata', 'extra',
'system_metadata', 'info_cache', 'pci_devices'])
def test_instance_get_all_by_host_and_node_no_join(self):
instance = self.create_instance_with_args()
result = db.instance_get_all_by_host_and_node(self.ctxt, 'h1', 'n1')