1 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
15 """Function for testing collectd plug-ins with different oup plug-ins"""
21 def test_snmp_sends_data(
22 compute, interval, logger, client, mib_file=None,
23 mib_strings=None, in_command=None, conf=None):
24 """Check that SNMP deta are updated"""
25 logger.debug('Interval: {}'.format(interval))
26 if mib_file is not None:
28 'Getting SNMP metrics of MIB file {} and '.format(mib_file)
29 + 'following MIB strings: {}...'.format(', '.join(mib_strings)))
30 snmp_metrics = client.get_snmp_metrics(compute, mib_file, mib_strings)
32 return len(snmp_metrics) > 1
33 if in_command is not None and conf is not None:
34 conf.execute_command(in_command, compute.get_ip())
38 while (attempt <= 10) and not is_passed:
40 # wait Interval time + 2 sec for db update
41 sleep_time = interval + 2
43 logger.info('Starting attempt {}'.format(attempt))
45 'Sleeping for {} seconds to get updated entries'.format(sleep_time)
46 + ' (interval is {} sec)...'.format(interval))
47 time.sleep(sleep_time)
50 'Getting SNMP metrics of MIB file {} and '.format(mib_file)
51 + 'following MIB strings: {}...'.format(', '.join(mib_strings)))
52 snmp_metrics2 = client.get_snmp_metrics(compute, mib_file, mib_strings)
53 unchanged_snmp_metrics = [
54 snmp_metric for snmp_metric in snmp_metrics
55 if snmp_metrics[snmp_metric] == snmp_metrics2[snmp_metric]]
56 if len(unchanged_snmp_metrics) > 0:
57 logger.error("Following SNMP metrics didn't change: {}".format(
58 ', '.join(unchanged_snmp_metrics)))
62 logger.warning('After sleep new entries were not found.')
64 logger.error('This was the last attempt.')
66 logger.info('All SNMP metrics are changed.')
70 def test_ceilometer_node_sends_data(
71 node_id, interval, logger, client, criteria_list=[],
72 resource_id_substrings=['']):
73 """ Test that data reported by Ceilometer are updated in the given interval.
77 interval -- interval to check
78 logger -- logger instance
79 client -- CeilometerClient instance
80 criteria_list -- list of criteria used in ceilometer calls
81 resource_id_substrings -- list of substrings to search for in resource ID
83 Return boolean value indicating success or failure.
86 def _search_meterlist_latest_entry(meterlist, node_str, substr=''):
87 """Search for latest entry in meter list
90 meterlist -- list of metrics
91 node_str -- name of node, which will be found in meter list
92 substr -- substring which will be found in meter list
94 Return latest entry from meter list which contains given node string
95 and (if defined) subsrting.
98 entry for entry in meterlist if node_str in entry['resource_id']
99 and substr in entry['resource_id']]
107 node_str = 'node-{}'.format(node_id) if node_id else ''
110 'Searching for timestamps of latest entries{0}{1}{2}...'.format(
111 '' if node_str == '' else ' for {}'.format(node_str),
112 '' if len(criteria_list) == 0 else (
113 ' for criteria ' + ', '.join(criteria_list)),
114 '' if resource_id_substrings == [''] else
115 ' and resource ID substrings "{}"'.format(
116 '", "'.join(resource_id_substrings))))
117 for criterion in criteria_list if len(criteria_list) > 0 else [None]:
118 meter_list = client.get_gnocchi_metrics(criterion)
119 for resource_id_substring in resource_id_substrings:
120 last_entry = _search_meterlist_latest_entry(
121 meter_list, node_str, resource_id_substring)
122 if len(last_entry) == 0:
123 logger.error('Entry{0}{1}{2} not found'.format(
124 '' if node_str == '' else ' for {}'.format(node_str),
125 '' if criterion is None else 'for criterion {}'.format(
127 '' if resource_id_substring == '' else 'and resource '
128 + 'ID substring "{}"'.format(resource_id_substring)))
130 timestamp = last_entry['timestamp']
131 logger.debug('Last entry found: {0} {1}'.format(
132 timestamp, last_entry['resource_id']))
133 timestamps[(criterion, resource_id_substring)] = timestamp
137 while (attempt <= 10) and not is_passed:
139 # wait Interval time + 2 sec for db update
140 sleep_time = interval + 2
142 logger.info('Starting attempt {}'.format(attempt))
144 'Sleeping for {} seconds to get updated entries '.format(sleep_time)
145 + '(interval is {} sec)...'.format(interval))
146 time.sleep(sleep_time)
149 'Searching for timestamps of latest entries{}{}{}...' .format(
150 '' if node_str == '' else ' for {}'.format(node_str),
151 '' if len(criteria_list) == 0 else (
152 ' for criteria ' + ', ' .join(criteria_list)),
153 '' if resource_id_substrings == ['']
154 else ' and resource ID substrings "{}"' .format(
155 '", "'.join(resource_id_substrings))))
156 for criterion in criteria_list if len(criteria_list) > 0 else [None]:
157 meter_list = client.get_ceil_metrics(criterion)
158 for resource_id_substring in resource_id_substrings:
159 last_entry = _search_meterlist_latest_entry(
160 meter_list, node_str, resource_id_substring)
161 if len(last_entry) == 0:
162 logger.error('Entry{0}{1}{2} not found'.format(
163 '' if node_str == '' else ' for {}'.format(node_str),
164 '' if criterion is None else 'for criterion {}'.format(
166 '' if resource_id_substring == '' else ' and resource'
167 + 'ID substring "{}"'.format(resource_id_substring)))
169 timestamp = last_entry['timestamp']
170 logger.debug('Last entry found: {} {}'.format(
171 timestamp, last_entry['resource_id']))
172 if timestamp == timestamps[(criterion, resource_id_substring)]:
174 'Last entry{0}{1}{2} has the same timestamp as '
175 + 'before the sleep'.format(
176 '' if node_str == '' else ' for {}'.format(
178 '' if resource_id_substring == ''
179 else ', substring "{}"'.format(
180 resource_id_substring),
181 '' if criterion is None else
182 ' for criterion {}'.format(criterion)))
186 logger.warning('After sleep new entries were not found.')
188 logger.error('This was the last attempt.')
190 logger.info('All latest entries found.')
194 def test_csv_handles_plugin_data(
195 compute, interval, plugin, plugin_subdirs, meter_categories,
197 """Check that CSV data are updated by the plugin.
200 compute -- object compute node
201 interval -- interval to check
202 plugin -- plugin which will be tested
203 plugin_subdirs -- list subdirectories in csv folder
204 meter_categories -- list of meter categories which will be tested
205 logger -- logger instance
206 client -- CSVClient instance
208 Return boolean value indicating success or failure.
211 'Getting CSV metrics of plugin {} on compute node {}...' .format(
212 plugin, compute.get_id()))
213 logger.debug('Interval: {}'.format(interval))
214 logger.debug('Plugin subdirs: {}'.format(plugin_subdirs))
215 logger.debug('Plugin meter categories: {}'.format(meter_categories))
216 plugin_metrics = client.get_csv_metrics(
217 compute, plugin_subdirs, meter_categories)
218 if len(plugin_metrics) < len(plugin_subdirs) * len(meter_categories):
219 logger.error('Some plugin metrics not found')
223 'Checking that last two entries in metrics are corresponding'
225 for metric in plugin_metrics:
226 logger.debug('{0} {1} {2} ... '.format(metric[0], metric[1], metric[2]))
227 if math.floor(metric[3] - metric[2]) > interval:
229 'Time of last two entries differ by '
230 + '{}, but interval is {}'.format(
231 metric[3] - metric[2], interval))
237 # wait Interval time + 2 sec
238 sleep_time = interval + 2
240 'Sleeping for {} seconds to get updated entries '.format(sleep_time)
241 + '(interval is {} sec)...'.format(interval))
242 time.sleep(sleep_time)
244 logger.info('Getting new metrics of compute node {}...'.format(
246 plugin_metrics2 = client.get_csv_metrics(
247 compute, plugin_subdirs, meter_categories)
248 if len(plugin_metrics2) < len(plugin_subdirs) * len(meter_categories):
249 logger.error('Some plugin metrics not found')
252 logger.info('Comparing old and new metrics...')
253 logger.debug(plugin_metrics)
254 logger.debug(plugin_metrics2)
255 if len(plugin_metrics) != len(plugin_metrics2):
256 logger.error('Plugin metrics length before and after sleep differ')
258 for i in range(len(plugin_metrics2)):
259 logger.debug('{0} {1} {2} - {3} {4} {5} ... '.format(
260 plugin_metrics[i][0], plugin_metrics[i][1],
261 plugin_metrics[i][2], plugin_metrics2[i][0],
262 plugin_metrics2[i][1], plugin_metrics2[i][2]))
263 if plugin_metrics[i] == plugin_metrics2[i]: