From 2b745471efe898ab9abe42131bcbbf12cac9a8ff Mon Sep 17 00:00:00 2001 From: "jose.lausuch" Date: Wed, 13 May 2015 10:53:06 +0200 Subject: vPing.py: code cleaned. JIRA: FUNCTEST-3 Change-Id: I25bb3df162a61d027afcb260b33cb5c1dc87a0d7 Signed-off-by: jose.lausuch --- testcases/vPing/CI/libraries/vPing.py | 137 ++++++++++++---------------------- 1 file changed, 47 insertions(+), 90 deletions(-) diff --git a/testcases/vPing/CI/libraries/vPing.py b/testcases/vPing/CI/libraries/vPing.py index f2a18ead1..3836a1a4c 100644 --- a/testcases/vPing/CI/libraries/vPing.py +++ b/testcases/vPing/CI/libraries/vPing.py @@ -16,9 +16,6 @@ import os, time, subprocess, logging, argparse, yaml import pprint import novaclient.v2.client as novaclient -import neutronclient.client as neutronclient -#import novaclient.v1_1.client as novaclient -import cinderclient.v1.client as cinderclient pp = pprint.PrettyPrinter(indent=4) EXIT_CODE = -1 @@ -114,42 +111,43 @@ def get_status(nova,vm): def main(): creds = get_credentials("nova") nova = novaclient.Client(**creds) - cinder = cinderclient.Client(**creds) - """ - # print images and server resources - # print nova_images - print_title("images list") - pMsg(nova.images.list()) - print_title("servers list") - pMsg(nova.servers.list()) - """ - # Check if the given image is created - images=nova.images.list() - image_found = False - for image in images: - if image.name == GLANCE_IMAGE_NAME: - logger.info("Glance image found '%s'" %image.name) - image_found = True - if not image_found: - logger.error("ERROR: Glance image %s not found." % GLANCE_IMAGE_NAME) + image = None + network = None + flavor = None + + # Check if the given image exists + try: + image = nova.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()) exit(-1) - # Check if the given neutron network is created - networks=nova.networks.list() - network_found = False - for net in networks: - if net.human_id == NEUTRON_PRIVATE_NET_NAME: - logger.info("Network found '%s'" %net.human_id) - network_found = True - if not network_found: - logger.error("Neutron network %s not found." % NEUTRON_PRIVATE_NET_NAME) + # 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()) exit(-1) + # Check if the given flavor exists + try: + flavor = nova.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()) + exit(-1) + + + # Deleting instances if they exist servers=nova.servers.list() for server in servers: if server.name == NAME_VM_1 or server.name == NAME_VM_2: @@ -160,90 +158,49 @@ def main(): # boot VM 1 # basic boot - # tune (e.g. flavor, images, network) to your specific openstack configuration here - m = NAME_VM_1 - f = nova.flavors.find(name = FLAVOR) - i = nova.images.find(name = GLANCE_IMAGE_NAME) - n = nova.networks.find(label = NEUTRON_PRIVATE_NET_NAME) - u = "#cloud-config\npassword: opnfv\nchpasswd: { expire: False }\nssh_pwauth: True" - #k = "demo-key" + # tune (e.g. flavor, images, network) to your specific openstack configuration here # create VM - logger.info("Creating instance '%s'..." %m) - logger.debug("Configuration:\n name=%s \n flavor=%s \n image=%s \n network=%s \n userdata= \n%s" %(m,f,i,n,u)) + 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( - name = m, - flavor = f, - image = i, - nics = [{"net-id": n.id}], - #key_name = k, - userdata = u, + name = NAME_VM_1, + flavor = flavor, + image = image, + nics = [{"net-id": network.id}] ) - #pMsg(vm1) - #wait until VM status is active waitVmActive(nova,vm1) #retrieve IP of first VM logger.debug("Fetching IP...") - server = get_server(creds, m) - #pMsg(server.networks) + server = get_server(creds, 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] - logger.debug("Instance '%s' got %s" %(m,test_ip)) - test_cmd = '/tmp/vping.sh %s'%test_ip - + logger.debug("Instance '%s' got %s" %(NAME_VM_1,test_ip)) # boot VM 2 # we will boot then execute a ping script with cloud-init # the long chain corresponds to the ping procedure converted with base 64 - # tune (e.g. flavor, images, network) to your specific openstack configuration here - m = NAME_VM_2 - f = nova.flavors.find(name = FLAVOR) - i = nova.images.find(name = GLANCE_IMAGE_NAME) - n = nova.networks.find(label = NEUTRON_PRIVATE_NET_NAME) - # use base 64 format because bad surprises with sh script with cloud-init but script is just pinging - #k = "demo-key" - #u = "#cloud-config\npassword: opnfv\nchpasswd: { expire: False }\nssh_pwauth: True\nwrite_files:\n- encoding: b64\n path: /tmp/vping.sh\n permissions: '0777'\n owner: root:root\n content: IyEvYmluL2Jhc2gKCndoaWxlIHRydWU7IGRvCiBwaW5nIC1jIDEgJDEgMj4mMSA+L2Rldi9udWxsCiBSRVM9JD8KIGlmIFsgIlokUkVTIiA9ICJaMCIgXSA7IHRoZW4KICBlY2hvICJ2UGluZyBPSyIKICBzbGVlcCAxMAogIHN1ZG8gc2h1dGRvd24gLWggbm93CiAgYnJlYWsKIGVsc2UKICBlY2hvICJ2UGluZyBLTyIKIGZpCiBzbGVlcCAxCmRvbmUK\nruncmd:\n - [ sh, -c, %s]"%test_cmd + # tune (e.g. flavor, images, network) to your specific openstack configuration here u = "#!/bin/sh\n\nwhile true; do\n ping -c 1 %s 2>&1 >/dev/null\n RES=$?\n if [ \"Z$RES\" = \"Z0\" ] ; then\n echo 'vPing OK'\n break\n else\n echo 'vPing KO'\n fi\n sleep 1\ndone\n"%test_ip + # create VM - logger.info("Creating instance '%s'..." %m) - logger.debug("Configuration:\n name=%s \n flavor=%s \n image=%s \n network=%s \n userdata= \n%s" %(m,f,i,n,u)) + 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( - name = m, - flavor = f, - image = i, - nics = [{"net-id": n.id}], - #key_name = k, + name = NAME_VM_2, + flavor = flavor, + image = image, + nics = [{"net-id": network.id}], userdata = u, - #security_groups = s, - #config_drive = v.id ) - # The injected script will shutdown the VM2 when the ping works - # The console-log method is more consistent but doesn't work yet waitVmActive(nova,vm2) - """ - logger.info("Waiting for ping, timeout is %d sec..." % PING_TIMEOUT) - sec = 0 - while True: - status = get_status(nova, vm2) - #print status - if status == "SHUTOFF" : - EXIT_CODE = 0 - logger.info("vPing SUCCESSFUL after %d sec" % sec) - break - if sec == PING_TIMEOUT: - logger.info("Timeout. vPing UNSUCCESSFUL.") - break - time.sleep(1) - sec+=1 - """ - # I leave this here until we fix the console-log output sec = 0 console_log = vm2.get_console_output() while True: @@ -270,9 +227,9 @@ def main(): nova.servers.delete(vm2) logger.debug("Instance %s terminated." % NAME_VM_2) - if EXIT_CODE = 0 : + if EXIT_CODE == 0: logger.info("vPing OK") - else : + else: logger.error("vPing FAILED") exit(EXIT_CODE) -- cgit 1.2.3-korg