def test_start(self, fake_setraw, fake_tcgetattr): """``dockerpty.tty`` Terminal 'start' sets the file descripter to a raw TTY""" fake_fd = MagicMock() terminal = dtty.Terminal(fake_fd) terminal.start() self.assertTrue(fake_setraw.called)
def test_init(self): """``dockerpty.tty`` Terminal object requires a file descriptor for init""" fake_fd = MagicMock() terminal = dtty.Terminal(fake_fd) self.assertTrue(isinstance(terminal, dtty.Terminal))
def test_stop(self, fake_tcsetattr): """``dockerpty.tty`` Terminal 'stop' resets the terminal attributes""" fake_fd = MagicMock() terminal = dtty.Terminal(fake_fd) terminal.original_attributes = 'wooter' terminal.stop() self.assertTrue(fake_tcsetattr.called)
def test_repr(self): """``dockerpty.tty`` Terminal has a handy repr""" fake_fd = FakeObj() terminal = dtty.Terminal(fake_fd) the_repr = '{}'.format(terminal) expected = 'Terminal(FakeObj, raw=True)' self.assertEqual(the_repr, expected)
def test_context_manager(self, fake_stop, fake_start): """``dockerpty.tty`` Terminal object supports the with-statement""" fake_fd = MagicMock() with dtty.Terminal(fake_fd): pass self.assertTrue(fake_start.called) self.assertTrue(fake_stop.called)
def _hijack_tty(self, pumps): with tty.Terminal(self.operation.stdin, raw=self.operation.israw()): self.resize() keep_running = True stdin_stream = self._get_stdin_pump(pumps) while keep_running: read_pumps = [p for p in pumps if not p.eof] write_streams = [ p.to_stream for p in pumps if p.to_stream.needs_write() ] read_ready, write_ready = io.select(read_pumps, write_streams, timeout=2) try: for write_stream in write_ready: write_stream.do_write() for pump in read_ready: pump.flush() if sys.stdin.isatty(): if all([p.is_done() for p in pumps]): keep_running = False elif stdin_stream.is_done(): # If stdin isn't a TTY, this is probably an SSH session. # The most common use case for an SSH session without a # TTY is SCP/SFTP; like, someone coping a file to a remote # server. Those file transfer clients mark the end of # the session by sending an empty packet, then waiting # for the TCP session to terminate, We need to break out # of this loop to return control to the calling application # so it can tear down the SCP/SFTP process running inside # the container. keep_running = False except SSLError as doh: if 'The operation did not complete' not in doh.strerror: raise doh else: if not self.operation.info()['State']['Running']: #pylint: disable=W0212 keep_running = False
def test_israw(self): """``dockerpty.tty`` Terminal 'israw' returns the 'raw' attribute""" fake_fd = MagicMock() terminal = dtty.Terminal(fake_fd) self.assertTrue(terminal.israw() is terminal.raw)