15129044dSC.J. Collier#!/usr/bin/python
25129044dSC.J. Collier
35129044dSC.J. Collier#   BSD LICENSE
45129044dSC.J. Collier#
55129044dSC.J. Collier#   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
65129044dSC.J. Collier#   All rights reserved.
75129044dSC.J. Collier#
85129044dSC.J. Collier#   Redistribution and use in source and binary forms, with or without
95129044dSC.J. Collier#   modification, are permitted provided that the following conditions
105129044dSC.J. Collier#   are met:
115129044dSC.J. Collier#
125129044dSC.J. Collier#     * Redistributions of source code must retain the above copyright
135129044dSC.J. Collier#       notice, this list of conditions and the following disclaimer.
145129044dSC.J. Collier#     * Redistributions in binary form must reproduce the above copyright
155129044dSC.J. Collier#       notice, this list of conditions and the following disclaimer in
165129044dSC.J. Collier#       the documentation and/or other materials provided with the
175129044dSC.J. Collier#       distribution.
185129044dSC.J. Collier#     * Neither the name of Intel Corporation nor the names of its
195129044dSC.J. Collier#       contributors may be used to endorse or promote products derived
205129044dSC.J. Collier#       from this software without specific prior written permission.
215129044dSC.J. Collier#
225129044dSC.J. Collier#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
235129044dSC.J. Collier#   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
245129044dSC.J. Collier#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
255129044dSC.J. Collier#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
265129044dSC.J. Collier#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
275129044dSC.J. Collier#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
285129044dSC.J. Collier#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
295129044dSC.J. Collier#   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
305129044dSC.J. Collier#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
315129044dSC.J. Collier#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
325129044dSC.J. Collier#   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
335129044dSC.J. Collier
345129044dSC.J. Collier# Test functions
355129044dSC.J. Collier
365129044dSC.J. Collierimport sys, pexpect, time, os, re
375129044dSC.J. Collier
385129044dSC.J. Collier# default autotest, used to run most tests
395129044dSC.J. Collier# waits for "Test OK"
405129044dSC.J. Collierdef default_autotest(child, test_name):
415129044dSC.J. Collier	child.sendline(test_name)
425129044dSC.J. Collier	result = child.expect(["Test OK", "Test Failed",
435129044dSC.J. Collier		"Command not found", pexpect.TIMEOUT], timeout = 900)
445129044dSC.J. Collier	if result == 1:
455129044dSC.J. Collier		return -1, "Fail"
465129044dSC.J. Collier	elif result == 2:
475129044dSC.J. Collier		return -1, "Fail [Not found]"
485129044dSC.J. Collier	elif result == 3:
495129044dSC.J. Collier		return -1, "Fail [Timeout]"
505129044dSC.J. Collier	return 0, "Success"
515129044dSC.J. Collier
525129044dSC.J. Collier# autotest used to run dump commands
535129044dSC.J. Collier# just fires the command
545129044dSC.J. Collierdef dump_autotest(child, test_name):
555129044dSC.J. Collier	child.sendline(test_name)
565129044dSC.J. Collier	return 0, "Success"
575129044dSC.J. Collier
585129044dSC.J. Collier# memory autotest
595129044dSC.J. Collier# reads output and waits for Test OK
605129044dSC.J. Collierdef memory_autotest(child, test_name):
615129044dSC.J. Collier	child.sendline(test_name)
625129044dSC.J. Collier	regexp = "phys:0x[0-9a-f]*, len:([0-9]*), virt:0x[0-9a-f]*, socket_id:[0-9]*"
635129044dSC.J. Collier	index = child.expect([regexp, pexpect.TIMEOUT], timeout = 180)
645129044dSC.J. Collier	if index != 0:
655129044dSC.J. Collier		return -1, "Fail [Timeout]"
665129044dSC.J. Collier	size = int(child.match.groups()[0], 16)
675129044dSC.J. Collier	if size <= 0:
685129044dSC.J. Collier		return -1, "Fail [Bad size]"
695129044dSC.J. Collier	index = child.expect(["Test OK", "Test Failed",
705129044dSC.J. Collier		          pexpect.TIMEOUT], timeout = 10)
715129044dSC.J. Collier	if index == 1:
725129044dSC.J. Collier		return -1, "Fail"
735129044dSC.J. Collier	elif index == 2:
745129044dSC.J. Collier		return -1, "Fail [Timeout]"
755129044dSC.J. Collier	return 0, "Success"
765129044dSC.J. Collier
775129044dSC.J. Collierdef spinlock_autotest(child, test_name):
785129044dSC.J. Collier	i = 0
795129044dSC.J. Collier	ir = 0
805129044dSC.J. Collier	child.sendline(test_name)
815129044dSC.J. Collier	while True:
825129044dSC.J. Collier		index = child.expect(["Test OK",
835129044dSC.J. Collier			"Test Failed",
845129044dSC.J. Collier			"Hello from core ([0-9]*) !",
855129044dSC.J. Collier			"Hello from within recursive locks from ([0-9]*) !",
86809f0800SChristian Ehrhardt		pexpect.TIMEOUT], timeout = 5)
875129044dSC.J. Collier		# ok
885129044dSC.J. Collier		if index == 0:
895129044dSC.J. Collier			break
905129044dSC.J. Collier
915129044dSC.J. Collier		# message, check ordering
925129044dSC.J. Collier		elif index == 2:
935129044dSC.J. Collier			if int(child.match.groups()[0]) < i:
945129044dSC.J. Collier				return -1, "Fail [Bad order]"
955129044dSC.J. Collier			i = int(child.match.groups()[0])
965129044dSC.J. Collier		elif index == 3:
975129044dSC.J. Collier			if int(child.match.groups()[0]) < ir:
985129044dSC.J. Collier				return -1, "Fail [Bad order]"
995129044dSC.J. Collier			ir = int(child.match.groups()[0])
1005129044dSC.J. Collier
1015129044dSC.J. Collier		# fail
1025129044dSC.J. Collier		elif index == 4:
1035129044dSC.J. Collier			return -1, "Fail [Timeout]"
1045129044dSC.J. Collier		elif index == 1:
1055129044dSC.J. Collier			return -1, "Fail"
1065129044dSC.J. Collier
1075129044dSC.J. Collier	return 0, "Success"
1085129044dSC.J. Collier
1095129044dSC.J. Collierdef rwlock_autotest(child, test_name):
1105129044dSC.J. Collier	i = 0
1115129044dSC.J. Collier	child.sendline(test_name)
1125129044dSC.J. Collier	while True:
1135129044dSC.J. Collier		index = child.expect(["Test OK",
1145129044dSC.J. Collier			"Test Failed",
1155129044dSC.J. Collier			"Hello from core ([0-9]*) !",
1165129044dSC.J. Collier			"Global write lock taken on master core ([0-9]*)",
1175129044dSC.J. Collier		pexpect.TIMEOUT], timeout = 10)
1185129044dSC.J. Collier		# ok
1195129044dSC.J. Collier		if index == 0:
1205129044dSC.J. Collier			if i != 0xffff:
1215129044dSC.J. Collier				return -1, "Fail [Message is missing]"
1225129044dSC.J. Collier			break
1235129044dSC.J. Collier
1245129044dSC.J. Collier		# message, check ordering
1255129044dSC.J. Collier		elif index == 2:
1265129044dSC.J. Collier			if int(child.match.groups()[0]) < i:
1275129044dSC.J. Collier				return -1, "Fail [Bad order]"
1285129044dSC.J. Collier			i = int(child.match.groups()[0])
1295129044dSC.J. Collier
1305129044dSC.J. Collier		# must be the last message, check ordering
1315129044dSC.J. Collier		elif index == 3:
1325129044dSC.J. Collier			i = 0xffff
1335129044dSC.J. Collier
1345129044dSC.J. Collier		elif index == 4:
1355129044dSC.J. Collier			return -1, "Fail [Timeout]"
1365129044dSC.J. Collier
1375129044dSC.J. Collier		# fail
1385129044dSC.J. Collier		else:
1395129044dSC.J. Collier			return -1, "Fail"
1405129044dSC.J. Collier
1415129044dSC.J. Collier	return 0, "Success"
1425129044dSC.J. Collier
1435129044dSC.J. Collierdef logs_autotest(child, test_name):
1445129044dSC.J. Collier	i = 0
1455129044dSC.J. Collier	child.sendline(test_name)
1465129044dSC.J. Collier
1475129044dSC.J. Collier	log_list = [
148809f0800SChristian Ehrhardt		"TESTAPP1: error message",
149809f0800SChristian Ehrhardt		"TESTAPP1: critical message",
150809f0800SChristian Ehrhardt		"TESTAPP2: critical message",
151809f0800SChristian Ehrhardt		"TESTAPP1: error message",
1525129044dSC.J. Collier	]
1535129044dSC.J. Collier
1545129044dSC.J. Collier	for log_msg in log_list:
1555129044dSC.J. Collier		index = child.expect([log_msg,
1565129044dSC.J. Collier				      "Test OK",
1575129044dSC.J. Collier				      "Test Failed",
1585129044dSC.J. Collier				      pexpect.TIMEOUT], timeout = 10)
1595129044dSC.J. Collier
1605129044dSC.J. Collier		if index == 3:
1615129044dSC.J. Collier			return -1, "Fail [Timeout]"
1625129044dSC.J. Collier		# not ok
1635129044dSC.J. Collier		elif index != 0:
1645129044dSC.J. Collier			return -1, "Fail"
1655129044dSC.J. Collier
1665129044dSC.J. Collier	index = child.expect(["Test OK",
1675129044dSC.J. Collier		"Test Failed",
1685129044dSC.J. Collier		pexpect.TIMEOUT], timeout = 10)
1695129044dSC.J. Collier
1705129044dSC.J. Collier	return 0, "Success"
1715129044dSC.J. Collier
1725129044dSC.J. Collierdef timer_autotest(child, test_name):
1735129044dSC.J. Collier	i = 0
1745129044dSC.J. Collier	child.sendline(test_name)
1755129044dSC.J. Collier
176809f0800SChristian Ehrhardt	index = child.expect(["Start timer stress tests",
1775129044dSC.J. Collier		"Test Failed",
178809f0800SChristian Ehrhardt		pexpect.TIMEOUT], timeout = 5)
1795129044dSC.J. Collier
1805129044dSC.J. Collier	if index == 1:
1815129044dSC.J. Collier		return -1, "Fail"
1825129044dSC.J. Collier	elif index == 2:
1835129044dSC.J. Collier		return -1, "Fail [Timeout]"
1845129044dSC.J. Collier
1855129044dSC.J. Collier	index = child.expect(["Start timer stress tests 2",
1865129044dSC.J. Collier		"Test Failed",
187809f0800SChristian Ehrhardt		pexpect.TIMEOUT], timeout = 5)
1885129044dSC.J. Collier
1895129044dSC.J. Collier	if index == 1:
1905129044dSC.J. Collier		return -1, "Fail"
1915129044dSC.J. Collier	elif index == 2:
1925129044dSC.J. Collier		return -1, "Fail [Timeout]"
1935129044dSC.J. Collier
194809f0800SChristian Ehrhardt	index = child.expect(["Start timer basic tests",
1955129044dSC.J. Collier		"Test Failed",
196809f0800SChristian Ehrhardt		pexpect.TIMEOUT], timeout = 5)
1975129044dSC.J. Collier
1985129044dSC.J. Collier	if index == 1:
1995129044dSC.J. Collier		return -1, "Fail"
2005129044dSC.J. Collier	elif index == 2:
2015129044dSC.J. Collier		return -1, "Fail [Timeout]"
2025129044dSC.J. Collier
2035129044dSC.J. Collier	prev_lcore_timer1 = -1
2045129044dSC.J. Collier
2055129044dSC.J. Collier	lcore_tim0 = -1
2065129044dSC.J. Collier	lcore_tim1 = -1
2075129044dSC.J. Collier	lcore_tim2 = -1
2085129044dSC.J. Collier	lcore_tim3 = -1
2095129044dSC.J. Collier
2105129044dSC.J. Collier	while True:
2115129044dSC.J. Collier		index = child.expect(["TESTTIMER: ([0-9]*): callback id=([0-9]*) count=([0-9]*) on core ([0-9]*)",
2125129044dSC.J. Collier			"Test OK",
2135129044dSC.J. Collier			"Test Failed",
2145129044dSC.J. Collier			pexpect.TIMEOUT], timeout = 10)
2155129044dSC.J. Collier
2165129044dSC.J. Collier		if index == 1:
2175129044dSC.J. Collier			break
2185129044dSC.J. Collier
2195129044dSC.J. Collier		if index == 2:
2205129044dSC.J. Collier			return -1, "Fail"
2215129044dSC.J. Collier		elif index == 3:
2225129044dSC.J. Collier			return -1, "Fail [Timeout]"
2235129044dSC.J. Collier
2245129044dSC.J. Collier		try:
2255129044dSC.J. Collier			t = int(child.match.groups()[0])
2265129044dSC.J. Collier			id = int(child.match.groups()[1])
2275129044dSC.J. Collier			cnt = int(child.match.groups()[2])
2285129044dSC.J. Collier			lcore = int(child.match.groups()[3])
2295129044dSC.J. Collier		except:
2305129044dSC.J. Collier			return -1, "Fail [Cannot parse]"
2315129044dSC.J. Collier
2325129044dSC.J. Collier		# timer0 always expires on the same core when cnt < 20
2335129044dSC.J. Collier		if id == 0:
2345129044dSC.J. Collier			if lcore_tim0 == -1:
2355129044dSC.J. Collier				lcore_tim0 = lcore
2365129044dSC.J. Collier			elif lcore != lcore_tim0 and cnt < 20:
2375129044dSC.J. Collier				return -1, "Fail [lcore != lcore_tim0 (%d, %d)]"%(lcore, lcore_tim0)
2385129044dSC.J. Collier			if cnt > 21:
2395129044dSC.J. Collier				return -1, "Fail [tim0 cnt > 21]"
2405129044dSC.J. Collier
2415129044dSC.J. Collier		# timer1 each time expires on a different core
2425129044dSC.J. Collier		if id == 1:
2435129044dSC.J. Collier			if lcore == lcore_tim1:
2445129044dSC.J. Collier				return -1, "Fail [lcore == lcore_tim1 (%d, %d)]"%(lcore, lcore_tim1)
2455129044dSC.J. Collier			lcore_tim1 = lcore
2465129044dSC.J. Collier			if cnt > 10:
2475129044dSC.J. Collier				return -1, "Fail [tim1 cnt > 30]"
2485129044dSC.J. Collier
2495129044dSC.J. Collier		# timer0 always expires on the same core
2505129044dSC.J. Collier		if id == 2:
2515129044dSC.J. Collier			if lcore_tim2 == -1:
2525129044dSC.J. Collier				lcore_tim2 = lcore
2535129044dSC.J. Collier			elif lcore != lcore_tim2:
2545129044dSC.J. Collier				return -1, "Fail [lcore != lcore_tim2 (%d, %d)]"%(lcore, lcore_tim2)
2555129044dSC.J. Collier			if cnt > 30:
2565129044dSC.J. Collier				return -1, "Fail [tim2 cnt > 30]"
2575129044dSC.J. Collier
2585129044dSC.J. Collier		# timer0 always expires on the same core
2595129044dSC.J. Collier		if id == 3:
2605129044dSC.J. Collier			if lcore_tim3 == -1:
2615129044dSC.J. Collier				lcore_tim3 = lcore
2625129044dSC.J. Collier			elif lcore != lcore_tim3:
2635129044dSC.J. Collier				return -1, "Fail [lcore_tim3 changed (%d -> %d)]"%(lcore, lcore_tim3)
2645129044dSC.J. Collier			if cnt > 30:
2655129044dSC.J. Collier				return -1, "Fail [tim3 cnt > 30]"
2665129044dSC.J. Collier
2675129044dSC.J. Collier	# must be 2 different cores
2685129044dSC.J. Collier	if lcore_tim0 == lcore_tim3:
2695129044dSC.J. Collier		return -1, "Fail [lcore_tim0 (%d) == lcore_tim3 (%d)]"%(lcore_tim0, lcore_tim3)
2705129044dSC.J. Collier
2715129044dSC.J. Collier	return 0, "Success"
2725129044dSC.J. Collier
2735129044dSC.J. Collierdef ring_autotest(child, test_name):
2745129044dSC.J. Collier	child.sendline(test_name)
2755129044dSC.J. Collier	index = child.expect(["Test OK", "Test Failed",
276809f0800SChristian Ehrhardt		pexpect.TIMEOUT], timeout = 2)
2775129044dSC.J. Collier	if index == 1:
2785129044dSC.J. Collier		return -1, "Fail"
2795129044dSC.J. Collier	elif index == 2:
2805129044dSC.J. Collier		return -1, "Fail [Timeout]"
2815129044dSC.J. Collier
2825129044dSC.J. Collier	child.sendline("set_watermark test 100")
2835129044dSC.J. Collier	child.sendline("dump_ring test")
2845129044dSC.J. Collier	index = child.expect(["  watermark=100",
2855129044dSC.J. Collier		pexpect.TIMEOUT], timeout = 1)
2865129044dSC.J. Collier	if index != 0:
2875129044dSC.J. Collier		return -1, "Fail [Bad watermark]"
2885129044dSC.J. Collier
2895129044dSC.J. Collier	return 0, "Success"
290