File size: 11,802 Bytes
065fee7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
from docker.utils.proxy import ProxyConfig
from docker.utils.socket import next_frame_header, read_exactly
from ..helpers import (
assert_cat_socket_detached_with_keys,
ctrl_with,
requires_api_version,
)
from .base import TEST_IMG, BaseAPIIntegrationTest
class ExecTest(BaseAPIIntegrationTest):
def test_execute_command_with_proxy_env(self):
# Set a custom proxy config on the client
self.client._proxy_configs = ProxyConfig(
ftp='a', https='b', http='c', no_proxy='d'
)
container = self.client.create_container(
TEST_IMG, 'cat', detach=True, stdin_open=True,
)
self.client.start(container)
self.tmp_containers.append(container)
cmd = 'sh -c "env | grep -i proxy"'
# First, just make sure the environment variables from the custom
# config are set
res = self.client.exec_create(container, cmd=cmd)
output = self.client.exec_start(res).decode('utf-8').split('\n')
expected = [
'ftp_proxy=a', 'https_proxy=b', 'http_proxy=c', 'no_proxy=d',
'FTP_PROXY=a', 'HTTPS_PROXY=b', 'HTTP_PROXY=c', 'NO_PROXY=d'
]
for item in expected:
assert item in output
# Overwrite some variables with a custom environment
env = {'https_proxy': 'xxx', 'HTTPS_PROXY': 'XXX'}
res = self.client.exec_create(container, cmd=cmd, environment=env)
output = self.client.exec_start(res).decode('utf-8').split('\n')
expected = [
'ftp_proxy=a', 'https_proxy=xxx', 'http_proxy=c', 'no_proxy=d',
'FTP_PROXY=a', 'HTTPS_PROXY=XXX', 'HTTP_PROXY=c', 'NO_PROXY=d'
]
for item in expected:
assert item in output
def test_execute_command(self):
container = self.client.create_container(TEST_IMG, 'cat',
detach=True, stdin_open=True)
id = container['Id']
self.client.start(id)
self.tmp_containers.append(id)
res = self.client.exec_create(id, ['echo', 'hello'])
assert 'Id' in res
exec_log = self.client.exec_start(res)
assert exec_log == b'hello\n'
def test_exec_command_string(self):
container = self.client.create_container(TEST_IMG, 'cat',
detach=True, stdin_open=True)
id = container['Id']
self.client.start(id)
self.tmp_containers.append(id)
res = self.client.exec_create(id, 'echo hello world')
assert 'Id' in res
exec_log = self.client.exec_start(res)
assert exec_log == b'hello world\n'
def test_exec_command_as_user(self):
container = self.client.create_container(TEST_IMG, 'cat',
detach=True, stdin_open=True)
id = container['Id']
self.client.start(id)
self.tmp_containers.append(id)
res = self.client.exec_create(id, 'whoami', user='postgres')
assert 'Id' in res
exec_log = self.client.exec_start(res)
assert exec_log == b'postgres\n'
def test_exec_command_as_root(self):
container = self.client.create_container(TEST_IMG, 'cat',
detach=True, stdin_open=True)
id = container['Id']
self.client.start(id)
self.tmp_containers.append(id)
res = self.client.exec_create(id, 'whoami')
assert 'Id' in res
exec_log = self.client.exec_start(res)
assert exec_log == b'root\n'
def test_exec_command_streaming(self):
container = self.client.create_container(TEST_IMG, 'cat',
detach=True, stdin_open=True)
id = container['Id']
self.tmp_containers.append(id)
self.client.start(id)
exec_id = self.client.exec_create(id, ['echo', 'hello\nworld'])
assert 'Id' in exec_id
res = b''
for chunk in self.client.exec_start(exec_id, stream=True):
res += chunk
assert res == b'hello\nworld\n'
def test_exec_start_socket(self):
container = self.client.create_container(TEST_IMG, '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 test_exec_start_detached(self):
container = self.client.create_container(TEST_IMG, 'cat',
detach=True, stdin_open=True)
container_id = container['Id']
self.client.start(container_id)
self.tmp_containers.append(container_id)
exec_id = self.client.exec_create(
container_id, ['printf', "asdqwe"])
assert 'Id' in exec_id
response = self.client.exec_start(exec_id, detach=True)
assert response == ""
def test_exec_inspect(self):
container = self.client.create_container(TEST_IMG, 'cat',
detach=True, stdin_open=True)
id = container['Id']
self.client.start(id)
self.tmp_containers.append(id)
exec_id = self.client.exec_create(id, ['mkdir', '/does/not/exist'])
assert 'Id' in exec_id
self.client.exec_start(exec_id)
exec_info = self.client.exec_inspect(exec_id)
assert 'ExitCode' in exec_info
assert exec_info['ExitCode'] != 0
@requires_api_version('1.25')
def test_exec_command_with_env(self):
container = self.client.create_container(TEST_IMG, 'cat',
detach=True, stdin_open=True)
id = container['Id']
self.client.start(id)
self.tmp_containers.append(id)
res = self.client.exec_create(id, 'env', environment=["X=Y"])
assert 'Id' in res
exec_log = self.client.exec_start(res)
assert b'X=Y\n' in exec_log
@requires_api_version('1.35')
def test_exec_command_with_workdir(self):
container = self.client.create_container(
TEST_IMG, 'cat', detach=True, stdin_open=True
)
self.tmp_containers.append(container)
self.client.start(container)
res = self.client.exec_create(container, 'pwd', workdir='/var/opt')
exec_log = self.client.exec_start(res)
assert exec_log == b'/var/opt\n'
def test_detach_with_default(self):
container = self.client.create_container(
TEST_IMG, 'cat', detach=True, stdin_open=True
)
id = container['Id']
self.client.start(id)
self.tmp_containers.append(id)
exec_id = self.client.exec_create(
id, 'cat', stdin=True, tty=True, stdout=True
)
sock = self.client.exec_start(exec_id, tty=True, socket=True)
self.addCleanup(sock.close)
assert_cat_socket_detached_with_keys(
sock, [ctrl_with('p'), ctrl_with('q')]
)
def test_detach_with_config_file(self):
self.client._general_configs['detachKeys'] = 'ctrl-p'
container = self.client.create_container(
TEST_IMG, 'cat', detach=True, stdin_open=True
)
id = container['Id']
self.client.start(id)
self.tmp_containers.append(id)
exec_id = self.client.exec_create(
id, 'cat', stdin=True, tty=True, stdout=True
)
sock = self.client.exec_start(exec_id, tty=True, socket=True)
self.addCleanup(sock.close)
assert_cat_socket_detached_with_keys(sock, [ctrl_with('p')])
class ExecDemuxTest(BaseAPIIntegrationTest):
cmd = 'sh -c "{}"'.format(' ; '.join([
# Write something on stdout
'echo hello out',
# Busybox's sleep does not handle sub-second times.
# This loops takes ~0.3 second to execute on my machine.
'sleep 0.5',
# Write something on stderr
'echo hello err >&2'])
)
def setUp(self):
super().setUp()
self.container = self.client.create_container(
TEST_IMG, 'cat', detach=True, stdin_open=True
)
self.client.start(self.container)
self.tmp_containers.append(self.container)
def test_exec_command_no_stream_no_demux(self):
# tty=False, stream=False, demux=False
res = self.client.exec_create(self.container, self.cmd)
exec_log = self.client.exec_start(res)
assert b'hello out\n' in exec_log
assert b'hello err\n' in exec_log
def test_exec_command_stream_no_demux(self):
# tty=False, stream=True, demux=False
res = self.client.exec_create(self.container, self.cmd)
exec_log = list(self.client.exec_start(res, stream=True))
assert len(exec_log) == 2
assert b'hello out\n' in exec_log
assert b'hello err\n' in exec_log
def test_exec_command_no_stream_demux(self):
# tty=False, stream=False, demux=True
res = self.client.exec_create(self.container, self.cmd)
exec_log = self.client.exec_start(res, demux=True)
assert exec_log == (b'hello out\n', b'hello err\n')
def test_exec_command_stream_demux(self):
# tty=False, stream=True, demux=True
res = self.client.exec_create(self.container, self.cmd)
exec_log = list(self.client.exec_start(res, demux=True, stream=True))
assert len(exec_log) == 2
assert (b'hello out\n', None) in exec_log
assert (None, b'hello err\n') in exec_log
def test_exec_command_tty_no_stream_no_demux(self):
# tty=True, stream=False, demux=False
res = self.client.exec_create(self.container, self.cmd, tty=True)
exec_log = self.client.exec_start(res)
assert exec_log == b'hello out\r\nhello err\r\n'
def test_exec_command_tty_stream_no_demux(self):
# tty=True, stream=True, demux=False
res = self.client.exec_create(self.container, self.cmd, tty=True)
exec_log = list(self.client.exec_start(res, stream=True))
assert b'hello out\r\n' in exec_log
if len(exec_log) == 2:
assert b'hello err\r\n' in exec_log
else:
assert len(exec_log) == 3
assert b'hello err' in exec_log
assert b'\r\n' in exec_log
def test_exec_command_tty_no_stream_demux(self):
# tty=True, stream=False, demux=True
res = self.client.exec_create(self.container, self.cmd, tty=True)
exec_log = self.client.exec_start(res, demux=True)
assert exec_log == (b'hello out\r\nhello err\r\n', None)
def test_exec_command_tty_stream_demux(self):
# tty=True, stream=True, demux=True
res = self.client.exec_create(self.container, self.cmd, tty=True)
exec_log = list(self.client.exec_start(res, demux=True, stream=True))
assert (b'hello out\r\n', None) in exec_log
if len(exec_log) == 2:
assert (b'hello err\r\n', None) in exec_log
else:
assert len(exec_log) == 3
assert (b'hello err', None) in exec_log
assert (b'\r\n', None) in exec_log
|