]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Separate wait_until to standalone function
authorJakub Libosvar <libosvar@redhat.com>
Tue, 30 Sep 2014 16:13:50 +0000 (18:13 +0200)
committerJakub Libosvar <libosvar@redhat.com>
Fri, 12 Dec 2014 12:15:50 +0000 (13:15 +0100)
It makes wait_until more usable outside of unittest.TestCase classes.
Part of this patch is renaming pinger module to helpers thus we can have
helpers module with useful utilities for testing.

Change-Id: Ie8e4854c23aa3739b0df618db1b3944466d64bd2
Related-Bug: 1243216

neutron/tests/functional/agent/linux/base.py
neutron/tests/functional/agent/linux/helpers.py [moved from neutron/tests/functional/agent/linux/pinger.py with 72% similarity]
neutron/tests/functional/agent/test_l3_agent.py
neutron/tests/functional/base.py

index d667a4325b6b4a70f39639cb50925b66231bfbf9..4b06bdf7b37a9745db85f0496d3141d6409dd0d9 100644 (file)
@@ -19,7 +19,7 @@ from neutron.agent.linux import ovs_lib
 from neutron.agent.linux import utils
 from neutron.common import constants as n_const
 from neutron.openstack.common import uuidutils
-from neutron.tests.functional.agent.linux import pinger
+from neutron.tests.functional.agent.linux import helpers
 from neutron.tests.functional import base as functional_base
 
 
@@ -97,7 +97,7 @@ class BaseIPVethTestCase(BaseLinuxTestCase):
     def setUp(self):
         super(BaseIPVethTestCase, self).setUp()
         self.check_sudo_enabled()
-        self.pinger = pinger.Pinger(self)
+        self.pinger = helpers.Pinger(self)
 
     @staticmethod
     def _set_ip_up(device, cidr, broadcast, ip_version=4):
similarity index 72%
rename from neutron/tests/functional/agent/linux/pinger.py
rename to neutron/tests/functional/agent/linux/helpers.py
index a79e8235683fd79ce5adf013d9a38ab0d2893bd6..f3c765d21c89b160f5ec76a00e1522d4b4b48b67 100644 (file)
 #    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 eventlet
+
+
+def wait_until_true(predicate, timeout=60, sleep=1, exception=None):
+    """
+    Wait until callable predicate is evaluated as True
+
+    :param predicate: Callable deciding whether waiting should continue.
+    Best practice is to instantiate predicate with functools.partial()
+    :param timeout: Timeout in seconds how long should function wait.
+    :param sleep: Polling interval for results in seconds.
+    :param exception: Exception class for eventlet.Timeout.
+    (see doc for eventlet.Timeout for more information)
+    """
+    with eventlet.timeout.Timeout(timeout, exception):
+        while not predicate():
+            eventlet.sleep(sleep)
 
 
 class Pinger(object):
index 99f1d1edd4e98baa644772464c1a6e958b02da76..b2353881e648a197e4c5835aef11f01aaab8ec91 100644 (file)
@@ -14,6 +14,7 @@
 #    under the License.
 
 import copy
+import functools
 
 import fixtures
 import mock
@@ -29,6 +30,7 @@ from neutron.openstack.common import log as logging
 from neutron.openstack.common import uuidutils
 from neutron.tests.common.agents import l3_agent as l3_test_agent
 from neutron.tests.functional.agent.linux import base
+from neutron.tests.functional.agent.linux import helpers
 from neutron.tests.unit import test_l3_agent
 
 LOG = logging.getLogger(__name__)
@@ -233,16 +235,19 @@ class L3AgentTestCase(L3AgentTestFramework):
         router = self.manage_router(self.agent, router_info)
 
         if enable_ha:
-            self.wait_until(lambda: router.ha_state == 'master')
+            helpers.wait_until_true(lambda: router.ha_state == 'master')
 
             # Keepalived notifies of a state transition when it starts,
             # not when it ends. Thus, we have to wait until keepalived finishes
             # configuring everything. We verify this by waiting until the last
             # device has an IP address.
             device = router.router[l3_constants.INTERFACE_KEY][-1]
-            self.wait_until(self.device_exists_with_ip_mac, device,
-                            self.agent.get_internal_device_name,
-                            router.ns_name)
+            device_exists = functools.partial(
+                self.device_exists_with_ip_mac,
+                device,
+                self.agent.get_internal_device_name,
+                router.ns_name)
+            helpers.wait_until_true(device_exists)
 
         self.assertTrue(self._namespace_exists(router))
         self.assertTrue(self._metadata_proxy_exists(self.agent.conf, router))
@@ -347,8 +352,8 @@ class L3HATestFramework(L3AgentTestFramework):
 
         router2 = self.manage_router(self.failover_agent, router_info_2)
 
-        self.wait_until(lambda: router1.ha_state == 'master')
-        self.wait_until(lambda: router2.ha_state == 'backup')
+        helpers.wait_until_true(lambda: router1.ha_state == 'master')
+        helpers.wait_until_true(lambda: router2.ha_state == 'backup')
 
         device_name = self.agent.get_ha_device_name(
             router1.router[l3_constants.HA_INTERFACE_KEY]['id'])
@@ -356,5 +361,5 @@ class L3HATestFramework(L3AgentTestFramework):
                                     router1.ns_name)
         ha_device.link.set_down()
 
-        self.wait_until(lambda: router2.ha_state == 'master')
-        self.wait_until(lambda: router1.ha_state == 'fault')
+        helpers.wait_until_true(lambda: router2.ha_state == 'master')
+        helpers.wait_until_true(lambda: router1.ha_state == 'fault')
index 7ca3237273506de51c3f91cc78c42252fca2ec30..fe25535fa92bf83c41a29a719ad792d2acf2e7ba 100644 (file)
 #    under the License.
 
 import os
-import time
 
 from neutron.tests import base
 
 
 SUDO_CMD = 'sudo -n'
-TIMEOUT = 60
-SLEEP_INTERVAL = 1
 
 
 class BaseSudoTestCase(base.BaseTestCase):
@@ -58,8 +55,3 @@ class BaseSudoTestCase(base.BaseTestCase):
     def check_sudo_enabled(self):
         if not self.sudo_enabled:
             self.skipTest('testing with sudo is not enabled')
-
-    def wait_until(self, predicate, *args, **kwargs):
-        with self.assert_max_execution_time(TIMEOUT):
-            while not predicate(*args, **kwargs):
-                time.sleep(SLEEP_INTERVAL)