From 72faebffc6066211a47fdac2bf942e03bd6bed58 Mon Sep 17 00:00:00 2001 From: geichelberger <35195803+geichelberger@users.noreply.github.com> Date: Mon, 13 Jun 2022 11:56:10 +0200 Subject: [PATCH] nmcli: do not convert undefined lists to empty strings (#4813) * do not convert undefined lists to empty strings * add changelog fragment (#4813) --- .../4813-fix-nmcli-convert-list.yaml | 2 ++ plugins/modules/net_tools/nmcli.py | 5 ++- .../plugins/modules/net_tools/test_nmcli.py | 35 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/4813-fix-nmcli-convert-list.yaml diff --git a/changelogs/fragments/4813-fix-nmcli-convert-list.yaml b/changelogs/fragments/4813-fix-nmcli-convert-list.yaml new file mode 100644 index 0000000000..5035a9e584 --- /dev/null +++ b/changelogs/fragments/4813-fix-nmcli-convert-list.yaml @@ -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). diff --git a/plugins/modules/net_tools/nmcli.py b/plugins/modules/net_tools/nmcli.py index 859f74b91d..bb4a5016ff 100644 --- a/plugins/modules/net_tools/nmcli.py +++ b/plugins/modules/net_tools/nmcli.py @@ -1836,7 +1836,10 @@ class Nmcli(object): @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): diff --git a/tests/unit/plugins/modules/net_tools/test_nmcli.py b/tests/unit/plugins/modules/net_tools/test_nmcli.py index 39cb3e05e9..487465d863 100644 --- a/tests/unit/plugins/modules/net_tools/test_nmcli.py +++ b/tests/unit/plugins/modules/net_tools/test_nmcli.py @@ -455,6 +455,17 @@ ipv6.ignore-auto-dns: no 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', @@ -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): """