Merge "Count all active hypervisors"
[functest.git] / functest / utils / functest_utils.py
1 #!/usr/bin/env python
2 #
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
9
10 # pylint: disable=missing-docstring
11
12 from __future__ import print_function
13 import logging
14 import os
15 import subprocess
16 import sys
17 import yaml
18
19 from shade import _utils
20 import six
21
22 LOGGER = logging.getLogger(__name__)
23
24
25 def execute_command_raise(cmd, info=False, error_msg="",
26                           verbose=True, output_file=None):
27     ret = execute_command(cmd, info, error_msg, verbose, output_file)
28     if ret != 0:
29         raise Exception(error_msg)
30
31
32 def execute_command(cmd, info=False, error_msg="",
33                     verbose=True, output_file=None):
34     if not error_msg:
35         error_msg = ("The command '%s' failed." % cmd)
36     msg_exec = ("Executing command: '%s'" % cmd)
37     if verbose:
38         if info:
39             LOGGER.info(msg_exec)
40         else:
41             LOGGER.debug(msg_exec)
42     popen = subprocess.Popen(
43         cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
44     if output_file:
45         ofd = open(output_file, "w")
46     for line in iter(popen.stdout.readline, b''):
47         if output_file:
48             ofd.write(line.decode("utf-8"))
49         else:
50             line = line.decode("utf-8").replace('\n', '')
51             print(line)
52             sys.stdout.flush()
53     if output_file:
54         ofd.close()
55     popen.stdout.close()
56     returncode = popen.wait()
57     if returncode != 0:
58         if verbose:
59             LOGGER.error(error_msg)
60
61     return returncode
62
63
64 def get_parameter_from_yaml(parameter, yfile):
65     """
66     Returns the value of a given parameter in file.yaml
67     parameter must be given in string format with dots
68     Example: general.openstack.image_name
69     """
70     with open(yfile) as yfd:
71         file_yaml = yaml.safe_load(yfd)
72     value = file_yaml
73     for element in parameter.split("."):
74         value = value.get(element)
75         if value is None:
76             raise ValueError("The parameter %s is not defined in"
77                              " %s" % (parameter, yfile))
78     return value
79
80
81 def get_nova_version(cloud):
82     """ Get Nova API microversion
83
84     Returns:
85
86     - Nova API microversion
87     - None on operation error
88     """
89     # pylint: disable=protected-access
90     try:
91         request = cloud._compute_client.request("/", "GET")
92         LOGGER.debug('cloud._compute_client.request: %s', request)
93         version = request["version"]["version"]
94         major, minor = version.split('.')
95         LOGGER.debug('nova version: %s', (int(major), int(minor)))
96         return (int(major), int(minor))
97     except Exception:  # pylint: disable=broad-except
98         LOGGER.exception("Cannot detect Nova version")
99         return None
100
101
102 def get_openstack_version(cloud):
103     """ Detect OpenStack version via Nova API microversion
104
105     It follows `MicroversionHistory
106     <https://docs.openstack.org/nova/latest/reference/api-microversion-history.html>`_.
107
108     Returns:
109
110     - OpenStack release
111     - Unknown on operation error
112     """
113     # pylint: disable=too-many-branches
114     version = get_nova_version(cloud)
115     try:
116         assert version
117         if version > (2, 79):
118             osversion = "Master"
119         elif version > (2, 72):
120             osversion = "Train"
121         elif version > (2, 65):
122             osversion = "Stein"
123         elif version > (2, 60):
124             osversion = "Rocky"
125         elif version > (2, 53):
126             osversion = "Queens"
127         elif version > (2, 42):
128             osversion = "Pike"
129         elif version > (2, 38):
130             osversion = "Ocata"
131         elif version > (2, 25):
132             osversion = "Newton"
133         elif version > (2, 12):
134             osversion = "Mitaka"
135         elif version > (2, 3):
136             osversion = "Liberty"
137         elif version >= (2, 1):
138             osversion = "Kilo"
139         else:
140             osversion = "Unknown"
141         LOGGER.info('Detect OpenStack version: %s', osversion)
142         return osversion
143     except AssertionError:
144         LOGGER.exception("Cannot detect OpenStack version")
145         return "Unknown"
146
147
148 def list_services(cloud):
149     # pylint: disable=protected-access
150     """Search Keystone services via $OS_INTERFACE.
151
152     It mainly conforms with `Shade
153     <https://docs.openstack.org/shade/latest>`_ but allows testing vs
154     public endpoints. It's worth mentioning that it doesn't support keystone
155     v2.
156
157     :returns: a list of ``munch.Munch`` containing the services description
158
159     :raises: ``OpenStackCloudException`` if something goes wrong during the
160         openstack API call.
161     """
162     url, key = '/services', 'services'
163     data = cloud._identity_client.get(
164         url, endpoint_filter={
165             'interface': os.environ.get('OS_INTERFACE', 'public')},
166         error_message="Failed to list services")
167     services = cloud._get_and_munchify(key, data)
168     return _utils.normalize_keystone_services(services)
169
170
171 def search_services(cloud, name_or_id=None, filters=None):
172     # pylint: disable=protected-access
173     """Search Keystone services ia $OS_INTERFACE.
174
175     It mainly conforms with `Shade
176     <https://docs.openstack.org/shade/latest>`_ but allows testing vs
177     public endpoints. It's worth mentioning that it doesn't support keystone
178     v2.
179
180     :param name_or_id: Name or id of the desired service.
181     :param filters: a dict containing additional filters to use. e.g.
182                     {'type': 'network'}.
183
184     :returns: a list of ``munch.Munch`` containing the services description
185
186     :raises: ``OpenStackCloudException`` if something goes wrong during the
187         openstack API call.
188     """
189     services = list_services(cloud)
190     return _utils._filter_list(services, name_or_id, filters)
191
192
193 def convert_dict_to_ini(value):
194     "Convert dict to oslo.conf input"
195     assert isinstance(value, dict)
196     return ",".join("{}:{}".format(
197         key, val) for (key, val) in six.iteritems(value))
198
199
200 def convert_list_to_ini(value):
201     "Convert list to oslo.conf input"
202     assert isinstance(value, list)
203     return ",".join("{}".format(val) for val in value)
204
205
206 def convert_ini_to_dict(value):
207     "Convert oslo.conf input to dict"
208     assert isinstance(value, str)
209     try:
210         return {k: v for k, v in (x.rsplit(':', 1) for x in value.split(','))}
211     except ValueError:
212         return {}
213
214
215 def convert_ini_to_list(value):
216     "Convert list to oslo.conf input"
217     assert isinstance(value, str)
218     if not value:
219         return []
220     return [x for x in value.split(',')]