def testunit_open_RAISES_OSError(self, mock_open): def side_effect(*unused_args): """Manages a two-step return value.""" # Hi! welcome to an old version of the mock module. In this old version, # side_effect behavior is managed manually. For this test the second call # to os.open should raise an OSError. So, on the first call we reset the # side_effect reference to 'second_call' which raises the exception we # need the second time os.open is called. # # More recent versions of mock allow side_effect to be an array, and # automatically raises objects of type Exception. # # If mock is updated, this will keep working, but a better approach is: # mock_open.side_effect = [3, OSError(-1, 'Forced OS error')] def second_call(*unused_args): """Raises OSError.""" raise OSError(-1, 'Forced OS error') mock_open.side_effect = second_call return 3 # Any integer like a file descriptor from os.open. mock_open.side_effect = side_effect (fifo_in, fifo_out) = mlab.get_vsys_fifo_names('mock_target') expected_calls = [ mock.call(fifo_in, os.O_WRONLY), mock.call(fifo_out, os.O_RDONLY) ] vsys = mlab.VsysFrontend('mock_target', open_nonblock=False) self.assertRaises(mlab.VsysOpenException, vsys.open) self.assertEqual(mock_open.mock_calls, expected_calls)
def __init__(self, target): """Initializes a new FakeVsysBackend. Example usage: backend = FakeVsysBackend('target') backend.start() Args: target: str, name of vsys target. Uses mlab.get_vsys_fifo_names to translate target name to actual FIFO path names. """ (self._fifo_in, self._fifo_out) = mlab.get_vsys_fifo_names(target) # Predefined request/response pairs with special meanings. self._responses = { 'shutdown_reader': 'whatever', 'shutdown_writer': 'whatever', 'send_empty_reply': '', 'take_too_long': 'whatever' } self._writer_closed = False self._reader_closed = False self._complete_during_shutdown = threading.Event() self._thread = threading.Thread(target=self._runbackend) # Daemon threads don't require a join to clean up thread after exit. self._thread.daemon = True
def __init__(self, target): """Initializes a new FakeVsysBackend. Example usage: backend = FakeVsysBackend('target') backend.start() Args: target: str, name of vsys target. Uses mlab.get_vsys_fifo_names to translate target name to actual FIFO path names. """ (self._fifo_in, self._fifo_out) = mlab.get_vsys_fifo_names(target) # Predefined request/response pairs with special meanings. self._responses = {'shutdown_reader': 'whatever', 'shutdown_writer': 'whatever', 'send_empty_reply': '', 'take_too_long': 'whatever'} self._writer_closed = False self._reader_closed = False self._complete_during_shutdown = threading.Event() self._thread = threading.Thread(target=self._runbackend) # Daemon threads don't require a join to clean up thread after exit. self._thread.daemon = True
def setUp(self): self._testdata_dir = os.path.join(os.path.dirname(mlab.__file__), 'testdata') mlab._VSYS_FMT_IN = os.path.join(self._testdata_dir, '%s.in') mlab._VSYS_FMT_OUT = os.path.join(self._testdata_dir, '%s.out') (fifo_in, fifo_out) = mlab.get_vsys_fifo_names('mock_target') if not os.path.exists(fifo_in): os.mkfifo(fifo_in) if not os.path.exists(fifo_out): os.mkfifo(fifo_out)
def setUp(self): self._testdata_dir = os.path.join( os.path.dirname(mlab.__file__), 'testdata') mlab._VSYS_FMT_IN = os.path.join(self._testdata_dir, '%s.in') mlab._VSYS_FMT_OUT = os.path.join(self._testdata_dir, '%s.out') (fifo_in, fifo_out) = mlab.get_vsys_fifo_names('mock_target') if not os.path.exists(fifo_in): os.mkfifo(fifo_in) if not os.path.exists(fifo_out): os.mkfifo(fifo_out)
def testunit_open_RAISES_OSError(self, mock_open): mock_open.side_effect = [3, OSError(-1, 'Forced OS error')] (fifo_in, fifo_out) = mlab.get_vsys_fifo_names('mock_target') expected_calls = [ mock.call(fifo_in, os.O_WRONLY), mock.call(fifo_out, os.O_RDONLY) ] vsys = mlab.VsysFrontend('mock_target', open_nonblock=False) self.assertRaises(mlab.VsysOpenException, vsys.open) self.assertEqual(mock_open.mock_calls, expected_calls)
def setUp(self): self._testdata_dir = os.path.join(os.path.dirname(mlab.__file__), 'testdata') mlab._VSYS_FMT_IN = os.path.join(self._testdata_dir, '%s.in') mlab._VSYS_FMT_OUT = os.path.join(self._testdata_dir, '%s.out') (fifo_in, fifo_out) = mlab.get_vsys_fifo_names('mock_target') if not os.path.exists(fifo_in): os.mkfifo(fifo_in) self.addCleanup(os.remove, fifo_in) if not os.path.exists(fifo_out): os.mkfifo(fifo_out) self.addCleanup(os.remove, fifo_out) self.backend = FakeVsysBackend('mock_target') self.backend.start()