aboutsummaryrefslogtreecommitdiffstats
path: root/baro_tests/local_agent.py
blob: 8201dee051aa8ee4c737ee9dd35a313e7ce3d4d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# -*- coding: utf-8 -*-

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# Patch on October 10 2017

"""Executing test of LocalAgent"""

import os
import pika
import requests
import time

import tests

logger = None


class LocalAgentClient(object):
    """Client to request LocalAgent"""
    def __init__(self, host, port, user, passwd):
        """
        Keyword arguments:
        host -- Host URL
        port -- Host Port
        user -- Username
        passwd -- Password
        """
        self._host = host
        self._port = port
        self._user = user
        self._passwd = passwd

    def set(self, file):
        logger.error('Do nothing to LocalAgent')

    def __str__(self):
        return ('host: {0}, port: {1}, user: {2}, pass: {3}'
                .format(self._host, self._port,
                        self._user, (self._passwd and '<Filterd>')))


class RestLocalAgentClient(LocalAgentClient):
    """Client to request LocalAgent using REST"""
    def __init__(self, host, port, user, passwd):
        super(self.__class__, self).__init__(host, port, user, passwd)

    def set(self, file):
        logger.debug('Send to localagent using REST -- {}'.format(str(self)))

        if not os.path.isfile(file):
            print '{} is not found'.format(file)
            return False
        filename = os.path.basename(file)

        url = 'http://{0}:{1}/collectd/conf'.format(self._host, self._port)
        config = {'file': (filename, open(file, 'r'))}
        requests.post(url, files=config)

        return True


class PubLocalAgentClient(LocalAgentClient):
    """Client to request LocalAgent using AMQP Publish"""
    def __init__(self, host, port, user, passwd):
        super(self.__class__, self).__init__(host, port, user, passwd)

    def set(self, file):
        logger.debug('Send to localagent using AMQP Publish -- {}'
                     .format(str(self)))

        if not os.path.isfile(file):
            print '{} is not found'.format(file)
            return False
        filename = os.path.basename(file)
        filebody = open(file, 'r').read()
        message = filename + '/' + filebody

        credentials = pika.PlainCredentials(self._user, self._passwd)
        connection = pika.BlockingConnection(pika.ConnectionParameters(
                host=self._host, port=int(self._port),
                credentials=credentials))
        channel = connection.channel()
        channel.exchange_declare(exchange='collectd-conf',
                                 exchange_type='fanout')
        channel.basic_publish(exchange='collectd-conf',
                              routing_key='',
                              body=message)

        connection.close()
        return True


def _process_localagent_result(compute_node, testfunc,
                               result, results_list, node):
    """Print LocalAgent test result and append it to results list.

    Keyword arguments:
    testfunc -- localagent function name
    result -- boolean test result
    results_list -- results list
    """
    if result:
        logger.info(
            'Test case for {0} with LocalAgent PASSED on {1}.'.format(
                node, testfunc))
    else:
        logger.error(
            'Test case for {0} with LocalAgent FAILED on {1}.'.format(
                node, testfunc))
    results_list.append((compute_node, "LocalAgent", testfunc, result))


def _print_result_of_localagent(compute_ids, results):
    """Print results of LocalAgent.

    Keyword arguments:
    compute_ids -- list of compute node IDs
    results -- results list
    """
    compute_node_names = ['Node-{}'.format(i) for i in range(
        len((compute_ids)))]
    all_computes_in_line = ''
    for compute in compute_node_names:
        all_computes_in_line += '| ' + compute + (' ' * (7 - len(compute)))
    line_of_nodes = '| Test           ' + all_computes_in_line + '|'
    logger.info('=' * 70)
    logger.info('+' + ('-' * ((9 * len(compute_node_names))+16)) + '+')
    logger.info(
        '|' + ' ' * ((9*len(compute_node_names))/2)
        + ' LOCALAGENT TEST'
        + ' ' * (
            9*len(compute_node_names) - (9*len(compute_node_names))/2)
        + '|')
    logger.info(
        '+' + ('-' * 16) + '+' + (('-' * 8) + '+') * len(compute_node_names))
    logger.info(line_of_nodes)
    logger.info(
        '+' + ('-' * 16) + '+' + (('-' * 8) + '+') * len(compute_node_names))

    testname = "LocalAgent"
    print_line = ''
    for id in compute_ids:
        all_result = \
            'FAIL' if [
                testfunc for comp_id, testname, testfunc, res in results
                if comp_id == id and not res] else 'PASS'
        print_line += '| ' + all_result + '   '
    logger.info(
        '| {}'.format(testname) + (' ' * (15 - len(testname)))
        + print_line + '|')

    for testfunc in ['Server', 'InfoFetch']:
        print_line = ''
        for id in compute_ids:
            if (id, testname, testfunc, True) in results:
                print_line += ' PASS   |'
            elif (id, testname, testfunc, False) in results:
                print_line += ' FAIL   |'
            else:
                print_line += ' SKIP   |'
        logger.info(
            '|  {}'.format(testfunc) + (' ' * (14-len(testfunc)))
            + '|' + print_line)

    logger.info(
        '+' + ('-' * 16) + '+'
        + (('-' * 8) + '+') * len(compute_node_names))
    logger.info('=' * 70)


def local_agent_main(bt_logger, conf, computes):
    """Check LocalAgent of each compute node.

    Keyword arguments:
    bt_logger -- logger instance
    computes -- compute node list
    """
    global logger
    logger = bt_logger

    compute_ids = []
    agent_results = []
    for compute_node in computes:
        node_id = compute_node.get_id()
        compute_ids.append(node_id)

        agent_server_running = conf.is_localagent_server_running(compute_node)
        agent_infofetch_running = (
            conf.is_localagent_infofetch_running(compute_node) and
            conf.is_redis_running(compute_node))

        if agent_server_running:
            test_name = 'barotest'
            tmpfile = '/tmp/' + test_name + '.conf'

            agent_config = conf.get_localagent_config(compute_node)
            listen_ip = compute_node.get_ip()
            listen_port = agent_config.get('server').get('listen_port')
            amqp_host = agent_config.get('server').get('amqp_host')
            amqp_port = agent_config.get('server').get('amqp_port')
            amqp_user = agent_config.get('server').get('amqp_user')
            amqp_passwd = agent_config.get('server').get('amqp_password')
            rest_client = RestLocalAgentClient(
                              listen_ip, listen_port, '', '')
            pub_client = PubLocalAgentClient(
                             amqp_host, amqp_port, amqp_user,
                             amqp_passwd)

            all_res = True
            for client in [rest_client, pub_client]:
                tests.test_localagent_server_set_collectd(
                    compute_node, tmpfile, logger, client)
                sleep_time = 1
                logger.info(
                    'Sleeping for {} seconds'.format(sleep_time)
                    + ' before localagent server test...')
                time.sleep(sleep_time)
                res = conf.check_localagent_dummy_included(
                          compute_node, test_name)
                all_res = all_res and res

            _process_localagent_result(
                compute_node.get_id(), 'Server',
                all_res, agent_results, compute_node.get_name())

        if agent_infofetch_running:
            test_name = 'barotest'
            resources = conf.create_testvm(compute_node, test_name)
            sleep_time = 5
            logger.info(
                'Sleeping for {} seconds'.format(sleep_time)
                + ' before localagent infofetch test...')
            time.sleep(sleep_time)
            res = conf.test_localagent_infofetch_get_data(
                      compute_node, test_name)
            conf.delete_testvm(resources)

            _process_localagent_result(
                compute_node.get_id(), 'InfoFetch',
                res, agent_results, compute_node.get_name())

    _print_result_of_localagent(compute_ids, agent_results)

    for res in agent_results:
        if not res[3]:
            logger.error('Some tests have failed or have not been executed')
            logger.error('LocalAgent test is Fail')
            return 1
        else:
            pass
    return 0