annotate test/testemulation.py @ 391:a139da1f44f6

Merge
author Windel Bouwman
date Fri, 16 May 2014 12:30:10 +0200
parents 94f5b719ad0b
children
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
375
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
7 import shutil
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
8
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
9 from testzcc import ZccBaseTestCase
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
10
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
11 # Store testdir for safe switch back to directory:
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
12 testdir = os.path.dirname(os.path.abspath(__file__))
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
13
369
5333318ee33d Python 3.2 compatible issues
Windel Bouwman
parents: 366
diff changeset
14 def tryrm(fn):
5333318ee33d Python 3.2 compatible issues
Windel Bouwman
parents: 366
diff changeset
15 try:
5333318ee33d Python 3.2 compatible issues
Windel Bouwman
parents: 366
diff changeset
16 os.remove(fn)
5333318ee33d Python 3.2 compatible issues
Windel Bouwman
parents: 366
diff changeset
17 except OSError:
5333318ee33d Python 3.2 compatible issues
Windel Bouwman
parents: 366
diff changeset
18 pass
5333318ee33d Python 3.2 compatible issues
Windel Bouwman
parents: 366
diff changeset
19
375
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
20 qemu_app = 'qemu-system-arm'
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
21
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
22 def has_qemu():
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
23 """ Determines if qemu is possible """
384
94f5b719ad0b Small refactor
Windel Bouwman
parents: 377
diff changeset
24 if not hasattr(shutil, 'which'):
94f5b719ad0b Small refactor
Windel Bouwman
parents: 377
diff changeset
25 return False
375
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
26 return bool(shutil.which(qemu_app))
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
27
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
28
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
29 def runQemu(kernel, machine='lm3s811evb'):
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
30 """ Runs qemu on a given kernel file """
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
31
375
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
32 if not has_qemu():
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
33 return ''
369
5333318ee33d Python 3.2 compatible issues
Windel Bouwman
parents: 366
diff changeset
34 tryrm('qemucontrol.sock')
5333318ee33d Python 3.2 compatible issues
Windel Bouwman
parents: 366
diff changeset
35 tryrm('qemuserial.sock')
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
36
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
37 # Listen to the control socket:
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
38 qemu_control_serve = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
39 qemu_control_serve.bind('qemucontrol.sock')
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
40 qemu_control_serve.listen(0)
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
41
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
42 # Listen to the serial output:
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
43 qemu_serial_serve = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
44 qemu_serial_serve.bind('qemuserial.sock')
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
45 qemu_serial_serve.listen(0)
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
46
375
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
47 args = [qemu_app, '-M', machine, '-m', '16M',
372
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 371
diff changeset
48 '-nographic',
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 371
diff changeset
49 '-kernel', kernel,
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 371
diff changeset
50 '-monitor', 'unix:qemucontrol.sock',
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 371
diff changeset
51 '-serial', 'unix:qemuserial.sock',
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 371
diff changeset
52 '-S']
370
f86e79246602 Remove null stdio
Windel Bouwman
parents: 369
diff changeset
53 p = subprocess.Popen(args)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
54
372
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 371
diff changeset
55 #qemu_serial Give process some time to boot:
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 371
diff changeset
56 qemu_serial_serve.settimeout(3)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 371
diff changeset
57 qemu_control_serve.settimeout(3)
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
58 qemu_serial, address_peer = qemu_serial_serve.accept()
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
59 qemu_control, address_peer = qemu_control_serve.accept()
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
60
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
61 # Give the go command:
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
62 qemu_control.send('cont\n'.encode('ascii'))
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
63
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
64 qemu_serial.settimeout(0.2)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
65
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
66 # Receive all data:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
67 data = bytearray()
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
68 for i in range(400):
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
69 try:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
70 data += qemu_serial.recv(1)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
71 except socket.timeout as e:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
72 break
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
73 data = data.decode('ascii')
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
74 # print(data)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
75
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
76 # Send quit command:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
77 qemu_control.send("quit\n".encode('ascii'))
371
9c95f93f5b7a Python 3.2 compatible issues
Windel Bouwman
parents: 370
diff changeset
78 if hasattr(subprocess, 'TimeoutExpired'):
9c95f93f5b7a Python 3.2 compatible issues
Windel Bouwman
parents: 370
diff changeset
79 try:
9c95f93f5b7a Python 3.2 compatible issues
Windel Bouwman
parents: 370
diff changeset
80 p.wait(timeout=3)
9c95f93f5b7a Python 3.2 compatible issues
Windel Bouwman
parents: 370
diff changeset
81 except subprocess.TimeoutExpired:
9c95f93f5b7a Python 3.2 compatible issues
Windel Bouwman
parents: 370
diff changeset
82 p.kill()
9c95f93f5b7a Python 3.2 compatible issues
Windel Bouwman
parents: 370
diff changeset
83 else:
9c95f93f5b7a Python 3.2 compatible issues
Windel Bouwman
parents: 370
diff changeset
84 time.sleep(2)
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
85 p.kill()
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
86 qemu_control.close()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
87 qemu_serial.close()
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
88 qemu_control_serve.close()
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
89 qemu_serial_serve.close()
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
90
369
5333318ee33d Python 3.2 compatible issues
Windel Bouwman
parents: 366
diff changeset
91 tryrm('qemucontrol.sock')
5333318ee33d Python 3.2 compatible issues
Windel Bouwman
parents: 366
diff changeset
92 tryrm('qemuserial.sock')
364
c49459768aaa Work on globals
Windel Bouwman
parents: 361
diff changeset
93
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
94 # Check that output was correct:
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
95 return data
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
96
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
97
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
98 class EmulationTestCase(ZccBaseTestCase):
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
99 """ Tests the compiler driver """
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
100 def setUp(self):
375
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
101 if not has_qemu():
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 372
diff changeset
102 self.skipTest('Not running Qemu test')
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
103
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
104 def testM3Bare(self):
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
105 """ Build bare m3 binary and emulate it """
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
106 recipe = os.path.join(testdir, 'm3_bare', 'build.xml')
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
107 self.buildRecipe(recipe)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
108 data = runQemu('m3_bare/bare.bin')
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
109 self.assertEqual('Hello worle', data)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
110
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
111 def testA9Bare(self):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
112 """ Build vexpress cortex-A9 binary and emulate it """
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
113 recipe = os.path.join(testdir, '..', 'examples', 'qemu_a9_hello',
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
114 'build.xml')
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
115 self.buildRecipe(recipe)
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
116 data = runQemu('../examples/qemu_a9_hello/hello.bin',
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
117 machine='vexpress-a9')
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 340
diff changeset
118 self.assertEqual('Hello worle', data)
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
119
352
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
120 def testKernelVexpressA9(self):
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
121 """ Build vexpress cortex-A9 binary and emulate it """
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
122 recipe = os.path.join(testdir, '..', 'kernel', 'build.xml')
352
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
123 self.buildRecipe(recipe)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents: 354
diff changeset
124 data = runQemu('../kernel/kernel_arm.bin', machine='vexpress-a9')
361
614a7f6d4d4d Fixed test
Windel Bouwman
parents: 356
diff changeset
125 self.assertEqual('Welcome to lcfos!', data[:17])
352
899ae3aea803 First kernel run for vexpressA9
Windel Bouwman
parents: 346
diff changeset
126
338
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
127
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
128 if __name__ == '__main__':
8eb4a6fe8fc8 Added testcase with emulator
Windel Bouwman
parents:
diff changeset
129 unittest.main()