forked from sonic-net/sonic-mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ip_bgp.py
More file actions
220 lines (178 loc) · 8.17 KB
/
Copy pathtest_ip_bgp.py
File metadata and controls
220 lines (178 loc) · 8.17 KB
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
import logging
import pytest
import ipaddress
import re
from tests.common.helpers.assertions import pytest_assert
from tests.common.gu_utils import apply_patch, expect_op_success, expect_op_failure
from tests.common.gu_utils import generate_tmpfile, delete_tmpfile
from tests.common.gu_utils import format_json_patch_for_multiasic
from tests.common.gu_utils import create_checkpoint, delete_checkpoint, rollback_or_reload
logger = logging.getLogger(__name__)
pytestmark = [
pytest.mark.topology('t0', 't1', 't2', 'm0', 'mx'),
]
@pytest.fixture(autouse=True)
def ensure_dut_readiness(duthost):
"""
Setup/teardown fixture for each ipv6 test
rollback to check if it goes back to starting config
Args:
duthost: DUT host object under test
"""
create_checkpoint(duthost)
yield
try:
logger.info("Rolled back to original checkpoint")
rollback_or_reload(duthost)
finally:
delete_checkpoint(duthost)
def get_ip_neighbor(duthost, namespace=None, ip_version=6):
"""
Returns ip BGP neighbor address, properties of BGP neighbor
Args:
duthost: DUT host object
namespace: DUT asic namespace. asic0, asic1, None
ip_version: IP version. 4, 6
"""
config_facts = duthost.config_facts(host=duthost.hostname, source="running",
verbose=False, namespace=namespace)['ansible_facts']
bgp_neighbors_data = config_facts['BGP_NEIGHBOR']
for neighbor_address in list(bgp_neighbors_data.keys()):
if ipaddress.ip_address((neighbor_address.encode().decode())).version == ip_version:
return neighbor_address, bgp_neighbors_data[neighbor_address]
pytest_assert(True, "No existing ipv{} neighbor".format(ip_version))
def check_neighbor_existence(duthost, neighbor_address, ip_version=6):
cmd = 'show ip bgp su' if ip_version == 4 else 'show ipv6 bgp su'
ip_bgp_su = duthost.shell(cmd)['stdout']
return re.search(r'\b{}\b'.format(neighbor_address), ip_bgp_su)
def add_deleted_ip_neighbor(duthost, namespace=None, ip_version=6):
ip_neighbor_address, ip_neighbor_config = get_ip_neighbor(duthost, namespace, ip_version)
neighbor_exists = check_neighbor_existence(duthost, ip_neighbor_address, ip_version)
pytest_assert(neighbor_exists, "Nonexistent ipv{} BGP neighbor".format(ip_version))
duthost.shell('config bgp remove neighbor {}'.format(ip_neighbor_address))
neighbor_exists = check_neighbor_existence(duthost, ip_neighbor_address, ip_version)
pytest_assert(not neighbor_exists,
"Failed to remove ipv{} BGP neighbor under test".format(ip_version))
json_namespace = '' if namespace is None else '/' + namespace
json_patch = [
{
"op": "add",
"path": "{}/BGP_NEIGHBOR/{}".format(json_namespace, ip_neighbor_address),
"value": ip_neighbor_config
}
]
json_patch = format_json_patch_for_multiasic(duthost=duthost, json_data=json_patch)
tmpfile = generate_tmpfile(duthost)
logger.info("tmpfile {}".format(tmpfile))
try:
output = apply_patch(duthost, json_data=json_patch, dest_file=tmpfile)
expect_op_success(duthost, output)
neighbor_exists = check_neighbor_existence(duthost, ip_neighbor_address, ip_version)
pytest_assert(neighbor_exists,
"GCU failed to add back deleted ipv{} BGP neighbor".format(ip_version))
finally:
delete_tmpfile(duthost, tmpfile)
def add_duplicate_ip_neighbor(duthost, namespace=None, ip_version=6):
ip_neighbor_address, ip_neighbor_config = get_ip_neighbor(duthost, namespace, ip_version)
json_namespace = '' if namespace is None else '/' + namespace
json_patch = [
{
"op": "add",
"path": "{}/BGP_NEIGHBOR/{}".format(json_namespace, ip_neighbor_address),
"value": ip_neighbor_config
}
]
json_patch = format_json_patch_for_multiasic(duthost=duthost, json_data=json_patch)
tmpfile = generate_tmpfile(duthost)
logger.info("tmpfile {}".format(tmpfile))
try:
output = apply_patch(duthost, json_data=json_patch, dest_file=tmpfile)
expect_op_success(duthost, output)
neighbor_exists = check_neighbor_existence(duthost, ip_neighbor_address, ip_version)
pytest_assert(neighbor_exists,
"Expected ipv{} BGP neighbor does not exist".format(ip_version))
finally:
delete_tmpfile(duthost, tmpfile)
def invalid_ip_neighbor(duthost, namespace=None, ip_version=6):
xfailv6_input = [
("add", "FC00::xyz/126"),
("remove", "FC00::01/126")
]
xfailv4_input = [
("add", "10.0.0.256/31"),
("remove", "10.0.0.0/31")
]
xfail_input = xfailv4_input if ip_version == 4 else xfailv6_input
json_namespace = '' if namespace is None else '/' + namespace
for op, dummy_neighbor_ip_address in xfail_input:
json_patch = [
{
"op": "{}".format(op),
"path": "{}/BGP_NEIGHBOR/{}".format(json_namespace, dummy_neighbor_ip_address),
"value": {}
}
]
json_patch = format_json_patch_for_multiasic(duthost=duthost, json_data=json_patch)
tmpfile = generate_tmpfile(duthost)
logger.info("tmpfile {}".format(tmpfile))
try:
output = apply_patch(duthost, json_data=json_patch, dest_file=tmpfile)
expect_op_failure(output)
finally:
delete_tmpfile(duthost, tmpfile)
def ip_neighbor_admin_change(duthost, namespace=None, ip_version=6):
ip_neighbor_address, ip_neighbor_config = get_ip_neighbor(duthost, namespace, ip_version)
json_namespace = '' if namespace is None else '/' + namespace
json_patch = [
{
"op": "add",
"path": "{}/BGP_NEIGHBOR/{}/admin_status".format(json_namespace, ip_neighbor_address),
"value": "up"
},
{
"op": "replace",
"path": "{}/BGP_NEIGHBOR/{}/admin_status".format(json_namespace, ip_neighbor_address),
"value": "down"
}
]
json_patch = format_json_patch_for_multiasic(duthost=duthost, json_data=json_patch)
tmpfile = generate_tmpfile(duthost)
logger.info("tmpfile {}".format(tmpfile))
try:
output = apply_patch(duthost, json_data=json_patch, dest_file=tmpfile)
expect_op_success(duthost, output)
ip_type = 'ip' if ip_version == 4 else 'ipv6'
cmds = "show {} bgp su | grep -w {}".format(ip_type, ip_neighbor_address)
output = duthost.shell(cmds)
pytest_assert(not output['rc'] and "Idle (Admin)" in output['stdout'],
"BGP Neighbor with addr {} failed to admin down.".format(ip_neighbor_address))
finally:
delete_tmpfile(duthost, tmpfile)
def delete_ip_neighbor(duthost, namespace=None, ip_version=6):
ip_neighbor_address, ip_neighbor_config = get_ip_neighbor(duthost, ip_version)
json_namespace = '' if namespace is None else '/' + namespace
json_patch = [
{
"op": "remove",
"path": "{}/BGP_NEIGHBOR/{}".format(json_namespace, ip_neighbor_address)
}
]
json_patch = format_json_patch_for_multiasic(duthost=duthost, json_data=json_patch)
tmpfile = generate_tmpfile(duthost)
logger.info("tmpfile {}".format(tmpfile))
try:
output = apply_patch(duthost, json_data=json_patch, dest_file=tmpfile)
expect_op_success(duthost, output)
neighbor_exists = check_neighbor_existence(duthost, ip_neighbor_address, ip_version)
pytest_assert(not neighbor_exists,
"Failed to remove ipv{} BGP neighbor under test".format(ip_version))
finally:
delete_tmpfile(duthost, tmpfile)
@pytest.mark.parametrize("ip_version", [6, 4])
def test_ip_suite(duthost, ensure_dut_readiness, rand_asic_namespace, ip_version):
asic_namespace, _asic_id = rand_asic_namespace
add_deleted_ip_neighbor(duthost, asic_namespace, ip_version)
add_duplicate_ip_neighbor(duthost, asic_namespace, ip_version)
invalid_ip_neighbor(duthost, asic_namespace, ip_version)
ip_neighbor_admin_change(duthost, asic_namespace, ip_version)
delete_ip_neighbor(duthost, asic_namespace, ip_version)