'Test that we can queue and fetch images using the
CLI utility'

def test_queue(self):

    self.cleanup()
    self.start_servers(**self.__dict__.copy())
    api_port = self.api_port
    exe_cmd = ('%s    -m    glance.cmd.cache_manage' % sys.executable)
    cmd = ('%s    --port=%d    list-cached' % (exe_cmd, api_port))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    self.assertIn('No    cached    images', out.strip())
    cmd = ('%s    --port=%d    list-queued' % (exe_cmd, api_port))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    self.assertIn('No    queued    images', out.strip())
    ids = {}
    for x in range(4):
        ids[x] = self.add_image(('Image%s' % x))
    cmd = ('%s    --port=%d    --force    queue-image    %s' % (exe_cmd, api_port, ids[1]))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    cmd = ('%s    --port=%d    list-queued' % (exe_cmd, api_port))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    out = out.decode('utf-8')
    self.assertIn(ids[1], out, ('Image    %s    was    not    queued!' % ids[1]))
    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, 'lock_path': self.test_dir, 'log_file': os.path.join(self.test_dir, 'cache.log'), '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))
    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)
    cmd = ('%s    --port=%d    list-queued' % (exe_cmd, api_port))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    self.assertIn('No    queued    images', out.strip())
    cmd = ('%s    --port=%d    list-cached' % (exe_cmd, api_port))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    out = out.decode('utf-8')
    self.assertIn(ids[1], out, ('Image    %s    was    not    cached!' % ids[1]))
    cmd = ('%s    --port=%d    --force    queue-image    %s' % (exe_cmd, api_port, ids[2]))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    cmd = ('%s    --port=%d    list-queued' % (exe_cmd, api_port))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    out = out.decode('utf-8')
    self.assertIn(ids[2], out, ('Image    %s    was    not    queued!' % ids[2]))
    cmd = ('%s    --port=%d    --force    delete-queued-image    %s' % (exe_cmd, api_port, ids[2]))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    cmd = ('%s    --port=%d    list-queued' % (exe_cmd, api_port))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    self.assertIn('No    queued    images', out.strip())
    for x in range(4):
        cmd = ('%s    --port=%d    --force    queue-image    %s' % (exe_cmd, api_port, ids[x]))
        (exitcode, out, err) = execute(cmd)
        self.assertEqual(0, exitcode)
    cmd = ('%s    --port=%d    list-queued' % (exe_cmd, api_port))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    self.assertIn('Found    3    queued    images', out)
    cmd = ('%s    --port=%d    --force    delete-all-queued-images' % (exe_cmd, api_port))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    cmd = ('%s    --port=%d    list-queued' % (exe_cmd, api_port))
    (exitcode, out, err) = execute(cmd)
    self.assertEqual(0, exitcode)
    self.assertIn('No    queued    images', out.strip())
    cmd = ('%s    --port=%d    --force    queue-image    %s    %s' % (exe_cmd, api_port, ids[0], ids[1]))
    (exitcode, out, err) = execute(cmd, raise_error=False)
    self.assertEqual(1, exitcode)
    self.assertIn('Please    specify    one    and    only    ID    of    the    image    you    wish    to    ', out.strip())
    cmd = ('%s    --port=%d    --force    delete-queued-image    %s    %s' % (exe_cmd, api_port, ids[0], ids[1]))
    (exitcode, out, err) = execute(cmd, raise_error=False)
    self.assertEqual(1, exitcode)
    self.assertIn('Please    specify    one    and    only    ID    of    the    image    you    wish    to    ', out.strip())
    cmd = ('%s    --port=%d    --force    delete-cached-image    %s    %s' % (exe_cmd, api_port, ids[0], ids[1]))
    (exitcode, out, err) = execute(cmd, raise_error=False)
    self.assertEqual(1, exitcode)
    self.assertIn('Please    specify    one    and    only    ID    of    the    image    you    wish    to    ', out.strip())
    self.stop_servers()
