Merge "Replace glance client calls with openstack sdk"
[sdnvpn.git] / sdnvpn / test / functest / testcase_10.py
index cbad82d..78d5684 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python
 #
 # Copyright (c) 2017 All rights reserved
 # This program and the accompanying materials
@@ -8,26 +8,17 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 #
 
-import argparse
 import logging
 import re
 import sys
 import time
 
-from functest.utils import openstack_utils as os_utils
 from multiprocessing import Process, Manager, Lock
 from sdnvpn.lib import config as sdnvpn_config
+from sdnvpn.lib import openstack_utils as os_utils
 from sdnvpn.lib import utils as test_utils
 from sdnvpn.lib.results import Results
 
-parser = argparse.ArgumentParser()
-
-parser.add_argument("-r", "--report",
-                    help="Create json result file",
-                    action="store_true")
-
-args = parser.parse_args()
-
 logger = logging.getLogger('__name__')
 
 std_out_lock = Lock()
@@ -40,7 +31,7 @@ TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
 def monitor(in_data, out_data, vm):
     # At the beginning of ping we might have some
     # failures, so we ignore the first 10 pings
-    lines_offset = 10
+    lines_offset = 20
     while in_data["stop_thread"] is False:
         try:
             time.sleep(1)
@@ -54,20 +45,20 @@ def monitor(in_data, out_data, vm):
                                                   format(vm.name))
                         # Atomic write to std out
                         with std_out_lock:
-                            logging.error("Failure during ping from "
-                                          "instance {}: {}".
-                                          format(vm.name, console_line))
+                            logger.error("Failure during ping from "
+                                         "instance {}: {}".
+                                         format(vm.name, console_line))
                     elif re.match(r'ping.*OK', console_line):
                         # Atomic write to std out
                         with std_out_lock:
-                            logging.info("Ping from instance {}: {}".
-                                         format(vm.name, console_line))
+                            logger.info("Ping from instance {}: {}".
+                                        format(vm.name, console_line))
                 lines_offset = len(vm_console_out_lines)
         except:
             # Atomic write to std out
             with std_out_lock:
-                logging.error("Failure in monitor_thread of instance {}".
-                              format(vm.name))
+                logger.error("Failure in monitor_thread of instance {}".
+                             format(vm.name))
     # Return to main process
     return
 
@@ -81,11 +72,11 @@ def main():
 
     nova_client = os_utils.get_nova_client()
     neutron_client = os_utils.get_neutron_client()
-    glance_client = os_utils.get_glance_client()
+    conn = os_utils.get_os_connection()
 
     (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
      subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
-    image_id = os_utils.create_glance_image(glance_client,
+    image_id = os_utils.create_glance_image(conn,
                                             TESTCASE_CONFIG.image_name,
                                             COMMON_CONFIG.image_path,
                                             disk=COMMON_CONFIG.image_format,
@@ -122,7 +113,7 @@ def main():
         compute_node=av_zone_1)
     vm2_ip = test_utils.get_instance_ip(vm_2)
 
-    u1 = test_utils.generate_ping_userdata([vm2_ip], 1)
+    u1 = test_utils.generate_ping_userdata([vm2_ip])
     vm_1 = test_utils.create_instance(
         nova_client,
         TESTCASE_CONFIG.instance_1_name,
@@ -134,7 +125,7 @@ def main():
         userdata=u1)
     vm1_ip = test_utils.get_instance_ip(vm_1)
 
-    u3 = test_utils.generate_ping_userdata([vm1_ip, vm2_ip], 1)
+    u3 = test_utils.generate_ping_userdata([vm1_ip, vm2_ip])
     vm_3 = test_utils.create_instance(
         nova_client,
         TESTCASE_CONFIG.instance_3_name,
@@ -150,11 +141,11 @@ def main():
     instance_ids.extend([vm_1.id, vm_3.id])
 
     # Wait for VMs to get ips.
-    instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
-                                                    vm_3)
+    instances_up = test_utils.wait_for_instances_up(vm_2)
+    instances_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_1, vm_3)
 
-    if not instances_up:
-        logger.error("One or more instances is down")
+    if (not instances_up or not instances_dhcp_up):
+        logger.error("One or more instances are down")
         # TODO: Handle this appropriately
     # Create monitor threads to monitor traffic between vm_1, vm_2 and vm_3
     m = Manager()
@@ -181,11 +172,11 @@ def main():
     thread_inputs = [monitor_input1, monitor_input2, monitor_input3]
     thread_outputs = [monitor_output1, monitor_output2, monitor_output3]
     try:
-        logging.info("Starting all monitor threads")
+        logger.info("Starting all monitor threads")
         # Start all monitor threads
         for thread in threads:
             thread.start()
-        logging.info("Wait before subtest")
+        logger.info("Wait before subtest")
         test_utils.wait_before_subtest()
         monitor_err_msg = ""
         for thread_output in thread_outputs:
@@ -201,11 +192,18 @@ def main():
         # Stop monitor thread 2 and delete instance vm_2
         thread_inputs[1]["stop_thread"] = True
         if not os_utils.delete_instance(nova_client, vm_2.id):
-            logging.error("Fail to delete vm_2 instance during "
-                          "testing process")
+            logger.error("Fail to delete vm_2 instance during "
+                         "testing process")
             raise Exception("Fail to delete instance vm_2.")
+        for thread_input in thread_inputs:
+            thread_input["stop_thread"] = True
+        for thread in threads:
+            thread.join()
+        threads = []
+        thread_inputs = []
+        thread_outputs = []
         # Create a new vm (vm_4) on compute 1 node
-        u4 = test_utils.generate_ping_userdata([vm1_ip, vm3_ip], 1)
+        u4 = test_utils.generate_ping_userdata([vm1_ip, vm3_ip])
         vm_4 = test_utils.create_instance(
             nova_client,
             TESTCASE_CONFIG.instance_4_name,
@@ -216,9 +214,10 @@ def main():
             compute_node=av_zone_1,
             userdata=u4)
         instance_ids.append(vm_4.id)
+
         # Wait for VMs to get ips.
-        instances_up = test_utils.wait_for_instances_up(vm_4)
-        if not instances_up:
+        instances_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_4)
+        if not instances_dhcp_up:
             logger.error("Instance vm_4 failed to start.")
             # TODO: Handle this appropriately
         # Create and start a new monitor thread for vm_4
@@ -232,8 +231,8 @@ def main():
         threads.append(monitor_thread4)
         thread_inputs.append(monitor_input4)
         thread_outputs.append(monitor_output4)
-        logging.info("Starting monitor thread of vm_4")
-        threads[3].start()
+        logger.info("Starting monitor thread of vm_4")
+        threads[0].start()
         test_utils.wait_before_subtest()
         monitor_err_msg = ""
         for thread_output in thread_outputs:
@@ -253,14 +252,15 @@ def main():
         raise
     finally:
         # Give a stop signal to all threads
-        logging.info("Sending stop signal to monitor thread")
+        logger.info("Sending stop signal to monitor thread")
         for thread_input in thread_inputs:
             thread_input["stop_thread"] = True
         # Wait for all threads to stop and return to the main process
         for thread in threads:
             thread.join()
 
-        test_utils.cleanup_nova(nova_client, instance_ids, image_ids)
+        test_utils.cleanup_nova(nova_client, instance_ids)
+        test_utils.cleanup_glance(conn, image_ids)
         test_utils.cleanup_neutron(neutron_client, floatingip_ids, bgpvpn_ids,
                                    interfaces, subnet_ids, router_ids,
                                    network_ids)
@@ -269,5 +269,4 @@ def main():
 
 
 if __name__ == '__main__':
-    logging.basicConfig(level=logging.INFO)
     sys.exit(main())