"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-3.1.0/monasca_api/tests/test_sub_alarm_definition.py" (27 Sep 2019, 6510 Bytes) of package /linux/misc/openstack/monasca-api-3.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 2019 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 from monasca_api.common.repositories.model import sub_alarm_definition
   15 from monasca_api.expression_parser import alarm_expr_parser
   16 from monasca_api.tests import base
   17 
   18 
   19 class TestSubAlarmDefinition(base.BaseTestCase):
   20     def test_init_from_row(self):
   21         sub_alarm_d_dict = {'id': '111',
   22                             'alarm_definition_id': '123',
   23                             'function': 'AVG',
   24                             'metric_name': 'darth.vader',
   25                             'operator': 'GT',
   26                             'threshold': 10,
   27                             'period': 60,
   28                             'periods': 1,
   29                             'is_deterministic': 1,
   30                             'dimensions': 'device=1,image_id=888'}
   31         dimension_dict = {'device': '1',
   32                           'image_id': '888'}
   33         sub_alarm_d = sub_alarm_definition.SubAlarmDefinition(row=sub_alarm_d_dict)
   34         self.assertEqual(sub_alarm_d_dict['id'], sub_alarm_d.id)
   35         self.assertEqual(sub_alarm_d_dict['alarm_definition_id'], sub_alarm_d.alarm_definition_id)
   36         self.assertEqual(sub_alarm_d_dict['metric_name'], sub_alarm_d.metric_name)
   37         self.assertEqual(sub_alarm_d_dict['dimensions'], sub_alarm_d.dimensions_str)
   38         self.assertEqual(dimension_dict, sub_alarm_d.dimensions)
   39         self.assertEqual(sub_alarm_d_dict['function'], sub_alarm_d.function)
   40         self.assertEqual(sub_alarm_d_dict['operator'], sub_alarm_d.operator)
   41         self.assertEqual(sub_alarm_d_dict['period'], sub_alarm_d.period)
   42         self.assertEqual(sub_alarm_d_dict['periods'], sub_alarm_d.periods)
   43         self.assertEqual(sub_alarm_d_dict['threshold'], sub_alarm_d.threshold)
   44         self.assertEqual(True, sub_alarm_d.deterministic)
   45 
   46     def test_init_from_sub_expr(self):
   47         sub_alarm_d_dict = {'function': 'AVG',
   48                             'metric_name': 'darth.vader',
   49                             'operator': 'GT',
   50                             'threshold': 10.0,
   51                             'period': 60,
   52                             'periods': 1,
   53                             'is_deterministic': 0,
   54                             'dimensions': 'device=1,image_id=888'}
   55         dimension_dict = {'device': '1',
   56                           'image_id': '888'}
   57         expression = 'avg(darth.vader{device=1,image_id=888}, 60) GT 10.0 times 1'
   58         sub_expr_list = alarm_expr_parser.AlarmExprParser(expression).sub_expr_list
   59         sub_alarm_d = sub_alarm_definition.SubAlarmDefinition(sub_expr=sub_expr_list[0])
   60         self.assertEqual(sub_alarm_d_dict['metric_name'], sub_alarm_d.metric_name)
   61         self.assertEqual(sub_alarm_d_dict['dimensions'], sub_alarm_d.dimensions_str)
   62         self.assertEqual(dimension_dict, sub_alarm_d.dimensions)
   63         self.assertEqual(sub_alarm_d_dict['function'], sub_alarm_d.function)
   64         self.assertEqual(sub_alarm_d_dict['operator'], sub_alarm_d.operator)
   65         self.assertEqual(sub_alarm_d_dict['period'], sub_alarm_d.period)
   66         self.assertEqual(sub_alarm_d_dict['periods'], sub_alarm_d.periods)
   67         self.assertEqual(sub_alarm_d_dict['threshold'], sub_alarm_d.threshold)
   68         self.assertEqual(False, sub_alarm_d.deterministic)
   69 
   70     def test_init_from_both_row_and_sub_expr(self):
   71         sub_alarm_d_dict = {'id': '111',
   72                             'alarm_definition_id': '123',
   73                             'function': 'AVG',
   74                             'metric_name': 'darth.vader',
   75                             'operator': 'GT',
   76                             'threshold': 10,
   77                             'period': 60,
   78                             'periods': 1,
   79                             'is_deterministic': 0,
   80                             'dimensions': 'device=1,image_id=888'}
   81         expression = 'avg(darth.vader.compute{device=1,image_id=888}, 60) GT 10.0 times 1'
   82         sub_expr_list = alarm_expr_parser.AlarmExprParser(expression).sub_expr_list
   83         self.assertRaises(Exception, sub_alarm_definition.SubAlarmDefinition,
   84                           sub_alarm_d_dict, sub_expr_list)  # noqa: E202
   85 
   86     def test_build_expression_all_parameters(self):
   87         expression = 'avg(darth.vader{over=9000}, deterministic, 60) GT 10.0 times 1'
   88         sub_expr_list = alarm_expr_parser.AlarmExprParser(expression).sub_expr_list
   89         sub_alarm_d = sub_alarm_definition.SubAlarmDefinition(sub_expr=sub_expr_list[0])
   90         self.assertEqual(expression, sub_alarm_d.expression)
   91 
   92         sub_alarm_d.dimensions_str = None
   93         sub_alarm_d.dimensions = None
   94         sub_alarm_d.deterministic = False
   95         sub_alarm_d.period = None
   96         sub_alarm_d.periods = None
   97         self.assertEqual('avg(darth.vader) GT 10.0', sub_alarm_d.expression)
   98 
   99     def test_equality_method(self):
  100         sub_alarm_d_dict = {'id': '111',
  101                             'alarm_definition_id': '123',
  102                             'function': 'AVG',
  103                             'metric_name': 'darth.vader',
  104                             'operator': 'GT',
  105                             'threshold': 10,
  106                             'period': 60,
  107                             'periods': 1,
  108                             'is_deterministic': 0,
  109                             'dimensions': 'device=1,image_id=888'}
  110         sub_alarm_d = sub_alarm_definition.SubAlarmDefinition(row=sub_alarm_d_dict)
  111         sub_alarm_d2 = sub_alarm_definition.SubAlarmDefinition(row=sub_alarm_d_dict)
  112 
  113         # same object
  114         self.assertEqual(True, sub_alarm_d == sub_alarm_d)
  115         # different type
  116         self.assertEqual(False, sub_alarm_d == list())
  117         # Equal
  118         self.assertEqual(True, sub_alarm_d == sub_alarm_d2)
  119         # equal but different id
  120         sub_alarm_d2.id = '222'
  121         self.assertEqual(True, sub_alarm_d == sub_alarm_d2)
  122         # Not Equal
  123         sub_alarm_d2.metric_name = 'luck.iamyourfather'
  124         self.assertEqual(False, sub_alarm_d == sub_alarm_d2)