Merge "Avoid empty values in 'nets' options"

This commit is contained in:
Zuul 2018-03-10 23:51:54 +00:00 committed by Gerrit Code Review
commit 618479fb7d
2 changed files with 10 additions and 11 deletions

View File

@ -237,8 +237,8 @@ def parse_nets(ns):
"with only one of network, or port specified.")
nets = []
for net_str in ns:
net_info = {"network": "", "v4-fixed-ip": "", "v6-fixed-ip": "",
"port": ""}
keys = ["network", "port", "v4-fixed-ip", "v6-fixed-ip"]
net_info = {}
for kv_str in net_str.split(","):
try:
k, v = kv_str.split("=", 1)
@ -246,22 +246,22 @@ def parse_nets(ns):
v = v.strip()
except ValueError:
raise apiexec.CommandError(err_msg % net_str)
if k in net_info:
if net_info[k]:
if k in keys:
if net_info.get(k):
raise apiexec.CommandError(err_msg % net_str)
net_info[k] = v
else:
raise apiexec.CommandError(err_msg % net_str)
if net_info['v4-fixed-ip'] and not netutils.is_valid_ipv4(
if net_info.get('v4-fixed-ip') and not netutils.is_valid_ipv4(
net_info['v4-fixed-ip']):
raise apiexec.CommandError("Invalid ipv4 address.")
if net_info['v6-fixed-ip'] and not netutils.is_valid_ipv6(
if net_info.get('v6-fixed-ip') and not netutils.is_valid_ipv6(
net_info['v6-fixed-ip']):
raise apiexec.CommandError("Invalid ipv6 address.")
if bool(net_info['network']) == bool(net_info['port']):
if bool(net_info.get('network')) == bool(net_info.get('port')):
raise apiexec.CommandError(err_msg % net_str)
nets.append(net_info)

View File

@ -220,14 +220,13 @@ class ParseNetsTest(test_utils.BaseTestCase):
def test_nets_with_network(self):
nets = [' network = 1234567 , v4-fixed-ip = 172.17.0.3 ']
result = utils.parse_nets(nets)
self.assertEqual([{'network': '1234567', 'v4-fixed-ip': '172.17.0.3',
'port': '', 'v6-fixed-ip': ''}], result)
self.assertEqual([{'network': '1234567', 'v4-fixed-ip': '172.17.0.3'}],
result)
def test_nets_with_port(self):
nets = ['port=1234567, v6-fixed-ip=2001:db8::2']
result = utils.parse_nets(nets)
self.assertEqual([{'network': '', 'v4-fixed-ip': '',
'port': '1234567', 'v6-fixed-ip': '2001:db8::2'}],
self.assertEqual([{'port': '1234567', 'v6-fixed-ip': '2001:db8::2'}],
result)
def test_nets_with_only_ip(self):