From: peusterm Date: Thu, 18 May 2017 13:51:57 +0000 (+0200) Subject: Fix: Removed problem with race conditions in OpenStack dummy API tests X-Git-Tag: v3.1~27^2 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=f4ac4f2dcb58ecfd59b4c13e31bf024bf60d1da4;p=osm%2Fvim-emu.git Fix: Removed problem with race conditions in OpenStack dummy API tests --- diff --git a/src/emuvim/api/openstack/openstack_api_endpoint.py b/src/emuvim/api/openstack/openstack_api_endpoint.py index 127c3e8..f554f0c 100644 --- a/src/emuvim/api/openstack/openstack_api_endpoint.py +++ b/src/emuvim/api/openstack/openstack_api_endpoint.py @@ -4,6 +4,8 @@ import logging import threading import compute import requests +import socket +import time class OpenstackApiEndpoint(): @@ -55,7 +57,7 @@ class OpenstackApiEndpoint(): logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % ( self.__class__.__name__, self.ip, self.port)) - def start(self): + def start(self, wait_for_port=False): """ Start all connected OpenStack endpoints that are connected to this API endpoint. """ @@ -66,6 +68,9 @@ class OpenstackApiEndpoint(): thread.daemon = True thread.name = component.__class__ thread.start() + if wait_for_port: + self._wait_for_port(component.ip, component.port) + def stop(self): """ @@ -78,3 +83,14 @@ class OpenstackApiEndpoint(): except: # seems to be stopped pass + + def _wait_for_port(self, ip, port): + for i in range(0, 10): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(1) # 1 Second Timeout + r = s.connect_ex((ip, port)) + if r == 0: + break # port is open proceed + else: + logging.warning("Waiting for {}:{} ... ({}/10)".format(ip, port, i + 1)) + time.sleep(1) diff --git a/src/emuvim/test/api_base_openstack.py b/src/emuvim/test/api_base_openstack.py index 6fb9b49..c951665 100755 --- a/src/emuvim/test/api_base_openstack.py +++ b/src/emuvim/test/api_base_openstack.py @@ -103,7 +103,7 @@ class ApiBaseOpenStack(unittest.TestCase): def startApi(self): for i in self.api: - i.start() + i.start(wait_for_port=True) def stopApi(self): for i in self.api: diff --git a/src/emuvim/test/unittests/test_openstack.py b/src/emuvim/test/unittests/test_openstack.py index cb4fb03..6c2bf5a 100755 --- a/src/emuvim/test/unittests/test_openstack.py +++ b/src/emuvim/test/unittests/test_openstack.py @@ -55,11 +55,9 @@ class testRestApi(ApiBaseOpenStack): # start api self.startApi() - time.sleep(2) # start Mininet network self.startNet() - time.sleep(2) @unittest.skip("temporarily disabled") def testChainingDummy(self): diff --git a/src/emuvim/test/unittests/test_sonata_dummy_gatekeeper.py b/src/emuvim/test/unittests/test_sonata_dummy_gatekeeper.py index 110907c..1b13158 100755 --- a/src/emuvim/test/unittests/test_sonata_dummy_gatekeeper.py +++ b/src/emuvim/test/unittests/test_sonata_dummy_gatekeeper.py @@ -56,6 +56,7 @@ class testSonataDummyGatekeeper(SimpleTestTopology): sdkg1.connectDatacenter(self.dc[1]) # run the dummy gatekeeper (in another thread, don't block) sdkg1.start() + time.sleep(3) # start Mininet network self.startNet() time.sleep(1) @@ -161,6 +162,7 @@ class testSonataDummyGatekeeper(SimpleTestTopology): sdkg1.connectDatacenter(self.dc[1]) # run the dummy gatekeeper (in another thread, don't block) sdkg1.start() + time.sleep(3) # start Mininet network self.startNet() time.sleep(1) @@ -216,6 +218,7 @@ class testSonataDummyGatekeeper(SimpleTestTopology): sdkg1.connectDatacenter(self.dc[1]) # run the dummy gatekeeper (in another thread, don't block) sdkg1.start() + time.sleep(3) # start Mininet network self.startNet() time.sleep(1)