From ab5bbdd9577260602b998c85ac40a9dd7d9f9942 Mon Sep 17 00:00:00 2001 From: Vladislav Kuzmin Date: Fri, 25 Oct 2013 15:38:44 +0400 Subject: [PATCH] Replace tests.base part4 It is the fourth 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. Change-Id: I584db92369ecb8c787b1c669cdeb2ac7fe9ddfb3 --- tests/objectstore/test_swift.py | 6 +++-- tests/objectstore/test_swift_middleware.py | 11 +++++---- tests/publisher/test_file.py | 18 ++++++++++----- tests/publisher/test_rpc_publisher.py | 27 ++++++++++++---------- tests/publisher/test_udp.py | 14 +++++++---- tests/storage/test_base.py | 4 ++-- tests/storage/test_get_engine.py | 4 ++-- tests/storage/test_impl_log.py | 8 +++++-- tests/storage/test_models.py | 4 ++-- 9 files changed, 60 insertions(+), 36 deletions(-) diff --git a/tests/objectstore/test_swift.py b/tests/objectstore/test_swift.py index e89e260dc..1f6f2c924 100644 --- a/tests/objectstore/test_swift.py +++ b/tests/objectstore/test_swift.py @@ -24,7 +24,8 @@ import testscenarios from ceilometer.central import manager from ceilometer.objectstore import swift -from ceilometer.tests import base +from ceilometer.openstack.common import test +from ceilometer.openstack.common.fixture import moxstubout from keystoneclient import exceptions from swiftclient import client as swift_client @@ -48,7 +49,7 @@ class TestManager(manager.AgentManager): self.keystone = mock.MagicMock() -class TestSwiftPollster(base.TestCase): +class TestSwiftPollster(test.BaseTestCase): # Define scenarios to run all of the tests against all of the # pollsters. @@ -72,6 +73,7 @@ class TestSwiftPollster(base.TestCase): @mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock()) def setUp(self): super(TestSwiftPollster, self).setUp() + self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs self.pollster = self.factory() self.manager = TestManager() diff --git a/tests/objectstore/test_swift_middleware.py b/tests/objectstore/test_swift_middleware.py index ffce02e2d..ce25c44dd 100644 --- a/tests/objectstore/test_swift_middleware.py +++ b/tests/objectstore/test_swift_middleware.py @@ -20,11 +20,12 @@ import cStringIO as StringIO import mock -from oslo.config import cfg from webob import Request -from ceilometer.tests import base from ceilometer.objectstore import swift_middleware +from ceilometer.openstack.common import test +from ceilometer.openstack.common.fixture import config +from ceilometer.openstack.common.fixture import moxstubout from ceilometer import pipeline @@ -42,7 +43,7 @@ class FakeApp(object): return self.body -class TestSwiftMiddleware(base.TestCase): +class TestSwiftMiddleware(test.BaseTestCase): class _faux_pipeline_manager(pipeline.PipelineManager): class _faux_pipeline(object): @@ -64,8 +65,10 @@ class TestSwiftMiddleware(base.TestCase): def setUp(self): super(TestSwiftMiddleware, self).setUp() + self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs self.pipeline_manager = self._faux_pipeline_manager() self.stubs.Set(pipeline, 'setup_pipeline', self._faux_setup_pipeline) + self.CONF = self.useFixture(config.Config()).conf @staticmethod def start_response(*args): @@ -73,7 +76,7 @@ class TestSwiftMiddleware(base.TestCase): def test_rpc_setup(self): swift_middleware.CeilometerMiddleware(FakeApp(), {}) - self.assertEqual(cfg.CONF.control_exchange, 'ceilometer') + self.assertEqual(self.CONF.control_exchange, 'ceilometer') def test_get(self): app = swift_middleware.CeilometerMiddleware(FakeApp(), {}) diff --git a/tests/publisher/test_file.py b/tests/publisher/test_file.py index 91879cfd3..99a86069b 100644 --- a/tests/publisher/test_file.py +++ b/tests/publisher/test_file.py @@ -22,13 +22,15 @@ import datetime import os import logging import logging.handlers +import tempfile + from ceilometer import sample from ceilometer.publisher import file -from ceilometer.tests import base +from ceilometer.openstack.common import test from ceilometer.openstack.common.network_utils import urlsplit -class TestFilePublisher(base.TestCase): +class TestFilePublisher(test.BaseTestCase): test_data = [ sample.Sample( @@ -68,8 +70,10 @@ class TestFilePublisher(base.TestCase): def test_file_publisher_maxbytes(self): # Test valid configurations - name = '%s/log_file' % self.tempdir.path - parsed_url = urlsplit('file://%s?max_bytes=50&backup_count=3' % name) + tempdir = tempfile.mkdtemp() + name = '%s/log_file' % tempdir + parsed_url = urlsplit('file://%s?max_bytes=50&backup_count=3' + % name) publisher = file.FilePublisher(parsed_url) publisher.publish_samples(None, self.test_data) @@ -85,7 +89,8 @@ class TestFilePublisher(base.TestCase): def test_file_publisher(self): # Test missing max bytes, backup count configurations - name = '%s/log_file_plain' % self.tempdir.path + tempdir = tempfile.mkdtemp() + name = '%s/log_file_plain' % tempdir parsed_url = urlsplit('file://%s' % name) publisher = file.FilePublisher(parsed_url) publisher.publish_samples(None, @@ -107,9 +112,10 @@ class TestFilePublisher(base.TestCase): def test_file_publisher_invalid(self): # Test invalid max bytes, backup count configurations + tempdir = tempfile.mkdtemp() parsed_url = urlsplit( 'file://%s/log_file_bad' - '?max_bytes=yus&backup_count=5y' % self.tempdir.path) + '?max_bytes=yus&backup_count=5y' % tempdir) publisher = file.FilePublisher(parsed_url) publisher.publish_samples(None, self.test_data) diff --git a/tests/publisher/test_rpc_publisher.py b/tests/publisher/test_rpc_publisher.py index 201b44686..853eb525c 100644 --- a/tests/publisher/test_rpc_publisher.py +++ b/tests/publisher/test_rpc_publisher.py @@ -21,17 +21,18 @@ import eventlet import datetime -from oslo.config import cfg from ceilometer import sample from ceilometer.openstack.common import jsonutils from ceilometer.openstack.common import network_utils from ceilometer.openstack.common import rpc as oslo_rpc +from ceilometer.openstack.common import test +from ceilometer.openstack.common.fixture import config +from ceilometer.openstack.common.fixture import moxstubout from ceilometer.publisher import rpc -from ceilometer.tests import base -class TestSignature(base.TestCase): +class TestSignature(test.BaseTestCase): def test_compute_signature_change_key(self): sig1 = rpc.compute_signature({'a': 'A', 'b': 'B'}, @@ -110,7 +111,7 @@ class TestSignature(base.TestCase): self.assertTrue(rpc.verify_signature(jsondata, 'not-so-secret')) -class TestCounter(base.TestCase): +class TestCounter(test.BaseTestCase): TEST_COUNTER = sample.Sample(name='name', type='typ', @@ -142,7 +143,7 @@ class TestCounter(base.TestCase): yield compare, f, getattr(self.TEST_COUNTER, f), msg_f, msg[msg_f] -class TestPublish(base.TestCase): +class TestPublish(test.BaseTestCase): test_data = [ sample.Sample( @@ -214,8 +215,10 @@ class TestPublish(base.TestCase): def setUp(self): super(TestPublish, self).setUp() + self.CONF = self.useFixture(config.Config()).conf self.published = [] self.rpc_unreachable = False + self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs self.stubs.Set(oslo_rpc, 'cast', self.faux_cast) def test_published(self): @@ -225,7 +228,7 @@ class TestPublish(base.TestCase): self.test_data) self.assertEqual(len(self.published), 1) self.assertEqual(self.published[0][0], - cfg.CONF.publisher_rpc.metering_topic) + self.CONF.publisher_rpc.metering_topic) self.assertIsInstance(self.published[0][1]['args']['data'], list) self.assertEqual(self.published[0][1]['method'], 'record_metering_data') @@ -237,7 +240,7 @@ class TestPublish(base.TestCase): self.test_data) self.assertEqual(len(self.published), 1) self.assertEqual(self.published[0][0], - cfg.CONF.publisher_rpc.metering_topic) + self.CONF.publisher_rpc.metering_topic) self.assertIsInstance(self.published[0][1]['args']['data'], list) self.assertEqual(self.published[0][1]['method'], 'custom_procedure_call') @@ -251,20 +254,20 @@ class TestPublish(base.TestCase): for topic, rpc_call in self.published: meters = rpc_call['args']['data'] self.assertIsInstance(meters, list) - if topic != cfg.CONF.publisher_rpc.metering_topic: + if topic != self.CONF.publisher_rpc.metering_topic: self.assertEqual(len(set(meter['counter_name'] for meter in meters)), 1, "Meter are published grouped by name") topics = [topic for topic, meter in self.published] - self.assertIn(cfg.CONF.publisher_rpc.metering_topic, topics) + self.assertIn(self.CONF.publisher_rpc.metering_topic, topics) self.assertIn( - cfg.CONF.publisher_rpc.metering_topic + '.' + 'test', topics) + self.CONF.publisher_rpc.metering_topic + '.' + 'test', topics) self.assertIn( - cfg.CONF.publisher_rpc.metering_topic + '.' + 'test2', topics) + self.CONF.publisher_rpc.metering_topic + '.' + 'test2', topics) self.assertIn( - cfg.CONF.publisher_rpc.metering_topic + '.' + 'test3', topics) + self.CONF.publisher_rpc.metering_topic + '.' + 'test3', topics) def test_published_concurrency(self): """This test the concurrent access to the local queue diff --git a/tests/publisher/test_udp.py b/tests/publisher/test_udp.py index 7ee83958d..55d50e4e7 100644 --- a/tests/publisher/test_udp.py +++ b/tests/publisher/test_udp.py @@ -21,18 +21,19 @@ import datetime import mock import msgpack -from oslo.config import cfg from ceilometer import sample from ceilometer.publisher import udp -from ceilometer.tests import base +from ceilometer.openstack.common import test from ceilometer.openstack.common import network_utils +from ceilometer.openstack.common.fixture import config +from ceilometer.openstack.common.fixture import moxstubout COUNTER_SOURCE = 'testsource' -class TestUDPPublisher(base.TestCase): +class TestUDPPublisher(test.BaseTestCase): test_data = [ sample.Sample( @@ -107,6 +108,11 @@ class TestUDPPublisher(base.TestCase): return udp_socket return _fake_socket_socket + def setUp(self): + super(TestUDPPublisher, self).setUp() + self.mox = self.useFixture(moxstubout.MoxStubout()).mox + self.CONF = self.useFixture(config.Config()).conf + def test_published(self): self.data_sent = [] with mock.patch('socket.socket', @@ -126,7 +132,7 @@ class TestUDPPublisher(base.TestCase): # Check destination self.assertEqual(dest, ('somehost', - cfg.CONF.collector.udp_port)) + self.CONF.collector.udp_port)) # Check that counters are equal self.assertEqual(sorted(sent_counters), diff --git a/tests/storage/test_base.py b/tests/storage/test_base.py index 727b3039b..3ea8fb3fa 100644 --- a/tests/storage/test_base.py +++ b/tests/storage/test_base.py @@ -18,11 +18,11 @@ import datetime import math +from ceilometer.openstack.common import test from ceilometer.storage import base -from ceilometer.tests import base as test_base -class BaseTest(test_base.TestCase): +class BaseTest(test.BaseTestCase): def test_iter_period(self): times = list(base.iter_period( diff --git a/tests/storage/test_get_engine.py b/tests/storage/test_get_engine.py index 498b6f4de..515158875 100644 --- a/tests/storage/test_get_engine.py +++ b/tests/storage/test_get_engine.py @@ -19,13 +19,13 @@ """ import mox -import testtools from ceilometer import storage +from ceilometer.openstack.common import test from ceilometer.storage import impl_log -class EngineTest(testtools.TestCase): +class EngineTest(test.BaseTestCase): def test_get_engine(self): conf = mox.Mox().CreateMockAnything() diff --git a/tests/storage/test_impl_log.py b/tests/storage/test_impl_log.py index 41608366c..fa3a72274 100644 --- a/tests/storage/test_impl_log.py +++ b/tests/storage/test_impl_log.py @@ -17,11 +17,15 @@ # under the License. """Tests for ceilometer/storage/impl_log.py """ -from ceilometer.tests import base +from ceilometer.openstack.common import test +from ceilometer.openstack.common.fixture import moxstubout from ceilometer.storage import impl_log -class ConnectionTest(base.TestCase): +class ConnectionTest(test.BaseTestCase): + def setUp(self): + super(ConnectionTest, self).setUp() + self.mox = self.useFixture(moxstubout.MoxStubout()).mox def test_get_connection(self): conf = self.mox.CreateMockAnything() diff --git a/tests/storage/test_models.py b/tests/storage/test_models.py index 4720b30c7..54f3b213f 100644 --- a/tests/storage/test_models.py +++ b/tests/storage/test_models.py @@ -16,8 +16,8 @@ # License for the specific language governing permissions and limitations # under the License. +from ceilometer.openstack.common import test from ceilometer.storage import models -from ceilometer.tests import base class FakeModel(models.Model): @@ -25,7 +25,7 @@ class FakeModel(models.Model): models.Model.__init__(self, arg1=arg1, arg2=arg2) -class ModelTest(base.TestCase): +class ModelTest(test.BaseTestCase): def test_create_attributes(self): m = FakeModel(1, 2)