forked from Kinetic/kinetic-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
276 lines (239 loc) · 9.21 KB
/
base.py
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
274
275
276
# Copyright 2013-2015 Seagate Technology LLC.
#
# This Source Code Form is subject to the terms of the Mozilla
# Public License, v. 2.0. If a copy of the MPL was not
# distributed with this file, You can obtain one at
# https://mozilla.org/MP:/2.0/.
#
# This program is distributed in the hope that it will be useful,
# but is provided AS-IS, WITHOUT ANY WARRANTY; including without
# the implied warranty of MERCHANTABILITY, NON-INFRINGEMENT or
# FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public
# License for more details.
#
# See www.openkinetic.org for more project information
#
#@author: Ignacio Corderi
import errno
import functools
import logging
import os
import subprocess
import shutil
import socket
import tempfile
import time
import unittest
from kinetic import Client
from kinetic import buildRange
KINETIC_JAR = os.environ.get('KINETIC_JAR')
KINETIC_PORT = os.environ.get('KINETIC_PORT', 9123)
KINETIC_HOST = os.environ.get('KINETIC_HOST', 'localhost')
class SimulatorRuntimeError(RuntimeError):
def __init__(self, stdout, stderr, returncode):
super(SimulatorRuntimeError, self).__init__(stdout, stderr, returncode)
# reopen file's in read mode
self.stdout = open(stdout.name).read()
self.stderr = open(stderr.name).read()
self.returncode = returncode
def __str__(self):
return '\n'.join([
'Simulator exited abnormally',
'STDOUT:\n%s' % self.stdout,
'STDERR:\n%s' % self.stderr,
'RETURNCODE: %s' % self.returncode
])
def _find_kinetic_jar(jar_path=None):
if jar_path:
jar_path = os.path.abspath(os.path.expanduser(
os.path.expandvars(jar_path)
))
else:
jar_path = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
# /kinetic-py/test/.
'../../kinetic-java/kinetic-simulator/target/',
'kinetic-simulator-0.6.0.3-SNAPSHOT-jar-with-dependencies.jar',
)
)
if not os.path.exists(jar_path):
if 'KINETIC_JAR' not in os.environ:
raise KeyError('KINETIC_JAR environment variable is not set')
else:
msg = "%s: '%s'" % (os.strerror(errno.ENOENT), jar_path)
raise IOError(errno.ENOENT, msg)
return jar_path
class BaseTestCase(unittest.TestCase):
"""
This is the BaseTestCase for running python test code against the
Kinetic simulator.
Each TestCase will independently verify a connection to a simulator
running on localhost at the port defined by the environment variable
KINETIC_PORT (default 9123) spawning an instance of the simulator if
necessary.
In the common case no simulator will currently be listening on port 9123
and unless your override KINETIC_PORT in your environment before
running tests - the TestCase will spawn an instance of the simulator using
the system java runtime by pointing it at the .jar defined by the
environment variable KINETIC_JAR. If KINETIC_JAR is not defined it
will go looking for it relative to this file.
If you want to connect to an instance of the simulator you already have
running (or a development instance that hasn't yet been packaged in a jar)
you can `set KINETIC_PORT=8123` (or wherever the server is).
If the .jar is not readily locatable you will get an error and need to
ensure that the KINETIC_JAR environment variable points to the real
path for kinetic-simulator.
"""
@classmethod
def _check_simulator(cls):
if not cls.simulator:
cls.datadir = tempfile.mkdtemp()
cls.stdout = open(os.path.join(cls.datadir, 'simulator.log'), 'w')
cls.stderr = open(os.path.join(cls.datadir, 'simulator.err'), 'w')
args = ['java', '-jar', cls.jar_path, '-port', str(cls.port), '-home', cls.datadir]
cls.simulator = subprocess.Popen(args, stdout=cls.stdout.fileno(),
stderr=cls.stderr.fileno())
if cls.simulator.poll():
raise SimulatorRuntimeError(cls.stdout, cls.stderr, cls.simulator.returncode)
@classmethod
def setUpClass(cls):
cls.client = None
cls.baseKey = "tests/py/%s/" % cls.__name__
cls.port = int(KINETIC_PORT)
cls.host = KINETIC_HOST
cls.jar_path = _find_kinetic_jar(KINETIC_JAR)
cls.datadir = None
cls.simulator = None
cls.stdout = cls.stderr = None
try:
backoff = 0.1
while True:
sock = socket.socket()
try:
sock.connect((cls.host, cls.port))
except socket.error:
if backoff > 2:
raise
else:
# k, we can connect
sock.close()
break
cls._check_simulator()
time.sleep(backoff)
backoff *= 2 # double it!
except:
if hasattr(cls, 'stdout'):
try:
raise SimulatorRuntimeError(cls.stdout, cls.stderr,
cls.simulator.returncode)
except:
# this is some dodgy shit to setup the re-raise at the bottom
pass
cls.tearDownClass()
raise
cls.client = Client(cls.host, cls.port)
cls.client.connect()
@classmethod
def tearDownClass(cls):
# remove all keys used by this test case
r = buildRange(cls.baseKey)
if cls.client:
xs = cls.client.getRange(r.startKey, r.endKey, r.startKeyInclusive, r.endKeyInclusive)
for x in xs:
cls.client.delete(x.key, x.metadata.version)
else:
print 'WARNING: no cls.client'
if cls.simulator:
if cls.simulator.poll() is None:
cls.simulator.terminate()
cls.simulator.wait()
[f.close() for f in (cls.stdout, cls.stderr) if f]
if cls.datadir:
shutil.rmtree(cls.datadir)
def tearDown(self):
r = buildRange(self.baseKey)
xs = self.client.getRange(r.startKey, r.endKey, r.startKeyInclusive, r.endKeyInclusive)
for x in xs:
self.client.delete(x.key, x.metadata.version)
def buildKey(self, n='test'):
# self.id returns the name of the running test
return self.baseKey + "%s/%s" % (self.id(), str(n))
@classmethod
def debug_logging(cls, f):
"""
Decorator, enables logging for the test
"""
@functools.wraps(f)
def wrapper(self, *args, **kwargs):
logging.basicConfig(level=logging.DEBUG)
logging.critical('DEBUG LOGGING FOR %s' % self.id())
try:
logging.disable(level=logging.NOTSET)
return f(self, *args, **kwargs)
finally:
logging.disable(level=logging.CRITICAL)
return wrapper
def start_simulators(jar_path, data_dir, *ports):
sim_map = {}
with open(os.devnull, 'w') as null:
for port in ports:
args = ['java', '-jar', jar_path, str(port),
os.path.join(data_dir, str(port)), str(port + 443)]
sim_map[port] = subprocess.Popen(args, stdout=null, stderr=null)
time.sleep(1)
connected = []
backoff = 0.1
timeout = time.time() + 3
while len(connected) < len(sim_map) and time.time() < timeout:
for port in sim_map:
if port in connected:
continue
sock = socket.socket()
try:
sock.connect(('localhost', port))
except socket.error:
time.sleep(backoff)
backoff *= 2
else:
connected.append(port)
sock.close()
if len(connected) < len(sim_map):
teardown_simulators(sim_map)
raise Exception('only able to connect to %r out of %r' % (connected,
sim_map))
return sim_map
def teardown_simulators(sim_map):
for proc in sim_map.values():
try:
proc.terminate()
except OSError, e:
if e.errno != errno.ESRCH:
raise
continue
proc.wait()
class MultiSimulatorTestCase(unittest.TestCase):
PORTS = (9010, 9020)
def setUp(self):
self.test_dir = tempfile.mkdtemp()
self.ports = self.PORTS
self._sim_map = {}
try:
self._sim_map = start_simulators(_find_kinetic_jar(KINETIC_JAR),
self.test_dir, *self.ports)
self.client_map = {}
for port in self.ports:
self.client_map[port] = Client('localhost', port)
self.client_map[port].connect()
except Exception:
self.tearDown()
def tearDown(self):
teardown_simulators(self._sim_map)
shutil.rmtree(self.test_dir)
def buildKey(self, key):
return "tests/py/%s.%s/%s" % (
self.__class__.__name__, self.id(), str(key))
if __name__ == "__main__":
# sanity
print _find_kinetic_jar(KINETIC_JAR)
print int(KINETIC_PORT)