From 0fd99acb5b0ef0461330daab065edef0449122e9 Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Mon, 13 Apr 2015 15:59:59 -0700 Subject: [PATCH] MonkeyPatch time.sleep in unit tests to avoid wait There's no need to wait real world seconds when there are no real world servers at the other end of these API calls. We still do wait a little bit of time, in case there is some reliance on actually having called the real time.sleep, but this should feed up test running in a loop quite a bit. Also lowering timeout on rebuild_server as it unnecessarily sleeps for 1 second. Change-Id: Ic26e90af12aedbedfbe0cc468332b921516a8409 --- shade/tests/base.py | 2 +- shade/tests/unit/base.py | 42 +++++++++++++++++++++++++ shade/tests/unit/test_caching.py | 4 +-- shade/tests/unit/test_rebuild_server.py | 4 +-- shade/tests/unit/test_shade.py | 2 +- 5 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 shade/tests/unit/base.py diff --git a/shade/tests/base.py b/shade/tests/base.py index 0b5b1f446..41d89a04a 100644 --- a/shade/tests/base.py +++ b/shade/tests/base.py @@ -25,7 +25,7 @@ _TRUE_VALUES = ('true', '1', 'yes') class TestCase(testtools.TestCase): - """Test case base class for all unit tests.""" + """Test case base class for all tests.""" def setUp(self): """Run before each test method to initialize test environment.""" diff --git a/shade/tests/unit/base.py b/shade/tests/unit/base.py new file mode 100644 index 000000000..2c7e57f43 --- /dev/null +++ b/shade/tests/unit/base.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +# Copyright 2010-2011 OpenStack Foundation +# Copyright (c) 2013 Hewlett-Packard Development Company, L.P. +# +# 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. + +import time + +import fixtures + +from shade.tests import base + + +class TestCase(base.TestCase): + + """Test case base class for all unit tests.""" + + def setUp(self): + """Run before each test method to initialize test environment.""" + + super(TestCase, self).setUp() + + # Sleeps are for real testing, but unit tests shouldn't need them + realsleep = time.sleep + + def _nosleep(seconds): + return realsleep(seconds * 0.0001) + + self.sleep_fixture = self.useFixture(fixtures.MonkeyPatch( + 'time.sleep', + _nosleep)) diff --git a/shade/tests/unit/test_caching.py b/shade/tests/unit/test_caching.py index 648ef54b8..35c901a8f 100644 --- a/shade/tests/unit/test_caching.py +++ b/shade/tests/unit/test_caching.py @@ -18,7 +18,7 @@ import os_client_config as occ import yaml import shade -from shade.tests import base +from shade.tests.unit import base class TestMemoryCache(base.TestCase): @@ -26,7 +26,7 @@ class TestMemoryCache(base.TestCase): CACHING_CONFIG = { 'cache': { - 'max_age': 10, + 'max_age': 90, 'class': 'dogpile.cache.memory', }, 'clouds': diff --git a/shade/tests/unit/test_rebuild_server.py b/shade/tests/unit/test_rebuild_server.py index 6cba05f5d..960840a85 100644 --- a/shade/tests/unit/test_rebuild_server.py +++ b/shade/tests/unit/test_rebuild_server.py @@ -22,7 +22,7 @@ Tests for the `rebuild_server` command. from mock import patch, Mock from shade import ( OpenStackCloud, OpenStackCloudException, OpenStackCloudTimeout) -from shade.tests import base +from shade.tests.unit import base class TestRebuildServer(base.TestCase): @@ -72,7 +72,7 @@ class TestRebuildServer(base.TestCase): OpenStackCloud.nova_client = Mock(**config) self.assertRaises( OpenStackCloudTimeout, - self.client.rebuild_server, "a", "b", wait=True, timeout=1) + self.client.rebuild_server, "a", "b", wait=True, timeout=0.001) def test_rebuild_server_no_wait(self): """ diff --git a/shade/tests/unit/test_shade.py b/shade/tests/unit/test_shade.py index 918d1f1bc..bfb2d498f 100644 --- a/shade/tests/unit/test_shade.py +++ b/shade/tests/unit/test_shade.py @@ -15,7 +15,7 @@ import mock import shade -from shade.tests import base +from shade.tests.unit import base class TestShade(base.TestCase):