2017-01-09 17:19:25 +01:00
|
|
|
#
|
|
|
|
# (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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2018-10-13 05:01:14 +02:00
|
|
|
from units.compat.mock import patch, MagicMock
|
2017-01-09 17:19:25 +01:00
|
|
|
from ansible.modules.network.ios import ios_config
|
2018-06-06 07:42:45 +02:00
|
|
|
from ansible.plugins.cliconf.ios import Cliconf
|
2017-11-17 18:17:07 +01:00
|
|
|
from units.modules.utils import set_module_args
|
|
|
|
from .ios_module import TestIosModule, load_fixture
|
2017-01-09 17:19:25 +01:00
|
|
|
|
|
|
|
|
2017-02-13 16:22:14 +01:00
|
|
|
class TestIosConfigModule(TestIosModule):
|
2017-01-09 17:19:25 +01:00
|
|
|
|
2017-02-13 16:22:14 +01:00
|
|
|
module = ios_config
|
2017-01-09 17:19:25 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
2017-11-17 18:17:07 +01:00
|
|
|
super(TestIosConfigModule, self).setUp()
|
|
|
|
|
2017-01-09 17:19:25 +01:00
|
|
|
self.mock_get_config = patch('ansible.modules.network.ios.ios_config.get_config')
|
|
|
|
self.get_config = self.mock_get_config.start()
|
|
|
|
|
2018-06-06 07:42:45 +02:00
|
|
|
self.mock_get_connection = patch('ansible.modules.network.ios.ios_config.get_connection')
|
|
|
|
self.get_connection = self.mock_get_connection.start()
|
|
|
|
|
|
|
|
self.conn = self.get_connection()
|
|
|
|
self.conn.edit_config = MagicMock()
|
2017-01-09 17:19:25 +01:00
|
|
|
|
|
|
|
self.mock_run_commands = patch('ansible.modules.network.ios.ios_config.run_commands')
|
|
|
|
self.run_commands = self.mock_run_commands.start()
|
|
|
|
|
2018-06-06 07:42:45 +02:00
|
|
|
self.cliconf_obj = Cliconf(MagicMock())
|
|
|
|
self.running_config = load_fixture('ios_config_config.cfg')
|
|
|
|
|
2017-01-09 17:19:25 +01:00
|
|
|
def tearDown(self):
|
2017-11-17 18:17:07 +01:00
|
|
|
super(TestIosConfigModule, self).tearDown()
|
2017-01-09 17:19:25 +01:00
|
|
|
self.mock_get_config.stop()
|
|
|
|
self.mock_run_commands.stop()
|
2018-06-06 07:42:45 +02:00
|
|
|
self.mock_get_connection.stop()
|
2017-01-09 17:19:25 +01:00
|
|
|
|
2017-02-13 16:22:14 +01:00
|
|
|
def load_fixtures(self, commands=None):
|
|
|
|
config_file = 'ios_config_config.cfg'
|
2017-01-09 17:19:25 +01:00
|
|
|
self.get_config.return_value = load_fixture(config_file)
|
2018-06-06 07:42:45 +02:00
|
|
|
self.get_connection.edit_config.return_value = None
|
2017-01-09 17:19:25 +01:00
|
|
|
|
|
|
|
def test_ios_config_unchanged(self):
|
|
|
|
src = load_fixture('ios_config_config.cfg')
|
2018-06-06 07:42:45 +02:00
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff(src, src))
|
2017-01-09 17:19:25 +01:00
|
|
|
set_module_args(dict(src=src))
|
|
|
|
self.execute_module()
|
|
|
|
|
|
|
|
def test_ios_config_src(self):
|
|
|
|
src = load_fixture('ios_config_src.cfg')
|
|
|
|
set_module_args(dict(src=src))
|
2018-06-06 07:42:45 +02:00
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff(src, self.running_config))
|
2017-01-09 17:19:25 +01:00
|
|
|
commands = ['hostname foo', 'interface GigabitEthernet0/0',
|
|
|
|
'no ip address']
|
|
|
|
self.execute_module(changed=True, commands=commands)
|
|
|
|
|
|
|
|
def test_ios_config_backup(self):
|
|
|
|
set_module_args(dict(backup=True))
|
|
|
|
result = self.execute_module()
|
|
|
|
self.assertIn('__backup__', result)
|
|
|
|
|
2017-12-13 15:36:43 +01:00
|
|
|
def test_ios_config_save_changed_true(self):
|
|
|
|
src = load_fixture('ios_config_src.cfg')
|
|
|
|
set_module_args(dict(src=src, save_when='changed'))
|
|
|
|
commands = ['hostname foo', 'interface GigabitEthernet0/0', 'no ip address']
|
2018-06-06 07:42:45 +02:00
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff(src, self.running_config))
|
2017-12-13 15:36:43 +01:00
|
|
|
self.execute_module(changed=True, commands=commands)
|
|
|
|
self.assertEqual(self.run_commands.call_count, 1)
|
|
|
|
self.assertEqual(self.get_config.call_count, 1)
|
2018-06-06 07:42:45 +02:00
|
|
|
self.assertEqual(self.conn.edit_config.call_count, 1)
|
2017-12-13 15:36:43 +01:00
|
|
|
args = self.run_commands.call_args[0][1]
|
|
|
|
self.assertIn('copy running-config startup-config\r', args)
|
|
|
|
|
2018-02-02 05:27:49 +01:00
|
|
|
def test_ios_config_save_changed_false(self):
|
2017-12-13 15:36:43 +01:00
|
|
|
set_module_args(dict(save_when='changed'))
|
|
|
|
self.execute_module(changed=False)
|
|
|
|
self.assertEqual(self.run_commands.call_count, 0)
|
|
|
|
self.assertEqual(self.get_config.call_count, 0)
|
2018-06-06 07:42:45 +02:00
|
|
|
self.assertEqual(self.conn.edit_config.call_count, 0)
|
2017-12-13 15:36:43 +01:00
|
|
|
|
2019-01-22 15:18:26 +01:00
|
|
|
def test_ios_config_save_always(self):
|
2017-12-13 15:36:43 +01:00
|
|
|
self.run_commands.return_value = "hostname foo"
|
2019-01-22 15:18:26 +01:00
|
|
|
set_module_args(dict(save_when='always'))
|
2017-12-13 15:36:43 +01:00
|
|
|
self.execute_module(changed=True)
|
|
|
|
self.assertEqual(self.run_commands.call_count, 1)
|
2017-01-09 17:19:25 +01:00
|
|
|
self.assertEqual(self.get_config.call_count, 0)
|
2018-06-06 07:42:45 +02:00
|
|
|
self.assertEqual(self.conn.edit_config.call_count, 0)
|
2017-01-09 17:19:25 +01:00
|
|
|
args = self.run_commands.call_args[0][1]
|
2017-11-09 18:13:49 +01:00
|
|
|
self.assertIn('copy running-config startup-config\r', args)
|
2017-01-09 17:19:25 +01:00
|
|
|
|
|
|
|
def test_ios_config_lines_wo_parents(self):
|
2018-06-06 07:42:45 +02:00
|
|
|
lines = ['hostname foo']
|
|
|
|
set_module_args(dict(lines=lines))
|
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff('\n'.join(lines), self.running_config))
|
2017-01-09 17:19:25 +01:00
|
|
|
commands = ['hostname foo']
|
|
|
|
self.execute_module(changed=True, commands=commands)
|
|
|
|
|
|
|
|
def test_ios_config_lines_w_parents(self):
|
2018-06-06 07:42:45 +02:00
|
|
|
lines = ['shutdown']
|
|
|
|
parents = ['interface GigabitEthernet0/0']
|
|
|
|
set_module_args(dict(lines=lines, parents=parents))
|
|
|
|
module = MagicMock()
|
|
|
|
module.params = {'lines': lines, 'parents': parents, 'src': None}
|
|
|
|
candidate_config = ios_config.get_candidate_config(module)
|
|
|
|
|
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff(candidate_config, self.running_config))
|
|
|
|
|
2017-01-09 17:19:25 +01:00
|
|
|
commands = ['interface GigabitEthernet0/0', 'shutdown']
|
|
|
|
self.execute_module(changed=True, commands=commands)
|
|
|
|
|
|
|
|
def test_ios_config_before(self):
|
2018-06-06 07:42:45 +02:00
|
|
|
lines = ['hostname foo']
|
|
|
|
set_module_args(dict(lines=lines, before=['test1', 'test2']))
|
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff('\n'.join(lines), self.running_config))
|
2017-01-09 17:19:25 +01:00
|
|
|
commands = ['test1', 'test2', 'hostname foo']
|
|
|
|
self.execute_module(changed=True, commands=commands, sort=False)
|
|
|
|
|
|
|
|
def test_ios_config_after(self):
|
2018-06-06 07:42:45 +02:00
|
|
|
lines = ['hostname foo']
|
|
|
|
set_module_args(dict(lines=lines, after=['test1', 'test2']))
|
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff('\n'.join(lines), self.running_config))
|
2017-01-09 17:19:25 +01:00
|
|
|
commands = ['hostname foo', 'test1', 'test2']
|
|
|
|
self.execute_module(changed=True, commands=commands, sort=False)
|
|
|
|
|
2017-01-24 17:16:16 +01:00
|
|
|
def test_ios_config_before_after_no_change(self):
|
2018-06-06 07:42:45 +02:00
|
|
|
lines = ['hostname router']
|
|
|
|
set_module_args(dict(lines=lines,
|
2017-01-09 17:19:25 +01:00
|
|
|
before=['test1', 'test2'],
|
2017-05-30 19:05:19 +02:00
|
|
|
after=['test3', 'test4']))
|
2018-06-06 07:42:45 +02:00
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff('\n'.join(lines), self.running_config))
|
2017-01-09 17:19:25 +01:00
|
|
|
self.execute_module()
|
|
|
|
|
|
|
|
def test_ios_config_config(self):
|
|
|
|
config = 'hostname localhost'
|
2018-06-06 07:42:45 +02:00
|
|
|
lines = ['hostname router']
|
|
|
|
set_module_args(dict(lines=lines, config=config))
|
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff('\n'.join(lines), config))
|
2017-01-09 17:19:25 +01:00
|
|
|
commands = ['hostname router']
|
|
|
|
self.execute_module(changed=True, commands=commands)
|
|
|
|
|
|
|
|
def test_ios_config_replace_block(self):
|
|
|
|
lines = ['description test string', 'test string']
|
|
|
|
parents = ['interface GigabitEthernet0/0']
|
|
|
|
set_module_args(dict(lines=lines, replace='block', parents=parents))
|
2018-06-06 07:42:45 +02:00
|
|
|
|
|
|
|
module = MagicMock()
|
|
|
|
module.params = {'lines': lines, 'parents': parents, 'src': None}
|
|
|
|
candidate_config = ios_config.get_candidate_config(module)
|
|
|
|
|
2018-07-27 07:35:40 +02:00
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff(candidate_config, self.running_config, diff_replace='block', path=parents))
|
2018-06-06 07:42:45 +02:00
|
|
|
|
2017-01-09 17:19:25 +01:00
|
|
|
commands = parents + lines
|
|
|
|
self.execute_module(changed=True, commands=commands)
|
|
|
|
|
2017-07-12 02:34:20 +02:00
|
|
|
def test_ios_config_match_none(self):
|
2017-01-09 17:19:25 +01:00
|
|
|
lines = ['hostname router']
|
2017-07-12 02:34:20 +02:00
|
|
|
set_module_args(dict(lines=lines, match='none'))
|
2018-07-27 07:35:40 +02:00
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff('\n'.join(lines), self.running_config, diff_match='none'))
|
2017-01-09 17:19:25 +01:00
|
|
|
self.execute_module(changed=True, commands=lines)
|
|
|
|
|
2019-12-13 01:26:54 +01:00
|
|
|
def test_ios_config_match_none2(self):
|
2017-01-09 17:19:25 +01:00
|
|
|
lines = ['ip address 1.2.3.4 255.255.255.0', 'description test string']
|
|
|
|
parents = ['interface GigabitEthernet0/0']
|
|
|
|
set_module_args(dict(lines=lines, parents=parents, match='none'))
|
2018-06-06 07:42:45 +02:00
|
|
|
|
|
|
|
module = MagicMock()
|
|
|
|
module.params = {'lines': lines, 'parents': parents, 'src': None}
|
|
|
|
candidate_config = ios_config.get_candidate_config(module)
|
2018-07-27 07:35:40 +02:00
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff(candidate_config, self.running_config, diff_match='none', path=parents))
|
2018-06-06 07:42:45 +02:00
|
|
|
|
2017-01-09 17:19:25 +01:00
|
|
|
commands = parents + lines
|
|
|
|
self.execute_module(changed=True, commands=commands, sort=False)
|
|
|
|
|
|
|
|
def test_ios_config_match_strict(self):
|
|
|
|
lines = ['ip address 1.2.3.4 255.255.255.0', 'description test string',
|
|
|
|
'shutdown']
|
|
|
|
parents = ['interface GigabitEthernet0/0']
|
|
|
|
set_module_args(dict(lines=lines, parents=parents, match='strict'))
|
2018-06-06 07:42:45 +02:00
|
|
|
|
|
|
|
module = MagicMock()
|
|
|
|
module.params = {'lines': lines, 'parents': parents, 'src': None}
|
|
|
|
candidate_config = ios_config.get_candidate_config(module)
|
2018-07-27 07:35:40 +02:00
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff(candidate_config, self.running_config, diff_match='strict', path=parents))
|
2018-06-06 07:42:45 +02:00
|
|
|
|
2017-01-09 17:19:25 +01:00
|
|
|
commands = parents + ['shutdown']
|
|
|
|
self.execute_module(changed=True, commands=commands, sort=False)
|
|
|
|
|
|
|
|
def test_ios_config_match_exact(self):
|
|
|
|
lines = ['ip address 1.2.3.4 255.255.255.0', 'description test string',
|
|
|
|
'shutdown']
|
|
|
|
parents = ['interface GigabitEthernet0/0']
|
|
|
|
set_module_args(dict(lines=lines, parents=parents, match='exact'))
|
2018-06-06 07:42:45 +02:00
|
|
|
|
|
|
|
module = MagicMock()
|
|
|
|
module.params = {'lines': lines, 'parents': parents, 'src': None}
|
|
|
|
candidate_config = ios_config.get_candidate_config(module)
|
2018-07-27 07:35:40 +02:00
|
|
|
self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff(candidate_config, self.running_config, diff_match='exact', path=parents))
|
2018-06-06 07:42:45 +02:00
|
|
|
|
2017-01-09 17:19:25 +01:00
|
|
|
commands = parents + lines
|
|
|
|
self.execute_module(changed=True, commands=commands, sort=False)
|
2018-01-03 15:47:12 +01:00
|
|
|
|
|
|
|
def test_ios_config_src_and_lines_fails(self):
|
|
|
|
args = dict(src='foo', lines='foo')
|
|
|
|
set_module_args(args)
|
2019-12-13 01:26:54 +01:00
|
|
|
self.execute_module(failed=True)
|
2018-01-03 15:47:12 +01:00
|
|
|
|
|
|
|
def test_ios_config_src_and_parents_fails(self):
|
|
|
|
args = dict(src='foo', parents='foo')
|
|
|
|
set_module_args(args)
|
2019-12-13 01:26:54 +01:00
|
|
|
self.execute_module(failed=True)
|
2018-01-03 15:47:12 +01:00
|
|
|
|
|
|
|
def test_ios_config_match_exact_requires_lines(self):
|
|
|
|
args = dict(match='exact')
|
|
|
|
set_module_args(args)
|
2019-12-13 01:26:54 +01:00
|
|
|
self.execute_module(failed=True)
|
2018-01-03 15:47:12 +01:00
|
|
|
|
|
|
|
def test_ios_config_match_strict_requires_lines(self):
|
|
|
|
args = dict(match='strict')
|
|
|
|
set_module_args(args)
|
2019-12-13 01:26:54 +01:00
|
|
|
self.execute_module(failed=True)
|
2018-01-03 15:47:12 +01:00
|
|
|
|
|
|
|
def test_ios_config_replace_block_requires_lines(self):
|
|
|
|
args = dict(replace='block')
|
|
|
|
set_module_args(args)
|
2019-12-13 01:26:54 +01:00
|
|
|
self.execute_module(failed=True)
|
2018-01-03 15:47:12 +01:00
|
|
|
|
|
|
|
def test_ios_config_replace_config_requires_src(self):
|
|
|
|
args = dict(replace='config')
|
|
|
|
set_module_args(args)
|
2019-12-13 01:26:54 +01:00
|
|
|
self.execute_module(failed=True)
|