Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions lib/ansible/modules/network/ios/ios_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
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,
Expand All @@ -48,6 +48,7 @@
description:
- Size of buffer. The acceptable value is in range from 4096 to
4294967295 bytes.
default: 4096
facility:
description:
- Set logging facility.
Expand Down Expand Up @@ -128,7 +129,7 @@

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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/units/modules/network/ios/fixtures/ios_logging_config.cfg
Original file line number Diff line number Diff line change
@@ -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
!
58 changes: 58 additions & 0 deletions test/units/modules/network/ios/test_ios_logging.py
Original file line number Diff line number Diff line change
@@ -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)