1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

ios_logging: Fix some smaller issues, add unit test (#32321)

* ios_logging: Fix typo in documentation

* ios_logging: Fix traceback when setting buffered destination without size

When the size parameter is not configured while configuring the buffered
destination, a traceback occurs due to the fact that validate_size expects the
parameter to be an int. Explicitely converting value to int makes the
check work for every case.

* ios_logging: Update size parameter documentation

Update the documentation of the size paramter to reflect the current behaviour
of setting a default of 4096 for the buffered dest.

* ios_logging: Add unit test

Add unit test for ios_logging testing the behaviour clarified in the previous
commits.

* ios_logging: Fix python 2.6 compliance
This commit is contained in:
Paul Neumann 2017-10-31 04:55:07 +01:00 committed by Trishna Guha
parent a2e4196c11
commit 53fead7c96
3 changed files with 80 additions and 11 deletions

View file

@ -38,7 +38,7 @@ options:
dest: dest:
description: description:
- Destination of the logs. - Destination of the logs.
choices: ['on', 'host', console', 'monitor', 'buffered'] choices: ['on', 'host', 'console', 'monitor', 'buffered']
name: name:
description: description:
- If value of C(dest) is I(file) it indicates file-name, - If value of C(dest) is I(file) it indicates file-name,
@ -48,6 +48,7 @@ options:
description: description:
- Size of buffer. The acceptable value is in range from 4096 to - Size of buffer. The acceptable value is in range from 4096 to
4294967295 bytes. 4294967295 bytes.
default: 4096
facility: facility:
description: description:
- Set logging facility. - Set logging facility.
@ -128,7 +129,7 @@ from ansible.module_utils.ios import ios_argument_spec, check_args
def validate_size(value, module): def validate_size(value, module):
if value: if value:
if not int(4096) <= value <= int(4294967295): if not int(4096) <= int(value) <= int(4294967295):
module.fail_json(msg='size must be between 4096 and 4294967295') module.fail_json(msg='size must be between 4096 and 4294967295')
else: else:
return value return value
@ -148,32 +149,32 @@ def map_obj_to_commands(updates, module):
if state == 'absent' and w in have: if state == 'absent' and w in have:
if dest == 'host': if dest == 'host':
commands.append('no logging host {}'.format(name)) commands.append('no logging host {0}'.format(name))
elif dest: elif dest:
commands.append('no logging {}'.format(dest)) commands.append('no logging {0}'.format(dest))
else: else:
module.fail_json(msg='dest must be among console, monitor, buffered, host, on') module.fail_json(msg='dest must be among console, monitor, buffered, host, on')
if facility: if facility:
commands.append('no logging facility {}'.format(facility)) commands.append('no logging facility {0}'.format(facility))
if state == 'present' and w not in have: if state == 'present' and w not in have:
if facility: if facility:
commands.append('logging facility {}'.format(facility)) commands.append('logging facility {0}'.format(facility))
if dest == 'host': if dest == 'host':
commands.append('logging host {}'.format(name)) commands.append('logging host {0}'.format(name))
elif dest == 'on': elif dest == 'on':
commands.append('logging on') commands.append('logging on')
elif dest == 'buffered' and size: elif dest == 'buffered' and size:
commands.append('logging buffered {}'.format(size)) commands.append('logging buffered {0}'.format(size))
else: else:
dest_cmd = 'logging {}'.format(dest) dest_cmd = 'logging {0}'.format(dest)
if level: if level:
dest_cmd += ' {}'.format(level) dest_cmd += ' {0}'.format(level)
commands.append(dest_cmd) commands.append(dest_cmd)
return commands return commands
@ -228,7 +229,7 @@ def parse_level(line, dest):
level = 'debugging' level = 'debugging'
else: else:
match = re.search(r'logging {} (\S+)'.format(dest), line, re.M) match = re.search(r'logging {0} (\S+)'.format(dest), line, re.M)
if match: if match:
if match.group(1) in level_group: if match.group(1) in level_group:
level = match.group(1) level = match.group(1)

View file

@ -0,0 +1,10 @@
!
logging buffered 5000
logging console informational
logging facility local0
logging host 1.2.3.4 transport tcp
logging host 1.2.3.4
logging host 2.3.4.5
logging host 1.2.3.4 transport tcp port 1000
logging host 1.2.3.4 transport udp port 1000
!

View file

@ -0,0 +1,58 @@
#
# (c) 2016 Red Hat Inc.
# (c) 2017 Paul Neumann
#
# 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
import json
from ansible.compat.tests.mock import patch
from ansible.modules.network.ios import ios_logging
from .ios_module import TestIosModule, load_fixture, set_module_args
class TestIosLoggingModule(TestIosModule):
module = ios_logging
def setUp(self):
self.mock_get_config = patch('ansible.modules.network.ios.ios_logging.get_config')
self.get_config = self.mock_get_config.start()
self.mock_load_config = patch('ansible.modules.network.ios.ios_logging.load_config')
self.load_config = self.mock_load_config.start()
def tearDown(self):
self.mock_get_config.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
self.get_config.return_value = load_fixture('ios_logging_config.cfg')
self.load_config.return_value = None
def test_ios_logging_buffer_size_changed_implicit(self):
set_module_args(dict(dest='buffered'))
commands = ['logging buffered 4096']
self.execute_module(changed=True, commands=commands)
def test_ios_logging_buffer_size_changed_explicit(self):
set_module_args(dict(dest='buffered', size=6000))
commands = ['logging buffered 6000']
self.execute_module(changed=True, commands=commands)