9
9
import tempfile
10
10
11
11
from docker import auth
12
+ from docker .auth .auth import parse_auth
12
13
from docker import errors
13
14
14
15
from .. import base
@@ -34,25 +35,31 @@ class ResolveRepositoryNameTest(base.BaseTestCase):
34
35
def test_resolve_repository_name_hub_library_image (self ):
35
36
self .assertEqual (
36
37
auth .resolve_repository_name ('image' ),
37
- ('index. docker.io' , 'image' ),
38
+ ('docker.io' , 'image' ),
38
39
)
39
40
40
41
def test_resolve_repository_name_dotted_hub_library_image (self ):
41
42
self .assertEqual (
42
43
auth .resolve_repository_name ('image.valid' ),
43
- ('index. docker.io' , 'image.valid' )
44
+ ('docker.io' , 'image.valid' )
44
45
)
45
46
46
47
def test_resolve_repository_name_hub_image (self ):
47
48
self .assertEqual (
48
49
auth .resolve_repository_name ('username/image' ),
49
- ('index. docker.io' , 'username/image' ),
50
+ ('docker.io' , 'username/image' ),
50
51
)
51
52
52
53
def test_explicit_hub_index_library_image (self ):
54
+ self .assertEqual (
55
+ auth .resolve_repository_name ('docker.io/image' ),
56
+ ('docker.io' , 'image' )
57
+ )
58
+
59
+ def test_explicit_legacy_hub_index_library_image (self ):
53
60
self .assertEqual (
54
61
auth .resolve_repository_name ('index.docker.io/image' ),
55
- ('index. docker.io' , 'image' )
62
+ ('docker.io' , 'image' )
56
63
)
57
64
58
65
def test_resolve_repository_name_private_registry (self ):
@@ -104,88 +111,105 @@ def test_invalid_index_name(self):
104
111
)
105
112
106
113
114
+ def encode_auth (auth_info ):
115
+ return base64 .b64encode (
116
+ auth_info .get ('username' , '' ).encode ('utf-8' ) + b':' +
117
+ auth_info .get ('password' , '' ).encode ('utf-8' ))
118
+
119
+
107
120
class ResolveAuthTest (base .BaseTestCase ):
108
- auth_config = {
109
- 'https://index.docker.io/v1/' : {'auth' : 'indexuser' },
110
- 'my.registry.net' : {'auth' : 'privateuser' },
111
- 'http://legacy.registry.url/v1/' : {'auth' : 'legacyauth' }
112
- }
121
+ index_config = {'auth' : encode_auth ({'username' : 'indexuser' })}
122
+ private_config = {'auth' : encode_auth ({'username' : 'privateuser' })}
123
+ legacy_config = {'auth' : encode_auth ({'username' : 'legacyauth' })}
124
+
125
+ auth_config = parse_auth ({
126
+ 'https://index.docker.io/v1/' : index_config ,
127
+ 'my.registry.net' : private_config ,
128
+ 'http://legacy.registry.url/v1/' : legacy_config ,
129
+ })
113
130
114
131
def test_resolve_authconfig_hostname_only (self ):
115
132
self .assertEqual (
116
- auth .resolve_authconfig (self .auth_config , 'my.registry.net' ),
117
- {'auth' : 'privateuser' }
133
+ auth .resolve_authconfig (
134
+ self .auth_config , 'my.registry.net'
135
+ )['username' ],
136
+ 'privateuser'
118
137
)
119
138
120
139
def test_resolve_authconfig_no_protocol (self ):
121
140
self .assertEqual (
122
- auth .resolve_authconfig (self .auth_config , 'my.registry.net/v1/' ),
123
- {'auth' : 'privateuser' }
141
+ auth .resolve_authconfig (
142
+ self .auth_config , 'my.registry.net/v1/'
143
+ )['username' ],
144
+ 'privateuser'
124
145
)
125
146
126
147
def test_resolve_authconfig_no_path (self ):
127
148
self .assertEqual (
128
149
auth .resolve_authconfig (
129
150
self .auth_config , 'http://my.registry.net'
130
- ),
131
- { 'auth' : ' privateuser'}
151
+ )[ 'username' ] ,
152
+ ' privateuser'
132
153
)
133
154
134
155
def test_resolve_authconfig_no_path_trailing_slash (self ):
135
156
self .assertEqual (
136
157
auth .resolve_authconfig (
137
158
self .auth_config , 'http://my.registry.net/'
138
- ),
139
- { 'auth' : ' privateuser'}
159
+ )[ 'username' ] ,
160
+ ' privateuser'
140
161
)
141
162
142
163
def test_resolve_authconfig_no_path_wrong_secure_proto (self ):
143
164
self .assertEqual (
144
165
auth .resolve_authconfig (
145
166
self .auth_config , 'https://my.registry.net'
146
- ),
147
- { 'auth' : ' privateuser'}
167
+ )[ 'username' ] ,
168
+ ' privateuser'
148
169
)
149
170
150
171
def test_resolve_authconfig_no_path_wrong_insecure_proto (self ):
151
172
self .assertEqual (
152
173
auth .resolve_authconfig (
153
174
self .auth_config , 'http://index.docker.io'
154
- ),
155
- { 'auth' : ' indexuser'}
175
+ )[ 'username' ] ,
176
+ ' indexuser'
156
177
)
157
178
158
179
def test_resolve_authconfig_path_wrong_proto (self ):
159
180
self .assertEqual (
160
181
auth .resolve_authconfig (
161
182
self .auth_config , 'https://my.registry.net/v1/'
162
- ),
163
- { 'auth' : ' privateuser'}
183
+ )[ 'username' ] ,
184
+ ' privateuser'
164
185
)
165
186
166
187
def test_resolve_authconfig_default_registry (self ):
167
188
self .assertEqual (
168
- auth .resolve_authconfig (self .auth_config ), {'auth' : 'indexuser' }
189
+ auth .resolve_authconfig (self .auth_config )['username' ],
190
+ 'indexuser'
169
191
)
170
192
171
193
def test_resolve_authconfig_default_explicit_none (self ):
172
194
self .assertEqual (
173
- auth .resolve_authconfig (self .auth_config , None ),
174
- { 'auth' : ' indexuser'}
195
+ auth .resolve_authconfig (self .auth_config , None )[ 'username' ] ,
196
+ ' indexuser'
175
197
)
176
198
177
199
def test_resolve_authconfig_fully_explicit (self ):
178
200
self .assertEqual (
179
201
auth .resolve_authconfig (
180
202
self .auth_config , 'http://my.registry.net/v1/'
181
- ),
182
- { 'auth' : ' privateuser'}
203
+ )[ 'username' ] ,
204
+ ' privateuser'
183
205
)
184
206
185
207
def test_resolve_authconfig_legacy_config (self ):
186
208
self .assertEqual (
187
- auth .resolve_authconfig (self .auth_config , 'legacy.registry.url' ),
188
- {'auth' : 'legacyauth' }
209
+ auth .resolve_authconfig (
210
+ self .auth_config , 'legacy.registry.url'
211
+ )['username' ],
212
+ 'legacyauth'
189
213
)
190
214
191
215
def test_resolve_authconfig_no_match (self ):
@@ -198,26 +222,44 @@ def test_resolve_registry_and_auth_library_image(self):
198
222
self .assertEqual (
199
223
auth .resolve_authconfig (
200
224
self .auth_config , auth .resolve_repository_name (image )[0 ]
201
- ),
202
- { 'auth' : ' indexuser'} ,
225
+ )[ 'username' ] ,
226
+ ' indexuser' ,
203
227
)
204
228
205
229
def test_resolve_registry_and_auth_hub_image (self ):
206
230
image = 'username/image'
207
231
self .assertEqual (
208
232
auth .resolve_authconfig (
209
233
self .auth_config , auth .resolve_repository_name (image )[0 ]
210
- ),
211
- {'auth' : 'indexuser' },
234
+ )['username' ],
235
+ 'indexuser' ,
236
+ )
237
+
238
+ def test_resolve_registry_and_auth_explicit_hub (self ):
239
+ image = 'docker.io/username/image'
240
+ self .assertEqual (
241
+ auth .resolve_authconfig (
242
+ self .auth_config , auth .resolve_repository_name (image )[0 ]
243
+ )['username' ],
244
+ 'indexuser' ,
245
+ )
246
+
247
+ def test_resolve_registry_and_auth_explicit_legacy_hub (self ):
248
+ image = 'index.docker.io/username/image'
249
+ self .assertEqual (
250
+ auth .resolve_authconfig (
251
+ self .auth_config , auth .resolve_repository_name (image )[0 ]
252
+ )['username' ],
253
+ 'indexuser' ,
212
254
)
213
255
214
256
def test_resolve_registry_and_auth_private_registry (self ):
215
257
image = 'my.registry.net/image'
216
258
self .assertEqual (
217
259
auth .resolve_authconfig (
218
260
self .auth_config , auth .resolve_repository_name (image )[0 ]
219
- ),
220
- { 'auth' : ' privateuser'} ,
261
+ )[ 'username' ] ,
262
+ ' privateuser' ,
221
263
)
222
264
223
265
def test_resolve_registry_and_auth_unauthenticated_registry (self ):
0 commit comments