Replace tests.base part6

It is the sixth step to replace using ceilometer.tests.base with
openstack.common.test.
In this patch is changed for using
openstack.common.test and added needed fixtures in
test classes.
Added in ceilometer.tests.db code from ceilomete.tests.base
because ceilometer.tests.base will not be used in the future.

Change-Id: I3cacca4eff19eae8874decd892ab1225f95cfee9
This commit is contained in:
Vladislav Kuzmin 2013-10-25 15:55:15 +04:00
parent fd5a3db0ce
commit d289d253c4
20 changed files with 88 additions and 78 deletions

View File

@ -21,7 +21,6 @@
import urllib
import flask
from oslo.config import cfg
import pecan
import pecan.testing
@ -40,11 +39,12 @@ class TestBase(db_test_base.TestBase):
def setUp(self):
super(TestBase, self).setUp()
service.prepare_service([])
cfg.CONF.set_override("auth_version", "v2.0", group=acl.OPT_GROUP_NAME)
cfg.CONF.set_override("policy_file",
self.path_get('tests/policy.json'))
self.CONF.set_override("auth_version",
"v2.0", group=acl.OPT_GROUP_NAME)
self.CONF.set_override("policy_file",
self.path_get('tests/policy.json'))
sources_file = self.path_get('tests/sources.json')
self.app = v1_app.make_app(cfg.CONF,
self.app = v1_app.make_app(self.CONF,
enable_acl=False,
attach_storage=False,
sources_file=sources_file)
@ -87,9 +87,10 @@ class FunctionalTest(db_test_base.TestBase):
def setUp(self):
super(FunctionalTest, self).setUp()
cfg.CONF.set_override("auth_version", "v2.0", group=acl.OPT_GROUP_NAME)
cfg.CONF.set_override("policy_file",
self.path_get('tests/policy.json'))
self.CONF.set_override("auth_version", "v2.0",
group=acl.OPT_GROUP_NAME)
self.CONF.set_override("policy_file",
self.path_get('tests/policy.json'))
self.app = self._make_app()
def _make_app(self, enable_acl=False):

View File

@ -82,6 +82,18 @@ class BaseTestCase(test.BaseTestCase):
0.0,
places=5)
@staticmethod
def path_get(project_file=None):
root = os.path.abspath(os.path.join(os.path.dirname(__file__),
'..',
'..',
)
)
if project_file:
return os.path.join(root, project_file)
else:
return root
def _skip_decorator(func):
@functools.wraps(func)

View File

@ -24,28 +24,38 @@ import six
import uuid
import warnings
from oslo.config import cfg
from ceilometer import storage
from ceilometer.openstack.common.fixture import config
from ceilometer.tests import base as test_base
class TestBase(test_base.TestCase):
class TestBase(test_base.BaseTestCase):
def setUp(self):
super(TestBase, self).setUp()
cfg.CONF.set_override('connection', str(self.database_connection),
group='database')
self.CONF = self.useFixture(config.Config()).conf
self.CONF.set_override('connection', str(self.database_connection),
group='database')
with warnings.catch_warnings():
warnings.filterwarnings(
action='ignore',
message='.*you must provide a username and password.*')
try:
self.conn = storage.get_connection(cfg.CONF)
self.conn = storage.get_connection(self.CONF)
except storage.StorageBadVersion as e:
self.skipTest(str(e))
self.conn.upgrade()
self.CONF([], project='ceilometer')
# Set a default location for the pipeline config file so the
# tests work even if ceilometer is not installed globally on
# the system.
self.CONF.set_override(
'pipeline_cfg_file',
self.path_get('etc/ceilometer/pipeline.yaml')
)
def tearDown(self):
self.conn.clear()
self.conn = None

View File

@ -23,6 +23,7 @@ import logging
import testscenarios
from ceilometer.openstack.common import timeutils
from ceilometer.openstack.common.fixture import moxstubout
from ceilometer.storage import models
from ceilometer.tests import api as tests_api
from ceilometer.tests import db as tests_db
@ -38,6 +39,8 @@ class TestComputeDurationByResource(tests_api.TestBase,
def setUp(self):
super(TestComputeDurationByResource, self).setUp()
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
# Create events relative to the range and pretend
# that the intervening events exist.

View File

@ -21,10 +21,10 @@
import datetime
from ceilometer.api.v1 import blueprint
from ceilometer.tests import base
from ceilometer.openstack.common import test
class TestQueryTimestamps(base.TestCase):
class TestQueryTimestamps(test.BaseTestCase):
def test_get_query_timestamps_none_specified(self):
result = blueprint._get_query_timestamps()

View File

@ -22,8 +22,6 @@
import datetime
import testscenarios
from oslo.config import cfg
from ceilometer.publisher import rpc
from ceilometer import sample
@ -81,7 +79,7 @@ class TestListEvents(tests_api.TestBase,
]:
msg = rpc.meter_message_from_counter(
cnt,
cfg.CONF.publisher_rpc.metering_secret)
self.CONF.publisher_rpc.metering_secret)
self.conn.record_metering_data(msg)
def test_empty_project(self):

View File

@ -23,8 +23,6 @@ import datetime
import logging
import testscenarios
from oslo.config import cfg
from ceilometer.publisher import rpc
from ceilometer import sample
@ -49,7 +47,6 @@ class TestListMeters(tests_api.TestBase,
def setUp(self):
super(TestListMeters, self).setUp()
for cnt in [
sample.Sample(
'meter.test',
@ -113,7 +110,7 @@ class TestListMeters(tests_api.TestBase,
source='test_list_resources')]:
msg = rpc.meter_message_from_counter(
cnt,
cfg.CONF.publisher_rpc.metering_secret)
self.CONF.publisher_rpc.metering_secret)
self.conn.record_metering_data(msg)
def test_list_meters(self):

View File

@ -23,8 +23,6 @@ import datetime
import logging
import testscenarios
from oslo.config import cfg
from ceilometer.publisher import rpc
from ceilometer import sample
@ -64,7 +62,7 @@ class TestListProjects(tests_api.TestBase,
)
msg = rpc.meter_message_from_counter(
sample1,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)
@ -83,7 +81,7 @@ class TestListProjects(tests_api.TestBase,
)
msg2 = rpc.meter_message_from_counter(
sample2,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg2)

View File

@ -23,8 +23,6 @@ import datetime
import logging
import testscenarios
from oslo.config import cfg
from ceilometer.publisher import rpc
from ceilometer import sample
@ -105,7 +103,7 @@ class TestListResourcesBase(tests_api.TestBase,
)]:
msg = rpc.meter_message_from_counter(
cnt,
cfg.CONF.publisher_rpc.metering_secret)
self.CONF.publisher_rpc.metering_secret)
self.conn.record_metering_data(msg)

View File

@ -23,8 +23,6 @@ import datetime
import logging
import testscenarios
from oslo.config import cfg
from ceilometer.publisher import rpc
from ceilometer import sample
@ -66,7 +64,7 @@ class TestListUsers(tests_api.TestBase,
)
msg = rpc.meter_message_from_counter(
sample1,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)
@ -86,7 +84,7 @@ class TestListUsers(tests_api.TestBase,
)
msg2 = rpc.meter_message_from_counter(
sample2,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg2)

View File

@ -22,8 +22,6 @@
import datetime
import testscenarios
from oslo.config import cfg
from ceilometer.publisher import rpc
from ceilometer import sample
@ -38,7 +36,6 @@ class TestMaxProjectVolume(tests_api.TestBase,
def setUp(self):
super(TestMaxProjectVolume, self).setUp()
for i in range(3):
s = sample.Sample(
'volume.size',
@ -56,7 +53,7 @@ class TestMaxProjectVolume(tests_api.TestBase,
)
msg = rpc.meter_message_from_counter(
s,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)

View File

@ -21,8 +21,6 @@
import datetime
import testscenarios
from oslo.config import cfg
from ceilometer.publisher import rpc
from ceilometer import sample
@ -37,7 +35,6 @@ class TestMaxResourceVolume(tests_api.TestBase,
def setUp(self):
super(TestMaxResourceVolume, self).setUp()
for i in range(3):
s = sample.Sample(
'volume.size',
@ -55,7 +52,7 @@ class TestMaxResourceVolume(tests_api.TestBase,
)
msg = rpc.meter_message_from_counter(
s,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)

View File

@ -22,8 +22,6 @@
import datetime
import testscenarios
from oslo.config import cfg
from ceilometer.publisher import rpc
from ceilometer import sample
@ -38,7 +36,6 @@ class TestSumProjectVolume(tests_api.TestBase,
def setUp(self):
super(TestSumProjectVolume, self).setUp()
for i in range(3):
s = sample.Sample(
'volume.size',
@ -56,7 +53,7 @@ class TestSumProjectVolume(tests_api.TestBase,
)
msg = rpc.meter_message_from_counter(
s,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)

View File

@ -22,8 +22,6 @@
import datetime
import testscenarios
from oslo.config import cfg
from ceilometer.publisher import rpc
from ceilometer import sample
@ -38,7 +36,6 @@ class TestSumResourceVolume(tests_api.TestBase,
def setUp(self):
super(TestSumResourceVolume, self).setUp()
for i in range(3):
s = sample.Sample(
'volume.size',
@ -56,7 +53,7 @@ class TestSumResourceVolume(tests_api.TestBase,
)
msg = rpc.meter_message_from_counter(
s,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)

View File

@ -26,6 +26,7 @@ from ceilometer.api import app
from ceilometer.api import acl
from ceilometer import service
from ceilometer.openstack.common import gettextutils
from ceilometer.openstack.common.fixture import moxstubout
from ceilometer.tests import base
from ceilometer.tests import db as tests_db
from .base import FunctionalTest
@ -83,6 +84,10 @@ class TestApiMiddleware(FunctionalTest):
no_lang_translated_error = 'No lang translated error'
en_US_translated_error = 'en-US translated error'
def setUp(self):
super(TestApiMiddleware, self).setUp()
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
def _fake_get_localized_message(self, message, user_locale):
if user_locale is None:
return self.no_lang_translated_error

View File

@ -23,6 +23,7 @@ import logging
import testscenarios
from ceilometer.openstack.common import timeutils
from ceilometer.openstack.common.fixture import moxstubout
from ceilometer.storage import models
from ceilometer.tests import db as tests_db
from .base import FunctionalTest
@ -37,7 +38,7 @@ class TestComputeDurationByResource(FunctionalTest,
def setUp(self):
super(TestComputeDurationByResource, self).setUp()
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
# Create events relative to the range and pretend
# that the intervening events exist.

View File

@ -23,10 +23,9 @@ import datetime
import logging
import testscenarios
from oslo.config import cfg
from ceilometer.openstack.common import rpc
from ceilometer.openstack.common import timeutils
from ceilometer.openstack.common.fixture import moxstubout
from ceilometer.tests import db as tests_db
from .base import FunctionalTest
@ -47,6 +46,7 @@ class TestPostSamples(FunctionalTest,
def setUp(self):
super(TestPostSamples, self).setUp()
self.published = []
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
self.stubs.Set(rpc, 'cast', self.faux_cast)
def test_one(self):
@ -277,7 +277,7 @@ class TestPostSamples(FunctionalTest,
# source is modified to include the project_id.
s['source'] = '%s:%s' % (
s['project_id'],
s.get('source', cfg.CONF.sample_source)
s.get('source', self.CONF.sample_source)
)
# Ignore message id that is randomly generated
s['message_id'] = data.json[x]['message_id']

View File

@ -24,8 +24,7 @@
running the tests. Make sure the Thrift server is running on that server.
"""
from oslo.config import cfg
from ceilometer.openstack.common.fixture import moxstubout
from ceilometer.storage.impl_hbase import Connection
from ceilometer.storage.impl_hbase import MConnection
from ceilometer.tests import db as tests_db
@ -37,9 +36,13 @@ class HBaseEngineTestBase(tests_db.TestBase):
class ConnectionTest(HBaseEngineTestBase):
def setUp(self):
super(ConnectionTest, self).setUp()
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
def test_hbase_connection(self):
cfg.CONF.database.connection = self.database_connection
conn = Connection(cfg.CONF)
self.CONF.database.connection = self.database_connection
conn = Connection(self.CONF)
self.assertIsInstance(conn.conn, MConnection)
class TestConn(object):
@ -49,8 +52,8 @@ class ConnectionTest(HBaseEngineTestBase):
def open(self):
pass
cfg.CONF.database.connection = 'hbase://test_hbase:9090'
self.CONF.database.connection = 'hbase://test_hbase:9090'
self.stubs.Set(Connection, '_get_connection',
lambda self, x: TestConn(x['host'], x['port']))
conn = Connection(cfg.CONF)
conn = Connection(self.CONF)
self.assertIsInstance(conn.conn, TestConn)

View File

@ -27,8 +27,6 @@
import copy
import datetime
from oslo.config import cfg
from ceilometer.publisher import rpc
from ceilometer import sample
from ceilometer.storage import impl_mongodb
@ -46,14 +44,14 @@ class MongoDBEngineTestBase(tests_db.TestBase):
class MongoDBConnection(MongoDBEngineTestBase):
def test_connection_pooling(self):
self.assertEqual(self.conn.conn,
impl_mongodb.Connection(cfg.CONF).conn)
impl_mongodb.Connection(self.CONF).conn)
def test_replica_set(self):
cfg.CONF.set_override(
self.CONF.set_override(
'connection',
str(tests_db.MongoDBFakeConnectionUrl()) + '?replicaSet=foobar',
group='database')
conn = impl_mongodb.Connection(cfg.CONF)
conn = impl_mongodb.Connection(self.CONF)
self.assertTrue(conn.conn)
def test_recurse_sort_keys(self):
@ -100,24 +98,24 @@ class IndexTest(MongoDBEngineTestBase):
def test_meter_ttl_index_absent(self):
# create a fake index and check it is deleted
self.conn.db.meter.ensure_index('foo', name='meter_ttl')
cfg.CONF.set_override('time_to_live', -1, group='database')
self.CONF.set_override('time_to_live', -1, group='database')
self.conn.upgrade()
self.assertTrue(self.conn.db.meter.ensure_index('foo',
name='meter_ttl'))
cfg.CONF.set_override('time_to_live', 456789, group='database')
self.CONF.set_override('time_to_live', 456789, group='database')
self.conn.upgrade()
self.assertFalse(self.conn.db.meter.ensure_index('foo',
name='meter_ttl'))
def test_meter_ttl_index_present(self):
cfg.CONF.set_override('time_to_live', 456789, group='database')
self.CONF.set_override('time_to_live', 456789, group='database')
self.conn.upgrade()
self.assertFalse(self.conn.db.meter.ensure_index('foo',
name='meter_ttl'))
self.assertEqual(self.conn.db.meter.index_information()[
'meter_ttl']['expireAfterSeconds'], 456789)
cfg.CONF.set_override('time_to_live', -1, group='database')
self.CONF.set_override('time_to_live', -1, group='database')
self.conn.upgrade()
self.assertTrue(self.conn.db.meter.ensure_index('foo',
name='meter_ttl'))

View File

@ -22,10 +22,9 @@
import datetime
import testscenarios
from oslo.config import cfg
from ceilometer.publisher import rpc
from ceilometer.openstack.common import timeutils
from ceilometer.openstack.common.fixture import moxstubout
from ceilometer import sample
from ceilometer import storage
from ceilometer.storage import models
@ -53,13 +52,14 @@ class DBTestBase(tests_db.TestBase):
resource_metadata=metadata, source=source
)
msg = rpc.meter_message_from_counter(
s, cfg.CONF.publisher_rpc.metering_secret
s, self.CONF.publisher_rpc.metering_secret
)
self.conn.record_metering_data(msg)
return msg
def setUp(self):
super(DBTestBase, self).setUp()
self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs
self.prepare_data()
def tearDown(self):
@ -628,7 +628,7 @@ class RawSampleTest(DBTestBase,
def test_clear_metering_data(self):
# NOTE(jd) Override this test in MongoDB because our code doesn't clear
# the collections, this is handled by MongoDB TTL feature.
if cfg.CONF.database.connection.startswith('mongodb://'):
if self.CONF.database.connection.startswith('mongodb://'):
return
timeutils.utcnow.override_time = datetime.datetime(2012, 7, 2, 10, 45)
@ -646,7 +646,7 @@ class RawSampleTest(DBTestBase,
def test_clear_metering_data_no_data_to_remove(self):
# NOTE(jd) Override this test in MongoDB because our code doesn't clear
# the collections, this is handled by MongoDB TTL feature.
if cfg.CONF.database.connection.startswith('mongodb://'):
if self.CONF.database.connection.startswith('mongodb://'):
return
timeutils.utcnow.override_time = datetime.datetime(2010, 7, 2, 10, 45)
@ -924,7 +924,7 @@ class StatisticsGroupByTest(DBTestBase,
)
msg = rpc.meter_message_from_counter(
c,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)
@ -1788,7 +1788,7 @@ class CounterDataTypeTest(DBTestBase,
)
msg = rpc.meter_message_from_counter(
c,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)
@ -1807,7 +1807,7 @@ class CounterDataTypeTest(DBTestBase,
)
msg = rpc.meter_message_from_counter(
c,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)
@ -1825,7 +1825,7 @@ class CounterDataTypeTest(DBTestBase,
)
msg = rpc.meter_message_from_counter(
c,
cfg.CONF.publisher_rpc.metering_secret,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)