2022-07-18 23:16:54 +02:00
|
|
|
|
# Copyright (c) Ansible Project
|
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
2025-03-12 21:34:12 +01:00
|
|
|
|
from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch, MagicMock
|
|
|
|
|
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase
|
|
|
|
|
|
2022-05-23 14:44:02 +03:00
|
|
|
|
from ansible_collections.community.routeros.tests.unit.plugins.modules.fake_api import FakeLibRouterosError, Key, Or, fake_ros_api
|
2020-08-18 10:57:01 +03:00
|
|
|
|
from ansible_collections.community.routeros.plugins.modules import api
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRouterosApiModule(ModuleTestCase):
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
super(TestRouterosApiModule, self).setUp()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
self.module = api
|
2021-07-11 15:53:22 +02:00
|
|
|
|
self.module.LibRouterosError = FakeLibRouterosError
|
2020-08-18 10:57:01 +03:00
|
|
|
|
self.module.connect = MagicMock(new=fake_ros_api)
|
2022-05-15 22:42:26 +02:00
|
|
|
|
self.module.check_has_library = MagicMock()
|
2022-05-12 16:17:43 +02:00
|
|
|
|
self.patch_create_api = patch('ansible_collections.community.routeros.plugins.modules.api.create_api', MagicMock(new=fake_ros_api))
|
|
|
|
|
self.patch_create_api.start()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
self.module.Key = MagicMock(new=Key)
|
2022-05-23 14:44:02 +03:00
|
|
|
|
self.module.Or = MagicMock(new=Or)
|
2020-08-18 10:57:01 +03:00
|
|
|
|
self.config_module_args = {"username": "admin",
|
|
|
|
|
"password": "pаss",
|
|
|
|
|
"hostname": "127.0.0.1",
|
|
|
|
|
"path": "interface bridge"}
|
|
|
|
|
|
2022-05-12 16:17:43 +02:00
|
|
|
|
def tearDown(self):
|
|
|
|
|
self.patch_create_api.stop()
|
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
def test_module_fail_when_required_args_missing(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleFailJson) as exc:
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args({}):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['failed'], True)
|
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.path)
|
|
|
|
|
def test_api_path(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(self.config_module_args.copy()):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], False)
|
|
|
|
|
|
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api)
|
2020-08-18 10:57:01 +03:00
|
|
|
|
def test_api_add(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['add'] = "name=unit_test_brige"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], True)
|
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api)
|
|
|
|
|
def test_api_add_already_exist(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleFailJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['add'] = "name=unit_test_brige_exist"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['failed'], True)
|
|
|
|
|
self.assertEqual(result['msg'][0], 'failure: already have interface with such name')
|
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api)
|
|
|
|
|
def test_api_remove(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['remove'] = "*A1"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], True)
|
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api)
|
|
|
|
|
def test_api_remove_no_id(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleFailJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['remove'] = "*A2"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['failed'], True)
|
|
|
|
|
self.assertEqual(result['msg'][0], 'no such item (4)')
|
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.arbitrary)
|
|
|
|
|
def test_api_cmd(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['cmd'] = "add name=unit_test_brige_arbitrary"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], False)
|
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.arbitrary)
|
|
|
|
|
def test_api_cmd_none_existing_cmd(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleFailJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['cmd'] = "add NONE_EXIST=unit_test_brige_arbitrary"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['failed'], True)
|
|
|
|
|
self.assertEqual(result['msg'][0], 'no such command')
|
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api)
|
|
|
|
|
def test_api_update(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['update'] = ".id=*A1 name=unit_test_brige"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], True)
|
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api)
|
|
|
|
|
def test_api_update_none_existing_id(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleFailJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['update'] = ".id=*A2 name=unit_test_brige"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['failed'], True)
|
|
|
|
|
self.assertEqual(result['msg'][0], 'no such item (4)')
|
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api)
|
|
|
|
|
def test_api_query(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['query'] = ".id name"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], False)
|
2022-05-23 14:44:02 +03:00
|
|
|
|
self.assertEqual(result['msg'], [
|
|
|
|
|
{'.id': '*A1', 'name': 'dummy_bridge_A1'},
|
|
|
|
|
{'.id': '*A2', 'name': 'dummy_bridge_A2'},
|
|
|
|
|
{'.id': '*A3', 'name': 'dummy_bridge_A3'},
|
|
|
|
|
])
|
2021-07-11 15:53:22 +02:00
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api)
|
|
|
|
|
def test_api_query_missing_key(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['query'] = ".id other"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], False)
|
2022-05-23 14:44:02 +03:00
|
|
|
|
self.assertEqual(result['msg'], ["no results for 'interface bridge 'query' .id other"])
|
2021-07-11 15:53:22 +02:00
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where)
|
|
|
|
|
def test_api_query_and_WHERE(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['query'] = ".id name WHERE name == dummy_bridge_A2"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2020-08-18 10:57:01 +03:00
|
|
|
|
|
2021-07-11 15:53:22 +02:00
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], False)
|
2022-05-23 14:44:02 +03:00
|
|
|
|
self.assertEqual(result['msg'], [
|
|
|
|
|
{'.id': '*A1', 'name': 'dummy_bridge_A1'},
|
|
|
|
|
])
|
2021-07-11 15:53:22 +02:00
|
|
|
|
|
2020-08-18 10:57:01 +03:00
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where)
|
|
|
|
|
def test_api_query_and_WHERE_no_cond(self):
|
2021-07-11 15:53:22 +02:00
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
2020-08-18 10:57:01 +03:00
|
|
|
|
module_args = self.config_module_args.copy()
|
2021-07-11 15:53:22 +02:00
|
|
|
|
module_args['query'] = ".id name WHERE name != dummy_bridge_A2"
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2021-07-11 15:53:22 +02:00
|
|
|
|
|
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], False)
|
2022-05-23 14:44:02 +03:00
|
|
|
|
self.assertEqual(result['msg'], [
|
|
|
|
|
{'.id': '*A1', 'name': 'dummy_bridge_A1'},
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api)
|
|
|
|
|
def test_api_extended_query(self):
|
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['extended_query'] = {
|
|
|
|
|
'attributes': ['.id', 'name'],
|
|
|
|
|
}
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2022-05-23 14:44:02 +03:00
|
|
|
|
|
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], False)
|
|
|
|
|
self.assertEqual(result['msg'], [
|
|
|
|
|
{'.id': '*A1', 'name': 'dummy_bridge_A1'},
|
|
|
|
|
{'.id': '*A2', 'name': 'dummy_bridge_A2'},
|
|
|
|
|
{'.id': '*A3', 'name': 'dummy_bridge_A3'},
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api)
|
|
|
|
|
def test_api_extended_query_missing_key(self):
|
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['extended_query'] = {
|
|
|
|
|
'attributes': ['.id', 'other'],
|
|
|
|
|
}
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2022-05-23 14:44:02 +03:00
|
|
|
|
|
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], False)
|
|
|
|
|
self.assertEqual(result['msg'], [])
|
|
|
|
|
|
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where)
|
|
|
|
|
def test_api_extended_query_and_WHERE(self):
|
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['extended_query'] = {
|
|
|
|
|
'attributes': ['.id', 'name'],
|
|
|
|
|
'where': [
|
|
|
|
|
{
|
|
|
|
|
'attribute': 'name',
|
|
|
|
|
'is': '==',
|
|
|
|
|
'value': 'dummy_bridge_A2',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2022-05-23 14:44:02 +03:00
|
|
|
|
|
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], False)
|
|
|
|
|
self.assertEqual(result['msg'], [
|
|
|
|
|
{'.id': '*A1', 'name': 'dummy_bridge_A1'},
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where)
|
|
|
|
|
def test_api_extended_query_and_WHERE_no_cond(self):
|
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['extended_query'] = {
|
|
|
|
|
'attributes': ['.id', 'name'],
|
|
|
|
|
'where': [
|
|
|
|
|
{
|
|
|
|
|
'attribute': 'name',
|
|
|
|
|
'is': 'not',
|
|
|
|
|
'value': 'dummy_bridge_A2',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2022-05-23 14:44:02 +03:00
|
|
|
|
|
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], False)
|
|
|
|
|
self.assertEqual(result['msg'], [
|
|
|
|
|
{'.id': '*A1', 'name': 'dummy_bridge_A1'},
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where)
|
|
|
|
|
def test_api_extended_query_and_WHERE_or(self):
|
|
|
|
|
with self.assertRaises(AnsibleExitJson) as exc:
|
|
|
|
|
module_args = self.config_module_args.copy()
|
|
|
|
|
module_args['extended_query'] = {
|
|
|
|
|
'attributes': ['.id', 'name'],
|
|
|
|
|
'where': [
|
|
|
|
|
{
|
|
|
|
|
'or': [
|
|
|
|
|
{
|
|
|
|
|
'attribute': 'name',
|
|
|
|
|
'is': 'in',
|
|
|
|
|
'value': [1, 2],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'attribute': 'name',
|
|
|
|
|
'is': '!=',
|
|
|
|
|
'value': 5,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
2025-03-07 20:45:39 +01:00
|
|
|
|
with set_module_args(module_args):
|
|
|
|
|
self.module.main()
|
2022-05-23 14:44:02 +03:00
|
|
|
|
|
|
|
|
|
result = exc.exception.args[0]
|
|
|
|
|
self.assertEqual(result['changed'], False)
|
|
|
|
|
self.assertEqual(result['msg'], [
|
|
|
|
|
{'.id': '*A1', 'name': 'dummy_bridge_A1'},
|
|
|
|
|
])
|