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
@@ -104,88 +105,105 @@ def test_invalid_index_name(self):
104
105
)
105
106
106
107
108
+ def encode_auth (auth_info ):
109
+ return base64 .b64encode (
110
+ auth_info .get ('username' , '' ).encode ('utf-8' ) + b':' +
111
+ auth_info .get ('password' , '' ).encode ('utf-8' ))
112
+
113
+
107
114
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
- }
115
+ index_config = {'auth' : encode_auth ({'username' : 'indexuser' })}
116
+ private_config = {'auth' : encode_auth ({'username' : 'privateuser' })}
117
+ legacy_config = {'auth' : encode_auth ({'username' : 'legacyauth' })}
118
+
119
+ auth_config = parse_auth ({
120
+ 'https://index.docker.io/v1/' : index_config ,
121
+ 'my.registry.net' : private_config ,
122
+ 'http://legacy.registry.url/v1/' : legacy_config ,
123
+ })
113
124
114
125
def test_resolve_authconfig_hostname_only (self ):
115
126
self .assertEqual (
116
- auth .resolve_authconfig (self .auth_config , 'my.registry.net' ),
117
- {'auth' : 'privateuser' }
127
+ auth .resolve_authconfig (
128
+ self .auth_config , 'my.registry.net'
129
+ )['username' ],
130
+ 'privateuser'
118
131
)
119
132
120
133
def test_resolve_authconfig_no_protocol (self ):
121
134
self .assertEqual (
122
- auth .resolve_authconfig (self .auth_config , 'my.registry.net/v1/' ),
123
- {'auth' : 'privateuser' }
135
+ auth .resolve_authconfig (
136
+ self .auth_config , 'my.registry.net/v1/'
137
+ )['username' ],
138
+ 'privateuser'
124
139
)
125
140
126
141
def test_resolve_authconfig_no_path (self ):
127
142
self .assertEqual (
128
143
auth .resolve_authconfig (
129
144
self .auth_config , 'http://my.registry.net'
130
- ),
131
- { 'auth' : ' privateuser'}
145
+ )[ 'username' ] ,
146
+ ' privateuser'
132
147
)
133
148
134
149
def test_resolve_authconfig_no_path_trailing_slash (self ):
135
150
self .assertEqual (
136
151
auth .resolve_authconfig (
137
152
self .auth_config , 'http://my.registry.net/'
138
- ),
139
- { 'auth' : ' privateuser'}
153
+ )[ 'username' ] ,
154
+ ' privateuser'
140
155
)
141
156
142
157
def test_resolve_authconfig_no_path_wrong_secure_proto (self ):
143
158
self .assertEqual (
144
159
auth .resolve_authconfig (
145
160
self .auth_config , 'https://my.registry.net'
146
- ),
147
- { 'auth' : ' privateuser'}
161
+ )[ 'username' ] ,
162
+ ' privateuser'
148
163
)
149
164
150
165
def test_resolve_authconfig_no_path_wrong_insecure_proto (self ):
151
166
self .assertEqual (
152
167
auth .resolve_authconfig (
153
168
self .auth_config , 'http://index.docker.io'
154
- ),
155
- { 'auth' : ' indexuser'}
169
+ )[ 'username' ] ,
170
+ ' indexuser'
156
171
)
157
172
158
173
def test_resolve_authconfig_path_wrong_proto (self ):
159
174
self .assertEqual (
160
175
auth .resolve_authconfig (
161
176
self .auth_config , 'https://my.registry.net/v1/'
162
- ),
163
- { 'auth' : ' privateuser'}
177
+ )[ 'username' ] ,
178
+ ' privateuser'
164
179
)
165
180
166
181
def test_resolve_authconfig_default_registry (self ):
167
182
self .assertEqual (
168
- auth .resolve_authconfig (self .auth_config ), {'auth' : 'indexuser' }
183
+ auth .resolve_authconfig (self .auth_config )['username' ],
184
+ 'indexuser'
169
185
)
170
186
171
187
def test_resolve_authconfig_default_explicit_none (self ):
172
188
self .assertEqual (
173
- auth .resolve_authconfig (self .auth_config , None ),
174
- { 'auth' : ' indexuser'}
189
+ auth .resolve_authconfig (self .auth_config , None )[ 'username' ] ,
190
+ ' indexuser'
175
191
)
176
192
177
193
def test_resolve_authconfig_fully_explicit (self ):
178
194
self .assertEqual (
179
195
auth .resolve_authconfig (
180
196
self .auth_config , 'http://my.registry.net/v1/'
181
- ),
182
- { 'auth' : ' privateuser'}
197
+ )[ 'username' ] ,
198
+ ' privateuser'
183
199
)
184
200
185
201
def test_resolve_authconfig_legacy_config (self ):
186
202
self .assertEqual (
187
- auth .resolve_authconfig (self .auth_config , 'legacy.registry.url' ),
188
- {'auth' : 'legacyauth' }
203
+ auth .resolve_authconfig (
204
+ self .auth_config , 'legacy.registry.url'
205
+ )['username' ],
206
+ 'legacyauth'
189
207
)
190
208
191
209
def test_resolve_authconfig_no_match (self ):
@@ -198,26 +216,27 @@ def test_resolve_registry_and_auth_library_image(self):
198
216
self .assertEqual (
199
217
auth .resolve_authconfig (
200
218
self .auth_config , auth .resolve_repository_name (image )[0 ]
201
- ),
202
- { 'auth' : ' indexuser'} ,
219
+ )[ 'username' ] ,
220
+ ' indexuser' ,
203
221
)
204
222
205
223
def test_resolve_registry_and_auth_hub_image (self ):
206
224
image = 'username/image'
207
225
self .assertEqual (
208
226
auth .resolve_authconfig (
209
227
self .auth_config , auth .resolve_repository_name (image )[0 ]
210
- ),
211
- {'auth' : 'indexuser' },
228
+ )['username' ],
229
+ 'indexuser' ,
230
+ )
212
231
)
213
232
214
233
def test_resolve_registry_and_auth_private_registry (self ):
215
234
image = 'my.registry.net/image'
216
235
self .assertEqual (
217
236
auth .resolve_authconfig (
218
237
self .auth_config , auth .resolve_repository_name (image )[0 ]
219
- ),
220
- { 'auth' : ' privateuser'} ,
238
+ )[ 'username' ] ,
239
+ ' privateuser' ,
221
240
)
222
241
223
242
def test_resolve_registry_and_auth_unauthenticated_registry (self ):
0 commit comments