From 6ae520677da80103bae79e3db3569360df1220cd Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Fri, 24 May 2013 01:36:21 +0200 Subject: [PATCH] Replace custom skip_ methods. testtools has these built in - so we should use them. Change-Id: I2a9f6dbd69d5acef9a0dfbe5e90e9dbf706d8677 --- cinder/test.py | 58 ------------------- .../tests/scheduler/test_capacity_weigher.py | 14 +++-- .../tests/scheduler/test_filter_scheduler.py | 26 +++++---- cinder/tests/scheduler/test_host_filters.py | 29 +++++----- cinder/tests/test_migrations.py | 7 ++- cinder/tests/test_skip_examples.py | 47 --------------- cinder/tests/test_wsgi.py | 9 +-- 7 files changed, 46 insertions(+), 144 deletions(-) delete mode 100644 cinder/tests/test_skip_examples.py diff --git a/cinder/test.py b/cinder/test.py index a86c5a28e..4db727c9c 100644 --- a/cinder/test.py +++ b/cinder/test.py @@ -54,64 +54,6 @@ FLAGS.register_opts(test_opts) LOG = logging.getLogger(__name__) -class skip_test(object): - """Decorator that skips a test.""" - # TODO(tr3buchet): remember forever what comstud did here - def __init__(self, msg): - self.message = msg - - def __call__(self, func): - @functools.wraps(func) - def _skipper(*args, **kw): - """Wrapped skipper function.""" - raise testtools.TestCase.skipException(self.message) - return _skipper - - -class skip_if(object): - """Decorator that skips a test if condition is true.""" - def __init__(self, condition, msg): - self.condition = condition - self.message = msg - - def __call__(self, func): - @functools.wraps(func) - def _skipper(*args, **kw): - """Wrapped skipper function.""" - if self.condition: - raise testtools.TestCase.skipException(self.message) - func(*args, **kw) - return _skipper - - -class skip_unless(object): - """Decorator that skips a test if condition is not true.""" - def __init__(self, condition, msg): - self.condition = condition - self.message = msg - - def __call__(self, func): - @functools.wraps(func) - def _skipper(*args, **kw): - """Wrapped skipper function.""" - if not self.condition: - raise testtools.TestCase.skipException(self.message) - func(*args, **kw) - return _skipper - - -def skip_if_fake(func): - """Decorator that skips a test if running in fake mode.""" - def _skipper(*args, **kw): - """Wrapped skipper function.""" - if FLAGS.fake_tests: - raise testtools.TestCase.skipException( - 'Test cannot be run in fake mode') - else: - return func(*args, **kw) - return _skipper - - class TestingException(Exception): pass diff --git a/cinder/tests/scheduler/test_capacity_weigher.py b/cinder/tests/scheduler/test_capacity_weigher.py index a569ff255..4232bbd03 100644 --- a/cinder/tests/scheduler/test_capacity_weigher.py +++ b/cinder/tests/scheduler/test_capacity_weigher.py @@ -16,6 +16,8 @@ Tests For Capacity Weigher. """ +import testtools + from cinder import context from cinder.openstack.common.scheduler.weights import HostWeightHandler from cinder import test @@ -46,8 +48,8 @@ class CapacityWeigherTestCase(test.TestCase): self.mox.ResetAll() return host_states - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed') def test_default_of_spreading_first(self): hostinfo_list = self._get_all_hosts() @@ -61,8 +63,8 @@ class CapacityWeigherTestCase(test.TestCase): self.assertEqual(weighed_host.weight, 921.0) self.assertEqual(weighed_host.obj.host, 'host1') - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed') def test_capacity_weight_multiplier1(self): self.flags(capacity_weight_multiplier=-1.0) hostinfo_list = self._get_all_hosts() @@ -77,8 +79,8 @@ class CapacityWeigherTestCase(test.TestCase): self.assertEqual(weighed_host.weight, -190.0) self.assertEqual(weighed_host.obj.host, 'host4') - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed') def test_capacity_weight_multiplier2(self): self.flags(capacity_weight_multiplier=2.0) hostinfo_list = self._get_all_hosts() diff --git a/cinder/tests/scheduler/test_filter_scheduler.py b/cinder/tests/scheduler/test_filter_scheduler.py index f1e2b5bc9..167014aa8 100644 --- a/cinder/tests/scheduler/test_filter_scheduler.py +++ b/cinder/tests/scheduler/test_filter_scheduler.py @@ -16,6 +16,8 @@ Tests For Filter Scheduler. """ +import testtools + from cinder import context from cinder import exception from cinder import test @@ -37,8 +39,8 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): driver_cls = filter_scheduler.FilterScheduler - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed (try setup.py develop') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed (try setup.py develop') def test_create_volume_no_hosts(self): """ Ensure empty hosts & child_zones result in NoValidHosts exception. @@ -56,8 +58,8 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): self.assertRaises(exception.NoValidHost, sched.schedule_create_volume, fake_context, request_spec, {}) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed (try setup.py develop') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed (try setup.py develop') def test_create_volume_non_admin(self): """Test creating an instance locally using run_instance, passing a non-admin context. DB actions should work.""" @@ -82,8 +84,8 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): fake_context, request_spec, {}) self.assertTrue(self.was_admin) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed (try setup.py develop') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed (try setup.py develop') def test_schedule_happy_day(self): """Make sure there's nothing glaringly wrong with _schedule() by doing a happy day pass through.""" @@ -125,8 +127,8 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): self.assertRaises(exception.InvalidParameterValue, fakes.FakeFilterScheduler) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed (try setup.py develop') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed (try setup.py develop') def test_retry_disabled(self): # Retry info should not get populated when re-scheduling is off. self.flags(scheduler_max_attempts=1) @@ -143,8 +145,8 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): # should not have retry info in the populated filter properties: self.assertFalse("retry" in filter_properties) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed (try setup.py develop') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed (try setup.py develop') def test_retry_attempt_one(self): # Test retry logic on initial scheduling attempt. self.flags(scheduler_max_attempts=2) @@ -161,8 +163,8 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase): num_attempts = filter_properties['retry']['num_attempts'] self.assertEqual(1, num_attempts) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed (try setup.py develop') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed (try setup.py develop') def test_retry_attempt_two(self): # Test retry logic when re-scheduling. self.flags(scheduler_max_attempts=2) diff --git a/cinder/tests/scheduler/test_host_filters.py b/cinder/tests/scheduler/test_host_filters.py index 810056fd1..065752beb 100644 --- a/cinder/tests/scheduler/test_host_filters.py +++ b/cinder/tests/scheduler/test_host_filters.py @@ -17,6 +17,7 @@ Tests For Scheduler Host Filters. import httplib import stubout +import testtools from cinder import context from cinder import db @@ -76,8 +77,8 @@ class HostFiltersTestCase(test.TestCase): return ret_value self.stubs.Set(utils, 'service_is_up', fake_service_is_up) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed') def test_capacity_filter_passes(self): self._stub_service_is_up(True) filt_cls = self.class_map['CapacityFilter']() @@ -89,8 +90,8 @@ class HostFiltersTestCase(test.TestCase): 'service': service}) self.assertTrue(filt_cls.host_passes(host, filter_properties)) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed') def test_capacity_filter_fails(self): self._stub_service_is_up(True) filt_cls = self.class_map['CapacityFilter']() @@ -103,8 +104,8 @@ class HostFiltersTestCase(test.TestCase): 'service': service}) self.assertFalse(filt_cls.host_passes(host, filter_properties)) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed') def test_capacity_filter_passes_infinite(self): self._stub_service_is_up(True) filt_cls = self.class_map['CapacityFilter']() @@ -116,8 +117,8 @@ class HostFiltersTestCase(test.TestCase): 'service': service}) self.assertTrue(filt_cls.host_passes(host, filter_properties)) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed') def test_capacity_filter_passes_unknown(self): self._stub_service_is_up(True) filt_cls = self.class_map['CapacityFilter']() @@ -129,8 +130,8 @@ class HostFiltersTestCase(test.TestCase): 'service': service}) self.assertTrue(filt_cls.host_passes(host, filter_properties)) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed') def test_retry_filter_disabled(self): # Test case where retry/re-scheduling is disabled. filt_cls = self.class_map['RetryFilter']() @@ -138,8 +139,8 @@ class HostFiltersTestCase(test.TestCase): filter_properties = {} self.assertTrue(filt_cls.host_passes(host, filter_properties)) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed') def test_retry_filter_pass(self): # Node not previously tried. filt_cls = self.class_map['RetryFilter']() @@ -148,8 +149,8 @@ class HostFiltersTestCase(test.TestCase): filter_properties = dict(retry=retry) self.assertTrue(filt_cls.host_passes(host, filter_properties)) - @test.skip_if(not test_utils.is_cinder_installed(), - 'Test requires Cinder installed') + @testtools.skipIf(not test_utils.is_cinder_installed(), + 'Test requires Cinder installed') def test_retry_filter_fail(self): # Node was already tried. filt_cls = self.class_map['RetryFilter']() diff --git a/cinder/tests/test_migrations.py b/cinder/tests/test_migrations.py index 47b1050c5..c071fb2b0 100644 --- a/cinder/tests/test_migrations.py +++ b/cinder/tests/test_migrations.py @@ -32,6 +32,7 @@ import uuid from migrate.versioning import repository import sqlalchemy +import testtools import cinder.db.migration as migration import cinder.db.sqlalchemy.migrate_repo @@ -235,7 +236,7 @@ class TestMigrations(test.TestCase): if _is_mysql_avail(user="openstack_cifail"): self.fail("Shouldn't have connected") - @test.skip_unless(_have_mysql(), "mysql not available") + @testtools.skipUnless(_have_mysql(), "mysql not available") def test_mysql_innodb(self): """ Test that table creation on mysql only builds InnoDB tables @@ -276,8 +277,8 @@ class TestMigrations(test.TestCase): if _is_backend_avail('postgres', user="openstack_cifail"): self.fail("Shouldn't have connected") - @test.skip_unless(_is_backend_avail('postgres'), - "postgresql not available") + @testtools.skipUnless(_is_backend_avail('postgres'), + "postgresql not available") def test_postgresql_opportunistically(self): # add this to the global lists to make reset work with it, it's removed # automatically in tearDown so no need to clean it up here. diff --git a/cinder/tests/test_skip_examples.py b/cinder/tests/test_skip_examples.py deleted file mode 100644 index 2e51aef34..000000000 --- a/cinder/tests/test_skip_examples.py +++ /dev/null @@ -1,47 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. -# All Rights Reserved. -# -# 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. - -from cinder import test - - -class ExampleSkipTestCase(test.TestCase): - test_counter = 0 - - @test.skip_test("Example usage of @test.skip_test()") - def test_skip_test_example(self): - self.fail("skip_test failed to work properly.") - - @test.skip_if(True, "Example usage of @test.skip_if()") - def test_skip_if_example(self): - self.fail("skip_if failed to work properly.") - - @test.skip_unless(False, "Example usage of @test.skip_unless()") - def test_skip_unless_example(self): - self.fail("skip_unless failed to work properly.") - - @test.skip_if(False, "This test case should never be skipped.") - def test_001_increase_test_counter(self): - ExampleSkipTestCase.test_counter += 1 - - @test.skip_unless(True, "This test case should never be skipped.") - def test_002_increase_test_counter(self): - ExampleSkipTestCase.test_counter += 1 - - def test_003_verify_test_counter(self): - self.assertEquals(ExampleSkipTestCase.test_counter, 2, - "Tests were not skipped appropriately") diff --git a/cinder/tests/test_wsgi.py b/cinder/tests/test_wsgi.py index 43785ff94..37cf85e57 100644 --- a/cinder/tests/test_wsgi.py +++ b/cinder/tests/test_wsgi.py @@ -24,6 +24,7 @@ import tempfile import urllib2 from oslo.config import cfg +import testtools import webob import webob.dec @@ -110,8 +111,8 @@ class TestWSGIServer(test.TestCase): server.stop() server.wait() - @test.skip_if(not _ipv6_configured(), - "Test requires an IPV6 configured interface") + @testtools.skipIf(not _ipv6_configured(), + "Test requires an IPV6 configured interface") def test_start_random_port_with_ipv6(self): server = cinder.wsgi.Server("test_random_port", None, @@ -161,8 +162,8 @@ class TestWSGIServer(test.TestCase): server.stop() - @test.skip_if(not _ipv6_configured(), - "Test requires an IPV6 configured interface") + @testtools.skipIf(not _ipv6_configured(), + "Test requires an IPV6 configured interface") def test_app_using_ipv6_and_ssl(self): CONF.set_default("ssl_cert_file", os.path.join(TEST_VAR_DIR, 'certificate.crt')) -- 2.45.2