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