tests: refactor asf framework code
[vpp.git] / test / asf / test_cli.py
1 #!/usr/bin/env python3
2 """CLI functional tests"""
3
4 import unittest
5
6 from vpp_papi import VPPIOError
7
8 from asfframework import VppAsfTestCase, VppTestRunner
9
10
11 class TestCLI(VppAsfTestCase):
12     """CLI Test Case"""
13
14     maxDiff = None
15
16     @classmethod
17     def setUpClass(cls):
18         # using the framework default
19         cls.vapi_response_timeout = 5
20         super(TestCLI, cls).setUpClass()
21
22     @classmethod
23     def tearDownClass(cls):
24         super(TestCLI, cls).tearDownClass()
25
26     def setUp(self):
27         super(TestCLI, self).setUp()
28
29     def tearDown(self):
30         super(TestCLI, self).tearDown()
31
32     def test_cli_retval(self):
33         """CLI inband retval"""
34         rv = self.vapi.papi.cli_inband(cmd="this command does not exist")
35         self.assertNotEqual(rv.retval, 0)
36
37         rv = self.vapi.papi.cli_inband(cmd="show version")
38         self.assertEqual(rv.retval, 0)
39
40     def test_long_cli_delay(self):
41         """Test that VppApiClient raises VppIOError if timeout."""  # noqa
42         with self.assertRaises(VPPIOError) as ctx:
43             rv = self.vapi.papi.cli_inband(cmd="wait 10")
44
45     def test_long_cli_delay_override(self):
46         """Test per-command _timeout option."""  # noqa
47         rv = self.vapi.papi.cli_inband(cmd="wait 10", _timeout=15)
48         self.assertEqual(rv.retval, 0)
49
50
51 class TestCLIExtendedVapiTimeout(VppAsfTestCase):
52     maxDiff = None
53
54     @classmethod
55     def setUpClass(cls):
56         cls.vapi_response_timeout = 15
57         cls.__doc__ = (
58             " CLI Test Case w/ Extended (%ssec) Vapi Timeout "
59             % cls.vapi_response_timeout
60         )
61         super(TestCLIExtendedVapiTimeout, cls).setUpClass()
62
63     @classmethod
64     def tearDownClass(cls):
65         super(TestCLIExtendedVapiTimeout, cls).tearDownClass()
66
67     def setUp(self):
68         super(TestCLIExtendedVapiTimeout, self).setUp()
69
70     def tearDown(self):
71         super(TestCLIExtendedVapiTimeout, self).tearDown()
72
73     def test_long_cli_delay(self):
74         """Test that delayed result returns with extended timeout."""
75         wait_secs = self.vapi_response_timeout - 1
76
77         # get vpp time as float
78         start = self.vapi.papi.show_vpe_system_time(
79             _no_type_conversion=True
80         ).vpe_system_time
81         rv = self.vapi.papi.cli_inband(cmd="wait %s" % wait_secs)
82         now = self.vapi.papi.show_vpe_system_time(
83             _no_type_conversion=True
84         ).vpe_system_time
85
86         # assume that the overhead of the measurement is not more that .5 sec.
87         self.assertEqual(round(now - start), wait_secs)
88
89
90 if __name__ == "__main__":
91     unittest.main(testRunner=VppTestRunner)