def start(self, sockets=None, **kwargs): #pylint: disable=W0221 """ Present the PTY of the container inside the current process. This will take over the current process' TTY until the container's PTY is closed. """ pty_stdin, pty_stdout, pty_stderr = sockets or self.sockets() pumps = [] if pty_stdin and self.interactive: #pylint: disable=C0301 pumps.append( io.Pump(io.Stream(self.stdin), pty_stdin, wait_for_output=not sys.stdin.isatty())) if pty_stdout: pumps.append( io.Pump(pty_stdout, io.Stream(self.stdout), propagate_close=False)) if pty_stderr: pumps.append( io.Pump(pty_stderr, io.Stream(self.stderr), propagate_close=False)) if not self.info()['State']['Running']: self.client.start(self.container, **kwargs) #pylint: disable=W0613 return pumps
def start(self, sockets=None, **kwargs): #pylint: disable=W0221 """ start execution """ stream = sockets or self.sockets() pumps = [] if self.interactive: pumps.append( io.Pump(io.Stream(self.stdin), stream, wait_for_output=False)) pumps.append( io.Pump(stream, io.Stream(self.stdout), propagate_close=False)) return pumps
def test_init(self): """``dockerpty.io`` Pump requires two Streams for init""" fake_from_stream = MagicMock() fake_to_stream = MagicMock() pump = io.Pump(fake_from_stream, fake_to_stream) self.assertTrue(isinstance(pump, io.Pump))
def test_is_done(self): """``dockerpty.io`` Pump.is_done returns False if the to_stream.needs_write is True""" fake_from_stream = MagicMock() fake_to_stream = MagicMock() fake_to_stream.needs_write.return_value = True pump = io.Pump(fake_from_stream, fake_to_stream) self.assertFalse(pump.is_done())
def test_is_done_true(self): """``dockerpty.io`` Pump.is_done returns True if the to_stream.needs_write is False and the pump reaches EOF""" fake_from_stream = MagicMock() fake_to_stream = MagicMock() fake_to_stream.needs_write.return_value = False pump = io.Pump(fake_from_stream, fake_to_stream) pump.eof = True self.assertTrue(pump.is_done())
def test_set_blocking(self): """``dockerpty.io`` Pump.set_blocking adjusts the 'from_stream''""" fake_from_stream = MagicMock() fake_to_stream = MagicMock() pump = io.Pump(fake_from_stream, fake_to_stream) pump.set_blocking('some value') self.assertTrue(fake_from_stream.set_blocking.called)
def test_repr(self): """``dockerpty.io`` Pump has a handy repr""" fake_from_stream = FakeObj() fake_to_stream = FakeObj() pump = io.Pump(fake_from_stream, fake_to_stream) the_repr = '{}'.format(pump) expected = 'Pump(from=some object, to=some object)' self.assertEqual(the_repr, expected)
def test_flush_error(self): """``dockerpty.io`` Pump.flush raises errors""" error = OSError() error.errno = 8965 fake_from_stream = MagicMock() fake_from_stream.read.side_effect = [error] fake_to_stream = MagicMock() pump = io.Pump(fake_from_stream, fake_to_stream) with self.assertRaises(OSError): pump.flush()
def test_flush_eof(self): """``dockerpty.io`` Pump.flush returns None when the 'from_stream' reaches EOF""" fake_from_stream = MagicMock() fake_from_stream.read.return_value = b'' fake_to_stream = MagicMock() pump = io.Pump(fake_from_stream, fake_to_stream) written = pump.flush() self.assertTrue(written is None) self.assertTrue(pump.eof)
def test_fileno(self): """``dockerpty.io`` Pump.fileno returns the fileno of the 'from_stream'""" fake_from_stream = MagicMock() fake_from_stream.fileno.return_value = 9001 fake_to_stream = MagicMock() pump = io.Pump(fake_from_stream, fake_to_stream) fileno = pump.fileno() expected = 9001 self.assertEqual(fileno, expected)
def test_flush(self): """``dockerpty.io`` Pump.flush returns the number of bytes written into the 'to_stream'""" fake_from_stream = MagicMock() fake_from_stream.read.return_value = b'some bytes' fake_to_stream = MagicMock() fake_to_stream.write = lambda x: len(x) pump = io.Pump(fake_from_stream, fake_to_stream) written = pump.flush() expected = 10 self.assertEqual(written, expected)