mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Support for Openswitch (OPX) Control Plane Services (CPS) Module (#44745)
* Generic module to support Openswitch (OPX) Contorl Plane Services (CPS) * Address ansible-test sanity issues
This commit is contained in:
parent
4b9c2cb97d
commit
a64b97da42
12 changed files with 724 additions and 0 deletions
0
test/units/modules/network/opx/__init__.py
Normal file
0
test/units/modules/network/opx/__init__.py
Normal file
10
test/units/modules/network/opx/fixtures/opx_get_config.cfg
Normal file
10
test/units/modules/network/opx/fixtures/opx_get_config.cfg
Normal file
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"base-if-vlan/if/interfaces/interface/id": 105,
|
||||
"if/interfaces/interface/name": "br105",
|
||||
"dell-base-if-cmn/if/interfaces/interface/if-index": 74,
|
||||
"dell-if/if/interfaces/interface/learning-mode": 1,
|
||||
"dell-if/if/interfaces/interface/mtu": 1532,
|
||||
"dell-if/if/interfaces/interface/phys-address": "",
|
||||
"dell-if/if/interfaces/interface/vlan-type": 1,
|
||||
"if/interfaces/interface/enabled": 0
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"data": {
|
||||
"base-if-vlan/if/interfaces/interface/id": 105,
|
||||
"cps/key_data": {
|
||||
"if/interfaces/interface/name": "br105"
|
||||
},
|
||||
"cps/object-group/return-code": 0,
|
||||
"dell-base-if-cmn/if/interfaces/interface/if-index": 70,
|
||||
"if/interfaces/interface/type": "ianaift:l2vlan"
|
||||
},
|
||||
"key": "target/dell-base-if-cmn/if/interfaces/interface"
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"data": {
|
||||
"cps/object-group/return-code": 0,
|
||||
"if/interfaces/interface/name": "br105"
|
||||
},
|
||||
"key": "target/dell-base-if-cmn/if/interfaces/interface"
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"data": {
|
||||
"base-if-vlan/if/interfaces/interface/id": 105,
|
||||
"cps/key_data": {
|
||||
"if/interfaces/interface/name": "br105"
|
||||
},
|
||||
"dell-base-if-cmn/if/interfaces/interface/if-index": 74,
|
||||
"dell-if/if/interfaces/interface/learning-mode": 1,
|
||||
"dell-if/if/interfaces/interface/mtu": 1532,
|
||||
"dell-if/if/interfaces/interface/phys-address": "",
|
||||
"dell-if/if/interfaces/interface/vlan-type": 1,
|
||||
"if/interfaces/interface/enabled": 0
|
||||
},
|
||||
"key": "target/dell-base-if-cmn/if/interfaces/interface"
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"data": {
|
||||
"base-if-phy/if/interfaces/interface/learn-mode": 3,
|
||||
"base-if-phy/if/interfaces/interface/npu-id": 0,
|
||||
"base-if-phy/if/interfaces/interface/phy-media": 43,
|
||||
"base-if-phy/if/interfaces/interface/port-id": 25,
|
||||
"base-if-phy/if/interfaces/interface/tagging-mode": 3,
|
||||
"dell-base-if-cmn/if/interfaces/interface/if-index": 10,
|
||||
"dell-if/if/interfaces-state/interface/supported-speed": [
|
||||
3,
|
||||
4,
|
||||
6
|
||||
],
|
||||
"dell-if/if/interfaces/interface/auto-negotiation": 1,
|
||||
"dell-if/if/interfaces/interface/duplex": 1,
|
||||
"dell-if/if/interfaces/interface/mode": 1,
|
||||
"dell-if/if/interfaces/interface/mtu": 1532,
|
||||
"dell-if/if/interfaces/interface/phys-address": "ec:f4:bb:fc:61:9a",
|
||||
"dell-if/if/interfaces/interface/speed": 0,
|
||||
"if/interfaces/interface/enabled": 0,
|
||||
"if/interfaces/interface/name": "e101-001-0",
|
||||
"if/interfaces/interface/type": "ianaift:ethernetCsmacd"
|
||||
},
|
||||
"key": "target/dell-base-if-cmn/if/interfaces/interface"
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"data": {
|
||||
"cps/object-group/return-code": 0,
|
||||
"dell-if/if/interfaces/interface/untagged-ports": [
|
||||
"e101-001-0"
|
||||
],
|
||||
"if/interfaces/interface/name": "br105"
|
||||
},
|
||||
"key": "target/dell-base-if-cmn/if/interfaces/interface"
|
||||
}
|
||||
|
91
test/units/modules/network/opx/opx_module.py
Normal file
91
test/units/modules/network/opx/opx_module.py
Normal file
|
@ -0,0 +1,91 @@
|
|||
# (c) 2018 Red Hat Inc.
|
||||
#
|
||||
# (c) 2018 Dell Inc. or its subsidiaries. All Rights Reserved.
|
||||
#
|
||||
# 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 TestOpxModule(ModuleTestCase):
|
||||
|
||||
def execute_module(self, failed=False, changed=False,
|
||||
response=None, msg=None, db=None,
|
||||
commit_event=None):
|
||||
|
||||
self.load_fixtures(response)
|
||||
|
||||
if failed:
|
||||
result = self.failed(msg)
|
||||
self.assertTrue(result['failed'], result)
|
||||
else:
|
||||
result = self.changed(changed, db)
|
||||
self.assertEqual(result['changed'], changed, result)
|
||||
|
||||
return result
|
||||
|
||||
def failed(self, msg):
|
||||
with self.assertRaises(AnsibleFailJson) as exc:
|
||||
self.module.main()
|
||||
|
||||
result = exc.exception.args[0]
|
||||
self.assertTrue(result['failed'], result)
|
||||
self.assertEqual(result['msg'], msg, result)
|
||||
return result
|
||||
|
||||
def changed(self, changed=False, db=None):
|
||||
with self.assertRaises(AnsibleExitJson) as exc:
|
||||
self.module.main()
|
||||
|
||||
result = exc.exception.args[0]
|
||||
print("res" + str(result) + "dv=" + str(db) + "ch=" + str(changed))
|
||||
self.assertEqual(result['changed'], changed, result)
|
||||
if db:
|
||||
self.assertEqual(result['db'], db, result)
|
||||
|
||||
return result
|
||||
|
||||
def load_fixtures(self, response=None):
|
||||
pass
|
183
test/units/modules/network/opx/test_opx_cps.py
Normal file
183
test/units/modules/network/opx/test_opx_cps.py
Normal file
|
@ -0,0 +1,183 @@
|
|||
#
|
||||
# (c) 2018 Red Hat Inc.
|
||||
#
|
||||
# (c) 2018 Dell Inc. or its subsidiaries. All Rights Reserved.
|
||||
#
|
||||
# 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
|
||||
|
||||
from ansible.compat.tests.mock import patch, Mock, MagicMock
|
||||
import sys
|
||||
sys.modules['cps'] = Mock(QUALIFIERS=[
|
||||
"target",
|
||||
"observed",
|
||||
"proposed",
|
||||
"realtime",
|
||||
"registration",
|
||||
"running",
|
||||
"startup"
|
||||
], OPERATIONS=[
|
||||
"delete",
|
||||
"create",
|
||||
"set",
|
||||
"action",
|
||||
"get"
|
||||
])
|
||||
sys.modules['cps_object'] = Mock()
|
||||
sys.modules['cps_utils'] = Mock()
|
||||
|
||||
from ansible.modules.network.opx import opx_cps
|
||||
|
||||
from units.modules.utils import set_module_args
|
||||
from .opx_module import TestOpxModule, load_fixture
|
||||
|
||||
|
||||
class TestOpxCpsModule(TestOpxModule):
|
||||
|
||||
module = opx_cps
|
||||
|
||||
def setUp(self):
|
||||
super(TestOpxCpsModule, self).setUp()
|
||||
|
||||
self.mock_cps_get = patch('ansible.modules.network.opx.opx_cps.cps_get')
|
||||
self.cps_get = self.mock_cps_get.start()
|
||||
|
||||
self.mock_cps_transaction = patch('ansible.modules.network.opx.opx_cps.cps_transaction')
|
||||
self.cps_transaction = self.mock_cps_transaction.start()
|
||||
|
||||
self.mock_parse_cps_parameters = patch('ansible.modules.network.opx.opx_cps.parse_cps_parameters')
|
||||
self.parse_cps_parameters = self.mock_parse_cps_parameters.start()
|
||||
|
||||
self.mock_get_config = patch('ansible.modules.network.opx.opx_cps.get_config')
|
||||
self.get_config = self.mock_get_config.start()
|
||||
|
||||
def tearDown(self):
|
||||
super(TestOpxCpsModule, self).tearDown()
|
||||
self.mock_cps_get.stop()
|
||||
self.mock_cps_transaction.stop()
|
||||
self.mock_parse_cps_parameters.stop()
|
||||
self.mock_get_config.stop()
|
||||
|
||||
def test_opx_operation_create(self):
|
||||
resp = load_fixture('opx_operation_create.cfg')
|
||||
attr_data = {"base-if-vlan/if/interfaces/interface/id": 105,
|
||||
"if/interfaces/interface/type": "ianaift:l2vlan"}
|
||||
module_name = "dell-base-if-cmn/if/interfaces/interface"
|
||||
set_module_args(dict(module_name=module_name, operation="create", attr_data=attr_data))
|
||||
self.get_config.return_value = dict()
|
||||
self.cps_transaction.return_value = dict(changed=True, response=resp)
|
||||
self.execute_module(changed=True, response=resp)
|
||||
self.assertEqual(self.parse_cps_parameters.call_count, 2)
|
||||
self.assertEqual(self.cps_transaction.call_count, 1)
|
||||
|
||||
def test_opx_operation_set(self):
|
||||
resp = load_fixture('opx_operation_set.cfg')
|
||||
config_data = load_fixture('opx_get_config.cfg')
|
||||
attr_data = {"dell-if/if/interfaces/interface/untagged-ports": "e101-001-0",
|
||||
"if/interfaces/interface/name": "br105"}
|
||||
module_name = "dell-base-if-cmn/if/interfaces/interface"
|
||||
set_module_args(dict(module_name=module_name, operation="set", attr_data=attr_data))
|
||||
self.get_config.return_value = config_data
|
||||
self.cps_transaction.return_value = dict(changed=True, response=resp)
|
||||
self.execute_module(changed=True, response=resp)
|
||||
self.assertEqual(self.parse_cps_parameters.call_count, 2)
|
||||
self.assertEqual(self.cps_transaction.call_count, 1)
|
||||
|
||||
def test_opx_operation_delete(self):
|
||||
resp = load_fixture('opx_operation_delete.cfg')
|
||||
config_data = load_fixture('opx_get_config.cfg')
|
||||
attr_data = {"if/interfaces/interface/name": "br105"}
|
||||
module_name = "dell-base-if-cmn/if/interfaces/interface"
|
||||
set_module_args(dict(module_name=module_name, operation="delete", attr_data=attr_data))
|
||||
self.get_config.return_value = config_data
|
||||
self.cps_transaction.return_value = dict(changed=True, response=resp)
|
||||
self.execute_module(changed=True, response=resp)
|
||||
self.assertEqual(self.parse_cps_parameters.call_count, 1)
|
||||
self.assertEqual(self.cps_transaction.call_count, 1)
|
||||
|
||||
def test_opx_operation_delete_fail(self):
|
||||
resp = load_fixture('opx_operation_delete.cfg')
|
||||
attr_data = {"if/interfaces/interface/name": "br105"}
|
||||
module_name = "dell-base-if-cmn/if/interfaces/interface"
|
||||
set_module_args(dict(module_name=module_name, operation="delete", attr_data=attr_data))
|
||||
self.get_config.return_value = dict()
|
||||
self.execute_module(changed=False)
|
||||
self.assertEqual(self.parse_cps_parameters.call_count, 1)
|
||||
self.assertEqual(self.cps_transaction.call_count, 0)
|
||||
|
||||
def test_opx_operation_get(self):
|
||||
resp = load_fixture('opx_operation_get.cfg')
|
||||
attr_data = {"if/interfaces/interface/type": "ianaift:l2vlan"}
|
||||
module_name = "dell-base-if-cmn/if/interfaces/interface"
|
||||
set_module_args(dict(module_name=module_name, operation="get", attr_data=attr_data))
|
||||
self.cps_get.return_value = dict(changed=True, response=resp)
|
||||
self.cps_transaction.return_value = None
|
||||
self.execute_module(changed=True, response=resp)
|
||||
self.assertEqual(self.parse_cps_parameters.call_count, 1)
|
||||
self.assertEqual(self.cps_get.call_count, 1)
|
||||
self.cps_transaction.assert_not_called()
|
||||
|
||||
def test_opx_operation_set_fail(self):
|
||||
attr_data = {"dell-if/if/interfaces/interface/untagged-ports": "e101-001-0",
|
||||
"if/interfaces/interface/name": "br105"}
|
||||
exp_msg = "RuntimeError: Transaction error while set"
|
||||
module_name = "dell-base-if-cmn/if/interfaces/interface"
|
||||
set_module_args(dict(module_name=module_name, operation="set", attr_data=attr_data))
|
||||
self.get_config.return_value = dict()
|
||||
self.cps_transaction.side_effect = RuntimeError("Transaction error while set")
|
||||
self.execute_module(failed=True, msg=exp_msg)
|
||||
self.assertEqual(self.parse_cps_parameters.call_count, 2)
|
||||
self.assertEqual(self.cps_transaction.call_count, 1)
|
||||
|
||||
def test_opx_operation_create_fail(self):
|
||||
attr_data = {"if/interfaces/interface/type": "ianaift:l2vlan"}
|
||||
config_data = load_fixture('opx_get_config.cfg')
|
||||
exp_msg = "RuntimeError: Transaction error while create"
|
||||
module_name = "dell-base-if-cmn/if/interfaces/interface"
|
||||
set_module_args(dict(module_name=module_name, operation="create", attr_data=attr_data))
|
||||
self.get_config.return_value = config_data
|
||||
self.cps_transaction.side_effect = RuntimeError("Transaction error while create")
|
||||
self.execute_module(failed=True, msg=exp_msg)
|
||||
self.assertEqual(self.parse_cps_parameters.call_count, 2)
|
||||
self.assertEqual(self.cps_transaction.call_count, 1)
|
||||
|
||||
def test_opx_operation_get_db(self):
|
||||
resp = load_fixture('opx_operation_get_db.cfg')
|
||||
attr_data = {"if/interfaces/interface/name": "e101-001-0"}
|
||||
module_name = "dell-base-if-cmn/if/interfaces/interface"
|
||||
set_module_args(dict(module_name=module_name, operation="get", attr_data=attr_data, db=True))
|
||||
self.cps_get.return_value = dict(changed=True, response=resp)
|
||||
self.cps_transaction.return_value = None
|
||||
self.execute_module(changed=True, response=resp, db=True)
|
||||
self.assertEqual(self.parse_cps_parameters.call_count, 1)
|
||||
self.assertEqual(self.cps_get.call_count, 1)
|
||||
self.cps_transaction.assert_not_called()
|
||||
|
||||
def test_opx_operation_set_commit_event(self):
|
||||
resp = load_fixture('opx_operation_set.cfg')
|
||||
config_data = load_fixture('opx_get_config.cfg')
|
||||
attr_data = {"dell-if/if/interfaces/interface/untagged-ports": "e101-001-0",
|
||||
"if/interfaces/interface/name": "br105"}
|
||||
module_name = "dell-base-if-cmn/if/interfaces/interface"
|
||||
set_module_args(dict(module_name=module_name, operation="set", attr_data=attr_data, commit_event=True))
|
||||
self.get_config.return_value = config_data
|
||||
self.cps_transaction.return_value = dict(changed=True, response=resp)
|
||||
self.execute_module(changed=True, response=resp, commit_event=True)
|
||||
self.assertEqual(self.parse_cps_parameters.call_count, 2)
|
||||
self.assertEqual(self.cps_transaction.call_count, 1)
|
Loading…
Add table
Add a link
Reference in a new issue