1 # Copyright 2013 dotCloud inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
32 from unittest
import mock
37 def response(status_code
=200, content
='', headers
=None, reason
=None, elapsed
=0,
39 res
= requests
.Response()
40 res
.status_code
= status_code
41 if not isinstance(content
, six
.string_types
):
42 content
= json
.dumps(content
)
44 content
= content
.encode('ascii')
45 res
._content
= content
46 res
.headers
= requests
.structures
.CaseInsensitiveDict(headers
or {})
48 res
.elapsed
= datetime
.timedelta(elapsed
)
53 def fake_resp(url
, data
=None, **kwargs
):
54 status_code
, content
= fake_api
.fake_responses
[url
]()
55 return response(status_code
=status_code
, content
=content
)
57 fake_request
= mock
.Mock(side_effect
=fake_resp
)
58 url_prefix
= 'http+unix://var/run/docker.sock/v{0}/'.format(
59 docker
.client
.DEFAULT_DOCKER_API_VERSION
)
62 @mock.patch
.multiple('docker.Client', get
=fake_request
, post
=fake_request
,
63 put
=fake_request
, delete
=fake_request
)
64 class DockerClientTest(unittest
.TestCase
):
66 self
.client
= docker
.Client()
67 # Force-clear authconfig to avoid tampering with the tests
68 self
.client
._cfg
= {'Configs': {}}
70 #########################
71 ## INFORMATION TESTS ##
72 #########################
73 def test_version(self
):
76 except Exception as e
:
77 self
.fail('Command should not raise exception: {0}'.format(e
))
79 fake_request
.assert_called_with(
80 url_prefix
+ 'version',
81 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
87 except Exception as e
:
88 self
.fail('Command should not raise exception: {0}'.format(e
))
90 fake_request
.assert_called_with(
92 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
95 def test_search(self
):
97 self
.client
.search('busybox')
98 except Exception as e
:
99 self
.fail('Command should not raise exception: {0}'.format(e
))
101 fake_request
.assert_called_with(
102 url_prefix
+ 'images/search',
103 params
={'term': 'busybox'},
104 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
107 def test_image_viz(self
):
109 self
.client
.images('busybox', viz
=True)
110 self
.fail('Viz output should not be supported!')
118 def test_images(self
):
120 self
.client
.images(all
=True)
121 except Exception as e
:
122 self
.fail('Command should not raise exception: {0}'.format(e
))
123 fake_request
.assert_called_with(
124 url_prefix
+ 'images/json',
125 params
={'filter': None, 'only_ids': 0, 'all': 1},
126 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
129 def test_images_quiet(self
):
131 self
.client
.images(all
=True, quiet
=True)
132 except Exception as e
:
133 self
.fail('Command should not raise exception: {0}'.format(e
))
134 fake_request
.assert_called_with(
135 url_prefix
+ 'images/json',
136 params
={'filter': None, 'only_ids': 1, 'all': 1},
137 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
140 def test_image_ids(self
):
142 self
.client
.images(quiet
=True)
143 except Exception as e
:
144 self
.fail('Command should not raise exception: {0}'.format(e
))
146 fake_request
.assert_called_with(
147 url_prefix
+ 'images/json',
148 params
={'filter': None, 'only_ids': 1, 'all': 0},
149 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
152 def test_list_containers(self
):
154 self
.client
.containers(all
=True)
155 except Exception as e
:
156 self
.fail('Command should not raise exception: {0}'.format(e
))
158 fake_request
.assert_called_with(
159 url_prefix
+ 'containers/json',
167 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
170 #####################
171 ## CONTAINER TESTS ##
172 #####################
174 def test_create_container(self
):
176 self
.client
.create_container('busybox', 'true')
177 except Exception as e
:
178 self
.fail('Command should not raise exception: {0}'.format(e
))
180 args
= fake_request
.call_args
181 self
.assertEqual(args
[0][0],
182 url_prefix
+ 'containers/create')
183 self
.assertEqual(json
.loads(args
[1]['data']),
185 {"Tty": false, "Image": "busybox", "Cmd": ["true"],
186 "AttachStdin": false, "Memory": 0,
187 "AttachStderr": true, "AttachStdout": true,
189 "OpenStdin": false, "NetworkDisabled": false}'''))
190 self
.assertEqual(args
[1]['headers'],
191 {'Content-Type': 'application/json'})
193 def test_create_container_with_binds(self
):
195 #mount_origin = '/tmp'
198 self
.client
.create_container('busybox', ['ls', mount_dest
],
199 volumes
=[mount_dest
])
200 except Exception as e
:
201 self
.fail('Command should not raise exception: {0}'.format(e
))
203 args
= fake_request
.call_args
204 self
.assertEqual(args
[0][0],
205 url_prefix
+ 'containers/create')
206 self
.assertEqual(json
.loads(args
[1]['data']),
208 {"Tty": false, "Image": "busybox",
209 "Cmd": ["ls", "/mnt"], "AttachStdin": false,
210 "Volumes": {"/mnt": {}}, "Memory": 0,
211 "AttachStderr": true,
212 "AttachStdout": true, "OpenStdin": false,
214 "NetworkDisabled": false}'''))
215 self
.assertEqual(args
[1]['headers'],
216 {'Content-Type': 'application/json'})
218 def test_create_container_with_ports(self
):
220 self
.client
.create_container('busybox', 'ls',
221 ports
=[1111, (2222, 'udp'), (3333,)])
222 except Exception as e
:
223 self
.fail('Command should not raise exception: {0}'.format(e
))
225 args
= fake_request
.call_args
226 self
.assertEqual(args
[0][0],
227 url_prefix
+ 'containers/create')
228 self
.assertEqual(json
.loads(args
[1]['data']),
230 {"Tty": false, "Image": "busybox",
231 "Cmd": ["ls"], "AttachStdin": false,
232 "Memory": 0, "ExposedPorts": {
237 "AttachStderr": true,
238 "AttachStdout": true, "OpenStdin": false,
240 "NetworkDisabled": false}'''))
241 self
.assertEqual(args
[1]['headers'],
242 {'Content-Type': 'application/json'})
244 def test_create_container_with_entrypoint(self
):
246 self
.client
.create_container('busybox', 'hello',
248 except Exception as e
:
249 self
.fail('Command should not raise exception: {0}'.format(e
))
251 args
= fake_request
.call_args
252 self
.assertEqual(args
[0][0],
253 url_prefix
+ 'containers/create')
254 self
.assertEqual(json
.loads(args
[1]['data']),
256 {"Tty": false, "Image": "busybox",
257 "Cmd": ["hello"], "AttachStdin": false,
259 "AttachStderr": true,
260 "AttachStdout": true, "OpenStdin": false,
262 "NetworkDisabled": false,
263 "Entrypoint": "cowsay"}'''))
264 self
.assertEqual(args
[1]['headers'],
265 {'Content-Type': 'application/json'})
267 def test_create_container_with_cpu_shares(self
):
269 self
.client
.create_container('busybox', 'ls',
271 except Exception as e
:
272 self
.fail('Command should not raise exception: {0}'.format(e
))
274 args
= fake_request
.call_args
275 self
.assertEqual(args
[0][0],
276 url_prefix
+ 'containers/create')
277 self
.assertEqual(json
.loads(args
[1]['data']),
279 {"Tty": false, "Image": "busybox",
280 "Cmd": ["ls"], "AttachStdin": false,
282 "AttachStderr": true,
283 "AttachStdout": true, "OpenStdin": false,
285 "NetworkDisabled": false,
287 self
.assertEqual(args
[1]['headers'],
288 {'Content-Type': 'application/json'})
290 def test_create_container_with_working_dir(self
):
292 self
.client
.create_container('busybox', 'ls',
294 except Exception as e
:
295 self
.fail('Command should not raise exception: {0}'.format(e
))
297 args
= fake_request
.call_args
298 self
.assertEqual(args
[0][0],
299 url_prefix
+ 'containers/create')
300 self
.assertEqual(json
.loads(args
[1]['data']),
302 {"Tty": false, "Image": "busybox",
303 "Cmd": ["ls"], "AttachStdin": false,
305 "AttachStderr": true,
306 "AttachStdout": true, "OpenStdin": false,
308 "NetworkDisabled": false,
309 "WorkingDir": "/root"}'''))
310 self
.assertEqual(args
[1]['headers'],
311 {'Content-Type': 'application/json'})
313 def test_create_container_with_stdin_open(self
):
315 self
.client
.create_container('busybox', 'true', stdin_open
=True)
316 except Exception as e
:
317 self
.fail('Command should not raise exception: {0}'.format(e
))
319 args
= fake_request
.call_args
320 self
.assertEqual(args
[0][0],
321 url_prefix
+ 'containers/create')
322 self
.assertEqual(json
.loads(args
[1]['data']),
324 {"Tty": false, "Image": "busybox", "Cmd": ["true"],
325 "AttachStdin": true, "Memory": 0,
326 "AttachStderr": true, "AttachStdout": true,
328 "OpenStdin": true, "NetworkDisabled": false}'''))
329 self
.assertEqual(args
[1]['headers'],
330 {'Content-Type': 'application/json'})
332 def test_create_named_container(self
):
334 self
.client
.create_container('busybox', 'true',
335 name
='marisa-kirisame')
336 except Exception as e
:
337 self
.fail('Command should not raise exception: {0}'.format(e
))
339 args
= fake_request
.call_args
340 self
.assertEqual(args
[0][0],
341 url_prefix
+ 'containers/create')
342 self
.assertEqual(json
.loads(args
[1]['data']),
344 {"Tty": false, "Image": "busybox", "Cmd": ["true"],
345 "AttachStdin": false, "Memory": 0,
346 "AttachStderr": true, "AttachStdout": true,
348 "OpenStdin": false, "NetworkDisabled": false}'''))
349 self
.assertEqual(args
[1]['headers'],
350 {'Content-Type': 'application/json'})
351 self
.assertEqual(args
[1]['params'], {'name': 'marisa-kirisame'})
353 def test_start_container(self
):
355 self
.client
.start(fake_api
.FAKE_CONTAINER_ID
)
356 except Exception as e
:
358 self
.fail('Command should not raise exception: {0}'.format(e
))
359 args
= fake_request
.call_args
362 url_prefix
+ 'containers/3cc2351ab11b/start'
365 json
.loads(args
[1]['data']),
366 {"PublishAllPorts": False, "Privileged": False}
370 {'Content-Type': 'application/json'}
374 docker
.client
.DEFAULT_TIMEOUT_SECONDS
377 def test_start_container_with_lxc_conf(self
):
380 fake_api
.FAKE_CONTAINER_ID
,
381 lxc_conf
={'lxc.conf.k': 'lxc.conf.value'}
383 except Exception as e
:
384 self
.fail('Command should not raise exception: {0}'.format(e
))
385 args
= fake_request
.call_args
388 url_prefix
+ 'containers/3cc2351ab11b/start'
391 json
.loads(args
[1]['data']),
392 {"LxcConf": [{"Value": "lxc.conf.value", "Key": "lxc.conf.k"}],
393 "PublishAllPorts": False, "Privileged": False}
397 {'Content-Type': 'application/json'}
401 docker
.client
.DEFAULT_TIMEOUT_SECONDS
404 def test_start_container_with_lxc_conf_compat(self
):
407 fake_api
.FAKE_CONTAINER_ID
,
408 lxc_conf
=[{'Key': 'lxc.conf.k', 'Value': 'lxc.conf.value'}]
410 except Exception as e
:
411 self
.fail('Command should not raise exception: {0}'.format(e
))
413 args
= fake_request
.call_args
414 self
.assertEqual(args
[0][0], url_prefix
+
415 'containers/3cc2351ab11b/start')
417 json
.loads(args
[1]['data']),
419 "LxcConf": [{"Key": "lxc.conf.k", "Value": "lxc.conf.value"}],
420 "PublishAllPorts": False,
424 self
.assertEqual(args
[1]['headers'],
425 {'Content-Type': 'application/json'})
428 docker
.client
.DEFAULT_TIMEOUT_SECONDS
431 def test_start_container_with_binds(self
):
434 mount_origin
= '/tmp'
435 self
.client
.start(fake_api
.FAKE_CONTAINER_ID
,
436 binds
={mount_origin
: mount_dest
})
437 except Exception as e
:
438 self
.fail('Command should not raise exception: {0}'.format(e
))
440 args
= fake_request
.call_args
441 self
.assertEqual(args
[0][0], url_prefix
+
442 'containers/3cc2351ab11b/start')
443 self
.assertEqual(json
.loads(args
[1]['data']),
444 {"Binds": ["/tmp:/mnt"],
445 "PublishAllPorts": False,
446 "Privileged": False})
447 self
.assertEqual(args
[1]['headers'],
448 {'Content-Type': 'application/json'})
451 docker
.client
.DEFAULT_TIMEOUT_SECONDS
454 def test_start_container_with_port_binds(self
):
457 self
.client
.start(fake_api
.FAKE_CONTAINER_ID
, port_bindings
={
461 4444: ('127.0.0.1',),
462 5555: ('127.0.0.1', 5555),
463 6666: [('127.0.0.1',), ('192.168.0.1',)]
465 except Exception as e
:
466 self
.fail('Command should not raise exception: {0}'.format(e
))
468 args
= fake_request
.call_args
469 self
.assertEqual(args
[0][0], url_prefix
+
470 'containers/3cc2351ab11b/start')
471 data
= json
.loads(args
[1]['data'])
472 self
.assertEqual(data
['PublishAllPorts'], False)
473 self
.assertTrue('1111/tcp' in data
['PortBindings'])
474 self
.assertTrue('2222/tcp' in data
['PortBindings'])
475 self
.assertTrue('3333/udp' in data
['PortBindings'])
476 self
.assertTrue('4444/tcp' in data
['PortBindings'])
477 self
.assertTrue('5555/tcp' in data
['PortBindings'])
478 self
.assertTrue('6666/tcp' in data
['PortBindings'])
480 [{"HostPort": "", "HostIp": ""}],
481 data
['PortBindings']['1111/tcp']
484 [{"HostPort": "2222", "HostIp": ""}],
485 data
['PortBindings']['2222/tcp']
488 [{"HostPort": "3333", "HostIp": ""}],
489 data
['PortBindings']['3333/udp']
492 [{"HostPort": "", "HostIp": "127.0.0.1"}],
493 data
['PortBindings']['4444/tcp']
496 [{"HostPort": "5555", "HostIp": "127.0.0.1"}],
497 data
['PortBindings']['5555/tcp']
499 self
.assertEqual(len(data
['PortBindings']['6666/tcp']), 2)
500 self
.assertEqual(args
[1]['headers'],
501 {'Content-Type': 'application/json'})
504 docker
.client
.DEFAULT_TIMEOUT_SECONDS
507 def test_start_container_with_links(self
):
512 self
.client
.start(fake_api
.FAKE_CONTAINER_ID
,
513 links
={link_path
: alias
})
514 except Exception as e
:
515 self
.fail('Command should not raise exception: {0}'.format(e
))
517 args
= fake_request
.call_args
520 url_prefix
+ 'containers/3cc2351ab11b/start'
523 json
.loads(args
[1]['data']),
524 {"PublishAllPorts": False, "Privileged": False,
525 "Links": ["path:alias"]}
529 {'Content-Type': 'application/json'}
532 def test_start_container_with_multiple_links(self
):
537 fake_api
.FAKE_CONTAINER_ID
,
539 link_path
+ '1': alias
+ '1',
540 link_path
+ '2': alias
+ '2'
543 except Exception as e
:
544 self
.fail('Command should not raise exception: {0}'.format(e
))
546 args
= fake_request
.call_args
549 url_prefix
+ 'containers/3cc2351ab11b/start'
552 json
.loads(args
[1]['data']),
554 "PublishAllPorts": False,
556 "Links": ["path1:alias1", "path2:alias2"]
561 {'Content-Type': 'application/json'}
564 def test_start_container_with_links_as_list_of_tuples(self
):
569 self
.client
.start(fake_api
.FAKE_CONTAINER_ID
,
570 links
=[(link_path
, alias
)])
571 except Exception as e
:
572 self
.fail('Command should not raise exception: {0}'.format(e
))
574 args
= fake_request
.call_args
577 url_prefix
+ 'containers/3cc2351ab11b/start'
580 json
.loads(args
[1]['data']),
581 {"PublishAllPorts": False, "Privileged": False,
582 "Links": ["path:alias"]}
586 {'Content-Type': 'application/json'}
589 def test_start_container_privileged(self
):
591 self
.client
.start(fake_api
.FAKE_CONTAINER_ID
, privileged
=True)
592 except Exception as e
:
593 self
.fail('Command should not raise exception: {0}'.format(e
))
595 args
= fake_request
.call_args
598 url_prefix
+ 'containers/3cc2351ab11b/start'
600 self
.assertEqual(json
.loads(args
[1]['data']),
601 {"PublishAllPorts": False, "Privileged": True})
602 self
.assertEqual(args
[1]['headers'],
603 {'Content-Type': 'application/json'})
606 docker
.client
.DEFAULT_TIMEOUT_SECONDS
609 def test_start_container_with_dict_instead_of_id(self
):
611 self
.client
.start({'Id': fake_api
.FAKE_CONTAINER_ID
})
612 except Exception as e
:
613 self
.fail('Command should not raise exception: {0}'.format(e
))
614 args
= fake_request
.call_args
617 url_prefix
+ 'containers/3cc2351ab11b/start'
620 json
.loads(args
[1]['data']),
621 {"PublishAllPorts": False, "Privileged": False}
625 {'Content-Type': 'application/json'}
629 docker
.client
.DEFAULT_TIMEOUT_SECONDS
634 self
.client
.wait(fake_api
.FAKE_CONTAINER_ID
)
635 except Exception as e
:
636 self
.fail('Command should not raise exception: {0}'.format(e
))
638 fake_request
.assert_called_with(
639 url_prefix
+ 'containers/3cc2351ab11b/wait',
643 def test_wait_with_dict_instead_of_id(self
):
645 self
.client
.wait({'Id': fake_api
.FAKE_CONTAINER_ID
})
646 except Exception as e
:
648 self
.fail('Command should not raise exception: {0}'.format(e
))
650 fake_request
.assert_called_with(
651 url_prefix
+ 'containers/3cc2351ab11b/wait',
655 def test_url_compatibility_unix(self
):
656 c
= docker
.Client(base_url
="unix://socket")
658 assert c
.base_url
== "http+unix://socket"
660 def test_url_compatibility_unix_triple_slash(self
):
661 c
= docker
.Client(base_url
="unix:///socket")
663 assert c
.base_url
== "http+unix://socket"
665 def test_url_compatibility_http_unix_triple_slash(self
):
666 c
= docker
.Client(base_url
="http+unix:///socket")
668 assert c
.base_url
== "http+unix://socket"
670 def test_url_compatibility_http(self
):
671 c
= docker
.Client(base_url
="http://hostname")
673 assert c
.base_url
== "http://hostname"
675 def test_url_compatibility_tcp(self
):
676 c
= docker
.Client(base_url
="tcp://hostname")
678 assert c
.base_url
== "http://hostname"
682 self
.client
.logs(fake_api
.FAKE_CONTAINER_ID
)
683 except Exception as e
:
684 self
.fail('Command should not raise exception: {0}'.format(e
))
686 fake_request
.assert_called_with(
687 url_prefix
+ 'containers/3cc2351ab11b/attach',
688 params
={'stream': 0, 'logs': 1, 'stderr': 1, 'stdout': 1},
689 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
,
693 def test_logs_with_dict_instead_of_id(self
):
695 self
.client
.logs({'Id': fake_api
.FAKE_CONTAINER_ID
})
696 except Exception as e
:
697 self
.fail('Command should not raise exception: {0}'.format(e
))
699 fake_request
.assert_called_with(
700 url_prefix
+ 'containers/3cc2351ab11b/attach',
701 params
={'stream': 0, 'logs': 1, 'stderr': 1, 'stdout': 1},
702 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
,
706 def test_log_streaming(self
):
708 self
.client
.logs(fake_api
.FAKE_CONTAINER_ID
, stream
=True)
709 except Exception as e
:
710 self
.fail('Command should not raise exception: {0}'.format(e
))
712 fake_request
.assert_called_with(
713 url_prefix
+ 'containers/3cc2351ab11b/attach',
714 params
={'stream': 1, 'logs': 1, 'stderr': 1, 'stdout': 1},
715 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
,
721 self
.client
.diff(fake_api
.FAKE_CONTAINER_ID
)
722 except Exception as e
:
723 self
.fail('Command should not raise exception: {0}'.format(e
))
725 fake_request
.assert_called_with(
726 url_prefix
+ 'containers/3cc2351ab11b/changes',
727 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
730 def test_diff_with_dict_instead_of_id(self
):
732 self
.client
.diff({'Id': fake_api
.FAKE_CONTAINER_ID
})
733 except Exception as e
:
734 self
.fail('Command should not raise exception: {0}'.format(e
))
736 fake_request
.assert_called_with(
737 url_prefix
+ 'containers/3cc2351ab11b/changes',
738 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
743 self
.client
.port({'Id': fake_api
.FAKE_CONTAINER_ID
}, 1111)
744 except Exception as e
:
745 self
.fail('Command should not raise exception: {0}'.format(e
))
747 fake_request
.assert_called_with(
748 url_prefix
+ 'containers/3cc2351ab11b/json',
749 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
752 def test_stop_container(self
):
754 self
.client
.stop(fake_api
.FAKE_CONTAINER_ID
, timeout
=2)
755 except Exception as e
:
756 self
.fail('Command should not raise exception: {0}'.format(e
))
758 fake_request
.assert_called_with(
759 url_prefix
+ 'containers/3cc2351ab11b/stop',
761 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
764 def test_stop_container_with_dict_instead_of_id(self
):
766 self
.client
.stop({'Id': fake_api
.FAKE_CONTAINER_ID
}, timeout
=2)
767 except Exception as e
:
768 self
.fail('Command should not raise exception: {0}'.format(e
))
770 fake_request
.assert_called_with(
771 url_prefix
+ 'containers/3cc2351ab11b/stop',
773 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
776 def test_kill_container(self
):
778 self
.client
.kill(fake_api
.FAKE_CONTAINER_ID
)
779 except Exception as e
:
780 self
.fail('Command should not raise exception: {0}'.format(e
))
782 fake_request
.assert_called_with(
783 url_prefix
+ 'containers/3cc2351ab11b/kill',
785 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
788 def test_kill_container_with_dict_instead_of_id(self
):
790 self
.client
.kill({'Id': fake_api
.FAKE_CONTAINER_ID
})
791 except Exception as e
:
792 self
.fail('Command should not raise exception: {0}'.format(e
))
794 fake_request
.assert_called_with(
795 url_prefix
+ 'containers/3cc2351ab11b/kill',
797 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
800 def test_kill_container_with_signal(self
):
802 self
.client
.kill(fake_api
.FAKE_CONTAINER_ID
, signal
=signal
.SIGTERM
)
803 except Exception as e
:
804 self
.fail('Command should not raise exception: {0}'.format(e
))
806 fake_request
.assert_called_with(
807 url_prefix
+ 'containers/3cc2351ab11b/kill',
808 params
={'signal': signal
.SIGTERM
},
809 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
812 def test_restart_container(self
):
814 self
.client
.restart(fake_api
.FAKE_CONTAINER_ID
, timeout
=2)
815 except Exception as e
:
816 self
.fail('Command should not raise exception : {0}'.format(e
))
818 fake_request
.assert_called_with(
819 url_prefix
+ 'containers/3cc2351ab11b/restart',
821 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
824 def test_restart_container_with_dict_instead_of_id(self
):
826 self
.client
.restart({'Id': fake_api
.FAKE_CONTAINER_ID
}, timeout
=2)
827 except Exception as e
:
828 self
.fail('Command should not raise exception: {0}'.format(e
))
830 fake_request
.assert_called_with(
831 url_prefix
+ 'containers/3cc2351ab11b/restart',
833 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
836 def test_remove_container(self
):
838 self
.client
.remove_container(fake_api
.FAKE_CONTAINER_ID
)
839 except Exception as e
:
840 self
.fail('Command should not raise exception: {0}'.format(e
))
842 fake_request
.assert_called_with(
843 url_prefix
+ 'containers/3cc2351ab11b',
844 params
={'v': False, 'link': False},
845 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
848 def test_remove_container_with_dict_instead_of_id(self
):
850 self
.client
.remove_container({'Id': fake_api
.FAKE_CONTAINER_ID
})
851 except Exception as e
:
852 self
.fail('Command should not raise exception: {0}'.format(e
))
854 fake_request
.assert_called_with(
855 url_prefix
+ 'containers/3cc2351ab11b',
856 params
={'v': False, 'link': False},
857 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
860 def test_remove_link(self
):
862 self
.client
.remove_container(fake_api
.FAKE_CONTAINER_ID
, link
=True)
863 except Exception as e
:
864 self
.fail('Command should not raise exception: {0}'.format(e
))
866 fake_request
.assert_called_with(
867 url_prefix
+ 'containers/3cc2351ab11b',
868 params
={'v': False, 'link': True},
869 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
872 def test_export(self
):
874 self
.client
.export(fake_api
.FAKE_CONTAINER_ID
)
875 except Exception as e
:
876 self
.fail('Command should not raise exception: {0}'.format(e
))
878 fake_request
.assert_called_with(
879 url_prefix
+ 'containers/3cc2351ab11b/export',
881 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
884 def test_export_with_dict_instead_of_id(self
):
886 self
.client
.export({'Id': fake_api
.FAKE_CONTAINER_ID
})
887 except Exception as e
:
888 self
.fail('Command should not raise exception: {0}'.format(e
))
890 fake_request
.assert_called_with(
891 url_prefix
+ 'containers/3cc2351ab11b/export',
893 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
896 def test_inspect_container(self
):
898 self
.client
.inspect_container(fake_api
.FAKE_CONTAINER_ID
)
899 except Exception as e
:
900 self
.fail('Command should not raise exception: {0}'.format(e
))
902 fake_request
.assert_called_with(
903 url_prefix
+ 'containers/3cc2351ab11b/json',
904 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
913 self
.client
.pull('joffrey/test001')
914 except Exception as e
:
915 self
.fail('Command should not raise exception: {0}'.format(e
))
917 args
= fake_request
.call_args
920 url_prefix
+ 'images/create'
924 {'tag': None, 'fromImage': 'joffrey/test001'}
926 self
.assertFalse(args
[1]['stream'])
928 def test_pull_stream(self
):
930 self
.client
.pull('joffrey/test001', stream
=True)
931 except Exception as e
:
932 self
.fail('Command should not raise exception: {0}'.format(e
))
934 args
= fake_request
.call_args
937 url_prefix
+ 'images/create'
941 {'tag': None, 'fromImage': 'joffrey/test001'}
943 self
.assertTrue(args
[1]['stream'])
945 def test_commit(self
):
947 self
.client
.commit(fake_api
.FAKE_CONTAINER_ID
)
948 except Exception as e
:
949 self
.fail('Command should not raise exception: {0}'.format(e
))
951 fake_request
.assert_called_with(
952 url_prefix
+ 'commit',
954 headers
={'Content-Type': 'application/json'},
959 'container': '3cc2351ab11b',
962 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
965 def test_remove_image(self
):
967 self
.client
.remove_image(fake_api
.FAKE_IMAGE_ID
)
968 except Exception as e
:
969 self
.fail('Command should not raise exception: {0}'.format(e
))
971 fake_request
.assert_called_with(
972 url_prefix
+ 'images/e9aa60c60128',
973 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
976 def test_image_history(self
):
978 self
.client
.history(fake_api
.FAKE_IMAGE_NAME
)
979 except Exception as e
:
980 self
.fail('Command should not raise exception: {0}'.format(e
))
982 fake_request
.assert_called_with(
983 url_prefix
+ 'images/test_image/history',
984 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
987 def test_import_image(self
):
989 self
.client
.import_image(
990 fake_api
.FAKE_TARBALL_PATH
,
991 repository
=fake_api
.FAKE_REPO_NAME
,
992 tag
=fake_api
.FAKE_TAG_NAME
994 except Exception as e
:
995 self
.fail('Command should not raise exception: {0}'.format(e
))
997 fake_request
.assert_called_with(
998 url_prefix
+ 'images/create',
1000 'repo': fake_api
.FAKE_REPO_NAME
,
1001 'tag': fake_api
.FAKE_TAG_NAME
,
1002 'fromSrc': fake_api
.FAKE_TARBALL_PATH
1005 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
1008 def test_import_image_from_file(self
):
1009 buf
= tempfile
.NamedTemporaryFile(delete
=False)
1011 # pretent the buffer is a file
1012 self
.client
.import_image(
1014 repository
=fake_api
.FAKE_REPO_NAME
,
1015 tag
=fake_api
.FAKE_TAG_NAME
1017 except Exception as e
:
1018 self
.fail('Command should not raise exception: {0}'.format(e
))
1020 fake_request
.assert_called_with(
1021 url_prefix
+ 'images/create',
1023 'repo': fake_api
.FAKE_REPO_NAME
,
1024 'tag': fake_api
.FAKE_TAG_NAME
,
1028 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
1033 def test_import_image_from_image(self
):
1035 self
.client
.import_image(
1036 image
=fake_api
.FAKE_IMAGE_NAME
,
1037 repository
=fake_api
.FAKE_REPO_NAME
,
1038 tag
=fake_api
.FAKE_TAG_NAME
1040 except Exception as e
:
1041 self
.fail('Command should not raise exception: {0}'.format(e
))
1043 fake_request
.assert_called_with(
1044 url_prefix
+ 'images/create',
1046 'repo': fake_api
.FAKE_REPO_NAME
,
1047 'tag': fake_api
.FAKE_TAG_NAME
,
1048 'fromImage': fake_api
.FAKE_IMAGE_NAME
1051 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
1054 def test_inspect_image(self
):
1056 self
.client
.inspect_image(fake_api
.FAKE_IMAGE_NAME
)
1057 except Exception as e
:
1058 self
.fail('Command should not raise exception: {0}'.format(e
))
1060 fake_request
.assert_called_with(
1061 url_prefix
+ 'images/test_image/json',
1062 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
1065 def test_insert_image(self
):
1067 self
.client
.insert(fake_api
.FAKE_IMAGE_NAME
,
1068 fake_api
.FAKE_URL
, fake_api
.FAKE_PATH
)
1069 except Exception as e
:
1070 self
.fail('Command should not raise exception: {0}'.format(e
))
1072 fake_request
.assert_called_with(
1073 url_prefix
+ 'images/test_image/insert',
1075 'url': fake_api
.FAKE_URL
,
1076 'path': fake_api
.FAKE_PATH
1078 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
1081 def test_push_image(self
):
1083 self
.client
.push(fake_api
.FAKE_IMAGE_NAME
)
1084 except Exception as e
:
1085 self
.fail('Command should not raise exception: {0}'.format(e
))
1087 fake_request
.assert_called_with(
1088 url_prefix
+ 'images/test_image/push',
1090 headers
={'Content-Type': 'application/json'},
1092 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
1095 def test_push_image_stream(self
):
1097 self
.client
.push(fake_api
.FAKE_IMAGE_NAME
, stream
=True)
1098 except Exception as e
:
1099 self
.fail('Command should not raise exception: {0}'.format(e
))
1101 fake_request
.assert_called_with(
1102 url_prefix
+ 'images/test_image/push',
1104 headers
={'Content-Type': 'application/json'},
1106 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
1109 def test_tag_image(self
):
1111 self
.client
.tag(fake_api
.FAKE_IMAGE_ID
, fake_api
.FAKE_REPO_NAME
)
1112 except Exception as e
:
1113 self
.fail('Command should not raise exception: {0}'.format(e
))
1115 fake_request
.assert_called_with(
1116 url_prefix
+ 'images/e9aa60c60128/tag',
1122 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
1125 def test_tag_image_tag(self
):
1128 fake_api
.FAKE_IMAGE_ID
,
1129 fake_api
.FAKE_REPO_NAME
,
1130 tag
=fake_api
.FAKE_TAG_NAME
1132 except Exception as e
:
1133 self
.fail('Command should not raise exception: {0}'.format(e
))
1135 fake_request
.assert_called_with(
1136 url_prefix
+ 'images/e9aa60c60128/tag',
1142 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
1145 def test_tag_image_force(self
):
1148 fake_api
.FAKE_IMAGE_ID
, fake_api
.FAKE_REPO_NAME
, force
=True)
1149 except Exception as e
:
1150 self
.fail('Command should not raise exception: {0}'.format(e
))
1152 fake_request
.assert_called_with(
1153 url_prefix
+ 'images/e9aa60c60128/tag',
1159 timeout
=docker
.client
.DEFAULT_TIMEOUT_SECONDS
1166 def test_build_container(self
):
1167 script
= io
.BytesIO('\n'.join([
1169 'MAINTAINER docker-py',
1170 'RUN mkdir -p /tmp/test',
1172 'ADD https://dl.dropboxusercontent.com/u/20637798/silence.tar.gz'
1173 ' /tmp/silence.tar.gz'
1176 self
.client
.build(fileobj
=script
)
1177 except Exception as e
:
1178 self
.fail('Command should not raise exception: {0}'.format(e
))
1180 def test_build_container_stream(self
):
1181 script
= io
.BytesIO('\n'.join([
1183 'MAINTAINER docker-py',
1184 'RUN mkdir -p /tmp/test',
1186 'ADD https://dl.dropboxusercontent.com/u/20637798/silence.tar.gz'
1187 ' /tmp/silence.tar.gz'
1190 self
.client
.build(fileobj
=script
, stream
=True)
1191 except Exception as e
:
1192 self
.fail('Command should not raise exception: {0}'.format(e
))
1194 #######################
1195 ## PY SPECIFIC TESTS ##
1196 #######################
1198 def test_load_config_no_file(self
):
1199 folder
= tempfile
.mkdtemp()
1200 cfg
= docker
.auth
.load_config(folder
)
1201 self
.assertTrue(cfg
is not None)
1203 def test_load_config(self
):
1204 folder
= tempfile
.mkdtemp()
1205 f
= open(os
.path
.join(folder
, '.dockercfg'), 'w')
1206 auth_
= base64
.b64encode(b
'sakuya:izayoi').decode('ascii')
1207 f
.write('auth = {0}\n'.format(auth_
))
1208 f
.write('email = sakuya@scarlet.net')
1210 cfg
= docker
.auth
.load_config(folder
)
1211 self
.assertTrue(docker
.auth
.INDEX_URL
in cfg
)
1212 self
.assertNotEqual(cfg
[docker
.auth
.INDEX_URL
], None)
1213 cfg
= cfg
[docker
.auth
.INDEX_URL
]
1214 self
.assertEqual(cfg
['username'], 'sakuya')
1215 self
.assertEqual(cfg
['password'], 'izayoi')
1216 self
.assertEqual(cfg
['email'], 'sakuya@scarlet.net')
1217 self
.assertEqual(cfg
.get('auth'), None)
1220 if __name__
== '__main__':