Small fixes in sfc testcase
[functest.git] / testcases / features / sfc / sfc_colorado1.py
index b8f6b9d..5580d9e 100755 (executable)
@@ -26,7 +26,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"
@@ -51,6 +51,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():
 
@@ -62,7 +72,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)
@@ -139,6 +148,32 @@ def main():
     sg_id = os_utils.create_security_group_full(neutron_client,
                                                 SECGROUP_NAME, SECGROUP_DESCR)
 
+    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)
+
     iterator = 0
     while(iterator < 6):
         # boot INSTANCE
@@ -153,7 +188,8 @@ def main():
 
         if instance is None:
             logger.error("Error while booting instance.")
-            sys.exit(-1)
+            iterator += 1
+            continue
         # Retrieve IP of INSTANCE
         instance_ip = instance.networks.get(NET_NAME)[0]
         logger.debug("Instance '%s' got private ip '%s'." %
@@ -170,7 +206,8 @@ def main():
 
         if floatip_client is None:
             logger.error("Cannot create floating IP.")
-            sys.exit(-1)
+            iterator += 1
+            continue
         logger.info("Floating IP created: '%s'" % floatip_client)
 
         logger.info("Associating floating ip: '%s' to VM '%s' "
@@ -179,7 +216,8 @@ def main():
                                         instance.id,
                                         floatip_client):
             logger.error("Cannot associate floating IP to VM.")
-            sys.exit(-1)
+            iterator += 1
+            continue
 
     # STARTING SECOND VM (server) ###
 
@@ -196,7 +234,8 @@ def main():
 
         if instance_2 is None:
             logger.error("Error while booting instance.")
-            sys.exit(-1)
+            iterator += 1
+            continue
         # Retrieve IP of INSTANCE
         instance_ip_2 = instance_2.networks.get(NET_NAME)[0]
         logger.debug("Instance '%s' got private ip '%s'." %
@@ -213,7 +252,8 @@ def main():
 
         if floatip_server is None:
             logger.error("Cannot create floating IP.")
-            sys.exit(-1)
+            iterator += 1
+            continue
         logger.info("Floating IP created: '%s'" % floatip_server)
 
         logger.info("Associating floating ip: '%s' to VM '%s' "
@@ -223,7 +263,8 @@ def main():
                                         instance_2.id,
                                         floatip_server):
             logger.error("Cannot associate floating IP to VM.")
-            sys.exit(-1)
+            iterator += 1
+            continue
 
         # CREATION OF THE 2 SF ####
 
@@ -268,14 +309,31 @@ 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 {0}"
+                    .format(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")
+            iterator += 1
+            continue
+
+        logger.info("SSH connectivity to the SFs established")
+
         # SSH TO START THE VXLAN_TOOL ON SF1
         logger.info("Configuring the SFs")
         try: