1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

[PR #6769/9d8bec14 backport][stable-6] nmcli: fix empty list to replace / remove values (#6868)

nmcli: fix empty list to replace / remove values (#6769)

(cherry picked from commit 9d8bec14c0)

Co-authored-by: genofire <geno+dev@fireorbit.de>
This commit is contained in:
patchback[bot] 2023-07-06 21:37:25 +02:00 committed by GitHub
parent 177e327c24
commit 443a1c3ed7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 2 deletions

View 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).

View file

@ -2104,6 +2104,9 @@ class Nmcli(object):
if key and len(pair) > 1:
raw_value = pair[1].lstrip()
if raw_value == '--':
if key_type == list:
conn_info[key] = []
else:
conn_info[key] = None
elif key == 'bond.options':
# Aliases such as 'miimon', 'downdelay' are equivalent to the +bond.options 'option=value' syntax.
@ -2191,7 +2194,7 @@ class Nmcli(object):
# 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
# it actually isn't.
if value != 0 and not value:
if value not in (0, []) and not value:
continue
if key in conn_info:

View file

@ -262,6 +262,25 @@ ipv4.routes: { ip = 192.168.200.0/24, nh = 192.168.1.
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 = [
{
'type': 'ethernet',
@ -1671,6 +1690,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
def mocked_ethernet_connection_with_ipv6_static_address_static_route_metric_modify(mocker):
mocker_set(mocker,
@ -2991,6 +3021,38 @@ def test_ethernet_connection_static_ipv4_address_static_route_with_metric_modify
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'])
def test_ethernet_connection_static_ipv6_address_static_route_create(mocked_ethernet_connection_with_ipv6_static_address_static_route_create, capfd):
"""