summaryrefslogtreecommitdiffstats
path: root/test/functest/gluon-test-suite.py
blob: b712f8fc91d5029dcfd59af71df844dbb9eb6d99 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/python
#
# Copyright (c) 2017 Ericsson and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
from opnfv.deployment import factory
import functest.utils.openstack_utils as osutils
from functest.utils.constants import CONST

import json
import logging
import os
import sys
import time

logger = logging.getLogger('netready-gluon-vping')
logger.setLevel(logging.DEBUG)


class GluonVPing:

    def __init__(self):
        self.controller_node = None
        self.port_ids = []
        self.vpn_id = None
        self.vpnbinding_ids = []
        self.novaclient = osutils.get_nova_client()
        self.neutronclient = osutils.get_neutron_client()
        self.image_id = None
        self.flavor_id = None
        self.instance_1 = None
        self.instance_2 = None
        self.vm_boot_timeout = 180

    def prepare_test(self):
        logger.info("Preparing Gluon vPing testcase.")

        undercloud_ip = os.getenv("INSTALLER_IP")
        handler = factory.Factory.get_handler('apex',
                                              undercloud_ip,
                                              'stack',
                                              pkey_file='/root/.ssh/id_rsa')
        nodes = handler.get_nodes()
        self.controller_node = None
        for node in nodes:
            if node.is_controller():
                self.controller_node = node
                break

        if self.controller_node is None:
            logger.error("ERROR: no controller node found.")
            raise Exception('No controller node found')

    def run(self):
        self._prepare_image()
        self._prepare_flavor()
        self._create_ports()
        self._create_vpn()
        self._create_vpnbinding()
        self._boot_vms()

    def _run_protonclient(self, options):
        cmd = "protonclient --api net-l3vpn " + options
        if self.controller_node is not None:
            logger.debug("Running command: %s" % cmd)
            output = self.controller_node.run_cmd(cmd)
            if output is None:
                raise Exception('Remote command failed')
            else:
                return output

    def _create_ports(self):
        def _build_create_port_options(name, mac, tenant_id):
            return 'port-create' \
                ' --mac_address ' + mac + \
                ' --mtu 1500' \
                ' --admin_state_up True' \
                ' --name ' + name + \
                ' --vlan_transparency True' \
                ' --vnic_type normal' \
                ' --vif_type ovs' \
                ' --status ACTIVE' \
                ' --tenant_id ' + tenant_id

        tenant_id = osutils.get_tenant_id(
            osutils.get_keystone_client(),
            'admin')
        for i in [1, 2]:
            options = _build_create_port_options('gluonPort' + str(i),
                                                 'c0:2a:14:04:43:0' + str(i),
                                                 tenant_id)
            output = self._run_protonclient(options)
            port_data = json.loads(output)

            logger.info('Port %s created.' % port_data['id'])
            self.port_ids.append(port_data['id'])
            time.sleep(1)

    def _create_vpn(self):
        vpn_options = 'vpn-create '\
                      '--name "GluonVPN" ' \
                      '--ipv4_family 1000:1000 ' \
                      '--ipv6_family 1000:1000 ' \
                      '--route_distinguishers 1000:1000'
        output = self._run_protonclient(vpn_options)
        vpn_data = json.loads(output)

        logger.info('VPN %s created.' % vpn_data['id'])
        self.vpn_id = vpn_data['id']
        time.sleep(1)

    def _create_vpnbinding(self):
        def _build_create_vpnbinding(port_id, counter):
            return 'vpnbinding-create' \
                   ' --interface_id ' + port_id + \
                   ' --service_id ' + self.vpn_id + \
                   ' --ipaddress 10.10.' + str(counter) + '.2' \
                   ' --subnet_prefix 24' \
                   ' --gateway 10.10.' + str(counter) + '.1'

        counter = 0
        for port_id in self.port_ids:
            options = _build_create_vpnbinding(port_id, counter)
            output = self._run_protonclient(options)
            vpnbinding_data = json.loads(output)

            logger.info('VPN-binding %s created.'
                        % vpnbinding_data['interface_id'])
            self.vpnbinding_ids.append(vpnbinding_data['interface_id'])
            counter += 1
            time.sleep(1)

    def _cleanup_ports(self):
        for port_id in self.port_ids:
            logger.info("Deleting port %s" % port_id)
            self._run_protonclient('port-delete ' + str(port_id))

    def _cleanup_vpn(self):
        logger.info("Deleting VPN %s" % self.vpn_id)
        self._run_protonclient('vpn-delete ' + str(self.vpn_id))

    def _cleanup_vpnbindings(self):
        for vpnbinding_id in self.vpnbinding_ids:
            logger.info("Deleting VPN-binding %s" % vpnbinding_id)
            self._run_protonclient('vpnbinding-delete ' + str(vpnbinding_id))

    def cleanup(self):
        logger.info("Cleaning up Gluon vPing testcase...")
        self._cleanup_instances()
        self._cleanup_vpnbindings()
        self._cleanup_vpn()
        self._cleanup_ports()
        self._cleanup_image()
        self._cleanup_flavor()

    def _prepare_image(self):
        image_name = 'gluon-test-image'
        self.image_id = osutils.get_image_id(osutils.get_glance_client(),
                                             image_name)
        if self.image_id == '':
            image_filename = CONST.openstack_image_file_name
            image_format = CONST.openstack_image_disk_format
            image_path = '{0}/{1}'.format(CONST.dir_functest_data,
                                          image_filename)

            logger.info('Creating Glance image.')
            self.image_id = \
                osutils.create_glance_image(osutils.get_glance_client(),
                                            image_name,
                                            image_path,
                                            image_format,
                                            container='bare',
                                            public='public')
            if self.image_id is None:
                raise Exception('Glance image creation failed')
        else:
            logger.info('Test image %s already exists' % image_name)

    def _cleanup_image(self):
        logger.info('Deleting Glance image.')
        if not osutils.delete_glance_image(self.novaclient, self.image_id):
            raise Exception('Image deletion failed.')

    def _prepare_flavor(self):
        logger.info('Creating Nova flavor.')
        self.flavor_id = osutils.get_flavor_id(self.novaclient, 'gluon-flavor')
        if self.flavor_id == '':
            self.flavor_id = osutils.create_flavor(self.novaclient,
                                                   'gluon-flavor',
                                                   512, 1, 1)
            if self.flavor_id is None:
                raise Exception('Nova flavor creation failed')

    def _cleanup_flavor(self):
        logger.info('Deleting flavor')
        self.novaclient.flavors.delete(self.flavor_id)

    def _wait_for_instance(self, instance):
        if not self._wait_for_instance_status_change(self.novaclient,
                                                     instance):
            vm_status = osutils.get_instance_status(self.novaclient, instance)
            logger.error("Instance '%s' cannot be booted. Status is '%s'"
                         % (instance, vm_status))
            return None
        else:
            logger.info("Instance '%s' is ACTIVE." % instance)

    def _wait_for_instance_status_change(self, nova, vm):
        sleep_time = 3
        count = self.vm_boot_timeout / sleep_time
        while True:
            status = osutils.get_instance_status(nova, vm)
            logger.debug("Status: %s" % status)
            if status == "ACTIVE":
                return True
            if status == "ERROR" or status == "error":
                return False
            if count == 0:
                logger.debug("Booting a VM timed out...")
                return False
            count -= 1
            time.sleep(sleep_time)

    def _build_user_data(self, instance_ip, gw_ip, test_ip):
        return ("#!/bin/sh\n\n"
                "killall -9 udhcpc\n"
                "ifconfig eth0 %s netmask 255.255.255.0\n"
                "route add default gw %s\n"
                "while true; do\n"
                " ping -c 1 %s 2>&1 >/dev/null\n"
                " RES=$?\n"
                " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
                "  echo 'vPing OK'\n"
                "  break\n"
                " else\n"
                "  echo 'vPing KO'\n"
                " fi\n"
                " sleep 1\n"
                "done\n" % (instance_ip, gw_ip, test_ip))

    def _boot_vms(self):
        neutron_ports = self.neutronclient.list_ports()
        neutron_port_ids = [p['id'] for p in neutron_ports['ports']]
        logger.info("Neutron port IDs: %s" % neutron_port_ids)

        for port_id in self.port_ids:
            if port_id not in neutron_port_ids:
                raise Exception('Gluon port %s not found among existing '
                                'Neutron ports' % port_id)

        ip_instance_1 = '10.10.0.2'
        gw_instance_1 = '10.10.0.1'

        ip_instance_2 = '10.10.1.2'
        gw_instance_2 = '10.10.1.1'

        logger.info('Booting first instance.')
        user_data_1 = self._build_user_data(ip_instance_1,
                                            gw_instance_1,
                                            ip_instance_2)
        self.instance_1 = \
            self.novaclient.servers.create(name='Gluon VM 1',
                                           flavor=self.flavor_id,
                                           image=self.image_id,
                                           nics=
                                           [{"port-id": self.port_ids[0]}],
                                           config_drive=True,
                                           userdata=user_data_1)
        self._wait_for_instance(self.instance_1)

        logger.info('Booting second instance.')
        user_data_2 = self._build_user_data(ip_instance_2,
                                            gw_instance_2,
                                            ip_instance_1)
        self.instance_2 = \
            self.novaclient.servers.create(name='Gluon VM 2',
                                           flavor=self.flavor_id,
                                           image=self.image_id,
                                           nics=
                                           [{"port-id": self.port_ids[1]}],
                                           config_drive=True,
                                           userdata=user_data_2)
        self._wait_for_instance(self.instance_2)

        error_code = self._check_ping(self.instance_1, ip_instance_2, 200)
        if error_code != 0:
            raise Exception('Ping of instance 1 to %s failed' % ip_instance_2)

        error_code = self._check_ping(self.instance_2, ip_instance_1, 200)
        if error_code != 0:
            raise Exception('Ping of instance 2 to %s failed' % ip_instance_1)

    def _check_ping(self, instance, test_ip, timeout):
        logger.info("Waiting for ping...")
        EXIT_CODE = -1
        sec = 0
        tries = 0

        while True:
            time.sleep(1)
            p_console = instance.get_console_output()
            if "vPing OK" in p_console:
                logger.info("vPing detected!")
                EXIT_CODE = 0
                break
            elif "failed to read iid from metadata" in p_console or tries > 5:
                EXIT_CODE = -2
                break
            elif sec == timeout:
                logger.info("Timeout reached.")
                break
            elif sec % 10 == 0:
                if "request failed" in p_console:
                    logger.debug("It seems userdata is not supported "
                                 "in nova boot. Waiting a bit...")
                    tries += 1
                else:
                    logger.debug("Pinging %s. Waiting for response..."
                                 % test_ip)
            sec += 1

        return EXIT_CODE

    def _cleanup_instances(self):
        logger.info('Deleting instances.')
        self.novaclient.servers.delete(self.instance_1.id)
        self.novaclient.servers.delete(self.instance_2.id)


def main():
    logger.info("Running Gluon test suite...")

    try:
        gluon_vping = GluonVPing()
        gluon_vping.prepare_test()
        gluon_vping.run()
        gluon_vping.cleanup()
    except Exception as e:
        logger.info("Gluon test suite failed: %s" % e)
        return 1

    logger.info("Gluon test suite successfully completed.")
    return 0


if __name__ == '__main__':
    logging.basicConfig()
    sys.exit(main())