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:
33 i.config_ip4().resolve_arp()
39 def tearDownClass(cls):
40 super(TestLoopbackInterfaceCRUD, cls).tearDownClass()
43 def create_icmp_stream(src_if, dst_ifs):
46 :param VppInterface src_if: Packets are send to this interface,
47 using this interfaces remote host.
48 :param list dst_ifs: IPv4 ICMP requests are send to interfaces
50 :return: List of generated packets.
54 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
55 IP(src=src_if.remote_ip4, dst=i.local_ip4) /
56 ICMP(id=i.sw_if_index, type='echo-request'))
60 def verify_icmp(self, capture, request_src_if, dst_ifs):
63 :param capture: Capture to verify.
64 :param VppInterface request_src_if: Interface where was send packets.
65 :param list dst_ifs: Interfaces where was generated IPv4 ICMP requests.
75 info = (ip.src, ip.dst, icmp.type, icmp.id)
76 rcvd_icmp_pkts.append(info)
79 # 0 - icmp echo response
80 info = (i.local_ip4, request_src_if.remote_ip4, 0, i.sw_if_index)
81 self.assertIn(info, rcvd_icmp_pkts)
85 loopbacks = self.create_loopback_interfaces(20)
87 i.local_ip4_prefix_len = 32
88 i.config_ip4().admin_up()
90 # read (check sw if dump, ip4 fib, ip6 fib)
91 if_dump = self.vapi.sw_interface_dump(name_filter_valid=True,
93 fib4_dump = self.vapi.ip_route_dump(0)
95 self.assertTrue(i.is_interface_config_in_dump(if_dump))
96 self.assertTrue(i.is_ip4_entry_in_fib_dump(fib4_dump))
98 if_dump = self.vapi.sw_interface_dump(name_filter_valid=True,
99 name_filter='loopXYZ')
100 self.assertEqual(len(if_dump), 0)
103 stream = self.create_icmp_stream(self.pg0, loopbacks)
104 self.pg0.add_stream(stream)
105 self.pg_enable_capture(self.pg_interfaces)
107 capture = self.pg0.get_capture(expected_count=len(stream))
109 self.verify_icmp(capture, self.pg0, loopbacks)
113 i.remove_vpp_config()
115 # read (check not in sw if dump, ip4 fib, ip6 fib)
116 if_dump = self.vapi.sw_interface_dump()
117 fib4_dump = self.vapi.ip_route_dump(0)
119 self.assertFalse(i.is_interface_config_in_dump(if_dump))
120 self.assertFalse(i.is_ip4_entry_in_fib_dump(fib4_dump))
123 stream = self.create_icmp_stream(self.pg0, loopbacks)
124 self.pg0.add_stream(stream)
125 self.pg_enable_capture(self.pg_interfaces)
127 self.pg0.assert_nothing_captured()
131 loopbacks = self.create_loopback_interfaces(20)
133 i.local_ip4_prefix_len = 32
134 i.config_ip4().admin_up()
138 i.admin_down().unconfig_ip4()
140 # read (check not in sw if dump, ip4 fib, ip6 fib)
141 if_dump = self.vapi.sw_interface_dump()
142 fib4_dump = self.vapi.ip_route_dump(0)
144 self.assertTrue(i.is_interface_config_in_dump(if_dump))
145 self.assertFalse(i.is_ip4_entry_in_fib_dump(fib4_dump))
148 stream = self.create_icmp_stream(self.pg0, loopbacks)
149 self.pg0.add_stream(stream)
150 self.pg_enable_capture(self.pg_interfaces)
152 self.pg0.assert_nothing_captured()
155 class TestInterfaceDumpApiLocalOnly(VppTestCase):
156 """test_interface_crud.TestInterfaceDumpApiLocalOnly"""
158 def test_sw_if_index_0(self):
159 rv = self.vapi.sw_interface_dump(sw_if_index=0)
160 self.assertEqual(rv[0].sw_if_index, 0)
162 def test_sw_if_index_twiddle0(self):
163 rv = self.vapi.sw_interface_dump(sw_if_index=0xffffffff)
164 self.assertEqual(rv[0].sw_if_index, 0)
166 def test_sw_if_index_1_not_existing(self):
167 rv = self.vapi.sw_interface_dump(sw_if_index=1)
168 self.assertEqual(len(rv), 0, 'expected no records.')
171 class TestInterfaceDumpApi(VppTestCase):
172 """test_interface_crud.TestInterfaceDumpApi"""
174 def test_sw_if_index_1(self):
175 self.vapi.create_loopback_instance(is_specified=1,
177 self.vapi.create_loopback_instance(is_specified=1,
180 # Can I get back the specified record?
181 rv = self.vapi.sw_interface_dump(sw_if_index=1)
182 self.assertEqual(rv[0].sw_if_index, 1, rv)
184 # verify 3 interfaces
185 rv = self.vapi.sw_interface_dump(sw_if_index=0xffffffff)
186 self.assertEqual(len(rv), 3, 'Expected 3 interfaces.')
189 if __name__ == '__main__':
190 unittest.main(testRunner=VppTestRunner)