New upstream version 18.02
[deb_dpdk.git] / test / 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 logs_autotest(child, test_name):
135     child.sendline(test_name)
136
137     log_list = [
138         "TESTAPP1: error message",
139         "TESTAPP1: critical message",
140         "TESTAPP2: critical message",
141         "TESTAPP1: error message",
142     ]
143
144     for log_msg in log_list:
145         index = child.expect([log_msg,
146                               "Test OK",
147                               "Test Failed",
148                               pexpect.TIMEOUT], timeout=10)
149
150         if index == 3:
151             return -1, "Fail [Timeout]"
152         # not ok
153         elif index != 0:
154             return -1, "Fail"
155
156     index = child.expect(["Test OK",
157                           "Test Failed",
158                           pexpect.TIMEOUT], timeout=10)
159
160     return 0, "Success"
161
162
163 def timer_autotest(child, test_name):
164     child.sendline(test_name)
165
166     index = child.expect(["Start timer stress tests",
167                           "Test Failed",
168                           pexpect.TIMEOUT], timeout=5)
169
170     if index == 1:
171         return -1, "Fail"
172     elif index == 2:
173         return -1, "Fail [Timeout]"
174
175     index = child.expect(["Start timer stress tests 2",
176                           "Test Failed",
177                           pexpect.TIMEOUT], timeout=5)
178
179     if index == 1:
180         return -1, "Fail"
181     elif index == 2:
182         return -1, "Fail [Timeout]"
183
184     index = child.expect(["Start timer basic tests",
185                           "Test Failed",
186                           pexpect.TIMEOUT], timeout=5)
187
188     if index == 1:
189         return -1, "Fail"
190     elif index == 2:
191         return -1, "Fail [Timeout]"
192
193     lcore_tim0 = -1
194     lcore_tim1 = -1
195     lcore_tim2 = -1
196     lcore_tim3 = -1
197
198     while True:
199         index = child.expect(["TESTTIMER: ([0-9]*): callback id=([0-9]*) "
200                               "count=([0-9]*) on core ([0-9]*)",
201                               "Test OK",
202                               "Test Failed",
203                               pexpect.TIMEOUT], timeout=10)
204
205         if index == 1:
206             break
207
208         if index == 2:
209             return -1, "Fail"
210         elif index == 3:
211             return -1, "Fail [Timeout]"
212
213         try:
214             id = int(child.match.groups()[1])
215             cnt = int(child.match.groups()[2])
216             lcore = int(child.match.groups()[3])
217         except:
218             return -1, "Fail [Cannot parse]"
219
220         # timer0 always expires on the same core when cnt < 20
221         if id == 0:
222             if lcore_tim0 == -1:
223                 lcore_tim0 = lcore
224             elif lcore != lcore_tim0 and cnt < 20:
225                 return -1, "Fail [lcore != lcore_tim0 (%d, %d)]" \
226                     % (lcore, lcore_tim0)
227             if cnt > 21:
228                 return -1, "Fail [tim0 cnt > 21]"
229
230         # timer1 each time expires on a different core
231         if id == 1:
232             if lcore == lcore_tim1:
233                 return -1, "Fail [lcore == lcore_tim1 (%d, %d)]" \
234                     % (lcore, lcore_tim1)
235             lcore_tim1 = lcore
236             if cnt > 10:
237                 return -1, "Fail [tim1 cnt > 30]"
238
239         # timer0 always expires on the same core
240         if id == 2:
241             if lcore_tim2 == -1:
242                 lcore_tim2 = lcore
243             elif lcore != lcore_tim2:
244                 return -1, "Fail [lcore != lcore_tim2 (%d, %d)]" \
245                     % (lcore, lcore_tim2)
246             if cnt > 30:
247                 return -1, "Fail [tim2 cnt > 30]"
248
249         # timer0 always expires on the same core
250         if id == 3:
251             if lcore_tim3 == -1:
252                 lcore_tim3 = lcore
253             elif lcore != lcore_tim3:
254                 return -1, "Fail [lcore_tim3 changed (%d -> %d)]" \
255                     % (lcore, lcore_tim3)
256             if cnt > 30:
257                 return -1, "Fail [tim3 cnt > 30]"
258
259     # must be 2 different cores
260     if lcore_tim0 == lcore_tim3:
261         return -1, "Fail [lcore_tim0 (%d) == lcore_tim3 (%d)]" \
262             % (lcore_tim0, lcore_tim3)
263
264     return 0, "Success"
265
266
267 def ring_autotest(child, test_name):
268     child.sendline(test_name)
269     index = child.expect(["Test OK", "Test Failed",
270                           pexpect.TIMEOUT], timeout=2)
271     if index == 1:
272         return -1, "Fail"
273     elif index == 2:
274         return -1, "Fail [Timeout]"
275
276     return 0, "Success"