new module nxos_system (#21627)

* provides declarative config support for nxos system attributes
* adds unit test cases for new module
This commit is contained in:
Peter Sprygada
2017-02-18 18:56:02 -05:00
committed by GitHub
parent 2f10bdf0c7
commit 76c9ad9dfc
5 changed files with 611 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
hostname nxos01
!
no ip domain-lookup
ip domain-name ansible.com
ip domain-list ansible.com
ip domain-list redhat.com
ip name-server 8.8.8.8 172.26.1.1
!
vrf context management
ip domain-name eng.ansible.com
ip domain-list ansible.com
ip domain-list redhat.com
ip name-server 172.26.1.1 8.8.8.8
ip route 172.26.0.0/16 172.26.4.1

View File

@@ -0,0 +1,113 @@
# (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
import os
import json
from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch
from ansible.module_utils import basic
from ansible.module_utils._text import to_bytes
def set_module_args(args):
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def load_fixture(name):
path = os.path.join(fixture_path, name)
if path in fixture_data:
return fixture_data[path]
with open(path) as f:
data = f.read()
try:
data = json.loads(data)
except:
pass
fixture_data[path] = data
return data
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
class TestNxosModule(unittest.TestCase):
def execute_module(self, failed=False, changed=False, commands=None,
sort=True, defaults=False):
self.load_fixtures(commands)
if failed:
result = self.failed()
self.assertTrue(result['failed'], result)
else:
result = self.changed(changed)
self.assertEqual(result['changed'], changed, result)
if commands:
if sort:
self.assertEqual(sorted(commands), sorted(result['commands']), result['commands'])
else:
self.assertEqual(commands, result['commands'], result['commands'])
return result
def failed(self):
def fail_json(*args, **kwargs):
kwargs['failed'] = True
raise AnsibleFailJson(kwargs)
with patch.object(basic.AnsibleModule, 'fail_json', fail_json):
with self.assertRaises(AnsibleFailJson) as exc:
self.module.main()
result = exc.exception.args[0]
self.assertTrue(result['failed'], result)
return result
def changed(self, changed=False):
def exit_json(*args, **kwargs):
if 'changed' not in kwargs:
kwargs['changed'] = False
raise AnsibleExitJson(kwargs)
with patch.object(basic.AnsibleModule, 'exit_json', exit_json):
with self.assertRaises(AnsibleExitJson) as exc:
self.module.main()
result = exc.exception.args[0]
self.assertEqual(result['changed'], changed, result)
return result
def load_fixtures(self, commands=None):
pass

View File

@@ -0,0 +1,124 @@
#
# (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
import json
from ansible.compat.tests.mock import patch
from ansible.modules.network.nxos import nxos_system
from .nxos_module import TestNxosModule, load_fixture, set_module_args
class TestNxosSystemModule(TestNxosModule):
module = nxos_system
def setUp(self):
self.mock_get_config = patch('ansible.modules.network.nxos.nxos_system.get_config')
self.get_config = self.mock_get_config.start()
self.mock_load_config = patch('ansible.modules.network.nxos.nxos_system.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('nxos_system_config.cfg')
self.load_config.return_value = None
def test_nxos_system_hostname_changed(self):
set_module_args(dict(hostname='foo'))
commands = ['hostname foo']
self.execute_module(changed=True, commands=commands)
def test_nxos_system_domain_lookup(self):
set_module_args(dict(domain_lookup=True))
commands = ['ip domain-lookup']
self.execute_module(changed=True, commands=commands)
def test_nxos_system_missing_vrf(self):
domain_name = dict(name='example.com', vrf='example')
set_module_args(dict(domain_name=domain_name))
self.execute_module(failed=True)
def test_nxos_system_domain_name(self):
set_module_args(dict(domain_name=['example.net']))
commands = ['no ip domain-name ansible.com',
'vrf context management', 'no ip domain-name eng.ansible.com', 'exit',
'ip domain-name example.net']
self.execute_module(changed=True, commands=commands)
def test_nxos_system_domain_name_complex(self):
domain_name = dict(name='example.net', vrf='management')
set_module_args(dict(domain_name=[domain_name]))
commands = ['no ip domain-name ansible.com',
'vrf context management', 'no ip domain-name eng.ansible.com', 'exit',
'vrf context management', 'ip domain-name example.net', 'exit']
self.execute_module(changed=True, commands=commands)
def test_nxos_system_domain_search(self):
set_module_args(dict(domain_search=['example.net']))
commands = ['vrf context management', 'no ip domain-list ansible.com', 'exit',
'vrf context management', 'no ip domain-list redhat.com', 'exit',
'no ip domain-list ansible.com', 'no ip domain-list redhat.com',
'ip domain-list example.net']
self.execute_module(changed=True, commands=commands)
def test_nxos_system_domain_search_complex(self):
domain_search = dict(name='example.net', vrf='management')
set_module_args(dict(domain_search=[domain_search]))
commands = ['vrf context management', 'no ip domain-list ansible.com', 'exit',
'vrf context management', 'no ip domain-list redhat.com', 'exit',
'no ip domain-list ansible.com', 'no ip domain-list redhat.com',
'vrf context management', 'ip domain-list example.net', 'exit']
self.execute_module(changed=True, commands=commands)
def test_nxos_system_name_servers(self):
set_module_args(dict(name_servers=['1.2.3.4', '8.8.8.8']))
commands = ['no ip name-server 172.26.1.1',
'vrf context management', 'no ip name-server 8.8.8.8', 'exit',
'vrf context management', 'no ip name-server 172.26.1.1', 'exit',
'ip name-server 1.2.3.4']
self.execute_module(changed=True, commands=commands)
def test_nxos_system_name_servers_complex(self):
name_servers = dict(server='1.2.3.4', vrf='management')
set_module_args(dict(name_servers=[name_servers]))
commands = ['no ip name-server 8.8.8.8', 'no ip name-server 172.26.1.1',
'vrf context management', 'no ip name-server 8.8.8.8', 'exit',
'vrf context management', 'no ip name-server 172.26.1.1', 'exit',
'vrf context management', 'ip name-server 1.2.3.4', 'exit']
self.execute_module(changed=True, commands=commands)
def test_nxos_system_state_absent(self):
set_module_args(dict(state='absent'))
commands = ['no hostname', 'no ip domain-name ansible.com',
'vrf context management', 'no ip domain-name eng.ansible.com', 'exit',
'no ip domain-list ansible.com', 'no ip domain-list redhat.com',
'vrf context management', 'no ip domain-list ansible.com', 'exit',
'vrf context management', 'no ip domain-list redhat.com', 'exit',
'no ip name-server 8.8.8.8', 'no ip name-server 172.26.1.1',
'vrf context management', 'no ip name-server 8.8.8.8', 'exit',
'vrf context management', 'no ip name-server 172.26.1.1', 'exit']
self.execute_module(changed=True, commands=commands)