"Fossies" - the Fresh Open Source Software Archive

Member "magnum-8.1.0/magnum/tests/unit/conf/test_conf.py" (1 Oct 2019, 2828 Bytes) of package /linux/misc/openstack/magnum-8.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 2016 Fujitsu Ltd.
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # a copy of the License at
    6 #
    7 #      http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 import collections
   16 import mock
   17 from oslo_config import cfg
   18 import six
   19 
   20 from magnum.conf import opts
   21 from magnum.tests import base
   22 
   23 
   24 class ConfTestCase(base.TestCase):
   25 
   26     def test_list_opts(self):
   27         for group, opt_list in opts.list_opts():
   28             if isinstance(group, six.string_types):
   29                 self.assertEqual(group, 'DEFAULT')
   30             else:
   31                 self.assertIsInstance(group, cfg.OptGroup)
   32             for opt in opt_list:
   33                 self.assertIsInstance(opt, cfg.Opt)
   34 
   35     def test_list_module_name_invalid_mods(self):
   36         with mock.patch('pkgutil.iter_modules') as mock_mods:
   37             mock_mods.return_value = [(None, 'foo', True),
   38                                       (None, 'opts', False)]
   39             self.assertEqual([], opts._list_module_names())
   40 
   41     def test_list_module_name_valid_mods(self):
   42         with mock.patch('pkgutil.iter_modules') as mock_mods:
   43             mock_mods.return_value = [(None, 'foo', False)]
   44             self.assertEqual(['foo'], opts._list_module_names())
   45 
   46     def test_import_mods_no_func(self):
   47         modules = ['foo', 'bar']
   48         with mock.patch('importlib.import_module') as mock_import:
   49             mock_import.return_value = mock.sentinel.mods
   50             self.assertRaises(AttributeError, opts._import_modules, modules)
   51             mock_import.assert_called_once_with('magnum.conf.foo')
   52 
   53     def test_import_mods_valid_func(self):
   54         modules = ['foo', 'bar']
   55         with mock.patch('importlib.import_module') as mock_import:
   56             mock_mod = mock.MagicMock()
   57             mock_import.return_value = mock_mod
   58             self.assertEqual([mock_mod, mock_mod],
   59                              opts._import_modules(modules))
   60             mock_import.assert_has_calls([mock.call('magnum.conf.foo'),
   61                                           mock.call('magnum.conf.bar')])
   62 
   63     def test_append_config(self):
   64         opt = collections.defaultdict(list)
   65         mock_module = mock.MagicMock()
   66         mock_conf = mock.MagicMock()
   67         mock_module.list_opts.return_value = mock_conf
   68         mock_conf.items.return_value = [('foo', 'bar')]
   69         opts._append_config_options([mock_module], opt)
   70         self.assertEqual({'foo': ['b', 'a', 'r']}, opt)