annotate test/testemulation.py @ 361:614a7f6d4d4d

Fixed test
author Windel Bouwman
date Fri, 14 Mar 2014 16:18:54 +0100
parents 52492b304adf
children c49459768aaa
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
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
13 def runQemu(kernel, machine='lm3s811evb'):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
14 """ Runs qemu on a given kernel file on some machine """
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
15 args = ['qemu-system-arm', '-M', machine, '-m', '16M',
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
16 '-nographic', '-kernel', kernel, '-monitor',
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
17 'unix:qemucontrol.sock,server',
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
18 '-serial', 'unix:qemuserial.sock,server']
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
19 p = subprocess.Popen(args, stdout=subprocess.DEVNULL,
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
20 stderr=subprocess.DEVNULL)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
21
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
22 # Give process some time to boot:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
23 time.sleep(0.5)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
24
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
25 # Connect to the control socket:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
26 qemu_control = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
27 qemu_control.connect('qemucontrol.sock')
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
28
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
29 time.sleep(0.5)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
30
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
31 # Now connect to the serial output:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
32 qemu_serial = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
33 qemu_serial.connect('qemuserial.sock')
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
34
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
35 time.sleep(0.5)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
36
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
37 qemu_serial.settimeout(0.2)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
38
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
39 # Receive all data:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
40 data = bytearray()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
41 for i in range(40):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
42 try:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
43 data += qemu_serial.recv(1)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
44 except socket.timeout as e:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
45 break
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
46 data = data.decode('ascii')
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
47 # print(data)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
48
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
49 # Send quit command:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
50 qemu_control.send("quit\n".encode('ascii'))
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
51 p.wait(timeout=3)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
52 qemu_control.close()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
53 qemu_serial.close()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
54
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
55 # Check that output was correct:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
56 return data
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
57
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
58
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
59 class EmulationTestCase(ZccBaseTestCase):
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
60 """ Tests the compiler driver """
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
61 def setUp(self):
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
62 if 'TESTEMU' not in os.environ:
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
63 self.skipTest('Not running emulation tests')
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
64
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
65 def testM3Bare(self):
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
66 """ Build bare m3 binary and emulate it """
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
67 recipe = os.path.join(testdir, 'm3_bare', 'recipe.yaml')
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
68 self.buildRecipe(recipe)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
69 data = runQemu('m3_bare/bare.bin')
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
70 self.assertEqual('Hello worle', data)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
71
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
72 def testA9Bare(self):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
73 """ Build vexpress cortex-A9 binary and emulate it """
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
74 recipe = os.path.join(testdir, '..', 'examples', 'qemu_a9_hello', 'recipe.yaml')
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
75 self.buildRecipe(recipe)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
76 data = runQemu('../examples/qemu_a9_hello/hello.bin', machine='vexpress-a9')
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
77 self.assertEqual('Hello worle', data)
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
78
352
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
79 def testKernelVexpressA9(self):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
80 """ Build vexpress cortex-A9 binary and emulate it """
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
81 recipe = os.path.join(testdir, '..', 'kernel', 'arm.yaml')
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
82 self.buildRecipe(recipe)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
83 data = runQemu('../kernel/kernel_arm.bin', machine='vexpress-a9')
361
614a7f6d4d4d Fixed test
Windel Bouwman
parents: 356
diff changeset
84 self.assertEqual('Welcome to lcfos!', data[:17])
352
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
85
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
86
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
87 if __name__ == '__main__':
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
88 unittest.main()