Add Support for Mellanox switches: first module: mlnxos_command (#33121)

* Add Support for Mellanox switches: first module: mlnxos_command

Signed-off-by: Samer Deeb <samerd@mellanox.com>

* Add cliconf support for mlnxos

Signed-off-by: Samer Deeb <samerd@mellanox.com>

* 1- Fix short description, 2- remove waitfor

Signed-off-by: Samer Deeb <samerd@mellanox.com>

* remove usage of check_args

Signed-off-by: Samer Deeb <samerd@mellanox.com>
This commit is contained in:
Samer Deeb 2017-11-27 12:55:08 -08:00 committed by John R Barker
parent bb38e34fc5
commit cbf28c20cb
13 changed files with 869 additions and 1 deletions

View file

@ -0,0 +1,19 @@
Product name: MLNX-OS
Product release: 3.6.5000
Build ID: #1-dev
Build date: 2017-11-10 18:14:32
Target arch: x86_64
Target hw: x86_64
Built by: jenkins@cc45f26cd083
Version summary: X86_64 3.6.5000 2017-11-10 18:14:32 x86_64
Product model: x86onie
Host ID: 248A073D505C
System serial num: \"MT1632X00205\"
System UUID: 0b19d6d0-5eca-11e6-8000-7cfe90fadc40
Uptime: 1d 16h 31m 43.856s
CPU load averages: 0.06 / 0.12 / 0.13
Number of CPUs: 4
System memory: 2597 MB used / 5213 MB free / 7810 MB total
Swap: 0 MB used / 0 MB free / 0 MB total

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 json
import os
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 TestMlnxosModule(ModuleTestCase):
def execute_module(self, failed=False, changed=False, commands=None, inputs=None, sort=True, defaults=False, transport='cli'):
self.load_fixtures(commands, transport=transport)
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, transport='cli'):
pass

View file

@ -0,0 +1,114 @@
# (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.mlnxos import mlnxos_command
from units.modules.utils import set_module_args
from .mlnxos_module import TestMlnxosModule, load_fixture
class TestMlnxosCommandModule(TestMlnxosModule):
module = mlnxos_command
def setUp(self):
super(TestMlnxosCommandModule, self).setUp()
self.mock_run_commands = patch(
'ansible.modules.network.mlnxos.mlnxos_command.run_commands')
self.run_commands = self.mock_run_commands.start()
def tearDown(self):
super(TestMlnxosCommandModule, self).tearDown()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None, transport='cli'):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
for item in commands:
try:
obj = json.loads(item['command'])
command = obj['command']
except ValueError:
command = item['command']
filename = str(command).replace(' ', '_')
filename = 'mlnxos_command_%s.txt' % filename
output.append(load_fixture(filename))
return output
self.run_commands.side_effect = load_from_file
def test_mlnxos_command_simple(self):
set_module_args(dict(commands=['show version']))
result = self.execute_module()
self.assertEqual(len(result['stdout']), 1)
self.assertTrue(result['stdout'][0].startswith('Product name'))
def test_mlnxos_command_multiple(self):
set_module_args(dict(commands=['show version', 'show version']))
result = self.execute_module()
self.assertEqual(len(result['stdout']), 2)
self.assertTrue(result['stdout'][0].startswith('Product name'))
def test_mlnxos_command_wait_for(self):
wait_for = 'result[0] contains "MLNX"'
set_module_args(dict(commands=['show version'], wait_for=wait_for))
self.execute_module()
def test_mlnxos_command_wait_for_fails(self):
wait_for = 'result[0] contains "test string"'
set_module_args(dict(commands=['show version'], wait_for=wait_for))
self.execute_module(failed=True)
self.assertEqual(self.run_commands.call_count, 10)
def test_mlnxos_command_retries(self):
wait_for = 'result[0] contains "test string"'
set_module_args(
dict(commands=['show version'], wait_for=wait_for, retries=2))
self.execute_module(failed=True)
self.assertEqual(self.run_commands.call_count, 2)
def test_mlnxos_command_match_any(self):
wait_for = ['result[0] contains "MLNX"',
'result[0] contains "test string"']
set_module_args(dict(
commands=['show version'],
wait_for=wait_for,
match='any'))
self.execute_module()
def test_mlnxos_command_match_all(self):
wait_for = ['result[0] contains "MLNX"',
'result[0] contains "Version summary"']
set_module_args(
dict(commands=['show version'], wait_for=wait_for, match='all'))
self.execute_module()
def test_mlnxos_command_match_all_failure(self):
wait_for = ['result[0] contains "MLNX"',
'result[0] contains "test string"']
commands = ['show version', 'show version']
set_module_args(
dict(commands=commands, wait_for=wait_for, match='all'))
self.execute_module(failed=True)