mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-15 17:40:50 -07:00
updates vyos modules to use socket connection (#21228)
* updates all vyos modules to use socket connection * adds vyos local action handler * adds exec_command() to vyos * updates vyos_config local action * update unit test cases * add base class for testing vyos modules
This commit is contained in:
parent
85194234ba
commit
8adb108aa9
14 changed files with 426 additions and 547 deletions
|
@ -20,45 +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.vyos import vyos_config
|
||||
from ansible.module_utils import basic
|
||||
from ansible.module_utils._text import to_bytes
|
||||
from .vyos_module import TestVyosModule, load_fixture, set_module_args
|
||||
|
||||
|
||||
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
|
||||
fixture_data = {}
|
||||
class TestVyosConfigModule(TestVyosModule):
|
||||
|
||||
|
||||
def set_module_args(args):
|
||||
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
|
||||
basic._ANSIBLE_ARGS = to_bytes(args)
|
||||
|
||||
|
||||
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 TestVyosConfigModule(unittest.TestCase):
|
||||
module = vyos_config
|
||||
|
||||
def setUp(self):
|
||||
self.mock_get_config = patch('ansible.modules.network.vyos.vyos_config.get_config')
|
||||
|
@ -75,30 +46,11 @@ class TestVyosConfigModule(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 = 'vyos_config_defaults.cfg' if defaults else 'vyos_config_config.cfg'
|
||||
def load_fixtures(self, commands=None):
|
||||
config_file = 'vyos_config_config.cfg'
|
||||
self.get_config.return_value = load_fixture(config_file)
|
||||
self.load_config.return_value = None
|
||||
|
||||
with self.assertRaises(AnsibleModuleExit) as exc:
|
||||
vyos_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['commands']), result['commands'])
|
||||
else:
|
||||
self.assertEqual(commands, result['commands'], result['commands'])
|
||||
|
||||
return result
|
||||
|
||||
def test_vyos_config_unchanged(self):
|
||||
src = load_fixture('vyos_config_config.cfg')
|
||||
set_module_args(dict(src=src))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue