"Fossies" - the Fresh Open Source Software Archive

Member "murano-8.0.0/murano/tests/unit/common/test_statservice.py" (16 Oct 2019, 8681 Bytes) of package /linux/misc/openstack/murano-8.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 last Fossies "Diffs" side-by-side code changes report for "test_statservice.py": 4.0.0_vs_5.0.0.

    1 # Copyright 2016 AT&T Corp
    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 datetime as dt
   17 import mock
   18 import time
   19 
   20 from oslo_utils import timeutils
   21 
   22 from murano.common import statservice
   23 from murano.db import models
   24 from murano.db import session as db_session
   25 from murano.services import states
   26 from murano.tests.unit import base
   27 
   28 
   29 @mock.patch('murano.common.statservice.v1.stats',
   30             request_count=11, error_count=22, average_time=33,
   31             requests_per_tenant=44)
   32 class StatsCollectingServiceTest(base.MuranoTestCase):
   33 
   34     def setUp(self):
   35         super(StatsCollectingServiceTest, self).setUp()
   36         self.service = statservice.StatsCollectingService()
   37         self.service._prev_time = 0
   38         self.mock_new_stats = mock.MagicMock(request_count=1, error_count=2)
   39         self.service._stats_db = mock.MagicMock(
   40             get_stats_by_host=mock.MagicMock(return_value=self.mock_new_stats))
   41         statservice.LOG = mock.MagicMock()
   42 
   43     def test_service_start_and_stop(self, _):
   44         self.assertEqual(0, len(self.service.tg.threads))
   45         self.service.start()
   46         self.assertEqual(2, len(self.service.tg.threads))
   47         self.service.stop()
   48         self.assertEqual(0, len(self.service.tg.threads))
   49 
   50     @mock.patch('murano.common.statservice.time')
   51     def test_update_stats(self, mock_time, mock_stats):
   52         now = time.time()
   53         mock_time.time.return_value = now
   54 
   55         self.service.update_stats()
   56 
   57         statservice.LOG.debug.assert_any_call(
   58             'Stats: (Requests: 11  Errors: 22  Ave.Res.Time 33.0000\n Per '
   59             'tenant: 44)')
   60         self.assertEqual(now, self.service._prev_time)
   61         self.assertEqual(mock_stats.request_count,
   62                          self.mock_new_stats.request_count)
   63         self.assertEqual(mock_stats.error_count,
   64                          self.mock_new_stats.error_count)
   65         self.assertEqual(mock_stats.average_time,
   66                          self.mock_new_stats.average_response_time)
   67         self.assertEqual(str(mock_stats.requests_per_tenant),
   68                          self.mock_new_stats.requests_per_tenant)
   69         self.assertEqual((11 - 1) / now,
   70                          self.mock_new_stats.requests_per_second)
   71         self.assertEqual((22 - 2) / now,
   72                          self.mock_new_stats.errors_per_second)
   73         self.service._stats_db.update.assert_called_once_with(
   74             self.service._hostname, self.mock_new_stats)
   75 
   76     @mock.patch('murano.common.statservice.multiprocessing.cpu_count',
   77                 return_value=5)
   78     @mock.patch('murano.common.statservice.psutil.cpu_percent',
   79                 return_value=12.3)
   80     def test_update_stats_with_create_stats_db(self, _, __, mock_stats):
   81         self.service._stats_db.get_stats_by_host.return_value = None
   82 
   83         result = self.service.update_stats()
   84 
   85         self.assertIsNone(result)
   86         self.service._stats_db.create.assert_called_once_with(
   87             self.service._hostname, mock_stats.request_count,
   88             mock_stats.error_count, mock_stats.average_time,
   89             mock_stats.requests_per_tenant, 5, 12.3
   90         )
   91 
   92     @mock.patch('murano.common.statservice.LOG')
   93     def test_update_stats_handle_exception(self, mock_log, _):
   94         self.service._stats_db.update.side_effect =\
   95             Exception('test_error_code')
   96 
   97         self.service.update_stats()
   98 
   99         mock_log.exception.assert_called_once_with(
  100             "Failed to get statistics object from a "
  101             "database. {error_code}".format(error_code='test_error_code'))
  102 
  103 
  104 class EnvReportingTest(base.MuranoNotifyWithDBTestCase):
  105 
  106     def setUp(self):
  107         super(EnvReportingTest, self).setUp()
  108         self.service = statservice.StatsCollectingService()
  109 
  110     @mock.patch('murano.common.statservice.status_reporter.'
  111                 'Notification.report')
  112     def test_report_env_stats(self, mock_notifier):
  113         now = timeutils.utcnow()
  114         later = now + dt.timedelta(minutes=1)
  115 
  116         session = db_session.get_session()
  117 
  118         environment1 = models.Environment(
  119             name='test_environment1', tenant_id='test_tenant_id1',
  120             version=2, id='test_env_id_1',
  121             created=now,
  122             updated=later,
  123             description={
  124                 'Objects': {
  125                     'applications': ['app1'],
  126                     'services': ['service1']
  127                 }
  128             }
  129         )
  130         environment2 = models.Environment(
  131             name='test_environment2', tenant_id='test_tenant_id2',
  132             version=1, id='test_env_id_2',
  133             created=now,
  134             updated=later,
  135             description={
  136                 'Objects': {
  137                     'applications': ['app2'],
  138                     'services': ['service3']
  139                 }
  140             }
  141         )
  142         environment3 = models.Environment(
  143             name='test_environment3', tenant_id='test_tenant_id2',
  144             version=1, id='test_env_id_3',
  145             created=now,
  146             updated=later,
  147             description={}
  148         )
  149 
  150         session_1 = models.Session(
  151             environment=environment1, user_id='test_user_id',
  152             description={},
  153             state=states.SessionState.DEPLOYED,
  154             version=1
  155         )
  156 
  157         session_2 = models.Session(
  158             environment=environment2, user_id='test_user_id',
  159             description={},
  160             state=states.SessionState.DEPLOYED,
  161             version=0
  162         )
  163 
  164         session_3 = models.Session(
  165             environment=environment3, user_id='test_user_id',
  166             description={},
  167             state=states.SessionState.DEPLOY_FAILURE,
  168             version=1
  169         )
  170 
  171         task_1 = models.Task(
  172             id='task_id_1',
  173             environment=environment1,
  174             description={},
  175             created=now,
  176             started=now,
  177             updated=later,
  178             finished=later
  179         )
  180 
  181         task_2 = models.Task(
  182             id='task_id_2',
  183             environment=environment2,
  184             description={},
  185             created=now,
  186             started=now,
  187             updated=later,
  188             finished=later
  189         )
  190 
  191         task_3 = models.Task(
  192             id='task_id_3',
  193             environment=environment3,
  194             description={},
  195             created=now,
  196             started=now,
  197             updated=later,
  198             finished=later
  199         )
  200 
  201         status_1 = models.Status(
  202             id='status_id_1',
  203             task_id='task_id_1',
  204             text='Deployed',
  205             level='info'
  206         )
  207 
  208         status_2 = models.Status(
  209             id='status_id_2',
  210             task_id='task_id_2',
  211             text='Deployed',
  212             level='info'
  213         )
  214 
  215         status_3 = models.Status(
  216             id='status_id_3',
  217             task_id='task_id_3',
  218             text='Something was wrong',
  219             level='error'
  220         )
  221 
  222         session.add_all([environment1, environment2, environment3])
  223         session.add_all([session_1, session_2, session_3])
  224         session.add_all([task_1, task_2, task_3])
  225         session.add_all([status_1, status_2, status_3])
  226 
  227         session.flush()
  228 
  229         self.service.report_env_stats()
  230 
  231         self.assertEqual(mock_notifier.call_count, 2)
  232 
  233         dict_env_1 = {'version': 2,
  234                       'updated': later,
  235                       'tenant_id': u'test_tenant_id1',
  236                       'created': now,
  237                       'description_text': u'',
  238                       'status': 'ready',
  239                       'id': u'test_env_id_1',
  240                       'name': u'test_environment1'}
  241 
  242         dict_env_2 = {'version': 1,
  243                       'updated': later,
  244                       'tenant_id': u'test_tenant_id2',
  245                       'created': now,
  246                       'description_text': u'',
  247                       'status': 'ready',
  248                       'id': u'test_env_id_2',
  249                       'name': u'test_environment2'}
  250 
  251         calls = [mock.call('environment.exists', dict_env_1),
  252                  mock.call('environment.exists', dict_env_2)]
  253 
  254         mock_notifier.assert_has_calls(calls)