@@ -75,6 +75,75 @@ def test_exec_command_streaming(self):
75
75
res += chunk
76
76
assert res == b'hello\n world\n '
77
77
78
+ def test_exec_command_demux (self ):
79
+ container = self .client .create_container (
80
+ BUSYBOX , 'cat' , detach = True , stdin_open = True )
81
+ id = container ['Id' ]
82
+ self .client .start (id )
83
+ self .tmp_containers .append (id )
84
+
85
+ script = ' ; ' .join ([
86
+ # Write something on stdout
87
+ 'echo hello out' ,
88
+ # Busybox's sleep does not handle sub-second times.
89
+ # This loops takes ~0.3 second to execute on my machine.
90
+ 'for i in $(seq 1 50000); do echo $i>/dev/null; done' ,
91
+ # Write something on stderr
92
+ 'echo hello err >&2' ])
93
+ cmd = 'sh -c "{}"' .format (script )
94
+
95
+ # tty=False, stream=False, demux=False
96
+ res = self .client .exec_create (id , cmd )
97
+ exec_log = self .client .exec_start (res )
98
+ assert exec_log == b'hello out\n hello err\n '
99
+
100
+ # tty=False, stream=True, demux=False
101
+ res = self .client .exec_create (id , cmd )
102
+ exec_log = self .client .exec_start (res , stream = True )
103
+ assert next (exec_log ) == b'hello out\n '
104
+ assert next (exec_log ) == b'hello err\n '
105
+ with self .assertRaises (StopIteration ):
106
+ next (exec_log )
107
+
108
+ # tty=False, stream=False, demux=True
109
+ res = self .client .exec_create (id , cmd )
110
+ exec_log = self .client .exec_start (res , demux = True )
111
+ assert exec_log == (b'hello out\n ' , b'hello err\n ' )
112
+
113
+ # tty=False, stream=True, demux=True
114
+ res = self .client .exec_create (id , cmd )
115
+ exec_log = self .client .exec_start (res , demux = True , stream = True )
116
+ assert next (exec_log ) == (b'hello out\n ' , None )
117
+ assert next (exec_log ) == (None , b'hello err\n ' )
118
+ with self .assertRaises (StopIteration ):
119
+ next (exec_log )
120
+
121
+ # tty=True, stream=False, demux=False
122
+ res = self .client .exec_create (id , cmd , tty = True )
123
+ exec_log = self .client .exec_start (res )
124
+ assert exec_log == b'hello out\r \n hello err\r \n '
125
+
126
+ # tty=True, stream=True, demux=False
127
+ res = self .client .exec_create (id , cmd , tty = True )
128
+ exec_log = self .client .exec_start (res , stream = True )
129
+ assert next (exec_log ) == b'hello out\r \n '
130
+ assert next (exec_log ) == b'hello err\r \n '
131
+ with self .assertRaises (StopIteration ):
132
+ next (exec_log )
133
+
134
+ # tty=True, stream=False, demux=True
135
+ res = self .client .exec_create (id , cmd , tty = True )
136
+ exec_log = self .client .exec_start (res , demux = True )
137
+ assert exec_log == (b'hello out\r \n hello err\r \n ' , b'' )
138
+
139
+ # tty=True, stream=True, demux=True
140
+ res = self .client .exec_create (id , cmd , tty = True )
141
+ exec_log = self .client .exec_start (res , demux = True , stream = True )
142
+ assert next (exec_log ) == (b'hello out\r \n ' , None )
143
+ assert next (exec_log ) == (b'hello err\r \n ' , None )
144
+ with self .assertRaises (StopIteration ):
145
+ next (exec_log )
146
+
78
147
def test_exec_start_socket (self ):
79
148
container = self .client .create_container (BUSYBOX , 'cat' ,
80
149
detach = True , stdin_open = True )
0 commit comments