Merge "Update to VES API v3 URL"
[barometer.git] / baro_tests / tests.py
1 """Function for testing collectd plug-ins with different oup plug-ins"""
2 # -*- coding: utf-8 -*-
3
4 #Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import time
17
18
19 def test_ceilometer_node_sends_data(
20         node_id, interval, logger, client, criteria_list=[],
21         resource_id_substrings=['']):
22     """ Test that data reported by Ceilometer are updated in the given interval.
23
24     Keyword arguments:
25     node_id -- node ID
26     interval -- interval to check
27     logger -- logger instance
28     client -- CeilometerClient instance
29     criteria_list -- list of criteria used in ceilometer calls
30     resource_id_substrings -- list of substrings to search for in resource ID
31
32     Return boolean value indicating success or failure.
33     """
34
35     def _search_meterlist_latest_entry(meterlist, node_str, substr=''):
36         """Search for latest entry in meter list
37
38         Keyword arguments:
39         meterlist -- list of metrics
40         node_str -- name of node, which will be found in meter list
41         substr -- substring which will be found in meter list
42
43         Return latest entry from meter list which contains given node string
44         and (if defined) subsrting.
45         """
46         res = [entry for entry in meterlist if node_str in entry['resource_id']
47             and substr in entry['resource_id']]
48         if res:
49             return res[0]
50         else:
51             return []
52
53     client.auth_token()
54     timestamps = {}
55     node_str = 'node-{}'.format(node_id) if node_id else ''
56
57     logger.info('Searching for timestamps of latest entries{0}{1}{2}...'.format(
58         '' if node_str == '' else ' for {}'.format(node_str),
59         '' if len(criteria_list) == 0 else (' for criteria ' + ', '.join(criteria_list)),
60         '' if resource_id_substrings == [''] else ' and resource ID substrings "{}"'.format(
61             '", "'.join(resource_id_substrings))))
62     for criterion in criteria_list if len(criteria_list) > 0 else [None]:
63         meter_list = client.get_ceil_metrics(criterion)
64         for resource_id_substring in resource_id_substrings:
65             last_entry = _search_meterlist_latest_entry(meter_list, node_str, resource_id_substring)
66             if len(last_entry) == 0:
67                 logger.error('Entry{0}{1}{2} not found'.format(
68                     '' if node_str == '' else ' for {}'.format(node_str),
69                     '' if criterion is None else 'for criterion {}'.format(criterion),
70                     '' if resource_id_substring == ''
71                     else 'and resource ID substring "{}"'.format(resource_id_substring)))
72                 return False
73             timestamp = last_entry['timestamp']
74             logger.debug('Last entry found: {0} {1}'.format(timestamp, last_entry['resource_id']))
75             timestamps[(criterion, resource_id_substring)] = timestamp
76
77     attempt = 1
78     is_passed = False
79     while (attempt <= 10) and not is_passed:
80         is_passed = True
81         # wait Interval time + 2 sec for db update
82         sleep_time = interval + 2
83         if attempt > 1:
84             logger.info('Starting attempt {}'.format(attempt))
85         logger.info(
86             'Sleeping for {} seconds to get updated entries '.format(sleep_time)
87             + '(interval is {} sec)...'.format(interval))
88         time.sleep(sleep_time)
89
90         logger.info('Searching for timestamps of latest entries{}{}{}...'.format(
91             '' if node_str == '' else ' for {}'.format(node_str),
92             '' if len(criteria_list) == 0 else (' for criteria ' + ', '.join(criteria_list)),
93             '' if resource_id_substrings == ['']
94             else ' and resource ID substrings "{}"'.format('", "'.join(resource_id_substrings))))
95         for criterion in criteria_list if len(criteria_list) > 0 else [None]:
96             meter_list = client.get_ceil_metrics(criterion)
97             for resource_id_substring in resource_id_substrings:
98                 last_entry = _search_meterlist_latest_entry(
99                     meter_list, node_str, resource_id_substring)
100                 if len(last_entry) == 0:
101                     logger.error('Entry{0}{1}{2} not found'.format(
102                         '' if node_str == '' else ' for {}'.format(node_str),
103                         '' if criterion is None else 'for criterion {}'.format(criterion),
104                         '' if resource_id_substring == ''
105                         else ' and resource ID substring "{}"'.format(resource_id_substring)))
106                     return False
107                 timestamp = last_entry['timestamp']
108                 logger.debug('Last entry found: {} {}'.format(timestamp, last_entry['resource_id']))
109                 if timestamp == timestamps[(criterion, resource_id_substring)]:
110                     logger.warning(
111                         'Last entry{0}{1}{2} has the same timestamp as before the sleep'.format(
112                             '' if node_str == '' else ' for {}'.format(node_str),
113                             '' if resource_id_substring == ''
114                             else ', substring "{}"'.format(resource_id_substring),
115                             '' if criterion is None else ' for criterion {}'.format(criterion)))
116                     is_passed = False
117         attempt += 1
118         if not is_passed:
119             logger.warning('After sleep new entries were not found.')
120     if not is_passed:
121         logger.error('This was the last attempt.')
122         return False
123     logger.info('All latest entries found.')
124     return True
125
126
127 def test_csv_handles_plugin_data(
128         compute, interval, plugin, plugin_subdirs, meter_categories,
129         logger, client):
130     """Check that CSV data are updated by the plugin.
131
132     Keyword arguments:
133     compute -- object compute node
134     interval -- interval to check
135     plugin -- plugin which will be tested
136     plugin_subdirs -- list subdirectories in csv folder
137     meter_categories -- list of meter categories which will be tested
138     logger -- logger instance
139     client -- CSVClient instance
140
141     Return boolean value indicating success or failure.
142     """
143     logger.info('Getting CSV metrics of plugin {} on compute node {}...'.format(
144         plugin, compute.get_id()))
145     logger.debug('Interval: {}'.format(interval))
146     logger.debug('Plugin subdirs: {}'.format(plugin_subdirs))
147     logger.debug('Plugin meter categories: {}'.format(meter_categories))
148     plugin_metrics = client.get_csv_metrics(compute, plugin_subdirs, meter_categories)
149     if len(plugin_metrics) < len(plugin_subdirs) * len(meter_categories):
150         logger.error('Some plugin metrics not found')
151         return False
152
153     logger.info('Checking that last two entries in metrics are corresponding to interval...')
154     for metric in plugin_metrics:
155         logger.debug('{0} {1} {2} ... '.format(metric[0], metric[1], metric[2]))
156         if metric[3] - metric[2] != interval:
157             logger.error('Time of last two entries differ by {}, but interval is {}'.format(
158                 metric[3] - metric[2], interval))
159             return False
160         else:
161             logger.debug('OK')
162     logger.info('OK')
163
164     # wait Interval time + 2 sec
165     sleep_time = interval + 2
166     logger.info(
167         'Sleeping for {} seconds to get updated entries '.format(sleep_time)
168         + '(interval is {} sec)...'.format(interval))
169     time.sleep(sleep_time)
170
171     logger.info('Getting new metrics of compute node {}...'.format(compute.get_id()))
172     plugin_metrics2 = client.get_csv_metrics(compute, plugin_subdirs, meter_categories)
173     if len(plugin_metrics2) < len(plugin_subdirs) * len(meter_categories):
174         logger.error('Some plugin metrics not found')
175         return False
176
177     logger.info('Comparing old and new metrics...')
178     logger.debug(plugin_metrics)
179     logger.debug(plugin_metrics2)
180     if len(plugin_metrics) != len(plugin_metrics2):
181         logger.error('Plugin metrics length before and after sleep differ')
182         return False
183     for i in range(len(plugin_metrics2)):
184         logger.debug('{0} {1} {2}  - {3} {4} {5} ... '.format(
185             plugin_metrics[i][0], plugin_metrics[i][1], plugin_metrics[i][2], plugin_metrics2[i][0],
186             plugin_metrics2[i][1], plugin_metrics2[i][2]))
187         if plugin_metrics[i] == plugin_metrics2[i]:
188             logger.error('FAIL')
189             return False
190         else:
191             logger.debug('OK')
192     logger.info('OK')
193
194     return True