diff --git a/changelogs/fragments/3401-nmcli-needs-type.yml b/changelogs/fragments/3401-nmcli-needs-type.yml new file mode 100644 index 0000000000..9fe7593ba3 --- /dev/null +++ b/changelogs/fragments/3401-nmcli-needs-type.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - "nmcli - the option ``routing_rules4`` can now be specified as a list of strings, instead of as a single string (https://github.com/ansible-collections/community.general/issues/3401)." diff --git a/plugins/modules/net_tools/nmcli.py b/plugins/modules/net_tools/nmcli.py index 843e8bd8ef..c6680c3bd3 100644 --- a/plugins/modules/net_tools/nmcli.py +++ b/plugins/modules/net_tools/nmcli.py @@ -100,7 +100,8 @@ options: routing_rules4: description: - Is the same as in an C(ip route add) command, except always requires specifying a priority. - type: str + type: list + elements: str version_added: 3.3.0 never_default4: description: @@ -1470,6 +1471,7 @@ class Nmcli(object): elif setting in ('ipv4.dns', 'ipv4.dns-search', 'ipv4.routes', + 'ipv4.routing-rules', 'ipv4.route-metric' 'ipv6.dns', 'ipv6.dns-search', @@ -1758,7 +1760,7 @@ def main(): gw4_ignore_auto=dict(type='bool', default=False), routes4=dict(type='list', elements='str'), route_metric4=dict(type='int'), - routing_rules4=dict(type='str'), + routing_rules4=dict(type='list', elements='str'), never_default4=dict(type='bool', default=False), dns4=dict(type='list', elements='str'), dns4_search=dict(type='list', elements='str'), diff --git a/tests/unit/plugins/modules/net_tools/test_nmcli.py b/tests/unit/plugins/modules/net_tools/test_nmcli.py index bf2977e81d..33954e594d 100644 --- a/tests/unit/plugins/modules/net_tools/test_nmcli.py +++ b/tests/unit/plugins/modules/net_tools/test_nmcli.py @@ -122,6 +122,37 @@ ipv6.ignore-auto-dns: no ipv6.ignore-auto-routes: no """ +TESTCASE_GENERIC_MODIFY_ROUTING_RULES = [ + { + 'type': 'generic', + 'conn_name': 'non_existent_nw_device', + 'ifname': 'generic_non_existant', + 'ip4': '10.10.10.10/24', + 'gw4': '10.10.10.1', + 'routing_rules4': ['priority 5 from 10.0.0.0/24 table 5000', 'priority 10 from 10.0.1.0/24 table 5001'], + 'state': 'present', + '_ansible_check_mode': False, + }, +] + +TESTCASE_GENERIC_MODIFY_ROUTING_RULES_SHOW_OUTPUT = """\ +connection.id: non_existent_nw_device +connection.interface-name: generic_non_existant +connection.autoconnect: yes +ipv4.method: manual +ipv4.addresses: 10.10.10.10/24 +ipv4.gateway: 10.10.10.1 +ipv4.routing-rules: priority 5 from 10.0.0.0/24 table 5000, priority 10 from 10.0.1.0/24 table 5001 +ipv4.ignore-auto-dns: no +ipv4.ignore-auto-routes: no +ipv4.never-default: no +ipv4.may-fail: yes +ipv6.method: auto +ipv6.ignore-auto-dns: no +ipv6.ignore-auto-routes: no +""" + + TESTCASE_GENERIC_DNS4_SEARCH = [ { 'type': 'generic', @@ -738,6 +769,13 @@ def mocked_generic_connection_unchanged(mocker): execute_return=(0, TESTCASE_GENERIC_SHOW_OUTPUT, "")) +@pytest.fixture +def mocked_generic_connection_unchanged(mocker): + mocker_set(mocker, + connection_exists=True, + execute_return=(0, TESTCASE_GENERIC_MODIFY_ROUTING_RULES_SHOW_OUTPUT, "")) + + @pytest.fixture def mocked_generic_connection_dns_search_unchanged(mocker): mocker_set(mocker, @@ -1038,6 +1076,26 @@ def test_generic_connection_unchanged(mocked_generic_connection_unchanged, capfd assert not results['changed'] +@pytest.mark.parametrize('patch_ansible_module', TESTCASE_GENERIC_MODIFY_ROUTING_RULES, indirect=['patch_ansible_module']) +def test_generic_connection_modify_routing_rules4(mocked_generic_connection_create, capfd): + """ + Test : Generic connection modified with routing-rules4 + """ + 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 'ipv4.routing-rules' 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_GENERIC_DNS4_SEARCH, indirect=['patch_ansible_module']) def test_generic_connection_create_dns_search(mocked_generic_connection_create, capfd): """