"Fossies" - the Fresh Open Source Software Archive

Member "masakari-9.0.0/masakari/tests/unit/test_service.py" (13 May 2020, 6569 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_service.py": 8.0.0_vs_9.0.0.

    1 # Copyright 2016 NTT DATA
    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 """
   17 Unit Tests for remote procedure calls using queue
   18 """
   19 
   20 from unittest import mock
   21 
   22 from oslo_concurrency import processutils
   23 from oslo_config import cfg
   24 from oslo_service import service as _service
   25 
   26 from masakari import exception
   27 from masakari import manager
   28 from masakari import rpc
   29 from masakari import service
   30 from masakari import test
   31 
   32 CONF = cfg.CONF
   33 
   34 
   35 class FakeManager(manager.Manager):
   36     """Fake manager for tests."""
   37     def test_method(self):
   38         return 'manager'
   39 
   40 
   41 class ServiceManagerTestCase(test.NoDBTestCase):
   42     """Test cases for Services."""
   43 
   44     @mock.patch.object(rpc, 'init')
   45     def test_message_gets_to_manager(self, mock_rpc_init):
   46         serv = service.Service('test',
   47                                'test',
   48                                'test',
   49                                'masakari.tests.unit.test_service.FakeManager')
   50         self.assertEqual('manager', serv.test_method())
   51 
   52 
   53 class ServiceTestCase(test.NoDBTestCase):
   54     """Test cases for Services."""
   55 
   56     def setUp(self):
   57         super(ServiceTestCase, self).setUp()
   58         self.host = 'foo'
   59         self.binary = 'masakari-engine'
   60         self.topic = 'fake'
   61 
   62     @mock.patch.object(rpc, 'init')
   63     def test_create(self, mock_rpc_init):
   64 
   65         app = service.Service.create(host=self.host, binary=self.binary,
   66                 topic=self.topic)
   67 
   68         self.assertTrue(app)
   69 
   70     @mock.patch.object(rpc, 'init')
   71     def test_repr(self, mock_rpc_init):
   72         # Test if a Service object is correctly represented, for example in
   73         # log files.
   74         serv = service.Service(self.host,
   75                                self.binary,
   76                                self.topic,
   77                                'masakari.tests.unit.test_service.FakeManager')
   78         exp = "<Service: host=foo, binary=masakari-engine, " \
   79               "manager_class_name=masakari.tests.unit." \
   80               "test_service.FakeManager>"
   81         self.assertEqual(exp, repr(serv))
   82 
   83     @mock.patch.object(_service.Service, 'stop')
   84     @mock.patch.object(rpc, 'init')
   85     @mock.patch.object(rpc, 'get_server')
   86     def test_parent_graceful_shutdown(self, mock_rpc, mock_rpc_init,
   87                                       mock_stop):
   88         serv = service.Service(self.host,
   89                                self.binary,
   90                                self.topic,
   91                                'masakari.tests.unit.test_service.FakeManager')
   92 
   93         serv.manager = mock.Mock()
   94         serv.manager.service_name = self.topic
   95 
   96         serv.start()
   97 
   98         serv.stop()
   99 
  100         serv.rpcserver.start.assert_called_once_with()
  101         serv.rpcserver.stop.assert_called_once_with()
  102         mock_stop.assert_called_once_with()
  103 
  104     @mock.patch.object(rpc, 'init')
  105     def test_reset(self, mock_rpc_init):
  106         serv = service.Service(self.host,
  107                                self.binary,
  108                                self.topic,
  109                                'masakari.tests.unit.test_service.FakeManager')
  110         with mock.patch.object(serv.manager, 'reset') as mock_reset:
  111             serv.reset()
  112             mock_reset.assert_called_once_with()
  113 
  114 
  115 class TestWSGIService(test.NoDBTestCase):
  116 
  117     def setUp(self):
  118         super(TestWSGIService, self).setUp()
  119         self.stub_out('masakari.wsgi.Loader.load_app', mock.MagicMock())
  120 
  121     def test_workers_set_default(self):
  122         test_service = service.WSGIService("masakari_api")
  123         self.assertEqual(test_service.workers, processutils.get_worker_count())
  124 
  125     def test_workers_set_good_user_setting(self):
  126         CONF.set_override('masakari_api_workers', 8)
  127         test_service = service.WSGIService("masakari_api")
  128         self.assertEqual(test_service.workers, 8)
  129 
  130     def test_workers_set_zero_user_setting(self):
  131         CONF.set_override('masakari_api_workers', 0)
  132         test_service = service.WSGIService("masakari_api")
  133         # If a value less than 1 is used, defaults to number of procs available
  134         self.assertEqual(test_service.workers, processutils.get_worker_count())
  135 
  136     def test_service_start_with_illegal_workers(self):
  137         CONF.set_override("masakari_api_workers", -1)
  138         self.assertRaises(exception.InvalidInput,
  139                           service.WSGIService, "masakari_api")
  140 
  141     def test_reset_pool_size_to_default(self):
  142         test_service = service.WSGIService("test_service")
  143         test_service.start()
  144 
  145         # Stopping the service, which in turn sets pool size to 0
  146         test_service.stop()
  147         self.assertEqual(test_service.server._pool.size, 0)
  148 
  149         # Resetting pool size to default
  150         test_service.reset()
  151         test_service.start()
  152         self.assertEqual(test_service.server._pool.size,
  153                          CONF.wsgi.default_pool_size)
  154 
  155 
  156 class TestLauncher(test.NoDBTestCase):
  157 
  158     @mock.patch.object(_service, 'launch')
  159     def test_launch_app(self, mock_launch):
  160         service._launcher = None
  161         service.serve(mock.sentinel.service)
  162         mock_launch.assert_called_once_with(mock.ANY,
  163                                             mock.sentinel.service,
  164                                             workers=None,
  165                                             restart_method='mutate')
  166 
  167     @mock.patch.object(_service, 'launch')
  168     def test_launch_app_with_workers(self, mock_launch):
  169         service._launcher = None
  170         service.serve(mock.sentinel.service, workers=mock.sentinel.workers)
  171         mock_launch.assert_called_once_with(mock.ANY,
  172                                             mock.sentinel.service,
  173                                             workers=mock.sentinel.workers,
  174                                             restart_method='mutate')
  175 
  176     @mock.patch.object(_service, 'launch')
  177     def test_launch_app_more_than_once_raises(self, mock_launch):
  178         service._launcher = None
  179         service.serve(mock.sentinel.service)
  180         self.assertRaises(RuntimeError, service.serve, mock.sentinel.service)