Skip to content

Commit

Permalink
nmcli: do not convert undefined lists to empty strings (#4813)
Browse files Browse the repository at this point in the history
* do not convert undefined lists to empty strings

* add changelog fragment (#4813)
  • Loading branch information
geichelberger authored Jun 13, 2022
1 parent e512218 commit 72faebf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/4813-fix-nmcli-convert-list.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- nmcli - fix error caused by adding undefined module arguments for list options (https://github.com/ansible-collections/community.general/issues/4373, https://github.com/ansible-collections/community.general/pull/4813).
5 changes: 4 additions & 1 deletion plugins/modules/net_tools/nmcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1836,7 +1836,10 @@ def bool_to_string(boolean):

@staticmethod
def list_to_string(lst):
return ",".join(lst or [""])
if lst is None:
return None
else:
return ",".join(lst)

@staticmethod
def settings_type(setting):
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/plugins/modules/net_tools/test_nmcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,17 @@
ipv6.ignore-auto-routes: no
"""

TESTCASE_GENERIC_ZONE_ONLY = [
{
'type': 'generic',
'conn_name': 'non_existent_nw_device',
'ifname': 'generic_non_existant',
'state': 'present',
'zone': 'public',
'_ansible_check_mode': False,
}
]

TESTCASE_BOND = [
{
'type': 'bond',
Expand Down Expand Up @@ -1888,6 +1899,30 @@ def test_generic_connection_zone_unchanged(mocked_generic_connection_zone_unchan
assert not results['changed']


@pytest.mark.parametrize('patch_ansible_module', TESTCASE_GENERIC_ZONE_ONLY, indirect=['patch_ansible_module'])
def test_generic_connection_modify_zone_only(mocked_generic_connection_modify, capfd):
"""
Test : Generic connection modified with zone only
"""
with pytest.raises(SystemExit):
nmcli.main()

assert nmcli.Nmcli.execute_command.call_count == 1
arg_list = nmcli.Nmcli.execute_command.call_args_list
args, kwargs = arg_list[0]

assert 'connection.zone' in args[0]
assert 'ipv4.addresses' not in args[0]
assert 'ipv4.gateway' not in args[0]
assert 'ipv6.addresses' not in args[0]
assert 'ipv6.gateway' not in args[0]

out, err = capfd.readouterr()
results = json.loads(out)
assert not results.get('failed')
assert results['changed']


@pytest.mark.parametrize('patch_ansible_module', TESTCASE_CONNECTION, indirect=['patch_ansible_module'])
def test_zone_none(mocked_connection_exists, capfd):
"""
Expand Down

0 comments on commit 72faebf

Please sign in to comment.