New upstream version 18.02
[deb_dpdk.git] / test / cmdline_test / cmdline_test.py
1 #!/usr/bin/env python
2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2010-2014 Intel Corporation
4
5 # Script that runs cmdline_test app and feeds keystrokes into it.
6 from __future__ import print_function
7 import cmdline_test_data
8 import os
9 import pexpect
10 import sys
11
12
13 #
14 # function to run test
15 #
16 def runTest(child, test):
17     child.send(test["Sequence"])
18     if test["Result"] is None:
19         return 0
20     child.expect(test["Result"], 1)
21
22
23 #
24 # history test is a special case
25 #
26 # This test does the following:
27 # 1) fills the history with garbage up to its full capacity
28 #    (just enough to remove last entry)
29 # 2) scrolls back history to the very beginning
30 # 3) checks if the output is as expected, that is, the first
31 #    number in the sequence (not the last entry before it)
32 #
33 # This is a self-contained test, it needs only a pexpect child
34 #
35 def runHistoryTest(child):
36     # find out history size
37     child.sendline(cmdline_test_data.CMD_GET_BUFSIZE)
38     child.expect("History buffer size: \\d+", timeout=1)
39     history_size = int(child.after[len(cmdline_test_data.BUFSIZE_TEMPLATE):])
40     i = 0
41
42     # fill the history with numbers
43     while i < history_size / 10:
44         # add 1 to prevent from parsing as octals
45         child.send("1" + str(i).zfill(8) + cmdline_test_data.ENTER)
46         # the app will simply print out the number
47         child.expect(str(i + 100000000), timeout=1)
48         i += 1
49     # scroll back history
50     child.send(cmdline_test_data.UP * (i + 2) + cmdline_test_data.ENTER)
51     child.expect("100000000", timeout=1)
52
53 # the path to cmdline_test executable is supplied via command-line.
54 if len(sys.argv) < 2:
55     print("Error: please supply cmdline_test app path")
56     sys.exit(1)
57
58 test_app_path = sys.argv[1]
59
60 if not os.path.exists(test_app_path):
61     print("Error: please supply cmdline_test app path")
62     sys.exit(1)
63
64 child = pexpect.spawn(test_app_path)
65
66 print("Running command-line tests...")
67 for test in cmdline_test_data.tests:
68     testname = (test["Name"] + ":").ljust(30)
69     try:
70         runTest(child, test)
71         print(testname, "PASS")
72     except:
73         print(testname, "FAIL")
74         print(child)
75         sys.exit(1)
76
77 # since last test quits the app, run new instance
78 child = pexpect.spawn(test_app_path)
79
80 testname = ("History fill test:").ljust(30)
81 try:
82     runHistoryTest(child)
83     print(testname, "PASS")
84 except:
85     print(testname, "FAIL")
86     print(child)
87     sys.exit(1)
88 child.close()
89 sys.exit(0)