Skip to content

Commit

Permalink
add more unittest for evpn
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Xiao <xiaoquwl@gmail.com>
  • Loading branch information
xiaopeng163 committed Jan 26, 2016
1 parent 203fe4f commit da22663
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
19 changes: 19 additions & 0 deletions yabgp/message/attribute/mpunreachnlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from yabgp.message.attribute.nlri.ipv4_mpls_vpn import IPv4MPLSVPN
from yabgp.message.attribute.nlri.ipv4_flowspec import IPv4FlowSpec
from yabgp.message.attribute.nlri.ipv6_unicast import IPv6Unicast
from yabgp.message.attribute.nlri.evpn import EVPN
from yabgp.common import afn
from yabgp.common import safn
from yabgp.common import exception as excep
Expand Down Expand Up @@ -85,6 +86,13 @@ def parse(cls, value):
return dict(afi_safi=(afi, safi), withdraw=IPv6Unicast.parse(nlri_data=nlri_bin))
else:
return dict(afi_safi=(afi, safi), withdraw=repr(nlri_bin))
# for l2vpn
elif afi == afn.AFNUM_L2VPN:
# for evpn
if safi == safn.SAFNUM_EVPN:
return dict(afi_safi=(afi, safi), withdraw=EVPN.parse(nlri_data=nlri_bin))
else:
return dict(afi_safi=(afi, safi), withdraw=repr(nlri_bin))

else:
return dict(afi_safi=(afi, safi), withdraw=repr(nlri_bin))
Expand Down Expand Up @@ -137,6 +145,17 @@ def construct(cls, value):
+ struct.pack('!B', len(attr_value)) + attr_value
else:
return None
# for l2vpn
elif afi == afn.AFNUM_L2VPN:
# for evpn
if safi == safn.SAFNUM_EVPN:
nlri = EVPN.construct(nlri_list=value['withdraw'])
if nlri:
attr_value = struct.pack('!H', afi) + struct.pack('!B', safi) + nlri
return struct.pack('!B', cls.FLAG) + struct.pack('!B', cls.ID) \
+ struct.pack('!B', len(attr_value)) + attr_value
else:
return None
else:
raise excep.ConstructAttributeFailed(
reason='unsupport this sub address family',
Expand Down
4 changes: 2 additions & 2 deletions yabgp/message/attribute/nlri/evpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def construct(cls, data):
data_hex += b'\x00\x00' + struct.pack('!d', data['esi'])
# ethernet tag
data_hex += struct.pack('!I', data['eth_tag_id'])
data_hex += MPLSVPN.construct_mpls_label_stack(data['label'], bos=False)
data_hex += MPLSVPN.construct_mpls_label_stack(data['label'])
return data_hex


Expand Down Expand Up @@ -183,7 +183,7 @@ def construct(cls, data):
else:
data_hex += b'\x00'
if data.get('label'):
data_hex += MPLSVPN.construct_mpls_label_stack(data['label'], bos=False)
data_hex += MPLSVPN.construct_mpls_label_stack(data['label'])
return data_hex


Expand Down
12 changes: 7 additions & 5 deletions yabgp/message/attribute/nlri/mpls_vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ def parse_mpls_label_stack(cls, data):
return labels

@classmethod
def construct_mpls_label_stack(cls, labels, bos=True):
def construct_mpls_label_stack(cls, labels):
data = b''
for label in labels:
if bos:
data += struct.pack('!L', (label << 4 | 1))[1:]
continue
last_label = labels[-1]
for label in labels[:-1]:
data += struct.pack('!L', label << 4)[1:]
if last_label != 0:
data += struct.pack('!L', (last_label << 4 | 1))[1:]
else:
data += b'\x00\x00\x00'
return data

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions yabgp/tests/unit/message/attribute/nlri/test_evpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_construct_mac_ip_adv(self):

def test_parse_eth_auto_dis(self):
data_hex = b'\x01\x19\x00\x01\x01\x01\x01\x01\x80\x63\x00\x00\x00' \
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x64\x00\x00\xa0'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x64\x00\x00\xa1'
data_list = [{
'type': bgp_cons.BGPNLRI_EVPN_ETHERNET_AUTO_DISCOVERY,
'value': {
Expand All @@ -66,7 +66,7 @@ def test_parse_eth_auto_dis(self):

def test_construct_eth_auto_dis(self):
data_hex = b'\x01\x19\x00\x01\x01\x01\x01\x01\x80\x63\x00\x00\x00' \
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x64\x00\x00\xa0'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x64\x00\x00\xa1'
data_list = [{
'type': bgp_cons.BGPNLRI_EVPN_ETHERNET_AUTO_DISCOVERY,
'value': {
Expand Down
2 changes: 1 addition & 1 deletion yabgp/tests/unit/message/attribute/test_mpreachnlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_ipv4_flowspec_construct(self):
def test_l2vpn_evpn_parse_route_type2(self):
data_bin = b'\x80\x0e\x30\x00\x19\x46\x04\xac\x11\x00\x03\x00\x02\x25\x00\x01\xac\x11' \
b'\x00\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x6c' \
b'\x30\x00\x11\x22\x33\x44\x55\x20\x0b\x0b\x0b\x01\x00\x00\x00'
b'\x30\x00\x11\x22\x33\x44\x55\x20\x0b\x0b\x0b\x01\x00\x00\x01'
data_dict = {
'afi_safi': (25, 70),
'nexthop': '172.17.0.3',
Expand Down
16 changes: 16 additions & 0 deletions yabgp/tests/unit/message/attribute/test_mpunreachnlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ def test_ipv4_flowspec_construct(self):
nlri_dict = {'afi_safi': (1, 133), 'withdraw': [{1: '192.85.2.0/24'}, {2: '192.85.1.0/24'}]}
self.assertEqual(data_bin, MpUnReachNLRI.construct(nlri_dict)[3:])

def test_l2vpn_evpn_route_type_2_parse_construct(self):
data_dict = {
'afi_safi': (25, 70),
'withdraw': [
{
'type': 2,
'value': {
'eth_tag_id': 108,
'ip': '11.11.11.1',
'label': [0],
'rd': '172.17.0.3:2',
'mac': '00-11-22-33-44-55',
'esi': 0}}]
}
self.assertEqual(data_dict, MpUnReachNLRI.parse(MpUnReachNLRI.construct(data_dict)[3:]))


if __name__ == '__main__':
unittest.main()

0 comments on commit da22663

Please sign in to comment.