summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGomathiselviS <gomathiselvi@gmail.com>2020-08-10 11:13:45 -0400
committerGitHub <noreply@github.com>2020-08-10 15:13:45 +0000
commita69b936ac650fee178a5c07c74db6aeea739267a (patch)
treeb23d6462eb2ef7e6cfd72c430354beda5a836770
parentf88b875dd9377951b973cdfe871f7dd4a19b09f1 (diff)
downloadvyos.vyos-a69b936ac650fee178a5c07c74db6aeea739267a.tar.gz
vyos.vyos-a69b936ac650fee178a5c07c74db6aeea739267a.zip
vyos_interface : refactor testcases (#71)
vyos_interface : refactor testcases Reviewed-by: https://github.com/apps/ansible-zuul
-rw-r--r--changelogs/fragments/71-refactor-interface-test.yaml3
-rw-r--r--plugins/modules/vyos_interface.py19
-rw-r--r--tests/integration/targets/vyos_interface/tests/cli/intent.yaml162
-rw-r--r--tests/unit/modules/network/vyos/fixtures/vyos_interface_config.cfg6
-rw-r--r--tests/unit/modules/network/vyos/fixtures/vyos_lldp_neighbor_config.cfg68
-rw-r--r--tests/unit/modules/network/vyos/test_vyos_interface.py231
6 files changed, 322 insertions, 167 deletions
diff --git a/changelogs/fragments/71-refactor-interface-test.yaml b/changelogs/fragments/71-refactor-interface-test.yaml
new file mode 100644
index 0000000..55c4e57
--- /dev/null
+++ b/changelogs/fragments/71-refactor-interface-test.yaml
@@ -0,0 +1,3 @@
+---
+minor_changes:
+ - Moved intent testcases from integration suite to unit tests.
diff --git a/plugins/modules/vyos_interface.py b/plugins/modules/vyos_interface.py
index 839e559..21b0dc2 100644
--- a/plugins/modules/vyos_interface.py
+++ b/plugins/modules/vyos_interface.py
@@ -277,7 +277,6 @@ def search_obj_in_list(name, lst):
def map_obj_to_commands(updates):
commands = list()
want, have = updates
-
params = ("speed", "description", "duplex", "mtu")
for w in want:
name = w["name"]
@@ -402,6 +401,18 @@ def map_params_to_obj(module):
return obj
+def get_interfaces_data(module, name):
+ command = "show interfaces ethernet %s" % name
+ rc, out, err = exec_command(module, command)
+ return [rc, out, err]
+
+
+def get_lldp_neighbor(module):
+ command = "show lldp neighbors detail"
+ rc, out, err = exec_command(module, command)
+ return [rc, out, err]
+
+
def check_declarative_intent_params(module, want, result):
failed_conditions = []
have_neighbors = None
@@ -416,7 +427,7 @@ def check_declarative_intent_params(module, want, result):
sleep(w["delay"])
command = "show interfaces ethernet %s" % w["name"]
- rc, out, err = exec_command(module, command)
+ rc, out, err = get_interfaces_data(w["name"], module)
if rc != 0:
module.fail_json(
msg=to_text(err, errors="surrogate_then_replace"),
@@ -438,9 +449,7 @@ def check_declarative_intent_params(module, want, result):
have_host = []
have_port = []
if have_neighbors is None:
- rc, have_neighbors, err = exec_command(
- module, "show lldp neighbors detail"
- )
+ rc, have_neighbors, err = get_lldp_neighbor(module)
if rc != 0:
module.fail_json(
msg=to_text(err, errors="surrogate_then_replace"),
diff --git a/tests/integration/targets/vyos_interface/tests/cli/intent.yaml b/tests/integration/targets/vyos_interface/tests/cli/intent.yaml
deleted file mode 100644
index e3245d3..0000000
--- a/tests/integration/targets/vyos_interface/tests/cli/intent.yaml
+++ /dev/null
@@ -1,162 +0,0 @@
----
-- debug: msg="START cli/intent.yaml on connection={{ ansible_connection }}"
-
-- name: Detect if we have existing lldp neighbors configured
- register: neighbors_out
- vyos.vyos.vyos_command:
- commands:
- - show lldp neighbors detail
-
-
-# setting a variable dynamically using set_fact isn't working in devel.
-# - name: Should we run lldp tests?
-# set_fact:
-# run_lldp_tests: "'PortDescr: eth0' in neighbors_out.stdout[0]"
-
-- name: Enable LLDP service
- when: "'PortDescr: eth0' in neighbors_out.stdout[0]"
- vyos.vyos.vyos_lldp:
- state: present
-
-- name: Create LLDP configuration
- when: "'PortDescr: eth0' in neighbors_out.stdout[0]"
- vyos.vyos.vyos_lldp_interface:
- name: eth1
- state: present
-
-- name: Setup (interface is up)
- register: result
- vyos.vyos.vyos_interface:
- name: eth1
- enabled: true
- state: present
-
-- name: Check intent arguments
- register: result
- vyos.vyos.vyos_interface:
- name: eth1
- state: up
-
-- assert:
- that:
- - result.failed == false
-
-- name: Check lldp neighbors intent arguments
- when: "'PortDescr: eth0' in neighbors_out.stdout[0]"
- register: result
- vyos.vyos.vyos_interface:
- name: eth0
- neighbors:
-
- - port: eth0
-
-- assert:
- that:
- - result.failed == false
- when: "'PortDescr: eth0' in neighbors_out.stdout[0]"
-
-- name: Check intent arguments (failed condition)
- ignore_errors: true
- register: result
- vyos.vyos.vyos_interface:
- name: eth1
- state: down
-
-- assert:
- that:
- - result.failed == true
- - "'state eq(down)' in result.failed_conditions"
-
-- name: Check lldp neighbors intent arguments (failed)
- ignore_errors: true
- when: "'PortDescr: eth0' in neighbors_out.stdout[0]"
- register: result
- vyos.vyos.vyos_interface:
- name: eth0
- neighbors:
-
- - port: dummy_port
- host: dummy_host
-
-- assert:
- that:
- - result.failed == true
- - "'host dummy_host' in result.failed_conditions"
- - "'port dummy_port' in result.failed_conditions"
- when: "'PortDescr: eth0' in neighbors_out.stdout[0]"
-
-- name: Config + intent
- register: result
- vyos.vyos.vyos_interface:
- name: eth1
- enabled: false
- state: down
-
-- assert:
- that:
- - result.failed == false
-
-- name: Config + intent (fail)
- ignore_errors: true
- register: result
- vyos.vyos.vyos_interface:
- name: eth1
- enabled: false
- state: up
-
-- assert:
- that:
- - result.failed == true
- - "'state eq(up)' in result.failed_conditions"
-
-- name: Aggregate config + intent (pass)
- ignore_errors: true
- register: result
- vyos.vyos.vyos_interface:
- aggregate:
-
- - name: eth1
- enabled: true
- state: up
-
-- assert:
- that:
- - result.failed == false
-
-- name: Check lldp neighbors intent aggregate arguments
- when: "'PortDescr: eth0' in neighbors_out.stdout[0]"
- register: result
- vyos.vyos.vyos_interface:
- aggregate:
-
- - name: eth0
- neighbors:
-
- - port: eth0
-
-- assert:
- that:
- - result.failed == false
- when: "'PortDescr: eth0' in neighbors_out.stdout[0]"
-
-- name: Check lldp neighbors intent aggregate arguments (failed)
- ignore_errors: true
- when: "'PortDescr: eth0' in neighbors_out.stdout[0]"
- register: result
- vyos.vyos.vyos_interface:
- aggregate:
-
- - name: eth0
- neighbors:
-
- - port: eth0
-
- - port: dummy_port
- host: dummy_host
-
-- assert:
- that:
- - result.failed == true
- - "'host dummy_host' in result.failed_conditions"
- - "'port dummy_port' in result.failed_conditions"
- when: "'PortDescr: eth0' in neighbors_out.stdout[0]"
diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_interface_config.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_interface_config.cfg
new file mode 100644
index 0000000..0876916
--- /dev/null
+++ b/tests/unit/modules/network/vyos/fixtures/vyos_interface_config.cfg
@@ -0,0 +1,6 @@
+set interfaces ethernet eth0 address 'dhcp'
+set interfaces ethernet eth0 hw-id '08:00:27:7c:85:05'
+set interfaces ethernet eth1 description 'test-interface'
+set interfaces ethernet eth2 hw-id '08:00:27:04:85:99'
+set interfaces ethernet eth3 hw-id '08:00:27:1c:82:d1'
+set interfaces loopback 'lo'
diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_lldp_neighbor_config.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_lldp_neighbor_config.cfg
new file mode 100644
index 0000000..267958c
--- /dev/null
+++ b/tests/unit/modules/network/vyos/fixtures/vyos_lldp_neighbor_config.cfg
@@ -0,0 +1,68 @@
+-------------------------------------------------------------------------------
+LLDP neighbors:
+-------------------------------------------------------------------------------
+Interface: eth1, via: LLDP, RID: 1, Time: 0 day, 00:30:34
+ Chassis:
+ ChassisID: mac 08:00:27:7c:85:05
+ SysName: vyos
+ SysDescr: Vyatta Router running on VyOS 1.1.8 (helium)
+ MgmtIP: 192.168.56.101
+ Capability: Bridge, off
+ Capability: Router, on
+ Capability: Wlan, off
+ Port:
+ PortID: mac 08:00:27:04:85:99
+ PortDescr: eth0
+ PMD autoneg: supported: yes, enabled: yes
+ Adv: 10Base-T, HD: yes, FD: yes
+ Adv: 100Base-T, HD: yes, FD: yes
+ Adv: 1000Base-T, HD: no, FD: yes
+ MAU oper type: 1000BaseTFD - Four-pair Category 5 UTP, full duplex mode
+ LLDP-MED:
+ Device Type: Network Connectivity Device
+ Capability: Capabilities
+ Capability: Policy
+ Capability: Location
+ Capability: MDI/PSE
+ Capability: MDI/PD
+ Capability: Inventory
+ Inventory:
+ Hardware Revision: 1.2
+ Software Revision: 3.13.11-1-586-vyos
+ Firmware Revision: VirtualBox
+ Serial Number: 0
+ Manufacturer: innotek GmbH
+ Model: VirtualBox
+-------------------------------------------------------------------------------
+Interface: eth0, via: LLDP, RID: 1, Time: 0 day, 00:32:50
+ Chassis:
+ ChassisID: mac 08:00:27:7c:85:05
+ SysName: vyos
+ SysDescr: Vyatta Router running on VyOS 1.1.8 (helium)
+ MgmtIP: 192.168.56.101
+ Capability: Bridge, off
+ Capability: Router, on
+ Capability: Wlan, off
+ Port:
+ PortID: mac 08:00:27:18:e6:f2
+ PortDescr: eth1
+ PMD autoneg: supported: yes, enabled: yes
+ Adv: 10Base-T, HD: yes, FD: yes
+ Adv: 100Base-T, HD: yes, FD: yes
+ Adv: 1000Base-T, HD: no, FD: yes
+ MAU oper type: 1000BaseTFD - Four-pair Category 5 UTP, full duplex mode
+ LLDP-MED:
+ Device Type: Network Connectivity Device
+ Capability: Capabilities
+ Capability: Policy
+ Capability: Location
+ Capability: MDI/PSE
+ Capability: MDI/PD
+ Capability: Inventory
+ Inventory:
+ Hardware Revision: 1.2
+ Software Revision: 3.13.11-1-586-vyos
+ Firmware Revision: VirtualBox
+ Serial Number: 0
+ Manufacturer: innotek GmbH
+ Model: VirtualBox
diff --git a/tests/unit/modules/network/vyos/test_vyos_interface.py b/tests/unit/modules/network/vyos/test_vyos_interface.py
new file mode 100644
index 0000000..96bc9f6
--- /dev/null
+++ b/tests/unit/modules/network/vyos/test_vyos_interface.py
@@ -0,0 +1,231 @@
+# (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
+
+from ansible_collections.vyos.vyos.tests.unit.compat.mock import patch
+from ansible_collections.vyos.vyos.plugins.modules import vyos_interface
+from ansible_collections.vyos.vyos.tests.unit.modules.utils import (
+ set_module_args,
+)
+from .vyos_module import TestVyosModule, load_fixture
+
+
+class TestVyosInterfaceModule(TestVyosModule):
+
+ module = vyos_interface
+
+ def setUp(self):
+ super(TestVyosInterfaceModule, self).setUp()
+ self.mock_get_config = patch(
+ "ansible_collections.vyos.vyos.plugins.modules.vyos_interface.get_config"
+ )
+ self.get_config = self.mock_get_config.start()
+
+ self.mock_load_config = patch(
+ "ansible_collections.vyos.vyos.plugins.modules.vyos_interface.load_config"
+ )
+ self.load_config = self.mock_load_config.start()
+
+ self.mock_execute_interfaces_command = patch(
+ "ansible_collections.vyos.vyos.plugins.modules.vyos_interface.get_interfaces_data"
+ )
+ self.execute_interfaces_command = (
+ self.mock_execute_interfaces_command.start()
+ )
+ self.mock_execute_lldp_command = patch(
+ "ansible_collections.vyos.vyos.plugins.modules.vyos_interface.get_lldp_neighbor"
+ )
+ self.execute_lldp_command = self.mock_execute_lldp_command.start()
+
+ # self.mock_get_config = patch(
+ # "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.network.Config.get_config"
+ # )
+ # self.get_config = self.mock_get_config.start()
+
+ # self.mock_load_config = patch(
+ # "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.network.Config.load_config"
+ # )
+ # self.load_config = self.mock_load_config.start()
+
+ # self.mock_get_resource_connection_config = patch(
+ # "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.cfg.base.get_resource_connection"
+ # )
+ # self.get_resource_connection_config = (
+ # self.mock_get_resource_connection_config.start()
+ # )
+
+ # self.mock_get_resource_connection_facts = patch(
+ # "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.facts.facts.get_resource_connection"
+ # )
+ # self.get_resource_connection_facts = (
+ # self.mock_get_resource_connection_facts.start()
+ # )
+
+ def tearDown(self):
+ super(TestVyosInterfaceModule, self).tearDown()
+ self.mock_get_config.stop()
+ self.mock_load_config.stop()
+ self.mock_execute_lldp_command.stop()
+ self.mock_execute_interfaces_command.stop()
+
+ def load_fixtures(self, commands=None, transport="cli"):
+ self.get_config.return_value = load_fixture(
+ "vyos_interface_config.cfg"
+ )
+ self.execute_interfaces_command.return_value = [
+ 0,
+ load_fixture("vyos_interface_config.cfg"),
+ None,
+ ]
+ self.execute_lldp_command.return_value = [
+ 0,
+ load_fixture("vyos_lldp_neighbor_config.cfg"),
+ None,
+ ]
+ self.load_config.return_value = dict(diff=None, session="session")
+
+ def test_vyos_setup_int(self):
+ set_module_args(
+ dict(
+ name="eth1",
+ enabled=True,
+ state="present",
+ speed="100",
+ duplex="half",
+ )
+ )
+ commands = [
+ "set interfaces ethernet eth1 speed 100",
+ "set interfaces ethernet eth1 duplex half",
+ ]
+ self.execute_module(changed=True, commands=commands)
+
+ def test_vyos_setup_required_params(self):
+ set_module_args(
+ dict(name="eth1", enabled=True, state="present", speed="100",)
+ )
+ result = self.execute_module(failed=True)
+ self.assertIn(
+ "parameters are required together: speed, duplex", result["msg"]
+ )
+
+ def test_vyos_setup_int_idempotent(self):
+ set_module_args(dict(name="eth1", enabled=True, state="present",))
+ self.execute_module(changed=False, commands=[])
+
+ def test_vyos_disable_int(self):
+ set_module_args(dict(name="eth1", state="absent",))
+ commands = ["delete interfaces ethernet eth1"]
+ self.execute_module(changed=True, commands=commands)
+
+ def test_vyos_setup_int_aggregate(self):
+ set_module_args(
+ dict(
+ aggregate=[
+ dict(
+ name="eth1",
+ enabled=True,
+ state="present",
+ mtu="512",
+ duplex="half",
+ speed="100",
+ ),
+ dict(
+ name="eth2",
+ enabled=True,
+ state="present",
+ speed="1000",
+ duplex="full",
+ mtu="256",
+ ),
+ ]
+ )
+ )
+ commands = [
+ "set interfaces ethernet eth1 speed 100",
+ "set interfaces ethernet eth1 duplex half",
+ "set interfaces ethernet eth1 mtu 512",
+ "set interfaces ethernet eth2 speed 1000",
+ "set interfaces ethernet eth2 duplex full",
+ "set interfaces ethernet eth2 mtu 256",
+ ]
+ self.execute_module(changed=True, commands=commands)
+
+ def test_vyos_delete_int_aggregate(self):
+ set_module_args(
+ dict(
+ aggregate=[
+ dict(name="eth1", state="absent",),
+ dict(name="eth2", state="absent",),
+ ]
+ )
+ )
+ commands = [
+ "delete interfaces ethernet eth1",
+ "delete interfaces ethernet eth2",
+ ]
+ self.execute_module(changed=True, commands=commands)
+
+ def test_vyos_disable_int_aggregate(self):
+ set_module_args(
+ dict(
+ aggregate=[
+ dict(name="eth1", enabled=False,),
+ dict(name="eth2", enabled=False,),
+ ]
+ )
+ )
+ commands = [
+ "set interfaces ethernet eth1 disable",
+ "set interfaces ethernet eth2 disable",
+ ]
+ self.execute_module(changed=True, commands=commands)
+
+ def test_vyos_intent_wrongport(self):
+ set_module_args(
+ dict(
+ name="eth0",
+ neighbors=[dict(port="dummy_port", host="dummy_host")],
+ )
+ )
+ result = self.execute_module(failed=True)
+ self.assertIn(
+ "One or more conditional statements have not been satisfied",
+ result["msg"],
+ )
+
+ def test_vyos_intent_neighbor_fail(self):
+ set_module_args(dict(name="eth0", neighbors=[dict(port="eth0",)]))
+ result = self.execute_module(failed=True)
+ self.assertIn(
+ "One or more conditional statements have not been satisfied",
+ result["msg"],
+ )
+
+ def test_vyos_intent_neighbor(self):
+ set_module_args(dict(name="eth1", neighbors=[dict(port="eth0",)]))
+ self.execute_module(failed=False)
+
+ def test_vyos_intent_neighbor_aggregate(self):
+ set_module_args(
+ dict(aggregate=[dict(name="eth1", neighbors=[dict(port="eth0",)])])
+ )
+ self.execute_module(failed=False)