annotate test/testemulation.py @ 347:742588fb8cd6 devel

Merge into devel branch
author Windel Bouwman
date Fri, 07 Mar 2014 17:10:21 +0100
parents 3bb7dcfe5529
children 899ae3aea803
rev   line source
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
1 import unittest
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
2 import os
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
3 import sys
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
4 import subprocess
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
5 import socket
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
6 import time
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
7
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
8 from testzcc import ZccBaseTestCase
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
9
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
10 # Store testdir for safe switch back to directory:
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
11 testdir = os.path.dirname(os.path.abspath(__file__))
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
12
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
13
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
14 class EmulationTestCase(ZccBaseTestCase):
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
15 """ Tests the compiler driver """
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
16 def setUp(self):
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
17 if 'TESTEMU' not in os.environ:
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
18 self.skipTest('Not running emulation tests')
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
19
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
20 def runQemu(self, kernel, machine='lm3s811evb'):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
21 args = ['qemu-system-arm', '-M', machine, '-m', '16M',
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
22 '-nographic', '-kernel', kernel, '-monitor',
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
23 'unix:qemucontrol.sock,server',
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
24 '-serial', 'unix:qemuserial.sock,server']
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
25 p = subprocess.Popen(args)
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
26
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
27 # Give process some time to boot:
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
28 time.sleep(0.5)
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
29
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
30 # Connect to the control socket:
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
31 qemu_control = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
32 qemu_control.connect('qemucontrol.sock')
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
33
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 339
diff changeset
34 time.sleep(0.5)
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
35
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
36 # Now connect to the serial output:
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
37 qemu_serial = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
38 qemu_serial.connect('qemuserial.sock')
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
39
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
40 time.sleep(0.5)
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
41
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
42 qemu_serial.settimeout(0.2)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
43
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
44 # Receive all data:
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
45 data = bytearray()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
46 for i in range(40):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
47 try:
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
48 data += qemu_serial.recv(1)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
49 except socket.timeout as e:
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
50 break
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
51 data = data.decode('ascii')
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
52 # print(data)
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
53
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
54 # Send quit command:
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
55 qemu_control.send("quit\n".encode('ascii'))
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents: 339
diff changeset
56 p.wait(timeout=3)
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
57 qemu_control.close()
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
58 qemu_serial.close()
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
59
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
60 # Check that output was correct:
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
61 return data
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
62
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
63 def testM3Bare(self):
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
64 """ Build bare m3 binary and emulate it """
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
65 recipe = os.path.join(testdir, 'm3_bare', 'recipe.yaml')
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
66 self.buildRecipe(recipe)
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
67 data = self.runQemu('m3_bare/bare.bin')
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
68 self.assertEqual('Hello worle', data)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
69
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
70 def testA9Bare(self):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
71 """ Build vexpress cortex-A9 binary and emulate it """
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
72 recipe = os.path.join(testdir, '..', 'examples', 'qemu_a9_hello', 'recipe.yaml')
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
73 self.buildRecipe(recipe)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
74 data = self.runQemu('../examples/qemu_a9_hello/hello.bin', machine='vexpress-a9')
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
75 self.assertEqual('Hello worle', data)
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
76
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
77
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
78 if __name__ == '__main__':
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
79 unittest.main()