diff options
author | Romanos Skiadas <rski@intracom-telecom.com> | 2016-09-07 12:35:52 +0000 |
---|---|---|
committer | Jose Lausuch <jose.lausuch@ericsson.com> | 2016-09-09 13:35:05 +0000 |
commit | e0095c0f7bc359910633fda491013e9c1fae3408 (patch) | |
tree | 85273c61338a3bc25bab6f8b4a9663f85869740e /test/functest/utils.py | |
parent | 75589404a758145d18bdcb7f5813c818f12b3729 (diff) |
Refactor some common methods into utils
- generete_ping_userdata(): This method was exactly the same in two
tests
- create_instance(): The various implementations of this method had
almost the same signature, but not quite. This was fixed by adding
extra arguments as needed. Also a global variable was used in the
function, which was converted into a parameter.
- create_network(): The three implementations of create_network had
three different signatures and returned different variables. They
were all unified in a single create_network class and the calls to
create_network were adjusted accordingly.
Revision 1: Fix flake8 violations
Revision 2: Rename logger in utils
Change-Id: Ib6cfe798dc561a69eb50c901e3aa71c19d465821
Signed-off-by: Romanos Skiadas <rski@intracom-telecom.com>
(cherry picked from commit 4c2e5fb06be31502ab3476f25f6d1c2ef8d7b0f1)
Diffstat (limited to 'test/functest/utils.py')
-rw-r--r-- | test/functest/utils.py | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/test/functest/utils.py b/test/functest/utils.py new file mode 100644 index 0000000..4d921c8 --- /dev/null +++ b/test/functest/utils.py @@ -0,0 +1,112 @@ +#!/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 functest.utils.functest_logger as ft_logger +import functest.utils.openstack_utils as os_utils + + +logger = ft_logger.Logger("sndvpn_test_utils").getLogger() + + +def create_network(neutron_client, net, subnet1, cidr1, + router, subnet2=None, cidr2=None): + network_dic = os_utils.create_network_full(neutron_client, + net, + subnet1, + router, + cidr1) + if not network_dic: + logger.error( + "There has been a problem when creating the neutron network") + sys.exit(-1) + net_id = network_dic["net_id"] + subnet_id = network_dic["subnet_id"] + router_id = network_dic["router_id"] + + if subnet2 is not None: + logger.debug("Creating and attaching a second subnet...") + subnet_id = os_utils.create_neutron_subnet( + neutron_client, subnet2, cidr2, net_id) + if not subnet_id: + logger.error( + "There has been a problem when creating the second subnet") + sys.exit(-1) + logger.debug("Subnet '%s' created successfully" % subnet_id) + return net_id, subnet_id, router_id + + +def create_instance(nova_client, + name, + flavor, + image_id, + network_id, + sg_id, + secgroup_name=None, + fixed_ip=None, + compute_node='', + userdata=None, + files=None): + logger.info("Creating instance '%s'..." % name) + logger.debug( + "Configuration:\n name=%s \n flavor=%s \n image=%s \n" + " network=%s\n secgroup=%s \n hypervisor=%s \n" + " fixed_ip=%s\n files=%s\n userdata=\n%s\n" + % (name, flavor, image_id, network_id, sg_id, + compute_node, fixed_ip, files, userdata)) + instance = os_utils.create_instance_and_wait_for_active( + flavor, + image_id, + network_id, + name, + config_drive=True, + userdata=userdata, + av_zone=compute_node, + fixed_ip=fixed_ip, + files=files) + + if instance is None: + logger.error("Error while booting instance.") + sys.exit(-1) + # Retrieve IP of INSTANCE + # instance_ip = instance.networks.get(network_id)[0] + + if secgroup_name: + logger.debug("Adding '%s' to security group '%s'..." + % (name, secgroup_name)) + else: + logger.debug("Adding '%s' to security group '%s'..." + % (name, sg_id)) + os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id) + + return instance + + +def generate_ping_userdata(ips_array): + ips = "" + for ip in ips_array: + ips = ("%s %s" % (ips, ip)) + + ips = ips.replace(' ', ' ') + return ("#!/bin/sh\n" + "set%s\n" + "while true; do\n" + " for i do\n" + " ip=$i\n" + " ping -c 1 $ip 2>&1 >/dev/null\n" + " RES=$?\n" + " if [ \"Z$RES\" = \"Z0\" ] ; then\n" + " echo ping $ip OK\n" + " else echo ping $ip KO\n" + " fi\n" + " done\n" + " sleep 1\n" + "done\n" + % ips) |