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