diff options
Diffstat (limited to 'functest/opnfv_tests')
-rw-r--r-- | functest/opnfv_tests/OpenStack/__init__.py | 0 | ||||
-rw-r--r-- | functest/opnfv_tests/OpenStack/vPing/__init__.py | 0 | ||||
-rwxr-xr-x | functest/opnfv_tests/OpenStack/vPing/vping.py | 97 | ||||
-rw-r--r-- | functest/opnfv_tests/OpenStack/vPing/vping_base.py | 297 | ||||
-rw-r--r-- | functest/opnfv_tests/OpenStack/vPing/vping_ssh.py | 175 | ||||
-rw-r--r-- | functest/opnfv_tests/OpenStack/vPing/vping_userdata.py | 82 | ||||
-rw-r--r-- | functest/opnfv_tests/OpenStack/vPing/vping_util.py | 462 |
7 files changed, 554 insertions, 559 deletions
diff --git a/functest/opnfv_tests/OpenStack/__init__.py b/functest/opnfv_tests/OpenStack/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/opnfv_tests/OpenStack/__init__.py diff --git a/functest/opnfv_tests/OpenStack/vPing/__init__.py b/functest/opnfv_tests/OpenStack/vPing/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/functest/opnfv_tests/OpenStack/vPing/__init__.py diff --git a/functest/opnfv_tests/OpenStack/vPing/vping.py b/functest/opnfv_tests/OpenStack/vPing/vping.py deleted file mode 100755 index 90f66456..00000000 --- a/functest/opnfv_tests/OpenStack/vPing/vping.py +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/python -# -# Copyright (c) 2015 All rights reserved -# This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# 0.1: This script boots the VM1 and allocates IP address from Nova -# Later, the VM2 boots then execute cloud-init to ping VM1. -# After successful ping, both the VMs are deleted. -# 0.2: measure test duration and publish results under json format -# 0.3: adapt push 2 DB after Test API refacroting -# -# -import datetime -import time - -import argparse -import functest.utils.functest_logger as ft_logger - -import vping_util as util - -parser = argparse.ArgumentParser() -parser.add_argument("-d", "--debug", help="Debug mode", action="store_true") -parser.add_argument("-r", "--report", - help="Create json result file", - action="store_true") -parser.add_argument("-m", "--mode", default='ssh', - help="vPing mode: userdata or ssh", - action="store") - -args = parser.parse_args() - - -def main(): - if args.mode == 'ssh': - case = 'vping_ssh' - else: - case = 'vping_userdata' - - logger = ft_logger.Logger(case).getLogger() - - util.init(logger) - - util.check_repo_exist() - - vmname_1 = util.get_vmname_1() - vmname_2 = util.get_vmname_2() - - image_id = util.create_image() - - flavor = util.get_flavor() - - network_id = util.create_network_full() - - sg_id = util.create_security_group() - - util.delete_exist_vms() - - start_time = time.time() - logger.info("vPing Start Time:'%s'" % ( - datetime.datetime.fromtimestamp(start_time).strftime( - '%Y-%m-%d %H:%M:%S'))) - - vm1 = util.boot_vm(case, - vmname_1, - image_id, - flavor, - network_id, - None, - sg_id) - test_ip = util.get_test_ip(vm1) - vm2 = util.boot_vm(case, - vmname_2, - image_id, - flavor, - network_id, - test_ip, - sg_id) - - EXIT_CODE, stop_time = util.do_vping(case, vm2, test_ip) - details = util.check_result(EXIT_CODE, - start_time, - stop_time) - util.push_result(args.report, - case, - start_time, - stop_time, - details) - - exit(EXIT_CODE) - - -if __name__ == '__main__': - main() diff --git a/functest/opnfv_tests/OpenStack/vPing/vping_base.py b/functest/opnfv_tests/OpenStack/vPing/vping_base.py new file mode 100644 index 00000000..7d56e9da --- /dev/null +++ b/functest/opnfv_tests/OpenStack/vPing/vping_base.py @@ -0,0 +1,297 @@ +#!/usr/bin/python +# +# Copyright (c) 2015 All rights reserved +# This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# +# http://www.apache.org/licenses/LICENSE-2.0 + +import os +import pprint +import time +from datetime import datetime + +import functest.utils.functest_utils as ft_utils +import functest.utils.openstack_utils as os_utils +from functest.core import TestCasesBase + + +class VPingBase(TestCasesBase.TestCasesBase): + def __init__(self): + def get_conf(parameter): + return ft_utils.get_functest_config(parameter) + + super(VPingBase, self).__init__() + self.logger = None + self.repo = get_conf('general.directories.dir_vping') + self.vm1_name = get_conf('vping.vm_name_1') + self.vm2_name = get_conf('vping.vm_name_2') + self.vm_boot_timeout = 180 + self.vm_delete_timeout = 100 + self.ping_timeout = get_conf('vping.ping_timeout') + + self.image_name = get_conf('vping.image_name') + self.image_filename = get_conf('general.openstack.image_file_name') + self.image_format = get_conf('general.openstack.image_disk_format') + self.image_path = \ + "%s/%s" % (get_conf('general.directories.dir_functest_data'), + self.image_filename) + + self.flavor_name = get_conf('vping.vm_flavor') + + # NEUTRON Private Network parameters + self.private_net_name = get_conf('vping.vping_private_net_name') + self.private_subnet_name = get_conf('vping.vping_private_subnet_name') + self.private_subnet_cidr = get_conf('vping.vping_private_subnet_cidr') + self.router_name = get_conf('vping.vping_router_name') + self.sg_name = get_conf('vping.vping_sg_name') + self.sg_desc = get_conf('vping.vping_sg_descr') + self.neutron_client = os_utils.get_neutron_client() + self.glance_client = os_utils.get_glance_client() + self.nova_client = os_utils.get_nova_client() + + def run(self, **kwargs): + if not self.check_repo_exist(): + return TestCasesBase.TestCasesBase.EX_RUN_ERROR + + image_id = self.create_image() + if not image_id: + return TestCasesBase.TestCasesBase.EX_RUN_ERROR + + flavor = self.get_flavor() + if not flavor: + return TestCasesBase.TestCasesBase.EX_RUN_ERROR + + network_id = self.create_network_full() + if not network_id: + return TestCasesBase.TestCasesBase.EX_RUN_ERROR + + sg_id = self.create_security_group() + if not sg_id: + return TestCasesBase.TestCasesBase.EX_RUN_ERROR + + self.delete_exist_vms() + + self.start_time = time.time() + self.logger.info("vPing Start Time:'%s'" % ( + datetime.fromtimestamp(self.start_time).strftime( + '%Y-%m-%d %H:%M:%S'))) + + vm1 = self.boot_vm(self.vm1_name, + image_id, + flavor, + network_id, + None, + sg_id) + if not vm1: + return TestCasesBase.TestCasesBase.EX_RUN_ERROR + + test_ip = self.get_test_ip(vm1) + vm2 = self.boot_vm(self.vm2_name, + image_id, + flavor, + network_id, + test_ip, + sg_id) + if not vm2: + return TestCasesBase.TestCasesBase.EX_RUN_ERROR + + EXIT_CODE = self.do_vping(vm2, test_ip) + if EXIT_CODE == TestCasesBase.TestCasesBase.EX_RUN_ERROR: + return EXIT_CODE + + self.stop_time = time.time() + self.parse_result(EXIT_CODE, + self.start_time, + self.stop_time) + return TestCasesBase.TestCasesBase.EX_OK + + def boot_vm_preparation(self, config, vmname, test_ip): + pass + + def do_vping(self, vm, test_ip): + raise NotImplementedError('vping execution is not implemented') + + def check_repo_exist(self): + if not os.path.exists(self.functest_repo): + self.logger.error("Functest repository not found '%s'" + % self.functest_repo) + return False + return True + + def create_image(self): + _, image_id = os_utils.get_or_create_image(self.image_name, + self.image_path, + self.image_format) + if not image_id: + return None + + return image_id + + def get_flavor(self): + try: + flavor = self.nova_client.flavors.find(name=self.flavor_name) + self.logger.info("Using existing Flavor '%s'..." + % self.flavor_name) + return flavor + except: + self.logger.error("Flavor '%s' not found." % self.flavor_name) + self.logger.info("Available flavors are: ") + self.pMsg(self.nova_client.flavor.list()) + return None + + def create_network_full(self): + network_dic = os_utils.create_network_full(self.neutron_client, + self.private_net_name, + self.private_subnet_name, + self.router_name, + self.private_subnet_cidr) + + if not network_dic: + self.logger.error( + "There has been a problem when creating the neutron network") + return None + network_id = network_dic["net_id"] + return network_id + + def create_security_group(self): + sg_id = os_utils.get_security_group_id(self.neutron_client, + self.sg_name) + if sg_id != '': + self.logger.info("Using existing security group '%s'..." + % self.sg_name) + else: + self.logger.info("Creating security group '%s'..." + % self.sg_name) + SECGROUP = os_utils.create_security_group(self.neutron_client, + self.sg_name, + self.sg_desc) + if not SECGROUP: + self.logger.error("Failed to create the security group...") + return None + + sg_id = SECGROUP['id'] + + self.logger.debug("Security group '%s' with ID=%s created " + "successfully." % (SECGROUP['name'], sg_id)) + + self.logger.debug("Adding ICMP rules in security group '%s'..." + % self.sg_name) + if not os_utils.create_secgroup_rule(self.neutron_client, sg_id, + 'ingress', 'icmp'): + self.logger.error("Failed to create security group rule...") + return None + + self.logger.debug("Adding SSH rules in security group '%s'..." + % self.sg_name) + if not os_utils.create_secgroup_rule(self.neutron_client, sg_id, + 'ingress', 'tcp', + '22', '22'): + self.logger.error("Failed to create security group rule...") + return None + + if not os_utils.create_secgroup_rule( + self.neutron_client, sg_id, 'egress', 'tcp', '22', '22'): + self.logger.error("Failed to create security group rule...") + return None + return sg_id + + def delete_exist_vms(self): + servers = self.nova_client.servers.list() + for server in servers: + if server.name == self.vm1_name or server.name == self.vm2_name: + self.logger.info("Deleting instance %s..." % server.name) + server.delete() + + def boot_vm(self, vmname, image_id, flavor, network_id, test_ip, sg_id): + config = dict() + config['name'] = vmname + config['flavor'] = flavor + config['image'] = image_id + config['nics'] = [{"net-id": network_id}] + self.boot_vm_preparation(config, vmname, test_ip) + self.logger.info("Creating instance '%s'..." % vmname) + self.logger.debug("Configuration: %s" % config) + vm = self.nova_client.servers.create(**config) + + # wait until VM status is active + if not self.waitVmActive(self.nova_client, vm): + vm_status = os_utils.get_instance_status(self.nova_client, vm) + self.logger.error("Instance '%s' cannot be booted. Status is '%s'" + % (vmname, vm_status)) + return None + else: + self.logger.info("Instance '%s' is ACTIVE." % vmname) + + self.add_secgroup(vmname, vm.id, sg_id) + + return vm + + def waitVmActive(self, nova, vm): + # sleep and wait for VM status change + sleep_time = 3 + count = self.vm_boot_timeout / sleep_time + while True: + status = os_utils.get_instance_status(nova, vm) + self.logger.debug("Status: %s" % status) + if status == "ACTIVE": + return True + if status == "ERROR" or status == "error": + return False + if count == 0: + self.logger.debug("Booting a VM timed out...") + return False + count -= 1 + time.sleep(sleep_time) + return False + + def add_secgroup(self, vmname, vm_id, sg_id): + self.logger.info("Adding '%s' to security group '%s'..." % + (vmname, self.sg_name)) + os_utils.add_secgroup_to_instance(self.nova_client, vm_id, sg_id) + + def get_test_ip(self, vm): + test_ip = vm.networks.get(self.private_net_name)[0] + self.logger.debug("Instance '%s' got %s" % (vm.name, test_ip)) + return test_ip + + def parse_result(self, code, start_time, stop_time): + test_status = "FAIL" + if code == 0: + self.logger.info("vPing OK") + duration = round(stop_time - start_time, 1) + self.logger.info("vPing duration:'%s'" % duration) + test_status = "PASS" + elif code == -2: + duration = 0 + self.logger.info("Userdata is not supported in nova boot. " + "Aborting test...") + else: + duration = 0 + self.logger.error("vPing FAILED") + + self.details = {'timestart': start_time, + 'duration': duration, + 'status': test_status} + self.criteria = test_status + + @staticmethod + def pMsg(msg): + """pretty printing""" + pprint.PrettyPrinter(indent=4).pprint(msg) + + +class VPingMain(object): + def __init__(self, vping_cls): + self.vping = vping_cls() + + def main(self, **kwargs): + try: + result = self.vping.run(**kwargs) + if result != VPingBase.EX_OK: + return result + if kwargs['report']: + return self.vping.push_to_db() + except Exception: + return VPingBase.EX_RUN_ERROR diff --git a/functest/opnfv_tests/OpenStack/vPing/vping_ssh.py b/functest/opnfv_tests/OpenStack/vPing/vping_ssh.py new file mode 100644 index 00000000..6fb35a7a --- /dev/null +++ b/functest/opnfv_tests/OpenStack/vPing/vping_ssh.py @@ -0,0 +1,175 @@ +#!/usr/bin/python +# +# Copyright (c) 2015 All rights reserved +# This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# +# http://www.apache.org/licenses/LICENSE-2.0 + +import os +import re +import sys +import time + +import argparse +import paramiko +from scp import SCPClient + +import functest.utils.functest_logger as ft_logger +import functest.utils.openstack_utils as os_utils +import vping_base +from functest.core.TestCasesBase import TestCasesBase + + +class VPingSSH(vping_base.VPingBase): + + def __init__(self): + super(VPingSSH, self).__init__() + self.case_name = 'vping_ssh' + self.logger = ft_logger.Logger(self.case_name).getLogger() + + def do_vping(self, vm, test_ip): + floatip = self.add_float_ip(vm) + if not floatip: + return TestCasesBase.EX_RUN_ERROR + ssh = self.establish_ssh(vm, floatip) + if not ssh: + return TestCasesBase.EX_RUN_ERROR + if not self.transfer_ping_script(ssh, floatip): + return TestCasesBase.EX_RUN_ERROR + return self.do_vping_ssh(ssh, test_ip) + + def add_float_ip(self, vm): + self.logger.info("Creating floating IP for VM '%s'..." % self.vm2_name) + floatip_dic = os_utils.create_floating_ip(self.neutron_client) + floatip = floatip_dic['fip_addr'] + + if floatip is None: + self.logger.error("Cannot create floating IP.") + return None + self.logger.info("Floating IP created: '%s'" % floatip) + + self.logger.info("Associating floating ip: '%s' to VM '%s' " + % (floatip, self.vm2_name)) + if not os_utils.add_floating_ip(self.nova_client, vm.id, floatip): + self.logger.error("Cannot associate floating IP to VM.") + return None + + return floatip + + def establish_ssh(self, vm, floatip): + self.logger.info("Trying to establish SSH connection to %s..." + % floatip) + username = 'cirros' + password = 'cubswin:)' + ssh = paramiko.SSHClient() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + timeout = 50 + nolease = False + got_ip = False + discover_count = 0 + cidr_first_octet = self.private_subnet_cidr.split('.')[0] + while timeout > 0: + try: + ssh.connect(floatip, username=username, + password=password, timeout=2) + self.logger.debug("SSH connection established to %s." + % floatip) + break + except: + self.logger.debug("Waiting for %s..." % floatip) + time.sleep(6) + timeout -= 1 + + console_log = vm.get_console_output() + + # print each "Sending discover" captured on the console log + if (len(re.findall("Sending discover", console_log)) > + discover_count and not got_ip): + discover_count += 1 + self.logger.debug("Console-log '%s': Sending discover..." + % self.vm2_name) + + # check if eth0 got an ip,the line looks like this: + # "inet addr:192.168.".... + # if the dhcp agent fails to assing ip, this line will not appear + if "inet addr:" + cidr_first_octet in console_log and not got_ip: + got_ip = True + self.logger.debug("The instance '%s' succeeded to get the IP " + "from the dhcp agent." % self.vm2_name) + + # if dhcp not work,it shows "No lease, failing".The test will fail + if "No lease, failing" in console_log \ + and not nolease \ + and not got_ip: + nolease = True + self.logger.debug("Console-log '%s': No lease, failing..." + % self.vm2_name) + self.logger.info("The instance failed to get an IP from DHCP " + "agent. The test will probably timeout...") + + if timeout == 0: # 300 sec timeout (5 min) + self.logger.error("Cannot establish connection to IP '%s'. " + "Aborting" % floatip) + return None + return ssh + + def transfer_ping_script(self, ssh, floatip): + self.logger.info("Trying to transfer ping.sh to %s..." % floatip) + scp = SCPClient(ssh.get_transport()) + + ping_script = os.path.join(self.repo + "ping.sh") + try: + scp.put(ping_script, "~/") + except: + self.logger.error("Cannot SCP the file '%s' to VM '%s'" + % (ping_script, floatip)) + return False + + cmd = 'chmod 755 ~/ping.sh' + (stdin, stdout, stderr) = ssh.exec_command(cmd) + for line in stdout.readlines(): + print line + + return True + + def do_vping_ssh(self, ssh, test_ip): + EXIT_CODE = -1 + self.logger.info("Waiting for ping...") + + sec = 0 + cmd = '~/ping.sh ' + test_ip + flag = False + + while True: + time.sleep(1) + (stdin, stdout, stderr) = ssh.exec_command(cmd) + output = stdout.readlines() + + for line in output: + if "vPing OK" in line: + self.logger.info("vPing detected!") + EXIT_CODE = 0 + flag = True + break + + elif sec == self.ping_timeout: + self.logger.info("Timeout reached.") + flag = True + break + if flag: + break + self.logger.debug("Pinging %s. Waiting for response..." % test_ip) + sec += 1 + return EXIT_CODE + + +if __name__ == '__main__': + args_parser = argparse.ArgumentParser() + args_parser.add_argument("-r", "--report", + help="Create json result file", + action="store_true") + args = vars(args_parser.parse_args()) + sys.exit(vping_base.VPingMain(VPingSSH).main(**args)) diff --git a/functest/opnfv_tests/OpenStack/vPing/vping_userdata.py b/functest/opnfv_tests/OpenStack/vPing/vping_userdata.py new file mode 100644 index 00000000..fa91c12a --- /dev/null +++ b/functest/opnfv_tests/OpenStack/vPing/vping_userdata.py @@ -0,0 +1,82 @@ +#!/usr/bin/python +# +# Copyright (c) 2015 All rights reserved +# This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# +# http://www.apache.org/licenses/LICENSE-2.0 + +import sys +import time + +import argparse + +import functest.utils.functest_logger as ft_logger +import vping_base + + +class VPingUserdata(vping_base.VPingBase): + + def __init__(self): + super(VPingUserdata, self).__init__() + self.case_name = 'vping_userdata' + self.logger = ft_logger.Logger(self.case_name).getLogger() + + def boot_vm_preparation(self, config, vmname, test_ip): + config['config_drive'] = True + if vmname == self.vm2_name: + u = ("#!/bin/sh\n\n" + "while 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\n" + "done\n" % test_ip) + config['userdata'] = u + + def do_vping(self, vm, test_ip): + self.logger.info("Waiting for ping...") + EXIT_CODE = -1 + sec = 0 + tries = 0 + + while True: + time.sleep(1) + p_console = vm.get_console_output() + if "vPing OK" in p_console: + self.logger.info("vPing detected!") + EXIT_CODE = 0 + break + elif "failed to read iid from metadata" in p_console or tries > 5: + EXIT_CODE = -2 + break + elif sec == self.ping_timeout: + self.logger.info("Timeout reached.") + break + elif sec % 10 == 0: + if "request failed" in p_console: + self.logger.debug("It seems userdata is not supported " + "in nova boot. Waiting a bit...") + tries += 1 + else: + self.logger.debug("Pinging %s. Waiting for response..." + % test_ip) + sec += 1 + + return EXIT_CODE + + +if __name__ == '__main__': + args_parser = argparse.ArgumentParser() + args_parser.add_argument("-r", "--report", + help="Create json result file", + action="store_true") + args = vars(args_parser.parse_args()) + + sys.exit(vping_base.VPingMain(VPingUserdata).main(**args)) diff --git a/functest/opnfv_tests/OpenStack/vPing/vping_util.py b/functest/opnfv_tests/OpenStack/vPing/vping_util.py deleted file mode 100644 index b8d0d777..00000000 --- a/functest/opnfv_tests/OpenStack/vPing/vping_util.py +++ /dev/null @@ -1,462 +0,0 @@ -import os -import pprint -import re -import sys -import time - -import paramiko -from scp import SCPClient - -import functest.utils.functest_utils as ft_utils -import functest.utils.openstack_utils as os_utils -FUNCTEST_REPO = ft_utils.FUNCTEST_REPO - -NAME_VM_1 = ft_utils.get_functest_config('vping.vm_name_1') -NAME_VM_2 = ft_utils.get_functest_config('vping.vm_name_2') - -VM_BOOT_TIMEOUT = 180 -VM_DELETE_TIMEOUT = 100 -PING_TIMEOUT = ft_utils.get_functest_config('vping.ping_timeout') - -GLANCE_IMAGE_NAME = ft_utils.get_functest_config('vping.image_name') -GLANCE_IMAGE_FILENAME = \ - ft_utils.get_functest_config('general.openstack.image_file_name') -GLANCE_IMAGE_FORMAT = \ - ft_utils.get_functest_config('general.openstack.image_disk_format') -GLANCE_IMAGE_PATH = \ - ft_utils.get_functest_config('general.directories.dir_functest_data') + \ - "/" + GLANCE_IMAGE_FILENAME - - -FLAVOR = ft_utils.get_functest_config('vping.vm_flavor') - -# NEUTRON Private Network parameters -PRIVATE_NET_NAME = \ - ft_utils.get_functest_config('vping.vping_private_net_name') -PRIVATE_SUBNET_NAME = \ - ft_utils.get_functest_config('vping.vping_private_subnet_name') -PRIVATE_SUBNET_CIDR = \ - ft_utils.get_functest_config('vping.vping_private_subnet_cidr') -ROUTER_NAME = ft_utils.get_functest_config('vping.vping_router_name') - -SECGROUP_NAME = ft_utils.get_functest_config('vping.vping_sg_name') -SECGROUP_DESCR = ft_utils.get_functest_config('vping.vping_sg_descr') - - -neutron_client = None -glance_client = None -nova_client = None -logger = None - -pp = pprint.PrettyPrinter(indent=4) - - -def pMsg(value): - """pretty printing""" - pp.pprint(value) - - -def check_repo_exist(): - if not os.path.exists(FUNCTEST_REPO): - logger.error("Functest repository not found '%s'" % FUNCTEST_REPO) - exit(-1) - - -def get_vmname_1(): - return NAME_VM_1 - - -def get_vmname_2(): - return NAME_VM_2 - - -def init(vping_logger): - global nova_client - nova_client = os_utils.get_nova_client() - global neutron_client - neutron_client = os_utils.get_neutron_client() - global glance_client - glance_client = os_utils.get_glance_client() - global logger - logger = vping_logger - - -def waitVmActive(nova, vm): - - # sleep and wait for VM status change - sleep_time = 3 - count = VM_BOOT_TIMEOUT / sleep_time - while True: - status = os_utils.get_instance_status(nova, vm) - logger.debug("Status: %s" % status) - if status == "ACTIVE": - return True - if status == "ERROR" or status == "error": - return False - if count == 0: - logger.debug("Booting a VM timed out...") - return False - count -= 1 - time.sleep(sleep_time) - return False - - -def create_security_group(): - sg_id = os_utils.get_security_group_id(neutron_client, - SECGROUP_NAME) - if sg_id != '': - logger.info("Using existing security group '%s'..." % SECGROUP_NAME) - else: - logger.info("Creating security group '%s'..." % SECGROUP_NAME) - SECGROUP = os_utils.create_security_group(neutron_client, - SECGROUP_NAME, - SECGROUP_DESCR) - if not SECGROUP: - logger.error("Failed to create the security group...") - return False - - sg_id = SECGROUP['id'] - - logger.debug("Security group '%s' with ID=%s created successfully." - % (SECGROUP['name'], sg_id)) - - logger.debug("Adding ICMP rules in security group '%s'..." - % SECGROUP_NAME) - if not os_utils.create_secgroup_rule(neutron_client, sg_id, - 'ingress', 'icmp'): - logger.error("Failed to create the security group rule...") - return False - - logger.debug("Adding SSH rules in security group '%s'..." - % SECGROUP_NAME) - if not os_utils.create_secgroup_rule(neutron_client, sg_id, - 'ingress', 'tcp', - '22', '22'): - logger.error("Failed to create the security group rule...") - return False - - if not os_utils.create_secgroup_rule( - neutron_client, sg_id, 'egress', 'tcp', '22', '22'): - logger.error("Failed to create the security group rule...") - return False - return sg_id - - -def create_image(): - _, image_id = os_utils.get_or_create_image(GLANCE_IMAGE_NAME, - GLANCE_IMAGE_PATH, - GLANCE_IMAGE_FORMAT) - if not image_id: - exit(-1) - - return image_id - - -def get_flavor(): - EXIT_CODE = -1 - - # Check if the given flavor exists - try: - flavor = nova_client.flavors.find(name=FLAVOR) - logger.info("Using existing Flavor '%s'..." % FLAVOR) - return flavor - except: - logger.error("Flavor '%s' not found." % FLAVOR) - logger.info("Available flavors are: ") - pMsg(nova_client.flavor.list()) - exit(EXIT_CODE) - - -def create_network_full(): - EXIT_CODE = -1 - - network_dic = os_utils.create_network_full(neutron_client, - PRIVATE_NET_NAME, - PRIVATE_SUBNET_NAME, - ROUTER_NAME, - PRIVATE_SUBNET_CIDR) - - if not network_dic: - logger.error( - "There has been a problem when creating the neutron network") - exit(EXIT_CODE) - network_id = network_dic["net_id"] - return network_id - - -def delete_exist_vms(): - 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() - - -def is_userdata(case): - return case == 'vping_userdata' - - -def is_ssh(case): - return case == 'vping_ssh' - - -def boot_vm(case, name, image_id, flavor, network_id, test_ip, sg_id): - EXIT_CODE = -1 - - config = dict() - config['name'] = name - config['flavor'] = flavor - config['image'] = image_id - config['nics'] = [{"net-id": network_id}] - if is_userdata(case): - config['config_drive'] = True - if name == NAME_VM_2: - u = ("#!/bin/sh\n\n" - "while 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\n" - "done\n" % test_ip) - config['userdata'] = u - - logger.info("Creating instance '%s'..." % name) - logger.debug("Configuration: %s" % config) - vm = nova_client.servers.create(**config) - - # wait until VM status is active - if not waitVmActive(nova_client, vm): - - logger.error("Instance '%s' cannot be booted. Status is '%s'" % ( - name, os_utils.get_instance_status(nova_client, vm))) - exit(EXIT_CODE) - else: - logger.info("Instance '%s' is ACTIVE." % name) - - add_secgroup(name, vm.id, sg_id) - - return vm - - -def get_test_ip(vm): - test_ip = vm.networks.get(PRIVATE_NET_NAME)[0] - logger.debug("Instance '%s' got %s" % (vm.name, test_ip)) - return test_ip - - -def add_secgroup(vmname, vm_id, sg_id): - logger.info("Adding '%s' to security group '%s'..." % - (vmname, SECGROUP_NAME)) - os_utils.add_secgroup_to_instance(nova_client, vm_id, sg_id) - - -def add_float_ip(vm): - EXIT_CODE = -1 - - logger.info("Creating floating IP for VM '%s'..." % NAME_VM_2) - floatip_dic = os_utils.create_floating_ip(neutron_client) - floatip = floatip_dic['fip_addr'] - - if floatip is None: - logger.error("Cannot create floating IP.") - exit(EXIT_CODE) - logger.info("Floating IP created: '%s'" % floatip) - - logger.info("Associating floating ip: '%s' to VM '%s' " - % (floatip, NAME_VM_2)) - if not os_utils.add_floating_ip(nova_client, vm.id, floatip): - logger.error("Cannot associate floating IP to VM.") - exit(EXIT_CODE) - - return floatip - - -def establish_ssh(vm, floatip): - EXIT_CODE = -1 - - logger.info("Trying to establish SSH connection to %s..." % floatip) - username = 'cirros' - password = 'cubswin:)' - ssh = paramiko.SSHClient() - ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - - timeout = 50 - nolease = False - got_ip = False - discover_count = 0 - cidr_first_octet = PRIVATE_SUBNET_CIDR.split('.')[0] - while timeout > 0: - try: - ssh.connect(floatip, username=username, - password=password, timeout=2) - logger.debug("SSH connection established to %s." % floatip) - break - except: - logger.debug("Waiting for %s..." % floatip) - time.sleep(6) - timeout -= 1 - - console_log = vm.get_console_output() - - # print each "Sending discover" captured on the console log - if (len(re.findall("Sending discover", console_log)) > - discover_count and not got_ip): - discover_count += 1 - logger.debug("Console-log '%s': Sending discover..." - % NAME_VM_2) - - # check if eth0 got an ip,the line looks like this: - # "inet addr:192.168.".... - # if the dhcp agent fails to assing ip, this line will not appear - if "inet addr:" + cidr_first_octet in console_log and not got_ip: - got_ip = True - logger.debug("The instance '%s' succeeded to get the IP " - "from the dhcp agent." % NAME_VM_2) - - # if dhcp doesnt work,it shows "No lease, failing".The test will fail - if "No lease, failing" in console_log and not nolease and not got_ip: - nolease = True - logger.debug("Console-log '%s': No lease, failing..." - % NAME_VM_2) - logger.info("The instance failed to get an IP from the " - "DHCP agent. The test will probably timeout...") - - if timeout == 0: # 300 sec timeout (5 min) - logger.error("Cannot establish connection to IP '%s'. Aborting" - % floatip) - exit(EXIT_CODE) - return ssh - - -def transfer_ping_script(ssh, floatip): - EXIT_CODE = -1 - - logger.info("Trying to transfer ping.sh to %s..." % floatip) - scp = SCPClient(ssh.get_transport()) - - ping_script = ( - FUNCTEST_REPO + "/functest/opnfv_tests/OpenStack/vPing/ping.sh") - try: - scp.put(ping_script, "~/") - except: - logger.error("Cannot SCP the file '%s' to VM '%s'" - % (ping_script, floatip)) - exit(EXIT_CODE) - - cmd = 'chmod 755 ~/ping.sh' - (stdin, stdout, stderr) = ssh.exec_command(cmd) - for line in stdout.readlines(): - print line - - -def do_vping_ssh(ssh, test_ip): - logger.info("Waiting for ping...") - - sec = 0 - cmd = '~/ping.sh ' + test_ip - flag = False - - while True: - time.sleep(1) - (stdin, stdout, stderr) = ssh.exec_command(cmd) - output = stdout.readlines() - - for line in output: - if "vPing OK" in line: - logger.info("vPing detected!") - EXIT_CODE = 0 - flag = True - break - - elif sec == PING_TIMEOUT: - logger.info("Timeout reached.") - flag = True - break - if flag: - break - logger.debug("Pinging %s. Waiting for response..." % test_ip) - sec += 1 - return EXIT_CODE, time.time() - - -def do_vping_userdata(vm, test_ip): - logger.info("Waiting for ping...") - EXIT_CODE = -1 - sec = 0 - metadata_tries = 0 - - while True: - time.sleep(1) - console_log = vm.get_console_output() - if "vPing OK" in console_log: - logger.info("vPing detected!") - EXIT_CODE = 0 - break - elif ("failed to read iid from metadata" in console_log or - metadata_tries > 5): - EXIT_CODE = -2 - break - elif sec == PING_TIMEOUT: - logger.info("Timeout reached.") - break - elif sec % 10 == 0: - if "request failed" in console_log: - logger.debug("It seems userdata is not supported in " - "nova boot. Waiting a bit...") - metadata_tries += 1 - else: - logger.debug("Pinging %s. Waiting for response..." % test_ip) - sec += 1 - - return EXIT_CODE, time.time() - - -def do_vping(case, vm, test_ip): - if is_userdata(case): - return do_vping_userdata(vm, test_ip) - else: - floatip = add_float_ip(vm) - ssh = establish_ssh(vm, floatip) - transfer_ping_script(ssh, floatip) - return do_vping_ssh(ssh, test_ip) - - -def check_result(code, start_time, stop_time): - test_status = "FAIL" - if code == 0: - logger.info("vPing OK") - duration = round(stop_time - start_time, 1) - logger.info("vPing duration:'%s'" % duration) - test_status = "PASS" - elif code == -2: - duration = 0 - logger.info("Userdata is not supported in nova boot. Aborting test...") - else: - duration = 0 - logger.error("vPing FAILED") - - details = {'timestart': start_time, - 'duration': duration, - 'status': test_status} - - return details - - -def push_result(report, case, start_time, stop_time, details): - if report: - try: - logger.debug("Pushing vPing %s results into DB..." % case) - ft_utils.push_results_to_db('functest', - case, - start_time, - stop_time, - details['status'], - details=details) - except: - logger.error("Error pushing results into Database '%s'" - % sys.exc_info()[0]) |