mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
nmcli: fix empty list to replace / remove values (#6769)
This commit is contained in:
parent
601fa0df62
commit
9d8bec14c0
3 changed files with 69 additions and 2 deletions
2
changelogs/fragments/6769-nmcli-fix-empty-list.yml
Normal file
2
changelogs/fragments/6769-nmcli-fix-empty-list.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- nmcli - fix support for empty list (in compare and scrape) (https://github.com/ansible-collections/community.general/pull/6769).
|
|
@ -2183,7 +2183,10 @@ class Nmcli(object):
|
||||||
if key and len(pair) > 1:
|
if key and len(pair) > 1:
|
||||||
raw_value = pair[1].lstrip()
|
raw_value = pair[1].lstrip()
|
||||||
if raw_value == '--':
|
if raw_value == '--':
|
||||||
conn_info[key] = None
|
if key_type == list:
|
||||||
|
conn_info[key] = []
|
||||||
|
else:
|
||||||
|
conn_info[key] = None
|
||||||
elif key == 'bond.options':
|
elif key == 'bond.options':
|
||||||
# Aliases such as 'miimon', 'downdelay' are equivalent to the +bond.options 'option=value' syntax.
|
# Aliases such as 'miimon', 'downdelay' are equivalent to the +bond.options 'option=value' syntax.
|
||||||
opts = raw_value.split(',')
|
opts = raw_value.split(',')
|
||||||
|
@ -2270,7 +2273,7 @@ class Nmcli(object):
|
||||||
# We can't just do `if not value` because then if there's a value
|
# We can't just do `if not value` because then if there's a value
|
||||||
# of 0 specified as an integer it'll be interpreted as empty when
|
# of 0 specified as an integer it'll be interpreted as empty when
|
||||||
# it actually isn't.
|
# it actually isn't.
|
||||||
if value != 0 and not value:
|
if value not in (0, []) and not value:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if key in conn_info:
|
if key in conn_info:
|
||||||
|
|
|
@ -262,6 +262,25 @@ ipv4.routes: { ip = 192.168.200.0/24, nh = 192.168.1.
|
||||||
ipv4.route-metric: 10
|
ipv4.route-metric: 10
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
TESTCASE_ETHERNET_MOD_IPV4_INT_WITH_ROUTE_AND_METRIC_CLEAR = [
|
||||||
|
{
|
||||||
|
'type': 'ethernet',
|
||||||
|
'conn_name': 'non_existent_nw_device',
|
||||||
|
'routes4': [],
|
||||||
|
'state': 'present',
|
||||||
|
'_ansible_check_mode': False,
|
||||||
|
'_ansible_diff': True,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'type': 'ethernet',
|
||||||
|
'conn_name': 'non_existent_nw_device',
|
||||||
|
'routes4_extended': [],
|
||||||
|
'state': 'present',
|
||||||
|
'_ansible_check_mode': False,
|
||||||
|
'_ansible_diff': True,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
TESTCASE_ETHERNET_MOD_IPV6_INT_WITH_ROUTE_AND_METRIC = [
|
TESTCASE_ETHERNET_MOD_IPV6_INT_WITH_ROUTE_AND_METRIC = [
|
||||||
{
|
{
|
||||||
'type': 'ethernet',
|
'type': 'ethernet',
|
||||||
|
@ -1672,6 +1691,17 @@ def mocked_ethernet_connection_with_ipv4_static_address_static_route_metric_modi
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mocked_ethernet_connection_with_ipv4_static_address_static_route_metric_clear(mocker):
|
||||||
|
mocker_set(mocker,
|
||||||
|
connection_exists=True,
|
||||||
|
execute_return=None,
|
||||||
|
execute_side_effect=(
|
||||||
|
(0, TESTCASE_ETHERNET_MOD_IPV4_INT_WITH_ROUTE_AND_METRIC_SHOW_OUTPUT, ""),
|
||||||
|
(0, "", ""),
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mocked_ethernet_connection_with_ipv6_static_address_static_route_metric_modify(mocker):
|
def mocked_ethernet_connection_with_ipv6_static_address_static_route_metric_modify(mocker):
|
||||||
mocker_set(mocker,
|
mocker_set(mocker,
|
||||||
|
@ -2992,6 +3022,38 @@ def test_ethernet_connection_static_ipv4_address_static_route_with_metric_modify
|
||||||
assert not results.get('failed')
|
assert not results.get('failed')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_ETHERNET_MOD_IPV4_INT_WITH_ROUTE_AND_METRIC_CLEAR, indirect=['patch_ansible_module'])
|
||||||
|
def test_ethernet_connection_static_ipv4_address_static_route_with_metric_clear(
|
||||||
|
mocked_ethernet_connection_with_ipv4_static_address_static_route_metric_clear, capfd):
|
||||||
|
"""
|
||||||
|
Test : Modify ethernet connection with static IPv4 address and static route
|
||||||
|
"""
|
||||||
|
with pytest.raises(SystemExit):
|
||||||
|
nmcli.main()
|
||||||
|
|
||||||
|
arg_list = nmcli.Nmcli.execute_command.call_args_list
|
||||||
|
add_args, add_kw = arg_list[1]
|
||||||
|
|
||||||
|
assert add_args[0][0] == '/usr/bin/nmcli'
|
||||||
|
assert add_args[0][1] == 'con'
|
||||||
|
assert add_args[0][2] == 'modify'
|
||||||
|
assert add_args[0][3] == 'non_existent_nw_device'
|
||||||
|
|
||||||
|
add_args_text = list(map(to_text, add_args[0]))
|
||||||
|
|
||||||
|
for param in ['ipv4.routes', '']:
|
||||||
|
assert param in add_args_text
|
||||||
|
|
||||||
|
out, err = capfd.readouterr()
|
||||||
|
results = json.loads(out)
|
||||||
|
|
||||||
|
assert 'ipv4.routes' in results['diff']['before']
|
||||||
|
assert 'ipv4.routes' in results['diff']['after']
|
||||||
|
|
||||||
|
assert results.get('changed') is True
|
||||||
|
assert not results.get('failed')
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE, indirect=['patch_ansible_module'])
|
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE, indirect=['patch_ansible_module'])
|
||||||
def test_ethernet_connection_static_ipv6_address_static_route_create(mocked_ethernet_connection_with_ipv6_static_address_static_route_create, capfd):
|
def test_ethernet_connection_static_ipv6_address_static_route_create(mocked_ethernet_connection_with_ipv6_static_address_static_route_create, capfd):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue