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
14 # Patch on October 10 2017
16 """Executing test of LocalAgent"""
28 class LocalAgentClient(object):
29 """Client to request LocalAgent"""
30 def __init__(self, host, port, user, passwd):
44 logger.error('Do nothing to LocalAgent')
47 return ('host: {0}, port: {1}, user: {2}, pass: {3}'
48 .format(self._host, self._port,
49 self._user, (self._passwd and '<Filterd>')))
52 class RestLocalAgentClient(LocalAgentClient):
53 """Client to request LocalAgent using REST"""
54 def __init__(self, host, port, user, passwd):
55 super(self.__class__, self).__init__(host, port, user, passwd)
58 logger.debug('Send to localagent using REST -- {}'.format(str(self)))
60 if not os.path.isfile(file):
61 print '{} is not found'.format(file)
63 filename = os.path.basename(file)
65 url = 'http://{0}:{1}/collectd/conf'.format(self._host, self._port)
66 config = {'file': (filename, open(file, 'r'))}
67 requests.post(url, files=config)
72 class PubLocalAgentClient(LocalAgentClient):
73 """Client to request LocalAgent using AMQP Publish"""
74 def __init__(self, host, port, user, passwd):
75 super(self.__class__, self).__init__(host, port, user, passwd)
78 logger.debug('Send to localagent using AMQP Publish -- {}'
81 if not os.path.isfile(file):
82 print '{} is not found'.format(file)
84 filename = os.path.basename(file)
85 filebody = open(file, 'r').read()
86 message = filename + '/' + filebody
88 credentials = pika.PlainCredentials(self._user, self._passwd)
89 connection = pika.BlockingConnection(pika.ConnectionParameters(
90 host=self._host, port=int(self._port),
91 credentials=credentials))
92 channel = connection.channel()
93 channel.exchange_declare(exchange='collectd-conf',
94 exchange_type='fanout')
95 channel.basic_publish(exchange='collectd-conf',
103 def _process_localagent_result(compute_node, testfunc,
104 result, results_list, node):
105 """Print LocalAgent test result and append it to results list.
108 testfunc -- localagent function name
109 result -- boolean test result
110 results_list -- results list
114 'Test case for {0} with LocalAgent PASSED on {1}.'.format(
118 'Test case for {0} with LocalAgent FAILED on {1}.'.format(
120 results_list.append((compute_node, "LocalAgent", testfunc, result))
123 def _print_result_of_localagent(compute_ids, results):
124 """Print results of LocalAgent.
127 compute_ids -- list of compute node IDs
128 results -- results list
130 compute_node_names = ['Node-{}'.format(i) for i in range(
132 all_computes_in_line = ''
133 for compute in compute_node_names:
134 all_computes_in_line += '| ' + compute + (' ' * (7 - len(compute)))
135 line_of_nodes = '| Test ' + all_computes_in_line + '|'
136 logger.info('=' * 70)
137 logger.info('+' + ('-' * ((9 * len(compute_node_names))+16)) + '+')
139 '|' + ' ' * ((9*len(compute_node_names))/2)
142 9*len(compute_node_names) - (9*len(compute_node_names))/2)
145 '+' + ('-' * 16) + '+' + (('-' * 8) + '+') * len(compute_node_names))
146 logger.info(line_of_nodes)
148 '+' + ('-' * 16) + '+' + (('-' * 8) + '+') * len(compute_node_names))
150 testname = "LocalAgent"
152 for id in compute_ids:
155 testfunc for comp_id, testname, testfunc, res in results
156 if comp_id == id and not res] else 'PASS'
157 print_line += '| ' + all_result + ' '
159 '| {}'.format(testname) + (' ' * (15 - len(testname)))
162 for testfunc in ['Server', 'InfoFetch']:
164 for id in compute_ids:
165 if (id, testname, testfunc, True) in results:
166 print_line += ' PASS |'
167 elif (id, testname, testfunc, False) in results:
168 print_line += ' FAIL |'
170 print_line += ' SKIP |'
172 '| {}'.format(testfunc) + (' ' * (14-len(testfunc)))
176 '+' + ('-' * 16) + '+'
177 + (('-' * 8) + '+') * len(compute_node_names))
178 logger.info('=' * 70)
181 def local_agent_main(bt_logger, conf, computes):
182 """Check LocalAgent of each compute node.
185 bt_logger -- logger instance
186 computes -- compute node list
193 for compute_node in computes:
194 node_id = compute_node.get_id()
195 compute_ids.append(node_id)
197 agent_server_running = conf.is_localagent_server_running(compute_node)
198 agent_infofetch_running = (
199 conf.is_localagent_infofetch_running(compute_node) and
200 conf.is_redis_running(compute_node))
202 if agent_server_running:
203 test_name = 'barotest'
204 tmpfile = '/tmp/' + test_name + '.conf'
206 agent_config = conf.get_localagent_config(compute_node)
207 listen_ip = compute_node.get_ip()
208 listen_port = agent_config.get('server').get('listen_port')
209 amqp_host = agent_config.get('server').get('amqp_host')
210 amqp_port = agent_config.get('server').get('amqp_port')
211 amqp_user = agent_config.get('server').get('amqp_user')
212 amqp_passwd = agent_config.get('server').get('amqp_password')
213 rest_client = RestLocalAgentClient(
214 listen_ip, listen_port, '', '')
215 pub_client = PubLocalAgentClient(
216 amqp_host, amqp_port, amqp_user,
220 for client in [rest_client, pub_client]:
221 tests.test_localagent_server_set_collectd(
222 compute_node, tmpfile, logger, client)
225 'Sleeping for {} seconds'.format(sleep_time)
226 + ' before localagent server test...')
227 time.sleep(sleep_time)
228 res = conf.check_localagent_dummy_included(
229 compute_node, test_name)
230 all_res = all_res and res
232 _process_localagent_result(
233 compute_node.get_id(), 'Server',
234 all_res, agent_results, compute_node.get_name())
236 if agent_infofetch_running:
237 test_name = 'barotest'
238 resources = conf.create_testvm(compute_node, test_name)
241 'Sleeping for {} seconds'.format(sleep_time)
242 + ' before localagent infofetch test...')
243 time.sleep(sleep_time)
244 res = conf.test_localagent_infofetch_get_data(
245 compute_node, test_name)
246 conf.delete_testvm(resources)
248 _process_localagent_result(
249 compute_node.get_id(), 'InfoFetch',
250 res, agent_results, compute_node.get_name())
252 _print_result_of_localagent(compute_ids, agent_results)
254 for res in agent_results:
256 logger.error('Some tests have failed or have not been executed')
257 logger.error('LocalAgent test is Fail')