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