test/service: use compiler atomics for lock sync
[dpdk.git] / app / test / autotest_test_funcs.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2010-2014 Intel Corporation
4
5 # Test functions
6
7 import pexpect
8
9 # default autotest, used to run most tests
10 # waits for "Test OK"
11
12
13 def default_autotest(child, test_name):
14     child.sendline(test_name)
15     result = child.expect(["Test OK", "Test Failed",
16                            "Command not found", pexpect.TIMEOUT,
17                            "Test Skipped"], timeout=900)
18     if result == 1:
19         return -1, "Fail"
20     elif result == 2:
21         return -1, "Fail [Not found]"
22     elif result == 3:
23         return -1, "Fail [Timeout]"
24     elif result == 4:
25         return 0, "Skipped [Not Run]"
26     return 0, "Success"
27
28 # autotest used to run dump commands
29 # just fires the command
30
31
32 def dump_autotest(child, test_name):
33     child.sendline(test_name)
34     return 0, "Success"
35
36 # memory autotest
37 # reads output and waits for Test OK
38
39
40 def memory_autotest(child, test_name):
41     lines = 0
42     error = ''
43     child.sendline(test_name)
44     while True:
45         regexp = "IOVA:0x[0-9a-f]*, len:([0-9]*), virt:0x[0-9a-f]*, " \
46                  "socket_id:[0-9]*"
47         index = child.expect([regexp, "Test OK", "Test Failed",
48                               pexpect.TIMEOUT], timeout=10)
49         if index == 3:
50             return -1, "Fail [Timeout]"
51         elif index == 1:
52             break
53         elif index == 2:
54             return -1, "Fail"
55         else:
56             lines = lines + 1
57             size = int(child.match.groups()[0], 10)
58             if size <= 0:
59                 error = 'Bad size'
60
61     if lines <= 0:
62         return -1, "Fail [No entries]"
63     if error != '':
64         return -1, "Fail [{}]".format(error)
65     return 0, "Success"
66
67
68 def spinlock_autotest(child, test_name):
69     i = 0
70     ir = 0
71     child.sendline(test_name)
72     while True:
73         index = child.expect(["Test OK",
74                               "Test Failed",
75                               "Hello from core ([0-9]*) !",
76                               "Hello from within recursive locks "
77                               "from ([0-9]*) !",
78                               pexpect.TIMEOUT], timeout=5)
79         # ok
80         if index == 0:
81             break
82
83         # message, check ordering
84         elif index == 2:
85             if int(child.match.groups()[0]) < i:
86                 return -1, "Fail [Bad order]"
87             i = int(child.match.groups()[0])
88         elif index == 3:
89             if int(child.match.groups()[0]) < ir:
90                 return -1, "Fail [Bad order]"
91             ir = int(child.match.groups()[0])
92
93         # fail
94         elif index == 4:
95             return -1, "Fail [Timeout]"
96         elif index == 1:
97             return -1, "Fail"
98
99     return 0, "Success"
100
101
102 def rwlock_autotest(child, test_name):
103     i = 0
104     child.sendline(test_name)
105     while True:
106         index = child.expect(["Test OK",
107                               "Test Failed",
108                               "Hello from core ([0-9]*) !",
109                               "Global write lock taken on main "
110                               "core ([0-9]*)",
111                               pexpect.TIMEOUT], timeout=10)
112         # ok
113         if index == 0:
114             if i != 0xffff:
115                 return -1, "Fail [Message is missing]"
116             break
117
118         # message, check ordering
119         elif index == 2:
120             if int(child.match.groups()[0]) < i:
121                 return -1, "Fail [Bad order]"
122             i = int(child.match.groups()[0])
123
124         # must be the last message, check ordering
125         elif index == 3:
126             i = 0xffff
127
128         elif index == 4:
129             return -1, "Fail [Timeout]"
130
131         # fail
132         else:
133             return -1, "Fail"
134
135     return 0, "Success"
136
137
138 def ticketlock_autotest(child, test_name):
139     i = 0
140     ir = 0
141     child.sendline(test_name)
142     while True:
143         index = child.expect(["Test OK",
144                               "Test Failed",
145                               "Hello from core ([0-9]*) !",
146                               "Hello from within recursive locks "
147                               "from ([0-9]*) !",
148                               pexpect.TIMEOUT], timeout=5)
149         # ok
150         if index == 0:
151             break
152
153         # message, check ordering
154         elif index == 2:
155             if int(child.match.groups()[0]) < i:
156                 return -1, "Fail [Bad order]"
157             i = int(child.match.groups()[0])
158         elif index == 3:
159             if int(child.match.groups()[0]) < ir:
160                 return -1, "Fail [Bad order]"
161             ir = int(child.match.groups()[0])
162
163         # fail
164         elif index == 4:
165             return -1, "Fail [Timeout]"
166         elif index == 1:
167             return -1, "Fail"
168
169     return 0, "Success"
170
171 def mcslock_autotest(child, test_name):
172     i = 0
173     ir = 0
174     child.sendline(test_name)
175     while True:
176         index = child.expect(["Test OK",
177                               "Test Failed",
178                               "lcore ([0-9]*) state: ([0-1])"
179                               "MCS lock taken on core ([0-9]*)",
180                               "MCS lock released on core ([0-9]*)",
181                               pexpect.TIMEOUT], timeout=5)
182         # ok
183         if index == 0:
184             break
185
186         # message, check ordering
187         elif index == 2:
188             if int(child.match.groups()[0]) < i:
189                 return -1, "Fail [Bad order]"
190             i = int(child.match.groups()[0])
191         elif index == 3:
192             if int(child.match.groups()[0]) < ir:
193                 return -1, "Fail [Bad order]"
194             ir = int(child.match.groups()[0])
195
196         # fail
197         elif index == 4:
198             return -1, "Fail [Timeout]"
199         elif index == 1:
200             return -1, "Fail"
201
202     return 0, "Success"
203
204 def logs_autotest(child, test_name):
205     child.sendline(test_name)
206
207     log_list = [
208         "TESTAPP1: error message",
209         "TESTAPP1: critical message",
210         "TESTAPP2: critical message",
211         "TESTAPP1: error message",
212     ]
213
214     for log_msg in log_list:
215         index = child.expect([log_msg,
216                               "Test OK",
217                               "Test Failed",
218                               pexpect.TIMEOUT], timeout=10)
219
220         if index == 3:
221             return -1, "Fail [Timeout]"
222         # not ok
223         elif index != 0:
224             return -1, "Fail"
225
226     index = child.expect(["Test OK",
227                           "Test Failed",
228                           pexpect.TIMEOUT], timeout=10)
229
230     return 0, "Success"
231
232
233 def timer_autotest(child, test_name):
234     child.sendline(test_name)
235
236     index = child.expect(["Start timer stress tests",
237                           "Test Failed",
238                           pexpect.TIMEOUT], timeout=5)
239
240     if index == 1:
241         return -1, "Fail"
242     elif index == 2:
243         return -1, "Fail [Timeout]"
244
245     index = child.expect(["Start timer stress tests 2",
246                           "Test Failed",
247                           pexpect.TIMEOUT], timeout=5)
248
249     if index == 1:
250         return -1, "Fail"
251     elif index == 2:
252         return -1, "Fail [Timeout]"
253
254     index = child.expect(["Start timer basic tests",
255                           "Test Failed",
256                           pexpect.TIMEOUT], timeout=5)
257
258     if index == 1:
259         return -1, "Fail"
260     elif index == 2:
261         return -1, "Fail [Timeout]"
262
263     lcore_tim0 = -1
264     lcore_tim1 = -1
265     lcore_tim2 = -1
266     lcore_tim3 = -1
267
268     while True:
269         index = child.expect(["TESTTIMER: ([0-9]*): callback id=([0-9]*) "
270                               "count=([0-9]*) on core ([0-9]*)",
271                               "Test OK",
272                               "Test Failed",
273                               pexpect.TIMEOUT], timeout=10)
274
275         if index == 1:
276             break
277
278         if index == 2:
279             return -1, "Fail"
280         elif index == 3:
281             return -1, "Fail [Timeout]"
282
283         try:
284             id = int(child.match.groups()[1])
285             cnt = int(child.match.groups()[2])
286             lcore = int(child.match.groups()[3])
287         except:
288             return -1, "Fail [Cannot parse]"
289
290         # timer0 always expires on the same core when cnt < 20
291         if id == 0:
292             if lcore_tim0 == -1:
293                 lcore_tim0 = lcore
294             elif lcore != lcore_tim0 and cnt < 20:
295                 return -1, "Fail [lcore != lcore_tim0 (%d, %d)]" \
296                     % (lcore, lcore_tim0)
297             if cnt > 21:
298                 return -1, "Fail [tim0 cnt > 21]"
299
300         # timer1 each time expires on a different core
301         if id == 1:
302             if lcore == lcore_tim1:
303                 return -1, "Fail [lcore == lcore_tim1 (%d, %d)]" \
304                     % (lcore, lcore_tim1)
305             lcore_tim1 = lcore
306             if cnt > 10:
307                 return -1, "Fail [tim1 cnt > 30]"
308
309         # timer0 always expires on the same core
310         if id == 2:
311             if lcore_tim2 == -1:
312                 lcore_tim2 = lcore
313             elif lcore != lcore_tim2:
314                 return -1, "Fail [lcore != lcore_tim2 (%d, %d)]" \
315                     % (lcore, lcore_tim2)
316             if cnt > 30:
317                 return -1, "Fail [tim2 cnt > 30]"
318
319         # timer0 always expires on the same core
320         if id == 3:
321             if lcore_tim3 == -1:
322                 lcore_tim3 = lcore
323             elif lcore != lcore_tim3:
324                 return -1, "Fail [lcore_tim3 changed (%d -> %d)]" \
325                     % (lcore, lcore_tim3)
326             if cnt > 30:
327                 return -1, "Fail [tim3 cnt > 30]"
328
329     # must be 2 different cores
330     if lcore_tim0 == lcore_tim3:
331         return -1, "Fail [lcore_tim0 (%d) == lcore_tim3 (%d)]" \
332             % (lcore_tim0, lcore_tim3)
333
334     return 0, "Success"
335
336
337 def ring_autotest(child, test_name):
338     child.sendline(test_name)
339     index = child.expect(["Test OK", "Test Failed",
340                           pexpect.TIMEOUT], timeout=2)
341     if index == 1:
342         return -1, "Fail"
343     elif index == 2:
344         return -1, "Fail [Timeout]"
345
346     return 0, "Success"