]>
Commit | Line | Data |
---|---|---|
96d423f7 | 1 | #!/usr/bin/env python |
acddc0ed | 2 | # SPDX-License-Identifier: ISC |
96d423f7 HS |
3 | |
4 | # | |
5 | # test_srv6_manager.py | |
6 | # Part of NetDEF Topology Tests | |
7 | # | |
8 | # Copyright (c) 2020 by | |
9 | # LINE Corporation, Hiroki Shirokura <slank.dev@gmail.com> | |
10 | # | |
96d423f7 HS |
11 | |
12 | """ | |
13 | test_srv6_manager.py: | |
14 | Test for SRv6 manager on zebra | |
15 | """ | |
16 | ||
17 | import os | |
18 | import sys | |
19 | import json | |
96d423f7 HS |
20 | import pytest |
21 | import functools | |
22 | ||
23 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
a53c08bc | 24 | sys.path.append(os.path.join(CWD, "../")) |
96d423f7 HS |
25 | |
26 | # pylint: disable=C0413 | |
27 | from lib import topotest | |
28 | from lib.topogen import Topogen, TopoRouter, get_topogen | |
29 | from lib.topolog import logger | |
96d423f7 | 30 | |
9a47e7b2 DS |
31 | pytestmark = [pytest.mark.bgpd, pytest.mark.sharpd] |
32 | ||
96d423f7 HS |
33 | |
34 | def open_json_file(filename): | |
35 | try: | |
36 | with open(filename, "r") as f: | |
37 | return json.load(f) | |
38 | except IOError: | |
39 | assert False, "Could not read file {}".format(filename) | |
40 | ||
41 | ||
96d423f7 | 42 | def setup_module(mod): |
8db751b8 | 43 | tgen = Topogen({None: "r1"}, mod.__name__) |
96d423f7 | 44 | tgen.start_topology() |
96d423f7 HS |
45 | for rname, router in tgen.routers().items(): |
46 | router.run("/bin/bash {}/{}/setup.sh".format(CWD, rname)) | |
a53c08bc CH |
47 | router.load_config( |
48 | TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) | |
49 | ) | |
50 | router.load_config( | |
51 | TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname)) | |
52 | ) | |
53 | router.load_config( | |
54 | TopoRouter.RD_SHARP, os.path.join(CWD, "{}/sharpd.conf".format(rname)) | |
55 | ) | |
96d423f7 HS |
56 | tgen.start_router() |
57 | ||
58 | ||
59 | def teardown_module(mod): | |
60 | tgen = get_topogen() | |
61 | tgen.stop_topology() | |
62 | ||
63 | ||
64 | def test_srv6(): | |
65 | tgen = get_topogen() | |
66 | if tgen.routers_have_failure(): | |
67 | pytest.skip(tgen.errors) | |
a53c08bc | 68 | router = tgen.gears["r1"] |
96d423f7 HS |
69 | |
70 | def _check_srv6_locator(router, expected_locator_file): | |
71 | logger.info("checking zebra locator status") | |
72 | output = json.loads(router.vtysh_cmd("show segment-routing srv6 locator json")) | |
73 | expected = open_json_file("{}/{}".format(CWD, expected_locator_file)) | |
74 | return topotest.json_cmp(output, expected) | |
75 | ||
76 | def _check_sharpd_chunk(router, expected_chunk_file): | |
77 | logger.info("checking sharpd locator chunk status") | |
a2df1e4f | 78 | output = json.loads(router.vtysh_cmd("show sharp segment-routing srv6 json")) |
96d423f7 HS |
79 | expected = open_json_file("{}/{}".format(CWD, expected_chunk_file)) |
80 | return topotest.json_cmp(output, expected) | |
81 | ||
82 | def check_srv6_locator(router, expected_file): | |
83 | func = functools.partial(_check_srv6_locator, router, expected_file) | |
d3a6af08 | 84 | success, result = topotest.run_and_expect(func, None, count=10, wait=0.5) |
a53c08bc | 85 | assert result is None, "Failed" |
96d423f7 HS |
86 | |
87 | def check_sharpd_chunk(router, expected_file): | |
88 | func = functools.partial(_check_sharpd_chunk, router, expected_file) | |
d3a6af08 | 89 | success, result = topotest.run_and_expect(func, None, count=10, wait=0.5) |
a53c08bc | 90 | assert result is None, "Failed" |
96d423f7 | 91 | |
e08c5663 HS |
92 | # FOR DEVELOPER: |
93 | # If you want to stop some specific line and start interactive shell, | |
94 | # please use tgen.mininet_cli() to start it. | |
95 | ||
96d423f7 HS |
96 | logger.info("Test1 for Locator Configuration") |
97 | check_srv6_locator(router, "expected_locators1.json") | |
98 | check_sharpd_chunk(router, "expected_chunks1.json") | |
99 | ||
100 | logger.info("Test2 get chunk for locator loc1") | |
101 | router.vtysh_cmd("sharp srv6-manager get-locator-chunk loc1") | |
102 | check_srv6_locator(router, "expected_locators2.json") | |
103 | check_sharpd_chunk(router, "expected_chunks2.json") | |
104 | ||
105 | logger.info("Test3 release chunk for locator loc1") | |
106 | router.vtysh_cmd("sharp srv6-manager release-locator-chunk loc1") | |
107 | check_srv6_locator(router, "expected_locators3.json") | |
108 | check_sharpd_chunk(router, "expected_chunks3.json") | |
109 | ||
e08c5663 HS |
110 | logger.info("Test4 additional locator loc3") |
111 | router.vtysh_cmd( | |
112 | """ | |
113 | configure terminal | |
114 | segment-routing | |
115 | srv6 | |
116 | locators | |
117 | locator loc3 | |
118 | prefix 2001:db8:3:3::/64 | |
119 | """ | |
120 | ) | |
96d423f7 HS |
121 | check_srv6_locator(router, "expected_locators4.json") |
122 | check_sharpd_chunk(router, "expected_chunks4.json") | |
123 | ||
e08c5663 | 124 | logger.info("Test5 delete locator and chunk is released automatically") |
96d423f7 HS |
125 | router.vtysh_cmd( |
126 | """ | |
127 | configure terminal | |
128 | segment-routing | |
129 | srv6 | |
130 | locators | |
e08c5663 | 131 | no locator loc1 |
96d423f7 HS |
132 | """ |
133 | ) | |
134 | check_srv6_locator(router, "expected_locators5.json") | |
135 | check_sharpd_chunk(router, "expected_chunks5.json") | |
136 | ||
e08c5663 HS |
137 | logger.info("Test6 delete srv6 all configuration") |
138 | router.vtysh_cmd( | |
139 | """ | |
140 | configure terminal | |
141 | segment-routing | |
142 | no srv6 | |
143 | """ | |
144 | ) | |
145 | check_srv6_locator(router, "expected_locators6.json") | |
146 | check_sharpd_chunk(router, "expected_chunks6.json") | |
147 | ||
96d423f7 | 148 | |
a53c08bc | 149 | if __name__ == "__main__": |
96d423f7 HS |
150 | args = ["-s"] + sys.argv[1:] |
151 | sys.exit(pytest.main(args)) |