Skip to content

Commit

Permalink
client: use 12 character short IDs (#2862)
Browse files Browse the repository at this point in the history
Use 12 characters for Docker resource IDs for
consistency with the Docker CLI.

Signed-off-by: Ben Fasoli <benfasoli@gmail.com>
  • Loading branch information
benfasoli authored Jul 29, 2022
1 parent ab43018 commit 23cf16f
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 81 deletions.
10 changes: 5 additions & 5 deletions docker/models/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def labels(self):
@property
def short_id(self):
"""
The ID of the image truncated to 10 characters, plus the ``sha256:``
The ID of the image truncated to 12 characters, plus the ``sha256:``
prefix.
"""
if self.id.startswith('sha256:'):
return self.id[:17]
return self.id[:10]
return self.id[:19]
return self.id[:12]

@property
def tags(self):
Expand Down Expand Up @@ -141,10 +141,10 @@ def id(self):
@property
def short_id(self):
"""
The ID of the image truncated to 10 characters, plus the ``sha256:``
The ID of the image truncated to 12 characters, plus the ``sha256:``
prefix.
"""
return self.id[:17]
return self.id[:19]

def pull(self, platform=None):
"""
Expand Down
4 changes: 2 additions & 2 deletions docker/models/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def id(self):
@property
def short_id(self):
"""
The ID of the object, truncated to 10 characters.
The ID of the object, truncated to 12 characters.
"""
return self.id[:10]
return self.id[:12]

def reload(self):
"""
Expand Down
87 changes: 50 additions & 37 deletions tests/unit/api_container_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def test_start_container(self):
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

Expand Down Expand Up @@ -117,7 +118,8 @@ def test_start_container_with_dict_instead_of_id(self):
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

Expand Down Expand Up @@ -1079,7 +1081,8 @@ def test_resize_container(self):

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
)
Expand All @@ -1092,7 +1095,8 @@ def test_rename_container(self):

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
)
Expand All @@ -1102,7 +1106,7 @@ def test_wait(self):

fake_request.assert_called_with(
'POST',
url_prefix + 'containers/3cc2351ab11b/wait',
url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/wait',
timeout=None,
params={}
)
Expand All @@ -1112,7 +1116,7 @@ def test_wait_with_dict_instead_of_id(self):

fake_request.assert_called_with(
'POST',
url_prefix + 'containers/3cc2351ab11b/wait',
url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/wait',
timeout=None,
params={}
)
Expand All @@ -1124,7 +1128,7 @@ def test_logs(self):

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,
Expand All @@ -1140,7 +1144,7 @@ def test_logs_with_dict_instead_of_id(self):

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,
Expand All @@ -1157,7 +1161,7 @@ def test_log_streaming(self):

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,
Expand All @@ -1172,7 +1176,7 @@ def test_log_following(self):

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,
Expand All @@ -1186,7 +1190,7 @@ def test_log_following_backwards(self):

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,
Expand All @@ -1201,7 +1205,7 @@ def test_log_streaming_and_following(self):

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,
Expand All @@ -1217,7 +1221,7 @@ def test_log_tail(self):

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,
Expand All @@ -1233,7 +1237,7 @@ def test_log_since(self):

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,
Expand All @@ -1250,7 +1254,7 @@ def test_log_since_with_datetime(self):

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,
Expand All @@ -1276,7 +1280,7 @@ def test_log_tty(self):
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,
Expand All @@ -1288,7 +1292,8 @@ def test_diff(self):

fake_request.assert_called_with(
'GET',
url_prefix + 'containers/3cc2351ab11b/changes',
(url_prefix + 'containers/' +
fake_api.FAKE_CONTAINER_ID + '/changes'),
timeout=DEFAULT_TIMEOUT_SECONDS
)

Expand All @@ -1297,7 +1302,8 @@ def test_diff_with_dict_instead_of_id(self):

fake_request.assert_called_with(
'GET',
url_prefix + 'containers/3cc2351ab11b/changes',
(url_prefix + 'containers/' +
fake_api.FAKE_CONTAINER_ID + '/changes'),
timeout=DEFAULT_TIMEOUT_SECONDS
)

Expand All @@ -1306,7 +1312,7 @@ def test_port(self):

fake_request.assert_called_with(
'GET',
url_prefix + 'containers/3cc2351ab11b/json',
url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/json',
timeout=DEFAULT_TIMEOUT_SECONDS
)

Expand All @@ -1317,7 +1323,7 @@ def test_stop_container(self):

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)
)
Expand All @@ -1330,7 +1336,7 @@ def test_stop_container_with_dict_instead_of_id(self):

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)
)
Expand All @@ -1340,7 +1346,8 @@ def test_pause_container(self):

fake_request.assert_called_with(
'POST',
url_prefix + 'containers/3cc2351ab11b/pause',
(url_prefix + 'containers/' +
fake_api.FAKE_CONTAINER_ID + '/pause'),
timeout=(DEFAULT_TIMEOUT_SECONDS)
)

Expand All @@ -1349,7 +1356,8 @@ def test_unpause_container(self):

fake_request.assert_called_with(
'POST',
url_prefix + 'containers/3cc2351ab11b/unpause',
(url_prefix + 'containers/' +
fake_api.FAKE_CONTAINER_ID + '/unpause'),
timeout=(DEFAULT_TIMEOUT_SECONDS)
)

Expand All @@ -1358,7 +1366,7 @@ def test_kill_container(self):

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
)
Expand All @@ -1368,7 +1376,7 @@ def test_kill_container_with_dict_instead_of_id(self):

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
)
Expand All @@ -1378,7 +1386,7 @@ def test_kill_container_with_signal(self):

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
)
Expand All @@ -1388,7 +1396,8 @@ def test_restart_container(self):

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)
)
Expand All @@ -1398,7 +1407,8 @@ def test_restart_container_with_dict_instead_of_id(self):

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)
)
Expand All @@ -1408,7 +1418,7 @@ def test_remove_container(self):

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
)
Expand All @@ -1418,7 +1428,7 @@ def test_remove_container_with_dict_instead_of_id(self):

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
)
Expand All @@ -1428,7 +1438,8 @@ def test_export(self):

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
)
Expand All @@ -1438,7 +1449,8 @@ def test_export_with_dict_instead_of_id(self):

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
)
Expand All @@ -1448,7 +1460,7 @@ def test_inspect_container(self):

fake_request.assert_called_with(
'GET',
url_prefix + 'containers/3cc2351ab11b/json',
url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/json',
timeout=DEFAULT_TIMEOUT_SECONDS
)

Expand All @@ -1464,7 +1476,7 @@ def test_container_stats(self):

fake_request.assert_called_with(
'GET',
url_prefix + 'containers/3cc2351ab11b/stats',
url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/stats',
timeout=60,
stream=True
)
Expand All @@ -1474,7 +1486,7 @@ def test_container_top(self):

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
)
Expand All @@ -1484,7 +1496,7 @@ def test_container_top_with_psargs(self):

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
)
Expand All @@ -1496,7 +1508,8 @@ def test_container_update(self):
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
}
Expand Down
Loading

0 comments on commit 23cf16f

Please sign in to comment.