ansible-collections / cisco.nxos

Ansible Network Collection for Cisco NXOS
GNU General Public License v3.0
114 stars 105 forks source link

[route_maps] Enable handling of route-maps without set/match #856

Closed Miyoshi-Ryota closed 1 month ago

Miyoshi-Ryota commented 2 months ago
SUMMARY

This commit introduces the ability to handle simple route-maps that do not contain set or match statements.

It allows for the creation and management of purely basic route-map entries like 'route-map test-1 permit 10'.

ISSUE TYPE
COMPONENT NAME

cisco.nxos.nxos_route_maps

ADDITIONAL INFORMATION

cisco.nxos.nxos_route_maps module cannot handle following task which does not have a set, a match and a description section for now.

See also unit tests.

- name: Sample Task
  cisco.nxos.nxos_route_maps:
    config:
      - route_map: rmap1
        entries:
          - sequence: 10
            action: permit
    state: merged
pytest result after editing source code.
% pytest tests/unit/modules/network/nxos/test_nxos_route_maps.py 
========================================================== test session starts ==========================================================
platform darwin -- Python 3.10.14, pytest-8.1.1, pluggy-1.4.0
ansible: 2.16.5
rootdir: /Users/me/Documents/programming/PycharmProjects/cisco.nxos
configfile: pyproject.toml
plugins: ansible-24.1.2, xdist-3.5.0
collected 21 items                                                                                                                      

tests/unit/modules/network/nxos/test_nxos_route_maps.py .....................                                                     [100%]

========================================================== 21 passed in 0.82s ===========================================================
pytest result when just adding tests and without editing source code.
% pytest tests/unit/modules/network/nxos/test_nxos_route_maps.py
========================================================== test session starts ==========================================================
platform darwin -- Python 3.10.14, pytest-8.1.1, pluggy-1.4.0
ansible: 2.16.5
rootdir: /Users/me/Documents/programming/PycharmProjects/cisco.nxos
configfile: pyproject.toml
plugins: ansible-24.1.2, xdist-3.5.0
collected 21 items                                                                                                                      

tests/unit/modules/network/nxos/test_nxos_route_maps.py ..................FFF                                                     [100%]

=============================================================== FAILURES ================================================================
_______________________________ TestNxosRouteMapsModule.test_nxos_route_maps_without_match_and_set_merged _______________________________

self = <tests.unit.modules.network.nxos.test_nxos_route_maps.TestNxosRouteMapsModule testMethod=test_nxos_route_maps_without_match_and_set_merged>

    def test_nxos_route_maps_without_match_and_set_merged(self):
        self.get_config.return_value = dedent(
            """\
            route-map test-1 permit 10
            """
        )
        set_module_args(
            dict(
                config=[
                    dict(
                        route_map="test-1",
                        entries=[
                            dict(
                                action="permit",
                                sequence=20,
                            ),
                        ],
                    ),
                ],
                state="merged",
            )
        )
        commands = [
            "route-map test-1 permit 20",
        ]
>       result = self.execute_module(changed=True)

tests/unit/modules/network/nxos/test_nxos_route_maps.py:1522: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tests/unit/modules/network/nxos/nxos_module.py:98: in execute_module
    result = self.changed(changed)
tests/unit/modules/network/nxos/nxos_module.py:124: in changed
    self.assertEqual(result["changed"], changed, result)
E   AssertionError: False != True : {'warnings': [], 'commands': [], 'before': [{'route_map': 'test-1', 'entries': [{'sequence': 10, 'action': 'permit'}]}], 'changed': False}
_____________________________ TestNxosRouteMapsModule.test_nxos_route_maps_without_match_and_set_overridden _____________________________

self = <tests.unit.modules.network.nxos.test_nxos_route_maps.TestNxosRouteMapsModule testMethod=test_nxos_route_maps_without_match_and_set_overridden>

    def test_nxos_route_maps_without_match_and_set_overridden(self):
        self.get_config.return_value = dedent(
            """\
            route-map test-1 permit 10
            """
        )
        set_module_args(
            dict(
                config=[
                    dict(
                        route_map="test-2",
                        entries=[
                            dict(
                                action="permit",
                                sequence=10,
                            ),
                        ],
                    ),
                ],
                state="overridden",
            )
        )
        commands = [
            "no route-map test-1 permit 10",
            "route-map test-2 permit 10",
        ]
        result = self.execute_module(changed=True)
>       self.assertEqual(set(result["commands"]), set(commands))
E       AssertionError: Items in the second set but not the first:
E       'route-map test-2 permit 10'

tests/unit/modules/network/nxos/test_nxos_route_maps.py:1552: AssertionError
______________________________ TestNxosRouteMapsModule.test_nxos_route_maps_without_match_and_set_replaced ______________________________

self = <tests.unit.modules.network.nxos.test_nxos_route_maps.TestNxosRouteMapsModule testMethod=test_nxos_route_maps_without_match_and_set_replaced>

    def test_nxos_route_maps_without_match_and_set_replaced(self):
        self.get_config.return_value = dedent(
            """\
            route-map test-1 permit 10
            route-map test-1 permit 20
            route-map test-2 permit 10
            """
        )
        set_module_args(
            dict(
                config=[
                    dict(
                        route_map="test-1",
                        entries=[
                            dict(
                                action="permit",
                                sequence=30,
                            ),
                        ],
                    ),
                ],
                state="replaced",
            )
        )
        commands = [
            "no route-map test-1 permit 10",
            "no route-map test-1 permit 20",
            "route-map test-1 permit 30",
        ]
        result = self.execute_module(changed=True)
>       self.assertEqual(set(result["commands"]), set(commands))
E       AssertionError: Items in the second set but not the first:
E       'route-map test-1 permit 30'

tests/unit/modules/network/nxos/test_nxos_route_maps.py:1584: AssertionError
======================================================== short test summary info ========================================================
FAILED tests/unit/modules/network/nxos/test_nxos_route_maps.py::TestNxosRouteMapsModule::test_nxos_route_maps_without_match_and_set_merged - AssertionError: False != True : {'warnings': [], 'commands': [], 'before': [{'route_map': 'test-1', 'entries': [{'sequence': 10, 'ac...
FAILED tests/unit/modules/network/nxos/test_nxos_route_maps.py::TestNxosRouteMapsModule::test_nxos_route_maps_without_match_and_set_overridden - AssertionError: Items in the second set but not the first:
FAILED tests/unit/modules/network/nxos/test_nxos_route_maps.py::TestNxosRouteMapsModule::test_nxos_route_maps_without_match_and_set_replaced - AssertionError: Items in the second set but not the first:
===================================================== 3 failed, 18 passed in 0.86s ======================================================
Miyoshi-Ryota commented 2 months ago

Please let me know if I need to do additional work for the review. CI is failing, but I think I cannot add safe to test label by me.

Miyoshi-Ryota commented 2 months ago

Thank you for adding the label. I will fix the ansible-lint error soon.

If I update this PR, the label may come off again. If that's the case, I'm sorry for bothering you twice.

NilashishC commented 2 months ago

@Miyoshi-Ryota this is an excellent PR! Thank you so much.