Fix when pyyaml has interpreted line as a dictionary

Added unit tests in build_extra_hosts + fix

Signed-off-by: CJ <lim@chernjie.com>
This commit is contained in:
Thomas Desvenain 2015-01-21 20:33:51 +01:00 • committed by CJ
commit 8098b65576
2 changed files with 37 additions and 4 deletions

View file

@ -629,7 +629,17 @@ def build_extra_hosts(extra_hosts_config):
if extra_hosts_config is None:
return None
if isinstance(extra_hosts_config, list):
return dict(r.split(':') for r in extra_hosts_config)
else:
return dict([extra_hosts_config.split(':')])
if isinstance(extra_hosts_config, basestring):
extra_hosts_config = [extra_hosts_config]
extra_hosts_dict = {}
for extra_hosts_line in extra_hosts_config:
if isinstance(extra_hosts_line, dict):
# already interpreted as a dict (depends on pyyaml version)
extra_hosts_dict.update(extra_hosts_line)
else:
# not already interpreted as a dict
host, ip = extra_hosts_line.split(':')
extra_hosts_dict.update({host.strip(): ip.strip()})
return extra_hosts_dict