stop hardcoded FUNCTEST_REPO path everywhere
[functest.git] / testcases / Controllers / ONOS / Teston / onosfunctest.py
index b215b08..6b922fb 100755 (executable)
@@ -19,9 +19,17 @@ import os
 import re
 import time
 
+import argparse
 import functest.utils.functest_logger as ft_logger
 import functest.utils.functest_utils as functest_utils
 import functest.utils.openstack_utils as openstack_utils
+from functest.utils.functest_utils import FUNCTEST_REPO as REPO_PATH
+from neutronclient.v2_0 import client as neutronclient
+
+parser = argparse.ArgumentParser()
+parser.add_argument("-t", "--testcase", help="Testcase name")
+args = parser.parse_args()
+
 
 """ logging configuration """
 logger = ft_logger.Logger("onos").getLogger()
@@ -33,10 +41,6 @@ ONOS_REPO_PATH = functest_utils.get_parameter_from_yaml(
     "general.directories.dir_repos")
 ONOS_CONF_DIR = functest_utils.get_parameter_from_yaml(
     "general.directories.dir_functest_conf")
-REPO_PATH = ONOS_REPO_PATH + '/functest/'
-if not os.path.exists(REPO_PATH):
-    logger.error("Functest repository directory not found '%s'" % REPO_PATH)
-    exit(-1)
 
 ONOSCI_PATH = ONOS_REPO_PATH + "/"
 starttime = datetime.datetime.now()
@@ -51,7 +55,7 @@ GLANCE_IMAGE_FILENAME = functest_utils.get_parameter_from_yaml(
     "onos_sfc.image_file_name")
 GLANCE_IMAGE_PATH = functest_utils.get_parameter_from_yaml(
     "general.directories.dir_functest_data") + "/" + GLANCE_IMAGE_FILENAME
-SFC_PATH = REPO_PATH + functest_utils.get_parameter_from_yaml(
+SFC_PATH = REPO_PATH + "/" + functest_utils.get_parameter_from_yaml(
     "general.directories.dir_onos_sfc")
 
 
@@ -186,23 +190,36 @@ def SfcTest():
     os.system(cmd)
 
 
-def SetSfcIp():
-    cmd = "openstack catalog show network | grep publicURL"
+def GetIp(type):
+    cmd = "openstack catalog show " + type + " | grep publicURL"
     cmd_output = os.popen(cmd).read()
     ip = re.search(r"\d+\.\d+\.\d+\.\d+", cmd_output).group()
-    cmd_onos_ip = "sed -i 's/onos_ip/" + ip + "/g' " + SFC_PATH + "Sfc_fun.py"
-    cmd_openstack_ip = "sed -i 's/openstack_ip/" + ip\
-                       + "/g' " + SFC_PATH + "Sfc_fun.py"
-    logger.info("Modify ip for SFC")
-    os.system(cmd_onos_ip)
-    os.system(cmd_openstack_ip)
+    return ip
 
 
-def main():
+def Replace(before, after):
+    file = "Sfc_fun.py"
+    cmd = "sed -i 's/" + before + "/" + after + "/g' " + SFC_PATH + file
+    os.system(cmd)
+
+
+def SetSfcConf():
+    Replace("keystone_ip", GetIp("keystone"))
+    Replace("neutron_ip", GetIp("neutron"))
+    Replace("nova_ip", GetIp("nova"))
+    Replace("glance_ip", GetIp("glance"))
+    pwd = os.environ['OS_PASSWORD']
+    Replace("console", pwd)
+    creds_neutron = openstack_utils.get_credentials("neutron")
+    neutron_client = neutronclient.Client(**creds_neutron)
+    ext_net = openstack_utils.get_external_net(neutron_client)
+    Replace("admin_floating_net", ext_net)
+    logger.info("Modify configuration for SFC")
+
+
+def OnosTest():
     start_time = time.time()
     stop_time = start_time
-    # DownloadCodes()
-    # if args.installer == "joid":
     if INSTALLER_TYPE == "joid":
         logger.debug("Installer is Joid")
         SetOnosIpForJoid()
@@ -210,10 +227,6 @@ def main():
         SetOnosIp()
     RunScript("FUNCvirNetNB")
     RunScript("FUNCvirNetNBL3")
-    if DEPLOY_SCENARIO == "os-onos-sfc-ha":
-        CreateImage()
-        SetSfcIp()
-        SfcTest()
     try:
         logger.debug("Push ONOS results into DB")
         # TODO check path result for the file
@@ -241,8 +254,19 @@ def main():
     except:
         logger.error("Error pushing results into Database")
 
-    # CleanOnosTest()
+    if status == "FAIL":
+        EXIT_CODE = -1
+        exit(EXIT_CODE)
+
+
+def main():
 
+    if args.testcase == "sfc":
+        CreateImage()
+        SetSfcConf()
+        SfcTest()
+    else:
+        OnosTest()
 
 if __name__ == '__main__':
     main()