improve functional tests 46/90246/4
authormanuedelf <emmanuelle.delfour@gmail.com>
Thu, 4 Jun 2020 09:29:18 +0000 (11:29 +0200)
committerGuillaume Lambert <guillaume.lambert@orange.com>
Tue, 9 Jun 2020 17:37:14 +0000 (17:37 +0000)
- Refactor test_util 2.2.1 to remove duplicated code
- improve sed command in tox.ini to patch Karaf exec

Signed-off-by: Guillaume Lambert <guillaume.lambert@orange.com>
Change-Id: Ieccc45df64b038e910ea1d2b7ff44fdcbcfcb301

tests/transportpce_tests/2.2.1/test_utils.py
tox.ini

index 4d5fe3efb61369e77ded8e328713f5394c3f39fb..0275d05d42368e723c98e2b5c2e5edf887052984 100644 (file)
@@ -1,6 +1,25 @@
+#!/usr/bin/env python
+##############################################################################
+# Copyright (c) 2020 Orange, Inc. and others.  All rights reserved.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+import json
 import os
+import re
+import signal
 import subprocess
 
+import psutil
+import requests
+
+HONEYNODE_OK_START_MSG = re.escape("Netconf SSH endpoint started successfully at 0.0.0.0")
+
+TYPE_APPLICATION_JSON = {'content-type': 'application/json'}
+
 honeynode_executable = os.path.join(
     os.path.dirname(os.path.realpath(__file__)),
     "..", "..", "honeynode", "2.2.1", "honeynode-simulator", "honeycomb-tpce")
@@ -8,53 +27,49 @@ samples_directory = os.path.join(
     os.path.dirname(os.path.realpath(__file__)),
     "..", "..", "sample_configs", "openroadm", "2.2.1")
 
+log_directory = os.path.dirname(os.path.realpath(__file__))
+
 
 def start_xpdra_honeynode():
-    if os.path.isfile(honeynode_executable):
-        with open('honeynode1.log', 'w') as outfile:
-            return subprocess.Popen(
-                [honeynode_executable, "17840", os.path.join(samples_directory, "oper-XPDRA.xml")],
-                stdout=outfile, stderr=outfile)
+    log_file = os.path.join(log_directory, "oper-XPDRA.log")
+    process = start_node(log_file, "17840", "oper-XPDRA.xml")
+    wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 5000)
+    return process
 
 
 def start_roadma_honeynode():
-    if os.path.isfile(honeynode_executable):
-        with open('honeynode2.log', 'w') as outfile:
-            return subprocess.Popen(
-                [honeynode_executable, "17841", os.path.join(samples_directory, "oper-ROADMA.xml")],
-                stdout=outfile, stderr=outfile)
+    log_file = os.path.join(log_directory, "oper-ROADMA.log")
+    process = start_node(log_file, "17841", "oper-ROADMA.xml")
+    wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 5000)
+    return process
 
 
 def start_roadmb_honeynode():
-    if os.path.isfile(honeynode_executable):
-        with open('honeynode5.log', 'w') as outfile:
-            return subprocess.Popen(
-                [honeynode_executable, "17842", os.path.join(samples_directory, "oper-ROADMB.xml")],
-                stdout=outfile, stderr=outfile)
+    log_file = os.path.join(log_directory, "oper-ROADMB.log")
+    process = start_node(log_file, "17842", "oper-ROADMB.xml")
+    wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 5000)
+    return process
 
 
 def start_roadmc_honeynode():
-    if os.path.isfile(honeynode_executable):
-        with open('honeynode3.log', 'w') as outfile:
-            return subprocess.Popen(
-                [honeynode_executable, "17843", os.path.join(samples_directory, "oper-ROADMC.xml")],
-                stdout=outfile, stderr=outfile)
+    log_file = os.path.join(log_directory, "oper-ROADMC.log")
+    process = start_node(log_file, "17843", "oper-ROADMC.xml")
+    wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 5000)
+    return process
 
 
 def start_xpdrc_honeynode():
-    if os.path.isfile(honeynode_executable):
-        with open('honeynode4.log', 'w') as outfile:
-            return subprocess.Popen(
-                [honeynode_executable, "17844", os.path.join(samples_directory, "oper-XPDRC.xml")],
-                stdout=outfile, stderr=outfile)
+    log_file = os.path.join(log_directory, "oper-XPDRC.log")
+    process = start_node(log_file, "17844", "oper-XPDRC.xml")
+    wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 5000)
+    return process
 
 
 def start_spdra_honeynode():
-    if os.path.isfile(honeynode_executable):
-        with open('honeynode6.log', 'w') as outfile:
-            return subprocess.Popen(
-                [honeynode_executable, "17845", os.path.join(samples_directory, "oper-SPDRAv2.xml")],
-                stdout=outfile, stderr=outfile)
+    log_file = os.path.join(log_directory, "oper-SPDRAv2.log")
+    process = start_node(log_file, "17845", "oper-SPDRAv2.xml")
+    wait_until_log_contains(log_file, HONEYNODE_OK_START_MSG, 5000)
+    return process
 
 
 def start_tpce():
@@ -77,3 +92,118 @@ def start_tpce():
             return subprocess.Popen(
                 ["sh", executable, "server"], stdout=outfile, stderr=outfile,
                 stdin=open(os.devnull))
+
+
+def install_karaf_feature(feature_name: str):
+    print("installing feature " + feature_name)
+    executable = os.path.join(
+        os.path.dirname(os.path.realpath(__file__)),
+        "..", "..", "..", "karaf", "target", "assembly", "bin", "client")
+    return subprocess.run([executable],
+                          input='feature:install ' + feature_name + '\n feature:list | grep tapi \n logout \n',
+                          universal_newlines=True)
+
+
+def post_request(url, data, username, password):
+    return requests.request(
+        "POST", url, data=json.dumps(data),
+        headers=TYPE_APPLICATION_JSON, auth=(username, password))
+
+
+def put_request(url, data, username, password):
+    return requests.request(
+        "PUT", url, data=json.dumps(data), headers=TYPE_APPLICATION_JSON,
+        auth=(username, password))
+
+
+def delete_request(url, username, password):
+    return requests.request(
+        "DELETE", url, headers=TYPE_APPLICATION_JSON,
+        auth=(username, password))
+
+
+def generate_connect_data(node_id: str, node_port: str):
+    data = {"node": [{
+        "node-id": node_id,
+        "netconf-node-topology:username": "admin",
+        "netconf-node-topology:password": "admin",
+        "netconf-node-topology:host": "127.0.0.1",
+        "netconf-node-topology:port": node_port,
+        "netconf-node-topology:tcp-only": "false",
+        "netconf-node-topology:pass-through": {}}]}
+    return data
+
+
+def generate_link_data(xpdr_node: str, xpdr_num: str, network_num: str, rdm_node: str, srg_num: str,
+                       termination_num: str):
+    data = {
+        "networkutils:input": {
+            "networkutils:links-input": {
+                "networkutils:xpdr-node": xpdr_node,
+                "networkutils:xpdr-num": xpdr_num,
+                "networkutils:network-num": network_num,
+                "networkutils:rdm-node": rdm_node,
+                "networkutils:srg-num": srg_num,
+                "networkutils:termination-point-num": termination_num
+            }
+        }
+    }
+    return data
+
+
+def shutdown_process(process):
+    if process is not None:
+        for child in psutil.Process(process.pid).children():
+            child.send_signal(signal.SIGINT)
+            child.wait()
+        process.send_signal(signal.SIGINT)
+
+
+def start_node(log_file: str, node_port: str, node_config_file_name: str):
+    if os.path.isfile(honeynode_executable):
+        with open(log_file, 'w') as outfile:
+            return subprocess.Popen(
+                [honeynode_executable, node_port, os.path.join(samples_directory, node_config_file_name)],
+                stdout=outfile, stderr=outfile)
+
+
+def wait_until_log_contains(log_file, searched_string, time_to_wait=20):
+    found = False
+    tail = None
+    try:
+        with timeout(seconds=time_to_wait):
+            print("Waiting for " + searched_string)
+            tail = subprocess.Popen(['tail', '-F', log_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+            regexp = re.compile(searched_string)
+            while True:
+                line = tail.stdout.readline().decode('utf-8')
+                if regexp.search(line):
+                    print("Searched string found.")
+                    found = True
+                    break
+    except TimeoutError:
+        print("Cannot find string "+searched_string+" after waiting for "+str(time_to_wait))
+    finally:
+        if tail is not None:
+            print("Stopping tail command")
+            tail.stderr.close()
+            tail.stdout.close()
+            tail.kill()
+            tail.wait()
+        return found
+
+
+class timeout:
+    def __init__(self, seconds=1, error_message='Timeout'):
+        self.seconds = seconds
+        self.error_message = error_message
+
+    def handle_timeout(self, signum, frame):
+        raise TimeoutError(self.error_message)
+
+    def __enter__(self):
+        signal.signal(signal.SIGALRM, self.handle_timeout)
+        signal.alarm(self.seconds)
+
+    def __exit__(self, type, value, traceback):
+        signal.alarm(0)
diff --git a/tox.ini b/tox.ini
index f3c0c051793da881349e3d327519d37f8f2a7d5b..f42b174f7027231817866117e9b7795db398acec 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -27,7 +27,7 @@ commands =
   {py3,portmapping,topoPortMapping,rspn,topology,pce,olm,end2end,portmapping221,rspn221,otnrenderer,topology221,otntopology,olm221,end2end221,gnpy}: - sh -c ". $PWD/reflectwarn.sh && cd .. && mvn clean install -s tests/odl_settings.xml -DskipTests -Dmaven.javadoc.skip=true -Dodlparent.spotbugs.skip -Dodlparent.checkstyle.skip"
   {py3,portmapping,topoPortMapping,rspn,topology,olm,end2end,portmapping221,rspn221,otnrenderer,topology221,otn-topology,olm221,end2end221}: - sh -c "mv  ../olm/src/main/java/org/opendaylight/transportpce/olm/util/OlmUtils.java_  ../olm/src/main/java/org/opendaylight/transportpce/olm/util/OlmUtils.java"
 #patch Karaf exec for the same reason at runtime
-  {py3,portmapping,topoPortMapping,rspn,topology,pce,olm,end2end,portmapping221,rspn221,otnrenderer,topology221,otntopology,olm221,end2end221,gnpy}: - sh -c "sed -i'_' 's@!/bin/sh@!/bin/sh\'$'\n. $(dirname $0)/../../../../tests/reflectwarn.sh@' ../karaf/target/assembly/bin/karaf"
+  {py3,portmapping,topoPortMapping,rspn,topology,pce,olm,end2end,portmapping221,rspn221,otnrenderer,topology221,otntopology,olm221,end2end221,gnpy}: - sh -c "sed -i'_' '1 a\. \$(dirname \$0)/../../../../tests/reflectwarn.sh' ../karaf/target/assembly/bin/karaf"
 #build Lighty if needed
   {py3,portmapping,topoPortMapping,rspn,topology,pce,olm,end2end,portmapping221,rspn221,otnrenderer,topology221,otntopology,olm221,end2end221,gnpy}: - sh -c 'if [ "$USE_LIGHTY" = "True" ]; then (cd ../lighty && ./build.sh); fi'
 #run 1.2.1 functional tests