From 53fead7c9680d95a50905584307d72d976596e4b Mon Sep 17 00:00:00 2001 From: Paul Neumann Date: Tue, 31 Oct 2017 04:55:07 +0100 Subject: [PATCH] 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 --- .../modules/network/ios/ios_logging.py | 23 ++++---- .../ios/fixtures/ios_logging_config.cfg | 10 ++++ .../modules/network/ios/test_ios_logging.py | 58 +++++++++++++++++++ 3 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 test/units/modules/network/ios/fixtures/ios_logging_config.cfg create mode 100644 test/units/modules/network/ios/test_ios_logging.py diff --git a/lib/ansible/modules/network/ios/ios_logging.py b/lib/ansible/modules/network/ios/ios_logging.py index 47ef5e7485..3ce53f563d 100644 --- a/lib/ansible/modules/network/ios/ios_logging.py +++ b/lib/ansible/modules/network/ios/ios_logging.py @@ -38,7 +38,7 @@ options: dest: description: - Destination of the logs. - choices: ['on', 'host', console', 'monitor', 'buffered'] + choices: ['on', 'host', 'console', 'monitor', 'buffered'] name: description: - If value of C(dest) is I(file) it indicates file-name, @@ -48,6 +48,7 @@ options: description: - Size of buffer. The acceptable value is in range from 4096 to 4294967295 bytes. + default: 4096 facility: description: - Set logging facility. @@ -128,7 +129,7 @@ from ansible.module_utils.ios import ios_argument_spec, check_args def validate_size(value, module): 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') else: return value @@ -148,32 +149,32 @@ def map_obj_to_commands(updates, module): if state == 'absent' and w in have: if dest == 'host': - commands.append('no logging host {}'.format(name)) + commands.append('no logging host {0}'.format(name)) elif dest: - commands.append('no logging {}'.format(dest)) + commands.append('no logging {0}'.format(dest)) else: module.fail_json(msg='dest must be among console, monitor, buffered, host, on') 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 facility: - commands.append('logging facility {}'.format(facility)) + commands.append('logging facility {0}'.format(facility)) if dest == 'host': - commands.append('logging host {}'.format(name)) + commands.append('logging host {0}'.format(name)) elif dest == 'on': commands.append('logging on') elif dest == 'buffered' and size: - commands.append('logging buffered {}'.format(size)) + commands.append('logging buffered {0}'.format(size)) else: - dest_cmd = 'logging {}'.format(dest) + dest_cmd = 'logging {0}'.format(dest) if level: - dest_cmd += ' {}'.format(level) + dest_cmd += ' {0}'.format(level) commands.append(dest_cmd) return commands @@ -228,7 +229,7 @@ def parse_level(line, dest): level = 'debugging' 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.group(1) in level_group: level = match.group(1) diff --git a/test/units/modules/network/ios/fixtures/ios_logging_config.cfg b/test/units/modules/network/ios/fixtures/ios_logging_config.cfg new file mode 100644 index 0000000000..88ea910352 --- /dev/null +++ b/test/units/modules/network/ios/fixtures/ios_logging_config.cfg @@ -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 +! diff --git a/test/units/modules/network/ios/test_ios_logging.py b/test/units/modules/network/ios/test_ios_logging.py new file mode 100644 index 0000000000..7138a0cd8d --- /dev/null +++ b/test/units/modules/network/ios/test_ios_logging.py @@ -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 . + +# 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)