From 537c60badde7582546c19828d36247157ffe9bca Mon Sep 17 00:00:00 2001 From: Vladislav Kuzmin Date: Thu, 24 Oct 2013 15:04:10 +0400 Subject: [PATCH] Replace tests.base part8 It is the last step to replace using openstack.common.test. In this patch is changed for using openstack.common.test and added needed fixtures in test classes. Cleaning base.py and deleted TestCase class. Change-Id: If4b5992de70fb01cf29363243fb7e50196653fff --- ceilometer/tests/base.py | 43 -------------- tests/agentbase.py | 10 +++- tests/alarm/evaluator/test_base.py | 4 +- tests/api/v1/test_app.py | 38 +++++++------ tests/api/v2/test_app.py | 48 ++++++++-------- tests/storage/sqlalchemy/test_models.py | 4 +- tests/test_bin.py | 74 ++++++++++++++----------- tests/test_service.py | 41 +++++++------- 8 files changed, 122 insertions(+), 140 deletions(-) diff --git a/ceilometer/tests/base.py b/ceilometer/tests/base.py index 2d9143bf0..840889f79 100644 --- a/ceilometer/tests/base.py +++ b/ceilometer/tests/base.py @@ -18,55 +18,12 @@ # under the License. """Test base classes. """ -import fixtures import functools -from oslo.config import cfg import os.path -import testtools from testtools import testcase from ceilometer.openstack.common import test from ceilometer.openstack.common import timeutils -from ceilometer.openstack.common.fixture import config -from ceilometer.openstack.common.fixture import moxstubout - - -cfg.CONF.import_opt('pipeline_cfg_file', 'ceilometer.pipeline') - - -class TestCase(testtools.TestCase): - def setUp(self): - super(TestCase, self).setUp() - self.tempdir = self.useFixture(fixtures.TempDir()) - self.useFixture(fixtures.FakeLogger()) - self.useFixture(config.Config()) - moxfixture = self.useFixture(moxstubout.MoxStubout()) - self.mox = moxfixture.mox - self.stubs = moxfixture.stubs - - cfg.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. - cfg.CONF.set_override( - 'pipeline_cfg_file', - self.path_get('etc/ceilometer/pipeline.yaml') - ) - - def path_get(self, 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 temp_config_file_path(self, name='ceilometer.conf'): - return os.path.join(self.tempdir.path, name) class BaseTestCase(test.BaseTestCase): diff --git a/tests/agentbase.py b/tests/agentbase.py index 383b8e70d..5ef1b41fe 100644 --- a/tests/agentbase.py +++ b/tests/agentbase.py @@ -29,8 +29,9 @@ from stevedore.tests import manager as extension_tests from ceilometer import sample from ceilometer import pipeline from ceilometer import agent -from ceilometer.tests import base from ceilometer import transformer +from ceilometer.openstack.common.fixture import config +from ceilometer.tests import base default_test_data = sample.Sample( @@ -64,7 +65,7 @@ class TestPollsterException(TestPollster): raise Exception() -class BaseAgentManagerTestCase(base.TestCase): +class BaseAgentManagerTestCase(base.BaseTestCase): class Pollster(TestPollster): samples = [] @@ -162,6 +163,11 @@ class BaseAgentManagerTestCase(base.TestCase): 'publishers': ["test"], }, ] self.setup_pipeline() + self.CONF = self.useFixture(config.Config()).conf + self.CONF.set_override( + 'pipeline_cfg_file', + self.path_get('etc/ceilometer/pipeline.yaml') + ) def tearDown(self): self.Pollster.samples = [] diff --git a/tests/alarm/evaluator/test_base.py b/tests/alarm/evaluator/test_base.py index 63d48b8ec..9b739db81 100644 --- a/tests/alarm/evaluator/test_base.py +++ b/tests/alarm/evaluator/test_base.py @@ -19,11 +19,11 @@ """ import mock -from ceilometer.tests import base +from ceilometer.openstack.common import test from ceilometer.alarm import evaluator -class TestEvaluatorBaseClass(base.TestCase): +class TestEvaluatorBaseClass(test.BaseTestCase): def setUp(self): super(TestEvaluatorBaseClass, self).setUp() self.called = False diff --git a/tests/api/v1/test_app.py b/tests/api/v1/test_app.py index 89368bc83..89c02af62 100644 --- a/tests/api/v1/test_app.py +++ b/tests/api/v1/test_app.py @@ -19,36 +19,38 @@ """ import os -from oslo.config import cfg - from ceilometer.api.v1 import app from ceilometer.api import acl from ceilometer import service -from ceilometer.tests import base +from ceilometer.openstack.common import fileutils +from ceilometer.openstack.common import test +from ceilometer.openstack.common.fixture import config -class TestApp(base.TestCase): +class TestApp(test.BaseTestCase): - def tearDown(self): - super(TestApp, self).tearDown() - cfg.CONF.reset() + def setUp(self): + super(TestApp, self).setUp() + self.CONF = self.useFixture(config.Config()).conf def test_keystone_middleware_conf(self): - cfg.CONF.set_override("auth_protocol", "foottp", - group=acl.OPT_GROUP_NAME) - cfg.CONF.set_override("auth_version", "v2.0", group=acl.OPT_GROUP_NAME) - cfg.CONF.set_override("auth_uri", None, - group=acl.OPT_GROUP_NAME) - api_app = app.make_app(cfg.CONF, attach_storage=False) + self.CONF.set_override("auth_protocol", "foottp", + group=acl.OPT_GROUP_NAME) + self.CONF.set_override("auth_version", "v2.0", + group=acl.OPT_GROUP_NAME) + self.CONF.set_override("auth_uri", None, + group=acl.OPT_GROUP_NAME) + api_app = app.make_app(self.CONF, attach_storage=False) self.assertTrue(api_app.wsgi_app.auth_uri.startswith('foottp')) def test_keystone_middleware_parse_conffile(self): - tmpfile = self.temp_config_file_path() - with open(tmpfile, "w") as f: - f.write("[%s]\nauth_protocol = barttp" % acl.OPT_GROUP_NAME) - f.write("\nauth_version = v2.0") + content = "[{0}]\nauth_protocol = barttp"\ + "\nauth_version = v2.0".format(acl.OPT_GROUP_NAME) + tmpfile = fileutils.write_to_tempfile(content=content, + prefix='ceilometer', + suffix='.conf') service.prepare_service(['ceilometer-api', '--config-file=%s' % tmpfile]) - api_app = app.make_app(cfg.CONF, attach_storage=False) + api_app = app.make_app(self.CONF, attach_storage=False) self.assertTrue(api_app.wsgi_app.auth_uri.startswith('barttp')) os.unlink(tmpfile) diff --git a/tests/api/v2/test_app.py b/tests/api/v2/test_app.py index bbcb08b8f..a2c70f632 100644 --- a/tests/api/v2/test_app.py +++ b/tests/api/v2/test_app.py @@ -20,48 +20,52 @@ """ import os -from oslo.config import cfg - from ceilometer.api import app from ceilometer.api import acl from ceilometer import service +from ceilometer.openstack.common import fileutils from ceilometer.openstack.common import gettextutils +from ceilometer.openstack.common.fixture import config from ceilometer.openstack.common.fixture import moxstubout from ceilometer.tests import base from ceilometer.tests import db as tests_db from .base import FunctionalTest -class TestApp(base.TestCase): +class TestApp(base.BaseTestCase): - def tearDown(self): - super(TestApp, self).tearDown() - cfg.CONF.reset() + def setUp(self): + super(TestApp, self).setUp() + self.CONF = self.useFixture(config.Config()).conf def test_keystone_middleware_conf(self): - cfg.CONF.set_override("auth_protocol", "foottp", - group=acl.OPT_GROUP_NAME) - cfg.CONF.set_override("auth_version", "v2.0", group=acl.OPT_GROUP_NAME) - cfg.CONF.set_override("pipeline_cfg_file", - self.path_get("etc/ceilometer/pipeline.yaml")) - cfg.CONF.set_override('connection', "log://", group="database") - cfg.CONF.set_override("auth_uri", None, group=acl.OPT_GROUP_NAME) + self.CONF.set_override("auth_protocol", "foottp", + group=acl.OPT_GROUP_NAME) + self.CONF.set_override("auth_version", "v2.0", + group=acl.OPT_GROUP_NAME) + self.CONF.set_override("pipeline_cfg_file", + self.path_get("etc/ceilometer/pipeline.yaml")) + self.CONF.set_override('connection', "log://", group="database") + self.CONF.set_override("auth_uri", None, group=acl.OPT_GROUP_NAME) api_app = app.setup_app() self.assertTrue(api_app.auth_uri.startswith('foottp')) def test_keystone_middleware_parse_conffile(self): - tmpfile = self.temp_config_file_path() - with open(tmpfile, "w") as f: - f.write("[DEFAULT]\n") - f.write("pipeline_cfg_file = %s\n" % - self.path_get("etc/ceilometer/pipeline.yaml")) - f.write("[%s]\n" % acl.OPT_GROUP_NAME) - f.write("auth_protocol = barttp\n") - f.write("auth_version = v2.0\n") + pipeline_conf = self.path_get("etc/ceilometer/pipeline.yaml") + content = "[DEFAULT]\n"\ + "pipeline_cfg_file = {0}\n"\ + "[{1}]\n"\ + "auth_protocol = barttp\n"\ + "auth_version = v2.0\n".format(pipeline_conf, + acl.OPT_GROUP_NAME) + + tmpfile = fileutils.write_to_tempfile(content=content, + prefix='ceilometer', + suffix='.conf') service.prepare_service(['ceilometer-api', '--config-file=%s' % tmpfile]) - cfg.CONF.set_override('connection', "log://", group="database") + self.CONF.set_override('connection', "log://", group="database") api_app = app.setup_app() self.assertTrue(api_app.auth_uri.startswith('barttp')) os.unlink(tmpfile) diff --git a/tests/storage/sqlalchemy/test_models.py b/tests/storage/sqlalchemy/test_models.py index 107e811f6..c13614edb 100644 --- a/tests/storage/sqlalchemy/test_models.py +++ b/tests/storage/sqlalchemy/test_models.py @@ -23,11 +23,11 @@ from sqlalchemy.types import DATETIME, NUMERIC from sqlalchemy.dialects.mysql import DECIMAL from ceilometer import utils +from ceilometer.openstack.common import test from ceilometer.storage.sqlalchemy import models -from ceilometer.tests import base -class PreciseTimestampTest(base.TestCase): +class PreciseTimestampTest(test.BaseTestCase): @staticmethod def fake_dialect(name): diff --git a/tests/test_bin.py b/tests/test_bin.py index a358b5794..1e4074249 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -19,21 +19,28 @@ import httplib2 import json +import os import random import socket import subprocess import time from ceilometer.tests import base +from ceilometer.openstack.common import fileutils -class BinTestCase(base.TestCase): +class BinTestCase(base.BaseTestCase): def setUp(self): super(BinTestCase, self).setUp() - self.tempfile = self.temp_config_file_path() - with open(self.tempfile, 'w') as tmp: - tmp.write("[database]\n") - tmp.write("connection=log://localhost\n") + content = "[database]\n"\ + "connection=log://localhost\n" + self.tempfile = fileutils.write_to_tempfile(content=content, + prefix='ceilometer', + suffix='.conf') + + def tearDown(self): + super(BinTestCase, self).tearDown() + os.remove(self.tempfile) def test_dbsync_run(self): subp = subprocess.Popen(['ceilometer-dbsync', @@ -46,17 +53,21 @@ class BinTestCase(base.TestCase): self.assertEqual(subp.wait(), 0) -class BinSendCounterTestCase(base.TestCase): +class BinSendCounterTestCase(base.BaseTestCase): def setUp(self): super(BinSendCounterTestCase, self).setUp() - self.tempfile = self.temp_config_file_path() pipeline_cfg_file = self.path_get('etc/ceilometer/pipeline.yaml') - with open(self.tempfile, 'w') as tmp: - tmp.write("[DEFAULT]\n") - tmp.write( - "rpc_backend=ceilometer.openstack.common.rpc.impl_fake\n") - tmp.write( - "pipeline_cfg_file=%s\n" % pipeline_cfg_file) + content = "[DEFAULT]\n"\ + "rpc_backend=ceilometer.openstack.common.rpc.impl_fake\n"\ + "pipeline_cfg_file={0}\n".format(pipeline_cfg_file) + + self.tempfile = fileutils.write_to_tempfile(content=content, + prefix='ceilometer', + suffix='.conf') + + def tearDown(self): + super(BinSendCounterTestCase, self).tearDown() + os.remove(self.tempfile) def test_send_counter_run(self): subp = subprocess.Popen([self.path_get('bin/ceilometer-send-counter'), @@ -66,32 +77,30 @@ class BinSendCounterTestCase(base.TestCase): self.assertEqual(subp.wait(), 0) -class BinApiTestCase(base.TestCase): +class BinApiTestCase(base.BaseTestCase): def setUp(self): super(BinApiTestCase, self).setUp() self.api_port = random.randint(10000, 11000) self.http = httplib2.Http() - self.tempfile = self.temp_config_file_path() pipeline_cfg_file = self.path_get('etc/ceilometer/pipeline.yaml') policy_file = self.path_get('tests/policy.json') - with open(self.tempfile, 'w') as tmp: - tmp.write("[DEFAULT]\n") - tmp.write( - "rpc_backend=ceilometer.openstack.common.rpc.impl_fake\n") - tmp.write( - "auth_strategy=noauth\n") - tmp.write( - "debug=true\n") - tmp.write( - "pipeline_cfg_file=%s\n" % pipeline_cfg_file) - tmp.write( - "policy_file=%s\n" % policy_file) - tmp.write("[api]\n") - tmp.write( - "port=%s\n" % self.api_port) - tmp.write("[database]\n") - tmp.write("connection=log://localhost\n") + content = "[DEFAULT]\n"\ + "rpc_backend=ceilometer.openstack.common.rpc.impl_fake\n"\ + "auth_strategy=noauth\n"\ + "debug=true\n"\ + "pipeline_cfg_file={0}\n"\ + "policy_file={1}\n"\ + "[api]\n"\ + "port={2}\n"\ + "[database]\n"\ + "connection=log://localhost\n".format(pipeline_cfg_file, + policy_file, + self.api_port) + + self.tempfile = fileutils.write_to_tempfile(content=content, + prefix='ceilometer', + suffix='.conf') self.subp = subprocess.Popen(['ceilometer-api', "--config-file=%s" % self.tempfile]) @@ -99,6 +108,7 @@ class BinApiTestCase(base.TestCase): super(BinApiTestCase, self).tearDown() self.subp.kill() self.subp.wait() + os.remove(self.tempfile) def get_response(self, path): url = 'http://%s:%d/%s' % ('127.0.0.1', self.api_port, path) diff --git a/tests/test_service.py b/tests/test_service.py index 5245c3d5b..1f662644a 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -26,9 +26,11 @@ import time import threading from ceilometer import service from ceilometer.tests import base +from ceilometer.openstack.common import fileutils +from ceilometer.openstack.common import test -class ServiceTestCase(base.TestCase): +class ServiceTestCase(test.BaseTestCase): def test_prepare_service(self): service.prepare_service([]) @@ -60,31 +62,30 @@ class ParseOutput(threading.Thread): self.thread_stop = True -class ServiceRestartTest(base.TestCase): +class ServiceRestartTest(base.BaseTestCase): def setUp(self): super(ServiceRestartTest, self).setUp() - self.tempfile = self.temp_config_file_path() - self.pipeline_cfg_file = self.temp_config_file_path(name= - 'pipeline.yaml') + self.pipeline_cfg_file = fileutils.write_to_tempfile(content='', + prefix='pipeline', + suffix='.yaml') shutil.copy(self.path_get('etc/ceilometer/pipeline.yaml'), self.pipeline_cfg_file) self.pipelinecfg_read_from_file() policy_file = self.path_get('tests/policy.json') - with open(self.tempfile, 'w') as tmp: - tmp.write("[DEFAULT]\n") - tmp.write( - "rpc_backend=ceilometer.openstack.common.rpc.impl_fake\n") - tmp.write( - "auth_strategy=noauth\n") - tmp.write( - "debug=true\n") - tmp.write( - "pipeline_cfg_file=%s\n" % self.pipeline_cfg_file) - tmp.write( - "policy_file=%s\n" % policy_file) - tmp.write("[database]\n") - tmp.write("connection=log://localhost\n") + content = "[DEFAULT]\n"\ + "rpc_backend=ceilometer.openstack.common.rpc.impl_fake\n"\ + "auth_strategy=noauth\n"\ + "debug=true\n"\ + "pipeline_cfg_file={0}\n"\ + "policy_file={1}\n"\ + "[database]\n"\ + "connection=log://localhost\n".format(self.pipeline_cfg_file, + policy_file) + + self.tempfile = fileutils.write_to_tempfile(content=content, + prefix='ceilometer', + suffix='.conf') def _modify_pipeline_file(self): with open(self.pipeline_cfg_file, 'w') as pipe_fd: @@ -100,6 +101,8 @@ class ServiceRestartTest(base.TestCase): super(ServiceRestartTest, self).tearDown() self.sub.kill() self.sub.wait() + os.remove(self.pipeline_cfg_file) + os.remove(self.tempfile) @staticmethod def _check_process_alive(pid):