'Tests that images may be queued and prefetched'

@skip_if_disabled
def test_queue_and_prefetch(self):

    self.cleanup()
    self.start_servers(**self.__dict__.copy())
    cache_config_filepath = os.path.join(self.test_dir, 'etc', 'glance-cache.conf')
    cache_file_options = {'image_cache_dir': self.api_server.image_cache_dir, 'image_cache_driver': self.image_cache_driver, 'registry_port': self.registry_server.bind_port, 'log_file': os.path.join(self.test_dir, 'cache.log'), 'lock_path': self.test_dir, 'metadata_encryption_key': '012345678901234567890123456789ab', 'filesystem_store_datadir': self.test_dir}
    with open(cache_config_filepath, 'w') as cache_file:
        cache_file.write(('[DEFAULT]\ndebug    =    True\nlock_path    =    %(lock_path)s\nimage_cache_dir    =    %(image_cache_dir)s\nimage_cache_driver    =    %(image_cache_driver)s\nregistry_host    =    127.0.0.1\nregistry_port    =    %(registry_port)s\nmetadata_encryption_key    =    %(metadata_encryption_key)s\nlog_file    =    %(log_file)s\n\n[glance_store]\nfilesystem_store_datadir=%(filesystem_store_datadir)s\n' % cache_file_options))
    self.verify_no_images()
    ids = {}
    for x in range(4):
        ids[x] = self.add_image(('Image%s' % str(x)))
    path = ('http://%s:%d/v1/queued_images/%s' % ('127.0.0.1', self.api_port, ids[0]))
    http = httplib2.Http()
    (response, content) = http.request(path, 'PUT')
    self.assertEqual(http_client.OK, response.status)
    self.verify_no_cached_images()
    cmd = ('%s    -m    glance.cmd.cache_prefetcher    --config-file    %s' % (sys.executable, cache_config_filepath))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    self.assertEqual('', out.strip(), out)
    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(1, len(cached_images))
    self.assertIn(ids[0], [r['image_id'] for r in data['cached_images']])
    self.stop_servers()
