| 1 |
# Copyright (c) Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Make coding more python3-ish |
| 6 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch, MagicMock |
| 10 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase |
| 11 |
|
| 12 |
from ansible_collections.community.routeros.tests.unit.plugins.modules.fake_api import FakeLibRouterosError, Key, Or, fake_ros_api |
| 13 |
from ansible_collections.community.routeros.plugins.modules import api |
| 14 |
|
| 15 |
|
| 16 |
class TestRouterosApiModule(ModuleTestCase): |
| 17 |
|
| 18 |
def setUp(self): |
| 19 |
super(TestRouterosApiModule, self).setUp() |
| 20 |
self.module = api |
| 21 |
self.module.LibRouterosError = FakeLibRouterosError |
| 22 |
self.module.connect = MagicMock(new=fake_ros_api) |
| 23 |
self.module.check_has_library = MagicMock() |
| 24 |
self.patch_create_api = patch('ansible_collections.community.routeros.plugins.modules.api.create_api', MagicMock(new=fake_ros_api)) |
| 25 |
self.patch_create_api.start() |
| 26 |
self.module.Key = MagicMock(new=Key) |
| 27 |
self.module.Or = MagicMock(new=Or) |
| 28 |
self.config_module_args = {"username": "admin", |
| 29 |
"password": "pаss", |
| 30 |
"hostname": "127.0.0.1", |
| 31 |
"path": "interface bridge"} |
| 32 |
|
| 33 |
def tearDown(self): |
| 34 |
self.patch_create_api.stop() |
| 35 |
|
| 36 |
def test_module_fail_when_required_args_missing(self): |
| 37 |
with self.assertRaises(AnsibleFailJson) as exc: |
| 38 |
with set_module_args({}): |
| 39 |
self.module.main() |
| 40 |
|
| 41 |
result = exc.exception.args[0] |
| 42 |
self.assertEqual(result['failed'], True) |
| 43 |
|
| 44 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.path) |
| 45 |
def test_api_path(self): |
| 46 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 47 |
with set_module_args(self.config_module_args.copy()): |
| 48 |
self.module.main() |
| 49 |
|
| 50 |
result = exc.exception.args[0] |
| 51 |
self.assertEqual(result['changed'], False) |
| 52 |
|
| 53 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) |
| 54 |
def test_api_add(self): |
| 55 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 56 |
module_args = self.config_module_args.copy() |
| 57 |
module_args['add'] = "name=unit_test_brige" |
| 58 |
with set_module_args(module_args): |
| 59 |
self.module.main() |
| 60 |
|
| 61 |
result = exc.exception.args[0] |
| 62 |
self.assertEqual(result['changed'], True) |
| 63 |
|
| 64 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) |
| 65 |
def test_api_add_already_exist(self): |
| 66 |
with self.assertRaises(AnsibleFailJson) as exc: |
| 67 |
module_args = self.config_module_args.copy() |
| 68 |
module_args['add'] = "name=unit_test_brige_exist" |
| 69 |
with set_module_args(module_args): |
| 70 |
self.module.main() |
| 71 |
|
| 72 |
result = exc.exception.args[0] |
| 73 |
self.assertEqual(result['failed'], True) |
| 74 |
self.assertEqual(result['msg'][0], 'failure: already have interface with such name') |
| 75 |
|
| 76 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) |
| 77 |
def test_api_remove(self): |
| 78 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 79 |
module_args = self.config_module_args.copy() |
| 80 |
module_args['remove'] = "*A1" |
| 81 |
with set_module_args(module_args): |
| 82 |
self.module.main() |
| 83 |
|
| 84 |
result = exc.exception.args[0] |
| 85 |
self.assertEqual(result['changed'], True) |
| 86 |
|
| 87 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) |
| 88 |
def test_api_remove_no_id(self): |
| 89 |
with self.assertRaises(AnsibleFailJson) as exc: |
| 90 |
module_args = self.config_module_args.copy() |
| 91 |
module_args['remove'] = "*A2" |
| 92 |
with set_module_args(module_args): |
| 93 |
self.module.main() |
| 94 |
|
| 95 |
result = exc.exception.args[0] |
| 96 |
self.assertEqual(result['failed'], True) |
| 97 |
self.assertEqual(result['msg'][0], 'no such item (4)') |
| 98 |
|
| 99 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.arbitrary) |
| 100 |
def test_api_cmd(self): |
| 101 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 102 |
module_args = self.config_module_args.copy() |
| 103 |
module_args['cmd'] = "add name=unit_test_brige_arbitrary" |
| 104 |
with set_module_args(module_args): |
| 105 |
self.module.main() |
| 106 |
|
| 107 |
result = exc.exception.args[0] |
| 108 |
self.assertEqual(result['changed'], False) |
| 109 |
|
| 110 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.arbitrary) |
| 111 |
def test_api_cmd_none_existing_cmd(self): |
| 112 |
with self.assertRaises(AnsibleFailJson) as exc: |
| 113 |
module_args = self.config_module_args.copy() |
| 114 |
module_args['cmd'] = "add NONE_EXIST=unit_test_brige_arbitrary" |
| 115 |
with set_module_args(module_args): |
| 116 |
self.module.main() |
| 117 |
|
| 118 |
result = exc.exception.args[0] |
| 119 |
self.assertEqual(result['failed'], True) |
| 120 |
self.assertEqual(result['msg'][0], 'no such command') |
| 121 |
|
| 122 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) |
| 123 |
def test_api_update(self): |
| 124 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 125 |
module_args = self.config_module_args.copy() |
| 126 |
module_args['update'] = ".id=*A1 name=unit_test_brige" |
| 127 |
with set_module_args(module_args): |
| 128 |
self.module.main() |
| 129 |
|
| 130 |
result = exc.exception.args[0] |
| 131 |
self.assertEqual(result['changed'], True) |
| 132 |
|
| 133 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) |
| 134 |
def test_api_update_none_existing_id(self): |
| 135 |
with self.assertRaises(AnsibleFailJson) as exc: |
| 136 |
module_args = self.config_module_args.copy() |
| 137 |
module_args['update'] = ".id=*A2 name=unit_test_brige" |
| 138 |
with set_module_args(module_args): |
| 139 |
self.module.main() |
| 140 |
|
| 141 |
result = exc.exception.args[0] |
| 142 |
self.assertEqual(result['failed'], True) |
| 143 |
self.assertEqual(result['msg'][0], 'no such item (4)') |
| 144 |
|
| 145 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) |
| 146 |
def test_api_query(self): |
| 147 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 148 |
module_args = self.config_module_args.copy() |
| 149 |
module_args['query'] = ".id name" |
| 150 |
with set_module_args(module_args): |
| 151 |
self.module.main() |
| 152 |
|
| 153 |
result = exc.exception.args[0] |
| 154 |
self.assertEqual(result['changed'], False) |
| 155 |
self.assertEqual(result['msg'], [ |
| 156 |
{'.id': '*A1', 'name': 'dummy_bridge_A1'}, |
| 157 |
{'.id': '*A2', 'name': 'dummy_bridge_A2'}, |
| 158 |
{'.id': '*A3', 'name': 'dummy_bridge_A3'}, |
| 159 |
]) |
| 160 |
|
| 161 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) |
| 162 |
def test_api_query_missing_key(self): |
| 163 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 164 |
module_args = self.config_module_args.copy() |
| 165 |
module_args['query'] = ".id other" |
| 166 |
with set_module_args(module_args): |
| 167 |
self.module.main() |
| 168 |
|
| 169 |
result = exc.exception.args[0] |
| 170 |
self.assertEqual(result['changed'], False) |
| 171 |
self.assertEqual(result['msg'], ["no results for 'interface bridge 'query' .id other"]) |
| 172 |
|
| 173 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where) |
| 174 |
def test_api_query_and_WHERE(self): |
| 175 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 176 |
module_args = self.config_module_args.copy() |
| 177 |
module_args['query'] = ".id name WHERE name == dummy_bridge_A2" |
| 178 |
with set_module_args(module_args): |
| 179 |
self.module.main() |
| 180 |
|
| 181 |
result = exc.exception.args[0] |
| 182 |
self.assertEqual(result['changed'], False) |
| 183 |
self.assertEqual(result['msg'], [ |
| 184 |
{'.id': '*A1', 'name': 'dummy_bridge_A1'}, |
| 185 |
]) |
| 186 |
|
| 187 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where) |
| 188 |
def test_api_query_and_WHERE_no_cond(self): |
| 189 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 190 |
module_args = self.config_module_args.copy() |
| 191 |
module_args['query'] = ".id name WHERE name != dummy_bridge_A2" |
| 192 |
with set_module_args(module_args): |
| 193 |
self.module.main() |
| 194 |
|
| 195 |
result = exc.exception.args[0] |
| 196 |
self.assertEqual(result['changed'], False) |
| 197 |
self.assertEqual(result['msg'], [ |
| 198 |
{'.id': '*A1', 'name': 'dummy_bridge_A1'}, |
| 199 |
]) |
| 200 |
|
| 201 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) |
| 202 |
def test_api_extended_query(self): |
| 203 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 204 |
module_args = self.config_module_args.copy() |
| 205 |
module_args['extended_query'] = { |
| 206 |
'attributes': ['.id', 'name'], |
| 207 |
} |
| 208 |
with set_module_args(module_args): |
| 209 |
self.module.main() |
| 210 |
|
| 211 |
result = exc.exception.args[0] |
| 212 |
self.assertEqual(result['changed'], False) |
| 213 |
self.assertEqual(result['msg'], [ |
| 214 |
{'.id': '*A1', 'name': 'dummy_bridge_A1'}, |
| 215 |
{'.id': '*A2', 'name': 'dummy_bridge_A2'}, |
| 216 |
{'.id': '*A3', 'name': 'dummy_bridge_A3'}, |
| 217 |
]) |
| 218 |
|
| 219 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) |
| 220 |
def test_api_extended_query_missing_key(self): |
| 221 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 222 |
module_args = self.config_module_args.copy() |
| 223 |
module_args['extended_query'] = { |
| 224 |
'attributes': ['.id', 'other'], |
| 225 |
} |
| 226 |
with set_module_args(module_args): |
| 227 |
self.module.main() |
| 228 |
|
| 229 |
result = exc.exception.args[0] |
| 230 |
self.assertEqual(result['changed'], False) |
| 231 |
self.assertEqual(result['msg'], []) |
| 232 |
|
| 233 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where) |
| 234 |
def test_api_extended_query_and_WHERE(self): |
| 235 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 236 |
module_args = self.config_module_args.copy() |
| 237 |
module_args['extended_query'] = { |
| 238 |
'attributes': ['.id', 'name'], |
| 239 |
'where': [ |
| 240 |
{ |
| 241 |
'attribute': 'name', |
| 242 |
'is': '==', |
| 243 |
'value': 'dummy_bridge_A2', |
| 244 |
}, |
| 245 |
], |
| 246 |
} |
| 247 |
with set_module_args(module_args): |
| 248 |
self.module.main() |
| 249 |
|
| 250 |
result = exc.exception.args[0] |
| 251 |
self.assertEqual(result['changed'], False) |
| 252 |
self.assertEqual(result['msg'], [ |
| 253 |
{'.id': '*A1', 'name': 'dummy_bridge_A1'}, |
| 254 |
]) |
| 255 |
|
| 256 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where) |
| 257 |
def test_api_extended_query_and_WHERE_no_cond(self): |
| 258 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 259 |
module_args = self.config_module_args.copy() |
| 260 |
module_args['extended_query'] = { |
| 261 |
'attributes': ['.id', 'name'], |
| 262 |
'where': [ |
| 263 |
{ |
| 264 |
'attribute': 'name', |
| 265 |
'is': 'not', |
| 266 |
'value': 'dummy_bridge_A2', |
| 267 |
}, |
| 268 |
], |
| 269 |
} |
| 270 |
with set_module_args(module_args): |
| 271 |
self.module.main() |
| 272 |
|
| 273 |
result = exc.exception.args[0] |
| 274 |
self.assertEqual(result['changed'], False) |
| 275 |
self.assertEqual(result['msg'], [ |
| 276 |
{'.id': '*A1', 'name': 'dummy_bridge_A1'}, |
| 277 |
]) |
| 278 |
|
| 279 |
@patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where) |
| 280 |
def test_api_extended_query_and_WHERE_or(self): |
| 281 |
with self.assertRaises(AnsibleExitJson) as exc: |
| 282 |
module_args = self.config_module_args.copy() |
| 283 |
module_args['extended_query'] = { |
| 284 |
'attributes': ['.id', 'name'], |
| 285 |
'where': [ |
| 286 |
{ |
| 287 |
'or': [ |
| 288 |
{ |
| 289 |
'attribute': 'name', |
| 290 |
'is': 'in', |
| 291 |
'value': [1, 2], |
| 292 |
}, |
| 293 |
{ |
| 294 |
'attribute': 'name', |
| 295 |
'is': '!=', |
| 296 |
'value': 5, |
| 297 |
}, |
| 298 |
], |
| 299 |
}, |
| 300 |
], |
| 301 |
} |
| 302 |
with set_module_args(module_args): |
| 303 |
self.module.main() |
| 304 |
|
| 305 |
result = exc.exception.args[0] |
| 306 |
self.assertEqual(result['changed'], False) |
| 307 |
self.assertEqual(result['msg'], [ |
| 308 |
{'.id': '*A1', 'name': 'dummy_bridge_A1'}, |
| 309 |
]) |