"Fossies" - the Fresh Open Source Software Archive

Member "glance-24.1.0/glance/tests/unit/common/test_config.py" (8 Jun 2022, 4355 Bytes) of package /linux/misc/openstack/glance-24.1.0.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 2011 OpenStack Foundation
    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 
   16 import os.path
   17 import shutil
   18 
   19 import fixtures
   20 import oslo_middleware
   21 
   22 from glance.api.middleware import context
   23 from glance.common import config
   24 from glance.tests import utils as test_utils
   25 
   26 
   27 class TestPasteApp(test_utils.BaseTestCase):
   28 
   29     def setUp(self):
   30         super(TestPasteApp, self).setUp()
   31 
   32     def _do_test_load_paste_app(self,
   33                                 expected_app_type,
   34                                 make_paste_file=True,
   35                                 paste_flavor=None,
   36                                 paste_config_file=None,
   37                                 paste_append=None):
   38 
   39         def _writeto(path, str):
   40             with open(path, 'w') as f:
   41                 f.write(str or '')
   42                 f.flush()
   43 
   44         def _appendto(orig, copy, str):
   45             shutil.copy(orig, copy)
   46             with open(copy, 'a') as f:
   47                 f.write(str or '')
   48                 f.flush()
   49 
   50         self.config(flavor=paste_flavor,
   51                     config_file=paste_config_file,
   52                     group='paste_deploy')
   53 
   54         temp_dir = self.useFixture(fixtures.TempDir()).path
   55         temp_file = os.path.join(temp_dir, 'testcfg.conf')
   56 
   57         _writeto(temp_file, '[DEFAULT]\n')
   58 
   59         config.parse_args(['--config-file', temp_file])
   60 
   61         paste_to = temp_file.replace('.conf', '-paste.ini')
   62         if not paste_config_file and make_paste_file:
   63             paste_from = os.path.join(os.getcwd(),
   64                                       'etc/glance-api-paste.ini')
   65             _appendto(paste_from, paste_to, paste_append)
   66 
   67         app = config.load_paste_app('glance-api')
   68 
   69         self.assertIsInstance(app, expected_app_type)
   70 
   71     def test_load_paste_app(self):
   72         expected_middleware = oslo_middleware.CORS
   73         self._do_test_load_paste_app(expected_middleware)
   74 
   75     def test_load_paste_app_paste_config_not_found(self):
   76         expected_middleware = context.UnauthenticatedContextMiddleware
   77         self.assertRaises(RuntimeError, self._do_test_load_paste_app,
   78                           expected_middleware, make_paste_file=False)
   79 
   80     def test_load_paste_app_with_paste_flavor(self):
   81         pipeline = ('[pipeline:glance-api-incomplete]\n'
   82                     'pipeline = context rootapp')
   83         expected_middleware = context.ContextMiddleware
   84         self._do_test_load_paste_app(expected_middleware,
   85                                      paste_flavor='incomplete',
   86                                      paste_append=pipeline)
   87 
   88     def test_load_paste_app_with_paste_config_file(self):
   89         paste_config_file = os.path.join(os.getcwd(),
   90                                          'etc/glance-api-paste.ini')
   91         expected_middleware = oslo_middleware.CORS
   92         self._do_test_load_paste_app(expected_middleware,
   93                                      paste_config_file=paste_config_file)
   94 
   95     def test_load_paste_app_with_paste_config_file_but_not_exist(self):
   96         paste_config_file = os.path.abspath("glance-api-paste.ini")
   97         expected_middleware = oslo_middleware.Healthcheck
   98         self.assertRaises(RuntimeError, self._do_test_load_paste_app,
   99                           expected_middleware,
  100                           paste_config_file=paste_config_file)
  101 
  102     def test_get_path_non_exist(self):
  103         self.assertRaises(RuntimeError, config._get_deployment_config_file)
  104 
  105 
  106 class TestDefaultConfig(test_utils.BaseTestCase):
  107 
  108     def setUp(self):
  109         super(TestDefaultConfig, self).setUp()
  110         self.CONF = config.cfg.CONF
  111         self.CONF.import_group('profiler', 'glance.common.wsgi')
  112 
  113     def test_osprofiler_disabled(self):
  114         self.assertFalse(self.CONF.profiler.enabled)
  115         self.assertFalse(self.CONF.profiler.trace_sqlalchemy)