tests: replace pycodestyle with black
[vpp.git] / test / test_vhost.py
1 #!/usr/bin/env python3
2
3 import unittest
4
5 from framework import VppTestCase, VppTestRunner
6
7 from vpp_vhost_interface import VppVhostInterface
8
9
10 class TesVhostInterface(VppTestCase):
11     """Vhost User Test Case"""
12
13     @classmethod
14     def setUpClass(cls):
15         super(TesVhostInterface, cls).setUpClass()
16
17     @classmethod
18     def tearDownClass(cls):
19         super(TesVhostInterface, cls).tearDownClass()
20
21     def tearDown(self):
22         super(TesVhostInterface, self).tearDown()
23         if not self.vpp_dead:
24             if_dump = self.vapi.sw_interface_vhost_user_dump()
25             for ifc in if_dump:
26                 self.vapi.delete_vhost_user_if(ifc.sw_if_index)
27
28     def test_vhost(self):
29         """Vhost User add/delete interface test"""
30         self.logger.info("Vhost User add interfaces")
31
32         # create interface 1 (VirtualEthernet0/0/0)
33         vhost_if1 = VppVhostInterface(self, sock_filename="/tmp/sock1")
34         vhost_if1.add_vpp_config()
35         vhost_if1.admin_up()
36
37         # create interface 2 (VirtualEthernet0/0/1)
38         vhost_if2 = VppVhostInterface(self, sock_filename="/tmp/sock2")
39         vhost_if2.add_vpp_config()
40         vhost_if2.admin_up()
41
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)
46
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))
51
52         # delete VirtualEthernet0/0/1
53         self.logger.info("Deleting VirtualEthernet0/0/1")
54         vhost_if2.remove_vpp_config()
55
56         self.logger.info("Verifying VirtualEthernet0/0/1 is deleted")
57
58         ifs = self.vapi.cli("show interface")
59         # verify VirtualEthernet0/0/0 still in the show
60         self.assertIn("VirtualEthernet0/0/0", ifs)
61
62         # verify VirtualEthernet0/0/1 not in the show
63         self.assertNotIn("VirtualEthernet0/0/1", ifs)
64
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))
68
69         # verify VirtualEthernet0/0/0 is still in the dump
70         self.assertTrue(vhost_if1.is_interface_config_in_dump(if_dump))
71
72         # delete VirtualEthernet0/0/0
73         self.logger.info("Deleting VirtualEthernet0/0/0")
74         vhost_if1.remove_vpp_config()
75
76         self.logger.info("Verifying VirtualEthernet0/0/0 is deleted")
77
78         # verify VirtualEthernet0/0/0 not in the show
79         ifs = self.vapi.cli("show interface")
80         self.assertNotIn("VirtualEthernet0/0/0", ifs)
81
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))
85
86     def test_vhost_interface_state(self):
87         """Vhost User interface states and events test"""
88
89         self.vapi.want_interface_events()
90
91         # clear outstanding events
92         # (like delete interface events from other tests)
93         self.vapi.collect_events()
94
95         vhost_if = VppVhostInterface(self, sock_filename="/tmp/sock1")
96
97         # create vhost interface
98         vhost_if.add_vpp_config()
99         self.sleep(0.1)
100         events = self.vapi.collect_events()
101         # creating interface does now create events
102         self.assert_equal(len(events), 1, "number of events")
103
104         vhost_if.admin_up()
105         vhost_if.assert_interface_state(1, 0, expect_event=True)
106
107         vhost_if.admin_down()
108         vhost_if.assert_interface_state(0, 0, expect_event=True)
109
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")
115
116         # verify there are no more events
117         events = self.vapi.collect_events()
118         self.assert_equal(len(events), 0, "number of events")
119
120     def test_vhost_interface_custom_mac_addr(self):
121         """Vhost User interface custom mac address test"""
122
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
126         )
127
128         # create vhost interface
129         vhost_if.add_vpp_config()
130         self.sleep(0.1)
131
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")
135
136         [if_dump] = if_dump_list
137         self.assert_equal(if_dump.l2_address.mac_string, mac_addr, "MAC Address")
138
139         # delete VirtualEthernet
140         self.logger.info("Deleting VirtualEthernet")
141         vhost_if.remove_vpp_config()
142
143
144 if __name__ == "__main__":
145     unittest.main(testRunner=VppTestRunner)