1 """Function for testing collectd plug-ins with different oup plug-ins"""
2 # -*- coding: utf-8 -*-
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
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.
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
32 Return boolean value indicating success or failure.
35 def _search_meterlist_latest_entry(meterlist, node_str, substr=''):
36 """Search for latest entry in meter list
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
43 Return latest entry from meter list which contains given node string
44 and (if defined) subsrting.
46 res = [entry for entry in meterlist if node_str in entry['resource_id']
47 and substr in entry['resource_id']]
55 node_str = 'node-{}'.format(node_id) if node_id else ''
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)))
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
79 while (attempt <= 10) and not is_passed:
81 # wait Interval time + 2 sec for db update
82 sleep_time = interval + 2
84 logger.info('Starting attempt {}'.format(attempt))
86 'Sleeping for {} seconds to get updated entries '.format(sleep_time)
87 + '(interval is {} sec)...'.format(interval))
88 time.sleep(sleep_time)
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)))
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)]:
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)))
119 logger.warning('After sleep new entries were not found.')
121 logger.error('This was the last attempt.')
123 logger.info('All latest entries found.')
127 def test_csv_handles_plugin_data(
128 compute, interval, plugin, plugin_subdirs, meter_categories,
130 """Check that CSV data are updated by the plugin.
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
141 Return boolean value indicating success or failure.
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')
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))
164 # wait Interval time + 2 sec
165 sleep_time = interval + 2
167 'Sleeping for {} seconds to get updated entries '.format(sleep_time)
168 + '(interval is {} sec)...'.format(interval))
169 time.sleep(sleep_time)
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')
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')
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]: