2 """CRUD tests of APIs (Create, Read, Update, Delete) HLD:
4 - interface up/down/add/delete - interface type:
10 - tuntap (root privileges needed)
16 from scapy.layers.inet import IP, ICMP
17 from scapy.layers.l2 import Ether
19 from framework import VppTestCase, VppTestRunner
22 class TestLoopbackInterfaceCRUD(VppTestCase):
29 super(TestLoopbackInterfaceCRUD, cls).setUpClass()
31 cls.create_pg_interfaces(range(1))
32 for i in cls.pg_interfaces:
40 def tearDownClass(cls):
41 super(TestLoopbackInterfaceCRUD, cls).tearDownClass()
44 def create_icmp_stream(src_if, dst_ifs):
47 :param VppInterface src_if: Packets are send to this interface,
48 using this interfaces remote host.
49 :param list dst_ifs: IPv4 ICMP requests are send to interfaces
51 :return: List of generated packets.
55 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
56 IP(src=src_if.remote_ip4, dst=i.local_ip4) /
57 ICMP(id=i.sw_if_index, type='echo-request'))
61 def verify_icmp(self, capture, request_src_if, dst_ifs):
64 :param capture: Capture to verify.
65 :param VppInterface request_src_if: Interface where was send packets.
66 :param list dst_ifs: Interfaces where was generated IPv4 ICMP requests.
76 info = (ip.src, ip.dst, icmp.type, icmp.id)
77 rcvd_icmp_pkts.append(info)
80 # 0 - icmp echo response
81 info = (i.local_ip4, request_src_if.remote_ip4, 0, i.sw_if_index)
82 self.assertIn(info, rcvd_icmp_pkts)
86 loopbacks = self.create_loopback_interfaces(20)
88 i.local_ip4_prefix.len = 32
92 # read (check sw if dump, ip4 fib, ip6 fib)
93 if_dump = self.vapi.sw_interface_dump(name_filter_valid=True,
95 fib4_dump = self.vapi.ip_route_dump(0)
97 self.assertTrue(i.is_interface_config_in_dump(if_dump))
98 self.assertTrue(i.is_ip4_entry_in_fib_dump(fib4_dump))
100 if_dump = self.vapi.sw_interface_dump(name_filter_valid=True,
101 name_filter='loopXYZ')
102 self.assertEqual(len(if_dump), 0)
105 stream = self.create_icmp_stream(self.pg0, loopbacks)
106 self.pg0.add_stream(stream)
107 self.pg_enable_capture(self.pg_interfaces)
109 capture = self.pg0.get_capture(expected_count=len(stream))
111 self.verify_icmp(capture, self.pg0, loopbacks)
115 i.remove_vpp_config()
117 # read (check not in sw if dump, ip4 fib, ip6 fib)
118 if_dump = self.vapi.sw_interface_dump()
119 fib4_dump = self.vapi.ip_route_dump(0)
121 self.assertFalse(i.is_interface_config_in_dump(if_dump))
122 self.assertFalse(i.is_ip4_entry_in_fib_dump(fib4_dump))
125 stream = self.create_icmp_stream(self.pg0, loopbacks)
126 self.pg0.add_stream(stream)
127 self.pg_enable_capture(self.pg_interfaces)
129 self.pg0.assert_nothing_captured()
133 loopbacks = self.create_loopback_interfaces(20)
135 i.local_ip4_prefix.len = 32
144 # read (check not in sw if dump, ip4 fib, ip6 fib)
145 if_dump = self.vapi.sw_interface_dump()
146 fib4_dump = self.vapi.ip_route_dump(0)
148 self.assertTrue(i.is_interface_config_in_dump(if_dump))
149 self.assertFalse(i.is_ip4_entry_in_fib_dump(fib4_dump))
152 stream = self.create_icmp_stream(self.pg0, loopbacks)
153 self.pg0.add_stream(stream)
154 self.pg_enable_capture(self.pg_interfaces)
156 self.pg0.assert_nothing_captured()
159 class TestInterfaceDumpApiLocalOnly(VppTestCase):
160 """test_interface_crud.TestInterfaceDumpApiLocalOnly"""
162 def test_sw_if_index_0(self):
163 rv = self.vapi.sw_interface_dump(sw_if_index=0)
164 self.assertEqual(rv[0].sw_if_index, 0)
166 def test_sw_if_index_twiddle0(self):
167 rv = self.vapi.sw_interface_dump(sw_if_index=0xffffffff)
168 self.assertEqual(rv[0].sw_if_index, 0)
170 def test_sw_if_index_1_not_existing(self):
171 rv = self.vapi.sw_interface_dump(sw_if_index=1)
172 self.assertEqual(len(rv), 0, 'expected no records.')
175 class TestInterfaceDumpApi(VppTestCase):
176 """test_interface_crud.TestInterfaceDumpApi"""
178 def test_sw_if_index_1(self):
179 self.vapi.create_loopback_instance(is_specified=1,
181 self.vapi.create_loopback_instance(is_specified=1,
184 # Can I get back the specified record?
185 rv = self.vapi.sw_interface_dump(sw_if_index=1)
186 self.assertEqual(rv[0].sw_if_index, 1, rv)
188 # verify 3 interfaces
189 rv = self.vapi.sw_interface_dump(sw_if_index=0xffffffff)
190 self.assertEqual(len(rv), 3, 'Expected 3 interfaces.')
192 if __name__ == '__main__':
193 unittest.main(testRunner=VppTestRunner)