Replace custom skip_ methods.

testtools has these built in - so we should use them.

Change-Id: I2a9f6dbd69d5acef9a0dfbe5e90e9dbf706d8677
This commit is contained in:
Monty Taylor 2013-05-24 01:36:21 +02:00 committed by John Griffith
parent 11b184f01d
commit 6ae520677d
7 changed files with 46 additions and 144 deletions

View File

@ -54,64 +54,6 @@ FLAGS.register_opts(test_opts)
LOG = logging.getLogger(__name__) 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): class TestingException(Exception):
pass pass

View File

@ -16,6 +16,8 @@
Tests For Capacity Weigher. Tests For Capacity Weigher.
""" """
import testtools
from cinder import context from cinder import context
from cinder.openstack.common.scheduler.weights import HostWeightHandler from cinder.openstack.common.scheduler.weights import HostWeightHandler
from cinder import test from cinder import test
@ -46,7 +48,7 @@ class CapacityWeigherTestCase(test.TestCase):
self.mox.ResetAll() self.mox.ResetAll()
return host_states return host_states
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed') 'Test requires Cinder installed')
def test_default_of_spreading_first(self): def test_default_of_spreading_first(self):
hostinfo_list = self._get_all_hosts() hostinfo_list = self._get_all_hosts()
@ -61,7 +63,7 @@ class CapacityWeigherTestCase(test.TestCase):
self.assertEqual(weighed_host.weight, 921.0) self.assertEqual(weighed_host.weight, 921.0)
self.assertEqual(weighed_host.obj.host, 'host1') self.assertEqual(weighed_host.obj.host, 'host1')
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed') 'Test requires Cinder installed')
def test_capacity_weight_multiplier1(self): def test_capacity_weight_multiplier1(self):
self.flags(capacity_weight_multiplier=-1.0) self.flags(capacity_weight_multiplier=-1.0)
@ -77,7 +79,7 @@ class CapacityWeigherTestCase(test.TestCase):
self.assertEqual(weighed_host.weight, -190.0) self.assertEqual(weighed_host.weight, -190.0)
self.assertEqual(weighed_host.obj.host, 'host4') self.assertEqual(weighed_host.obj.host, 'host4')
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed') 'Test requires Cinder installed')
def test_capacity_weight_multiplier2(self): def test_capacity_weight_multiplier2(self):
self.flags(capacity_weight_multiplier=2.0) self.flags(capacity_weight_multiplier=2.0)

View File

@ -16,6 +16,8 @@
Tests For Filter Scheduler. Tests For Filter Scheduler.
""" """
import testtools
from cinder import context from cinder import context
from cinder import exception from cinder import exception
from cinder import test from cinder import test
@ -37,7 +39,7 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
driver_cls = filter_scheduler.FilterScheduler driver_cls = filter_scheduler.FilterScheduler
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed (try setup.py develop') 'Test requires Cinder installed (try setup.py develop')
def test_create_volume_no_hosts(self): def test_create_volume_no_hosts(self):
""" """
@ -56,7 +58,7 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
self.assertRaises(exception.NoValidHost, sched.schedule_create_volume, self.assertRaises(exception.NoValidHost, sched.schedule_create_volume,
fake_context, request_spec, {}) fake_context, request_spec, {})
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed (try setup.py develop') 'Test requires Cinder installed (try setup.py develop')
def test_create_volume_non_admin(self): def test_create_volume_non_admin(self):
"""Test creating an instance locally using run_instance, passing """Test creating an instance locally using run_instance, passing
@ -82,7 +84,7 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
fake_context, request_spec, {}) fake_context, request_spec, {})
self.assertTrue(self.was_admin) self.assertTrue(self.was_admin)
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed (try setup.py develop') 'Test requires Cinder installed (try setup.py develop')
def test_schedule_happy_day(self): def test_schedule_happy_day(self):
"""Make sure there's nothing glaringly wrong with _schedule() """Make sure there's nothing glaringly wrong with _schedule()
@ -125,7 +127,7 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
self.assertRaises(exception.InvalidParameterValue, self.assertRaises(exception.InvalidParameterValue,
fakes.FakeFilterScheduler) fakes.FakeFilterScheduler)
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed (try setup.py develop') 'Test requires Cinder installed (try setup.py develop')
def test_retry_disabled(self): def test_retry_disabled(self):
# Retry info should not get populated when re-scheduling is off. # Retry info should not get populated when re-scheduling is off.
@ -143,7 +145,7 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
# should not have retry info in the populated filter properties: # should not have retry info in the populated filter properties:
self.assertFalse("retry" in filter_properties) self.assertFalse("retry" in filter_properties)
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed (try setup.py develop') 'Test requires Cinder installed (try setup.py develop')
def test_retry_attempt_one(self): def test_retry_attempt_one(self):
# Test retry logic on initial scheduling attempt. # Test retry logic on initial scheduling attempt.
@ -161,7 +163,7 @@ class FilterSchedulerTestCase(test_scheduler.SchedulerTestCase):
num_attempts = filter_properties['retry']['num_attempts'] num_attempts = filter_properties['retry']['num_attempts']
self.assertEqual(1, num_attempts) self.assertEqual(1, num_attempts)
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed (try setup.py develop') 'Test requires Cinder installed (try setup.py develop')
def test_retry_attempt_two(self): def test_retry_attempt_two(self):
# Test retry logic when re-scheduling. # Test retry logic when re-scheduling.

View File

@ -17,6 +17,7 @@ Tests For Scheduler Host Filters.
import httplib import httplib
import stubout import stubout
import testtools
from cinder import context from cinder import context
from cinder import db from cinder import db
@ -76,7 +77,7 @@ class HostFiltersTestCase(test.TestCase):
return ret_value return ret_value
self.stubs.Set(utils, 'service_is_up', fake_service_is_up) self.stubs.Set(utils, 'service_is_up', fake_service_is_up)
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed') 'Test requires Cinder installed')
def test_capacity_filter_passes(self): def test_capacity_filter_passes(self):
self._stub_service_is_up(True) self._stub_service_is_up(True)
@ -89,7 +90,7 @@ class HostFiltersTestCase(test.TestCase):
'service': service}) 'service': service})
self.assertTrue(filt_cls.host_passes(host, filter_properties)) self.assertTrue(filt_cls.host_passes(host, filter_properties))
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed') 'Test requires Cinder installed')
def test_capacity_filter_fails(self): def test_capacity_filter_fails(self):
self._stub_service_is_up(True) self._stub_service_is_up(True)
@ -103,7 +104,7 @@ class HostFiltersTestCase(test.TestCase):
'service': service}) 'service': service})
self.assertFalse(filt_cls.host_passes(host, filter_properties)) self.assertFalse(filt_cls.host_passes(host, filter_properties))
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed') 'Test requires Cinder installed')
def test_capacity_filter_passes_infinite(self): def test_capacity_filter_passes_infinite(self):
self._stub_service_is_up(True) self._stub_service_is_up(True)
@ -116,7 +117,7 @@ class HostFiltersTestCase(test.TestCase):
'service': service}) 'service': service})
self.assertTrue(filt_cls.host_passes(host, filter_properties)) self.assertTrue(filt_cls.host_passes(host, filter_properties))
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed') 'Test requires Cinder installed')
def test_capacity_filter_passes_unknown(self): def test_capacity_filter_passes_unknown(self):
self._stub_service_is_up(True) self._stub_service_is_up(True)
@ -129,7 +130,7 @@ class HostFiltersTestCase(test.TestCase):
'service': service}) 'service': service})
self.assertTrue(filt_cls.host_passes(host, filter_properties)) self.assertTrue(filt_cls.host_passes(host, filter_properties))
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed') 'Test requires Cinder installed')
def test_retry_filter_disabled(self): def test_retry_filter_disabled(self):
# Test case where retry/re-scheduling is disabled. # Test case where retry/re-scheduling is disabled.
@ -138,7 +139,7 @@ class HostFiltersTestCase(test.TestCase):
filter_properties = {} filter_properties = {}
self.assertTrue(filt_cls.host_passes(host, filter_properties)) self.assertTrue(filt_cls.host_passes(host, filter_properties))
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed') 'Test requires Cinder installed')
def test_retry_filter_pass(self): def test_retry_filter_pass(self):
# Node not previously tried. # Node not previously tried.
@ -148,7 +149,7 @@ class HostFiltersTestCase(test.TestCase):
filter_properties = dict(retry=retry) filter_properties = dict(retry=retry)
self.assertTrue(filt_cls.host_passes(host, filter_properties)) self.assertTrue(filt_cls.host_passes(host, filter_properties))
@test.skip_if(not test_utils.is_cinder_installed(), @testtools.skipIf(not test_utils.is_cinder_installed(),
'Test requires Cinder installed') 'Test requires Cinder installed')
def test_retry_filter_fail(self): def test_retry_filter_fail(self):
# Node was already tried. # Node was already tried.

View File

@ -32,6 +32,7 @@ import uuid
from migrate.versioning import repository from migrate.versioning import repository
import sqlalchemy import sqlalchemy
import testtools
import cinder.db.migration as migration import cinder.db.migration as migration
import cinder.db.sqlalchemy.migrate_repo import cinder.db.sqlalchemy.migrate_repo
@ -235,7 +236,7 @@ class TestMigrations(test.TestCase):
if _is_mysql_avail(user="openstack_cifail"): if _is_mysql_avail(user="openstack_cifail"):
self.fail("Shouldn't have connected") 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): def test_mysql_innodb(self):
""" """
Test that table creation on mysql only builds InnoDB tables Test that table creation on mysql only builds InnoDB tables
@ -276,7 +277,7 @@ class TestMigrations(test.TestCase):
if _is_backend_avail('postgres', user="openstack_cifail"): if _is_backend_avail('postgres', user="openstack_cifail"):
self.fail("Shouldn't have connected") self.fail("Shouldn't have connected")
@test.skip_unless(_is_backend_avail('postgres'), @testtools.skipUnless(_is_backend_avail('postgres'),
"postgresql not available") "postgresql not available")
def test_postgresql_opportunistically(self): def test_postgresql_opportunistically(self):
# add this to the global lists to make reset work with it, it's removed # add this to the global lists to make reset work with it, it's removed

View File

@ -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")

View File

@ -24,6 +24,7 @@ import tempfile
import urllib2 import urllib2
from oslo.config import cfg from oslo.config import cfg
import testtools
import webob import webob
import webob.dec import webob.dec
@ -110,7 +111,7 @@ class TestWSGIServer(test.TestCase):
server.stop() server.stop()
server.wait() server.wait()
@test.skip_if(not _ipv6_configured(), @testtools.skipIf(not _ipv6_configured(),
"Test requires an IPV6 configured interface") "Test requires an IPV6 configured interface")
def test_start_random_port_with_ipv6(self): def test_start_random_port_with_ipv6(self):
server = cinder.wsgi.Server("test_random_port", server = cinder.wsgi.Server("test_random_port",
@ -161,7 +162,7 @@ class TestWSGIServer(test.TestCase):
server.stop() server.stop()
@test.skip_if(not _ipv6_configured(), @testtools.skipIf(not _ipv6_configured(),
"Test requires an IPV6 configured interface") "Test requires an IPV6 configured interface")
def test_app_using_ipv6_and_ssl(self): def test_app_using_ipv6_and_ssl(self):
CONF.set_default("ssl_cert_file", CONF.set_default("ssl_cert_file",