5 from framework import VppTestCase, VppTestRunner
7 from vpp_vhost_interface import VppVhostInterface
10 class TesVhostInterface(VppTestCase):
11 """Vhost User Test Case"""
15 super(TesVhostInterface, cls).setUpClass()
18 def tearDownClass(cls):
19 super(TesVhostInterface, cls).tearDownClass()
22 super(TesVhostInterface, self).tearDown()
24 if_dump = self.vapi.sw_interface_vhost_user_dump()
26 self.vapi.delete_vhost_user_if(ifc.sw_if_index)
29 """Vhost User add/delete interface test"""
30 self.logger.info("Vhost User add interfaces")
32 # create interface 1 (VirtualEthernet0/0/0)
33 vhost_if1 = VppVhostInterface(self, sock_filename="/tmp/sock1")
34 vhost_if1.add_vpp_config()
37 # create interface 2 (VirtualEthernet0/0/1)
38 vhost_if2 = VppVhostInterface(self, sock_filename="/tmp/sock2")
39 vhost_if2.add_vpp_config()
42 # verify both interfaces in the show
43 ifs = self.vapi.cli("show interface")
44 self.assertIn("VirtualEthernet0/0/0", ifs)
45 self.assertIn("VirtualEthernet0/0/1", ifs)
47 # verify they are in the dump also
48 if_dump = self.vapi.sw_interface_vhost_user_dump()
49 self.assertTrue(vhost_if1.is_interface_config_in_dump(if_dump))
50 self.assertTrue(vhost_if2.is_interface_config_in_dump(if_dump))
52 # delete VirtualEthernet0/0/1
53 self.logger.info("Deleting VirtualEthernet0/0/1")
54 vhost_if2.remove_vpp_config()
56 self.logger.info("Verifying VirtualEthernet0/0/1 is deleted")
58 ifs = self.vapi.cli("show interface")
59 # verify VirtualEthernet0/0/0 still in the show
60 self.assertIn("VirtualEthernet0/0/0", ifs)
62 # verify VirtualEthernet0/0/1 not in the show
63 self.assertNotIn("VirtualEthernet0/0/1", ifs)
65 # verify VirtualEthernet0/0/1 is not in the dump
66 if_dump = self.vapi.sw_interface_vhost_user_dump()
67 self.assertFalse(vhost_if2.is_interface_config_in_dump(if_dump))
69 # verify VirtualEthernet0/0/0 is still in the dump
70 self.assertTrue(vhost_if1.is_interface_config_in_dump(if_dump))
72 # delete VirtualEthernet0/0/0
73 self.logger.info("Deleting VirtualEthernet0/0/0")
74 vhost_if1.remove_vpp_config()
76 self.logger.info("Verifying VirtualEthernet0/0/0 is deleted")
78 # verify VirtualEthernet0/0/0 not in the show
79 ifs = self.vapi.cli("show interface")
80 self.assertNotIn("VirtualEthernet0/0/0", ifs)
82 # verify VirtualEthernet0/0/0 is not in the dump
83 if_dump = self.vapi.sw_interface_vhost_user_dump()
84 self.assertFalse(vhost_if1.is_interface_config_in_dump(if_dump))
86 def test_vhost_interface_state(self):
87 """Vhost User interface states and events test"""
89 self.vapi.want_interface_events()
91 # clear outstanding events
92 # (like delete interface events from other tests)
93 self.vapi.collect_events()
95 vhost_if = VppVhostInterface(self, sock_filename="/tmp/sock1")
97 # create vhost interface
98 vhost_if.add_vpp_config()
100 events = self.vapi.collect_events()
101 # creating interface does now create events
102 self.assert_equal(len(events), 1, "number of events")
105 vhost_if.assert_interface_state(1, 0, expect_event=True)
107 vhost_if.admin_down()
108 vhost_if.assert_interface_state(0, 0, expect_event=True)
110 # delete vhost interface
111 vhost_if.remove_vpp_config()
112 event = self.vapi.wait_for_event(timeout=1)
113 self.assert_equal(event.sw_if_index, vhost_if.sw_if_index, "sw_if_index")
114 self.assert_equal(event.deleted, 1, "deleted flag")
116 # verify there are no more events
117 events = self.vapi.collect_events()
118 self.assert_equal(len(events), 0, "number of events")
120 def test_vhost_interface_custom_mac_addr(self):
121 """Vhost User interface custom mac address test"""
123 mac_addr = "aa:bb:cc:dd:ee:ff"
124 vhost_if = VppVhostInterface(
125 self, sock_filename="/tmp/sock1", use_custom_mac=1, mac_address=mac_addr
128 # create vhost interface
129 vhost_if.add_vpp_config()
132 # verify mac in the dump
133 if_dump_list = self.vapi.sw_interface_dump(sw_if_index=vhost_if.sw_if_index)
134 self.assert_equal(len(if_dump_list), 1, "if dump length")
136 [if_dump] = if_dump_list
137 self.assert_equal(if_dump.l2_address.mac_string, mac_addr, "MAC Address")
139 # delete VirtualEthernet
140 self.logger.info("Deleting VirtualEthernet")
141 vhost_if.remove_vpp_config()
144 if __name__ == "__main__":
145 unittest.main(testRunner=VppTestRunner)