1
1
import base64
2
- import json
3
2
import os
4
- import shutil
5
3
import tempfile
6
4
import time
7
5
import unittest
8
6
import warnings
9
7
10
8
import docker
11
- import six
12
9
13
- BUSYBOX = 'busybox:buildroot-2014.02'
14
- EXEC_DRIVER = []
10
+ from .. import helpers
15
11
16
12
17
- def exec_driver_is_native ():
18
- global EXEC_DRIVER
19
- if not EXEC_DRIVER :
20
- c = docker_client ()
21
- EXEC_DRIVER = c .info ()['ExecutionDriver' ]
22
- c .close ()
23
- return EXEC_DRIVER .startswith ('native' )
24
-
25
-
26
- def docker_client (** kwargs ):
27
- return docker .Client (** docker_client_kwargs (** kwargs ))
28
-
29
-
30
- def docker_client_kwargs (** kwargs ):
31
- client_kwargs = docker .utils .kwargs_from_env (assert_hostname = False )
32
- client_kwargs .update (kwargs )
33
- return client_kwargs
34
-
35
-
36
- def setup_module ():
37
- warnings .simplefilter ('error' )
38
- c = docker_client ()
39
- try :
40
- c .inspect_image (BUSYBOX )
41
- except docker .errors .NotFound :
42
- os .write (2 , "\n pulling busybox\n " .encode ('utf-8' ))
43
- for data in c .pull (BUSYBOX , stream = True ):
44
- data = json .loads (data .decode ('utf-8' ))
45
- os .write (2 , ("%c[2K\r " % 27 ).encode ('utf-8' ))
46
- status = data .get ("status" )
47
- progress = data .get ("progress" )
48
- detail = "{0} - {1}" .format (status , progress ).encode ('utf-8' )
49
- os .write (2 , detail )
50
- os .write (2 , "\n pulled busybox\n " .encode ('utf-8' ))
51
-
52
- # Double make sure we now have busybox
53
- c .inspect_image (BUSYBOX )
54
- c .close ()
55
-
56
-
57
- class BaseTestCase (unittest .TestCase ):
58
- tmp_imgs = []
59
- tmp_containers = []
60
- tmp_folders = []
61
- tmp_volumes = []
62
-
63
- def setUp (self ):
64
- if six .PY2 :
65
- self .assertRegex = self .assertRegexpMatches
66
- self .assertCountEqual = self .assertItemsEqual
67
- self .client = docker_client (timeout = 60 )
68
- self .tmp_imgs = []
69
- self .tmp_containers = []
70
- self .tmp_folders = []
71
- self .tmp_volumes = []
72
- self .tmp_networks = []
73
-
74
- def tearDown (self ):
75
- for img in self .tmp_imgs :
76
- try :
77
- self .client .remove_image (img )
78
- except docker .errors .APIError :
79
- pass
80
- for container in self .tmp_containers :
81
- try :
82
- self .client .stop (container , timeout = 1 )
83
- self .client .remove_container (container )
84
- except docker .errors .APIError :
85
- pass
86
- for network in self .tmp_networks :
87
- try :
88
- self .client .remove_network (network )
89
- except docker .errors .APIError :
90
- pass
91
- for folder in self .tmp_folders :
92
- shutil .rmtree (folder )
93
-
94
- for volume in self .tmp_volumes :
95
- try :
96
- self .client .remove_volume (volume )
97
- except docker .errors .APIError :
98
- pass
99
-
100
- self .client .close ()
101
-
102
- def run_container (self , * args , ** kwargs ):
103
- container = self .client .create_container (* args , ** kwargs )
104
- self .tmp_containers .append (container )
105
- self .client .start (container )
106
- exitcode = self .client .wait (container )
107
-
108
- if exitcode != 0 :
109
- output = self .client .logs (container )
110
- raise Exception (
111
- "Container exited with code {}:\n {}"
112
- .format (exitcode , output ))
113
-
114
- return container
115
-
116
-
117
- #########################
118
- # INFORMATION TESTS #
119
- #########################
120
-
121
-
122
- class InformationTest (BaseTestCase ):
13
+ class InformationTest (helpers .BaseTestCase ):
123
14
def test_version (self ):
124
15
res = self .client .version ()
125
16
self .assertIn ('GoVersion' , res )
@@ -133,24 +24,19 @@ def test_info(self):
133
24
self .assertIn ('Debug' , res )
134
25
135
26
def test_search (self ):
136
- self .client = docker_client (timeout = 10 )
27
+ self .client = helpers . docker_client (timeout = 10 )
137
28
res = self .client .search ('busybox' )
138
29
self .assertTrue (len (res ) >= 1 )
139
30
base_img = [x for x in res if x ['name' ] == 'busybox' ]
140
31
self .assertEqual (len (base_img ), 1 )
141
32
self .assertIn ('description' , base_img [0 ])
142
33
143
34
144
- #################
145
- # LINKS TESTS #
146
- #################
147
-
148
-
149
- class LinkTest (BaseTestCase ):
35
+ class LinkTest (helpers .BaseTestCase ):
150
36
def test_remove_link (self ):
151
37
# Create containers
152
38
container1 = self .client .create_container (
153
- BUSYBOX , 'cat' , detach = True , stdin_open = True
39
+ helpers . BUSYBOX , 'cat' , detach = True , stdin_open = True
154
40
)
155
41
container1_id = container1 ['Id' ]
156
42
self .tmp_containers .append (container1_id )
@@ -162,7 +48,7 @@ def test_remove_link(self):
162
48
link_alias = 'mylink'
163
49
164
50
container2 = self .client .create_container (
165
- BUSYBOX , 'cat' , host_config = self .client .create_host_config (
51
+ helpers . BUSYBOX , 'cat' , host_config = self .client .create_host_config (
166
52
links = {link_path : link_alias }, network_mode = 'none'
167
53
)
168
54
)
@@ -188,11 +74,7 @@ def test_remove_link(self):
188
74
self .assertEqual (len (retrieved ), 2 )
189
75
190
76
191
- #######################
192
- # PY SPECIFIC TESTS #
193
- #######################
194
-
195
- class LoadConfigTest (BaseTestCase ):
77
+ class LoadConfigTest (helpers .BaseTestCase ):
196
78
def test_load_legacy_config (self ):
197
79
folder = tempfile .mkdtemp ()
198
80
self .tmp_folders .append (folder )
@@ -231,7 +113,7 @@ def test_load_json_config(self):
231
113
232
114
class AutoDetectVersionTest (unittest .TestCase ):
233
115
def test_client_init (self ):
234
- client = docker_client (version = 'auto' )
116
+ client = helpers . docker_client (version = 'auto' )
235
117
client_version = client ._version
236
118
api_version = client .version (api_version = False )['ApiVersion' ]
237
119
self .assertEqual (client_version , api_version )
@@ -240,15 +122,17 @@ def test_client_init(self):
240
122
client .close ()
241
123
242
124
def test_auto_client (self ):
243
- client = docker .AutoVersionClient (** docker_client_kwargs ())
125
+ client = docker .AutoVersionClient (** helpers . docker_client_kwargs ())
244
126
client_version = client ._version
245
127
api_version = client .version (api_version = False )['ApiVersion' ]
246
128
self .assertEqual (client_version , api_version )
247
129
api_version_2 = client .version ()['ApiVersion' ]
248
130
self .assertEqual (client_version , api_version_2 )
249
131
client .close ()
250
132
with self .assertRaises (docker .errors .DockerException ):
251
- docker .AutoVersionClient (** docker_client_kwargs (version = '1.11' ))
133
+ docker .AutoVersionClient (
134
+ ** helpers .docker_client_kwargs (version = '1.11' )
135
+ )
252
136
253
137
254
138
class ConnectionTimeoutTest (unittest .TestCase ):
@@ -283,7 +167,7 @@ def test_resource_warnings(self):
283
167
with warnings .catch_warnings (record = True ) as w :
284
168
warnings .simplefilter ('always' )
285
169
286
- client = docker_client ()
170
+ client = helpers . docker_client ()
287
171
client .images ()
288
172
client .close ()
289
173
del client
0 commit comments