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