Refactor resolve_authconfig tests

The structure of the fake config dictionary was not reflective of what
actual parsed config looks like.

Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
Aanand Prasad 2015-12-10 12:36:51 +00:00
parent caa64b3f6a
commit 9b890c4540
2 changed files with 52 additions and 38 deletions

View File

@ -82,11 +82,6 @@ def convert_to_hostname(url):
return url.replace('http://', '').replace('https://', '').split('/', 1)[0] return url.replace('http://', '').replace('https://', '').split('/', 1)[0]
def encode_auth(auth_info):
return base64.b64encode(auth_info.get('username', '') + b':' +
auth_info.get('password', ''))
def decode_auth(auth): def decode_auth(auth):
if isinstance(auth, six.string_types): if isinstance(auth, six.string_types):
auth = auth.encode('ascii') auth = auth.encode('ascii')
@ -121,7 +116,7 @@ def parse_auth(entries):
conf[registry] = { conf[registry] = {
'username': username, 'username': username,
'password': password, 'password': password,
'email': entry['email'], 'email': entry.get('email'),
'serveraddress': registry, 'serveraddress': registry,
} }
return conf return conf

View File

@ -9,6 +9,7 @@ import shutil
import tempfile import tempfile
from docker import auth from docker import auth
from docker.auth.auth import parse_auth
from docker import errors from docker import errors
from .. import base from .. import base
@ -104,88 +105,105 @@ class ResolveRepositoryNameTest(base.BaseTestCase):
) )
def encode_auth(auth_info):
return base64.b64encode(
auth_info.get('username', '').encode('utf-8') + b':' +
auth_info.get('password', '').encode('utf-8'))
class ResolveAuthTest(base.BaseTestCase): class ResolveAuthTest(base.BaseTestCase):
auth_config = { index_config = {'auth': encode_auth({'username': 'indexuser'})}
'https://index.docker.io/v1/': {'auth': 'indexuser'}, private_config = {'auth': encode_auth({'username': 'privateuser'})}
'my.registry.net': {'auth': 'privateuser'}, legacy_config = {'auth': encode_auth({'username': 'legacyauth'})}
'http://legacy.registry.url/v1/': {'auth': 'legacyauth'}
} auth_config = parse_auth({
'https://index.docker.io/v1/': index_config,
'my.registry.net': private_config,
'http://legacy.registry.url/v1/': legacy_config,
})
def test_resolve_authconfig_hostname_only(self): def test_resolve_authconfig_hostname_only(self):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig(self.auth_config, 'my.registry.net'), auth.resolve_authconfig(
{'auth': 'privateuser'} self.auth_config, 'my.registry.net'
)['username'],
'privateuser'
) )
def test_resolve_authconfig_no_protocol(self): def test_resolve_authconfig_no_protocol(self):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig(self.auth_config, 'my.registry.net/v1/'), auth.resolve_authconfig(
{'auth': 'privateuser'} self.auth_config, 'my.registry.net/v1/'
)['username'],
'privateuser'
) )
def test_resolve_authconfig_no_path(self): def test_resolve_authconfig_no_path(self):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig( auth.resolve_authconfig(
self.auth_config, 'http://my.registry.net' self.auth_config, 'http://my.registry.net'
), )['username'],
{'auth': 'privateuser'} 'privateuser'
) )
def test_resolve_authconfig_no_path_trailing_slash(self): def test_resolve_authconfig_no_path_trailing_slash(self):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig( auth.resolve_authconfig(
self.auth_config, 'http://my.registry.net/' self.auth_config, 'http://my.registry.net/'
), )['username'],
{'auth': 'privateuser'} 'privateuser'
) )
def test_resolve_authconfig_no_path_wrong_secure_proto(self): def test_resolve_authconfig_no_path_wrong_secure_proto(self):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig( auth.resolve_authconfig(
self.auth_config, 'https://my.registry.net' self.auth_config, 'https://my.registry.net'
), )['username'],
{'auth': 'privateuser'} 'privateuser'
) )
def test_resolve_authconfig_no_path_wrong_insecure_proto(self): def test_resolve_authconfig_no_path_wrong_insecure_proto(self):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig( auth.resolve_authconfig(
self.auth_config, 'http://index.docker.io' self.auth_config, 'http://index.docker.io'
), )['username'],
{'auth': 'indexuser'} 'indexuser'
) )
def test_resolve_authconfig_path_wrong_proto(self): def test_resolve_authconfig_path_wrong_proto(self):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig( auth.resolve_authconfig(
self.auth_config, 'https://my.registry.net/v1/' self.auth_config, 'https://my.registry.net/v1/'
), )['username'],
{'auth': 'privateuser'} 'privateuser'
) )
def test_resolve_authconfig_default_registry(self): def test_resolve_authconfig_default_registry(self):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig(self.auth_config), {'auth': 'indexuser'} auth.resolve_authconfig(self.auth_config)['username'],
'indexuser'
) )
def test_resolve_authconfig_default_explicit_none(self): def test_resolve_authconfig_default_explicit_none(self):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig(self.auth_config, None), auth.resolve_authconfig(self.auth_config, None)['username'],
{'auth': 'indexuser'} 'indexuser'
) )
def test_resolve_authconfig_fully_explicit(self): def test_resolve_authconfig_fully_explicit(self):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig( auth.resolve_authconfig(
self.auth_config, 'http://my.registry.net/v1/' self.auth_config, 'http://my.registry.net/v1/'
), )['username'],
{'auth': 'privateuser'} 'privateuser'
) )
def test_resolve_authconfig_legacy_config(self): def test_resolve_authconfig_legacy_config(self):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig(self.auth_config, 'legacy.registry.url'), auth.resolve_authconfig(
{'auth': 'legacyauth'} self.auth_config, 'legacy.registry.url'
)['username'],
'legacyauth'
) )
def test_resolve_authconfig_no_match(self): def test_resolve_authconfig_no_match(self):
@ -198,8 +216,8 @@ class ResolveAuthTest(base.BaseTestCase):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig( auth.resolve_authconfig(
self.auth_config, auth.resolve_repository_name(image)[0] self.auth_config, auth.resolve_repository_name(image)[0]
), )['username'],
{'auth': 'indexuser'}, 'indexuser',
) )
def test_resolve_registry_and_auth_hub_image(self): def test_resolve_registry_and_auth_hub_image(self):
@ -207,8 +225,9 @@ class ResolveAuthTest(base.BaseTestCase):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig( auth.resolve_authconfig(
self.auth_config, auth.resolve_repository_name(image)[0] self.auth_config, auth.resolve_repository_name(image)[0]
), )['username'],
{'auth': 'indexuser'}, 'indexuser',
)
) )
def test_resolve_registry_and_auth_private_registry(self): def test_resolve_registry_and_auth_private_registry(self):
@ -216,8 +235,8 @@ class ResolveAuthTest(base.BaseTestCase):
self.assertEqual( self.assertEqual(
auth.resolve_authconfig( auth.resolve_authconfig(
self.auth_config, auth.resolve_repository_name(image)[0] self.auth_config, auth.resolve_repository_name(image)[0]
), )['username'],
{'auth': 'privateuser'}, 'privateuser',
) )
def test_resolve_registry_and_auth_unauthenticated_registry(self): def test_resolve_registry_and_auth_unauthenticated_registry(self):