diff test/testemulation.py @ 364:c49459768aaa

Work on globals
author Windel Bouwman
date Wed, 19 Mar 2014 20:24:03 +0100
parents 614a7f6d4d4d
children 39bf68bf1891
line wrap: on
line diff
--- a/test/testemulation.py	Sun Mar 16 11:28:47 2014 +0100
+++ b/test/testemulation.py	Wed Mar 19 20:24:03 2014 +0100
@@ -11,34 +11,45 @@
 testdir = os.path.dirname(os.path.abspath(__file__))
 
 def runQemu(kernel, machine='lm3s811evb'):
-    """ Runs qemu on a given kernel file on some machine """
+    """ Runs qemu on a given kernel file """
+    try:
+        os.remove('qemucontrol.sock')
+    except FileNotFoundError:
+        pass
+
+    try:
+        os.remove('qemuserial.sock')
+    except FileNotFoundError:
+        pass
+
+    # Listen to the control socket:
+    qemu_control = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+    qemu_control.bind('qemucontrol.sock')
+    qemu_control.listen(0)
+
+    # Listen to the serial output:
+    qemu_serial = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+    qemu_serial.bind('qemuserial.sock')
+    qemu_serial.listen(0)
+
     args = ['qemu-system-arm', '-M', machine, '-m', '16M',
         '-nographic', '-kernel', kernel, '-monitor',
-        'unix:qemucontrol.sock,server',
-        '-serial', 'unix:qemuserial.sock,server']
-    p = subprocess.Popen(args, stdout=subprocess.DEVNULL,
-        stderr=subprocess.DEVNULL)
+        'unix:qemucontrol.sock',
+        '-serial', 'unix:qemuserial.sock', '-S']
+    p = subprocess.Popen(args)
+        #stdout=subprocess.DEVNULL,
+        #stderr=subprocess.DEVNULL)
 
     # Give process some time to boot:
-    time.sleep(0.5)
-
-    # Connect to the control socket:
-    qemu_control = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-    qemu_control.connect('qemucontrol.sock')
-
-    time.sleep(0.5)
-
-    # Now connect to the serial output:
-    qemu_serial = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-    qemu_serial.connect('qemuserial.sock')
-
-    time.sleep(0.5)
+    qemu_serial, address_peer = qemu_serial.accept()
+    qemu_control, address_peer = qemu_control.accept()
+    qemu_control.send('cont\n'.encode('ascii'))
 
     qemu_serial.settimeout(0.2)
 
     # Receive all data:
     data = bytearray()
-    for i in range(40):
+    for i in range(400):
         try:
             data += qemu_serial.recv(1)
         except socket.timeout as e:
@@ -48,10 +59,24 @@
 
     # Send quit command:
     qemu_control.send("quit\n".encode('ascii'))
-    p.wait(timeout=3)
+    try:
+        p.wait(timeout=3)
+    except subprocess.TimeoutExpired:
+        p.kill()
+        print(p.communicate())
     qemu_control.close()
     qemu_serial.close()
 
+    try:
+        os.remove('qemucontrol.sock')
+    except FileNotFoundError:
+        pass
+
+    try:
+        os.remove('qemuserial.sock')
+    except FileNotFoundError:
+        pass
+
     # Check that output was correct:
     return data