Add junos_config unit test (#24005)
This commit is contained in:
parent
1f7c2c63c2
commit
9c4daded94
9 changed files with 227 additions and 13 deletions
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?><rpc-reply message-id="urn:uuid:72c481b8">
|
||||
<configuration-set>
|
||||
set version 15.1X49-D15.4
|
||||
set system host-name vsrx01
|
||||
set system domain-name ansible.com
|
||||
</configuration-set>
|
||||
</rpc-reply>
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?><rpc-reply message-id="urn:uuid:6fcc8e22">
|
||||
<configuration-information>
|
||||
<configuration-output>
|
||||
[edit interfaces]
|
||||
+ ae11 {
|
||||
+ unit 0 {
|
||||
+ description Test;
|
||||
+ }
|
||||
+ }
|
||||
</configuration-output>
|
||||
</configuration-information>
|
||||
</rpc-reply>
|
28
test/units/modules/network/junos/fixtures/junos_config.json
Normal file
28
test/units/modules/network/junos/fixtures/junos_config.json
Normal file
|
@ -0,0 +1,28 @@
|
|||
|
||||
{
|
||||
"interfaces" : [
|
||||
{
|
||||
"interface" : [
|
||||
{
|
||||
"name" :
|
||||
{
|
||||
"data" : "ae11"
|
||||
},
|
||||
"unit" : [
|
||||
{
|
||||
"name" :
|
||||
{
|
||||
"data" : "0"
|
||||
},
|
||||
"description" : [
|
||||
{
|
||||
"data" : "Test"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
delete interfaces ae11
|
||||
set interfaces ae11 unit 0 description Test
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
interfaces {
|
||||
ae11 {
|
||||
unit 0 {
|
||||
description Test
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<interfaces>
|
||||
<interface>
|
||||
<name>ae11</name>
|
||||
<unit>
|
||||
<name>0</name>
|
||||
<description>Test</description>
|
||||
</unit>
|
||||
</interface>
|
||||
</interfaces>
|
|
@ -38,15 +38,23 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
|
|||
fixture_data = {}
|
||||
|
||||
|
||||
def load_fixture(name):
|
||||
def load_fixture(name, content='xml'):
|
||||
path = os.path.join(fixture_path, name)
|
||||
if path in fixture_data:
|
||||
return fixture_data[path]
|
||||
|
||||
try:
|
||||
data = ET.parse(path).getroot()
|
||||
except:
|
||||
pass
|
||||
if content == 'str':
|
||||
with open(path) as f:
|
||||
data = f.read()
|
||||
try:
|
||||
data = json.load(path)
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
data = ET.parse(path).getroot()
|
||||
except:
|
||||
pass
|
||||
|
||||
fixture_data[path] = data
|
||||
return data
|
||||
|
@ -64,7 +72,7 @@ class TestJunosModule(unittest.TestCase):
|
|||
|
||||
def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False, format='text'):
|
||||
|
||||
self.load_fixtures(commands, format)
|
||||
self.load_fixtures(commands, format, changed=changed)
|
||||
|
||||
if failed:
|
||||
result = self.failed()
|
||||
|
@ -73,12 +81,6 @@ class TestJunosModule(unittest.TestCase):
|
|||
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):
|
||||
|
|
|
@ -41,7 +41,7 @@ class TestJunosCommandModule(TestJunosModule):
|
|||
def tearDown(self):
|
||||
self.mock_send_request.stop()
|
||||
|
||||
def load_fixtures(self, commands=None, format='text'):
|
||||
def load_fixtures(self, commands=None, format='text', changed=False):
|
||||
def load_from_file(*args, **kwargs):
|
||||
module, element = args
|
||||
|
||||
|
|
146
test/units/modules/network/junos/test_junos_config.py
Normal file
146
test/units/modules/network/junos/test_junos_config.py
Normal file
|
@ -0,0 +1,146 @@
|
|||
#
|
||||
# (c) 2017 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
|
||||
|
||||
from ansible.compat.tests.mock import patch
|
||||
from ansible.modules.network.junos import junos_config
|
||||
from .junos_module import TestJunosModule, load_fixture, set_module_args
|
||||
|
||||
|
||||
class TestJunosConfigModule(TestJunosModule):
|
||||
|
||||
module = junos_config
|
||||
|
||||
def setUp(self):
|
||||
self.mock_get_config = patch('ansible.modules.network.junos.junos_config.get_configuration')
|
||||
self.get_config = self.mock_get_config.start()
|
||||
|
||||
self.mock_load_config = patch('ansible.modules.network.junos.junos_config.load_config')
|
||||
self.load_config = self.mock_load_config.start()
|
||||
|
||||
self.mock_get_diff = patch('ansible.modules.network.junos.junos_config.get_diff')
|
||||
self.get_diff = self.mock_get_diff.start()
|
||||
|
||||
self.mock_send_request = patch('ansible.modules.network.junos.junos_config.send_request')
|
||||
self.send_request = self.mock_send_request.start()
|
||||
|
||||
def tearDown(self):
|
||||
self.mock_get_config.stop()
|
||||
self.mock_load_config.stop()
|
||||
self.mock_send_request.stop()
|
||||
|
||||
def load_fixtures(self, commands=None, format='text', changed=False):
|
||||
self.get_config.return_value = load_fixture('get_configuration_rpc_reply.txt')
|
||||
if changed:
|
||||
self.load_config.return_value = load_fixture('get_configuration_rpc_reply_diff.txt')
|
||||
else:
|
||||
self.load_config.return_value = None
|
||||
|
||||
def test_junos_config_unchanged(self):
|
||||
src = load_fixture('junos_config.set', content='str')
|
||||
set_module_args(dict(src=src))
|
||||
self.execute_module()
|
||||
|
||||
def test_junos_config_src_set(self):
|
||||
src = load_fixture('junos_config.set', content='str')
|
||||
set_module_args(dict(src=src))
|
||||
self.execute_module(changed=True)
|
||||
args, kwargs = self.load_config.call_args
|
||||
self.assertEqual(kwargs['action'], 'set')
|
||||
self.assertEqual(kwargs['format'], 'text')
|
||||
|
||||
def test_junos_config_backup(self):
|
||||
set_module_args(dict(backup=True))
|
||||
result = self.execute_module()
|
||||
self.assertIn('__backup__', result)
|
||||
|
||||
def test_junos_config_lines(self):
|
||||
set_module_args(dict(lines=['delete interfaces ae11', 'set interfaces ae11 unit 0 description Test']))
|
||||
self.execute_module(changed=True)
|
||||
args, kwargs = self.load_config.call_args
|
||||
self.assertEqual(args[1][0], 'set interfaces ae11 unit 0 description Test')
|
||||
self.assertEqual(kwargs['action'], 'set')
|
||||
self.assertEqual(kwargs['format'], 'text')
|
||||
|
||||
def test_junos_config_confirm(self):
|
||||
src = load_fixture('junos_config.set', content='str')
|
||||
set_module_args(dict(src=src, confirm=40))
|
||||
self.execute_module()
|
||||
args, kwargs = self.load_config.call_args
|
||||
self.assertEqual(kwargs['confirm_timeout'], 40)
|
||||
|
||||
def test_junos_config_rollback(self):
|
||||
set_module_args(dict(rollback=10))
|
||||
self.execute_module(changed=True)
|
||||
self.assertEqual(self.get_diff.call_count, 1)
|
||||
|
||||
def test_junos_config_src_text(self):
|
||||
src = load_fixture('junos_config.text', content='str')
|
||||
set_module_args(dict(src=src))
|
||||
self.execute_module(changed=True)
|
||||
args, kwargs = self.load_config.call_args
|
||||
self.assertEqual(kwargs['action'], 'merge')
|
||||
self.assertEqual(kwargs['format'], 'text')
|
||||
|
||||
def test_junos_config_src_xml(self):
|
||||
src = load_fixture('junos_config.xml', content='str')
|
||||
set_module_args(dict(src=src))
|
||||
self.execute_module(changed=True)
|
||||
args, kwargs = self.load_config.call_args
|
||||
self.assertEqual(kwargs['action'], 'merge')
|
||||
self.assertEqual(kwargs['format'], 'xml')
|
||||
|
||||
def test_junos_config_src_json(self):
|
||||
src = load_fixture('junos_config.json', content='str')
|
||||
set_module_args(dict(src=src))
|
||||
self.execute_module(changed=True)
|
||||
args, kwargs = self.load_config.call_args
|
||||
self.assertEqual(kwargs['action'], 'merge')
|
||||
self.assertEqual(kwargs['format'], 'json')
|
||||
|
||||
def test_junos_config_update_override(self):
|
||||
src = load_fixture('junos_config.xml', content='str')
|
||||
set_module_args(dict(src=src, update='override'))
|
||||
self.execute_module()
|
||||
args, kwargs = self.load_config.call_args
|
||||
self.assertEqual(kwargs['action'], 'override')
|
||||
self.assertEqual(kwargs['format'], 'xml')
|
||||
|
||||
def test_junos_config_update_replace(self):
|
||||
src = load_fixture('junos_config.json', content='str')
|
||||
set_module_args(dict(src=src, update='replace'))
|
||||
self.execute_module()
|
||||
args, kwargs = self.load_config.call_args
|
||||
self.assertEqual(kwargs['action'], 'replace')
|
||||
self.assertEqual(kwargs['format'], 'json')
|
||||
|
||||
def test_junos_config_zeroize(self):
|
||||
src = load_fixture('junos_config.json', content='str')
|
||||
set_module_args(dict(zeroize='yes'))
|
||||
self.execute_module(changed=True)
|
||||
self.assertEqual(self.send_request.call_count, 1)
|
||||
|
||||
def test_junos_config_src_format_xml(self):
|
||||
src = load_fixture('junos_config.json', content='str')
|
||||
set_module_args(dict(src=src, src_format='xml'))
|
||||
self.execute_module()
|
||||
args, kwargs = self.load_config.call_args
|
||||
self.assertEqual(kwargs['format'], 'xml')
|
Loading…
Reference in a new issue