From b475e0408c820e8b28cfc9aaf508e15761af0617 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Wed, 6 Nov 2019 19:02:12 -0500 Subject: [PATCH] eos_lacp: Add unit testcases to eos_lacp (#64417) * Added unit testcases eos_lacp * Added unit testcases eos_lacp --- .../network/eos/facts/lacp/lacp.py | 5 +- .../network/eos/fixtures/eos_lacp_config.cfg | 1 + .../modules/network/eos/test_eos_lacp.py | 106 ++++++++++++++++++ 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 test/units/modules/network/eos/fixtures/eos_lacp_config.cfg create mode 100644 test/units/modules/network/eos/test_eos_lacp.py diff --git a/lib/ansible/module_utils/network/eos/facts/lacp/lacp.py b/lib/ansible/module_utils/network/eos/facts/lacp/lacp.py index 720254787d3..370cb9df67d 100644 --- a/lib/ansible/module_utils/network/eos/facts/lacp/lacp.py +++ b/lib/ansible/module_utils/network/eos/facts/lacp/lacp.py @@ -37,6 +37,9 @@ class LacpFacts(object): self.generated_spec = utils.generate_dict(facts_argument_spec) + def get_device_data(self, connection): + return connection.get('show running-config | section ^lacp') + def populate_facts(self, connection, ansible_facts, data=None): """ Populate the facts for lacp :param connection: the device connection @@ -46,7 +49,7 @@ class LacpFacts(object): :returns: facts """ if not data: - data = connection.get('show running-config | section ^lacp') + data = self.get_device_data(connection) # split the config into instances of the resource resource_delim = 'lacp' diff --git a/test/units/modules/network/eos/fixtures/eos_lacp_config.cfg b/test/units/modules/network/eos/fixtures/eos_lacp_config.cfg new file mode 100644 index 00000000000..729ece989a9 --- /dev/null +++ b/test/units/modules/network/eos/fixtures/eos_lacp_config.cfg @@ -0,0 +1 @@ +lacp system-priority 10 diff --git a/test/units/modules/network/eos/test_eos_lacp.py b/test/units/modules/network/eos/test_eos_lacp.py new file mode 100644 index 00000000000..ce19e66ccfc --- /dev/null +++ b/test/units/modules/network/eos/test_eos_lacp.py @@ -0,0 +1,106 @@ +# (c) 2019, Ansible by Red Hat, inc +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +# + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from units.compat.mock import patch +from ansible.modules.network.eos import eos_lacp +from units.modules.utils import set_module_args +from .eos_module import TestEosModule, load_fixture + + +class TestEosLacpInterfacesModule(TestEosModule): + module = eos_lacp + + def setUp(self): + super(TestEosLacpInterfacesModule, self).setUp() + + self.mock_get_config = patch('ansible.module_utils.network.common.network.Config.get_config') + self.get_config = self.mock_get_config.start() + + self.mock_load_config = patch('ansible.module_utils.network.common.network.Config.load_config') + self.load_config = self.mock_load_config.start() + + self.mock_get_resource_connection_config = patch('ansible.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.module_utils.network.common.facts.facts.get_resource_connection') + self.get_resource_connection_facts = self.mock_get_resource_connection_facts.start() + + self.mock_edit_config = patch('ansible.module_utils.network.eos.providers.providers.CliProvider.edit_config') + self.edit_config = self.mock_edit_config.start() + + self.mock_execute_show_command = patch('ansible.module_utils.network.eos.facts.lacp.lacp.LacpFacts.get_device_data') + self.execute_show_command = self.mock_execute_show_command.start() + + def tearDown(self): + super(TestEosLacpInterfacesModule, self).tearDown() + self.mock_get_resource_connection_config.stop() + self.mock_get_resource_connection_facts.stop() + self.mock_edit_config.stop() + self.mock_get_config.stop() + self.mock_load_config.stop() + self.mock_execute_show_command.stop() + + def load_fixtures(self, commands=None, transport='cli'): + def load_from_file(*args, **kwargs): + return load_fixture('eos_lacp_config.cfg') + self.execute_show_command.side_effect = load_from_file + + def test_eos_lacp_default(self): + set_module_args(dict( + config=dict( + system=dict(priority=50) + ) + )) + commands = ['lacp system-priority 50'] + self.execute_module(changed=True, commands=commands) + + def test_eos_lacp_default_idempotent(self): + set_module_args(dict( + config=dict( + system=dict(priority=10) + ) + )) + self.execute_module(changed=False, commands=[]) + + def test_eos_lacp_merged(self): + set_module_args(dict( + config=dict( + system=dict(priority=50) + ), state="merged" + )) + commands = ['lacp system-priority 50'] + self.execute_module(changed=True, commands=commands) + + def test_eos_lacp_merged_idempotent(self): + set_module_args(dict( + config=dict( + system=dict(priority=10) + ), state="merged" + )) + self.execute_module(changed=False, commands=[]) + + def test_eos_l3_interfaces_replaced(self): + set_module_args(dict( + config=dict( + system=dict(priority=20) + ), state="replaced" + )) + commands = ['lacp system-priority 20'] + self.execute_module(changed=True, commands=commands) + + def test_eos_l3_interfaces_replaced_idempotent(self): + set_module_args(dict( + config=dict( + system=dict(priority=10) + ), state="replaced" + )) + self.execute_module(changed=False, commands=[]) + + def test_eos_l3_interfaces_deleted(self): + set_module_args(dict(state="deleted")) + commands = ['no lacp system-priority'] + self.execute_module(changed=True, commands=commands)