"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-4.0.0/monasca_api/tests/test_healthchecks.py" (13 May 2020, 6464 Bytes) of package /linux/misc/openstack/monasca-api-4.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_healthchecks.py": 3.1.0_vs_4.0.0.

    1 # Copyright 2017 FUJITSU LIMITED
    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 falcon
   16 from unittest import mock
   17 
   18 from monasca_api import config
   19 from monasca_api.healthcheck import base
   20 from monasca_api import healthchecks
   21 from monasca_api.tests import base as test_base
   22 
   23 
   24 CONF = config.CONF
   25 ENDPOINT = '/healthcheck'
   26 
   27 
   28 class TestHealthChecks(test_base.BaseApiTestCase):
   29 
   30     @classmethod
   31     def tearDownClass(cls):
   32         if hasattr(CONF, 'sql_engine'):
   33             delattr(CONF, 'sql_engine')
   34 
   35     def setUp(self):
   36         super(TestHealthChecks, self).setUp()
   37         self.resources = healthchecks.HealthChecks()
   38         self.app.add_route(
   39             ENDPOINT,
   40             self.resources
   41         )
   42 
   43     @mock.patch('monasca_api.healthcheck.alarms_db_check.sql_repository.get_engine')
   44     @mock.patch(
   45         'monasca_api.healthcheck.metrics_db_check.MetricsDbCheck')
   46     def test_should_return_200_for_head(self, metrics_db_check, _):
   47         result = self.simulate_request(path=ENDPOINT, method='HEAD')
   48         self.assertEqual(falcon.HTTP_NO_CONTENT, result.status)
   49 
   50     @mock.patch('monasca_api.healthcheck.kafka_check.KafkaHealthCheck')
   51     @mock.patch(
   52         'monasca_api.healthcheck.alarms_db_check.AlarmsDbHealthCheck')
   53     @mock.patch(
   54         'monasca_api.healthcheck.metrics_db_check.MetricsDbCheck')
   55     @mock.patch(
   56         'monasca_api.healthcheck.alarms_db_check.sql_repository.SQLRepository')
   57     def test_should_report_healthy_if_all_services_healthy(self, kafka_check,
   58                                                            alarms_db_check,
   59                                                            metrics_db_check,
   60                                                            _):
   61         kafka_check.health_check.return_value = base.CheckResult(True, 'OK')
   62         alarms_db_check.health_check.return_value = base.CheckResult(True,
   63                                                                      'OK')
   64         metrics_db_check.health_check.return_value = base.CheckResult(True,
   65                                                                       'OK')
   66         self.resources._kafka_check = kafka_check
   67         self.resources._alarm_db_check = alarms_db_check
   68         self.resources._metrics_db_check = metrics_db_check
   69 
   70         response = self.simulate_request(path=ENDPOINT,
   71                                          headers={
   72                                              'Content-Type': 'application/json'
   73                                          },
   74                                          method='GET')
   75         self.assertEqual(falcon.HTTP_OK, response.status)
   76 
   77         response = response.json
   78         self.assertIn('kafka', response)
   79         self.assertIn('alarms_database', response)
   80         self.assertIn('metrics_database', response)
   81         self.assertEqual('OK', response.get('kafka'))
   82         self.assertEqual('OK', response.get('alarms_database'))
   83         self.assertEqual('OK', response.get('metrics_database'))
   84 
   85     @mock.patch('monasca_api.healthcheck.kafka_check.KafkaHealthCheck')
   86     @mock.patch(
   87         'monasca_api.healthcheck.alarms_db_check.AlarmsDbHealthCheck')
   88     @mock.patch(
   89         'monasca_api.healthcheck.metrics_db_check.MetricsDbCheck')
   90     @mock.patch(
   91         'monasca_api.healthcheck.alarms_db_check.sql_repository.SQLRepository')
   92     def test_should_report_not_healthy_if_one_service_not_healthy(self,
   93                                                                   kafka_check,
   94                                                                   alarms_db_check,
   95                                                                   metrics_db_check,
   96                                                                   _):
   97         test_list = [
   98             {'kafka': {'healthy': False, 'message': 'Unavailable'},
   99              'alarms_db': {'healthy': True, 'message': 'OK'},
  100              'netrics_db': {'healthy': True, 'message': 'OK'}
  101              },
  102             {'kafka': {'healthy': True, 'message': 'OK'},
  103              'alarms_db': {'healthy': False, 'message': 'Connection Error'},
  104              'netrics_db': {'healthy': True, 'message': 'OK'}
  105              },
  106             {'kafka': {'healthy': True, 'message': 'OK'},
  107              'alarms_db': {'healthy': True, 'message': 'OK'},
  108              'netrics_db': {'healthy': False, 'message': 'Error'}
  109              },
  110         ]
  111 
  112         for service in test_list:
  113             kafka_check.health_check.return_value = base.CheckResult(service['kafka']['healthy'],
  114                                                                      service['kafka']['message'])
  115             alarms_db_check.health_check.return_value = base.CheckResult(
  116                 service['alarms_db']['healthy'], service['alarms_db']['message'])
  117             metrics_db_check.health_check.return_value = base.CheckResult(
  118                 service['netrics_db']['healthy'], service['netrics_db']['message'])
  119             self.resources._kafka_check = kafka_check
  120             self.resources._alarm_db_check = alarms_db_check
  121             self.resources._metrics_db_check = metrics_db_check
  122 
  123             response = self.simulate_request(path=ENDPOINT,
  124                                              headers={
  125                                                  'Content-Type': 'application/json'
  126                                              },
  127                                              method='GET')
  128             self.assertEqual(falcon.HTTP_SERVICE_UNAVAILABLE,
  129                              response.status)
  130 
  131             response = response.json
  132             self.assertIn('kafka', response)
  133             self.assertIn('alarms_database', response)
  134             self.assertIn('metrics_database', response)
  135             self.assertEqual(service['kafka']['message'], response.get('kafka'))
  136             self.assertEqual(service['alarms_db']['message'], response.get('alarms_database'))
  137             self.assertEqual(service['netrics_db']['message'], response.get('metrics_database'))