From 9c4daded944e6dd8b03561ab5eae18fb8ede2a17 Mon Sep 17 00:00:00 2001 From: Ganesh Nalawade Date: Thu, 27 Apr 2017 20:37:15 +0530 Subject: [PATCH] Add junos_config unit test (#24005) --- .../fixtures/get_configuration_rpc_reply.txt | 7 + .../get_configuration_rpc_reply_diff.txt | 12 ++ .../network/junos/fixtures/junos_config.json | 28 ++++ .../network/junos/fixtures/junos_config.set | 2 + .../network/junos/fixtures/junos_config.text | 8 + .../network/junos/fixtures/junos_config.xml | 9 ++ .../modules/network/junos/junos_module.py | 26 ++-- .../network/junos/test_junos_command.py | 2 +- .../network/junos/test_junos_config.py | 146 ++++++++++++++++++ 9 files changed, 227 insertions(+), 13 deletions(-) create mode 100644 test/units/modules/network/junos/fixtures/get_configuration_rpc_reply.txt create mode 100644 test/units/modules/network/junos/fixtures/get_configuration_rpc_reply_diff.txt create mode 100644 test/units/modules/network/junos/fixtures/junos_config.json create mode 100644 test/units/modules/network/junos/fixtures/junos_config.set create mode 100644 test/units/modules/network/junos/fixtures/junos_config.text create mode 100644 test/units/modules/network/junos/fixtures/junos_config.xml create mode 100644 test/units/modules/network/junos/test_junos_config.py diff --git a/test/units/modules/network/junos/fixtures/get_configuration_rpc_reply.txt b/test/units/modules/network/junos/fixtures/get_configuration_rpc_reply.txt new file mode 100644 index 00000000000..1d5b2399058 --- /dev/null +++ b/test/units/modules/network/junos/fixtures/get_configuration_rpc_reply.txt @@ -0,0 +1,7 @@ + + + set version 15.1X49-D15.4 + set system host-name vsrx01 + set system domain-name ansible.com + + \ No newline at end of file diff --git a/test/units/modules/network/junos/fixtures/get_configuration_rpc_reply_diff.txt b/test/units/modules/network/junos/fixtures/get_configuration_rpc_reply_diff.txt new file mode 100644 index 00000000000..c2b803f8284 --- /dev/null +++ b/test/units/modules/network/junos/fixtures/get_configuration_rpc_reply_diff.txt @@ -0,0 +1,12 @@ + + + +[edit interfaces] ++ ae11 { ++ unit 0 { ++ description Test; ++ } ++ } + + + diff --git a/test/units/modules/network/junos/fixtures/junos_config.json b/test/units/modules/network/junos/fixtures/junos_config.json new file mode 100644 index 00000000000..46fb623dda9 --- /dev/null +++ b/test/units/modules/network/junos/fixtures/junos_config.json @@ -0,0 +1,28 @@ + +{ + "interfaces" : [ + { + "interface" : [ + { + "name" : + { + "data" : "ae11" + }, + "unit" : [ + { + "name" : + { + "data" : "0" + }, + "description" : [ + { + "data" : "Test" + } + ] + } + ] + } + ] + } + ] +} diff --git a/test/units/modules/network/junos/fixtures/junos_config.set b/test/units/modules/network/junos/fixtures/junos_config.set new file mode 100644 index 00000000000..c822fdb4be3 --- /dev/null +++ b/test/units/modules/network/junos/fixtures/junos_config.set @@ -0,0 +1,2 @@ +delete interfaces ae11 +set interfaces ae11 unit 0 description Test \ No newline at end of file diff --git a/test/units/modules/network/junos/fixtures/junos_config.text b/test/units/modules/network/junos/fixtures/junos_config.text new file mode 100644 index 00000000000..b3d61b993ac --- /dev/null +++ b/test/units/modules/network/junos/fixtures/junos_config.text @@ -0,0 +1,8 @@ + +interfaces { + ae11 { + unit 0 { + description Test + } + } + } \ No newline at end of file diff --git a/test/units/modules/network/junos/fixtures/junos_config.xml b/test/units/modules/network/junos/fixtures/junos_config.xml new file mode 100644 index 00000000000..a0e8fe1b75c --- /dev/null +++ b/test/units/modules/network/junos/fixtures/junos_config.xml @@ -0,0 +1,9 @@ + + + ae11 + + 0 + Test + + + diff --git a/test/units/modules/network/junos/junos_module.py b/test/units/modules/network/junos/junos_module.py index 0bf12f68602..26fd98f3281 100644 --- a/test/units/modules/network/junos/junos_module.py +++ b/test/units/modules/network/junos/junos_module.py @@ -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): diff --git a/test/units/modules/network/junos/test_junos_command.py b/test/units/modules/network/junos/test_junos_command.py index 707c47a6d25..51e07b34098 100644 --- a/test/units/modules/network/junos/test_junos_command.py +++ b/test/units/modules/network/junos/test_junos_command.py @@ -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 diff --git a/test/units/modules/network/junos/test_junos_config.py b/test/units/modules/network/junos/test_junos_config.py new file mode 100644 index 00000000000..68a794e6664 --- /dev/null +++ b/test/units/modules/network/junos/test_junos_config.py @@ -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 . + +# 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')