3 # jose.lausuch@ericsson.com
4 # valentin.boucher@orange.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # pylint: disable=missing-docstring
12 from __future__ import print_function
18 from functest.utils import env
20 LOGGER = logging.getLogger(__name__)
23 def execute_command_raise(cmd, info=False, error_msg="",
24 verbose=True, output_file=None):
25 ret = execute_command(cmd, info, error_msg, verbose, output_file)
27 raise Exception(error_msg)
30 def execute_command(cmd, info=False, error_msg="",
31 verbose=True, output_file=None):
33 error_msg = ("The command '%s' failed." % cmd)
34 msg_exec = ("Executing command: '%s'" % cmd)
39 LOGGER.debug(msg_exec)
40 popen = subprocess.Popen(
41 cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
43 ofd = open(output_file, "w")
44 for line in iter(popen.stdout.readline, b''):
48 line = line.replace('\n', '')
54 returncode = popen.wait()
57 LOGGER.error(error_msg)
62 def get_parameter_from_yaml(parameter, yfile):
64 Returns the value of a given parameter in file.yaml
65 parameter must be given in string format with dots
66 Example: general.openstack.image_name
68 with open(yfile) as yfd:
69 file_yaml = yaml.safe_load(yfd)
71 for element in parameter.split("."):
72 value = value.get(element)
74 raise ValueError("The parameter %s is not defined in"
75 " %s" % (parameter, yfile))
79 def get_external_network(cloud):
81 Returns the configured external network name or
82 the first retrieved external network name
85 if env.get("EXTERNAL_NETWORK"):
86 network = cloud.get_network(
87 env.get("EXTERNAL_NETWORK"), {"router:external": True})
90 networks = cloud.list_networks({"router:external": True})