1 # -*- coding: utf-8 -*-
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
16 # Patch on October 10 2017
18 """Executing test of DMA"""
32 class DMAClient(object):
33 """Client to request DMA"""
34 def __init__(self, host, port, user, passwd):
48 logger.error('Do nothing to DMA')
51 return ('host: {0}, port: {1}, user: {2}, pass: {3}'
52 .format(self._host, self._port,
53 self._user, (self._passwd and '<Filterd>')))
56 class RestDMAClient(DMAClient):
57 """Client to request DMA using REST"""
58 def __init__(self, host, port, user, passwd):
59 super(self.__class__, self).__init__(host, port, user, passwd)
62 logger.debug('Send to DMA using REST -- {}'.format(str(self)))
64 if not os.path.isfile(file):
65 print '{} is not found'.format(file)
67 filename = os.path.basename(file)
69 url = 'http://{0}:{1}/collectd/conf'.format(self._host, self._port)
70 config = {'file': (filename, open(file, 'r'))}
71 requests.post(url, files=config)
76 class PubDMAClient(DMAClient):
77 """Client to request DMA using AMQP Publish"""
78 def __init__(self, host, port, user, passwd):
79 super(self.__class__, self).__init__(host, port, user, passwd)
82 logger.debug('Send to DMA using AMQP Publish -- {}'
85 if not os.path.isfile(file):
86 print '{} is not found'.format(file)
88 filename = os.path.basename(file)
89 filebody = open(file, 'r').read()
90 message = filename + '/' + filebody
92 credentials = pika.PlainCredentials(self._user, self._passwd)
93 connection = pika.BlockingConnection(pika.ConnectionParameters(
94 host=self._host, port=int(self._port),
95 credentials=credentials))
96 channel = connection.channel()
97 channel.exchange_declare(exchange='collectd-conf',
98 exchange_type='fanout')
99 channel.basic_publish(exchange='collectd-conf',
107 def _process_dma_result(compute_node, testfunc,
108 result, results_list, node):
109 """Print DMA test result and append it to results list.
112 testfunc -- DMA function name
113 result -- boolean test result
114 results_list -- results list
118 'Test case for {0} with DMA PASSED on {1}.'.format(
122 'Test case for {0} with DMA FAILED on {1}.'.format(
124 results_list.append((compute_node, "DMA", testfunc, result))
127 def _print_result_of_dma(compute_ids, results):
128 """Print results of DMA.
131 compute_ids -- list of compute node IDs
132 results -- results list
134 compute_node_names = ['Node-{}'.format(i) for i in range(
136 all_computes_in_line = ''
137 for compute in compute_node_names:
138 all_computes_in_line += '| ' + compute + (' ' * (7 - len(compute)))
139 line_of_nodes = '| Test ' + all_computes_in_line + '|'
140 logger.info('=' * 70)
141 logger.info('+' + ('-' * ((9 * len(compute_node_names))+16)) + '+')
143 '|' + ' ' * ((9*len(compute_node_names))/2)
146 9*len(compute_node_names) - (9*len(compute_node_names))/2)
149 '+' + ('-' * 16) + '+' + (('-' * 8) + '+') * len(compute_node_names))
150 logger.info(line_of_nodes)
152 '+' + ('-' * 16) + '+' + (('-' * 8) + '+') * len(compute_node_names))
156 for id in compute_ids:
159 testfunc for comp_id, testname, testfunc, res in results
160 if comp_id == id and not res] else 'PASS'
161 print_line += '| ' + all_result + ' '
163 '| {}'.format(testname) + (' ' * (15 - len(testname)))
166 for testfunc in ['Server', 'InfoFetch']:
168 for id in compute_ids:
169 if (id, testname, testfunc, True) in results:
170 print_line += ' PASS |'
171 elif (id, testname, testfunc, False) in results:
172 print_line += ' FAIL |'
174 print_line += ' SKIP |'
176 '| {}'.format(testfunc) + (' ' * (14-len(testfunc)))
180 '+' + ('-' * 16) + '+'
181 + (('-' * 8) + '+') * len(compute_node_names))
182 logger.info('=' * 70)
185 def dma_main(bt_logger, conf, computes):
186 """Check DMA of each compute node.
189 bt_logger -- logger instance
190 computes -- compute node list
197 for compute_node in computes:
198 node_id = compute_node.get_id()
199 compute_ids.append(node_id)
201 agent_server_running = conf.is_dma_server_running(compute_node)
202 agent_infofetch_running = (
203 conf.is_dma_infofetch_running(compute_node) and
204 conf.is_redis_running(compute_node))
206 if agent_server_running:
207 test_name = 'barotest'
208 tmpfile = TEMP_DIR + '/' + test_name + '.conf'
210 agent_config = conf.get_dma_config(compute_node)
211 listen_ip = compute_node.get_ip()
212 listen_port = agent_config.get('server').get('listen_port')
213 amqp_host = agent_config.get('server').get('amqp_host')
214 amqp_port = agent_config.get('server').get('amqp_port')
215 amqp_user = agent_config.get('server').get('amqp_user')
216 amqp_passwd = agent_config.get('server').get('amqp_password')
217 rest_client = RestDMAClient(
218 listen_ip, listen_port, '', '')
219 pub_client = PubDMAClient(
220 amqp_host, amqp_port, amqp_user,
224 for client in [rest_client, pub_client]:
225 tests.test_dma_server_set_collectd(
226 compute_node, tmpfile, logger, client)
229 'Sleeping for {} seconds'.format(sleep_time)
230 + ' before DMA server test...')
231 time.sleep(sleep_time)
232 res = conf.check_dma_dummy_included(
233 compute_node, test_name)
234 all_res = all_res and res
237 compute_node.get_id(), 'Server',
238 all_res, agent_results, compute_node.get_name())
240 if agent_infofetch_running:
241 test_name = 'barotest'
242 resources = conf.create_testvm(compute_node, test_name)
245 'Sleeping for {} seconds'.format(sleep_time)
246 + ' before DMA infofetch test...')
247 time.sleep(sleep_time)
248 res = conf.test_dma_infofetch_get_data(
249 compute_node, test_name)
250 conf.delete_testvm(resources)
253 compute_node.get_id(), 'InfoFetch',
254 res, agent_results, compute_node.get_name())
256 _print_result_of_dma(compute_ids, agent_results)
258 for res in agent_results:
260 logger.error('Some tests have failed or have not been executed')
261 logger.error('DMA test is Fail')