@require_admin_context
def service_get_by_args(context, host, binary):
- result = model_query(context, models.Service).\
+ results = model_query(context, models.Service).\
filter_by(host=host).\
filter_by(binary=binary).\
- first()
+ all()
- if not result:
- raise exception.HostBinaryNotFound(host=host, binary=binary)
+ for result in results:
+ if host == result['host']:
+ return result
- return result
+ raise exception.HostBinaryNotFound(host=host, binary=binary)
@require_admin_context
import datetime
import enum
+import mock
from oslo_config import cfg
from oslo_utils import uuidutils
import six
db.service_get_by_args,
self.ctxt, 'non-exists-host', 'a')
+ @mock.patch('cinder.db.sqlalchemy.api.model_query')
+ def test_service_get_by_args_with_case_insensitive(self, model_query):
+ class case_insensitive_filter(object):
+ def __init__(self, records):
+ self.records = records
+
+ def filter_by(self, **kwargs):
+ ret = mock.Mock()
+ ret.all = mock.Mock()
+
+ results = []
+ for record in self.records:
+ for key, value in kwargs.items():
+ if record[key].lower() != value.lower():
+ break
+ else:
+ results.append(record)
+
+ ret.filter_by = case_insensitive_filter(results).filter_by
+ ret.all.return_value = results
+ return ret
+
+ values = [
+ {'host': 'host', 'binary': 'a'},
+ {'host': 'HOST', 'binary': 'a'}
+ ]
+ services = [self._create_service(vals) for vals in values]
+
+ query = mock.Mock()
+ query.filter_by = case_insensitive_filter(services).filter_by
+ model_query.return_value = query
+
+ service1 = db.service_get_by_args(self.ctxt, 'host', 'a')
+ self._assertEqualObjects(services[0], service1)
+
+ service2 = db.service_get_by_args(self.ctxt, 'HOST', 'a')
+ self._assertEqualObjects(services[1], service2)
+
+ self.assertRaises(exception.HostBinaryNotFound,
+ db.service_get_by_args,
+ self.ctxt, 'Host', 'a')
+
class DBAPIVolumeTestCase(BaseTest):