updates ios modules to support persistent socket (#21258)

* updates all ios modules to support persistent socket
* adds ios action plugin to connect to device
* adds exec_command() to ios shared module
* fixes ios_config and ios_template local action
* update all unit test cases
* adds base test module for ios module testing
This commit is contained in:
Peter Sprygada 2017-02-13 10:22:14 -05:00 committed by John R Barker
parent 8bf69411d9
commit 7f1c43e597
19 changed files with 493 additions and 737 deletions

View file

@ -20,43 +20,16 @@
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, MagicMock
from ansible.errors import AnsibleModuleExit
from ansible.compat.tests.mock import patch
from ansible.modules.network.ios import ios_config
from ansible.module_utils import basic
from ansible.module_utils._text import to_bytes
from .ios_module import TestIosModule, load_fixture, set_module_args
def set_module_args(args):
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
class TestIosConfigModule(TestIosModule):
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 TestIosConfigModule(unittest.TestCase):
module = ios_config
def setUp(self):
self.mock_get_config = patch('ansible.modules.network.ios.ios_config.get_config')
@ -73,31 +46,11 @@ class TestIosConfigModule(unittest.TestCase):
self.mock_load_config.stop()
self.mock_run_commands.stop()
def execute_module(self, failed=False, changed=False, commands=None,
sort=True, defaults=False):
config_file = 'ios_config_defaults.cfg' if defaults else 'ios_config_config.cfg'
def load_fixtures(self, commands=None):
config_file = 'ios_config_config.cfg'
self.get_config.return_value = load_fixture(config_file)
self.load_config.return_value = None
with self.assertRaises(AnsibleModuleExit) as exc:
ios_config.main()
result = exc.exception.result
if failed:
self.assertTrue(result['failed'], result)
else:
self.assertEqual(result.get('changed'), changed, result)
if commands:
if sort:
self.assertEqual(sorted(commands), sorted(result['updates']), result['updates'])
else:
self.assertEqual(commands, result['updates'], result['updates'])
return result
def test_ios_config_unchanged(self):
src = load_fixture('ios_config_config.cfg')
set_module_args(dict(src=src))
@ -134,11 +87,6 @@ class TestIosConfigModule(unittest.TestCase):
commands = ['interface GigabitEthernet0/0', 'shutdown']
self.execute_module(changed=True, commands=commands)
def test_ios_config_defaults(self):
set_module_args(dict(lines=['no shutdown'], parents=['interface GigabitEthernet0/0'],
defaults=True))
self.execute_module(defaults=True)
def test_ios_config_before(self):
set_module_args(dict(lines=['hostname foo'], before=['test1','test2']))
commands = ['test1', 'test2', 'hostname foo']