summaryrefslogtreecommitdiff
path: root/tests/unit/api_container_test.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/api_container_test.py')
-rw-r--r--tests/unit/api_container_test.py196
1 files changed, 140 insertions, 56 deletions
diff --git a/tests/unit/api_container_test.py b/tests/unit/api_container_test.py
index a7e183c..3a2fbde 100644
--- a/tests/unit/api_container_test.py
+++ b/tests/unit/api_container_test.py
@@ -1,25 +1,19 @@
-# -*- coding: utf-8 -*-
-
import datetime
import json
import signal
import docker
+from docker.api import APIClient
+from unittest import mock
import pytest
-import six
from . import fake_api
from ..helpers import requires_api_version
from .api_test import (
BaseAPIClientTest, url_prefix, fake_request, DEFAULT_TIMEOUT_SECONDS,
- fake_inspect_container
+ fake_inspect_container, url_base
)
-try:
- from unittest import mock
-except ImportError:
- import mock
-
def fake_inspect_container_tty(self, container):
return fake_inspect_container(self, container, tty=True)
@@ -30,7 +24,8 @@ class StartContainerTest(BaseAPIClientTest):
self.client.start(fake_api.FAKE_CONTAINER_ID)
args = fake_request.call_args
- assert args[0][1] == url_prefix + 'containers/3cc2351ab11b/start'
+ assert args[0][1] == (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/start')
assert 'data' not in args[1]
assert args[1]['timeout'] == DEFAULT_TIMEOUT_SECONDS
@@ -123,7 +118,8 @@ class StartContainerTest(BaseAPIClientTest):
self.client.start({'Id': fake_api.FAKE_CONTAINER_ID})
args = fake_request.call_args
- assert args[0][1] == url_prefix + 'containers/3cc2351ab11b/start'
+ assert args[0][1] == (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/start')
assert 'data' not in args[1]
assert args[1]['timeout'] == DEFAULT_TIMEOUT_SECONDS
@@ -352,6 +348,22 @@ class CreateContainerTest(BaseAPIClientTest):
assert args[1]['headers'] == {'Content-Type': 'application/json'}
assert args[1]['params'] == {'name': 'marisa-kirisame'}
+ def test_create_container_with_platform(self):
+ self.client.create_container('busybox', 'true',
+ platform='linux')
+
+ args = fake_request.call_args
+ assert args[0][1] == url_prefix + 'containers/create'
+ assert json.loads(args[1]['data']) == json.loads('''
+ {"Tty": false, "Image": "busybox", "Cmd": ["true"],
+ "AttachStdin": false,
+ "AttachStderr": true, "AttachStdout": true,
+ "StdinOnce": false,
+ "OpenStdin": false, "NetworkDisabled": false}
+ ''')
+ assert args[1]['headers'] == {'Content-Type': 'application/json'}
+ assert args[1]['params'] == {'name': None, 'platform': 'linux'}
+
def test_create_container_with_mem_limit_as_int(self):
self.client.create_container(
'busybox', 'true', host_config=self.client.create_host_config(
@@ -767,10 +779,71 @@ class CreateContainerTest(BaseAPIClientTest):
assert args[1]['headers'] == {'Content-Type': 'application/json'}
assert args[1]['timeout'] == DEFAULT_TIMEOUT_SECONDS
+ def test_create_container_with_device_requests(self):
+ client = APIClient(version='1.40')
+ fake_api.fake_responses.setdefault(
+ f'{fake_api.prefix}/v1.40/containers/create',
+ fake_api.post_fake_create_container,
+ )
+ client.create_container(
+ 'busybox', 'true', host_config=client.create_host_config(
+ device_requests=[
+ {
+ 'device_ids': [
+ '0',
+ 'GPU-3a23c669-1f69-c64e-cf85-44e9b07e7a2a'
+ ]
+ },
+ {
+ 'driver': 'nvidia',
+ 'Count': -1,
+ 'capabilities': [
+ ['gpu', 'utility']
+ ],
+ 'options': {
+ 'key': 'value'
+ }
+ }
+ ]
+ )
+ )
+
+ args = fake_request.call_args
+ assert args[0][1] == url_base + 'v1.40/' + 'containers/create'
+ expected_payload = self.base_create_payload()
+ expected_payload['HostConfig'] = client.create_host_config()
+ expected_payload['HostConfig']['DeviceRequests'] = [
+ {
+ 'Driver': '',
+ 'Count': 0,
+ 'DeviceIDs': [
+ '0',
+ 'GPU-3a23c669-1f69-c64e-cf85-44e9b07e7a2a'
+ ],
+ 'Capabilities': [],
+ 'Options': {}
+ },
+ {
+ 'Driver': 'nvidia',
+ 'Count': -1,
+ 'DeviceIDs': [],
+ 'Capabilities': [
+ ['gpu', 'utility']
+ ],
+ 'Options': {
+ 'key': 'value'
+ }
+ }
+ ]
+ assert json.loads(args[1]['data']) == expected_payload
+ assert args[1]['headers']['Content-Type'] == 'application/json'
+ assert set(args[1]['headers']) <= {'Content-Type', 'User-Agent'}
+ assert args[1]['timeout'] == DEFAULT_TIMEOUT_SECONDS
+
def test_create_container_with_labels_dict(self):
labels_dict = {
- six.text_type('foo'): six.text_type('1'),
- six.text_type('bar'): six.text_type('2'),
+ 'foo': '1',
+ 'bar': '2',
}
self.client.create_container(
@@ -786,12 +859,12 @@ class CreateContainerTest(BaseAPIClientTest):
def test_create_container_with_labels_list(self):
labels_list = [
- six.text_type('foo'),
- six.text_type('bar'),
+ 'foo',
+ 'bar',
]
labels_dict = {
- six.text_type('foo'): six.text_type(),
- six.text_type('bar'): six.text_type(),
+ 'foo': '',
+ 'bar': '',
}
self.client.create_container(
@@ -951,11 +1024,11 @@ class CreateContainerTest(BaseAPIClientTest):
def test_create_container_with_unicode_envvars(self):
envvars_dict = {
- 'foo': u'☃',
+ 'foo': '☃',
}
expected = [
- u'foo=☃'
+ 'foo=☃'
]
self.client.create_container(
@@ -1024,7 +1097,8 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/resize',
+ (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/resize'),
params={'h': 15, 'w': 120},
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1037,7 +1111,8 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/rename',
+ (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/rename'),
params={'name': 'foobar'},
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1047,7 +1122,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/wait',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/wait',
timeout=None,
params={}
)
@@ -1057,7 +1132,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/wait',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/wait',
timeout=None,
params={}
)
@@ -1069,14 +1144,14 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/logs',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 0, 'stderr': 1, 'stdout': 1,
'tail': 'all'},
timeout=DEFAULT_TIMEOUT_SECONDS,
stream=False
)
- assert logs == 'Flowering Nights\n(Sakuya Iyazoi)\n'.encode('ascii')
+ assert logs == b'Flowering Nights\n(Sakuya Iyazoi)\n'
def test_logs_with_dict_instead_of_id(self):
with mock.patch('docker.api.client.APIClient.inspect_container',
@@ -1085,14 +1160,14 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/logs',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 0, 'stderr': 1, 'stdout': 1,
'tail': 'all'},
timeout=DEFAULT_TIMEOUT_SECONDS,
stream=False
)
- assert logs == 'Flowering Nights\n(Sakuya Iyazoi)\n'.encode('ascii')
+ assert logs == b'Flowering Nights\n(Sakuya Iyazoi)\n'
def test_log_streaming(self):
with mock.patch('docker.api.client.APIClient.inspect_container',
@@ -1102,7 +1177,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/logs',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 0, 'stderr': 1, 'stdout': 1,
'tail': 'all'},
timeout=DEFAULT_TIMEOUT_SECONDS,
@@ -1117,7 +1192,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/logs',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 1, 'stderr': 1, 'stdout': 1,
'tail': 'all'},
timeout=DEFAULT_TIMEOUT_SECONDS,
@@ -1131,7 +1206,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/logs',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 1, 'stderr': 1, 'stdout': 1,
'tail': 'all'},
timeout=DEFAULT_TIMEOUT_SECONDS,
@@ -1146,7 +1221,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/logs',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 1, 'stderr': 1, 'stdout': 1,
'tail': 'all'},
timeout=DEFAULT_TIMEOUT_SECONDS,
@@ -1162,7 +1237,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/logs',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 0, 'stderr': 1, 'stdout': 1,
'tail': 10},
timeout=DEFAULT_TIMEOUT_SECONDS,
@@ -1178,7 +1253,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/logs',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 0, 'stderr': 1, 'stdout': 1,
'tail': 'all', 'since': ts},
timeout=DEFAULT_TIMEOUT_SECONDS,
@@ -1195,7 +1270,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/logs',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 0, 'stderr': 1, 'stdout': 1,
'tail': 'all', 'since': ts},
timeout=DEFAULT_TIMEOUT_SECONDS,
@@ -1221,7 +1296,7 @@ class ContainerTest(BaseAPIClientTest):
assert m.called
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/logs',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 1, 'stderr': 1, 'stdout': 1,
'tail': 'all'},
timeout=DEFAULT_TIMEOUT_SECONDS,
@@ -1233,7 +1308,8 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/changes',
+ (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/changes'),
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1242,7 +1318,8 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/changes',
+ (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/changes'),
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1251,7 +1328,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/json',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/json',
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1262,7 +1339,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/stop',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/stop',
params={'t': timeout},
timeout=(DEFAULT_TIMEOUT_SECONDS + timeout)
)
@@ -1275,7 +1352,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/stop',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/stop',
params={'t': timeout},
timeout=(DEFAULT_TIMEOUT_SECONDS + timeout)
)
@@ -1285,7 +1362,8 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/pause',
+ (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/pause'),
timeout=(DEFAULT_TIMEOUT_SECONDS)
)
@@ -1294,7 +1372,8 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/unpause',
+ (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/unpause'),
timeout=(DEFAULT_TIMEOUT_SECONDS)
)
@@ -1303,7 +1382,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/kill',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/kill',
params={},
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1313,7 +1392,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/kill',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/kill',
params={},
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1323,7 +1402,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/kill',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/kill',
params={'signal': signal.SIGTERM},
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1333,7 +1412,8 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/restart',
+ (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/restart'),
params={'t': 2},
timeout=(DEFAULT_TIMEOUT_SECONDS + 2)
)
@@ -1343,7 +1423,8 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'POST',
- url_prefix + 'containers/3cc2351ab11b/restart',
+ (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/restart'),
params={'t': 2},
timeout=(DEFAULT_TIMEOUT_SECONDS + 2)
)
@@ -1353,7 +1434,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'DELETE',
- url_prefix + 'containers/3cc2351ab11b',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID,
params={'v': False, 'link': False, 'force': False},
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1363,7 +1444,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'DELETE',
- url_prefix + 'containers/3cc2351ab11b',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID,
params={'v': False, 'link': False, 'force': False},
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1373,7 +1454,8 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/export',
+ (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/export'),
stream=True,
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1383,7 +1465,8 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/export',
+ (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/export'),
stream=True,
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1393,7 +1476,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/json',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/json',
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1409,7 +1492,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/stats',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/stats',
timeout=60,
stream=True
)
@@ -1419,7 +1502,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/top',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/top',
params={},
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1429,7 +1512,7 @@ class ContainerTest(BaseAPIClientTest):
fake_request.assert_called_with(
'GET',
- url_prefix + 'containers/3cc2351ab11b/top',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/top',
params={'ps_args': 'waux'},
timeout=DEFAULT_TIMEOUT_SECONDS
)
@@ -1441,7 +1524,8 @@ class ContainerTest(BaseAPIClientTest):
blkio_weight=345
)
args = fake_request.call_args
- assert args[0][1] == url_prefix + 'containers/3cc2351ab11b/update'
+ assert args[0][1] == (url_prefix + 'containers/' +
+ fake_api.FAKE_CONTAINER_ID + '/update')
assert json.loads(args[1]['data']) == {
'Memory': 2 * 1024, 'CpuShares': 124, 'BlkioWeight': 345
}