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