diff mbox

[RFC,v2,3/6] Add core python test framework

Message ID 1322765012-3164-4-git-send-email-aliguori@us.ibm.com
State New
Headers show

Commit Message

Anthony Liguori Dec. 1, 2011, 6:43 p.m. UTC
---
 qtest.py |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 69 insertions(+), 0 deletions(-)
 create mode 100644 qtest.py
diff mbox

Patch

diff --git a/qtest.py b/qtest.py
new file mode 100644
index 0000000..5ac2e9b
--- /dev/null
+++ b/qtest.py
@@ -0,0 +1,69 @@ 
+import socket
+
+q = None
+
+class QTest(object):
+    def __init__(self, path):
+        self._sock = socket.socket(socket.AF_UNIX)
+        self._sock.connect(path)
+        self.inbuf = ''
+        self.irqs = {}
+        for i in range(16):
+            self.irqs[i] = False
+
+    def _recv(self):
+        while self.inbuf.find('\n') == -1:
+            self.inbuf += self._sock.recv(1024)
+
+        rsp, self.inbuf = self.inbuf.split('\n', 1)
+        return rsp.split()
+
+    def _send(self, command, *args):
+        outbuf = ' '.join([command] + map(str, args))
+        self._sock.sendall(outbuf + '\n')
+
+    def _cmd(self, command, *args):
+        self._send(command, *args)
+        while True:
+            rsp = self._recv()
+            if rsp[0] in ['IRQ']:
+                num = int(rsp[2], 0)
+                if rsp[1] in ['raise']:
+                    self.irqs[num] = True
+                else:
+                    self.irqs[num] = False
+                continue
+            if rsp[0] != 'OK':
+                raise Exception('Bad response')
+            break
+        return rsp[1:]
+
+    def get_irq(self, num):
+        return self.irqs[num]
+
+# Helpers to add expected platform functions in the current namespace
+
+def init(path):
+    global q
+    q = QTest(path)
+
+def outb(addr, value):
+    q._cmd('outb', addr, value)
+
+def outw(addr, value):
+    q._cmd('outw', addr, value)
+
+def outl(addr, value):
+    q._cmd('outl', addr, value)
+
+def inb(addr):
+    return int(q._cmd('inb', addr)[0], 0)
+
+def inw(addr):
+    return int(q._cmd('inw', addr)[0], 0)
+
+def inl(addr):
+    return int(q._cmd('inl', addr)[0], 0)
+
+def get_irq(num):
+    return q.get_irq(num)