Merge "Update rally version"
[functest.git] / testcases / features / sfc / sfc_colorado1.py
index dc13436..d31541d 100755 (executable)
@@ -8,6 +8,8 @@ import paramiko
 import functest.utils.functest_logger as ft_logger
 import functest.utils.functest_utils as ft_utils
 import functest.utils.openstack_utils as os_utils
+import SSHUtils as ssh_utils
+import ovs_utils
 
 parser = argparse.ArgumentParser()
 
@@ -20,13 +22,14 @@ args = parser.parse_args()
 """ logging configuration """
 logger = ft_logger.Logger("ODL_SFC").getLogger()
 
+FUNCTEST_RESULTS_DIR = '/home/opnfv/functest/results/odl-sfc'
 FUNCTEST_REPO = ft_utils.FUNCTEST_REPO
 
 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"
@@ -53,6 +56,12 @@ TACKER_CHANGECLASSI = "sfc_change_classi.bash"
 
 ssh_options = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
 
+PROXY = {
+    'ip': '10.20.0.2',
+    'username': 'root',
+    'password': 'r00tme'
+}
+
 
 def check_ssh(ip):
     cmd = "sshpass -p opnfv ssh " + ssh_options + " -q " + ip + " exit"
@@ -81,14 +90,14 @@ 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,
+    comp_cmd = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
+                " 'fuel node'|grep compute|awk '{print $10}'")
+    logger.info("Executing script to get compute IPs: '%s'" % comp_cmd)
+    process = subprocess.Popen(comp_cmd,
                                shell=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
-    ip_compute = process.stdout.readline().rstrip()
+    ip_computes = [ip.strip() for ip in process.stdout.readlines()]
 
     iptable_cmd1 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
                     " ssh " + ip_server + " iptables -P INPUT ACCEPT ")
@@ -114,6 +123,20 @@ def main():
     neutron_client = os_utils.get_neutron_client()
     glance_client = os_utils.get_glance_client()
 
+    ovs_logger = ovs_utils.OVSLogger(
+        os.path.join(os.getcwd(), 'ovs-logs'),
+        FUNCTEST_RESULTS_DIR)
+
+    controller_clients = [ssh_utils.get_ssh_client(ip_server,
+                                                   'root',
+                                                   proxy=PROXY)]
+    compute_clients = []
+    for c_ip in ip_computes:
+        c_client = ssh_utils.get_ssh_client(c_ip,
+                                            'root',
+                                            proxy=PROXY)
+        compute_clients.append(c_client)
+
 # Download the image
 
     if not os.path.isfile(IMAGE_PATH):
@@ -151,6 +174,14 @@ def main():
     secgroups = os_utils.get_security_groups(neutron_client)
 
     for sg in secgroups:
+        os_utils.create_secgroup_rule(neutron_client, sg['id'],
+                                      'ingress', 'udp',
+                                      port_range_min=67,
+                                      port_range_max=68)
+        os_utils.create_secgroup_rule(neutron_client, sg['id'],
+                                      'egress', 'udp',
+                                      port_range_min=67,
+                                      port_range_max=68)
         os_utils.create_secgroup_rule(neutron_client, sg['id'],
                                       'ingress', 'tcp',
                                       port_range_min=22,
@@ -159,6 +190,20 @@ def main():
                                       '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):
@@ -167,13 +212,16 @@ def main():
         logger.debug(
             "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
             "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
-        instance = os_utils.create_instance_and_wait_for_active(FLAVOR,
-                                                                image_id,
-                                                                network_id,
-                                                                INSTANCE_NAME)
+        instance = os_utils.create_instance_and_wait_for_active(
+            FLAVOR,
+            image_id,
+            network_id,
+            INSTANCE_NAME,
+            av_zone='nova')
 
         if instance is None:
             logger.error("Error while booting instance.")
+            iterator += 1
             continue
         # Retrieve IP of INSTANCE
         instance_ip = instance.networks.get(NET_NAME)[0]
@@ -191,6 +239,7 @@ def main():
 
         if floatip_client is None:
             logger.error("Cannot create floating IP.")
+            iterator += 1
             continue
         logger.info("Floating IP created: '%s'" % floatip_client)
 
@@ -200,23 +249,26 @@ def main():
                                         instance.id,
                                         floatip_client):
             logger.error("Cannot associate floating IP to VM.")
+            iterator += 1
             continue
 
     # 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.")
+            iterator += 1
             continue
         # Retrieve IP of INSTANCE
         instance_ip_2 = instance_2.networks.get(NET_NAME)[0]
@@ -234,6 +286,7 @@ def main():
 
         if floatip_server is None:
             logger.error("Cannot create floating IP.")
+            iterator += 1
             continue
         logger.info("Floating IP created: '%s'" % floatip_server)
 
@@ -244,6 +297,7 @@ def main():
                                         instance_2.id,
                                         floatip_server):
             logger.error("Cannot associate floating IP to VM.")
+            iterator += 1
             continue
 
         # CREATION OF THE 2 SF ####
@@ -309,6 +363,7 @@ def main():
 
         if not all(check):
             logger.error("Cannot establish SSH connection to the SFs")
+            iterator += 1
             continue
 
         logger.info("SSH connectivity to the SFs established")
@@ -372,20 +427,11 @@ 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
         logger.info("TEST STARTED")
+        time.sleep(70)
         try:
             ssh.connect(floatip_client, username="root",
                         password="opnfv", timeout=2)
@@ -399,8 +445,14 @@ def main():
                 i = i + 1
                 json_results.update({"Test 1: SSH Blocked": "Passed"})
             else:
-                logger.error('\033[91m' + "TEST 1 [FAILED] "
-                             "==> SSH NOT BLOCKED" + '\033[0m')
+                timestamp = time.strftime("%Y%m%d-%H%M%S")
+                error = ('\033[91m' + "TEST 1 [FAILED] "
+                         "==> SSH NOT BLOCKED" + '\033[0m')
+                logger.error(error)
+                ovs_logger.dump_ovs_logs(controller_clients,
+                                         compute_clients,
+                                         related_error=error,
+                                         timestamp=timestamp)
                 status = "FAIL"
                 json_results.update({"Test 1: SSH Blocked": "Failed"})
                 failures += 1
@@ -422,8 +474,14 @@ def main():
                 i = i + 1
                 json_results.update({"Test 2: HTTP works": "Passed"})
             else:
-                logger.error('\033[91m' + "TEST 2 [FAILED] "
-                             "==> HTTP BLOCKED" + '\033[0m')
+                timestamp = time.strftime("%Y%m%d-%H%M%S")
+                error = ('\033[91m' + "TEST 2 [FAILED] "
+                         "==> HTTP BLOCKED" + '\033[0m')
+                logger.error(error)
+                ovs_logger.dump_ovs_logs(controller_clients,
+                                         compute_clients,
+                                         related_error=error,
+                                         timestamp=timestamp)
                 status = "FAIL"
                 json_results.update({"Test 2: HTTP works": "Failed"})
                 failures += 1
@@ -439,17 +497,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
 
@@ -465,8 +513,14 @@ def main():
                 i = i + 1
                 json_results.update({"Test 3: HTTP Blocked": "Passed"})
             else:
-                logger.error('\033[91m' + "TEST 3 [FAILED] "
-                             "==> HTTP NOT BLOCKED" + '\033[0m')
+                timestamp = time.strftime("%Y%m%d-%H%M%S")
+                error = ('\033[91m' + "TEST 3 [FAILED] "
+                         "==> HTTP NOT BLOCKED" + '\033[0m')
+                logger.error(error)
+                ovs_logger.dump_ovs_logs(controller_clients,
+                                         compute_clients,
+                                         related_error=error,
+                                         timestamp=timestamp)
                 status = "FAIL"
                 json_results.update({"Test 3: HTTP Blocked": "Failed"})
                 failures += 1
@@ -488,8 +542,14 @@ def main():
                 i = i + 1
                 json_results.update({"Test 4: SSH works": "Passed"})
             else:
-                logger.error('\033[91m' + "TEST 4 [FAILED] "
-                             "==> SSH BLOCKED" + '\033[0m')
+                timestamp = time.strftime("%Y%m%d-%H%M%S")
+                error = ('\033[91m' + "TEST 4 [FAILED] "
+                         "==> SSH BLOCKED" + '\033[0m')
+                logger.error(error)
+                ovs_logger.dump_ovs_logs(controller_clients,
+                                         compute_clients,
+                                         related_error=error,
+                                         timestamp=timestamp)
                 status = "FAIL"
                 json_results.update({"Test 4: SSH works": "Failed"})
                 failures += 1
@@ -498,6 +558,8 @@ def main():
             time.sleep(6)
             # timeout -= 1
 
+        ovs_logger.create_artifact_archive()
+
         iterator += 1
         if i == 4:
             for x in range(0, 5):
@@ -505,10 +567,15 @@ def main():
                             " :) \n" + '\033[0m')
             break
         else:
-            logger.debug("Iterating again!")
-            delete = ("bash delete.sh")
-            subprocess.call(delete, shell=True, stderr=subprocess.PIPE)
-            time.sleep(10)
+            logger.info("Iterating again!")
+            delete = "bash %s/testcases/features/sfc/delete.sh" % \
+                     (FUNCTEST_REPO)
+            try:
+                subprocess.call(delete, shell=True, stderr=subprocess.PIPE)
+                time.sleep(10)
+            except Exception, e:
+                logger.error("Problem when executing the delete.sh")
+                logger.error("Problem %s" % e)
 
     if args.report:
         stop_time = time.time()