1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
import os
import re
import sys
import json
import time
import shutil
import socket
import tempfile
import unittest
import subprocess
class TestUnit(unittest.TestCase):
def setUp(self):
self.testdir = tempfile.mkdtemp(prefix='unit-test-')
os.mkdir(self.testdir + '/state')
pardir = os.path.abspath(os.path.join(os.path.dirname(__file__),
os.pardir))
print()
subprocess.call([pardir + '/build/unitd',
# TODO '--no-daemon',
'--modules', pardir + '/build',
'--state', self.testdir + '/state',
'--pid', self.testdir + '/unit.pid',
'--log', self.testdir + '/unit.log',
'--control', 'unix:' + self.testdir + '/control.unit.sock'])
if not self._waitforfiles(self.testdir + '/unit.pid',
self.testdir + '/unit.log', self.testdir + '/control.unit.sock'):
exit("Could not start unit")
# TODO dependency check
def tearDown(self):
with open(self.testdir + '/unit.pid', 'r') as f:
pid = f.read().rstrip()
subprocess.call(['kill', pid])
for i in range(50):
if not os.path.exists(self.testdir + '/unit.pid'):
break
time.sleep(0.1)
if os.path.exists(self.testdir + '/unit.pid'):
exit("Could not terminate unit")
if '--log' in sys.argv:
with open(self.testdir + '/unit.log', 'r') as f:
print(f.read())
if '--leave' not in sys.argv:
shutil.rmtree(self.testdir)
def _waitforfiles(self, *files):
for i in range(50):
wait = False
ret = 0
for f in files:
if not os.path.exists(f):
wait = True
break
if wait:
time.sleep(0.1)
else:
ret = 1
break
return ret
class TestUnitControl(TestUnit):
# TODO socket reuse
# TODO http client
def get(self, path='/'):
with self._control_sock() as sock:
req = ('GET ' + path
+ ' HTTP/1.1\r\nHost: localhost\r\n\r\n').encode()
sock.sendall(req)
if '--verbose' in sys.argv:
print('>>>\n', req)
resp = self._recvall(sock)
if '--verbose' in sys.argv:
print('<<<\n', resp)
return self._body_json(resp)
def delete(self, path='/'):
with self._control_sock() as sock:
req = ('DELETE ' + path
+ ' HTTP/1.1\r\nHost: localhost\r\n\r\n').encode()
sock.sendall(req)
if '--verbose' in sys.argv:
print('>>>\n', req)
resp = self._recvall(sock)
if '--verbose' in sys.argv:
print('<<<\n', resp)
return self._body_json(resp)
def put(self, path='/', data=''):
if isinstance(data, str):
data = data.encode()
with self._control_sock() as sock:
req = ('PUT ' + path + ' HTTP/1.1\nHost: localhost\n'
+ 'Content-Length: ' + str(len(data))
+ '\r\n\r\n').encode() + data
sock.sendall(req)
if '--verbose' in sys.argv:
print('>>>\n', req)
resp = self._recvall(sock)
if '--verbose' in sys.argv:
print('<<<\n', resp)
return self._body_json(resp)
def _control_sock(self):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(self.testdir + '/control.unit.sock')
return sock
def _recvall(self, sock, buff_size=4096):
data = ''
while True:
part = sock.recv(buff_size).decode()
data += part
if len(part) < buff_size:
break
return data
def _body_json(self, resp):
m = re.search('.*?\x0d\x0a?\x0d\x0a?(.*)', resp, re.M | re.S)
return json.loads(m.group(1))
|