Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions docker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
if not six.PY3:
import websocket

DEFAULT_DOCKER_API_VERSION = '1.16'
DEFAULT_DOCKER_API_VERSION = '1.17'
DEFAULT_TIMEOUT_SECONDS = 60
STREAM_HEADER_SIZE_BYTES = 8

Expand Down Expand Up @@ -374,8 +374,12 @@ def stream_result():

sep = bytes() if six.PY3 else str()

return stream and self._multiplexed_response_stream_helper(response) or \
sep.join([x for x in self._multiplexed_buffer_helper(response)])
if stream:
return self._multiplexed_response_stream_helper(response)
else:
return sep.join(
[x for x in self._multiplexed_buffer_helper(response)]
)

def attach_socket(self, container, params=None, ws=False):
if params is None:
Expand Down Expand Up @@ -892,6 +896,27 @@ def remove_image(self, image, force=False, noprune=False):
res = self._delete(self._url("/images/" + image), params=params)
self._raise_for_status(res)

def rename(self, container, name):
if utils.compare_version('1.17', self._version) < 0:
raise errors.InvalidVersion(
'rename was only introduced in API version 1.17'
)
if isinstance(container, dict):
container = container.get('Id')
url = self._url("/containers/{0}/rename".format(container))
params = {'name': name}
res = self._post(url, params=params)
self._raise_for_status(res)

def resize(self, container, height, width):
if isinstance(container, dict):
container = container.get('Id')

params = {'h': height, 'w': width}
url = self._url("/containers/{0}/resize".format(container))
res = self._post(url, params=params)
self._raise_for_status(res)

def restart(self, container, timeout=10):
if isinstance(container, dict):
container = container.get('Id')
Expand Down Expand Up @@ -939,15 +964,6 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
res = self._post_json(url, data=start_config)
self._raise_for_status(res)

def resize(self, container, height, width):
if isinstance(container, dict):
container = container.get('Id')

params = {'h': height, 'w': width}
url = self._url("/containers/{0}/resize".format(container))
res = self._post(url, params=params)
self._raise_for_status(res)

def stop(self, container, timeout=10):
if isinstance(container, dict):
container = container.get('Id')
Expand Down
4 changes: 2 additions & 2 deletions docker/unixconn/unixconn.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import six
import requests.adapters
import socket

if six.PY3:
import http.client as httplib
else:
import httplib
import requests.adapters
import socket

try:
import requests.packages.urllib3 as urllib3
Expand Down
9 changes: 9 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,15 @@ Remove an image. Similar to the `docker rmi` command.
* force (bool): Force removal of the image
* noprune (bool): Do not delete untagged parents

## rename

Rename a container. Similar to the `docker rename` command.

**Params**:

* container (str): ID of the container to rename
* name (str): New name for the container

## restart

Restart a container. Similar to the `docker restart` command.
Expand Down
9 changes: 8 additions & 1 deletion tests/fake_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

CURRENT_VERSION = 'v1.16'
CURRENT_VERSION = 'v1.17'

FAKE_CONTAINER_ID = '3cc2351ab11b'
FAKE_IMAGE_ID = 'e9aa60c60128'
Expand Down Expand Up @@ -271,6 +271,11 @@ def post_fake_restart_container():
return status_code, response


def post_fake_rename_container():
status_code = 204
return status_code, None


def delete_fake_remove_container():
status_code = 200
response = {'Id': FAKE_CONTAINER_ID}
Expand Down Expand Up @@ -348,6 +353,8 @@ def post_fake_tag_image():
post_fake_resize_container,
'{1}/{0}/containers/3cc2351ab11b/json'.format(CURRENT_VERSION, prefix):
get_fake_inspect_container,
'{1}/{0}/containers/3cc2351ab11b/rename'.format(CURRENT_VERSION, prefix):
post_fake_rename_container,
'{1}/{0}/images/e9aa60c60128/tag'.format(CURRENT_VERSION, prefix):
post_fake_tag_image,
'{1}/{0}/containers/3cc2351ab11b/wait'.format(CURRENT_VERSION, prefix):
Expand Down
12 changes: 12 additions & 0 deletions tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ def runTest(self):
self.assertEqual('/foobar', inspect['Name'])


class TestRenameContainer(BaseTestCase):
def runTest(self):
name = 'hong_meiling'
res = self.client.create_container('busybox', 'true')
self.assertIn('Id', res)
self.tmp_containers.append(res['Id'])
self.client.rename(res, name)
inspect = self.client.inspect_container(res['Id'])
self.assertIn('Name', inspect)
self.assertEqual(name, inspect['Name'])


class TestStartContainer(BaseTestCase):
def runTest(self):
res = self.client.create_container('busybox', 'true')
Expand Down
15 changes: 15 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,21 @@ def test_resize_container(self):
timeout=docker.client.DEFAULT_TIMEOUT_SECONDS
)

def test_rename_container(self):
try:
self.client.rename(
{'Id': fake_api.FAKE_CONTAINER_ID},
name='foobar'
)
except Exception as e:
self.fail('Command shold not raise exception: {0}'.format(e))

fake_request.assert_called_with(
url_prefix + 'containers/3cc2351ab11b/rename',
params={'name': 'foobar'},
timeout=docker.client.DEFAULT_TIMEOUT_SECONDS
)

def test_wait(self):
try:
self.client.wait(fake_api.FAKE_CONTAINER_ID)
Expand Down