"Fossies" - the Fresh Open Source Software Archive

Member "masakari-9.0.0/masakari/tests/unit/api/openstack/ha/test_extension_info.py" (13 May 2020, 4027 Bytes) of package /linux/misc/openstack/masakari-9.0.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. See also the latest Fossies "Diffs" side-by-side code changes report for "test_extension_info.py": 8.0.0_vs_9.0.0.

    1 # Copyright 2016 NTT DATA
    2 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    3 #    not use this file except in compliance with the License. You may obtain
    4 #    a copy of the License at
    5 #
    6 #         http://www.apache.org/licenses/LICENSE-2.0
    7 #
    8 #    Unless required by applicable law or agreed to in writing, software
    9 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   10 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   11 #    License for the specific language governing permissions and limitations
   12 #    under the License.
   13 
   14 from unittest import mock
   15 
   16 from masakari.api.openstack.ha import extension_info
   17 from masakari import policy
   18 from masakari import test
   19 from masakari.tests.unit.api.openstack import fakes
   20 
   21 
   22 class fake_extension(object):
   23     def __init__(self, name, alias, description, version):
   24         self.name = name
   25         self.alias = alias
   26         self.__doc__ = description
   27         self.version = version
   28 
   29 
   30 fake_extensions = {
   31     'ext1-alias': fake_extension('ext1', 'ext1-alias', 'ext1 description', 1),
   32     'ext2-alias': fake_extension('ext2', 'ext2-alias', 'ext2 description', 2),
   33     'ext3-alias': fake_extension('ext3', 'ext3-alias', 'ext3 description', 1)
   34 }
   35 
   36 simulated_extension_list = {
   37     'segments': fake_extension('Segments', 'segments', 'Segments.', 1),
   38     'hosts': fake_extension('Hosts', 'hosts', 'Hosts.', 2),
   39     'os-fake': fake_extension('Cells', 'os-fake', 'Cells description', 1)
   40 }
   41 
   42 
   43 def fake_policy_authorize_selective(context, action, target):
   44     return action != 'os_masakari_api:ext1-alias:discoverable'
   45 
   46 
   47 class ExtensionInfoTest(test.NoDBTestCase):
   48 
   49     def setUp(self):
   50         super(ExtensionInfoTest, self).setUp()
   51         ext_info = extension_info.LoadedExtensionInfo()
   52         ext_info.extensions = fake_extensions
   53         self.controller = extension_info.ExtensionInfoController(ext_info)
   54 
   55     def _filter_extensions(self, res_dict):
   56 
   57         for e in [x for x in res_dict['extensions'] if '-alias' in x['alias']]:
   58             self.assertIn(e['alias'], fake_extensions)
   59             self.assertEqual(e['name'], fake_extensions[e['alias']].name)
   60             self.assertEqual(e['alias'], fake_extensions[e['alias']].alias)
   61             self.assertEqual(e['description'],
   62                              fake_extensions[e['alias']].__doc__)
   63             self.assertEqual(e['updated'], "")
   64             self.assertEqual(e['links'], [])
   65             self.assertEqual(6, len(e))
   66 
   67     @mock.patch.object(policy, 'authorize', mock.Mock(return_value=True))
   68     def test_extension_info_list(self):
   69         req = fakes.HTTPRequest.blank('/extensions')
   70         res_dict = self.controller.index(req)
   71         self.assertGreaterEqual(len(res_dict['extensions']), 3)
   72         self._filter_extensions(res_dict)
   73 
   74     @mock.patch.object(policy, 'authorize', mock.Mock(return_value=True))
   75     def test_extension_info_show(self):
   76         req = fakes.HTTPRequest.blank('/extensions/ext1-alias')
   77         res_dict = self.controller.show(req, 'ext1-alias')
   78         self.assertEqual(1, len(res_dict))
   79         self.assertEqual(res_dict['extension']['name'],
   80                          fake_extensions['ext1-alias'].name)
   81         self.assertEqual(res_dict['extension']['alias'],
   82                          fake_extensions['ext1-alias'].alias)
   83         self.assertEqual(res_dict['extension']['description'],
   84                          fake_extensions['ext1-alias'].__doc__)
   85         self.assertEqual(res_dict['extension']['updated'], "")
   86         self.assertEqual(res_dict['extension']['links'], [])
   87         self.assertEqual(6, len(res_dict['extension']))
   88 
   89     @mock.patch.object(policy, 'authorize')
   90     def test_extension_info_list_not_all_discoverable(self, mock_authorize):
   91         mock_authorize.side_effect = fake_policy_authorize_selective
   92         req = fakes.HTTPRequest.blank('/extensions')
   93         res_dict = self.controller.index(req)
   94         self.assertGreaterEqual(len(res_dict['extensions']), 2)
   95         self._filter_extensions(res_dict)