devices: vhost API cleanup
[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     """
14     @classmethod
15     def setUpClass(cls):
16         super(TesVhostInterface, cls).setUpClass()
17
18     @classmethod
19     def tearDownClass(cls):
20         super(TesVhostInterface, cls).tearDownClass()
21
22     def tearDown(self):
23         super(TesVhostInterface, self).tearDown()
24         if not self.vpp_dead:
25             if_dump = self.vapi.sw_interface_vhost_user_dump()
26             for ifc in if_dump:
27                 self.vapi.delete_vhost_user_if(ifc.sw_if_index)
28
29     def test_vhost(self):
30         """ Vhost User add/delete interface test """
31         self.logger.info("Vhost User add interfaces")
32
33         # create interface 1 (VirtualEthernet0/0/0)
34         vhost_if1 = VppVhostInterface(self, sock_filename='/tmp/sock1')
35         vhost_if1.add_vpp_config()
36         vhost_if1.admin_up()
37
38         # create interface 2 (VirtualEthernet0/0/1)
39         vhost_if2 = VppVhostInterface(self, sock_filename='/tmp/sock2')
40         vhost_if2.add_vpp_config()
41         vhost_if2.admin_up()
42
43         # verify both interfaces in the show
44         ifs = self.vapi.cli("show interface")
45         self.assertIn('VirtualEthernet0/0/0', ifs)
46         self.assertIn('VirtualEthernet0/0/1', ifs)
47
48         # verify they are in the dump also
49         if_dump = self.vapi.sw_interface_vhost_user_dump()
50         self.assertTrue(vhost_if1.is_interface_config_in_dump(if_dump))
51         self.assertTrue(vhost_if2.is_interface_config_in_dump(if_dump))
52
53         # delete VirtualEthernet0/0/1
54         self.logger.info("Deleting VirtualEthernet0/0/1")
55         vhost_if2.remove_vpp_config()
56
57         self.logger.info("Verifying VirtualEthernet0/0/1 is deleted")
58
59         ifs = self.vapi.cli("show interface")
60         # verify VirtualEthernet0/0/0 still in the show
61         self.assertIn('VirtualEthernet0/0/0', ifs)
62
63         # verify VirtualEthernet0/0/1 not in the show
64         self.assertNotIn('VirtualEthernet0/0/1', ifs)
65
66         # verify VirtualEthernet0/0/1 is not in the dump
67         if_dump = self.vapi.sw_interface_vhost_user_dump()
68         self.assertFalse(vhost_if2.is_interface_config_in_dump(if_dump))
69
70         # verify VirtualEthernet0/0/0 is still in the dump
71         self.assertTrue(vhost_if1.is_interface_config_in_dump(if_dump))
72
73         # delete VirtualEthernet0/0/0
74         self.logger.info("Deleting VirtualEthernet0/0/0")
75         vhost_if1.remove_vpp_config()
76
77         self.logger.info("Verifying VirtualEthernet0/0/0 is deleted")
78
79         # verify VirtualEthernet0/0/0 not in the show
80         ifs = self.vapi.cli("show interface")
81         self.assertNotIn('VirtualEthernet0/0/0', ifs)
82
83         # verify VirtualEthernet0/0/0 is not in the dump
84         if_dump = self.vapi.sw_interface_vhost_user_dump()
85         self.assertFalse(vhost_if1.is_interface_config_in_dump(if_dump))
86
87     def test_vhost_interface_state(self):
88         """ Vhost User interface states and events test """
89
90         self.vapi.want_interface_events()
91
92         # clear outstanding events
93         # (like delete interface events from other tests)
94         self.vapi.collect_events()
95
96         vhost_if = VppVhostInterface(self, sock_filename='/tmp/sock1')
97
98         # create vhost interface
99         vhost_if.add_vpp_config()
100         self.sleep(0.1)
101         events = self.vapi.collect_events()
102         # creating interface does now create events
103         self.assert_equal(len(events), 1, "number of events")
104
105         vhost_if.admin_up()
106         vhost_if.assert_interface_state(1, 0, expect_event=True)
107
108         vhost_if.admin_down()
109         vhost_if.assert_interface_state(0, 0, expect_event=True)
110
111         # delete vhost interface
112         vhost_if.remove_vpp_config()
113         event = self.vapi.wait_for_event(timeout=1)
114         self.assert_equal(event.sw_if_index, vhost_if.sw_if_index,
115                           "sw_if_index")
116         self.assert_equal(event.deleted, 1, "deleted flag")
117
118         # verify there are no more events
119         events = self.vapi.collect_events()
120         self.assert_equal(len(events), 0, "number of events")
121
122 if __name__ == '__main__':
123     unittest.main(testRunner=VppTestRunner)