545200dd22
As the events API gets fleshed out and more robust, it will become increasingly necessary to ensure that the database schema modeling events has the ability to quickly retrieve a list of event types. With the current schema having a foreign key from event to unique_name (the monolithic generic string key lookup table), in order to get a simple list of event types, one needs to do the following: SELECT DISTINCT e.unique_name FROM event; Not only is this inefficient, but the readability of the model and database schema suffers from the generalization of the unique name field relationship. With this patch, a new event_type table is added to the schema to allow for quick and easy lookups of event types like so: SELECT id, desc FROM event_type; This will lead to future ability to categorize event types with ease. Instead of adding columns to the monolithic unique_name table, we would only need to add a foreign key relationship to the new event_type table tying the event_type to a future event_type_class table... This patch also renames "event_name" to "event_type" everywhere to avoid confusion. Change-Id: I6e630ec534f16ba1bb9370d1859ae7640fc6b05b Partial-Bug: 1211015
166 lines
5.8 KiB
Python
166 lines
5.8 KiB
Python
# -*- encoding: utf-8 -*-
|
|
#
|
|
# Author: John Tran <jhtran@att.com>
|
|
# Julien Danjou <julien@danjou.info>
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
"""Tests for ceilometer/storage/impl_sqlalchemy.py
|
|
|
|
.. note::
|
|
In order to run the tests against real SQL server set the environment
|
|
variable CEILOMETER_TEST_SQL_URL to point to a SQL server before running
|
|
the tests.
|
|
|
|
"""
|
|
|
|
import datetime
|
|
import repr
|
|
|
|
from mock import patch
|
|
|
|
from ceilometer.storage import models
|
|
from ceilometer.storage.sqlalchemy import models as sql_models
|
|
from ceilometer.tests import db as tests_db
|
|
from ceilometer import utils
|
|
|
|
|
|
class EventTestBase(tests_db.TestBase):
|
|
# Note: Do not derive from SQLAlchemyEngineTestBase, since we
|
|
# don't want to automatically inherit all the Meter setup.
|
|
database_connection = 'sqlite://'
|
|
|
|
|
|
class CeilometerBaseTest(EventTestBase):
|
|
def test_ceilometer_base(self):
|
|
base = sql_models.CeilometerBase()
|
|
base['key'] = 'value'
|
|
self.assertEqual(base['key'], 'value')
|
|
|
|
|
|
class UniqueNameTest(EventTestBase):
|
|
# UniqueName is a construct specific to sqlalchemy.
|
|
# Not applicable to other drivers.
|
|
|
|
def test_unique_exists(self):
|
|
u1 = self.conn._get_or_create_unique_name("foo")
|
|
self.assertTrue(u1.id >= 0)
|
|
u2 = self.conn._get_or_create_unique_name("foo")
|
|
self.assertEqual(u1.id, u2.id)
|
|
self.assertEqual(u1.key, u2.key)
|
|
|
|
def test_new_unique(self):
|
|
u1 = self.conn._get_or_create_unique_name("foo")
|
|
self.assertTrue(u1.id >= 0)
|
|
u2 = self.conn._get_or_create_unique_name("blah")
|
|
self.assertNotEqual(u1.id, u2.id)
|
|
self.assertNotEqual(u1.key, u2.key)
|
|
# Test the method __repr__ returns a string
|
|
self.assertTrue(repr.repr(u2))
|
|
|
|
|
|
class EventTypeTest(EventTestBase):
|
|
# EventType is a construct specific to sqlalchemy
|
|
# Not applicable to other drivers.
|
|
|
|
def test_event_type_exists(self):
|
|
et1 = self.conn._get_or_create_event_type("foo")
|
|
self.assertTrue(et1.id >= 0)
|
|
et2 = self.conn._get_or_create_event_type("foo")
|
|
self.assertEqual(et1.id, et2.id)
|
|
self.assertEqual(et1.desc, et2.desc)
|
|
|
|
def test_event_type_unique(self):
|
|
et1 = self.conn._get_or_create_event_type("foo")
|
|
self.assertTrue(et1.id >= 0)
|
|
et2 = self.conn._get_or_create_event_type("blah")
|
|
self.assertNotEqual(et1.id, et2.id)
|
|
self.assertNotEqual(et1.desc, et2.desc)
|
|
# Test the method __repr__ returns a string
|
|
self.assertTrue(repr.repr(et2))
|
|
|
|
|
|
class MyException(Exception):
|
|
pass
|
|
|
|
|
|
class EventTest(EventTestBase):
|
|
def test_string_traits(self):
|
|
model = models.Trait("Foo", models.Trait.TEXT_TYPE, "my_text")
|
|
trait = self.conn._make_trait(model, None)
|
|
self.assertEqual(trait.t_type, models.Trait.TEXT_TYPE)
|
|
self.assertIsNone(trait.t_float)
|
|
self.assertIsNone(trait.t_int)
|
|
self.assertIsNone(trait.t_datetime)
|
|
self.assertEqual(trait.t_string, "my_text")
|
|
self.assertIsNotNone(trait.name)
|
|
|
|
def test_int_traits(self):
|
|
model = models.Trait("Foo", models.Trait.INT_TYPE, 100)
|
|
trait = self.conn._make_trait(model, None)
|
|
self.assertEqual(trait.t_type, models.Trait.INT_TYPE)
|
|
self.assertIsNone(trait.t_float)
|
|
self.assertIsNone(trait.t_string)
|
|
self.assertIsNone(trait.t_datetime)
|
|
self.assertEqual(trait.t_int, 100)
|
|
self.assertIsNotNone(trait.name)
|
|
|
|
def test_float_traits(self):
|
|
model = models.Trait("Foo", models.Trait.FLOAT_TYPE, 123.456)
|
|
trait = self.conn._make_trait(model, None)
|
|
self.assertEqual(trait.t_type, models.Trait.FLOAT_TYPE)
|
|
self.assertIsNone(trait.t_int)
|
|
self.assertIsNone(trait.t_string)
|
|
self.assertIsNone(trait.t_datetime)
|
|
self.assertEqual(trait.t_float, 123.456)
|
|
self.assertIsNotNone(trait.name)
|
|
|
|
def test_datetime_traits(self):
|
|
now = datetime.datetime.utcnow()
|
|
model = models.Trait("Foo", models.Trait.DATETIME_TYPE, now)
|
|
trait = self.conn._make_trait(model, None)
|
|
self.assertEqual(trait.t_type, models.Trait.DATETIME_TYPE)
|
|
self.assertIsNone(trait.t_int)
|
|
self.assertIsNone(trait.t_string)
|
|
self.assertIsNone(trait.t_float)
|
|
self.assertEqual(trait.t_datetime, utils.dt_to_decimal(now))
|
|
self.assertIsNotNone(trait.name)
|
|
|
|
def test_bad_event(self):
|
|
now = datetime.datetime.utcnow()
|
|
m = [models.Event("1", "Foo", now, []),
|
|
models.Event("2", "Zoo", now, [])]
|
|
|
|
with patch.object(self.conn, "_record_event") as mock_save:
|
|
mock_save.side_effect = MyException("Boom")
|
|
problem_events = self.conn.record_events(m)
|
|
self.assertEqual(2, len(problem_events))
|
|
for bad, event in problem_events:
|
|
self.assertEqual(models.Event.UNKNOWN_PROBLEM, bad)
|
|
|
|
def test_get_none_value_traits(self):
|
|
model = sql_models.Trait(None, None, 5)
|
|
self.assertIsNone(model.get_value())
|
|
self.assertTrue(repr.repr(model))
|
|
|
|
def test_event_repr(self):
|
|
ev = sql_models.Event('msg_id', None, False)
|
|
ev.id = 100
|
|
self.assertTrue(repr.repr(ev))
|
|
|
|
|
|
class ModelTest(tests_db.TestBase):
|
|
database_connection = 'mysql://localhost'
|
|
|
|
def test_model_table_args(self):
|
|
self.assertIsNotNone(sql_models.table_args())
|