[ansible][fedora] Update package name
[barometer.git] / baro_tests / tests.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright(c) 2017-2019 Intel Corporation and OPNFV. All rights reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16 #
17
18 """Function for testing collectd plug-ins with different oup plug-ins"""
19
20 import time
21 import math
22
23
24 def test_snmp_sends_data(
25         compute, interval, logger, client, mib_file=None,
26         mib_strings=None, in_command=None, conf=None):
27     """Check that SNMP deta are updated"""
28     logger.debug('Interval: {}'.format(interval))
29     if mib_file is not None:
30         logger.info(
31             'Getting SNMP metrics of MIB file {} and '.format(mib_file)
32             + 'following MIB strings: {}...'.format(', '.join(mib_strings)))
33     snmp_metrics = client.get_snmp_metrics(compute, mib_file, mib_strings)
34     if mib_file is None:
35         return len(snmp_metrics) > 1
36     if in_command is not None and conf is not None:
37         conf.execute_command(in_command, compute.get_ip())
38
39     attempt = 1
40     is_passed = False
41     while (attempt <= 10) and not is_passed:
42         is_passed = True
43         # wait Interval time + 2 sec for db update
44         sleep_time = interval + 2
45         if attempt > 1:
46             logger.info('Starting attempt {}'.format(attempt))
47         logger.info(
48             'Sleeping for {} seconds to get updated entries'.format(sleep_time)
49             + ' (interval is {} sec)...'.format(interval))
50         time.sleep(sleep_time)
51
52         logger.info(
53             'Getting SNMP metrics of MIB file {} and '.format(mib_file)
54             + 'following MIB strings: {}...'.format(', '.join(mib_strings)))
55         snmp_metrics2 = client.get_snmp_metrics(compute, mib_file, mib_strings)
56         unchanged_snmp_metrics = [
57             snmp_metric for snmp_metric in snmp_metrics
58             if snmp_metrics[snmp_metric] == snmp_metrics2[snmp_metric]]
59         if len(unchanged_snmp_metrics) > 0:
60             logger.error("Following SNMP metrics didn't change: {}".format(
61                 ', '.join(unchanged_snmp_metrics)))
62             is_passed = False
63         attempt += 1
64         if not is_passed:
65             logger.warning('After sleep new entries were not found.')
66     if not is_passed:
67         logger.error('This was the last attempt.')
68         return False
69     logger.info('All SNMP metrics are changed.')
70     return True
71
72
73 def test_ceilometer_node_sends_data(
74         node_id, interval, logger, client, criteria_list=[],
75         resource_id_substrings=['']):
76     """ Test that data reported by Ceilometer are updated in the given interval.
77
78     Keyword arguments:
79     node_id -- node ID
80     interval -- interval to check
81     logger -- logger instance
82     client -- CeilometerClient instance
83     criteria_list -- list of criteria used in ceilometer calls
84     resource_id_substrings -- list of substrings to search for in resource ID
85
86     Return boolean value indicating success or failure.
87     """
88
89     def _search_meterlist_latest_entry(meterlist, node_str, substr=''):
90         """Search for latest entry in meter list
91
92         Keyword arguments:
93         meterlist -- list of metrics
94         node_str -- name of node, which will be found in meter list
95         substr -- substring which will be found in meter list
96
97         Return latest entry from meter list which contains given node string
98         and (if defined) subsrting.
99         """
100         res = [
101             entry for entry in meterlist if node_str in entry['resource_id']
102             and substr in entry['resource_id']]
103         if res:
104             return res[0]
105         else:
106             return []
107
108     client.auth_token()
109     timestamps = {}
110     node_str = 'node-{}'.format(node_id) if node_id else ''
111
112     logger.info(
113         'Searching for timestamps of latest entries{0}{1}{2}...'.format(
114             '' if node_str == '' else ' for {}'.format(node_str),
115             '' if len(criteria_list) == 0 else (
116                 ' for criteria ' + ', '.join(criteria_list)),
117             '' if resource_id_substrings == [''] else
118             ' and resource ID substrings "{}"'.format(
119                 '", "'.join(resource_id_substrings))))
120     for criterion in criteria_list if len(criteria_list) > 0 else [None]:
121         meter_list = client.get_gnocchi_metrics(criterion)
122         for resource_id_substring in resource_id_substrings:
123             last_entry = _search_meterlist_latest_entry(
124                 meter_list, node_str, resource_id_substring)
125             if len(last_entry) == 0:
126                 logger.error('Entry{0}{1}{2} not found'.format(
127                     '' if node_str == '' else ' for {}'.format(node_str),
128                     '' if criterion is None else 'for criterion {}'.format(
129                         criterion),
130                     '' if resource_id_substring == '' else 'and resource '
131                     + 'ID substring "{}"'.format(resource_id_substring)))
132                 return False
133             timestamp = last_entry['timestamp']
134             logger.debug('Last entry found: {0} {1}'.format(
135                 timestamp, last_entry['resource_id']))
136             timestamps[(criterion, resource_id_substring)] = timestamp
137
138     attempt = 1
139     is_passed = False
140     while (attempt <= 10) and not is_passed:
141         is_passed = True
142         # wait Interval time + 2 sec for db update
143         sleep_time = interval + 2
144         if attempt > 1:
145             logger.info('Starting attempt {}'.format(attempt))
146         logger.info(
147             'Sleeping for {} seconds to get updated entries '.format(sleep_time)
148             + '(interval is {} sec)...'.format(interval))
149         time.sleep(sleep_time)
150
151         logger.info(
152             'Searching for timestamps of latest entries{}{}{}...' .format(
153                 '' if node_str == '' else ' for {}'.format(node_str),
154                 '' if len(criteria_list) == 0 else (
155                     ' for criteria ' + ', ' .join(criteria_list)),
156                 '' if resource_id_substrings == ['']
157                 else ' and resource ID substrings "{}"' .format(
158                     '", "'.join(resource_id_substrings))))
159         for criterion in criteria_list if len(criteria_list) > 0 else [None]:
160             meter_list = client.get_ceil_metrics(criterion)
161             for resource_id_substring in resource_id_substrings:
162                 last_entry = _search_meterlist_latest_entry(
163                     meter_list, node_str, resource_id_substring)
164                 if len(last_entry) == 0:
165                     logger.error('Entry{0}{1}{2} not found'.format(
166                         '' if node_str == '' else ' for {}'.format(node_str),
167                         '' if criterion is None else 'for criterion {}'.format(
168                             criterion),
169                         '' if resource_id_substring == '' else ' and resource'
170                         + 'ID substring "{}"'.format(resource_id_substring)))
171                     return False
172                 timestamp = last_entry['timestamp']
173                 logger.debug('Last entry found: {} {}'.format(
174                     timestamp, last_entry['resource_id']))
175                 if timestamp == timestamps[(criterion, resource_id_substring)]:
176                     logger.warning(
177                         'Last entry{0}{1}{2} has the same timestamp as '
178                         + 'before the sleep'.format(
179                             '' if node_str == '' else ' for {}'.format(
180                                 node_str),
181                             '' if resource_id_substring == ''
182                             else ', substring "{}"'.format(
183                                 resource_id_substring),
184                             '' if criterion is None else
185                             ' for criterion {}'.format(criterion)))
186                     is_passed = False
187         attempt += 1
188         if not is_passed:
189             logger.warning('After sleep new entries were not found.')
190     if not is_passed:
191         logger.error('This was the last attempt.')
192         return False
193     logger.info('All latest entries found.')
194     return True
195
196
197 def test_csv_handles_plugin_data(
198         compute, interval, plugin, plugin_subdirs, meter_categories,
199         logger, client):
200     """Check that CSV data are updated by the plugin.
201
202     Keyword arguments:
203     compute -- object compute node
204     interval -- interval to check
205     plugin -- plugin which will be tested
206     plugin_subdirs -- list subdirectories in csv folder
207     meter_categories -- list of meter categories which will be tested
208     logger -- logger instance
209     client -- CSVClient instance
210
211     Return boolean value indicating success or failure.
212     """
213     logger.info(
214         'Getting CSV metrics of plugin {} on compute node {}...' .format(
215             plugin, compute.get_id()))
216     logger.debug('Interval: {}'.format(interval))
217     logger.debug('Plugin subdirs: {}'.format(plugin_subdirs))
218     logger.debug('Plugin meter categories: {}'.format(meter_categories))
219     plugin_metrics = client.get_csv_metrics(
220         compute, plugin_subdirs, meter_categories)
221     if len(plugin_metrics) < len(plugin_subdirs) * len(meter_categories):
222         logger.error('Some plugin metrics not found')
223         return False
224
225     logger.info(
226         'Checking that last two entries in metrics are corresponding'
227         + 'to interval...')
228     for metric in plugin_metrics:
229         logger.debug('{0} {1} {2} ... '.format(metric[0], metric[1], metric[2]))
230         # When there's a small interval or many metrics, there may be a slight
231         # delay in writing the metrics. e.g. a gap of 1.* is okay for an interval of 1
232         if math.floor(metric[3] - metric[2]) > interval + 1:
233             logger.error(
234                 'Time of last two entries differ by '
235                 + '{}, but interval is {}'.format(
236                     metric[3] - metric[2], interval))
237             return False
238         else:
239             logger.debug('OK')
240     logger.info('OK')
241
242     # wait Interval time + 2 sec
243     sleep_time = interval + 2
244     logger.info(
245         'Sleeping for {} seconds to get updated entries '.format(sleep_time)
246         + '(interval is {} sec)...'.format(interval))
247     time.sleep(sleep_time)
248
249     logger.info('Getting new metrics of compute node {}...'.format(
250         compute.get_name()))
251     plugin_metrics2 = client.get_csv_metrics(
252         compute, plugin_subdirs, meter_categories)
253     if len(plugin_metrics2) < len(plugin_subdirs) * len(meter_categories):
254         logger.error('Some plugin metrics not found')
255         return False
256
257     logger.info('Comparing old and new metrics...')
258     logger.debug(plugin_metrics)
259     logger.debug(plugin_metrics2)
260     if len(plugin_metrics) != len(plugin_metrics2):
261         logger.error('Plugin metrics length before and after sleep differ')
262         return False
263     for i in range(len(plugin_metrics2)):
264         logger.debug('{0} {1} {2}  - {3} {4} {5} ... '.format(
265             plugin_metrics[i][0], plugin_metrics[i][1],
266             plugin_metrics[i][2], plugin_metrics2[i][0],
267             plugin_metrics2[i][1], plugin_metrics2[i][2]))
268         if plugin_metrics[i] == plugin_metrics2[i]:
269             logger.error('FAIL')
270             return False
271         else:
272             logger.debug('OK')
273     logger.info('OK')
274
275     return True
276
277
278 def test_dma_server_set_collectd(compute, file, logger, client):
279     with open(file, mode='w') as f:
280         f.write('# dummy conf\n')
281     res = client.set(file)
282     if res:
283         logger.info('set collectd PASS')
284     else:
285         logger.error('set collectd FAIL')
286
287     return res