summaryrefslogtreecommitdiffhomepage
path: root/test/scenario_test/bgp_router_test.py
blob: 1a72c3e584e91dd98efcfb055c36d9df5cdcf67c (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
#
# 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.

import unittest
from fabric.api import local
from lib import base
from lib.gobgp import *
from lib.quagga import *
from lib.exabgp import *
import sys
import os
import time
import nose
from noseplugin import OptionParser, parser_option
from itertools import chain
import ryu.lib.pcaplib as pcap
from ryu.lib.packet.packet import Packet
from ryu.lib.packet.bgp import BGPMessage, BGPUpdate


class GoBGPTestBase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        gobgp_ctn_image_name = parser_option.gobgp_image
        base.TEST_PREFIX = parser_option.test_prefix

        g1 = GoBGPContainer(name='g1', asn=65000, router_id='192.168.0.1',
                            ctn_image_name=gobgp_ctn_image_name,
                            log_level=parser_option.gobgp_log_level)
        q1 = QuaggaBGPContainer(name='q1', asn=65001, router_id='192.168.0.2')
        q2 = QuaggaBGPContainer(name='q2', asn=65002, router_id='192.168.0.3')
        q3 = QuaggaBGPContainer(name='q3', asn=65003, router_id='192.168.0.4')

        qs = [q1, q2, q3]
        ctns = [g1, q1, q2, q3]

        # advertise a route from q1, q2, q3
        for idx, q in enumerate(qs):
            route = '10.0.{0}.0/24'.format(idx+1)
            q.add_route(route)

        initial_wait_time = max(ctn.run() for ctn in ctns)

        time.sleep(initial_wait_time)

        for q in qs:
            g1.add_peer(q, reload_config=False, passwd='passwd')
            q.add_peer(g1, passwd='passwd', passive=True)

        g1.create_config()
        g1.reload_config()

        cls.gobgp = g1
        cls.quaggas = {'q1': q1, 'q2': q2, 'q3': q3}

    # test each neighbor state is turned establish
    def test_01_neighbor_established(self):
        for q in self.quaggas.itervalues():
            self.gobgp.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=q)

    def test_02_check_gobgp_global_rib(self):
        for q in self.quaggas.itervalues():
            # paths expected to exist in gobgp's global rib
            routes = q.routes.keys()
            timeout = 120
            interval = 1
            count = 0

            while True:
                # gobgp's global rib
                state = self.gobgp.get_neighbor_state(q)
                self.assertEqual(state, BGP_FSM_ESTABLISHED)
                global_rib = [p['prefix'] for p in self.gobgp.get_global_rib()]

                for p in global_rib:
                    if p in routes:
                        routes.remove(p)

                if len(routes) == 0:
                    break

                time.sleep(interval)
                count += interval
                if count >= timeout:
                    raise Exception('timeout')

    # check gobgp properly add it's own asn to aspath
    def test_03_check_gobgp_adj_out_rib(self):
        for q in self.quaggas.itervalues():
            for path in self.gobgp.get_adj_rib_out(q):
                asns = path['aspath']
                self.assertTrue(self.gobgp.asn in asns)

    # check routes are properly advertised to all BGP speaker
    def test_04_check_quagga_global_rib(self):
        interval = 1
        timeout = int(120/interval)
        for q in self.quaggas.itervalues():
            done = False
            for _ in range(timeout):
                if done:
                    break
                global_rib = q.get_global_rib()
                global_rib = [p['prefix'] for p in global_rib]
                if len(global_rib) < len(self.quaggas):
                    time.sleep(interval)
                    continue

                self.assertTrue(len(global_rib) == len(self.quaggas))

                for c in self.quaggas.itervalues():
                    for r in c.routes:
                        self.assertTrue(r in global_rib)
                done = True
            if done:
                continue
            # should not reach here
            self.assertTrue(False)

    def test_05_add_quagga(self):
        q4 = QuaggaBGPContainer(name='q4', asn=65004, router_id='192.168.0.5')
        self.quaggas['q4'] = q4

        q4.add_route('10.0.4.0/24')

        initial_wait_time = q4.run()
        time.sleep(initial_wait_time)
        self.gobgp.add_peer(q4)
        q4.add_peer(self.gobgp)

        self.gobgp.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=q4)

    def test_06_check_global_rib(self):
        self.test_02_check_gobgp_global_rib()
        self.test_04_check_quagga_global_rib()

    def test_07_stop_one_quagga(self):
        g1 = self.gobgp
        q4 = self.quaggas['q4']
        q4.stop()
        self.gobgp.wait_for(expected_state=BGP_FSM_ACTIVE, peer=q4)

        g1.del_peer(q4)
        del self.quaggas['q4']

    # check gobgp properly send withdrawal message with q4's route
    def test_08_check_global_rib(self):
        self.test_02_check_gobgp_global_rib()
        self.test_04_check_quagga_global_rib()

    def test_09_add_distant_relative(self):
        q1 = self.quaggas['q1']
        q2 = self.quaggas['q2']
        q3 = self.quaggas['q3']
        q5 = QuaggaBGPContainer(name='q5', asn=65005, router_id='192.168.0.6')

        initial_wait_time = q5.run()
        time.sleep(initial_wait_time)

        for q in [q2, q3]:
            q5.add_peer(q)
            q.add_peer(q5)

        med200 = {'name': 'med200',
                  'type': 'permit',
                  'match': '0.0.0.0/0',
                  'med': 200}
        q2.add_policy(med200, self.gobgp, 'out')
        med100 = {'name': 'med100',
                  'type': 'permit',
                  'match': '0.0.0.0/0',
                  'med': 100}
        q3.add_policy(med100, self.gobgp, 'out')

        q5.add_route('10.0.6.0/24')

        self.gobgp.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=q2)
        self.gobgp.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=q3)
        q2.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=q5)
        q3.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=q5)

        timeout = 120
        interval = 1
        count = 0
        while True:
            paths = self.gobgp.get_adj_rib_out(q1, '10.0.6.0/24')
            if len(paths) > 0:
                path = paths[0]
                print "{0}'s nexthop is {1}".format(path['nlri']['prefix'],
                                                    path['nexthop'])
                n_addrs = [i[1].split('/')[0] for i in self.gobgp.ip_addrs]
                if path['nexthop'] in n_addrs:
                    break

            time.sleep(interval)
            count += interval
            if count >= timeout:
                raise Exception('timeout')

    def test_10_originate_path(self):
        self.gobgp.add_route('10.10.0.0/24')
        dst = self.gobgp.get_global_rib('10.10.0.0/24')
        self.assertTrue(len(dst) == 1)
        self.assertTrue(len(dst[0]['paths']) == 1)
        path = dst[0]['paths'][0]
        self.assertTrue(path['nexthop'] == '0.0.0.0')
        self.assertTrue(len(path['aspath']) == 0)

    def test_11_check_adj_rib_out(self):
        for q in self.quaggas.itervalues():
            paths = self.gobgp.get_adj_rib_out(q, '10.10.0.0/24')
            self.assertTrue(len(paths) == 1)
            path = paths[0]
            peer_info = self.gobgp.peers[q]
            local_addr = peer_info['local_addr'].split('/')[0]
            self.assertTrue(path['nexthop'] == local_addr)
            self.assertTrue(path['aspath'] == [self.gobgp.asn])

    def test_12_disable_peer(self):
        q1 = self.quaggas['q1']
        self.gobgp.disable_peer(q1)
        self.gobgp.wait_for(expected_state=BGP_FSM_IDLE, peer=q1)

        time.sleep(3)

        for route in q1.routes.iterkeys():
            dst = self.gobgp.get_global_rib(route)
            self.assertTrue(len(dst) == 0)

            for q in self.quaggas.itervalues():
                if q is q1:
                    continue
                paths = self.gobgp.get_adj_rib_out(q, route)
                self.assertTrue(len(paths) == 0)

    def test_13_enable_peer(self):
        q1 = self.quaggas['q1']
        self.gobgp.enable_peer(q1)
        self.gobgp.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=q1)

    def test_14_check_adj_rib_out(self):
        self.test_11_check_adj_rib_out()

    def test_15_check_active_connection(self):
        g1 = self.gobgp
        g2 = GoBGPContainer(name='g2', asn=65000, router_id='192.168.0.7',
                            ctn_image_name=self.gobgp.image,
                            log_level=parser_option.gobgp_log_level)
        time.sleep(g2.run())
        self.quaggas['g2'] = g2
        g2.add_peer(g1, passive=True)
        g1.add_peer(g2)
        g1.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=g2)

    def test_16_check_local_pref_and_med_handling(self):
        g1 = self.gobgp
        g1.add_route('10.20.0.0/24', local_pref=1000, med=2000)
        # iBGP peer
        g2 = self.quaggas['g2']
        paths = g2.get_global_rib('10.20.0.0/24')
        self.assertTrue(len(paths) == 1)
        self.assertTrue(len(paths[0]['paths']) == 1)
        path = paths[0]['paths'][0]
        local_pref = extract_path_attribute(path, BGP_ATTR_TYPE_LOCAL_PREF)
        self.assertTrue(local_pref['value'] == 1000)
        med = extract_path_attribute(path, BGP_ATTR_TYPE_MULTI_EXIT_DISC)
        self.assertTrue(med['metric'] == 2000)

        # eBGP peer
        q1 = self.quaggas['q1']
        paths = q1.get_global_rib('10.20.0.0/24')
        self.assertTrue(len(paths) == 1)
        path = paths[0]
        local_pref = extract_path_attribute(path, BGP_ATTR_TYPE_LOCAL_PREF)
        # local_pref's default value is 100
        self.assertTrue(local_pref['value'] == 100)
        med = extract_path_attribute(path, BGP_ATTR_TYPE_MULTI_EXIT_DISC)
        self.assertTrue(med['metric'] == 2000)

    def test_17_check_shutdown(self):
        g1 = self.gobgp
        q1 = self.quaggas['q1']
        q2 = self.quaggas['q2']
        q3 = self.quaggas['q3']

        q2.add_route('20.0.0.0/24')
        q3.add_route('20.0.0.0/24')

        self.test_01_neighbor_established()

        self.test_02_check_gobgp_global_rib()

        paths = q1.get_global_rib('20.0.0.0/24')
        self.assertTrue(len(paths) == 1)
        n_addrs = [i[1].split('/')[0] for i in self.gobgp.ip_addrs]
        self.assertTrue(paths[0]['nexthop'] in n_addrs)

        q3.stop()

        time.sleep(3)

        paths = q1.get_global_rib('20.0.0.0/24')
        self.assertTrue(len(paths) == 1)
        self.assertTrue(paths[0]['nexthop'] in n_addrs)

        g1.del_peer(q3)
        del self.quaggas['q3']

    def test_18_check_withdrawal(self):
        g1 = self.gobgp
        q1 = self.quaggas['q1']
        q2 = self.quaggas['q2']

        g1.add_route('30.0.0.0/24')
        q1.add_route('30.0.0.0/24')

        self.test_01_neighbor_established()

        self.test_02_check_gobgp_global_rib()

        paths = g1.get_adj_rib_out(q1, '30.0.0.0/24')
        self.assertTrue(len(paths) == 1)
        self.assertTrue('source-id' not in paths[0])
        paths = g1.get_adj_rib_out(q2, '30.0.0.0/24')
        self.assertTrue(len(paths) == 1)
        self.assertTrue('source-id' not in paths[0])

        g1.local('gobgp global rib del 30.0.0.0/24')

        paths = g1.get_adj_rib_out(q1, '30.0.0.0/24')
        self.assertTrue(len(paths) == 0)
        paths = g1.get_adj_rib_out(q2, '30.0.0.0/24')
        self.assertTrue(len(paths) == 1)
        self.assertTrue(paths[0]['source-id'] == '192.168.0.2')

    def test_19_check_grpc_add_neighbor(self):
        g1 = self.gobgp
        e1 = ExaBGPContainer(name='e1', asn=65000, router_id='192.168.0.7')
        time.sleep(e1.run())
        e1.add_peer(g1)
        n = e1.peers[g1]['local_addr'].split('/')[0]
        g1.local('gobgp n add {0} as 65000'.format(n))
        g1.add_peer(e1, reload_config=False)

        g1.wait_for(expected_state=BGP_FSM_ESTABLISHED, peer=e1)

    def test_20_check_withdrawal_2(self):
        g1 = self.gobgp
        g2 = self.quaggas['g2']

        dumpfile = g2.start_tcpdump()

        g1.add_route('10.40.0.0/24')

        time.sleep(1)

        paths = g2.get_global_rib('10.40.0.0/24')
        self.assertTrue(len(paths) == 1)

        g1.local('gobgp global rib del 10.40.0.0/24')

        time.sleep(1)

        paths = g2.get_global_rib('10.40.0.0/24')
        self.assertTrue(len(paths) == 0)

        g2.stop_tcpdump()
        time.sleep(1)

        cnt = 0
        for _, buf in pcap.Reader(open(dumpfile)):
            pkt = Packet(buf).get_protocol(BGPMessage)
            if isinstance(pkt, BGPUpdate):
                cnt += len(pkt.withdrawn_routes)

        self.assertTrue(cnt == 1)

    def test_21_check_cli_sorted(self):
        g1 = self.gobgp
        cnt = 0

        def next_prefix():
            for i in range(100, 105):
                for j in range(100, 105):
                    yield '{0}.{1}.0.0/24'.format(i, j)

        for p in next_prefix():
            g1.local('gobgp global rib add {0}'.format(p))
            cnt += 1

        cnt2 = 0
        g = next_prefix()
        n = g.next()
        for path in g1.local("gobgp global rib", capture=True).split('\n')[1:]:
            if [elem for elem in path.split(' ') if elem != ''][1] == n:
                try:
                    cnt2 += 1
                    n = g.next()
                except StopIteration:
                    break

        self.assertTrue(cnt == cnt2)


    def test_22_check_withdrawal3(self):
        gobgp_ctn_image_name = parser_option.gobgp_image
        g1 = self.gobgp
        g2 = GoBGPContainer(name='g2', asn=65006, router_id='192.168.0.8',
                            ctn_image_name=gobgp_ctn_image_name,
                            log_level=parser_option.gobgp_log_level)
        g3 = GoBGPContainer(name='g3', asn=65007, router_id='192.168.0.9',
                            ctn_image_name=gobgp_ctn_image_name,
                            log_level=parser_option.gobgp_log_level)

        initial_wait_time = max(ctn.run() for ctn in [g2, g3])
        time.sleep(initial_wait_time)

        self.quaggas = {'g2': g2, 'g3': g3}

        g2.local('gobgp global rib add 50.0.0.0/24')
        g3.local('gobgp global rib add 50.0.0.0/24 med 10')

        g1.add_peer(g2)
        g2.add_peer(g1)
        g1.add_peer(g3)
        g3.add_peer(g1)

        self.test_01_neighbor_established()

        self.test_02_check_gobgp_global_rib()

        paths = g1.get_adj_rib_out(g2, '50.0.0.0/24')
        self.assertTrue(len(paths) == 0)
        paths = g1.get_adj_rib_out(g3, '50.0.0.0/24')
        self.assertTrue(len(paths) == 1)
        self.assertTrue(paths[0]['source-id'] == '192.168.0.8')

        g2.local('gobgp global rib del 50.0.0.0/24')

        paths = g1.get_adj_rib_out(g2, '50.0.0.0/24')
        self.assertTrue(len(paths) == 1)
        self.assertTrue(paths[0]['source-id'] == '192.168.0.9')
        paths = g1.get_adj_rib_out(g3, '50.0.0.0/24')
        self.assertTrue(len(paths) == 0)


if __name__ == '__main__':
    if os.geteuid() is not 0:
        print "you are not root."
        sys.exit(1)
    output = local("which docker 2>&1 > /dev/null ; echo $?", capture=True)
    if int(output) is not 0:
        print "docker not found"
        sys.exit(1)

    nose.main(argv=sys.argv, addplugins=[OptionParser()],
              defaultTest=sys.argv[0])