|
| 1 | +from swsscommon import swsscommon |
| 2 | +import os |
| 3 | +import re |
| 4 | +import time |
| 5 | +import json |
| 6 | +import random |
| 7 | + |
| 8 | +def output(dvs, title): |
| 9 | + print "=========================== %s ===========================" % title |
| 10 | + exit_cod, sum_res = dvs.runcmd(["vtysh", "-c", "show ip bgp sum"]) |
| 11 | + exit_code, all_route = dvs.runcmd(["vtysh", "-c", "show ip bgp"]) |
| 12 | + exit_code, neig_1 = dvs.runcmd(["vtysh", "-c", "show ip bgp neighbors 10.0.0.1"]) |
| 13 | + exit_code, announce_route_1 = dvs.runcmd(["vtysh", "-c", "show ip bgp neighbors 10.0.0.1 advertised-routes"]) |
| 14 | + exit_code, received_route_1 = dvs.runcmd(["vtysh", "-c", "show ip bgp neighbors 10.0.0.1 routes"]) |
| 15 | + exit_code, announce_route_3 = dvs.runcmd(["vtysh", "-c", "show ip bgp neighbors 10.0.0.3 advertised-routes"]) |
| 16 | + exit_code, received_route_3 = dvs.runcmd(["vtysh", "-c", "show ip bgp neighbors 10.0.0.3 routes"]) |
| 17 | + print "Summary:" |
| 18 | + print sum_res |
| 19 | + print "Received routes:" |
| 20 | + print "10.0.0.1" |
| 21 | + print received_route_1 |
| 22 | + print "10.0.0.3" |
| 23 | + print received_route_3 |
| 24 | + print "Announces routes:" |
| 25 | + print "10.0.0.1" |
| 26 | + print announce_route_1 |
| 27 | + print "10.0.0.3" |
| 28 | + print announce_route_3 |
| 29 | + print "Neighbors" |
| 30 | + print "10.0.0.1" |
| 31 | + print neig_1 |
| 32 | + print "======================================================" |
| 33 | + |
| 34 | +def mkdir(path): |
| 35 | + if not os.path.exists(path): |
| 36 | + os.makedirs(path) |
| 37 | + |
| 38 | +def remove(path): |
| 39 | + if os.path.exists(path): |
| 40 | + os.unlink(path) |
| 41 | + |
| 42 | +def get_target_env(idx): |
| 43 | + return '/usr/local/etc/exabgp/exabgp_%d.env' % idx |
| 44 | + |
| 45 | +def prepare_exa_env(dvs, idx): |
| 46 | + mkdir('/usr/local/etc') |
| 47 | + mkdir('/usr/local/etc/exabgp') |
| 48 | + tmp_name = '/tmp/env.%d.%d' % (random.randint(0, 10000000), os.getpid()) |
| 49 | + dvs.servers[idx].runcmd("exabgp --fi > %s" % tmp_name) |
| 50 | + with open(tmp_name) as r_fp: |
| 51 | + with open(get_target_env(idx), 'w') as w_fp: |
| 52 | + for line in r_fp: |
| 53 | + if line.startswith('pipename'): |
| 54 | + line = "pipename = 'exabgp.%d'\n" % idx |
| 55 | + w_fp.write(line) |
| 56 | + os.unlink(tmp_name) |
| 57 | + |
| 58 | + |
| 59 | +def run_exa(dvs, idx, cfg): |
| 60 | + prepare_exa_env(dvs, idx) |
| 61 | + run_dir = "/var/run/exabgp" |
| 62 | + mkdir(run_dir) |
| 63 | + fifo_in_path = "%s/exabgp.%d.in" % (run_dir, idx) |
| 64 | + fifo_out_path = "%s/exabgp.%d.out" % (run_dir, idx) |
| 65 | + remove(fifo_in_path) |
| 66 | + remove(fifo_out_path) |
| 67 | + os.mkfifo(fifo_in_path) |
| 68 | + os.mkfifo(fifo_out_path) |
| 69 | + os.chmod(fifo_in_path, 0666) |
| 70 | + os.chmod(fifo_out_path, 0666) |
| 71 | + print "!!! Start exabgp instance %d" % idx |
| 72 | + cmd = "exabgp -d --env %s %s" % (get_target_env(idx), cfg) |
| 73 | + print "Cmd is ___ %s ___" % cmd |
| 74 | + return dvs.servers[idx].runcmd_async(cmd) |
| 75 | + |
| 76 | +def run_exacli(dvs, idx, cmd): |
| 77 | + return dvs.servers[idx].runcmd('exabgpcli --env %s %s' % (get_target_env(idx), cmd)) |
| 78 | + |
| 79 | +def test_gr_livelock(dvs, testlog): |
| 80 | + # update exabgp to version 4.0.10 |
| 81 | + dvs.servers[0].runcmd("pip install 'exabgp==4.0.10' --force-reinstall ") |
| 82 | + # |
| 83 | + dvs.copy_file("/etc/quagga/", "bgp/files/gr_livelock/bgpd.conf") |
| 84 | + dvs.servers[0].runcmd("pkill exabgp") # In case previous test didn't stop exa |
| 85 | + dvs.runcmd("supervisorctl stop bgpd") |
| 86 | + time.sleep(5) |
| 87 | + dvs.runcmd("supervisorctl start bgpd") |
| 88 | + dvs.runcmd("ip addr add 10.0.0.0/31 dev Ethernet0") |
| 89 | + dvs.runcmd("ifconfig Ethernet0 up") |
| 90 | + |
| 91 | + dvs.runcmd("ip addr add 10.0.0.2/31 dev Ethernet4") |
| 92 | + dvs.runcmd("ifconfig Ethernet4 up") |
| 93 | + |
| 94 | + dvs.servers[0].runcmd("ip addr add 10.0.0.1/31 dev eth0") |
| 95 | + dvs.servers[0].runcmd("ifconfig eth0 up") |
| 96 | + |
| 97 | + dvs.servers[1].runcmd("ip addr add 10.0.0.3/31 dev eth0") |
| 98 | + dvs.servers[1].runcmd("ifconfig eth0 up") |
| 99 | + |
| 100 | + time.sleep(5) |
| 101 | + |
| 102 | + # Run two bgp neighbors |
| 103 | + p1 = run_exa(dvs, 0, "bgp/files/gr_livelock/exabgp1.conf") |
| 104 | + p2 = run_exa(dvs, 1, "bgp/files/gr_livelock/exabgp2.conf") |
| 105 | + |
| 106 | + time.sleep(30) |
| 107 | + |
| 108 | + output(dvs, "First neighbor doesn't have GR enabled") |
| 109 | + |
| 110 | + # Check that we announce routes from the 1st neigbbor to 2nd, if 1st neighbor doesn't support graceful restart |
| 111 | + _, announced_routes = dvs.runcmd(["vtysh", "-c", "show ip bgp neighbors 10.0.0.3 advertised-routes"]) |
| 112 | + assert '2.2.2.2/32' in announced_routes |
| 113 | + |
| 114 | + # Stop 1st neighbor |
| 115 | + run_exacli(dvs, 0, 'shutdown') |
| 116 | + p1 = p1.wait() |
| 117 | + |
| 118 | + # Wait until quagga thinks that 1st neighbor was shut down |
| 119 | + time.sleep(300) |
| 120 | + |
| 121 | + # Start the 1st neighbor again with graceful restart enabled |
| 122 | + p1 = run_exa(dvs, 0, "bgp/files/gr_livelock/exabgp1.graceful.conf") |
| 123 | + |
| 124 | + time.sleep(60) |
| 125 | + |
| 126 | + output(dvs, "First neighbor has GR enalbed") |
| 127 | + |
| 128 | + run_exacli(dvs, 0, 'announce route 1.10.0.0/24 next-hop self') |
| 129 | + |
| 130 | + time.sleep(60) |
| 131 | + |
| 132 | + output(dvs, "Announced a new route from the first neighbor") |
| 133 | + |
| 134 | + # Check that we announce all routes from the 1st neighbor |
| 135 | + _, announced_routes = dvs.runcmd(["vtysh", "-c", "show ip bgp neighbors 10.0.0.3 advertised-routes"]) |
| 136 | + assert '2.2.2.2/32' in announced_routes |
| 137 | + assert '1.10.0.0/24' in announced_routes |
| 138 | + |
| 139 | + run_exacli(dvs, 0, 'shutdown') |
| 140 | + p1 = p1.wait() |
| 141 | + |
| 142 | + run_exacli(dvs, 1, 'shutdown') |
| 143 | + p2 = p2.wait() |
0 commit comments