1 # -*- coding: utf-8 -*-
3 # Copyright(c) 2017-2019 Intel Corporation and OPNFV. All rights reserved.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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
18 """Function for testing collectd plug-ins with different oup plug-ins"""
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:
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)
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())
41 while (attempt <= 10) and not is_passed:
43 # wait Interval time + 2 sec for db update
44 sleep_time = interval + 2
46 logger.info('Starting attempt {}'.format(attempt))
48 'Sleeping for {} seconds to get updated entries'.format(sleep_time)
49 + ' (interval is {} sec)...'.format(interval))
50 time.sleep(sleep_time)
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)))
65 logger.warning('After sleep new entries were not found.')
67 logger.error('This was the last attempt.')
69 logger.info('All SNMP metrics are changed.')
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.
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
86 Return boolean value indicating success or failure.
89 def _search_meterlist_latest_entry(meterlist, node_str, substr=''):
90 """Search for latest entry in meter list
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
97 Return latest entry from meter list which contains given node string
98 and (if defined) subsrting.
101 entry for entry in meterlist if node_str in entry['resource_id']
102 and substr in entry['resource_id']]
110 node_str = 'node-{}'.format(node_id) if node_id else ''
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(
130 '' if resource_id_substring == '' else 'and resource '
131 + 'ID substring "{}"'.format(resource_id_substring)))
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
140 while (attempt <= 10) and not is_passed:
142 # wait Interval time + 2 sec for db update
143 sleep_time = interval + 2
145 logger.info('Starting attempt {}'.format(attempt))
147 'Sleeping for {} seconds to get updated entries '.format(sleep_time)
148 + '(interval is {} sec)...'.format(interval))
149 time.sleep(sleep_time)
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(
169 '' if resource_id_substring == '' else ' and resource'
170 + 'ID substring "{}"'.format(resource_id_substring)))
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)]:
177 'Last entry{0}{1}{2} has the same timestamp as '
178 + 'before the sleep'.format(
179 '' if node_str == '' else ' for {}'.format(
181 '' if resource_id_substring == ''
182 else ', substring "{}"'.format(
183 resource_id_substring),
184 '' if criterion is None else
185 ' for criterion {}'.format(criterion)))
189 logger.warning('After sleep new entries were not found.')
191 logger.error('This was the last attempt.')
193 logger.info('All latest entries found.')
197 def test_csv_handles_plugin_data(
198 compute, interval, plugin, plugin_subdirs, meter_categories,
200 """Check that CSV data are updated by the plugin.
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
211 Return boolean value indicating success or failure.
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')
226 'Checking that last two entries in metrics are corresponding'
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:
234 'Time of last two entries differ by '
235 + '{}, but interval is {}'.format(
236 metric[3] - metric[2], interval))
242 # wait Interval time + 2 sec
243 sleep_time = interval + 2
245 'Sleeping for {} seconds to get updated entries '.format(sleep_time)
246 + '(interval is {} sec)...'.format(interval))
247 time.sleep(sleep_time)
249 logger.info('Getting new metrics of compute node {}...'.format(
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')
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')
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]:
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)
283 logger.info('set collectd PASS')
285 logger.error('set collectd FAIL')