1#!/usr/bin/env python3
2"""CLI functional tests"""
3
4import datetime
5import time
6import unittest
7
8from vpp_papi import vpp_transport_shmem
9
10from framework import VppTestCase, VppTestRunner
11
12
13class 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 VppTransportShmemIOError if timeout."""  # noqa
43        with self.assertRaises(
44                vpp_transport_shmem.VppTransportShmemIOError) as ctx:
45            rv = self.vapi.papi.cli_inband(cmd='wait 10')
46
47    def test_long_cli_delay_override(self):
48        """ Test per-command _timeout option."""  # noqa
49        rv = self.vapi.papi.cli_inband(cmd='wait 10', _timeout=15)
50        self.assertEqual(rv.retval, 0)
51
52
53class TestCLIExtendedVapiTimeout(VppTestCase):
54    maxDiff = None
55
56    @classmethod
57    def setUpClass(cls):
58        cls.vapi_response_timeout = 15
59        cls.__doc__ = " CLI Test Case w/ Extended (%ssec) Vapi Timeout " \
60                      % cls.vapi_response_timeout
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).vpe_system_time
80        rv = self.vapi.papi.cli_inband(cmd='wait %s' % wait_secs)
81        now = self.vapi.papi.show_vpe_system_time(
82            _no_type_conversion=True).vpe_system_time
83
84        # assume that the overhead of the measurement is not more that .5 sec.
85        self.assertEqual(round(now - start), wait_secs)
86
87
88if __name__ == '__main__':
89    unittest.main(testRunner=VppTestRunner)
90