Browse Source

Fix broken glance-cache-manage utility

glance-cahce-manage is broken due to circilar imports.
Fixed the same by importing 'prefetcher' module right before
'Prefetcher' object is initialized.

Closes-Bug: #1888349
Change-Id: I57d473572ca0a341082bacc3883cd9f763d77fa7
(cherry picked from commit ffd2f5e63f)
changes/42/742742/1
Abhishek Kekane 3 weeks ago
parent
commit
1bb6a32bad
5 changed files with 97 additions and 19 deletions
  1. +3
    -1
      glance/common/wsgi.py
  2. +0
    -6
      glance/tests/functional/test_client_exceptions.py
  3. +0
    -6
      glance/tests/functional/test_client_redirects.py
  4. +0
    -6
      glance/tests/functional/test_wsgi.py
  5. +94
    -0
      glance/tests/unit/test_cache_manage.py

+ 3
- 1
glance/common/wsgi.py View File

@@ -57,7 +57,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 = [
@@ -449,6 +448,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):


+ 0
- 6
glance/tests/functional/test_client_exceptions.py View File

@@ -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)


+ 0
- 6
glance/tests/functional/test_client_redirects.py View File

@@ -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')


+ 0
- 6
glance/tests/functional/test_wsgi.py View File

@@ -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!!!'



+ 94
- 0
glance/tests/unit/test_cache_manage.py View File

@@ -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)

Loading…
Cancel
Save