update copyright date to 2013
[dpdk.git] / app / cmdline_test / cmdline_test.py
1 #!/usr/bin/python
2
3 #   BSD LICENSE
4
5 #   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
6 #   All rights reserved.
7
8 #   Redistribution and use in source and binary forms, with or without 
9 #   modification, are permitted provided that the following conditions 
10 #   are met:
11
12 #     * Redistributions of source code must retain the above copyright 
13 #       notice, this list of conditions and the following disclaimer.
14 #     * Redistributions in binary form must reproduce the above copyright 
15 #       notice, this list of conditions and the following disclaimer in 
16 #       the documentation and/or other materials provided with the 
17 #       distribution.
18 #     * Neither the name of Intel Corporation nor the names of its 
19 #       contributors may be used to endorse or promote products derived 
20 #       from this software without specific prior written permission.
21
22 #   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
23 #   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
24 #   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
25 #   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
26 #   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
27 #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
28 #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
29 #   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
30 #   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31 #   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
32 #   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34
35 # Script that runs cmdline_test app and feeds keystrokes into it.
36
37 import sys, pexpect, string, os, cmdline_test_data
38
39 #
40 # function to run test
41 #
42 def runTest(child,test):
43         child.send(test["Sequence"])
44         if test["Result"] == None:
45                 return 0
46         child.expect(test["Result"],1)
47
48 #
49 # history test is a special case
50 #
51 # This test does the following:
52 # 1) fills the history with garbage up to its full capacity
53 #    (just enough to remove last entry)
54 # 2) scrolls back history to the very beginning
55 # 3) checks if the output is as expected, that is, the first
56 #    number in the sequence (not the last entry before it)
57 #
58 # This is a self-contained test, it needs only a pexpect child
59 #
60 def runHistoryTest(child):
61         # find out history size
62         child.sendline(cmdline_test_data.CMD_GET_BUFSIZE)
63         child.expect("History buffer size: \\d+", timeout=1)
64         history_size = int(child.after[len(cmdline_test_data.BUFSIZE_TEMPLATE):])
65         i = 0
66
67         # fill the history with numbers
68         while i < history_size / 10:
69                 # add 1 to prevent from parsing as octals
70                 child.send("1" + str(i).zfill(8) + cmdline_test_data.ENTER)
71                 # the app will simply print out the number                      
72                 child.expect(str(i + 100000000), timeout=1)
73                 i += 1
74         # scroll back history
75         child.send(cmdline_test_data.UP * (i + 2) + cmdline_test_data.ENTER)
76         child.expect("100000000", timeout=1)
77
78 # the path to cmdline_test executable is supplied via command-line.
79 if len(sys.argv) < 2:
80         print "Error: please supply cmdline_test app path"
81         sys.exit(1)
82
83 test_app_path = sys.argv[1]
84
85 if not os.path.exists(test_app_path):
86         print "Error: please supply cmdline_test app path"
87         sys.exit(1)
88
89 child = pexpect.spawn(test_app_path)
90
91 print "Running command-line tests..."
92 for test in cmdline_test_data.tests:
93         print (test["Name"] + ":").ljust(30),
94         try:
95                 runTest(child,test)
96                 print "PASS"
97         except:
98                 print "FAIL"
99                 print child
100                 sys.exit(1)
101
102 # since last test quits the app, run new instance
103 child = pexpect.spawn(test_app_path)
104
105 print ("History fill test:").ljust(30),
106 try:
107         runHistoryTest(child)
108         print "PASS"
109 except:
110         print "FAIL"
111         print child
112         sys.exit(1)
113 child.close()
114 sys.exit(0)
115