diff --git a/glance/common/wsgi.py b/glance/common/wsgi.py index 60971ad986..448efd8bcf 100644 --- a/glance/common/wsgi.py +++ b/glance/common/wsgi.py @@ -56,7 +56,6 @@ from glance.common import store_utils from glance.common import utils from glance import i18n from glance.i18n import _, _LE, _LI, _LW -from glance.image_cache import prefetcher bind_opts = [ @@ -448,6 +447,9 @@ class BaseServer(object): self.initialize_glance_store = initialize_glance_store self.initialize_prefetcher = initialize_prefetcher if self.initialize_prefetcher: + # NOTE(abhishekk): Importing the prefetcher just in time to avoid + # import loop during initialization + from glance.image_cache import prefetcher # noqa self.prefetcher = prefetcher.Prefetcher() def cache_images(self): diff --git a/glance/tests/functional/test_client_exceptions.py b/glance/tests/functional/test_client_exceptions.py index c2271a3d00..f8f5f15460 100644 --- a/glance/tests/functional/test_client_exceptions.py +++ b/glance/tests/functional/test_client_exceptions.py @@ -15,8 +15,6 @@ # under the License. """Functional test asserting strongly typed exceptions from glance client""" -import os - import eventlet.patcher import httplib2 from six.moves import http_client @@ -71,10 +69,6 @@ class TestClientExceptions(functional.FunctionalTest): def setUp(self): super(TestClientExceptions, self).setUp() self.port = utils.get_unused_port() - self.image_cache_dir = os.path.join(self.test_dir, - 'cache') - self.config(image_cache_dir=self.image_cache_dir) - self.config(image_cache_driver='sqlite') server = wsgi.Server() self.config(bind_host='127.0.0.1') self.config(workers=0) diff --git a/glance/tests/functional/test_client_redirects.py b/glance/tests/functional/test_client_redirects.py index 86fa68364f..2a71f4c983 100644 --- a/glance/tests/functional/test_client_redirects.py +++ b/glance/tests/functional/test_client_redirects.py @@ -14,8 +14,6 @@ # under the License. """Functional test cases testing glance client redirect-following.""" -import os - import eventlet.patcher from six.moves import http_client as http import webob.dec @@ -86,10 +84,6 @@ class TestClientRedirects(functional.FunctionalTest): super(TestClientRedirects, self).setUp() self.port_one = utils.get_unused_port() self.port_two = utils.get_unused_port() - self.image_cache_dir = os.path.join(self.test_dir, - 'cache') - self.config(image_cache_dir=self.image_cache_dir) - self.config(image_cache_driver='sqlite') server_one = wsgi.Server() server_two = wsgi.Server() self.config(bind_host='127.0.0.1') diff --git a/glance/tests/functional/test_wsgi.py b/glance/tests/functional/test_wsgi.py index d805a292c7..859b6130a8 100644 --- a/glance/tests/functional/test_wsgi.py +++ b/glance/tests/functional/test_wsgi.py @@ -19,8 +19,6 @@ import os import socket import time -import fixtures - from glance.common import wsgi from glance.tests import functional @@ -28,12 +26,8 @@ from glance.tests import functional class TestWSGIServer(functional.FunctionalTest): """WSGI server tests.""" def test_client_socket_timeout(self): - test_dir = self.useFixture(fixtures.TempDir()).path - image_cache_dir = os.path.join(test_dir, 'cache') self.config(workers=0) self.config(client_socket_timeout=1) - self.config(image_cache_dir=image_cache_dir) - self.config(image_cache_driver="sqlite") """Verify connections are timed out as per 'client_socket_timeout'""" greetings = b'Hello, World!!!' diff --git a/glance/tests/unit/test_cache_manage.py b/glance/tests/unit/test_cache_manage.py new file mode 100644 index 0000000000..9bec7242dd --- /dev/null +++ b/glance/tests/unit/test_cache_manage.py @@ -0,0 +1,94 @@ +# Copyright 2020 Red Hat Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +import sys +from unittest import mock + +import fixtures + +from glance.cmd import cache_manage +from glance.image_cache import client as cache_client +from glance.tests import utils as test_utils + + +UUID1 = 'c80a1a6c-bd1f-41c5-90ee-81afedb1d58d' + + +class TestCacheManage(test_utils.BaseTestCase): + + def setUp(self): + super(TestCacheManage, self).setUp() + + def _main_test_helper(self, argv, result=cache_manage.SUCCESS): + self.useFixture(fixtures.MonkeyPatch('sys.argv', argv)) + with mock.patch.object(cache_client, 'get_client'): + with mock.patch.object(sys, 'exit') as mock_exit: + cache_manage.main() + mock_exit.assert_called_once_with(result) + + def test_list_cached_images(self): + self._main_test_helper(['glance.cmd.cache_manage', 'list-cached']) + + def test_list_queued_images(self): + self._main_test_helper(['glance.cmd.cache_manage', 'list-queued']) + + @mock.patch.object(cache_manage, 'user_confirm') + def test_queue_image(self, mock_user_confirm): + self._main_test_helper(['glance.cmd.cache_manage', + 'queue-image', UUID1]) + self.assertEqual(1, mock_user_confirm.call_count) + + @mock.patch.object(cache_manage, 'user_confirm') + def test_queue_image_invalid_image_id(self, mock_user_confirm): + self._main_test_helper(['glance.cmd.cache_manage', 'queue-image', + 'fake_id'], + result=cache_manage.FAILURE) + + @mock.patch.object(cache_manage, 'user_confirm') + def test_delete_queued_image(self, mock_user_confirm): + self._main_test_helper(['glance.cmd.cache_manage', + 'delete-queued-image', UUID1]) + self.assertEqual(1, mock_user_confirm.call_count) + + @mock.patch.object(cache_manage, 'user_confirm') + def test_delete_queued_image_invalid_image_id(self, mock_user_confirm): + self._main_test_helper(['glance.cmd.cache_manage', + 'delete-queued-image', + 'fake_id'], + result=cache_manage.FAILURE) + + @mock.patch.object(cache_manage, 'user_confirm') + def test_delete_cached_image(self, mock_user_confirm): + self._main_test_helper(['glance.cmd.cache_manage', + 'delete-cached-image', UUID1]) + self.assertEqual(1, mock_user_confirm.call_count) + + @mock.patch.object(cache_manage, 'user_confirm') + def test_delete_cached_image_invalid_image_id(self, mock_user_confirm): + self._main_test_helper(['glance.cmd.cache_manage', + 'delete-cached-image', + 'fake_id'], + result=cache_manage.FAILURE) + + @mock.patch.object(cache_manage, 'user_confirm') + def test_delete_all_queued_image(self, mock_user_confirm): + self._main_test_helper(['glance.cmd.cache_manage', + 'delete-all-queued-images']) + self.assertEqual(1, mock_user_confirm.call_count) + + @mock.patch.object(cache_manage, 'user_confirm') + def test_delete_all_cached_image(self, mock_user_confirm): + self._main_test_helper(['glance.cmd.cache_manage', + 'delete-all-cached-images']) + self.assertEqual(1, mock_user_confirm.call_count)