"Fossies" - the Fresh Open Source Software Archive

Member "panko-8.1.0/panko/tests/functional/storage/test_storage_scenarios.py" (4 Aug 2021, 22564 Bytes) of package /linux/misc/openstack/panko-8.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. See also the latest Fossies "Diffs" side-by-side code changes report for "test_storage_scenarios.py": 8.0.0_vs_8.1.0.

    1 #
    2 # Copyright 2013 Intel Corp.
    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 """Base classes for DB backend implementation test"""
   16 
   17 import datetime
   18 import operator
   19 from unittest import mock
   20 
   21 from oslo_utils import timeutils
   22 
   23 from panko import storage
   24 from panko.storage import models
   25 from panko.tests import db as tests_db
   26 
   27 
   28 class EventTestBase(tests_db.TestBase):
   29     """Separate test base class.
   30 
   31     We don't want to inherit all the Meter stuff.
   32     """
   33 
   34     def setUp(self):
   35         super(EventTestBase, self).setUp()
   36         self.prepare_data()
   37 
   38     def prepare_data(self):
   39         self.models = []
   40         base = 0
   41         self.start = datetime.datetime(2013, 12, 31, 5, 0)
   42         now = self.start
   43         for event_type in ['Foo', 'Bar', 'Zoo', 'Foo', 'Bar', 'Zoo']:
   44             trait_models = [models.Trait(name, dtype, value)
   45                             for name, dtype, value in [
   46                                 ('trait_A', models.Trait.TEXT_TYPE,
   47                                     "my_%s_text" % event_type),
   48                                 ('trait_B', models.Trait.INT_TYPE,
   49                                     base + 1),
   50                                 ('trait_C', models.Trait.FLOAT_TYPE,
   51                                     float(base) + 0.123456),
   52                                 ('trait_D', models.Trait.DATETIME_TYPE,
   53                                     now)]]
   54             self.models.append(
   55                 models.Event("id_%s_%d" % (event_type, base),
   56                              event_type, now, trait_models,
   57                              {'status': {'nested': 'started'}}))
   58             base += 100
   59             now = now + datetime.timedelta(hours=1)
   60         self.end = now
   61 
   62         self.conn.record_events(self.models)
   63 
   64 
   65 @tests_db.run_with('sqlite', 'mysql', 'pgsql')
   66 class EventTTLTest(EventTestBase):
   67 
   68     @mock.patch.object(timeutils, 'utcnow')
   69     def test_clear_expired_data(self, mock_utcnow):
   70         mock_utcnow.return_value = datetime.datetime(2013, 12, 31, 10, 0)
   71         self.conn.clear_expired_data(3600, 100)
   72 
   73         events = list(self.conn.get_events(storage.EventFilter()))
   74         self.assertEqual(2, len(events))
   75         event_types = list(self.conn.get_event_types())
   76         self.assertEqual(['Bar', 'Zoo'], event_types)
   77         for event_type in event_types:
   78             trait_types = list(self.conn.get_trait_types(event_type))
   79             self.assertEqual(4, len(trait_types))
   80             traits = list(self.conn.get_traits(event_type))
   81             self.assertEqual(4, len(traits))
   82 
   83 
   84 @tests_db.run_with('sqlite', 'mysql', 'pgsql', 'mongodb')
   85 class EventTest(EventTestBase):
   86     def test_duplicate_message_id(self):
   87         now = datetime.datetime.utcnow()
   88         m = [models.Event("1", "Foo", now, None, {}),
   89              models.Event("1", "Zoo", now, [], {})]
   90         with mock.patch('%s.LOG' %
   91                         self.conn.record_events.__module__) as log:
   92             self.conn.record_events(m)
   93             self.assertEqual(1, log.debug.call_count)
   94 
   95     def test_bad_event(self):
   96         now = datetime.datetime.utcnow()
   97         broken_event = models.Event("1", "Foo", now, None, {})
   98         del(broken_event.__dict__['raw'])
   99         m = [broken_event, broken_event]
  100         with mock.patch('%s.LOG' %
  101                         self.conn.record_events.__module__) as log:
  102             self.assertRaises(AttributeError, self.conn.record_events, m)
  103             # ensure that record_events does not break on first error but
  104             # delays exception and tries to record each event.
  105             self.assertEqual(2, log.exception.call_count)
  106 
  107 
  108 class BigIntegerTest(EventTestBase):
  109     def test_trait_bigint(self):
  110         big = 99999999999999
  111         new_events = [models.Event(
  112             "id_testid", "MessageIDTest", self.start,
  113             [models.Trait('int', models.Trait.INT_TYPE, big)], {})]
  114         self.conn.record_events(new_events)
  115 
  116 
  117 class GetEventTest(EventTestBase):
  118 
  119     def test_generated_is_datetime(self):
  120         event_filter = storage.EventFilter(self.start, self.end)
  121         events = [event for event in self.conn.get_events(event_filter)]
  122         self.assertEqual(6, len(events))
  123         for i, event in enumerate(events):
  124             self.assertIsInstance(event.generated, datetime.datetime)
  125             self.assertEqual(event.generated,
  126                              self.models[i].generated)
  127             model_traits = self.models[i].traits
  128             for j, trait in enumerate(event.traits):
  129                 if trait.dtype == models.Trait.DATETIME_TYPE:
  130                     self.assertIsInstance(trait.value, datetime.datetime)
  131                     self.assertEqual(trait.value, model_traits[j].value)
  132 
  133     def test_simple_get(self):
  134         event_filter = storage.EventFilter(self.start, self.end)
  135         events = [event for event in self.conn.get_events(event_filter)]
  136         self.assertEqual(6, len(events))
  137         start_time = None
  138         for i, type in enumerate(['Foo', 'Bar', 'Zoo']):
  139             self.assertEqual(type, events[i].event_type)
  140             self.assertEqual(4, len(events[i].traits))
  141             # Ensure sorted results ...
  142             if start_time is not None:
  143                 # Python 2.6 has no assertLess :(
  144                 self.assertTrue(start_time < events[i].generated)
  145             start_time = events[i].generated
  146 
  147     def test_simple_get_event_type(self):
  148         expected_trait_values = {
  149             'id_Bar_100': {
  150                 'trait_A': 'my_Bar_text',
  151                 'trait_B': 101,
  152                 'trait_C': 100.123456,
  153                 'trait_D': self.start + datetime.timedelta(hours=1)
  154             },
  155             'id_Bar_400': {
  156                 'trait_A': 'my_Bar_text',
  157                 'trait_B': 401,
  158                 'trait_C': 400.123456,
  159                 'trait_D': self.start + datetime.timedelta(hours=4)
  160             }
  161         }
  162 
  163         event_filter = storage.EventFilter(self.start, self.end, "Bar")
  164         events = [event for event in self.conn.get_events(event_filter)]
  165         self.assertEqual(2, len(events))
  166         self.assertEqual("Bar", events[0].event_type)
  167         self.assertEqual("Bar", events[1].event_type)
  168         self.assertEqual(4, len(events[0].traits))
  169         self.assertEqual(4, len(events[1].traits))
  170         for event in events:
  171             trait_values = expected_trait_values.get(event.message_id,
  172                                                      None)
  173             if not trait_values:
  174                 self.fail("Unexpected event ID returned:" % event.message_id)
  175 
  176             for trait in event.traits:
  177                 expected_val = trait_values.get(trait.name)
  178                 if not expected_val:
  179                     self.fail("Unexpected trait type: %s" % trait.dtype)
  180                 self.assertEqual(expected_val, trait.value)
  181 
  182     def test_get_event_trait_filter(self):
  183         trait_filters = [{'key': 'trait_B', 'integer': 101}]
  184         event_filter = storage.EventFilter(self.start, self.end,
  185                                            traits_filter=trait_filters)
  186         events = [event for event in self.conn.get_events(event_filter)]
  187         self.assertEqual(1, len(events))
  188         self.assertEqual("Bar", events[0].event_type)
  189         self.assertEqual(4, len(events[0].traits))
  190 
  191     def test_get_event_trait_filter_op_string(self):
  192         trait_filters = [{'key': 'trait_A', 'string': 'my_Foo_text',
  193                           'op': 'eq'}]
  194         event_filter = storage.EventFilter(self.start, self.end,
  195                                            traits_filter=trait_filters)
  196         events = [event for event in self.conn.get_events(event_filter)]
  197         self.assertEqual(2, len(events))
  198         self.assertEqual("Foo", events[0].event_type)
  199         self.assertEqual(4, len(events[0].traits))
  200         trait_filters[0].update({'key': 'trait_A', 'op': 'lt'})
  201         event_filter = storage.EventFilter(self.start, self.end,
  202                                            traits_filter=trait_filters)
  203         events = [event for event in self.conn.get_events(event_filter)]
  204         self.assertEqual(2, len(events))
  205         self.assertEqual("Bar", events[0].event_type)
  206         trait_filters[0].update({'key': 'trait_A', 'op': 'le'})
  207         event_filter = storage.EventFilter(self.start, self.end,
  208                                            traits_filter=trait_filters)
  209         events = [event for event in self.conn.get_events(event_filter)]
  210         self.assertEqual(4, len(events))
  211         self.assertEqual("Bar", events[1].event_type)
  212         trait_filters[0].update({'key': 'trait_A', 'op': 'ne'})
  213         event_filter = storage.EventFilter(self.start, self.end,
  214                                            traits_filter=trait_filters)
  215         events = [event for event in self.conn.get_events(event_filter)]
  216         self.assertEqual(4, len(events))
  217         self.assertEqual("Zoo", events[3].event_type)
  218         trait_filters[0].update({'key': 'trait_A', 'op': 'gt'})
  219         event_filter = storage.EventFilter(self.start, self.end,
  220                                            traits_filter=trait_filters)
  221         events = [event for event in self.conn.get_events(event_filter)]
  222         self.assertEqual(2, len(events))
  223         self.assertEqual("Zoo", events[0].event_type)
  224         trait_filters[0].update({'key': 'trait_A', 'op': 'ge'})
  225         event_filter = storage.EventFilter(self.start, self.end,
  226                                            traits_filter=trait_filters)
  227         events = [event for event in self.conn.get_events(event_filter)]
  228         self.assertEqual(4, len(events))
  229         self.assertEqual("Foo", events[2].event_type)
  230 
  231     def test_get_event_trait_filter_op_integer(self):
  232         trait_filters = [{'key': 'trait_B', 'integer': 101, 'op': 'eq'}]
  233         event_filter = storage.EventFilter(self.start, self.end,
  234                                            traits_filter=trait_filters)
  235         events = [event for event in self.conn.get_events(event_filter)]
  236         self.assertEqual(1, len(events))
  237         self.assertEqual("Bar", events[0].event_type)
  238         self.assertEqual(4, len(events[0].traits))
  239         trait_filters[0].update({'key': 'trait_B', 'op': 'lt'})
  240         event_filter = storage.EventFilter(self.start, self.end,
  241                                            traits_filter=trait_filters)
  242         events = [event for event in self.conn.get_events(event_filter)]
  243         self.assertEqual(1, len(events))
  244         self.assertEqual("Foo", events[0].event_type)
  245         trait_filters[0].update({'key': 'trait_B', 'op': 'le'})
  246         event_filter = storage.EventFilter(self.start, self.end,
  247                                            traits_filter=trait_filters)
  248         events = [event for event in self.conn.get_events(event_filter)]
  249         self.assertEqual(2, len(events))
  250         self.assertEqual("Bar", events[1].event_type)
  251         trait_filters[0].update({'key': 'trait_B', 'op': 'ne'})
  252         event_filter = storage.EventFilter(self.start, self.end,
  253                                            traits_filter=trait_filters)
  254         events = [event for event in self.conn.get_events(event_filter)]
  255         self.assertEqual(5, len(events))
  256         self.assertEqual("Zoo", events[4].event_type)
  257         trait_filters[0].update({'key': 'trait_B', 'op': 'gt'})
  258         event_filter = storage.EventFilter(self.start, self.end,
  259                                            traits_filter=trait_filters)
  260         events = [event for event in self.conn.get_events(event_filter)]
  261         self.assertEqual(4, len(events))
  262         self.assertEqual("Zoo", events[0].event_type)
  263         trait_filters[0].update({'key': 'trait_B', 'op': 'ge'})
  264         event_filter = storage.EventFilter(self.start, self.end,
  265                                            traits_filter=trait_filters)
  266         events = [event for event in self.conn.get_events(event_filter)]
  267         self.assertEqual(5, len(events))
  268         self.assertEqual("Foo", events[2].event_type)
  269 
  270     def test_get_event_trait_filter_op_float(self):
  271         trait_filters = [{'key': 'trait_C', 'float': 300.123456, 'op': 'eq'}]
  272         event_filter = storage.EventFilter(self.start, self.end,
  273                                            traits_filter=trait_filters)
  274         events = [event for event in self.conn.get_events(event_filter)]
  275         self.assertEqual(1, len(events))
  276         self.assertEqual("Foo", events[0].event_type)
  277         self.assertEqual(4, len(events[0].traits))
  278         trait_filters[0].update({'key': 'trait_C', 'op': 'lt'})
  279         event_filter = storage.EventFilter(self.start, self.end,
  280                                            traits_filter=trait_filters)
  281         events = [event for event in self.conn.get_events(event_filter)]
  282         self.assertEqual(3, len(events))
  283         self.assertEqual("Zoo", events[2].event_type)
  284         trait_filters[0].update({'key': 'trait_C', 'op': 'le'})
  285         event_filter = storage.EventFilter(self.start, self.end,
  286                                            traits_filter=trait_filters)
  287         events = [event for event in self.conn.get_events(event_filter)]
  288         self.assertEqual(4, len(events))
  289         self.assertEqual("Bar", events[1].event_type)
  290         trait_filters[0].update({'key': 'trait_C', 'op': 'ne'})
  291         event_filter = storage.EventFilter(self.start, self.end,
  292                                            traits_filter=trait_filters)
  293         events = [event for event in self.conn.get_events(event_filter)]
  294         self.assertEqual(5, len(events))
  295         self.assertEqual("Zoo", events[2].event_type)
  296         trait_filters[0].update({'key': 'trait_C', 'op': 'gt'})
  297         event_filter = storage.EventFilter(self.start, self.end,
  298                                            traits_filter=trait_filters)
  299         events = [event for event in self.conn.get_events(event_filter)]
  300         self.assertEqual(2, len(events))
  301         self.assertEqual("Bar", events[0].event_type)
  302         trait_filters[0].update({'key': 'trait_C', 'op': 'ge'})
  303         event_filter = storage.EventFilter(self.start, self.end,
  304                                            traits_filter=trait_filters)
  305         events = [event for event in self.conn.get_events(event_filter)]
  306         self.assertEqual(3, len(events))
  307         self.assertEqual("Zoo", events[2].event_type)
  308 
  309     def test_get_event_trait_filter_op_datetime(self):
  310         trait_filters = [{'key': 'trait_D',
  311                           'datetime': self.start + datetime.timedelta(hours=2),
  312                           'op': 'eq'}]
  313         event_filter = storage.EventFilter(self.start, self.end,
  314                                            traits_filter=trait_filters)
  315         events = [event for event in self.conn.get_events(event_filter)]
  316         self.assertEqual(1, len(events))
  317         self.assertEqual("Zoo", events[0].event_type)
  318         self.assertEqual(4, len(events[0].traits))
  319         trait_filters[0].update({'key': 'trait_D', 'op': 'lt'})
  320         event_filter = storage.EventFilter(self.start, self.end,
  321                                            traits_filter=trait_filters)
  322         events = [event for event in self.conn.get_events(event_filter)]
  323         self.assertEqual(2, len(events))
  324         trait_filters[0].update({'key': 'trait_D', 'op': 'le'})
  325         self.assertEqual("Bar", events[1].event_type)
  326         event_filter = storage.EventFilter(self.start, self.end,
  327                                            traits_filter=trait_filters)
  328         events = [event for event in self.conn.get_events(event_filter)]
  329         self.assertEqual(3, len(events))
  330         self.assertEqual("Bar", events[1].event_type)
  331         trait_filters[0].update({'key': 'trait_D', 'op': 'ne'})
  332         event_filter = storage.EventFilter(self.start, self.end,
  333                                            traits_filter=trait_filters)
  334         events = [event for event in self.conn.get_events(event_filter)]
  335         self.assertEqual(5, len(events))
  336         self.assertEqual("Foo", events[2].event_type)
  337         trait_filters[0].update({'key': 'trait_D', 'op': 'gt'})
  338         event_filter = storage.EventFilter(self.start, self.end,
  339                                            traits_filter=trait_filters)
  340         events = [event for event in self.conn.get_events(event_filter)]
  341         self.assertEqual(3, len(events))
  342         self.assertEqual("Zoo", events[2].event_type)
  343         trait_filters[0].update({'key': 'trait_D', 'op': 'ge'})
  344         event_filter = storage.EventFilter(self.start, self.end,
  345                                            traits_filter=trait_filters)
  346         events = [event for event in self.conn.get_events(event_filter)]
  347         self.assertEqual(4, len(events))
  348         self.assertEqual("Bar", events[2].event_type)
  349 
  350     def test_get_event_multiple_trait_filter(self):
  351         trait_filters = [{'key': 'trait_B', 'integer': 1},
  352                          {'key': 'trait_C', 'float': 0.123456},
  353                          {'key': 'trait_A', 'string': 'my_Foo_text'}]
  354         event_filter = storage.EventFilter(self.start, self.end,
  355                                            traits_filter=trait_filters)
  356         events = [event for event in self.conn.get_events(event_filter)]
  357         self.assertEqual(1, len(events))
  358         self.assertEqual("Foo", events[0].event_type)
  359         self.assertEqual(4, len(events[0].traits))
  360 
  361     def test_get_event_multiple_trait_filter_expect_none(self):
  362         trait_filters = [{'key': 'trait_B', 'integer': 1},
  363                          {'key': 'trait_A', 'string': 'my_Zoo_text'}]
  364         event_filter = storage.EventFilter(self.start, self.end,
  365                                            traits_filter=trait_filters)
  366         events = [event for event in self.conn.get_events(event_filter)]
  367         self.assertEqual(0, len(events))
  368 
  369     def test_get_event_types(self):
  370         event_types = [e for e in
  371                        self.conn.get_event_types()]
  372 
  373         self.assertEqual(3, len(event_types))
  374         self.assertIn("Bar", event_types)
  375         self.assertIn("Foo", event_types)
  376         self.assertIn("Zoo", event_types)
  377 
  378     def test_get_trait_types(self):
  379         trait_types = [tt for tt in
  380                        self.conn.get_trait_types("Foo")]
  381         self.assertEqual(4, len(trait_types))
  382         trait_type_names = map(lambda x: x['name'], trait_types)
  383         self.assertIn("trait_A", trait_type_names)
  384         self.assertIn("trait_B", trait_type_names)
  385         self.assertIn("trait_C", trait_type_names)
  386         self.assertIn("trait_D", trait_type_names)
  387 
  388     def test_get_trait_types_unknown_event(self):
  389         trait_types = [tt for tt in
  390                        self.conn.get_trait_types("Moo")]
  391         self.assertEqual(0, len(trait_types))
  392 
  393     def test_get_traits(self):
  394         traits = self.conn.get_traits("Bar")
  395         # format results in a way that makes them easier to work with
  396         trait_dict = {}
  397         for trait in traits:
  398             trait_dict[trait.name] = trait.dtype
  399 
  400         self.assertIn("trait_A", trait_dict)
  401         self.assertEqual(models.Trait.TEXT_TYPE, trait_dict["trait_A"])
  402         self.assertIn("trait_B", trait_dict)
  403         self.assertEqual(models.Trait.INT_TYPE, trait_dict["trait_B"])
  404         self.assertIn("trait_C", trait_dict)
  405         self.assertEqual(models.Trait.FLOAT_TYPE, trait_dict["trait_C"])
  406         self.assertIn("trait_D", trait_dict)
  407         self.assertEqual(models.Trait.DATETIME_TYPE,
  408                          trait_dict["trait_D"])
  409 
  410     def test_get_all_traits(self):
  411         traits = self.conn.get_traits("Foo")
  412         traits = sorted([t for t in traits], key=operator.attrgetter('dtype'))
  413         self.assertEqual(8, len(traits))
  414         trait = traits[0]
  415         self.assertEqual("trait_A", trait.name)
  416         self.assertEqual(models.Trait.TEXT_TYPE, trait.dtype)
  417 
  418     def test_simple_get_event_no_traits(self):
  419         new_events = [models.Event("id_notraits", "NoTraits",
  420                       self.start, [], {})]
  421         self.conn.record_events(new_events)
  422         event_filter = storage.EventFilter(
  423             self.start, self.end, "NoTraits")
  424         events = [event for event in self.conn.get_events(event_filter)]
  425         self.assertEqual(1, len(events))
  426         self.assertEqual("id_notraits", events[0].message_id)
  427         self.assertEqual("NoTraits", events[0].event_type)
  428         self.assertEqual(0, len(events[0].traits))
  429 
  430     def test_simple_get_no_filters(self):
  431         event_filter = storage.EventFilter(None, None, None)
  432         events = [event for event in self.conn.get_events(event_filter)]
  433         self.assertEqual(6, len(events))
  434 
  435     def test_get_by_message_id(self):
  436         new_events = [models.Event("id_testid", "MessageIDTest",
  437                                    self.start, [], {})]
  438 
  439         self.conn.record_events(new_events)
  440         event_filter = storage.EventFilter(message_id="id_testid")
  441         events = [event for event in self.conn.get_events(event_filter)]
  442         self.assertEqual(1, len(events))
  443         event = events[0]
  444         self.assertEqual("id_testid", event.message_id)
  445 
  446     def test_simple_get_raw(self):
  447         event_filter = storage.EventFilter()
  448         events = [event for event in self.conn.get_events(event_filter)]
  449         self.assertTrue(events)
  450         self.assertEqual({'status': {'nested': 'started'}}, events[0].raw)
  451 
  452     def test_trait_type_enforced_on_none(self):
  453         new_events = [models.Event(
  454             "id_testid", "MessageIDTest", self.start,
  455             [models.Trait('text', models.Trait.TEXT_TYPE, ''),
  456              models.Trait('int', models.Trait.INT_TYPE, 0),
  457              models.Trait('float', models.Trait.FLOAT_TYPE, 0.0)],
  458             {})]
  459         self.conn.record_events(new_events)
  460         event_filter = storage.EventFilter(message_id="id_testid")
  461         events = [event for event in self.conn.get_events(event_filter)]
  462         options = [(models.Trait.TEXT_TYPE, ''),
  463                    (models.Trait.INT_TYPE, 0.0),
  464                    (models.Trait.FLOAT_TYPE, 0.0)]
  465         for trait in events[0].traits:
  466             options.remove((trait.dtype, trait.value))