[ansible][fedora] Update package name
[barometer.git] / baro_tests / dma.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright 2018 OPNFV
4 #
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
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
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
15 # under the License.
16 # Patch on October 10 2017
17
18 """Executing test of DMA"""
19
20 import os
21 import pika
22 import requests
23 import time
24
25 import tests
26
27 logger = None
28
29 TEMP_DIR = '/root'
30
31
32 class DMAClient(object):
33     """Client to request DMA"""
34     def __init__(self, host, port, user, passwd):
35         """
36         Keyword arguments:
37         host -- Host URL
38         port -- Host Port
39         user -- Username
40         passwd -- Password
41         """
42         self._host = host
43         self._port = port
44         self._user = user
45         self._passwd = passwd
46
47     def set(self, file):
48         logger.error('Do nothing to DMA')
49
50     def __str__(self):
51         return ('host: {0}, port: {1}, user: {2}, pass: {3}'
52                 .format(self._host, self._port,
53                         self._user, (self._passwd and '<Filterd>')))
54
55
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)
60
61     def set(self, file):
62         logger.debug('Send to DMA using REST -- {}'.format(str(self)))
63
64         if not os.path.isfile(file):
65             print '{} is not found'.format(file)
66             return False
67         filename = os.path.basename(file)
68
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)
72
73         return True
74
75
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)
80
81     def set(self, file):
82         logger.debug('Send to DMA using AMQP Publish -- {}'
83                      .format(str(self)))
84
85         if not os.path.isfile(file):
86             print '{} is not found'.format(file)
87             return False
88         filename = os.path.basename(file)
89         filebody = open(file, 'r').read()
90         message = filename + '/' + filebody
91
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',
100                               routing_key='',
101                               body=message)
102
103         connection.close()
104         return True
105
106
107 def _process_dma_result(compute_node, testfunc,
108                                result, results_list, node):
109     """Print DMA test result and append it to results list.
110
111     Keyword arguments:
112     testfunc -- DMA function name
113     result -- boolean test result
114     results_list -- results list
115     """
116     if result:
117         logger.info(
118             'Test case for {0} with DMA PASSED on {1}.'.format(
119                 node, testfunc))
120     else:
121         logger.error(
122             'Test case for {0} with DMA FAILED on {1}.'.format(
123                 node, testfunc))
124     results_list.append((compute_node, "DMA", testfunc, result))
125
126
127 def _print_result_of_dma(compute_ids, results):
128     """Print results of DMA.
129
130     Keyword arguments:
131     compute_ids -- list of compute node IDs
132     results -- results list
133     """
134     compute_node_names = ['Node-{}'.format(i) for i in range(
135         len((compute_ids)))]
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)) + '+')
142     logger.info(
143         '|' + ' ' * ((9*len(compute_node_names))/2)
144         + ' DMA TEST       '
145         + ' ' * (
146             9*len(compute_node_names) - (9*len(compute_node_names))/2)
147         + '|')
148     logger.info(
149         '+' + ('-' * 16) + '+' + (('-' * 8) + '+') * len(compute_node_names))
150     logger.info(line_of_nodes)
151     logger.info(
152         '+' + ('-' * 16) + '+' + (('-' * 8) + '+') * len(compute_node_names))
153
154     testname = "DMA"
155     print_line = ''
156     for id in compute_ids:
157         all_result = \
158             'FAIL' if [
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 + '   '
162     logger.info(
163         '| {}'.format(testname) + (' ' * (15 - len(testname)))
164         + print_line + '|')
165
166     for testfunc in ['Server', 'InfoFetch']:
167         print_line = ''
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   |'
173             else:
174                 print_line += ' SKIP   |'
175         logger.info(
176             '|  {}'.format(testfunc) + (' ' * (14-len(testfunc)))
177             + '|' + print_line)
178
179     logger.info(
180         '+' + ('-' * 16) + '+'
181         + (('-' * 8) + '+') * len(compute_node_names))
182     logger.info('=' * 70)
183
184
185 def dma_main(bt_logger, conf, computes):
186     """Check DMA of each compute node.
187
188     Keyword arguments:
189     bt_logger -- logger instance
190     computes -- compute node list
191     """
192     global logger
193     logger = bt_logger
194
195     compute_ids = []
196     agent_results = []
197     for compute_node in computes:
198         node_id = compute_node.get_id()
199         compute_ids.append(node_id)
200
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))
205
206         if agent_server_running:
207             test_name = 'barotest'
208             tmpfile = TEMP_DIR + '/' + test_name + '.conf'
209
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,
221                              amqp_passwd)
222
223             all_res = True
224             for client in [rest_client, pub_client]:
225                 tests.test_dma_server_set_collectd(
226                     compute_node, tmpfile, logger, client)
227                 sleep_time = 1
228                 logger.info(
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
235
236             _process_dma_result(
237                 compute_node.get_id(), 'Server',
238                 all_res, agent_results, compute_node.get_name())
239
240         if agent_infofetch_running:
241             test_name = 'barotest'
242             resources = conf.create_testvm(compute_node, test_name)
243             sleep_time = 5
244             logger.info(
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)
251
252             _process_dma_result(
253                 compute_node.get_id(), 'InfoFetch',
254                 res, agent_results, compute_node.get_name())
255
256     _print_result_of_dma(compute_ids, agent_results)
257
258     for res in agent_results:
259         if not res[3]:
260             logger.error('Some tests have failed or have not been executed')
261             logger.error('DMA test is Fail')
262             return 1
263         else:
264             pass
265     return 0