ansible-collections.communi.../tests/unit/plugins/modules/fake_api.py
Nikolay Dachev d57de117f5
update community.routeros.api query functionality (#63)
* update query to accept multiple librouteros ADN parameters

* update query for new yml strucutre

* add extended_query as separate function:(code in progress)

* extended_query main code is ready for review

* add changelog #63

* small fix for code indentation

* fix pep

* clear all pep issues

* extended_query ready for review (new yml structure)

* small doc fix for std query

* Update changelogs/fragments/63-add-extended_query.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update changelogs/fragments/63-add-extended_query.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update argument spec.

* Other suggestions.

* Fix syntax errors ('is' and 'or' are keywords).

* Make everything work again.

* Add docs, simplify code.

* Add some first tests.

* Do not add fake message when there is no search result.

* Improve tests.

* Fix tests.

* update extened query docs and ros api module examples

* fix pep plugins/modules/api.py:154:1: W293: blank line contains whitespace

* fix extended query example intend

* Update plugins/modules/api.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/api.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* fix example docs

Co-authored-by: dako <dako@syslin.sof.dachev.lan>
Co-authored-by: Felix Fontein <felix@fontein.de>
2022-05-23 14:44:02 +03:00

127 lines
4 KiB
Python

# 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 <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
class FakeLibRouterosError(Exception):
def __init__(self, message):
self.message = message
super(FakeLibRouterosError, self).__init__(self.message)
class TrapError(FakeLibRouterosError):
def __init__(self, message="failure: already have interface with such name"):
super(TrapError, self).__init__(message)
# fixtures
class fake_ros_api(object):
def __init__(self, api, path):
pass
@classmethod
def path(cls, api, path):
fake_bridge = [{".id": "*DC", "name": "b2", "mtu": "auto", "actual-mtu": 1500,
"l2mtu": 65535, "arp": "enabled", "arp-timeout": "auto",
"mac-address": "3A:C1:90:D6:E8:44", "protocol-mode": "rstp",
"fast-forward": "true", "igmp-snooping": "false",
"auto-mac": "true", "ageing-time": "5m", "priority":
"0x8000", "max-message-age": "20s", "forward-delay": "15s",
"transmit-hold-count": 6, "vlan-filtering": "false",
"dhcp-snooping": "false", "running": "true", "disabled": "false"}]
return fake_bridge
@classmethod
def arbitrary(cls, api, path):
def retr(self, *args, **kwargs):
if 'name' not in kwargs.keys():
raise TrapError(message="no such command")
dummy_test_string = '/interface/bridge add name=unit_test_brige_arbitrary'
result = "/%s/%s add name=%s" % (path[0], path[1], kwargs['name'])
return [result]
return retr
def add(self, name):
if name == "unit_test_brige_exist":
raise TrapError
return '*A1'
def remove(self, id):
if id != "*A1":
raise TrapError(message="no such item (4)")
return '*A1'
def update(self, **kwargs):
if kwargs['.id'] != "*A1" or 'name' not in kwargs.keys():
raise TrapError(message="no such item (4)")
return ["updated: {'.id': '%s' % kwargs['.id'], 'name': '%s' % kwargs['name']}"]
def select(self, *args):
dummy_bridge = [{".id": "*A1", "name": "dummy_bridge_A1"},
{".id": "*A2", "name": "dummy_bridge_A2"},
{".id": "*A3", "name": "dummy_bridge_A3"}]
result = []
for dummy in dummy_bridge:
found = {}
for search in args:
if search in dummy.keys():
found[search] = dummy[search]
else:
continue
if len(found.keys()) == 2:
result.append(found)
if result:
return result
else:
return []
@classmethod
def select_where(cls, api, path):
api_path = Where()
return api_path
class Where(object):
def __init__(self):
pass
def select(self, *args):
return self
def where(self, *args):
return [{".id": "*A1", "name": "dummy_bridge_A1"}]
class Key(object):
def __init__(self, name):
self.name = name
self.str_return()
def str_return(self):
return str(self.name)
class Or(object):
def __init__(self, *args):
self.args = args
self.str_return()
def str_return(self):
return repr(self.args)