def test_exec_start_socket(self): if not helpers.exec_driver_is_native(): pytest.skip('Exec driver not native') container = self.client.create_container(BUSYBOX, 'cat', detach=True, stdin_open=True) container_id = container['Id'] self.client.start(container_id) self.tmp_containers.append(container_id) line = 'yay, interactive exec!' # `echo` appends CRLF, `printf` doesn't exec_id = self.client.exec_create(container_id, ['printf', line], tty=True) self.assertIn('Id', exec_id) socket = self.client.exec_start(exec_id, socket=True) self.addCleanup(socket.close) next_size = next_frame_size(socket) self.assertEqual(next_size, len(line)) data = read_exactly(socket, next_size) self.assertEqual(data.decode('utf-8'), line)
def receiver(sock, stdout_stream, stderr_stream): from docker.utils.socket import next_frame_header from docker.utils.socket import read_exactly while True: stream, length = next_frame_header(sock) if stream == -1: break received = read_exactly(sock, length) if stream == 1: stdout_stream.write(received) else: INFO("stderr: %s" % received.decode('utf-8')) stderr_stream.write(received)
def test_run_container_reading_socket(self): line = 'hi there and stuff and things, words!' # `echo` appends CRLF, `printf` doesn't command = "printf '{0}'".format(line) container = self.client.create_container(BUSYBOX, command, detach=True, tty=False) ident = container['Id'] self.tmp_containers.append(ident) opts = {"stdout": 1, "stream": 1, "logs": 1} pty_stdout = self.client.attach_socket(ident, opts) self.addCleanup(pty_stdout.close) self.client.start(ident) next_size = next_frame_size(pty_stdout) self.assertEqual(next_size, len(line)) data = read_exactly(pty_stdout, next_size) self.assertEqual(data.decode('utf-8'), line)
def test_exec_start_socket(self): container = self.client.create_container(BUSYBOX, 'cat', detach=True, stdin_open=True) container_id = container['Id'] self.client.start(container_id) self.tmp_containers.append(container_id) line = 'yay, interactive exec!' # `echo` appends CRLF, `printf` doesn't exec_id = self.client.exec_create( container_id, ['printf', line], tty=True) self.assertIn('Id', exec_id) socket = self.client.exec_start(exec_id, socket=True) self.addCleanup(socket.close) next_size = next_frame_size(socket) self.assertEqual(next_size, len(line)) data = read_exactly(socket, next_size) self.assertEqual(data.decode('utf-8'), line)
def test_exec_start_socket(self): container = self.client.create_container(BUSYBOX, 'cat', detach=True, stdin_open=True) container_id = container['Id'] self.client.start(container_id) self.tmp_containers.append(container_id) line = 'yay, interactive exec!' # `echo` appends CRLF, `printf` doesn't exec_id = self.client.exec_create( container_id, ['printf', line], tty=True) assert 'Id' in exec_id socket = self.client.exec_start(exec_id, socket=True) self.addCleanup(socket.close) (stream, next_size) = next_frame_header(socket) assert stream == 1 # stdout (0 = stdin, 1 = stdout, 2 = stderr) assert next_size == len(line) data = read_exactly(socket, next_size) assert data.decode('utf-8') == line
def _read_header(socket): """ Reads the header from socket stream to determine the size of next frame to read. Header is 8 bytes long, where the first byte is the stream type and last four bytes (bigendian) is size of the payload header := [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4} Parameters ---------- socket Socket to read the responses from Returns ------- int Type of the frame int Size of the payload """ data = read_exactly(socket, 8) # >BxxxL is the struct notation to unpack data in correct header format in big-endian return struct.unpack('>BxxxL', data)
def _read_header(socket): data = read_exactly(socket, 8) return struct.unpack('>BxxxL', data)