'Tests that cached images may be deleted'

@skip_if_disabled
def test_cache_manage_delete_cached_images(self):

    self.cleanup()
    self.start_servers(**self.__dict__.copy())
    self.verify_no_images()
    ids = {}
    for x in range(4):
        ids[x] = self.add_image(('Image%s' % str(x)))
    self.verify_no_cached_images()
    for x in range(4):
        path = ('http://%s:%d/v1/images/%s' % ('127.0.0.1', self.api_port, ids[x]))
        http = httplib2.Http()
        (response, content) = http.request(path, 'GET')
        self.assertEqual(http_client.OK, response.status, ('Failed    to    find    image    %s' % ids[x]))
    path = ('http://%s:%d/v1/cached_images' % ('127.0.0.1', self.api_port))
    http = httplib2.Http()
    (response, content) = http.request(path, 'GET')
    self.assertEqual(http_client.OK, response.status)
    data = jsonutils.loads(content)
    self.assertIn('cached_images', data)
    cached_images = data['cached_images']
    self.assertEqual(4, len(cached_images))
    for x in range(4, 0):
        self.assertEqual(ids[x], cached_images[x]['image_id'])
        self.assertEqual(0, cached_images[x]['hits'])
    path = ('http://%s:%d/v1/cached_images/%s' % ('127.0.0.1', self.api_port, ids[2]))
    http = httplib2.Http()
    (response, content) = http.request(path, 'DELETE')
    self.assertEqual(http_client.OK, response.status)
    path = ('http://%s:%d/v1/cached_images' % ('127.0.0.1', self.api_port))
    http = httplib2.Http()
    (response, content) = http.request(path, 'GET')
    self.assertEqual(http_client.OK, response.status)
    data = jsonutils.loads(content)
    self.assertIn('cached_images', data)
    cached_images = data['cached_images']
    self.assertEqual(3, len(cached_images))
    self.assertNotIn(ids[2], [x['image_id'] for x in cached_images])
    path = ('http://%s:%d/v1/cached_images' % ('127.0.0.1', self.api_port))
    http = httplib2.Http()
    (response, content) = http.request(path, 'DELETE')
    self.assertEqual(http_client.OK, response.status)
    path = ('http://%s:%d/v1/cached_images' % ('127.0.0.1', self.api_port))
    http = httplib2.Http()
    (response, content) = http.request(path, 'GET')
    self.assertEqual(http_client.OK, response.status)
    data = jsonutils.loads(content)
    self.assertIn('cached_images', data)
    cached_images = data['cached_images']
    self.assertEqual(0, len(cached_images))
    self.stop_servers()
