config_functest.py:
[functest.git] / testcases / config_functest.py
index 05376be..7602004 100644 (file)
@@ -62,6 +62,7 @@ f.close()
 
 
 """ global variables """
+# Directories
 HOME = os.environ['HOME']+"/"
 FUNCTEST_BASE_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_functest")
 RALLY_REPO_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_rally_repo")
@@ -70,26 +71,39 @@ RALLY_INSTALLATION_DIR = HOME + functest_yaml.get("general").get("directories").
 BENCH_TESTS_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_rally_scn")
 VPING_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_vping")
 ODL_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_odl")
-NEUTRON_PUBLIC_NAME = functest_yaml.get("general").get("openstack").get("neutron_public_net_name")
-NEUTRON_NET_NAME = functest_yaml.get("general").get("openstack").get("neutron_net_name")
-NEUTRON_SUBNET_NAME = functest_yaml.get("general").get("openstack").get("neutron_subnet_name")
-NEUTRON_RANGE = functest_yaml.get("general").get("openstack").get("neutron_subnet_range")
+
+# NEUTRON Public Network parameters
+NEUTRON_PUBLIC_NET_NAME = functest_yaml.get("general").get("openstack").get("neutron_public_net_name")
+NEUTRON_PUBLIC_SUBNET_NAME = functest_yaml.get("general").get("openstack").get("neutron_public_subnet_name")
+NEUTRON_PUBLIC_SUBNET_CIDR = functest_yaml.get("general").get("openstack").get("neutron_public_subnet_cidr")
+NEUTRON_PUBLIC_SUBNET_START = functest_yaml.get("general").get("openstack").get("neutron_public_subnet_start")
+NEUTRON_PUBLIC_SUBNET_END = functest_yaml.get("general").get("openstack").get("neutron_public_subnet_end")
+
+# NEUTRON Private Network parameters
+NEUTRON_PRIVATE_NET_NAME = functest_yaml.get("general").get("openstack").get("neutron_private_net_name")
+NEUTRON_PRIVATE_SUBNET_NAME = functest_yaml.get("general").get("openstack").get("neutron_private_subnet_name")
+NEUTRON_PRIVATE_SUBNET_CIDR = functest_yaml.get("general").get("openstack").get("neutron_private_subnet_cidr")
 ROUTER_NAME = functest_yaml.get("general").get("openstack").get("neutron_router_name")
 
+#GLANCE image parameters
 IMAGE_URL = functest_yaml.get("general").get("openstack").get("image_url")
 IMAGE_DISK_FORMAT = functest_yaml.get("general").get("openstack").get("image_disk_format")
 IMAGE_NAME = functest_yaml.get("general").get("openstack").get("image_name")
 IMAGE_FILE_NAME = IMAGE_URL.rsplit('/')[-1]
 IMAGE_DOWNLOAD_PATH = FUNCTEST_BASE_DIR + IMAGE_FILE_NAME
 
+credentials = None
+neutron_client = None
 
 def config_functest_start():
     """
     Start the functest environment installation
     """
-    #if config_functest_check():
-    #    logger.info("Functest environment already installed in %s. Nothing to do." %FUNCTEST_BASE_DIR)
-    #    exit(0)
+
+    if config_functest_check():
+        logger.info("Functest environment already installed in %s. Nothing to do." %FUNCTEST_BASE_DIR)
+        exit(0)
+
     if not check_internet_connectivity():
         logger.error("There is no Internet connectivity. Please check the network configuration.")
         exit(-1)
@@ -97,14 +111,10 @@ def config_functest_start():
         logger.error("Please source the openrc credentials and run the script again.")
         #TODO: source the credentials in this script
         exit(-1)
-    elif not check_neutron_net(NEUTRON_PUBLIC_NAME):
-        #The public network is normally created by default, no need to create a new one
-        logger.debug("Public network '%s' not found." % NEUTRON_PUBLIC_NAME)
-        logger.error("A public Neutron network is needed for the environment. Please create one.")
-        #TODO: source the credentials in this script
-        exit(-1)
+
     else:
         # Clean in case there are left overs
+
         config_functest_clean()
 
         logger.info("Starting installationg of functest environment in %s" % FUNCTEST_BASE_DIR)
@@ -131,15 +141,29 @@ def config_functest_start():
             config_functest_clean()
             exit(-1)
 
-        logger.info("Creating a private Neutron network...")
-        logger.debug("Checking if private network '%s' exists..." % NEUTRON_NET_NAME)
-        #Now: if exists we don't create it again (the clean command does not clean the neutron network)
-        #TODO: this check will not be needed when cleaning the neutron is implemented
-        if check_neutron_net(NEUTRON_NET_NAME):
-            logger.info("Private network '%s' found. No need to create another one." % NEUTRON_NET_NAME)
+        credentials = get_credentials()
+        neutron_client = client.Client(**credentials)
+
+        logger.info("Configuring Neutron...")
+        logger.info("Checking if public network '%s' exists..." % NEUTRON_PUBLIC_NET_NAME)
+        #Now: if exists we don't create it again (the clean command does not clean the neutron networks yet)
+        if check_neutron_net(neutron_client, NEUTRON_PUBLIC_NET_NAME):
+            logger.info("Public network '%s' found. No need to create another one." % NEUTRON_PUBLIC_NET_NAME)
         else:
-            logger.info("Private network '%s' not found. Creating..." % NEUTRON_NET_NAME)
-            if not create_neutron_net():
+            logger.info("Public network '%s' not found. Creating..." % NEUTRON_PUBLIC_NET_NAME)
+            if not create_public_neutron_net(neutron_client):
+                logger.error("There has been a problem while creating the Neutron network.")
+                #config_functest_clean()
+                exit(-1)
+
+
+        logger.info("Checking if private network '%s' exists..." % NEUTRON_PRIVATE_NET_NAME)
+        #Now: if exists we don't create it again (the clean command does not clean the neutron networks yet)
+        if check_neutron_net(neutron_client, NEUTRON_PRIVATE_NET_NAME):
+            logger.info("Private network '%s' found. No need to create another one." % NEUTRON_PRIVATE_NET_NAME)
+        else:
+            logger.info("Private network '%s' not found. Creating..." % NEUTRON_PRIVATE_NET_NAME)
+            if not create_private_neutron_net(neutron_client):
                 logger.error("There has been a problem while creating the Neutron network.")
                 #config_functest_clean()
                 exit(-1)
@@ -168,7 +192,8 @@ def config_functest_check():
     errors_all = False
 
     logger.info("Checking current functest configuration...")
-
+    credentials = get_credentials()
+    neutron_client = client.Client(**credentials)
 
     logger.debug("Checking directories...")
     errors = False
@@ -197,19 +222,19 @@ def config_functest_check():
 
     logger.debug("Checking Neutron...")
     errors = False
-    if not check_neutron_net(NEUTRON_NET_NAME):
-        logger.debug("   Private network '%s' NOT found." % NEUTRON_NET_NAME)
+    if not check_neutron_net(neutron_client, NEUTRON_PRIVATE_NET_NAME):
+        logger.debug("   Private network '%s' NOT found." % NEUTRON_PRIVATE_NET_NAME)
         errors = True
         errors_all = True
     else:
-        logger.debug("   Private network '%s' found." % NEUTRON_NET_NAME)
+        logger.debug("   Private network '%s' found." % NEUTRON_PRIVATE_NET_NAME)
 
-    if not check_neutron_net(NEUTRON_PUBLIC_NAME):
-        logger.debug("   Public network '%s' NOT found." % NEUTRON_PUBLIC_NAME)
+    if not check_neutron_net(neutron_client, NEUTRON_PUBLIC_NET_NAME):
+        logger.debug("   Public network '%s' NOT found." % NEUTRON_PUBLIC_NET_NAME)
         errors = True
         errors_all = True
     else:
-        logger.debug("   Public network '%s' found." % NEUTRON_PUBLIC_NAME)
+        logger.debug("   Public network '%s' found." % NEUTRON_PUBLIC_NET_NAME)
 
     if not errors:
         logger.debug("...OK")
@@ -267,7 +292,7 @@ def config_functest_clean():
         cmd = "sudo rm -rf " + FUNCTEST_BASE_DIR #need to be sudo, not possible with rmtree
         execute_command(cmd)
 
-    #logger.debug("Deleting Neutron network %s" % NEUTRON_NET_NAME)
+    #logger.debug("Deleting Neutron network %s" % NEUTRON_PRIVATE_NET_NAME)
     #if not delete_neutron_net() :
     #    logger.error("Error deleting the network. Remove it manually.")
 
@@ -402,10 +427,11 @@ def download_tests():
     os.makedirs(ODL_DIR)
     os.makedirs(BENCH_TESTS_DIR)
 
-    logger.info("Downloading functest.yaml...")
-    yaml_url = 'https://git.opnfv.org/cgit/functest/plain/testcases/functest.yaml'
-    if not download_url(yaml_url,FUNCTEST_BASE_DIR):
-        logger.error("Unable to download the configuration file functest.yaml")
+    logger.info("Copying functest.yaml to functest environment...")
+    try:
+        shutil.copy("./functest.yaml", FUNCTEST_BASE_DIR+"functest.yaml")
+    except:
+        print "Error copying the file:", sys.exc_info()[0]
         return False
 
     logger.info("Downloading vPing test...")
@@ -438,14 +464,33 @@ def download_tests():
 
     return True
 
+def create_public_neutron_net(neutron):
+    try:
+        neutron.format = 'json'
+        logger.debug('Creating Neutron network %s...' % NEUTRON_PUBLIC_NET_NAME)
+        json_body = {'network': {'name': NEUTRON_PUBLIC_NET_NAME, 'admin_state_up': True, 'router:external': True}}
+        netw = neutron.create_network(body=json_body)
+        net_dict = netw['network']
+        network_id = net_dict['id']
+        logger.debug("Network '%s' created successfully" % network_id)
+
+        logger.debug('Creating Subnet....')
+        json_body = {'subnets': [{'cidr': NEUTRON_PUBLIC_SUBNET_CIDR,
+                           'ip_version': 4, 'network_id': network_id}]}
+        subnet = neutron.create_subnet(body=json_body)
+        logger.debug("Subnet '%s' created successfully" % subnet)
+    except:
+        print "Error:", sys.exc_info()[0]
+        return False
 
-def create_neutron_net():
-    credentials = get_credentials()
-    neutron = client.Client(**credentials)
+    logger.info("Public Neutron network created successfully.")
+    return True
+
+def create_private_neutron_net(neutron):
     try:
         neutron.format = 'json'
-        logger.debug('Creating Neutron network %s...' % NEUTRON_NET_NAME)
-        json_body = {'network': {'name': NEUTRON_NET_NAME,
+        logger.debug('Creating Neutron network %s...' % NEUTRON_PRIVATE_NET_NAME)
+        json_body = {'network': {'name': NEUTRON_PRIVATE_NET_NAME,
                     'admin_state_up': True}}
         netw = neutron.create_network(body=json_body)
         net_dict = netw['network']
@@ -453,7 +498,7 @@ def create_neutron_net():
         logger.debug("Network '%s' created successfully" % network_id)
 
         logger.debug('Creating Subnet....')
-        json_body = {'subnets': [{'cidr': NEUTRON_RANGE,
+        json_body = {'subnets': [{'cidr': NEUTRON_PRIVATE_SUBNET_CIDR,
                            'ip_version': 4, 'network_id': network_id}]}
         subnet = neutron.create_subnet(body=json_body)
         logger.debug("Subnet '%s' created successfully" % subnet)
@@ -475,21 +520,19 @@ def create_neutron_net():
         logger.debug("Port created successfully.")
 
         logger.debug('Setting up gateway...')
-        public_network_id = get_network_id(neutron,NEUTRON_PUBLIC_NAME)
+        public_network_id = get_network_id(neutron,NEUTRON_PUBLIC_NET_NAME)
         json_body = {'network_id': public_network_id, 'enable_snat' :  True}
         gateway = neutron.add_gateway_router(router_id,body=json_body)
         logger.debug("Gateway '%s' added successfully" % gateway)
     except:
-        logger.error("There has been a problem when creating the Neutron network.")
+        print "Error:", sys.exc_info()[0]
         return False
-    finally:
-        logger.info("Neutron network created successfully.")
-        return True
 
-    return False
+    logger.info("Private Neutron network created successfully.")
+    return True
 
-def get_network_id(neutron_client, network_name):
-    networks = neutron_client.list_networks()['networks']
+def get_network_id(neutron, network_name):
+    networks = neutron.list_networks()['networks']
     id  = ''
     for n in networks:
         if n['name'] == network_name:
@@ -497,23 +540,19 @@ def get_network_id(neutron_client, network_name):
             break
     return id
 
-def check_neutron_net(net_name):
-    credentials = get_credentials()
-    neutron = client.Client(**credentials)
+def check_neutron_net(neutron, net_name):
     for network in neutron.list_networks()['networks']:
         if network['name'] == net_name :
             for subnet in network['subnets']:
                 return True
     return False
 
-def delete_neutron_net():
+def delete_neutron_net(neutron):
     #TODO: remove router, ports
-    credentials = get_credentials()
-    neutron = client.Client(**credentials)
     try:
         #https://github.com/isginf/openstack_tools/blob/master/openstack_remove_tenant.py
         for network in neutron.list_networks()['networks']:
-            if network['name'] == NEUTRON_NET_NAME :
+            if network['name'] == NEUTRON_PRIVATE_NET_NAME :
                 for subnet in network['subnets']:
                     print "Deleting subnet " + subnet
                     neutron.delete_subnet(subnet)