f1d6092488800852a512be15fbacc12d40efcfe5
[dpdk.git] / app / test / autotest_test_funcs.py
1 #!/usr/bin/python
2
3 #   BSD LICENSE
4
5 #   Copyright(c) 2010-2012 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 # Test functions
36
37 import sys, pexpect, time, os, re
38
39 # default autotest, used to run most tests
40 # waits for "Test OK"
41 def default_autotest(child, test_name):
42         child.sendline(test_name)
43         result = child.expect(["Test OK", "Test Failed",
44                 "Command not found", pexpect.TIMEOUT], timeout = 900)
45         if result == 1:
46                 return -1, "Fail"
47         elif result == 2:
48                 return -1, "Fail [Not found]"
49         elif result == 3:
50                 return -1, "Fail [Timeout]"
51         return 0, "Success"
52
53 # autotest used to run dump commands
54 # just fires the command
55 def dump_autotest(child, test_name):
56         child.sendline(test_name)
57         return 0, "Success"
58
59 # memory autotest
60 # reads output and waits for Test OK
61 def memory_autotest(child, test_name):
62         child.sendline(test_name)
63         regexp = "phys:0x[0-9a-f]*, len:0x([0-9a-f]*), virt:0x[0-9a-f]*, socket_id:[0-9]*"
64         index = child.expect([regexp, pexpect.TIMEOUT], timeout = 180)
65         if index != 0:
66                 return -1, "Fail [Timeout]"
67         size = int(child.match.groups()[0], 16)
68         if size <= 0:
69                 return -1, "Fail [Bad size]"
70         index = child.expect(["Test OK", "Test Failed",
71                           pexpect.TIMEOUT], timeout = 10)
72         if index == 1:
73                 return -1, "Fail"
74         elif index == 2:
75                 return -1, "Fail [Timeout]"
76         return 0, "Success"
77
78 def spinlock_autotest(child, test_name):
79         i = 0
80         ir = 0
81         child.sendline(test_name)
82         while True:
83                 index = child.expect(["Test OK",
84                         "Test Failed",
85                         "Hello from core ([0-9]*) !",
86                         "Hello from within recursive locks from ([0-9]*) !",
87                 pexpect.TIMEOUT], timeout = 20)
88                 # ok
89                 if index == 0:
90                         break
91
92                 # message, check ordering
93                 elif index == 2:
94                         if int(child.match.groups()[0]) < i:
95                                 return -1, "Fail [Bad order]"
96                         i = int(child.match.groups()[0])
97                 elif index == 3:
98                         if int(child.match.groups()[0]) < ir:
99                                 return -1, "Fail [Bad order]"
100                         ir = int(child.match.groups()[0])
101
102                 # fail
103                 elif index == 4:
104                         return -1, "Fail [Timeout]"
105                 elif index == 1:
106                         return -1, "Fail"
107
108         return 0, "Success"
109
110 def rwlock_autotest(child, test_name):
111         i = 0
112         child.sendline(test_name)
113         while True:
114                 index = child.expect(["Test OK",
115                         "Test Failed",
116                         "Hello from core ([0-9]*) !",
117                         "Global write lock taken on master core ([0-9]*)",
118                 pexpect.TIMEOUT], timeout = 10)
119                 # ok
120                 if index == 0:
121                         if i != 0xffff:
122                                 return -1, "Fail [Message is missing]"
123                         break
124
125                 # message, check ordering
126                 elif index == 2:
127                         if int(child.match.groups()[0]) < i:
128                                 return -1, "Fail [Bad order]"
129                         i = int(child.match.groups()[0])
130
131                 # must be the last message, check ordering
132                 elif index == 3:
133                         i = 0xffff
134
135                 elif index == 4:
136                         return -1, "Fail [Timeout]"
137
138                 # fail
139                 else:
140                         return -1, "Fail"
141
142         return 0, "Success"
143
144 def logs_autotest(child, test_name):
145         i = 0
146         child.sendline(test_name)
147
148         log_list = [
149                 "TESTAPP1: this is a debug level message",
150                 "TESTAPP1: this is a info level message",
151                 "TESTAPP1: this is a warning level message",
152                 "TESTAPP2: this is a info level message",
153                 "TESTAPP2: this is a warning level message",
154                 "TESTAPP1: this is a debug level message",
155                 "TESTAPP1: this is a debug level message",
156                 "TESTAPP1: this is a info level message",
157                 "TESTAPP1: this is a warning level message",
158                 "TESTAPP2: this is a info level message",
159                 "TESTAPP2: this is a warning level message",
160                 "TESTAPP1: this is a debug level message",
161         ]
162
163         for log_msg in log_list:
164                 index = child.expect([log_msg,
165                                       "Test OK",
166                                       "Test Failed",
167                                       pexpect.TIMEOUT], timeout = 10)
168
169                 if index == 3:
170                         return -1, "Fail [Timeout]"
171                 # not ok
172                 elif index != 0:
173                         return -1, "Fail"
174
175         index = child.expect(["Test OK",
176                 "Test Failed",
177                 pexpect.TIMEOUT], timeout = 10)
178
179         return 0, "Success"
180
181 def timer_autotest(child, test_name):
182         i = 0
183         child.sendline(test_name)
184
185         index = child.expect(["Start timer stress tests \(30 seconds\)",
186                 "Test Failed",
187                 pexpect.TIMEOUT], timeout = 10)
188
189         if index == 1:
190                 return -1, "Fail"
191         elif index == 2:
192                 return -1, "Fail [Timeout]"
193
194         index = child.expect(["Start timer basic tests \(30 seconds\)",
195                 "Test Failed",
196                 pexpect.TIMEOUT], timeout = 40)
197
198         if index == 1:
199                 return -1, "Fail"
200         elif index == 2:
201                 return -1, "Fail [Timeout]"
202
203         prev_lcore_timer1 = -1
204
205         lcore_tim0 = -1
206         lcore_tim1 = -1
207         lcore_tim2 = -1
208         lcore_tim3 = -1
209
210         while True:
211                 index = child.expect(["TESTTIMER: ([0-9]*): callback id=([0-9]*) count=([0-9]*) on core ([0-9]*)",
212                         "Test OK",
213                         "Test Failed",
214                         pexpect.TIMEOUT], timeout = 10)
215
216                 if index == 1:
217                         break
218
219                 if index == 2:
220                         return -1, "Fail"
221                 elif index == 3:
222                         return -1, "Fail [Timeout]"
223
224                 try:
225                         t = int(child.match.groups()[0])
226                         id = int(child.match.groups()[1])
227                         cnt = int(child.match.groups()[2])
228                         lcore = int(child.match.groups()[3])
229                 except:
230                         return -1, "Fail [Cannot parse]"
231
232                 # timer0 always expires on the same core when cnt < 20
233                 if id == 0:
234                         if lcore_tim0 == -1:
235                                 lcore_tim0 = lcore
236                         elif lcore != lcore_tim0 and cnt < 20:
237                                 return -1, "Fail [lcore != lcore_tim0 (%d, %d)]"%(lcore, lcore_tim0)
238                         if cnt > 21:
239                                 return -1, "Fail [tim0 cnt > 21]"
240
241                 # timer1 each time expires on a different core
242                 if id == 1:
243                         if lcore == lcore_tim1:
244                                 return -1, "Fail [lcore == lcore_tim1 (%d, %d)]"%(lcore, lcore_tim1)
245                         lcore_tim1 = lcore
246                         if cnt > 10:
247                                 return -1, "Fail [tim1 cnt > 30]"
248
249                 # timer0 always expires on the same core
250                 if id == 2:
251                         if lcore_tim2 == -1:
252                                 lcore_tim2 = lcore
253                         elif lcore != lcore_tim2:
254                                 return -1, "Fail [lcore != lcore_tim2 (%d, %d)]"%(lcore, lcore_tim2)
255                         if cnt > 30:
256                                 return -1, "Fail [tim2 cnt > 30]"
257
258                 # timer0 always expires on the same core
259                 if id == 3:
260                         if lcore_tim3 == -1:
261                                 lcore_tim3 = lcore
262                         elif lcore != lcore_tim3:
263                                 return -1, "Fail [lcore_tim3 changed (%d -> %d)]"%(lcore, lcore_tim3)
264                         if cnt > 30:
265                                 return -1, "Fail [tim3 cnt > 30]"
266
267         # must be 2 different cores
268         if lcore_tim0 == lcore_tim3:
269                 return -1, "Fail [lcore_tim0 (%d) == lcore_tim3 (%d)]"%(lcore_tim0, lcore_tim3)
270
271         return 0, "Success"
272
273 def ring_autotest(child, test_name):
274         child.sendline(test_name)
275         index = child.expect(["Test OK", "Test Failed",
276                 pexpect.TIMEOUT], timeout = 1500)
277         if index == 1:
278                 return -1, "Fail"
279         elif index == 2:
280                 return -1, "Fail [Timeout]"
281
282         child.sendline("set_watermark test 100")
283         child.sendline("dump_ring test")
284         index = child.expect(["  watermark=100",
285                 pexpect.TIMEOUT], timeout = 1)
286         if index != 0:
287                 return -1, "Fail [Bad watermark]"
288
289         return 0, "Success"
290