Skip to content

Commit b99b4ea

Browse files
committed
Allow exception object to be passed to mock directly
1 parent 75fe346 commit b99b4ea

File tree

4 files changed

+48
-48
lines changed

4 files changed

+48
-48
lines changed

supervisor/tests/base.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
import mock
2424

2525
class DummyOptions:
26-
27-
make_pipes_error = None
28-
fork_error = None
29-
execv_error = None
30-
kill_error = None
31-
minfds = 5
3226
loglevel = 20
27+
minfds = 5
28+
29+
chdir_exception = None
30+
fork_exception = None
31+
execv_exception = None
32+
kill_exception = None
33+
make_pipes_exception = None
34+
remove_exception = None
35+
write_exception = None
3336

3437
def __init__(self):
3538
self.identifier = 'supervisor'
@@ -76,9 +79,7 @@ def __init__(self):
7679
self.logs_reopened = False
7780
self.environment_processed = False
7881
self.write_accept = None
79-
self.write_error = None
8082
self.tempfile_name = '/foo/bar'
81-
self.remove_error = None
8283
self.removed = []
8384
self.existing = []
8485
self.openreturn = None
@@ -88,7 +89,6 @@ def __init__(self):
8889
self.parse_infos = []
8990
self.serverurl = 'http://localhost:9001'
9091
self.changed_directory = False
91-
self.chdir_error = None
9292
self.umaskset = None
9393
self.poller = DummyPoller(self)
9494
self.silent = False
@@ -151,8 +151,8 @@ def waitpid(self):
151151
return self.waitpid_return
152152

153153
def kill(self, pid, sig):
154-
if self.kill_error:
155-
raise OSError(self.kill_error)
154+
if self.kill_exception is not None:
155+
raise self.kill_exception
156156
self.kills[pid] = sig
157157

158158
def stat(self, filename):
@@ -172,8 +172,8 @@ def check_execv_args(self, filename, argv, st):
172172
raise NotFound('bad filename')
173173

174174
def make_pipes(self, stderr=True):
175-
if self.make_pipes_error:
176-
raise OSError(self.make_pipes_error)
175+
if self.make_pipes_exception is not None:
176+
raise self.make_pipes_exception
177177
pipes = {'child_stdin': 3, 'stdin': 4, 'stdout': 5, 'child_stdout': 6}
178178
if stderr:
179179
pipes['stderr'], pipes['child_stderr'] = (7, 8)
@@ -182,8 +182,8 @@ def make_pipes(self, stderr=True):
182182
return pipes
183183

184184
def write(self, fd, chars):
185-
if self.write_error:
186-
raise OSError(self.write_error)
185+
if self.write_exception is not None:
186+
raise self.write_exception
187187
if self.write_accept:
188188
chars = chars[self.write_accept]
189189
data = self.written.setdefault(fd, '')
@@ -192,8 +192,8 @@ def write(self, fd, chars):
192192
return len(chars)
193193

194194
def fork(self):
195-
if self.fork_error:
196-
raise OSError(self.fork_error)
195+
if self.fork_exception is not None:
196+
raise self.fork_exception
197197
return self.forkpid
198198

199199
def close_fd(self, fd):
@@ -216,11 +216,8 @@ def _exit(self, code):
216216

217217
def execve(self, filename, argv, environment):
218218
self.execve_called = True
219-
if self.execv_error:
220-
if self.execv_error == errno.EPERM:
221-
raise OSError(self.execv_error)
222-
else:
223-
raise RuntimeError(self.execv_error)
219+
if self.execv_exception is not None:
220+
raise self.execv_exception
224221
self.execv_args = (filename, argv)
225222
self.execv_environment = environment
226223

@@ -243,8 +240,8 @@ def mktempfile(self, prefix, suffix, dir):
243240

244241
def remove(self, path):
245242
import os
246-
if self.remove_error:
247-
raise os.error(self.remove_error)
243+
if self.remove_exception is not None:
244+
raise self.remove_exception
248245
self.removed.append(path)
249246

250247
def exists(self, path):
@@ -258,8 +255,8 @@ def open(self, name, mode='r'):
258255
return open(name, mode)
259256

260257
def chdir(self, dir):
261-
if self.chdir_error:
262-
raise OSError(self.chdir_error)
258+
if self.chdir_exception is not None:
259+
raise self.chdir_exception
263260
self.changed_directory = True
264261

265262
def setumask(self, mask):
@@ -378,6 +375,8 @@ def get_socket(self):
378375

379376
@functools.total_ordering
380377
class DummyProcess(object):
378+
write_exception = None
379+
381380
# Initial state; overridden by instance variables
382381
pid = 0 # Subprocess pid; 0 when not running
383382
laststart = 0 # Last time the subprocess was started; 0 if never
@@ -430,7 +429,6 @@ def __init__(self, config, state=None):
430429
self.input_fd_drained = None
431430
self.output_fd_drained = None
432431
self.transitioned = False
433-
self.write_error = None
434432

435433
def reopenlogs(self):
436434
self.logs_reopened = True
@@ -499,8 +497,8 @@ def drain_input_fd(self, fd):
499497
self.input_fd_drained = fd
500498

501499
def write(self, chars):
502-
if self.write_error:
503-
raise OSError(self.write_error)
500+
if self.write_exception is not None:
501+
raise self.write_exception
504502
self.stdin_buffer += chars
505503

506504
def transition(self):
@@ -1106,14 +1104,16 @@ def reap(self):
11061104
self.reaped = True
11071105

11081106
class DummyDispatcher:
1107+
flush_exception = None
1108+
11091109
write_event_handled = False
11101110
read_event_handled = False
11111111
error_handled = False
11121112
logs_reopened = False
11131113
logs_removed = False
11141114
closed = False
1115-
flush_error = None
11161115
flushed = False
1116+
11171117
def __init__(self, readable=False, writable=False, error=False):
11181118
self._readable = readable
11191119
self._writable = writable
@@ -1145,8 +1145,8 @@ def handle_error(self):
11451145
def close(self):
11461146
self.closed = True
11471147
def flush(self):
1148-
if self.flush_error:
1149-
raise OSError(self.flush_error)
1148+
if self.flush_exception:
1149+
raise self.flush_exception
11501150
self.flushed = True
11511151

11521152
class DummyStream:

supervisor/tests/test_dispatchers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def test_handle_write_event_epipe_raised(self):
639639
dispatcher = self._makeOne(process)
640640
dispatcher.input_buffer = 'halloooo'
641641
import errno
642-
options.write_error = errno.EPIPE
642+
options.write_exception = OSError(errno.EPIPE)
643643
dispatcher.handle_write_event()
644644
self.assertEqual(dispatcher.input_buffer, b'')
645645
self.assertTrue(options.logger.data[0].startswith(
@@ -653,7 +653,7 @@ def test_handle_write_event_uncaught_raised(self):
653653
dispatcher = self._makeOne(process)
654654
dispatcher.input_buffer = 'halloooo'
655655
import errno
656-
options.write_error = errno.EBADF
656+
options.write_exception = OSError(errno.EBADF)
657657
self.assertRaises(OSError, dispatcher.handle_write_event)
658658

659659
def test_handle_write_event_over_os_limit(self):

supervisor/tests/test_process.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def test_spawn_fail_check_execv_args(self):
225225

226226
def test_spawn_fail_make_pipes_emfile(self):
227227
options = DummyOptions()
228-
options.make_pipes_error = errno.EMFILE
228+
options.make_pipes_exception = OSError(errno.EMFILE)
229229
config = DummyPConfig(options, 'good', '/good/filename')
230230
instance = self._makeOne(config)
231231
from supervisor.states import ProcessStates
@@ -250,7 +250,7 @@ def test_spawn_fail_make_pipes_emfile(self):
250250

251251
def test_spawn_fail_make_pipes_other(self):
252252
options = DummyOptions()
253-
options.make_pipes_error = errno.EPERM
253+
options.make_pipes_exception = OSError(errno.EPERM)
254254
config = DummyPConfig(options, 'good', '/good/filename')
255255
instance = self._makeOne(config)
256256
from supervisor.states import ProcessStates
@@ -301,7 +301,7 @@ def raise_eisdir(envelope):
301301

302302
def test_spawn_fork_fail_eagain(self):
303303
options = DummyOptions()
304-
options.fork_error = errno.EAGAIN
304+
options.fork_exception = OSError(errno.EAGAIN)
305305
config = DummyPConfig(options, 'good', '/good/filename')
306306
instance = self._makeOne(config)
307307
from supervisor.states import ProcessStates
@@ -327,7 +327,7 @@ def test_spawn_fork_fail_eagain(self):
327327

328328
def test_spawn_fork_fail_other(self):
329329
options = DummyOptions()
330-
options.fork_error = errno.EPERM
330+
options.fork_exception = OSError(errno.EPERM)
331331
config = DummyPConfig(options, 'good', '/good/filename')
332332
instance = self._makeOne(config)
333333
from supervisor.states import ProcessStates
@@ -436,7 +436,7 @@ def test_spawn_as_child_sets_umask(self):
436436
def test_spawn_as_child_cwd_fail(self):
437437
options = DummyOptions()
438438
options.forkpid = 0
439-
options.chdir_error = errno.ENOENT
439+
options.chdir_exception = OSError(errno.ENOENT)
440440
config = DummyPConfig(options, 'good', '/good/filename',
441441
directory='/tmp')
442442
instance = self._makeOne(config)
@@ -458,7 +458,7 @@ def test_spawn_as_child_cwd_fail(self):
458458
def test_spawn_as_child_execv_fail_oserror(self):
459459
options = DummyOptions()
460460
options.forkpid = 0
461-
options.execv_error = errno.EPERM
461+
options.execv_exception = OSError(errno.EPERM)
462462
config = DummyPConfig(options, 'good', '/good/filename')
463463
instance = self._makeOne(config)
464464
result = instance.spawn()
@@ -477,7 +477,7 @@ def test_spawn_as_child_execv_fail_oserror(self):
477477
def test_spawn_as_child_execv_fail_runtime_error(self):
478478
options = DummyOptions()
479479
options.forkpid = 0
480-
options.execv_error = errno.ENOENT
480+
options.execv_exception = RuntimeError(errno.ENOENT)
481481
config = DummyPConfig(options, 'good', '/good/filename')
482482
instance = self._makeOne(config)
483483
result = instance.spawn()
@@ -641,7 +641,7 @@ def test_write_dispatcher_flush_raises_epipe(self):
641641
options.forkpid = 1
642642
instance.spawn()
643643
stdin_fd = instance.pipes['stdin']
644-
instance.dispatchers[stdin_fd].flush_error = errno.EPIPE
644+
instance.dispatchers[stdin_fd].flush_exception = OSError(errno.EPIPE)
645645
self.assertRaises(OSError, instance.write, sent)
646646

647647
def _dont_test_spawn_and_kill(self):
@@ -815,7 +815,7 @@ def test_kill_nopid(self):
815815
def test_kill_error(self):
816816
options = DummyOptions()
817817
config = DummyPConfig(options, 'test', '/test')
818-
options.kill_error = errno.EPERM
818+
options.kill_exception = OSError(errno.EPERM)
819819
instance = self._makeOne(config)
820820
L = []
821821
from supervisor.states import ProcessStates
@@ -1000,7 +1000,7 @@ def kill(pid, sig):
10001000
def test_signal_error(self):
10011001
options = DummyOptions()
10021002
config = DummyPConfig(options, 'test', '/test')
1003-
options.kill_error = errno.EPERM
1003+
options.kill_exception = OSError(errno.EPERM)
10041004
instance = self._makeOne(config)
10051005
L = []
10061006
from supervisor.states import ProcessStates
@@ -2085,7 +2085,7 @@ def test_dispatch_pipe_error(self):
20852085
gconfig = DummyPGroupConfig(options, pconfigs=[pconfig1])
20862086
pool = self._makeOne(gconfig)
20872087
process1 = pool.processes['process1']
2088-
process1.write_error = errno.EPIPE
2088+
process1.write_exception = OSError(errno.EPIPE)
20892089
process1.listener_state = EventListenerStates.READY
20902090
event = DummyEvent()
20912091
pool._acceptEvent(event)

supervisor/tests/test_rpcinterfaces.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def test_clearLog_unremoveable(self):
187187
from supervisor import xmlrpc
188188
options = DummyOptions()
189189
options.existing = [options.logfile]
190-
options.remove_error = 1
190+
options.remove_exception = os.error(errno.EPERM)
191191
supervisord = DummySupervisor(options)
192192
interface = self._makeOne(supervisord)
193193
self.assertRaises(xmlrpc.RPCError, interface.clearLog)
@@ -1999,7 +1999,7 @@ def test_sendProcessStdin_raises_no_file_when_write_raises_epipe(self):
19991999
supervisord = PopulatedDummySupervisor(options, 'process1', pconfig1)
20002000
supervisord.set_procattr('process1', 'pid', 42)
20012001
supervisord.set_procattr('process1', 'killing', False)
2002-
supervisord.set_procattr('process1', 'write_error', errno.EPIPE)
2002+
supervisord.set_procattr('process1', 'write_exception', OSError(errno.EPIPE))
20032003
interface = self._makeOne(supervisord)
20042004
from supervisor import xmlrpc
20052005
self._assertRPCError(xmlrpc.Faults.NO_FILE,
@@ -2012,7 +2012,7 @@ def test_sendProcessStdin_reraises_other_oserrors(self):
20122012
supervisord = PopulatedDummySupervisor(options, 'process1', pconfig1)
20132013
supervisord.set_procattr('process1', 'pid', 42)
20142014
supervisord.set_procattr('process1', 'killing', False)
2015-
supervisord.set_procattr('process1', 'write_error', errno.EINTR)
2015+
supervisord.set_procattr('process1', 'write_exception', OSError(errno.EINTR))
20162016
interface = self._makeOne(supervisord)
20172017
self.assertRaises(OSError,
20182018
interface.sendProcessStdin,

0 commit comments

Comments
 (0)