tests for ssl connection for PRH, AAI and DmaaP 31/71031/2
authorpkaras <piotr.karas@nokia.com>
Tue, 23 Oct 2018 07:06:37 +0000 (09:06 +0200)
committerpkaras <piotr.karas@nokia.com>
Tue, 23 Oct 2018 07:26:15 +0000 (09:26 +0200)
Change-Id: If1e73dee9d517832852ae618d73e45da29052828
Issue-ID: DCAEGEN2-886
Signed-off-by: piotr.karas <piotr.karas@nokia.com>
tests/dcaegen2/prh-testcases/resources/simulator/AAI.py
tests/dcaegen2/prh-testcases/resources/simulator/AAI_simulator
tests/dcaegen2/prh-testcases/resources/simulator/DMaaP.py
tests/dcaegen2/prh-testcases/resources/simulator/DMaaP_simulator
tests/dcaegen2/prh-testcases/resources/simulator/httpServerLib.py [new file with mode: 0644]

index 76823b0..416e7f4 100644 (file)
@@ -1,9 +1,7 @@
-import _thread
 import re
-import ssl
 import time
 from http.server import BaseHTTPRequestHandler
-from http.server import HTTPServer
+import httpServerLib
 
 pnfs = 'Empty'
 
@@ -15,7 +13,7 @@ class AAISetup(BaseHTTPRequestHandler):
             global pnfs
             content_length = int(self.headers['Content-Length'])
             pnfs = self.rfile.read(content_length)
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
 
         return
 
@@ -23,10 +21,11 @@ class AAISetup(BaseHTTPRequestHandler):
         if re.search('/reset', self.path):
             global pnfs
             pnfs = 'Empty'
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
 
         return
 
+
 class AAIHandler(BaseHTTPRequestHandler):
 
     def do_PATCH(self):
@@ -41,37 +40,14 @@ class AAIHandler(BaseHTTPRequestHandler):
         return
 
 
-def _header_200_and_json(self):
-    self.send_response(200)
-    self.send_header('Content-Type', 'application/json')
-    self.end_headers()
-
-
 def _main_(handler_class=AAIHandler, protocol="HTTP/1.0"):
     handler_class.protocol_version = protocol
-    _thread.start_new_thread(_init_http_endpoints, (3333, AAIHandler))
-    _thread.start_new_thread(_init_https_endpoints, (3334, AAIHandler))
-    _thread.start_new_thread(_init_http_endpoints, (3335, AAISetup))
+    httpServerLib.start_http_endpoint(3333, AAIHandler)
+    httpServerLib.start_https_endpoint(3334, AAIHandler)
+    httpServerLib.start_http_endpoint(3335, AAISetup)
     while 1:
         time.sleep(10)
 
 
-def _init_http_endpoints(port, handler_class, server_class=HTTPServer):
-    server = server_class(('', port), handler_class)
-    sa = server.socket.getsockname()
-    print("Serving HTTP on", sa[0], "port", sa[1], "for", handler_class, "...")
-    server.serve_forever()
-
-
-def _init_https_endpoints(port, handler_class, server_class=HTTPServer):
-    server = server_class(('', port), handler_class)
-    server.socket = ssl.wrap_socket(server.socket,
-                                    keyfile="certs/server.key", certfile="certs/server.crt",
-                                    ca_certs="certs/client.crt", server_side=True)
-    sa = server.socket.getsockname()
-    print("Serving HTTPS on", sa[0], "port", sa[1], "for", handler_class, "...")
-    server.serve_forever()
-
-
 if __name__ == '__main__':
     _main_()
index 7364769..a906bc5 100644 (file)
@@ -1,6 +1,6 @@
 FROM python:3-alpine
 
-ADD AAI.py /
+ADD AAI.py httpServerLib.py /
 COPY certs/* /certs/
 
 CMD [ "python", "./AAI.py" ]
index 3ff951e..bb37ddd 100644 (file)
@@ -1,9 +1,7 @@
-import _thread
 import re
-import ssl
 import time
 from http.server import BaseHTTPRequestHandler
-from http.server import HTTPServer
+import httpServerLib
 
 posted_event_from_prh = b'Empty'
 received_event_to_get_method = b'Empty'
@@ -16,13 +14,13 @@ class DmaapSetup(BaseHTTPRequestHandler):
             global received_event_to_get_method
             content_length = int(self.headers['Content-Length'])
             received_event_to_get_method = self.rfile.read(content_length)
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
 
         return
 
     def do_GET(self):
         if re.search('/events/pnfReady', self.path):
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
             self.wfile.write(posted_event_from_prh)
 
         return
@@ -33,7 +31,7 @@ class DmaapSetup(BaseHTTPRequestHandler):
             global received_event_to_get_method
             posted_event_from_prh = b'Empty'
             received_event_to_get_method = b'Empty'
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
 
         return
 
@@ -45,49 +43,26 @@ class DMaaPHandler(BaseHTTPRequestHandler):
             global posted_event_from_prh
             content_length = int(self.headers['Content-Length'])
             posted_event_from_prh = self.rfile.read(content_length)
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
 
         return
 
     def do_GET(self):
         if re.search('/events/unauthenticated.VES_PNFREG_OUTPUT/OpenDcae-c12/c12', self.path):
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
             self.wfile.write(received_event_to_get_method)
 
         return
 
 
-def _header_200_and_json(self):
-    self.send_response(200)
-    self.send_header('Content-Type', 'application/json')
-    self.end_headers()
-
-
 def _main_(handler_class=DMaaPHandler, protocol="HTTP/1.0"):
     handler_class.protocol_version = protocol
-    _thread.start_new_thread(_init_http_endpoints, (2222, DMaaPHandler))
-    _thread.start_new_thread(_init_https_endpoints, (2223, DMaaPHandler))
-    _thread.start_new_thread(_init_http_endpoints, (2224, DmaapSetup))
+    httpServerLib.start_http_endpoint(2222, DMaaPHandler)
+    httpServerLib.start_https_endpoint(2223, DMaaPHandler)
+    httpServerLib.start_http_endpoint(2224, DmaapSetup)
     while 1:
         time.sleep(10)
 
 
-def _init_http_endpoints(port, handler_class, server_class=HTTPServer):
-    server = server_class(('', port), handler_class)
-    sa = server.socket.getsockname()
-    print("Serving HTTP on", sa[0], "port", sa[1], "for", handler_class, "...")
-    server.serve_forever()
-
-
-def _init_https_endpoints(port, handler_class, server_class=HTTPServer):
-    server = server_class(('', port), handler_class)
-    server.socket = ssl.wrap_socket(server.socket,
-                                    keyfile="certs/server.key", certfile="certs/server.crt",
-                                    ca_certs="certs/client.crt", server_side=True)
-    sa = server.socket.getsockname()
-    print("Serving HTTPS on", sa[0], "port", sa[1], "for", handler_class, "...")
-    server.serve_forever()
-
-
 if __name__ == '__main__':
     _main_()
index 40e1af0..8139fc3 100644 (file)
@@ -1,6 +1,6 @@
 FROM python:3-alpine
 
-ADD DMaaP.py /
+ADD DMaaP.py httpServerLib.py /
 COPY certs/* /certs/
 
 CMD [ "python", "./DMaaP.py" ]
diff --git a/tests/dcaegen2/prh-testcases/resources/simulator/httpServerLib.py b/tests/dcaegen2/prh-testcases/resources/simulator/httpServerLib.py
new file mode 100644 (file)
index 0000000..dcfdb7c
--- /dev/null
@@ -0,0 +1,34 @@
+import _thread
+import ssl
+from http.server import HTTPServer
+
+
+def header_200_and_json(self):
+    self.send_response(200)
+    self.send_header('Content-Type', 'application/json')
+    self.end_headers()
+
+
+def start_http_endpoint(port, handler_class):
+    _thread.start_new_thread(init_http_endpoints, (port, handler_class))
+
+
+def start_https_endpoint(port, handler_class, keyfile="certs/server.key",
+                         certfile="certs/server.crt", ca_certs="certs/client.crt"):
+    _thread.start_new_thread(init_https_endpoints, (port, handler_class, keyfile, certfile, ca_certs))
+
+
+def init_http_endpoints(port, handler_class, server_class=HTTPServer):
+    server = server_class(('', port), handler_class)
+    sa = server.socket.getsockname()
+    print("Serving HTTP on", sa[0], "port", sa[1], "for", handler_class, "...")
+    server.serve_forever()
+
+
+def init_https_endpoints(port, handler_class, keyfile, certfile, ca_certs, server_class=HTTPServer):
+    server = server_class(('', port), handler_class)
+    server.socket = ssl.wrap_socket(server.socket, keyfile=keyfile, certfile=certfile,
+                                    ca_certs=ca_certs, server_side=True)
+    sa = server.socket.getsockname()
+    print("Serving HTTPS on", sa[0], "port", sa[1], "for", handler_class, "...")
+    server.serve_forever()