Fix to - eos_vlans using state parameter replaced is giving odd behavior (#67346)

* backport PR 67318

* added changelog file

* Added missing fixture file
pull/67619/head
GomathiselviS 5 years ago committed by GitHub
parent 7cac554222
commit 48bfa3e906
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- fixes the eos_vlans repalced state behavior to configure the 'name' parameter(https://github.com/ansible/ansible/pull/67318)

@ -208,16 +208,17 @@ def generate_commands(vlan_id, to_set, to_remove):
if "vlan_id" in to_remove:
return ["no vlan {0}".format(vlan_id)]
for key in to_remove:
if key in to_set.keys():
continue
commands.append("no {0}".format(key))
for key, value in to_set.items():
if key == "vlan_id" or value is None:
continue
commands.append("{0} {1}".format(key, value))
for key in to_remove:
commands.append("no {0}".format(key))
if commands:
commands.insert(0, "vlan {0}".format(vlan_id))
return commands

@ -5,6 +5,7 @@
config:
- vlan_id: 20
state: suspend
name: twentyreplaced
other_config:
- vlan_id: 10
name: ten

@ -0,0 +1,172 @@
#
# (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_vlans
from units.modules.utils import set_module_args
from .eos_module import TestEosModule, load_fixture
class TestEosVlansModule(TestEosModule):
module = eos_vlans
def setUp(self):
super(TestEosVlansModule, 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.config.vlans.vlans.Vlans.get_vlans_facts')
self.execute_show_command = self.mock_execute_show_command.start()
def tearDown(self):
super(TestEosVlansModule, 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'):
file_cmd = load_fixture('eos_vlan_config.cfg').split()
file_cmd_dict = {}
for i in range(0, len(file_cmd), 2):
if file_cmd[i] == 'vlan_id':
y = int(file_cmd[i + 1])
else:
y = file_cmd[i + 1]
file_cmd_dict.update({file_cmd[i]: y})
self.execute_show_command.return_value = [file_cmd_dict]
def test_eos_vlan_default(self):
self.execute_show_command.return_value = []
set_module_args(dict(
config=[dict(
vlan_id=30,
name="thirty"
)]
))
commands = ['vlan 30', 'name thirty']
self.execute_module(changed=True, commands=commands)
def test_eos_vlan_default_idempotent(self):
self.execute_show_command.return_value = load_fixture('eos_vlan_config.cfg')
set_module_args(dict(
config=[dict(
vlan_id=10,
name="ten"
)]
))
self.execute_module(changed=False, commands=[])
def test_eos_vlan_merged(self):
self.execute_show_command.return_value = []
set_module_args(dict(
config=[dict(
vlan_id=30,
name="thirty"
)], state="merged"
))
commands = ['vlan 30', 'name thirty']
self.execute_module(changed=True, commands=commands)
def test_eos_vlan_merged_idempotent(self):
self.execute_show_command.return_value = load_fixture('eos_vlan_config.cfg')
set_module_args(dict(
config=[dict(
vlan_id=10,
name="ten"
)], state="merged"
))
self.execute_module(changed=False, commands=[])
def test_eos_vlan_replaced(self):
self.execute_show_command.return_value = []
set_module_args(dict(
config=[dict(
vlan_id=10,
name="tenreplaced",
state="suspend"
)], state="replaced"
))
commands = ['vlan 10', 'name tenreplaced', 'state suspend']
self.execute_module(changed=True, commands=commands)
def test_eos_vlan_replaced_idempotent(self):
self.execute_show_command.return_value = load_fixture('eos_vlan_config.cfg')
set_module_args(dict(
config=[dict(
vlan_id=10,
name="ten"
)], state="replaced"
))
self.execute_module(changed=False, commands=[])
def test_eos_vlan_overridden(self):
self.execute_show_command.return_value = []
set_module_args(dict(
config=[dict(
vlan_id=30,
name="thirty",
state="suspend"
)], state="overridden"
))
commands = ['no vlan 10', 'vlan 30', 'name thirty', 'state suspend']
self.execute_module(changed=True, commands=commands)
def test_eos_vlan_overridden_idempotent(self):
self.execute_show_command.return_value = load_fixture('eos_vlan_config.cfg')
set_module_args(dict(
config=[dict(
vlan_id=10,
name="ten"
)], state="overridden"
))
self.execute_module(changed=False, commands=[])
def test_eos_vlan_deleted(self):
set_module_args(dict(
config=[dict(
vlan_id=10,
name="ten",
)], state="deleted"
))
commands = ['no vlan 10']
self.execute_module(changed=True, commands=commands)
def test_eos_vlan_id_datatype(self):
set_module_args(dict(
config=[dict(
vlan_id="thirty"
)]
))
result = self.execute_module(failed=True)
self.assertIn("we were unable to convert to int", result['msg'])
def test_eos_vlan_state_datatype(self):
set_module_args(dict(
config=[dict(
vlan_id=30,
state=10
)]
))
result = self.execute_module(failed=True)
self.assertIn("value of state must be one of: active, suspend", result['msg'])
Loading…
Cancel
Save