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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
# Copyright (c) 2013-2014 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import threading
import time
import uuid
from OpenSSL import crypto
import fixtures
import testscenarios
import testtools
import gear
from gear import tests
def iterate_timeout(max_seconds, purpose):
start = time.time()
count = 0
while (time.time() < start + max_seconds):
count += 1
yield count
time.sleep(0)
raise Exception("Timeout waiting for %s" % purpose)
class TestFunctional(tests.BaseTestCase):
scenarios = [
('no_ssl', dict(ssl=False)),
('ssl', dict(ssl=True)),
]
def setUp(self):
super(TestFunctional, self).setUp()
if self.ssl:
self.tmp_root = self.useFixture(fixtures.TempDir()).path
root_subject, root_key = self.create_cert('root')
self.create_cert('server', root_subject, root_key)
self.create_cert('client', root_subject, root_key)
self.create_cert('worker', root_subject, root_key)
self.server = gear.Server(
0,
os.path.join(self.tmp_root, 'server.key'),
os.path.join(self.tmp_root, 'server.crt'),
os.path.join(self.tmp_root, 'root.crt'))
self.client = gear.Client('client')
self.worker = gear.Worker('worker')
self.client.addServer('127.0.0.1', self.server.port,
os.path.join(self.tmp_root, 'client.key'),
os.path.join(self.tmp_root, 'client.crt'),
os.path.join(self.tmp_root, 'root.crt'))
self.worker.addServer('127.0.0.1', self.server.port,
os.path.join(self.tmp_root, 'worker.key'),
os.path.join(self.tmp_root, 'worker.crt'),
os.path.join(self.tmp_root, 'root.crt'))
else:
self.server = gear.Server(0)
self.client = gear.Client('client')
self.worker = gear.Worker('worker')
self.client.addServer('127.0.0.1', self.server.port)
self.worker.addServer('127.0.0.1', self.server.port)
self.client.waitForServer()
self.worker.waitForServer()
def create_cert(self, cn, issuer=None, signing_key=None):
key = crypto.PKey()
key.generate_key(crypto.TYPE_RSA, 1024)
cert = crypto.X509()
subject = cert.get_subject()
subject.C = "US"
subject.ST = "State"
subject.L = "Locality"
subject.O = "Org"
subject.OU = "Org Unit"
subject.CN = cn
cert.set_serial_number(1)
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(3600)
cert.set_pubkey(key)
if issuer:
cert.set_issuer(issuer)
else:
cert.set_issuer(subject)
if signing_key:
cert.sign(signing_key, 'sha1')
else:
cert.sign(key, 'sha1')
open(os.path.join(self.tmp_root, '%s.crt' % cn), 'w').write(
crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode('utf-8'))
open(os.path.join(self.tmp_root, '%s.key' % cn), 'w').write(
crypto.dump_privatekey(crypto.FILETYPE_PEM, key).decode('utf-8'))
return (subject, key)
def test_job(self):
self.worker.registerFunction('test')
for jobcount in range(2):
job = gear.Job(b'test', b'testdata')
self.client.submitJob(job)
self.assertNotEqual(job.handle, None)
workerjob = self.worker.getJob()
self.assertEqual(workerjob.handle, job.handle)
self.assertEqual(workerjob.arguments, b'testdata')
workerjob.sendWorkData(b'workdata')
workerjob.sendWorkComplete()
for count in iterate_timeout(30, "job completion"):
if job.complete:
break
self.assertTrue(job.complete)
self.assertEqual(job.data, [b'workdata'])
def test_bg_job(self):
self.worker.registerFunction('test')
job = gear.Job(b'test', b'testdata')
self.client.submitJob(job, background=True)
self.assertNotEqual(job.handle, None)
self.client.shutdown()
del self.client
workerjob = self.worker.getJob()
self.assertEqual(workerjob.handle, job.handle)
self.assertEqual(workerjob.arguments, b'testdata')
workerjob.sendWorkData(b'workdata')
workerjob.sendWorkComplete()
def test_worker_termination(self):
def getJob():
with testtools.ExpectedException(gear.InterruptedError):
self.worker.getJob()
self.worker.registerFunction('test')
jobthread = threading.Thread(target=getJob)
jobthread.daemon = True
jobthread.start()
self.worker.stopWaitingForJobs()
def test_text_job_name(self):
self.worker.registerFunction('test')
for jobcount in range(2):
job = gear.Job('test', b'testdata')
self.client.submitJob(job)
self.assertNotEqual(job.handle, None)
workerjob = self.worker.getJob()
self.assertEqual('test', workerjob.name)
class TestFunctionalText(tests.BaseTestCase):
def setUp(self):
super(TestFunctionalText, self).setUp()
self.server = gear.Server(0)
self.client = gear.Client('client')
self.worker = gear.TextWorker('worker')
self.client.addServer('127.0.0.1', self.server.port)
self.worker.addServer('127.0.0.1', self.server.port)
self.client.waitForServer()
self.worker.waitForServer()
def test_text_job(self):
self.worker.registerFunction('test')
for jobcount in range(2):
job = gear.TextJob('test', 'testdata')
self.client.submitJob(job)
self.assertNotEqual(job.handle, None)
workerjob = self.worker.getJob()
self.assertEqual(workerjob.handle, job.handle)
self.assertEqual(workerjob.arguments, 'testdata')
workerjob.sendWorkData('workdata')
workerjob.sendWorkComplete()
for count in iterate_timeout(30, "job completion"):
if job.complete:
break
self.assertTrue(job.complete)
self.assertEqual(job.data, ['workdata'])
def test_text_job_unique(self):
self.worker.registerFunction('test')
for jobcount in range(2):
jobunique = uuid.uuid4().hex
job = gear.TextJob('test', 'testdata', unique=jobunique)
self.client.submitJob(job)
self.assertNotEqual(job.handle, None)
workerjob = self.worker.getJob()
self.assertEqual(workerjob.handle, job.handle)
self.assertEqual(workerjob.arguments, 'testdata')
workerjob.sendWorkData('workdata')
workerjob.sendWorkComplete()
for count in iterate_timeout(30, "job completion"):
if job.complete:
break
self.assertTrue(job.complete)
self.assertEqual(job.data, ['workdata'])
self.assertEqual(job.unique, jobunique)
self.assertEqual(workerjob.unique, jobunique)
def test_text_job_exception(self):
self.worker.registerFunction('test')
for jobcount in range(2):
job = gear.TextJob('test', 'testdata')
self.client.submitJob(job)
self.assertNotEqual(job.handle, None)
workerjob = self.worker.getJob()
self.assertEqual(workerjob.handle, job.handle)
self.assertEqual(workerjob.arguments, 'testdata')
workerjob.sendWorkException('work failed')
for count in iterate_timeout(30, "job completion"):
if job.complete:
break
self.assertTrue(job.complete)
self.assertEqual(job.exception, 'work failed')
def test_grab_job_after_register(self):
jobunique = uuid.uuid4().hex
job = gear.TextJob('test', 'testdata', unique=jobunique)
self.client.submitJob(job)
self.assertNotEqual(job.handle, None)
def getJob():
workerjob = self.worker.getJob()
workerjob.sendWorkComplete()
jobthread = threading.Thread(target=getJob)
jobthread.daemon = True
jobthread.start()
for count in iterate_timeout(30, "worker sleeping"):
if self.worker.active_connections[0].state == 'SLEEP':
break
self.assertEqual(1, len(self.server.normal_queue))
self.assertFalse(job.complete)
# When we register the function, the worker should send a
# grab_job packet and pick up the job and it should complete.
self.worker.registerFunction('test')
for count in iterate_timeout(30, "job completion"):
if job.complete:
break
self.assertEqual(0, len(self.server.normal_queue))
def load_tests(loader, in_tests, pattern):
return testscenarios.load_tests_apply_scenarios(loader, in_tests, pattern)
|