make test: fix missing log/packet messages
[vpp.git] / test / test_vxlan.py
1 #!/usr/bin/env python
2
3 import unittest
4 from framework import VppTestCase, VppTestRunner
5 from template_bd import BridgeDomain
6
7 from scapy.layers.l2 import Ether
8 from scapy.layers.inet import IP, UDP
9 from scapy_handlers.vxlan import VXLAN
10
11
12 class TestVxlan(BridgeDomain, VppTestCase):
13     """ VXLAN Test Case """
14
15     def __init__(self, *args):
16         BridgeDomain.__init__(self)
17         VppTestCase.__init__(self, *args)
18
19     def encapsulate(self, pkt):
20         """
21         Encapsulate the original payload frame by adding VXLAN header with its
22         UDP, IP and Ethernet fields
23         """
24         return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
25                 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
26                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
27                 VXLAN(vni=self.vni) /
28                 pkt)
29
30     def decapsulate(self, pkt):
31         """
32         Decapsulate the original payload frame by removing VXLAN header
33         """
34         return pkt[VXLAN].payload
35
36     # Method for checking VXLAN encapsulation.
37     #
38     def check_encapsulation(self, pkt):
39         # TODO: add error messages
40         # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
41         #  by VPP using ARP.
42         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
43         self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
44         # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
45         self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
46         self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
47         # Verify UDP destination port is VXLAN 4789, source UDP port could be
48         #  arbitrary.
49         self.assertEqual(pkt[UDP].dport, type(self).dport)
50         # TODO: checksum check
51         # Verify VNI, based on configuration it must be 1.
52         self.assertEqual(pkt[VXLAN].vni, type(self).vni)
53
54     # Class method to start the VXLAN test case.
55     #  Overrides setUpClass method in VppTestCase class.
56     #  Python try..except statement is used to ensure that the tear down of
57     #  the class will be executed even if exception is raised.
58     #  @param cls The class pointer.
59     @classmethod
60     def setUpClass(cls):
61         super(TestVxlan, cls).setUpClass()
62
63         try:
64             cls.dport = 4789
65             cls.vni = 1
66
67             # Create 2 pg interfaces.
68             cls.create_pg_interfaces(range(2))
69             cls.pg0.admin_up()
70             cls.pg1.admin_up()
71
72             # Configure IPv4 addresses on VPP pg0.
73             cls.pg0.config_ip4()
74
75             # Resolve MAC address for VPP's IP address on pg0.
76             cls.pg0.resolve_arp()
77
78             # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
79             #  into BD.
80             r = cls.vapi.vxlan_add_del_tunnel(
81                 src_addr=cls.pg0.local_ip4n,
82                 dst_addr=cls.pg0.remote_ip4n,
83                 vni=cls.vni)
84             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=1)
85             cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index, bd_id=1)
86         except Exception:
87             super(TestVxlan, cls).tearDownClass()
88             raise
89
90     # Method to define VPP actions before tear down of the test case.
91     #  Overrides tearDown method in VppTestCase class.
92     #  @param self The object pointer.
93     def tearDown(self):
94         super(TestVxlan, self).tearDown()
95         if not self.vpp_dead:
96             self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
97
98 if __name__ == '__main__':
99     unittest.main(testRunner=VppTestRunner)