1ead1e536SRenato Botelho do Couto#!/usr/bin/env python3
272d8758fSOle Troan"""CLI functional tests"""
372d8758fSOle Troan
4bfd7d294SPaul Vinciguerraimport datetime
5bfd7d294SPaul Vinciguerraimport time
672d8758fSOle Troanimport unittest
7bfd7d294SPaul Vinciguerra
8bfd7d294SPaul Vinciguerrafrom vpp_papi import vpp_transport_shmem
9bfd7d294SPaul Vinciguerra
1072d8758fSOle Troanfrom framework import VppTestCase, VppTestRunner
1172d8758fSOle Troan
1272d8758fSOle Troan
1372d8758fSOle Troanclass TestCLI(VppTestCase):
1472d8758fSOle Troan    """ CLI Test Case """
1572d8758fSOle Troan    maxDiff = None
1672d8758fSOle Troan
1772d8758fSOle Troan    @classmethod
1872d8758fSOle Troan    def setUpClass(cls):
19bfd7d294SPaul Vinciguerra        # using the framework default
20e2ccdf03SPaul Vinciguerra        cls.vapi_response_timeout = 5
2172d8758fSOle Troan        super(TestCLI, cls).setUpClass()
2272d8758fSOle Troan
2372d8758fSOle Troan    @classmethod
2472d8758fSOle Troan    def tearDownClass(cls):
2572d8758fSOle Troan        super(TestCLI, cls).tearDownClass()
2672d8758fSOle Troan
2772d8758fSOle Troan    def setUp(self):
2872d8758fSOle Troan        super(TestCLI, self).setUp()
2972d8758fSOle Troan
3072d8758fSOle Troan    def tearDown(self):
3172d8758fSOle Troan        super(TestCLI, self).tearDown()
3272d8758fSOle Troan
3372d8758fSOle Troan    def test_cli_retval(self):
3472d8758fSOle Troan        """ CLI inband retval """
3572d8758fSOle Troan        rv = self.vapi.papi.cli_inband(cmd='this command does not exist')
3672d8758fSOle Troan        self.assertNotEqual(rv.retval, 0)
3772d8758fSOle Troan
3872d8758fSOle Troan        rv = self.vapi.papi.cli_inband(cmd='show version')
3972d8758fSOle Troan        self.assertEqual(rv.retval, 0)
4072d8758fSOle Troan
41bfd7d294SPaul Vinciguerra    def test_long_cli_delay(self):
42bfd7d294SPaul Vinciguerra        """ Test that VppApiClient raises VppTransportShmemIOError if timeout."""  # noqa
43bfd7d294SPaul Vinciguerra        with self.assertRaises(
44bfd7d294SPaul Vinciguerra                vpp_transport_shmem.VppTransportShmemIOError) as ctx:
45bfd7d294SPaul Vinciguerra            rv = self.vapi.papi.cli_inband(cmd='wait 10')
46bfd7d294SPaul Vinciguerra
47e2ccdf03SPaul Vinciguerra    def test_long_cli_delay_override(self):
48e2ccdf03SPaul Vinciguerra        """ Test per-command _timeout option."""  # noqa
49e2ccdf03SPaul Vinciguerra        rv = self.vapi.papi.cli_inband(cmd='wait 10', _timeout=15)
50e2ccdf03SPaul Vinciguerra        self.assertEqual(rv.retval, 0)
51e2ccdf03SPaul Vinciguerra
52bfd7d294SPaul Vinciguerra
53bfd7d294SPaul Vinciguerraclass TestCLIExtendedVapiTimeout(VppTestCase):
54bfd7d294SPaul Vinciguerra    maxDiff = None
55bfd7d294SPaul Vinciguerra
56bfd7d294SPaul Vinciguerra    @classmethod
57bfd7d294SPaul Vinciguerra    def setUpClass(cls):
58bfd7d294SPaul Vinciguerra        cls.vapi_response_timeout = 15
59bfd7d294SPaul Vinciguerra        cls.__doc__ = " CLI Test Case w/ Extended (%ssec) Vapi Timeout " \
60bfd7d294SPaul Vinciguerra                      % cls.vapi_response_timeout
61bfd7d294SPaul Vinciguerra        super(TestCLIExtendedVapiTimeout, cls).setUpClass()
62bfd7d294SPaul Vinciguerra
63bfd7d294SPaul Vinciguerra    @classmethod
64bfd7d294SPaul Vinciguerra    def tearDownClass(cls):
65bfd7d294SPaul Vinciguerra        super(TestCLIExtendedVapiTimeout, cls).tearDownClass()
66bfd7d294SPaul Vinciguerra
67bfd7d294SPaul Vinciguerra    def setUp(self):
68bfd7d294SPaul Vinciguerra        super(TestCLIExtendedVapiTimeout, self).setUp()
69bfd7d294SPaul Vinciguerra
70bfd7d294SPaul Vinciguerra    def tearDown(self):
71bfd7d294SPaul Vinciguerra        super(TestCLIExtendedVapiTimeout, self).tearDown()
72bfd7d294SPaul Vinciguerra
73bfd7d294SPaul Vinciguerra    def test_long_cli_delay(self):
74bfd7d294SPaul Vinciguerra        """ Test that delayed result returns with extended timeout."""
75bfd7d294SPaul Vinciguerra        wait_secs = self.vapi_response_timeout - 1
76bfd7d294SPaul Vinciguerra
77bfd7d294SPaul Vinciguerra        # get vpp time as float
78bfd7d294SPaul Vinciguerra        start = self.vapi.papi.show_vpe_system_time(
79bfd7d294SPaul Vinciguerra            _no_type_conversion=True).vpe_system_time
80bfd7d294SPaul Vinciguerra        rv = self.vapi.papi.cli_inband(cmd='wait %s' % wait_secs)
81bfd7d294SPaul Vinciguerra        now = self.vapi.papi.show_vpe_system_time(
82bfd7d294SPaul Vinciguerra            _no_type_conversion=True).vpe_system_time
83bfd7d294SPaul Vinciguerra
84bfd7d294SPaul Vinciguerra        # assume that the overhead of the measurement is not more that .5 sec.
85bfd7d294SPaul Vinciguerra        self.assertEqual(round(now - start), wait_secs)
86bfd7d294SPaul Vinciguerra
8772d8758fSOle Troan
8872d8758fSOle Troanif __name__ == '__main__':
8972d8758fSOle Troan    unittest.main(testRunner=VppTestRunner)
90