diff options
author | jose.lausuch <jose.lausuch@ericsson.com> | 2015-05-29 17:09:22 +0200 |
---|---|---|
committer | jose.lausuch <jose.lausuch@ericsson.com> | 2015-05-30 19:56:34 +0200 |
commit | 73e6984c977287b263f7cbd81bdee0bf7cd8abb5 (patch) | |
tree | 061e4b3715685dc7545a808c7ca69989eb55ca26 /testcases/vPing/CI | |
parent | 486dfd57300d74d0a9f058951112393bf6bc210a (diff) |
Improvements
- Added: functest_utils.py for common util functions
- Moved: a lot of functions from config_functest.py to functest_utils.py
- Moved: create of functest-net from config_functest.py to vPing.py
- Improvements and code cleaned
JIRA: FUNCTEST-10
Change-Id: I0d3381576cbb24a999ea69e107baafea896a36e7
Signed-off-by: jose.lausuch <jose.lausuch@ericsson.com>
Diffstat (limited to 'testcases/vPing/CI')
-rw-r--r-- | testcases/vPing/CI/libraries/vPing.py | 228 |
1 files changed, 142 insertions, 86 deletions
diff --git a/testcases/vPing/CI/libraries/vPing.py b/testcases/vPing/CI/libraries/vPing.py index e1415535..379bf969 100644 --- a/testcases/vPing/CI/libraries/vPing.py +++ b/testcases/vPing/CI/libraries/vPing.py @@ -13,9 +13,10 @@ # Note: this is script works only with Ubuntu image, not with Cirros image # -import os, time, subprocess, logging, argparse, yaml -import pprint +import os, time, subprocess, logging, argparse, yaml, pprint, sys import novaclient.v2.client as novaclient +from neutronclient.v2_0 import client as neutronclient + pp = pprint.PrettyPrinter(indent=4) @@ -24,6 +25,9 @@ parser.add_argument("repo_path", help="Path to the repository") parser.add_argument("-d", "--debug", help="Debug mode", action="store_true") args = parser.parse_args() +sys.path.append(args.repo_path + "testcases/") +import functest_utils + """ logging configuration """ logger = logging.getLogger('vPing') logger.setLevel(logging.DEBUG) @@ -43,66 +47,33 @@ with open(args.repo_path+"testcases/config_functest.yaml") as f: functest_yaml = yaml.safe_load(f) f.close() +# vPing parameters VM_BOOT_TIMEOUT = 180 +VM_DELETE_TIMEOUT = 100 PING_TIMEOUT = functest_yaml.get("vping").get("ping_timeout") NAME_VM_1 = functest_yaml.get("vping").get("vm_name_1") NAME_VM_2 = functest_yaml.get("vping").get("vm_name_2") GLANCE_IMAGE_NAME = functest_yaml.get("general").get("openstack").get("image_name") -NEUTRON_PRIVATE_NET_NAME = functest_yaml.get("general").get("openstack").get("neutron_private_net_name") FLAVOR = functest_yaml.get("vping").get("vm_flavor") +# 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") +NEUTRON_ROUTER_NAME = functest_yaml.get("general").get("openstack").get("neutron_router_name") + def pMsg(value): """pretty printing""" pp.pprint(value) -def print_title(title): - """Print titles""" - print "\n"+"#"*40+"\n# "+title+"\n"+"#"*40+"\n" - -def get_credentials(service): - """Returns a creds dictionary filled with the following keys: - * username - * password/api_key (depending on the service) - * tenant_name/project_id (depending on the service) - * auth_url - :param service: a string indicating the name of the service - requesting the credentials. - """ - #TODO: get credentials from the openrc file - creds = {} - # Unfortunately, each of the OpenStack client will request slightly - # different entries in their credentials dict. - if service.lower() in ("nova", "cinder"): - password = "api_key" - tenant = "project_id" - else: - password = "password" - tenant = "tenant_name" - - # The most common way to pass these info to the script is to do it through - # environment variables. - creds.update({ - "username": os.environ.get('OS_USERNAME', "admin"), # add your cloud username details - password: os.environ.get("OS_PASSWORD", 'admin'), # add password - "auth_url": os.environ.get("OS_AUTH_URL","http://192.168.20.71:5000/v2.0"), # Auth URL - tenant: os.environ.get("OS_TENANT_NAME", "admin"), - }) - - return creds - - -def get_server(creds, servername): - nova = novaclient.Client(**creds) - return nova.servers.find(name=servername) - def waitVmActive(nova,vm): # sleep and wait for VM status change sleep_time = 3 count = VM_BOOT_TIMEOUT / sleep_time while True: - status = get_status(nova,vm) + status = functest_utils.get_instance_status(nova,vm) logger.debug("Status: %s" % status) if status == "ACTIVE": return True @@ -112,14 +83,106 @@ def waitVmActive(nova,vm): time.sleep(sleep_time) return False -def get_status(nova,vm): - vm = nova.servers.get(vm.id) - return vm.status +def waitVmDeleted(nova,vm): + # sleep and wait for VM status change + sleep_time = 3 + count = VM_DELETE_TIMEOUT / sleep_time + while True: + status = functest_utils.get_instance_status(nova,vm) + if not status: + return True + elif count == 0: + logger.debug("Timeout") + return False + else: + #return False + count-=1 + time.sleep(sleep_time) + return False + + +def create_private_neutron_net(neutron): + neutron.format = 'json' + logger.info('Creating neutron network %s...' % NEUTRON_PRIVATE_NET_NAME) + network_id = functest_utils.create_neutron_net(neutron, NEUTRON_PRIVATE_NET_NAME) + if not network_id: + return False + logger.debug("Network '%s' created successfully" % network_id) + + logger.debug('Creating Subnet....') + subnet_id = functest_utils.create_neutron_subnet(neutron, NEUTRON_PRIVATE_SUBNET_NAME, NEUTRON_PRIVATE_SUBNET_CIDR, network_id) + if not subnet_id: + return False + logger.debug("Subnet '%s' created successfully" % subnet_id) + + logger.debug('Creating Router...') + router_id = functest_utils.create_neutron_router(neutron, NEUTRON_ROUTER_NAME) + if not router_id: + return False + logger.debug("Router '%s' created successfully" % router_id) + + logger.debug('Adding router to subnet...') + result = functest_utils.add_interface_router(neutron, router_id, subnet_id) + if not result: + return False + logger.debug("Interface added successfully.") + + network_dic = {'net_id' : network_id, + 'subnet_id' : subnet_id, + 'router_id' : router_id} + return network_dic + + +def cleanup(nova,neutron,network_dic): + # delete both VMs + logger.info("Deleting Instances...") + logger.debug("Deleting '%s'..." %NAME_VM_1) + vm1 = nova.servers.find(name=NAME_VM_1) + nova.servers.delete(vm1) + #wait until VMs are deleted + if not waitVmDeleted(nova,vm1): + logger.error("Instance '%s' with cannot be deleted. Status is '%s'" % (NAME_VM_1,functest_utils.get_instance_status(nova_client,vm1))) + else: + logger.debug("Instance %s terminated." % NAME_VM_1) + + logger.debug("Deleting '%s'..." %NAME_VM_2) + vm2 = nova.servers.find(name=NAME_VM_2) + nova.servers.delete(vm2) + if not waitVmDeleted(nova,vm2): + logger.error("Instance '%s' with cannot be deleted. Status is '%s'" % (NAME_VM_2,functest_utils.get_instance_status(nova_client,vm2))) + else: + logger.debug("Instance %s terminated." % NAME_VM_2) + + # delete created network + logger.info("Deleting network '%s'..." % NEUTRON_PRIVATE_NET_NAME) + net_id=network_dic["net_id"] + subnet_id=network_dic["subnet_id"] + router_id=network_dic["router_id"] + if not functest_utils.remove_interface_router(neutron, router_id, subnet_id): + logger.error("Unable to remove subnet '%s' from router '%s'" %(subnet_id,router_id)) + return False + logger.debug("Interface removed successfully") + if not functest_utils.delete_neutron_router(neutron, router_id): + logger.error("Unable to delete router '%s'" %router_id) + return False + logger.debug("Router deleted successfully") + if not functest_utils.delete_neutron_subnet(neutron, subnet_id): + logger.error("Unable to delete subnet '%s'" %subnet_id) + return False + logger.debug("Subnet '%s' deleted successfully" %NEUTRON_PRIVATE_SUBNET_NAME) + if not functest_utils.delete_neutron_net(neutron, net_id): + logger.error("Unable to delete network '%s'" %net_id) + return False + logger.debug("Network '%s' deleted successfully" %NEUTRON_PRIVATE_NET_NAME) + return True + def main(): - creds = get_credentials("nova") - nova = novaclient.Client(**creds) + creds_nova = functest_utils.get_credentials("nova") + nova_client = novaclient.Client(**creds_nova) + creds_neutron = functest_utils.get_credentials("neutron") + neutron_client = neutronclient.Client(**creds_neutron) EXIT_CODE = -1 image = None network = None @@ -127,69 +190,66 @@ def main(): # Check if the given image exists try: - image = nova.images.find(name = GLANCE_IMAGE_NAME) + image = nova_client.images.find(name = GLANCE_IMAGE_NAME) logger.info("Glance image found '%s'" % GLANCE_IMAGE_NAME) except: logger.error("ERROR: Glance image '%s' not found." % GLANCE_IMAGE_NAME) logger.info("Available images are: ") - pMsg(nova.images.list()) + pMsg(nova_client.images.list()) exit(-1) - # Check if the given neutron network exists - try: - network = nova.networks.find(label = NEUTRON_PRIVATE_NET_NAME) - logger.info("Network found '%s'" % NEUTRON_PRIVATE_NET_NAME) - except: - logger.error("Neutron network '%s' not found." % NEUTRON_PRIVATE_NET_NAME) - logger.info("Available networks are: ") - pMsg(nova.networks.list()) + network_dic = create_private_neutron_net(neutron_client) + if not network_dic: + logger.error("There has been a problem when creating the neutron network") exit(-1) + network_id = network_dic["net_id"] + # Check if the given flavor exists try: - flavor = nova.flavors.find(name = FLAVOR) + flavor = nova_client.flavors.find(name = FLAVOR) logger.info("Flavor found '%s'" % FLAVOR) except: logger.error("Flavor '%s' not found." % FLAVOR) logger.info("Available flavors are: ") - pMsg(nova.flavor.list()) + pMsg(nova_client.flavor.list()) exit(-1) # Deleting instances if they exist - servers=nova.servers.list() + servers=nova_client.servers.list() for server in servers: if server.name == NAME_VM_1 or server.name == NAME_VM_2: logger.info("Instance %s found. Deleting..." %server.name) server.delete() - # boot VM 1 # basic boot # tune (e.g. flavor, images, network) to your specific openstack configuration here # create VM logger.info("Creating instance '%s'..." % NAME_VM_1) - logger.debug("Configuration:\n name=%s \n flavor=%s \n image=%s \n network=%s \n" %(NAME_VM_1,flavor,image,network)) - vm1 = nova.servers.create( + logger.debug("Configuration:\n name=%s \n flavor=%s \n image=%s \n network=%s \n" %(NAME_VM_1,flavor,image,network_id)) + vm1 = nova_client.servers.create( name = NAME_VM_1, flavor = flavor, image = image, - nics = [{"net-id": network.id}] + nics = [{"net-id": network_id}] ) #wait until VM status is active - if not waitVmActive(nova,vm1): - logger.error("Instance '%s' cannot be booted. Status is '%s'" % (NAME_VM_1,get_status(nova,vm1))) + if not waitVmActive(nova_client,vm1): + logger.error("Instance '%s' cannot be booted. Status is '%s'" % (NAME_VM_1,functest_utils.get_instance_status(nova_client,vm1))) + cleanup(nova_client,neutron_client,network_dic) return (EXIT_CODE) else: logger.info("Instance '%s' is ACTIVE." % NAME_VM_1) #retrieve IP of first VM logger.debug("Fetching IP...") - server = get_server(creds, NAME_VM_1) + server = functest_utils.get_instance_by_name(nova_client, NAME_VM_1) # theoretically there is only one IP address so we take the first element of the table # Dangerous! To be improved! test_ip = server.networks.get(NEUTRON_PRIVATE_NET_NAME)[0] @@ -203,21 +263,23 @@ def main(): # create VM logger.info("Creating instance '%s'..." % NAME_VM_2) - logger.debug("Configuration:\n name=%s \n flavor=%s \n image=%s \n network=%s \n userdata= \n%s" %(NAME_VM_2,flavor,image,network,u)) - vm2 = nova.servers.create( + logger.debug("Configuration:\n name=%s \n flavor=%s \n image=%s \n network=%s \n userdata= \n%s" %(NAME_VM_2,flavor,image,network_id,u)) + vm2 = nova_client.servers.create( name = NAME_VM_2, flavor = flavor, image = image, - nics = [{"net-id": network.id}], + nics = [{"net-id": network_id}], userdata = u, ) - if not waitVmActive(nova,vm2): - logger.error("Instance '%s' cannot be booted. Status is '%s'" % (NAME_VM_2,get_status(nova,vm2))) + if not waitVmActive(nova_client,vm2): + logger.error("Instance '%s' cannot be booted. Status is '%s'" % (NAME_VM_2,functest_utils.get_instance_status(nova_client,vm2))) + cleanup(nova_client,neutron_client,network_dic) return (EXIT_CODE) else: logger.info("Instance '%s' is ACTIVE." % NAME_VM_2) + logger.info("Waiting for ping...") sec = 0 console_log = vm2.get_console_output() while True: @@ -226,23 +288,17 @@ def main(): #print "--"+console_log # report if the test is failed if "vPing OK" in console_log: - logger.info("vPing is OK") + logger.info("vPing detected!") EXIT_CODE = 0 break - else: - logger.info("No vPing detected...") - sec+=1 - if sec == PING_TIMEOUT: + elif sec == PING_TIMEOUT: logger.info("Timeout reached.") break + else: + logger.debug("No vPing detected...") + sec+=1 - - # delete both VMs - logger.debug("Deleting Instances...") - nova.servers.delete(vm1) - logger.debug("Instance %s terminated." % NAME_VM_1) - nova.servers.delete(vm2) - logger.debug("Instance %s terminated." % NAME_VM_2) + cleanup(nova_client,neutron_client,network_dic) if EXIT_CODE == 0: logger.info("vPing OK") |