diff --git a/tests/unit/modules/network/vyos/test_vyos_static_routes14.py b/tests/unit/modules/network/vyos/test_vyos_static_routes14.py index ddac2f2e..0a18accb 100644 --- a/tests/unit/modules/network/vyos/test_vyos_static_routes14.py +++ b/tests/unit/modules/network/vyos/test_vyos_static_routes14.py @@ -1,367 +1,401 @@ # (c) 2016 Red Hat Inc. # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . # Make coding more python3-ish from __future__ import absolute_import, division, print_function __metaclass__ = type from unittest.mock import patch from ansible_collections.vyos.vyos.plugins.modules import vyos_static_routes from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args from .vyos_module import TestVyosModule, load_fixture class TestVyosStaticRoutesModule14(TestVyosModule): module = vyos_static_routes def setUp(self): super(TestVyosStaticRoutesModule14, self).setUp() self.mock_get_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.network.Config.get_config", ) self.get_config = self.mock_get_config.start() self.mock_load_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.network.Config.load_config", ) self.load_config = self.mock_load_config.start() self.mock_get_resource_connection_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.cfg.base.get_resource_connection", ) self.get_resource_connection_config = self.mock_get_resource_connection_config.start() self.mock_get_resource_connection_facts = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.facts.facts.get_resource_connection", ) self.get_resource_connection_facts = self.mock_get_resource_connection_facts.start() self.mock_execute_show_command = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.static_routes.static_routes.Static_routesFacts.get_device_data", ) self.execute_show_command = self.mock_execute_show_command.start() self.mock_get_os_version = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.static_routes.static_routes.get_os_version", ) self.test_version = "1.4" self.get_os_version = self.mock_get_os_version.start() self.get_os_version.return_value = self.test_version def tearDown(self): super(TestVyosStaticRoutesModule14, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_get_resource_connection_facts.stop() self.mock_get_config.stop() self.mock_load_config.stop() self.mock_execute_show_command.stop() def load_fixtures(self, commands=None, filename=None): def load_from_file(*args, **kwargs): return load_fixture(filename or "vyos_static_routes_config.cfg") self.execute_show_command.side_effect = load_from_file def test_vyos_static_routes_merged(self): set_module_args( dict( config=[ dict( address_families=[ dict( afi="ipv4", routes=[ dict( dest="192.0.2.48/28", next_hops=[ dict( forward_router_address="192.0.2.9", admin_distance=10, ), dict( forward_router_address="192.0.2.10", interface="eth0", ), ], ), ], ), ], ), ], state="merged", ), ) commands = [ "set protocols static route 192.0.2.48/28", "set protocols static route 192.0.2.48/28 next-hop '192.0.2.9'", "set protocols static route 192.0.2.48/28 next-hop 192.0.2.9 distance '10'", "set protocols static route 192.0.2.48/28 next-hop '192.0.2.10'", "set protocols static route 192.0.2.48/28 interface 'eth0'", ] self.execute_module(changed=True, commands=commands) def test_vyos_static_routes_merged_idempotent(self): set_module_args( dict( config=[ dict( address_families=[ dict( afi="ipv4", routes=[ dict( dest="192.0.2.32/28", next_hops=[ dict(forward_router_address="192.0.2.9"), dict(forward_router_address="192.0.2.10"), ], ), ], ), ], ), ], state="merged", ), ) self.execute_module(changed=False, commands=[]) def test_vyos_static_routes_replaced(self): set_module_args( dict( config=[ dict( address_families=[ dict( afi="ipv4", routes=[ dict( dest="192.0.2.48/28", next_hops=[ dict( forward_router_address="192.0.2.9", interface="eth0", ), dict( forward_router_address="192.0.2.10", admin_distance=10, ), ], ), ], ), ], ), ], state="replaced", ), ) commands = [ "set protocols static route 192.0.2.48/28", "set protocols static route 192.0.2.48/28 next-hop '192.0.2.9'", "set protocols static route 192.0.2.48/28 interface 'eth0'", "set protocols static route 192.0.2.48/28 next-hop '192.0.2.10'", "set protocols static route 192.0.2.48/28 next-hop 192.0.2.10 distance '10'", ] self.execute_module(changed=True, commands=commands) def test_vyos_static_routes_replaced_idempotent(self): set_module_args( dict( config=[ dict( address_families=[ dict( afi="ipv4", routes=[ dict( dest="192.0.2.32/28", next_hops=[ dict(forward_router_address="192.0.2.9"), dict(forward_router_address="192.0.2.10"), ], ), ], ), ], ), ], state="replaced", ), ) self.execute_module(changed=False, commands=[]) def test_vyos_static_routes_overridden(self): set_module_args( dict( config=[ dict( address_families=[ dict( afi="ipv4", routes=[ dict( dest="192.0.2.48/28", next_hops=[ dict(forward_router_address="192.0.2.9"), dict(forward_router_address="192.0.2.10"), ], ), ], ), ], ), ], state="overridden", ), ) commands = [ "delete protocols static route 192.0.2.32/28", "set protocols static route 192.0.2.48/28", "set protocols static route 192.0.2.48/28 next-hop '192.0.2.9'", "set protocols static route 192.0.2.48/28 next-hop '192.0.2.10'", ] self.execute_module(changed=True, commands=commands) def test_vyos_static_routes_overridden_idempotent(self): set_module_args( dict( config=[ dict( address_families=[ dict( afi="ipv4", routes=[ dict( dest="192.0.2.32/28", next_hops=[ dict(forward_router_address="192.0.2.9"), dict(forward_router_address="192.0.2.10"), ], ), ], ), ], ), ], state="overridden", ), ) self.execute_module(changed=False, commands=[]) def test_vyos_static_routes_deleted(self): set_module_args( dict( config=[dict(address_families=[dict(afi="ipv4")])], state="deleted", ), ) commands = ["delete protocols static route"] self.execute_module(changed=True, commands=commands) def _interface_route(self, next_hops): return [ dict( address_families=[ dict(afi="ipv4", routes=[dict(dest="10.0.0.0/8", next_hops=next_hops)]), ], ), ] def _interface_hops(self): return [ dict(interface="eth0"), dict(interface="eth1", admin_distance=20), dict(interface="eth2", enabled=False), dict(forward_router_address="192.0.2.1", admin_distance=10), ] + def test_vyos_static_routes_gathered_interface_next_hops(self): + set_module_args(dict(state="gathered")) + result = self.execute_module( + changed=False, + filename="vyos_static_routes_interface_config.cfg", + ) + self.assertEqual(result["gathered"], self._interface_route(self._interface_hops())) + + def test_vyos_static_routes_rendered_interface_next_hops(self): + set_module_args( + dict( + config=self._interface_route( + [ + dict(interface="eth3", admin_distance=30), + dict(interface="eth4", enabled=False), + ], + ), + state="rendered", + ), + ) + result = self.execute_module(changed=False) + self.assertEqual( + sorted(result["rendered"]), + sorted( + [ + "set protocols static route 10.0.0.0/8", + "set protocols static route 10.0.0.0/8 interface 'eth3'", + "set protocols static route 10.0.0.0/8 interface eth3 distance '30'", + "set protocols static route 10.0.0.0/8 interface 'eth4'", + "set protocols static route 10.0.0.0/8 interface eth4 'disable'", + ], + ), + ) + def test_vyos_static_routes_parsed_interface_next_hops(self): set_module_args( dict( running_config=load_fixture("vyos_static_routes_interface_config.cfg"), state="parsed", ), ) result = self.execute_module(changed=False) self.assertEqual(result["parsed"], self._interface_route(self._interface_hops())) def test_vyos_static_routes_merged_interface_next_hops_idempotent(self): set_module_args(dict(config=self._interface_route(self._interface_hops()), state="merged")) self.execute_module( changed=False, commands=[], filename="vyos_static_routes_interface_config.cfg", ) def test_vyos_static_routes_merged_interface_next_hops(self): set_module_args( dict( config=self._interface_route( [ dict(interface="eth3", admin_distance=30), dict(interface="eth4", enabled=False), ], ), state="merged", ), ) commands = [ "set protocols static route 10.0.0.0/8 interface 'eth3'", "set protocols static route 10.0.0.0/8 interface eth3 distance '30'", "set protocols static route 10.0.0.0/8 interface 'eth4'", "set protocols static route 10.0.0.0/8 interface eth4 'disable'", ] self.execute_module( changed=True, commands=commands, filename="vyos_static_routes_interface_config.cfg", ) def test_vyos_static_routes_replaced_interface_next_hops(self): set_module_args( dict( config=self._interface_route( [dict(interface="eth0"), dict(interface="eth1", admin_distance=20)], ), state="replaced", ), ) commands = [ "delete protocols static route 10.0.0.0/8 interface 'eth2'", "delete protocols static route 10.0.0.0/8 interface eth2 'disable'", "delete protocols static route 10.0.0.0/8 next-hop '192.0.2.1'", "delete protocols static route 10.0.0.0/8 next-hop 192.0.2.1 distance '10'", ] self.execute_module( changed=True, commands=commands, filename="vyos_static_routes_interface_config.cfg", ) def test_vyos_static_routes_deleted_interface_next_hops(self): set_module_args(dict(config=[dict(address_families=[dict(afi="ipv4")])], state="deleted")) self.execute_module( changed=True, commands=["delete protocols static route"], filename="vyos_static_routes_interface_config.cfg", )