mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 05:42:50 +00:00
New networking module: voss_command (#43741)
* new voss_command module * Removed incorrect version_added line
This commit is contained in:
committed by
Ricardo Carrillo Cruz
parent
3419c75411
commit
8429f777da
0
test/units/modules/network/voss/__init__.py
Normal file
0
test/units/modules/network/voss/__init__.py
Normal file
107
test/units/modules/network/voss/fixtures/show_sys-info
Normal file
107
test/units/modules/network/voss/fixtures/show_sys-info
Normal file
@@ -0,0 +1,107 @@
|
||||
General Info :
|
||||
|
||||
SysDescr : VSP-4450GSX-PWR+ (7.0.0.0_B015)
|
||||
SysName : VSP-4450GSX-PWR+
|
||||
SysUpTime : 5 day(s), 17:13:09
|
||||
SysContact : http://www.extremenetworks.com/contact/
|
||||
SysLocation :
|
||||
|
||||
Chassis Info:
|
||||
|
||||
Chassis : 4450GSX-PWR+
|
||||
ModelName : 4450GSX-PWR+
|
||||
BrandName : Extreme Networks.
|
||||
Serial# : 14JP512E0001
|
||||
H/W Revision : 01
|
||||
H/W Config : none
|
||||
Part Number :
|
||||
NumSlots : 1
|
||||
NumPorts : 50
|
||||
BaseMacAddr : b4:47:5e:00:00:00
|
||||
MacAddrCapacity : 256
|
||||
System MTU : 1950
|
||||
|
||||
Card Info :
|
||||
|
||||
Slot# CardType Serial# Part# Oper Admin Power
|
||||
Status Status State
|
||||
1 4450GSX-PWR+ 14JP512E0001 -- up up on
|
||||
|
||||
Temperature Info :
|
||||
|
||||
Chassis Temperature
|
||||
30
|
||||
|
||||
|
||||
Power Supply Info :
|
||||
|
||||
Ps#1 Status : UP
|
||||
Ps#1 Type : AC
|
||||
Ps#1 Description : AC-DC-54V-1000W
|
||||
Ps#1 Serial Number: LBNNTMPL20180R
|
||||
Ps#1 Version : --
|
||||
Ps#1 Part Number : 325220-A.01
|
||||
|
||||
Ps#2 Status : empty
|
||||
|
||||
Total Power Available : 1000 watts
|
||||
Total Power Usage : 127 watts
|
||||
|
||||
Fan Info :
|
||||
|
||||
Description OperStatus OperSpeed AirflowDir
|
||||
Tray 1 Fan 1 up mediumSpeed left-right
|
||||
Tray 1 Fan 2 up mediumSpeed left-right
|
||||
Tray 1 Fan 3 up mediumSpeed left-right
|
||||
|
||||
LED Info :
|
||||
|
||||
LED#1 Label : PWR
|
||||
LED#1 Status : GreenSteady
|
||||
|
||||
LED#2 Label : Status
|
||||
LED#2 Status : GreenSteady
|
||||
|
||||
LED#3 Label : Rps
|
||||
LED#3 Status : Off
|
||||
|
||||
LED#4 Label : Up
|
||||
LED#4 Status : UnSupported
|
||||
|
||||
LED#5 Label : Down
|
||||
LED#5 Status : UnSupported
|
||||
|
||||
LED#6 Label : Base
|
||||
LED#6 Status : UnSupported
|
||||
|
||||
System Error Info :
|
||||
|
||||
Send Login Success Trap : false
|
||||
Send Authentication Trap : false
|
||||
Error Code : 0
|
||||
Error Severity : 0
|
||||
|
||||
Port Lock Info :
|
||||
|
||||
Status : off
|
||||
LockedPorts :
|
||||
|
||||
Message Control Info :
|
||||
|
||||
Action : suppress-msg
|
||||
Control-Interval : 5
|
||||
Max-msg-num : 5
|
||||
Status : disable
|
||||
|
||||
|
||||
Configuration Operation Info Since Boot Up:
|
||||
Last Change: 0 day(s), 08:31:10 (5 day(s), 08:41:59 ago)
|
||||
Last Vlan Change: 0 day(s), 08:27:35 (5 day(s), 08:45:34 ago)
|
||||
Last Statistic Reset: 5 day(s), 16:56:45 (0 day(s), 00:16:24 ago)
|
||||
|
||||
Current Uboot Info :
|
||||
----------------------------------------------------------------------------------------------------
|
||||
|
||||
VU-Boot 2012.04-00002-g6fb1c26 (Apr 26 2017 - 13:37:44) bld=17042617
|
||||
|
||||
|
||||
120
test/units/modules/network/voss/test_voss_command.py
Normal file
120
test/units/modules/network/voss/test_voss_command.py
Normal file
@@ -0,0 +1,120 @@
|
||||
# (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.voss import voss_command
|
||||
from units.modules.utils import set_module_args
|
||||
from .voss_module import TestVossModule, load_fixture
|
||||
|
||||
|
||||
class TestVossCommandModule(TestVossModule):
|
||||
|
||||
module = voss_command
|
||||
|
||||
def setUp(self):
|
||||
super(TestVossCommandModule, self).setUp()
|
||||
|
||||
self.mock_run_commands = patch('ansible.modules.network.voss.voss_command.run_commands')
|
||||
self.run_commands = self.mock_run_commands.start()
|
||||
|
||||
def tearDown(self):
|
||||
super(TestVossCommandModule, 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'])
|
||||
command = obj['command']
|
||||
except ValueError:
|
||||
command = item['command']
|
||||
filename = str(command).replace(' ', '_')
|
||||
output.append(load_fixture(filename))
|
||||
return output
|
||||
|
||||
self.run_commands.side_effect = load_from_file
|
||||
|
||||
def test_voss_command_simple(self):
|
||||
set_module_args(dict(commands=['show sys-info']))
|
||||
result = self.execute_module()
|
||||
self.assertEqual(len(result['stdout']), 1)
|
||||
self.assertTrue(result['stdout'][0].startswith('General Info'))
|
||||
|
||||
def test_voss_command_multiple(self):
|
||||
set_module_args(dict(commands=['show sys-info', 'show sys-info']))
|
||||
result = self.execute_module()
|
||||
self.assertEqual(len(result['stdout']), 2)
|
||||
self.assertTrue(result['stdout'][0].startswith('General Info'))
|
||||
|
||||
def test_voss_command_wait_for(self):
|
||||
wait_for = 'result[0] contains "General Info"'
|
||||
set_module_args(dict(commands=['show sys-info'], wait_for=wait_for))
|
||||
self.execute_module()
|
||||
|
||||
def test_voss_command_wait_for_fails(self):
|
||||
wait_for = 'result[0] contains "test string"'
|
||||
set_module_args(dict(commands=['show sys-info'], wait_for=wait_for))
|
||||
self.execute_module(failed=True)
|
||||
self.assertEqual(self.run_commands.call_count, 10)
|
||||
|
||||
def test_voss_command_retries(self):
|
||||
wait_for = 'result[0] contains "test string"'
|
||||
set_module_args(dict(commands=['show sys-info'], wait_for=wait_for, retries=2))
|
||||
self.execute_module(failed=True)
|
||||
self.assertEqual(self.run_commands.call_count, 2)
|
||||
|
||||
def test_voss_command_match_any(self):
|
||||
wait_for = ['result[0] contains "General Info"',
|
||||
'result[0] contains "test string"']
|
||||
set_module_args(dict(commands=['show sys-info'], wait_for=wait_for, match='any'))
|
||||
self.execute_module()
|
||||
|
||||
def test_voss_command_match_all(self):
|
||||
wait_for = ['result[0] contains "General Info"',
|
||||
'result[0] contains "Chassis Info"']
|
||||
set_module_args(dict(commands=['show sys-info'], wait_for=wait_for, match='all'))
|
||||
self.execute_module()
|
||||
|
||||
def test_voss_command_match_all_failure(self):
|
||||
wait_for = ['result[0] contains "General Info"',
|
||||
'result[0] contains "test string"']
|
||||
commands = ['show sys-info', 'show sys-info']
|
||||
set_module_args(dict(commands=commands, wait_for=wait_for, match='all'))
|
||||
self.execute_module(failed=True)
|
||||
|
||||
def test_voss_command_configure_error(self):
|
||||
commands = ['configure terminal']
|
||||
set_module_args({
|
||||
'commands': commands,
|
||||
'_ansible_check_mode': True,
|
||||
})
|
||||
result = self.execute_module(failed=True)
|
||||
self.assertEqual(
|
||||
result['msg'],
|
||||
'voss_command does not support running config mode commands. Please use voss_config instead'
|
||||
)
|
||||
88
test/units/modules/network/voss/voss_module.py
Normal file
88
test/units/modules/network/voss/voss_module.py
Normal 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 TestVossModule(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
|
||||
Reference in New Issue
Block a user