3 # Copyright (c) 2017 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
17 import functest.utils.functest_logger as ft_logger
18 import functest.utils.openstack_utils as os_utils
19 from opnfv.deployment.factory import Factory as DeploymentFactory
21 from sdnvpn.lib import config as sdnvpn_config
23 logger = ft_logger.Logger("sndvpn_test_utils").getLogger()
25 common_config = sdnvpn_config.CommonConfig()
31 def create_custom_flavor():
32 return os_utils.get_or_create_flavor(common_config.custom_flavor_name,
33 common_config.custom_flavor_ram,
34 common_config.custom_flavor_disk,
35 common_config.custom_flavor_vcpus)
38 def create_net(neutron_client, name):
39 logger.debug("Creating network %s", name)
40 net_id = os_utils.create_neutron_net(neutron_client, name)
43 "There has been a problem when creating the neutron network")
48 def create_subnet(neutron_client, name, cidr, net_id):
49 logger.debug("Creating subnet %s in network %s with cidr %s",
51 subnet_id = os_utils.create_neutron_subnet(neutron_client,
57 "There has been a problem when creating the neutron subnet")
62 def create_network(neutron_client, net, subnet1, cidr1,
63 router, subnet2=None, cidr2=None):
64 """Network assoc won't work for networks/subnets created by this function.
66 It is an ODL limitation due to it handling routers as vpns.
67 See https://bugs.opendaylight.org/show_bug.cgi?id=6962"""
68 network_dic = os_utils.create_network_full(neutron_client,
75 "There has been a problem when creating the neutron network")
77 net_id = network_dic["net_id"]
78 subnet_id = network_dic["subnet_id"]
79 router_id = network_dic["router_id"]
81 if subnet2 is not None:
82 logger.debug("Creating and attaching a second subnet...")
83 subnet_id = os_utils.create_neutron_subnet(
84 neutron_client, subnet2, cidr2, net_id)
87 "There has been a problem when creating the second subnet")
89 logger.debug("Subnet '%s' created successfully" % subnet_id)
90 return net_id, subnet_id, router_id
93 def create_instance(nova_client,
105 if 'flavor' not in kwargs:
106 kwargs['flavor'] = common_config.default_flavor
108 logger.info("Creating instance '%s'..." % name)
110 "Configuration:\n name=%s \n flavor=%s \n image=%s \n"
111 " network=%s\n secgroup=%s \n hypervisor=%s \n"
112 " fixed_ip=%s\n files=%s\n userdata=\n%s\n"
113 % (name, kwargs['flavor'], image_id, network_id, sg_id,
114 compute_node, fixed_ip, files, userdata))
115 instance = os_utils.create_instance_and_wait_for_active(
122 av_zone=compute_node,
127 logger.error("Error while booting instance.")
130 logger.debug("Instance '%s' booted successfully. IP='%s'." %
131 (name, instance.networks.itervalues().next()[0]))
132 # Retrieve IP of INSTANCE
133 # instance_ip = instance.networks.get(network_id)[0]
136 logger.debug("Adding '%s' to security group '%s'..."
137 % (name, secgroup_name))
139 logger.debug("Adding '%s' to security group '%s'..."
141 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
146 def generate_ping_userdata(ips_array):
149 ips = ("%s %s" % (ips, ip))
151 ips = ips.replace(' ', ' ')
152 return ("#!/bin/sh\n"
157 " ping -c 1 $ip 2>&1 >/dev/null\n"
159 " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
160 " echo ping $ip OK\n"
161 " else echo ping $ip KO\n"
169 def generate_userdata_common():
170 return ("#!/bin/sh\n"
171 "sudo mkdir -p /home/cirros/.ssh/\n"
172 "sudo chown cirros:cirros /home/cirros/.ssh/\n"
173 "sudo chown cirros:cirros /home/cirros/id_rsa\n"
174 "mv /home/cirros/id_rsa /home/cirros/.ssh/\n"
175 "sudo echo ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgnWtSS98Am516e"
176 "stBsq0jbyOB4eLMUYDdgzsUHsnxFQCtACwwAg9/2uq3FoGUBUWeHZNsT6jcK9"
177 "sCMEYiS479CUCzbrxcd8XaIlK38HECcDVglgBNwNzX/WDfMejXpKzZG61s98rU"
178 "ElNvZ0YDqhaqZGqxIV4ejalqLjYrQkoly3R+2k= "
179 "cirros@test1>/home/cirros/.ssh/authorized_keys\n"
180 "sudo chown cirros:cirros /home/cirros/.ssh/authorized_keys\n"
181 "chmod 700 /home/cirros/.ssh\n"
182 "chmod 644 /home/cirros/.ssh/authorized_keys\n"
183 "chmod 600 /home/cirros/.ssh/id_rsa\n"
187 def generate_userdata_with_ssh(ips_array):
188 u1 = generate_userdata_common()
192 ips = ("%s %s" % (ips, ip))
194 ips = ips.replace(' ', ' ')
200 " hostname=$(ssh -y -i /home/cirros/.ssh/id_rsa "
201 "cirros@$ip 'hostname' </dev/zero 2>/dev/null)\n"
203 " if [ \"Z$RES\" = \"Z0\" ]; then echo $ip $hostname;\n"
204 " else echo $ip 'not reachable';fi;\n"
212 def get_installerHandler():
213 installer_type = str(os.environ['INSTALLER_TYPE'].lower())
214 installer_ip = get_installer_ip()
216 if installer_type not in ["fuel", "apex"]:
217 raise ValueError("%s is not supported" % installer_type)
219 if installer_type in ["apex"]:
220 developHandler = DeploymentFactory.get_handler(
224 pkey_file="/root/.ssh/id_rsa")
226 if installer_type in ["fuel"]:
227 developHandler = DeploymentFactory.get_handler(
232 return developHandler
236 developHandler = get_installerHandler()
237 return developHandler.get_nodes()
240 def get_installer_ip():
241 return str(os.environ['INSTALLER_IP'])
244 def get_instance_ip(instance):
245 instance_ip = instance.networks.itervalues().next()[0]
249 def wait_for_instance(instance):
250 logger.info("Waiting for instance %s to get a DHCP lease..." % instance.id)
251 # The sleep this function replaced waited for 80s
254 pattern = "Lease of .* obtained, lease time"
255 expected_regex = re.compile(pattern)
257 while tries > 0 and not expected_regex.search(console_log):
258 console_log = instance.get_console_output()
259 time.sleep(sleep_time)
262 if not expected_regex.search(console_log):
263 logger.error("Instance %s seems to have failed leasing an IP."
269 def wait_for_instances_up(*args):
270 check = [wait_for_instance(instance) for instance in args]
274 def wait_for_bgp_net_assoc(neutron_client, bgpvpn_id, net_id):
278 logger.debug("Waiting for network %s to associate with BGPVPN %s "
279 % (bgpvpn_id, net_id))
281 while tries > 0 and net_id not in nets:
282 nets = os_utils.get_bgpvpn_networks(neutron_client, bgpvpn_id)
283 time.sleep(sleep_time)
285 if net_id not in nets:
286 logger.error("Association of network %s with BGPVPN %s failed" %
292 def wait_for_bgp_net_assocs(neutron_client, bgpvpn_id, *args):
293 check = [wait_for_bgp_net_assoc(neutron_client, bgpvpn_id, id)
295 # Return True if all associations succeeded
299 def wait_for_bgp_router_assoc(neutron_client, bgpvpn_id, router_id):
303 logger.debug("Waiting for router %s to associate with BGPVPN %s "
304 % (bgpvpn_id, router_id))
305 while tries > 0 and router_id not in routers:
306 routers = os_utils.get_bgpvpn_routers(neutron_client, bgpvpn_id)
307 time.sleep(sleep_time)
309 if router_id not in routers:
310 logger.error("Association of router %s with BGPVPN %s failed" %
311 (router_id, bgpvpn_id))
316 def wait_for_bgp_router_assocs(neutron_client, bgpvpn_id, *args):
317 check = [wait_for_bgp_router_assoc(neutron_client, bgpvpn_id, id)
319 # Return True if all associations succeeded
323 def wait_before_subtest(*args, **kwargs):
324 ''' This is a placeholder.
325 TODO: Replace delay with polling logic. '''
329 def assert_and_get_compute_nodes(nova_client, required_node_number=2):
330 """Get the compute nodes in the deployment
332 Exit if the deployment doesn't have enough compute nodes"""
333 compute_nodes = os_utils.get_hypervisors(nova_client)
335 num_compute_nodes = len(compute_nodes)
336 if num_compute_nodes < 2:
337 logger.error("There are %s compute nodes in the deployment. "
338 "Minimum number of nodes to complete the test is 2."
342 logger.debug("Compute nodes: %s" % compute_nodes)
346 def open_icmp(neutron_client, security_group_id):
347 if os_utils.check_security_group_rules(neutron_client,
352 if not os_utils.create_secgroup_rule(neutron_client,
356 logger.error("Failed to create icmp security group rule...")
358 logger.info("This rule exists for security group: %s"
362 def open_http_port(neutron_client, security_group_id):
363 if os_utils.check_security_group_rules(neutron_client,
369 if not os_utils.create_secgroup_rule(neutron_client,
375 logger.error("Failed to create http security group rule...")
377 logger.info("This rule exists for security group: %s"
381 def open_bgp_port(neutron_client, security_group_id):
382 if os_utils.check_security_group_rules(neutron_client,
388 if not os_utils.create_secgroup_rule(neutron_client,
393 logger.error("Failed to create bgp security group rule...")
395 logger.info("This rule exists for security group: %s"
399 def exec_cmd(cmd, verbose):
401 logger.debug("Executing '%s'" % cmd)
402 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
403 stderr=subprocess.STDOUT)
405 for line in iter(p.stdout.readline, b''):
412 returncode = p.wait()
414 logger.error("Command %s failed to execute." % cmd)
417 return output, success
420 def check_odl_fib(ip, controller_ip):
421 """Check that there is an entry in the ODL Fib for `ip`"""
422 url = "http://" + controller_ip + \
423 ":8181/restconf/config/odl-fib:fibEntries/"
424 logger.debug("Querring '%s' for FIB entries", url)
425 res = requests.get(url, auth=(ODL_USER, ODL_PASS))
426 if res.status_code != 200:
427 logger.error("OpenDaylight response status code: %s", res.status_code)
429 logger.debug("Checking whether '%s' is in the OpenDaylight FIB"
431 logger.debug("OpenDaylight FIB: \n%s" % res.text)
432 return ip in res.text
435 def run_odl_cmd(odl_node, cmd):
436 '''Run a command in the OpenDaylight Karaf shell
438 This is a bit flimsy because of shell quote escaping, make sure that
439 the cmd passed does not have any top level double quotes or this
442 The /dev/null is used because client works, but outputs something
443 that contains "ERROR" and run_cmd doesn't like that.
446 karaf_cmd = ('/opt/opendaylight/bin/client -h 127.0.0.1 "%s"'
447 ' 2>/dev/null' % cmd)
448 return odl_node.run_cmd(karaf_cmd)
451 def wait_for_cloud_init(instance):
453 # ubuntu images take a long time to start
457 instance_log = instance.get_console_output()
458 if "Failed to run module" in instance_log:
460 logger.error("Cloud init failed to run. Reason: %s",
463 if re.search(r"Cloud-init v. .+ finished at", instance_log):
466 time.sleep(sleep_time)
470 logger.error("Cloud init timed out"
478 def attach_instance_to_ext_br(instance, compute_node):
479 libvirt_instance_name = getattr(instance, "OS-EXT-SRV-ATTR:instance_name")
480 installer_type = str(os.environ['INSTALLER_TYPE'].lower())
481 if installer_type == "fuel":
483 elif installer_type == "apex":
484 # In Apex, br-ex is an ovs bridge and virsh attach-interface
485 # won't just work. We work around it by creating a linux
486 # bridge, attaching that to br-ex with a veth pair
487 # and virsh-attaching the instance to the linux-bridge
491 sudo brctl addbr {bridge} &&
492 sudo ip link set {bridge} up &&
493 sudo ip link add quagga-tap type veth peer name ovs-quagga-tap &&
494 sudo ip link set dev ovs-quagga-tap up &&
495 sudo ip link set dev quagga-tap up &&
496 sudo ovs-vsctl add-port br-ex ovs-quagga-tap &&
497 sudo brctl addif {bridge} quagga-tap
499 compute_node.run_cmd(cmd.format(bridge=bridge))
501 compute_node.run_cmd("sudo virsh attach-interface %s"
502 " bridge %s" % (libvirt_instance_name, bridge))