diff options
author | Xu Yuandong <yuandongdeyouxiang@gmail.com> | 2019-10-14 16:14:43 +0200 |
---|---|---|
committer | John R Barker <john@johnrbarker.com> | 2019-10-14 16:14:43 +0200 |
commit | 36457bebc934a84546f0bcc13789067419cd5a63 (patch) | |
tree | 231e7fbdadbbf965354c9d6decb51bba6a30fb8a /test/units | |
parent | ovirt_vm correct graphical port (#63379) (diff) | |
download | ansible-36457bebc934a84546f0bcc13789067419cd5a63.tar.xz ansible-36457bebc934a84546f0bcc13789067419cd5a63.zip |
add a new module ce lacp to manage Eth-Trunk interfaces mode (#58077)
* add a new module to manage lacp
* add a new module to manage lacp
* add a new module to manage lacp
* add a new module to manage lacp
* add a new module to manage lacp
* update for shippable.
* update for shippable
* add units test to module ce_lacp.
* add units test to module ce_lacp.
* add units test to module ce_lacp.
* update
* update
* update
* update
* update
* update
* update for shippable.
* for shippable
* update ignore.txt to reslove conflict
* update for shippable
* update
* update unittest to remove provider.
* update unittest for shipppable.
* use to_native.
* intergration test
* syntax error
* syntax error
* syntax error
* update for `Andersson007` review and thanks.
* update for shippable
Diffstat (limited to 'test/units')
7 files changed, 296 insertions, 0 deletions
diff --git a/test/units/modules/network/cloudengine/__init__.py b/test/units/modules/network/cloudengine/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/test/units/modules/network/cloudengine/__init__.py diff --git a/test/units/modules/network/cloudengine/ce_module.py b/test/units/modules/network/cloudengine/ce_module.py new file mode 100644 index 0000000000..d7990d4138 --- /dev/null +++ b/test/units/modules/network/cloudengine/ce_module.py @@ -0,0 +1,90 @@ +# Copyright (c) 2019 Red Hat +# +# This file is a 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(module_name, name, device=''): + path = os.path.join(fixture_path, module_name, device, name) + if not os.path.exists(path): + path = os.path.join(fixture_path, module_name, name) + + if path in fixture_data: + return fixture_data[path] + + with open(path) as f: + data = f.read() + + try: + data = json.loads(data) + except Exception: + pass + + fixture_data[path] = data + return data + + +class TestCloudEngineModule(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 diff --git a/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_00.txt b/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_00.txt new file mode 100644 index 0000000000..974c52c764 --- /dev/null +++ b/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_00.txt @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1024"> + <data> + <ifmtrunk xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"> + <TrunkIfs> + <TrunkIf> + <ifName>Eth-Trunk10</ifName> + <lacpTrunk> + <isSupportPrmpt>false</isSupportPrmpt> + <rcvTimeoutType>Fast</rcvTimeoutType> + <fastTimeoutUserDefinedValue>3</fastTimeoutUserDefinedValue> + <selectPortStd>Speed</selectPortStd> + <promptDelay>30</promptDelay> + <maxActiveNum>1</maxActiveNum> + <collectMaxDelay>0</collectMaxDelay> + <mixRateEnable>false</mixRateEnable> + <dampStaFlapEn>false</dampStaFlapEn> + <dampUnexpMacEn>false</dampUnexpMacEn> + <trunkSysMac>11-22-33</trunkSysMac> + <trunkPortIdExt>false</trunkPortIdExt> + </lacpTrunk> + </TrunkIf> + </TrunkIfs> + </ifmtrunk> + </data> +</rpc-reply>
\ No newline at end of file diff --git a/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_01.txt b/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_01.txt new file mode 100644 index 0000000000..03b3f31e34 --- /dev/null +++ b/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_01.txt @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1024"> + <data> + <ifmtrunk xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"> + <TrunkIfs> + <TrunkIf> + <ifName>Eth-Trunk10</ifName> + <lacpTrunk> + <isSupportPrmpt>true</isSupportPrmpt> + <rcvTimeoutType>Fast</rcvTimeoutType> + <fastTimeoutUserDefinedValue>10</fastTimeoutUserDefinedValue> + <selectPortStd>Speed</selectPortStd> + <promptDelay>130</promptDelay> + <maxActiveNum>13</maxActiveNum> + <collectMaxDelay>12</collectMaxDelay> + <mixRateEnable>true</mixRateEnable> + <dampStaFlapEn>true</dampStaFlapEn> + <dampUnexpMacEn>true</dampUnexpMacEn> + <trunkSysMac>0000-1111-2222</trunkSysMac> + <trunkPortIdExt>true</trunkPortIdExt> + </lacpTrunk> + </TrunkIf> + </TrunkIfs> + </ifmtrunk> + </data> +</rpc-reply>
\ No newline at end of file diff --git a/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_10.txt b/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_10.txt new file mode 100644 index 0000000000..6abbbbfbe4 --- /dev/null +++ b/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_10.txt @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1024"> + <data> + <ifmtrunk xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"> + <lacpSysInfo> + <priority>32768</priority> + </lacpSysInfo> + </ifmtrunk> + </data> +</rpc-reply>
\ No newline at end of file diff --git a/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_11.txt b/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_11.txt new file mode 100644 index 0000000000..22260aa1e7 --- /dev/null +++ b/test/units/modules/network/cloudengine/fixtures/ce_lacp/ce_lacp_11.txt @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1024"> + <data> + <ifmtrunk xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"> + <lacpSysInfo> + <priority>32769</priority> + </lacpSysInfo> + </ifmtrunk> + </data> +</rpc-reply>
\ No newline at end of file diff --git a/test/units/modules/network/cloudengine/test_ce_lacp.py b/test/units/modules/network/cloudengine/test_ce_lacp.py new file mode 100644 index 0000000000..36ad6664bd --- /dev/null +++ b/test/units/modules/network/cloudengine/test_ce_lacp.py @@ -0,0 +1,134 @@ +# (c) 2019 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 + +from units.compat.mock import patch +from ansible.modules.network.cloudengine import ce_lacp +from units.modules.utils import set_module_args +from .ce_module import TestCloudEngineModule, load_fixture + + +class TestCloudEngineLacpModule(TestCloudEngineModule): + module = ce_lacp + + def setUp(self): + super(TestCloudEngineLacpModule, self).setUp() + + self.mock_get_config = patch('ansible.modules.network.cloudengine.ce_lacp.get_nc_config') + self.get_nc_config = self.mock_get_config.start() + + self.mock_set_config = patch('ansible.modules.network.cloudengine.ce_lacp.set_nc_config') + self.set_nc_config = self.mock_set_config.start() + self.set_nc_config.return_value = None + + def tearDown(self): + super(TestCloudEngineLacpModule, self).tearDown() + self.mock_set_config.stop() + self.mock_get_config.stop() + + def test_lacp_eturnk_present(self): + xml_existing = load_fixture('ce_lacp', 'ce_lacp_00.txt') + xml_end_state = load_fixture('ce_lacp', 'ce_lacp_01.txt') + update = ['lacp max active-linknumber 13', + 'lacp dampening state-flapping', + 'lacp port-id-extension enable', + 'lacp collector delay 12', + 'lacp preempt enable', + 'lacp system-id 0000-1111-2222', + 'lacp mixed-rate link enable', + 'lacp preempt delay 130', + 'lacp timeout user-defined 10', + 'lacp dampening unexpected-mac disable'] + self.get_nc_config.side_effect = (xml_existing, xml_end_state) + set_module_args(dict( + mode='Dynamic', + trunk_id='10', + preempt_enable='true', + state_flapping='true', + port_id_extension_enable='true', + unexpected_mac_disable='true', + system_id='0000-1111-2222', + timeout_type='Fast', + fast_timeout='10', + mixed_rate_link_enable='true', + preempt_delay=11, + collector_delay=12, + max_active_linknumber=13, + select='Speed', + state='present')) + result = self.execute_module(changed=True) + self.assertEquals(sorted(result['updates']), sorted(update)) + + def test_lacp_eturnk_absent(self): + xml_existing = load_fixture('ce_lacp', 'ce_lacp_10.txt') + xml_end_state = load_fixture('ce_lacp', 'ce_lacp_00.txt') + default_values = ['undo lacp priority', + 'lacp timeout Fast', + 'lacp max active-linknumber 1', + 'lacp collector delay 0', + 'lacp preempt enable false', + 'lacp dampening state-flapping false', + 'lacp dampening unexpected-mac disable false', + 'lacp mixed-rate link enable false', + 'lacp port-id-extension enable false', + 'lacp preempt delay 30', + 'lacp select Speed', + 'lacp system-id 11-22-33', + 'lacp timeout user-defined 3'] + self.get_nc_config.side_effect = (xml_existing, xml_end_state) + set_module_args(dict( + mode='Dynamic', + trunk_id='10', + preempt_enable='true', + state_flapping='true', + port_id_extension_enable='true', + unexpected_mac_disable='true', + system_id='0000-1111-2222', + timeout_type='Fast', + fast_timeout='10', + mixed_rate_link_enable='true', + preempt_delay=11, + collector_delay=12, + max_active_linknumber=13, + select='Speed', + state='absent' + )) + result = self.execute_module(changed=True) + self.assertEquals(sorted(result['updates']), sorted(default_values)) + + def test_lacp_global_present(self): + xml_existing = load_fixture('ce_lacp', 'ce_lacp_10.txt') + xml_end_state = load_fixture('ce_lacp', 'ce_lacp_11.txt') + self.get_nc_config.side_effect = (xml_existing, xml_end_state) + set_module_args(dict(global_priority=32769, + state='present')) + result = self.execute_module(changed=True) + self.assertEquals(result['updates'], ['lacp priority 32769']) + + def test_lacp_global_absent(self): + xml_existing = load_fixture('ce_lacp', 'ce_lacp_11.txt') + xml_end_state = load_fixture('ce_lacp', 'ce_lacp_10.txt') + self.get_nc_config.side_effect = (xml_existing, xml_end_state) + set_module_args(dict(global_priority=32769, + state='absent')) + result = self.execute_module(changed=True) + # excpect: lacp priority is set to default value(32768) + self.assertEquals(result['updates'], ['lacp priority 32768']) |