"Fossies" - the Fresh Open Source Software Archive

Member "glance-20.0.1/glance/tests/unit/test_cache_manage.py" (12 Aug 2020, 4007 Bytes) of package /linux/misc/openstack/glance-20.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright 2020 Red Hat Inc.
    2 # All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 import sys
   16 from unittest import mock
   17 
   18 import fixtures
   19 
   20 from glance.cmd import cache_manage
   21 from glance.image_cache import client as cache_client
   22 from glance.tests import utils as test_utils
   23 
   24 
   25 UUID1 = 'c80a1a6c-bd1f-41c5-90ee-81afedb1d58d'
   26 
   27 
   28 class TestCacheManage(test_utils.BaseTestCase):
   29 
   30     def setUp(self):
   31         super(TestCacheManage, self).setUp()
   32 
   33     def _main_test_helper(self, argv, result=cache_manage.SUCCESS):
   34         self.useFixture(fixtures.MonkeyPatch('sys.argv', argv))
   35         with mock.patch.object(cache_client, 'get_client'):
   36             with mock.patch.object(sys, 'exit') as mock_exit:
   37                 cache_manage.main()
   38             mock_exit.assert_called_once_with(result)
   39 
   40     def test_list_cached_images(self):
   41         self._main_test_helper(['glance.cmd.cache_manage', 'list-cached'])
   42 
   43     def test_list_queued_images(self):
   44         self._main_test_helper(['glance.cmd.cache_manage', 'list-queued'])
   45 
   46     @mock.patch.object(cache_manage, 'user_confirm')
   47     def test_queue_image(self, mock_user_confirm):
   48         self._main_test_helper(['glance.cmd.cache_manage',
   49                                 'queue-image', UUID1])
   50         self.assertEqual(1, mock_user_confirm.call_count)
   51 
   52     @mock.patch.object(cache_manage, 'user_confirm')
   53     def test_queue_image_invalid_image_id(self, mock_user_confirm):
   54         self._main_test_helper(['glance.cmd.cache_manage', 'queue-image',
   55                                 'fake_id'],
   56                                result=cache_manage.FAILURE)
   57 
   58     @mock.patch.object(cache_manage, 'user_confirm')
   59     def test_delete_queued_image(self, mock_user_confirm):
   60         self._main_test_helper(['glance.cmd.cache_manage',
   61                                 'delete-queued-image', UUID1])
   62         self.assertEqual(1, mock_user_confirm.call_count)
   63 
   64     @mock.patch.object(cache_manage, 'user_confirm')
   65     def test_delete_queued_image_invalid_image_id(self, mock_user_confirm):
   66         self._main_test_helper(['glance.cmd.cache_manage',
   67                                 'delete-queued-image',
   68                                 'fake_id'],
   69                                result=cache_manage.FAILURE)
   70 
   71     @mock.patch.object(cache_manage, 'user_confirm')
   72     def test_delete_cached_image(self, mock_user_confirm):
   73         self._main_test_helper(['glance.cmd.cache_manage',
   74                                 'delete-cached-image', UUID1])
   75         self.assertEqual(1, mock_user_confirm.call_count)
   76 
   77     @mock.patch.object(cache_manage, 'user_confirm')
   78     def test_delete_cached_image_invalid_image_id(self, mock_user_confirm):
   79         self._main_test_helper(['glance.cmd.cache_manage',
   80                                 'delete-cached-image',
   81                                 'fake_id'],
   82                                result=cache_manage.FAILURE)
   83 
   84     @mock.patch.object(cache_manage, 'user_confirm')
   85     def test_delete_all_queued_image(self, mock_user_confirm):
   86         self._main_test_helper(['glance.cmd.cache_manage',
   87                                 'delete-all-queued-images'])
   88         self.assertEqual(1, mock_user_confirm.call_count)
   89 
   90     @mock.patch.object(cache_manage, 'user_confirm')
   91     def test_delete_all_cached_image(self, mock_user_confirm):
   92         self._main_test_helper(['glance.cmd.cache_manage',
   93                                 'delete-all-cached-images'])
   94         self.assertEqual(1, mock_user_confirm.call_count)