'Check that image filtering works on user-defined properties'

def test_get_index_filter_on_user_defined_properties(self):

    image1_id = _gen_uuid()
    properties = {'distro': 'ubuntu', 'arch': 'i386'}
    extra_fixture = {'id': image1_id, 'status': 'active', 'is_public': True, 'disk_format': 'vhd', 'container_format': 'ovf', 'name': 'image-extra-1', 'size': 18, 'properties': properties, 'checksum': None}
    db_api.image_create(self.context, extra_fixture)
    image2_id = _gen_uuid()
    properties = {'distro': 'ubuntu', 'arch': 'x86_64', 'foo': 'bar'}
    extra_fixture = {'id': image2_id, 'status': 'active', 'is_public': True, 'disk_format': 'ami', 'container_format': 'ami', 'name': 'image-extra-2', 'size': 20, 'properties': properties, 'checksum': None}
    db_api.image_create(self.context, extra_fixture)
    req = webob.Request.blank('/images?property-distro=ubuntu')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(2, len(images))
    self.assertEqual(image2_id, images[0]['id'])
    self.assertEqual(image1_id, images[1]['id'])
    req = webob.Request.blank('/images?property-distro=fedora')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(0, len(images))
    req = webob.Request.blank('/images?property-arch=i386')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(1, len(images))
    self.assertEqual(image1_id, images[0]['id'])
    req = webob.Request.blank('/images?property-arch=x86_64')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(1, len(images))
    self.assertEqual(image2_id, images[0]['id'])
    req = webob.Request.blank('/images?property-foo=bar')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(1, len(images))
    self.assertEqual(image2_id, images[0]['id'])
    req = webob.Request.blank('/images?property-foo=baz')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(0, len(images))
    req = webob.Request.blank('/images?property-arch=x86_64&property-distro=ubuntu')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(1, len(images))
    self.assertEqual(image2_id, images[0]['id'])
    req = webob.Request.blank('/images?property-arch=i386&property-distro=ubuntu')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(1, len(images))
    self.assertEqual(image1_id, images[0]['id'])
    req = webob.Request.blank('/images?property-arch=random&property-distro=ubuntu')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(0, len(images))
    req = webob.Request.blank('/images?property-arch=random&property-distro=random')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(0, len(images))
    req = webob.Request.blank('/images?property-boo=far&property-poo=far')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(0, len(images))
    req = webob.Request.blank('/images?property-foo=bar&property-poo=far')
    res = req.get_response(self.api)
    self.assertEqual(http_client.OK, res.status_int)
    images = jsonutils.loads(res.body)['images']
    self.assertEqual(0, len(images))
