New module: routeros — manage MikroTik RouterOS (#41155)

* Implement initial RouterOS support

* Correct matchers for license prompts

* Documentation updates & mild refactor

* Remove one last Cisco function

* Sanity test fixes

* Move imports to the beginning

* Remove authorize property

* Handle ANSI codes

* Revert to_lines function

* CR fixes

* test(routeros): add unit tests

* Added another test (with ANSI colors and banner in fixture).

* Ignore CRLF line endings in system_package_print file

* fix: review by ganeshrn
This commit is contained in:
Egor Zaitsev 2018-07-30 07:58:03 +03:00 committed by Ganesh Nalawade
commit 249a6aae22
15 changed files with 941 additions and 27 deletions

View file

@ -10,6 +10,7 @@ def main():
'test/integration/targets/win_template/files/foo.dos.txt',
'test/integration/targets/win_module_utils/library/legacy_only_new_way_win_line_ending.ps1',
'test/integration/targets/win_module_utils/library/legacy_only_old_way_win_line_ending.ps1',
'test/units/modules/network/routeros/fixtures/system_package_print',
])
for path in sys.argv[1:] or sys.stdin.read().splitlines():

View file

@ -0,0 +1,106 @@
MMM MMM KKK TTTTTTTTTTT KKK
MMMM MMMM KKK TTTTTTTTTTT KKK
MMM MMMM MMM III KKK KKK RRRRRR OOOOOO TTT III KKK KKK
MMM MM MMM III KKKKK RRR RRR OOO OOO TTT III KKKKK
MMM MMM III KKK KKK RRRRRR OOO OOO TTT III KKK KKK
MMM MMM III KKK KKK RRR RRR OOOOOO TTT III KKK KKK
MikroTik RouterOS 6.42.5 (c) 1999-2018 http://www.mikrotik.com/
[?] Gives the list of available commands
command [?] Gives help on the command and list of arguments
[Tab] Completes the command/word. If the input is ambiguous,
a second [Tab] gives possible options
/ Move up to base level
.. Move up one level
/command Use command at the base level

Z <[?47l[?7h[?5l[?25h
[admin@MainRouter] >
[admin@MainRouter] > /system routerboard print
[admin@MainRouter] > /system routerboard print
routerboard: yes
model: 750GL
serial-number: 1234567890AB
firmware-type: ar7240
factory-firmware: 3.09
current-firmware: 6.41.2
upgrade-firmware: 6.42.5
[admin@MainRouter] >
[admin@MainRouter] > /system identity print
[admin@MainRouter] > /system identity print
name: MikroTik
[admin@MainRouter] >
[admin@MainRouter] > /system package print
[admin@MainRouter] > /system package print
Flags: X - disabled
 # NAME VERSION SCHEDULED
 0 routeros-mipsbe 6.42.5
1 system 6.42.5
2 ipv6 6.42.5
3 wireless 6.42.5
4 hotspot 6.42.5
5 dhcp 6.42.5
6 mpls 6.42.5
7 routing 6.42.5
8 ppp 6.42.5
9 security 6.42.5
10 advanced-tools 6.42.5
[admin@MainRouter] >
[admin@MainRouter] >

View file

@ -0,0 +1,17 @@
[admin@RB1100test] /system resource> print
uptime: 2w1d23h34m57s
version: "5.0rc1"
free-memory: 385272KiB
total-memory: 516708KiB
cpu: "e500v2"
cpu-count: 1
cpu-frequency: 799MHz
cpu-load: 9%
free-hdd-space: 466328KiB
total-hdd-space: 520192KiB
write-sect-since-reboot: 1411
write-sect-total: 70625
bad-blocks: 0.2%
architecture-name: "powerpc"
board-name: "RB1100"
platform: "MikroTik"

View file

@ -0,0 +1,88 @@
# (c) 2016 Red Hat Inc.
#
# 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
import os
import json
from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase
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 TestRouterosModule(ModuleTestCase):
def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False):
self.load_fixtures(commands)
if failed:
result = self.failed()
self.assertTrue(result['failed'], result)
else:
result = self.changed(changed)
self.assertEqual(result['changed'], changed, result)
if commands is not None:
if sort:
self.assertEqual(sorted(commands), sorted(result['commands']), result['commands'])
else:
self.assertEqual(commands, result['commands'], result['commands'])
return result
def failed(self):
with self.assertRaises(AnsibleFailJson) as exc:
self.module.main()
result = exc.exception.args[0]
self.assertTrue(result['failed'], result)
return result
def changed(self, changed=False):
with self.assertRaises(AnsibleExitJson) as exc:
self.module.main()
result = exc.exception.args[0]
self.assertEqual(result['changed'], changed, result)
return result
def load_fixtures(self, commands=None):
pass

View file

@ -0,0 +1,113 @@
# (c) 2016 Red Hat Inc.
#
# 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
import json
from ansible.compat.tests.mock import patch
from ansible.modules.network.routeros import routeros_command
from units.modules.utils import set_module_args
from .routeros_module import TestRouterosModule, load_fixture
class TestRouterosCommandModule(TestRouterosModule):
module = routeros_command
def setUp(self):
super(TestRouterosCommandModule, self).setUp()
self.mock_run_commands = patch('ansible.modules.network.routeros.routeros_command.run_commands')
self.run_commands = self.mock_run_commands.start()
def tearDown(self):
super(TestRouterosCommandModule, self).tearDown()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
for item in commands:
try:
obj = json.loads(item)
command = obj
except ValueError:
command = item
filename = str(command).replace(' ', '_').replace('/', '')
output.append(load_fixture(filename))
return output
self.run_commands.side_effect = load_from_file
def test_routeros_command_simple(self):
set_module_args(dict(commands=['/system resource print']))
result = self.execute_module()
self.assertEqual(len(result['stdout']), 1)
self.assertTrue('platform: "MikroTik"' in result['stdout'][0])
def test_routeros_command_multiple(self):
set_module_args(dict(commands=['/system resource print', '/system resource print']))
result = self.execute_module()
self.assertEqual(len(result['stdout']), 2)
self.assertTrue('platform: "MikroTik"' in result['stdout'][0])
def test_routeros_command_wait_for(self):
wait_for = 'result[0] contains "MikroTik"'
set_module_args(dict(commands=['/system resource print'], wait_for=wait_for))
self.execute_module()
def test_routeros_command_wait_for_fails(self):
wait_for = 'result[0] contains "test string"'
set_module_args(dict(commands=['/system resource print'], wait_for=wait_for))
self.execute_module(failed=True)
self.assertEqual(self.run_commands.call_count, 10)
def test_routeros_command_retries(self):
wait_for = 'result[0] contains "test string"'
set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, retries=2))
self.execute_module(failed=True)
self.assertEqual(self.run_commands.call_count, 2)
def test_routeros_command_match_any(self):
wait_for = ['result[0] contains "MikroTik"',
'result[0] contains "test string"']
set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, match='any'))
self.execute_module()
def test_routeros_command_match_all(self):
wait_for = ['result[0] contains "MikroTik"',
'result[0] contains "RB1100"']
set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, match='all'))
self.execute_module()
def test_routeros_command_match_all_failure(self):
wait_for = ['result[0] contains "MikroTik"',
'result[0] contains "test string"']
commands = ['/system resource print', '/system resource print']
set_module_args(dict(commands=commands, wait_for=wait_for, match='all'))
self.execute_module(failed=True)
def test_routeros_command_wait_for_2(self):
wait_for = 'result[0] contains "wireless"'
set_module_args(dict(commands=['/system package print'], wait_for=wait_for))
self.execute_module()