From d2e7af451e90b0b8dc9c840cf04b9fadb380d881 Mon Sep 17 00:00:00 2001 From: Till Maas Date: Thu, 6 Jun 2019 21:44:36 +0200 Subject: [PATCH 1/3] connections unit tests: Fix sys.path manipulation After moving the unit tests into a subdirectory, the path inserts need to go to one more parent directory. --- tests/unit/test_network_connections.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_network_connections.py b/tests/unit/test_network_connections.py index b420ced..7bb1b4b 100755 --- a/tests/unit/test_network_connections.py +++ b/tests/unit/test_network_connections.py @@ -10,8 +10,8 @@ import sys import unittest TESTS_BASEDIR = os.path.dirname(os.path.abspath(__file__)) -sys.path.insert(1, os.path.join(TESTS_BASEDIR, "..", "library")) -sys.path.insert(1, os.path.join(TESTS_BASEDIR, "..", "module_utils")) +sys.path.insert(1, os.path.join(TESTS_BASEDIR, "../..", "library")) +sys.path.insert(1, os.path.join(TESTS_BASEDIR, "../..", "module_utils")) try: from unittest import mock @@ -23,7 +23,7 @@ sys.modules["ansible.module_utils.basic"] = mock.Mock() sys.modules["ansible.module_utils"] = mock.Mock() sys.modules["ansible.module_utils.network_lsr"] = __import__("network_lsr") -# pylint: disable=import-error +# pylint: disable=import-error, wrong-import-position import network_lsr import network_connections as n From 4ad865af8aa1719573ff78bd0a1ba8b9b40f2922 Mon Sep 17 00:00:00 2001 From: Till Maas Date: Thu, 6 Jun 2019 21:49:10 +0200 Subject: [PATCH 2/3] Argument validator: generate Ethernet defaults --- module_utils/network_lsr/argument_validator.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/module_utils/network_lsr/argument_validator.py b/module_utils/network_lsr/argument_validator.py index 413f246..85ebcb5 100644 --- a/module_utils/network_lsr/argument_validator.py +++ b/module_utils/network_lsr/argument_validator.py @@ -486,13 +486,15 @@ class ArgValidator_DictEthernet(ArgValidatorDict): ArgValidatorNum( "speed", val_min=0, val_max=0xFFFFFFFF, default_value=0 ), - ArgValidatorStr("duplex", enum_values=["half", "full"]), + ArgValidatorStr( + "duplex", enum_values=["half", "full"], default_value=None + ), ], default_value=ArgValidator.MISSING, ) def get_default_ethernet(self): - return {"autoneg": None, "speed": 0, "duplex": None} + return dict([(k, v.default_value) for k, v in self.nested.items()]) def _validate_post(self, value, name, result): has_speed_or_duplex = result["speed"] != 0 or result["duplex"] is not None From ab60a47475fe792f0e52fc017883cc34e8fda42a Mon Sep 17 00:00:00 2001 From: Till Maas Date: Thu, 6 Jun 2019 22:05:25 +0200 Subject: [PATCH 3/3] connections unit tests: Introduce ETHERNET_DEFAULTS --- tests/unit/test_network_connections.py | 42 ++++++++++++++------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/tests/unit/test_network_connections.py b/tests/unit/test_network_connections.py index 7bb1b4b..d9918f6 100755 --- a/tests/unit/test_network_connections.py +++ b/tests/unit/test_network_connections.py @@ -67,6 +67,8 @@ def pprint(msg, obj): ARGS_CONNECTIONS = network_lsr.argument_validator.ArgValidator_ListConnections() VALIDATE_ONE_MODE_INITSCRIPTS = ARGS_CONNECTIONS.VALIDATE_ONE_MODE_INITSCRIPTS +ETHERNET_DEFAULTS = {"autoneg": None, "duplex": None, "speed": 0} + class TestValidator(unittest.TestCase): def setUp(self): @@ -74,7 +76,7 @@ class TestValidator(unittest.TestCase): self.default_connection_settings = { "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "ignore_errors": None, "ip": { "gateway6": None, @@ -313,7 +315,7 @@ class TestValidator(unittest.TestCase): "actions": ["present"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "ignore_errors": None, "interface_name": "5", "ip": { @@ -361,7 +363,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "5", @@ -404,7 +406,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": False, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "5", @@ -484,7 +486,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": None, @@ -544,7 +546,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "prod1", @@ -601,7 +603,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": None, @@ -647,7 +649,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "prod.100", @@ -736,7 +738,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": None, @@ -782,7 +784,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "prod.100", @@ -871,7 +873,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "eth0", @@ -1059,7 +1061,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "bridge2", @@ -1094,7 +1096,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "eth1", @@ -1153,7 +1155,7 @@ class TestValidator(unittest.TestCase): "autoconnect": True, "bond": {"mode": "balance-rr", "miimon": None}, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "bond1", @@ -1197,7 +1199,7 @@ class TestValidator(unittest.TestCase): "autoconnect": True, "bond": {"mode": "active-backup", "miimon": None}, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "bond1", @@ -1252,7 +1254,7 @@ class TestValidator(unittest.TestCase): "actions": ["present"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "ignore_errors": None, "interface_name": None, "ip": { @@ -1363,7 +1365,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "6643-master", @@ -1398,7 +1400,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "6643", @@ -1592,7 +1594,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "555", @@ -1682,7 +1684,7 @@ class TestValidator(unittest.TestCase): "actions": ["present", "up"], "autoconnect": True, "check_iface_exists": True, - "ethernet": {"autoneg": None, "duplex": None, "speed": 0}, + "ethernet": ETHERNET_DEFAULTS, "force_state_change": None, "ignore_errors": None, "interface_name": "e556",