Fix: Removed problem with race conditions in OpenStack dummy API tests
authorpeusterm <manuel.peuster@uni-paderborn.de>
Thu, 18 May 2017 13:51:57 +0000 (15:51 +0200)
committerpeusterm <manuel.peuster@uni-paderborn.de>
Thu, 18 May 2017 13:51:57 +0000 (15:51 +0200)
src/emuvim/api/openstack/openstack_api_endpoint.py
src/emuvim/test/api_base_openstack.py
src/emuvim/test/unittests/test_openstack.py
src/emuvim/test/unittests/test_sonata_dummy_gatekeeper.py

index 127c3e8..f554f0c 100644 (file)
@@ -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)
index 6fb9b49..c951665 100755 (executable)
@@ -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:
index cb4fb03..6c2bf5a 100755 (executable)
@@ -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):
index 110907c..1b13158 100755 (executable)
@@ -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)