Added more logs to track better errors
[functest.git] / testcases / features / sfc / sfc.py
index c73d8b2..122ed5d 100755 (executable)
@@ -27,7 +27,7 @@ HOME = os.environ['HOME'] + "/"
 
 VM_BOOT_TIMEOUT = 180
 INSTANCE_NAME = "client"
-FLAVOR = "m1.small"
+FLAVOR = "custom"
 IMAGE_NAME = "sf_nsh_colorado"
 IMAGE_FILENAME = "sf_nsh_colorado.qcow2"
 IMAGE_FORMAT = "qcow2"
@@ -52,6 +52,16 @@ TACKER_SCRIPT = 'sfc_tacker.bash'
 TEARDOWN_SCRIPT = "sfc_teardown.bash"
 TACKER_CHANGECLASSI = "sfc_change_classi.bash"
 
+ssh_options = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
+
+
+def check_ssh(ip):
+    cmd = "sshpass -p opnfv ssh " + ssh_options + " -q " + ip + " exit"
+    success = subprocess.call(cmd, shell=True) == 0
+    if not success:
+        logger.debug("Wating for SSH connectivity in SF with IP: %s" % ip)
+    return success
+
 
 def main():
 
@@ -63,7 +73,6 @@ def main():
     start_time = time.time()
     json_results = {}
 
-    ssh_options = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
     contr_cmd = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
                  " 'fuel node'|grep controller|awk '{print $10}'")
     logger.info("Executing script to get ip_server: '%s'" % contr_cmd)
@@ -73,15 +82,6 @@ def main():
                                stderr=subprocess.PIPE)
     ip_server = process.stdout.readline().rstrip()
 
-    contr_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
-                  " 'fuel node'|grep compute|awk '{print $10}'")
-    logger.info("Executing script to get ip_compute: '%s'" % contr_cmd2)
-    process = subprocess.Popen(contr_cmd2,
-                               shell=True,
-                               stdout=subprocess.PIPE,
-                               stderr=subprocess.PIPE)
-    ip_compute = process.stdout.readline().rstrip()
-
     iptable_cmd1 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
                     " ssh " + ip_server + " iptables -P INPUT ACCEPT ")
     iptable_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
@@ -98,7 +98,7 @@ def main():
     subprocess.call(iptable_cmd2, shell=True, stderr=subprocess.PIPE)
 
     logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd3)
-    subprocess.call(iptable_cmd2, shell=True, stderr=subprocess.PIPE)
+    subprocess.call(iptable_cmd3, shell=True, stderr=subprocess.PIPE)
 
 # Getting the different clients
 
@@ -140,7 +140,33 @@ def main():
     sg_id = os_utils.create_security_group_full(neutron_client,
                                                 SECGROUP_NAME, SECGROUP_DESCR)
 
-    # boot INTANCE
+    secgroups = os_utils.get_security_groups(neutron_client)
+
+    for sg in secgroups:
+        os_utils.create_secgroup_rule(neutron_client, sg['id'],
+                                      'ingress', 'tcp',
+                                      port_range_min=22,
+                                      port_range_max=22)
+        os_utils.create_secgroup_rule(neutron_client, sg['id'],
+                                      'egress', 'tcp',
+                                      port_range_min=22,
+                                      port_range_max=22)
+        os_utils.create_secgroup_rule(neutron_client, sg['id'],
+                                      'ingress', 'tcp',
+                                      port_range_min=80,
+                                      port_range_max=80)
+        os_utils.create_secgroup_rule(neutron_client, sg['id'],
+                                      'egress', 'tcp',
+                                      port_range_min=80,
+                                      port_range_max=80)
+
+    _, custom_flv_id = os_utils.get_or_create_flavor(
+        'custom', 1500, 10, 1, public=True)
+    if not custom_flv_id:
+        logger.error("Failed to create custom flavor")
+        sys.exit(1)
+
+    # boot INSTANCE
     logger.info("Creating instance '%s'..." % INSTANCE_NAME)
     logger.debug(
         "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
@@ -148,7 +174,8 @@ def main():
     instance = os_utils.create_instance_and_wait_for_active(FLAVOR,
                                                             image_id,
                                                             network_id,
-                                                            INSTANCE_NAME)
+                                                            INSTANCE_NAME,
+                                                            av_zone='nova')
 
     if instance is None:
         logger.error("Error while booting instance.")
@@ -181,14 +208,15 @@ def main():
 # STARTING SECOND VM (server) ###
 
     # boot INTANCE
-    logger.info("Creating instance '%s'..." % INSTANCE_NAME)
+    logger.info("Creating instance '%s'..." % INSTANCE_NAME_2)
     logger.debug(
         "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
-        "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
+        "network=%s \n" % (INSTANCE_NAME_2, FLAVOR, image_id, network_id))
     instance_2 = os_utils.create_instance_and_wait_for_active(FLAVOR,
                                                               image_id,
                                                               network_id,
-                                                              INSTANCE_NAME_2)
+                                                              INSTANCE_NAME_2,
+                                                              av_zone='nova')
 
     if instance_2 is None:
         logger.error("Error while booting instance.")
@@ -263,13 +291,29 @@ def main():
         logger.error('Failed to obtain IPs, cant continue, exiting')
         return
 
-    logger.info("Waiting 60 seconds for floating IP assignment")
-    for j in range(0, 6):
-        logger.debug("Test starting in {0} seconds".format(str((6 - j) * 10)))
-        time.sleep(10)
-
     logger.debug("Floating IPs for SFs: %s..." % ips)
 
+    # Check SSH connectivity to VNFs
+    r = 0
+    retries = 100
+    check = [False, False]
+
+    logger.info("Checking SSH connectivity to the SFs with ips %s" % str(ips))
+    while r < retries and not all(check):
+        try:
+            check = [check_ssh(ips[0]), check_ssh(ips[1])]
+        except Exception:
+            logger.exception("SSH check failed")
+            check = [False, False]
+        time.sleep(3)
+        r += 1
+
+    if not all(check):
+        logger.error("Cannot establish SSH connection to the SFs")
+        sys.exit(1)
+
+    logger.info("SSH connectivity to the SFs established")
+
     # SSH TO START THE VXLAN_TOOL ON SF1
     logger.info("Configuring the SFs")
     try:
@@ -321,15 +365,6 @@ def main():
     except Exception:
         logger.exception("vxlan_tool not started in SF2")
 
-    # SSH to modify the classification flows in compute
-
-    contr_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
-                  " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
-    logger.info("Executing script to modify the classi: '%s'" % contr_cmd3)
-    process = subprocess.Popen(contr_cmd3,
-                               shell=True,
-                               stdout=subprocess.PIPE)
-
     i = 0
 
     # SSH TO EXECUTE cmd_client
@@ -387,16 +422,7 @@ def main():
     subprocess.call(tacker_classi, shell=True)
 
     logger.info("Wait for ODL to update the classification rules in OVS")
-    time.sleep(10)
-
-    # SSH to modify the classification flows in compute
-
-    contr_cmd4 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
-                  " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
-    logger.info("Executing script to modify the classi: '%s'" % contr_cmd4)
-    process = subprocess.Popen(contr_cmd4,
-                               shell=True,
-                               stdout=subprocess.PIPE)
+    time.sleep(100)
 
     # SSH TO EXECUTE cmd_client