test/memory: fix autotest parsing
[dpdk.git] / test / test / autotest_test_funcs.py
1 #   BSD LICENSE
2 #
3 #   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
4 #   All rights reserved.
5 #
6 #   Redistribution and use in source and binary forms, with or without
7 #   modification, are permitted provided that the following conditions
8 #   are met:
9 #
10 #     * Redistributions of source code must retain the above copyright
11 #       notice, this list of conditions and the following disclaimer.
12 #     * Redistributions in binary form must reproduce the above copyright
13 #       notice, this list of conditions and the following disclaimer in
14 #       the documentation and/or other materials provided with the
15 #       distribution.
16 #     * Neither the name of Intel Corporation nor the names of its
17 #       contributors may be used to endorse or promote products derived
18 #       from this software without specific prior written permission.
19 #
20 #   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 #   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 #   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 #   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 #   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 #   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 #   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 #   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 #   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 # Test functions
33
34 import pexpect
35
36 # default autotest, used to run most tests
37 # waits for "Test OK"
38
39
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
55
56 def dump_autotest(child, test_name):
57     child.sendline(test_name)
58     return 0, "Success"
59
60 # memory autotest
61 # reads output and waits for Test OK
62
63
64 def memory_autotest(child, test_name):
65     lines = 0
66     error = ''
67     child.sendline(test_name)
68     while True:
69         regexp = "IOVA:0x[0-9a-f]*, len:([0-9]*), virt:0x[0-9a-f]*, " \
70                  "socket_id:[0-9]*"
71         index = child.expect([regexp, "Test OK", "Test Failed",
72                               pexpect.TIMEOUT], timeout=10)
73         if index == 3:
74             return -1, "Fail [Timeout]"
75         elif index == 1:
76             break
77         elif index == 2:
78             return -1, "Fail"
79         else:
80             lines = lines + 1
81             size = int(child.match.groups()[0], 10)
82             if size <= 0:
83                 error = 'Bad size'
84
85     if lines <= 0:
86         return -1, "Fail [No entries]"
87     if error != '':
88         return -1, "Fail [{}]".format(error)
89     return 0, "Success"
90
91
92 def spinlock_autotest(child, test_name):
93     i = 0
94     ir = 0
95     child.sendline(test_name)
96     while True:
97         index = child.expect(["Test OK",
98                               "Test Failed",
99                               "Hello from core ([0-9]*) !",
100                               "Hello from within recursive locks "
101                               "from ([0-9]*) !",
102                               pexpect.TIMEOUT], timeout=5)
103         # ok
104         if index == 0:
105             break
106
107         # message, check ordering
108         elif index == 2:
109             if int(child.match.groups()[0]) < i:
110                 return -1, "Fail [Bad order]"
111             i = int(child.match.groups()[0])
112         elif index == 3:
113             if int(child.match.groups()[0]) < ir:
114                 return -1, "Fail [Bad order]"
115             ir = int(child.match.groups()[0])
116
117         # fail
118         elif index == 4:
119             return -1, "Fail [Timeout]"
120         elif index == 1:
121             return -1, "Fail"
122
123     return 0, "Success"
124
125
126 def rwlock_autotest(child, test_name):
127     i = 0
128     child.sendline(test_name)
129     while True:
130         index = child.expect(["Test OK",
131                               "Test Failed",
132                               "Hello from core ([0-9]*) !",
133                               "Global write lock taken on master "
134                               "core ([0-9]*)",
135                               pexpect.TIMEOUT], timeout=10)
136         # ok
137         if index == 0:
138             if i != 0xffff:
139                 return -1, "Fail [Message is missing]"
140             break
141
142         # message, check ordering
143         elif index == 2:
144             if int(child.match.groups()[0]) < i:
145                 return -1, "Fail [Bad order]"
146             i = int(child.match.groups()[0])
147
148         # must be the last message, check ordering
149         elif index == 3:
150             i = 0xffff
151
152         elif index == 4:
153             return -1, "Fail [Timeout]"
154
155         # fail
156         else:
157             return -1, "Fail"
158
159     return 0, "Success"
160
161
162 def logs_autotest(child, test_name):
163     child.sendline(test_name)
164
165     log_list = [
166         "TESTAPP1: error message",
167         "TESTAPP1: critical message",
168         "TESTAPP2: critical message",
169         "TESTAPP1: error message",
170     ]
171
172     for log_msg in log_list:
173         index = child.expect([log_msg,
174                               "Test OK",
175                               "Test Failed",
176                               pexpect.TIMEOUT], timeout=10)
177
178         if index == 3:
179             return -1, "Fail [Timeout]"
180         # not ok
181         elif index != 0:
182             return -1, "Fail"
183
184     index = child.expect(["Test OK",
185                           "Test Failed",
186                           pexpect.TIMEOUT], timeout=10)
187
188     return 0, "Success"
189
190
191 def timer_autotest(child, test_name):
192     child.sendline(test_name)
193
194     index = child.expect(["Start timer stress tests",
195                           "Test Failed",
196                           pexpect.TIMEOUT], timeout=5)
197
198     if index == 1:
199         return -1, "Fail"
200     elif index == 2:
201         return -1, "Fail [Timeout]"
202
203     index = child.expect(["Start timer stress tests 2",
204                           "Test Failed",
205                           pexpect.TIMEOUT], timeout=5)
206
207     if index == 1:
208         return -1, "Fail"
209     elif index == 2:
210         return -1, "Fail [Timeout]"
211
212     index = child.expect(["Start timer basic tests",
213                           "Test Failed",
214                           pexpect.TIMEOUT], timeout=5)
215
216     if index == 1:
217         return -1, "Fail"
218     elif index == 2:
219         return -1, "Fail [Timeout]"
220
221     lcore_tim0 = -1
222     lcore_tim1 = -1
223     lcore_tim2 = -1
224     lcore_tim3 = -1
225
226     while True:
227         index = child.expect(["TESTTIMER: ([0-9]*): callback id=([0-9]*) "
228                               "count=([0-9]*) on core ([0-9]*)",
229                               "Test OK",
230                               "Test Failed",
231                               pexpect.TIMEOUT], timeout=10)
232
233         if index == 1:
234             break
235
236         if index == 2:
237             return -1, "Fail"
238         elif index == 3:
239             return -1, "Fail [Timeout]"
240
241         try:
242             id = int(child.match.groups()[1])
243             cnt = int(child.match.groups()[2])
244             lcore = int(child.match.groups()[3])
245         except:
246             return -1, "Fail [Cannot parse]"
247
248         # timer0 always expires on the same core when cnt < 20
249         if id == 0:
250             if lcore_tim0 == -1:
251                 lcore_tim0 = lcore
252             elif lcore != lcore_tim0 and cnt < 20:
253                 return -1, "Fail [lcore != lcore_tim0 (%d, %d)]" \
254                     % (lcore, lcore_tim0)
255             if cnt > 21:
256                 return -1, "Fail [tim0 cnt > 21]"
257
258         # timer1 each time expires on a different core
259         if id == 1:
260             if lcore == lcore_tim1:
261                 return -1, "Fail [lcore == lcore_tim1 (%d, %d)]" \
262                     % (lcore, lcore_tim1)
263             lcore_tim1 = lcore
264             if cnt > 10:
265                 return -1, "Fail [tim1 cnt > 30]"
266
267         # timer0 always expires on the same core
268         if id == 2:
269             if lcore_tim2 == -1:
270                 lcore_tim2 = lcore
271             elif lcore != lcore_tim2:
272                 return -1, "Fail [lcore != lcore_tim2 (%d, %d)]" \
273                     % (lcore, lcore_tim2)
274             if cnt > 30:
275                 return -1, "Fail [tim2 cnt > 30]"
276
277         # timer0 always expires on the same core
278         if id == 3:
279             if lcore_tim3 == -1:
280                 lcore_tim3 = lcore
281             elif lcore != lcore_tim3:
282                 return -1, "Fail [lcore_tim3 changed (%d -> %d)]" \
283                     % (lcore, lcore_tim3)
284             if cnt > 30:
285                 return -1, "Fail [tim3 cnt > 30]"
286
287     # must be 2 different cores
288     if lcore_tim0 == lcore_tim3:
289         return -1, "Fail [lcore_tim0 (%d) == lcore_tim3 (%d)]" \
290             % (lcore_tim0, lcore_tim3)
291
292     return 0, "Success"
293
294
295 def ring_autotest(child, test_name):
296     child.sendline(test_name)
297     index = child.expect(["Test OK", "Test Failed",
298                           pexpect.TIMEOUT], timeout=2)
299     if index == 1:
300         return -1, "Fail"
301     elif index == 2:
302         return -1, "Fail [Timeout]"
303
304     return 0, "Success"