From faffb6e4cdd285b12d0fbd98ae15900eec9f2d4e Mon Sep 17 00:00:00 2001 From: Edward Haas Date: Thu, 6 Jun 2019 22:09:41 +0300 Subject: [PATCH] tests integ: Introduce a link state monitor decorator Link state monitoring is used to detect link state changes while executing apply changes. In particular, to detect links that change to anything but UP. In order to allow simpler usage in selected tests, a decorator is introduced in addition to the existing context-manager. Note: six is used to overcome functools.wraps limitations with pytest (https://github.com/pytest-dev/pytest/issues/2782) Signed-off-by: Edward Haas --- tests/integration/nm/ipv4_test.py | 24 +++++++++++------------- tests/integration/testlib/iproutelib.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/tests/integration/nm/ipv4_test.py b/tests/integration/nm/ipv4_test.py index 30b8c12fad..101a199dbb 100644 --- a/tests/integration/nm/ipv4_test.py +++ b/tests/integration/nm/ipv4_test.py @@ -26,20 +26,18 @@ IPV4_ADDRESS1 = '192.0.2.251' +@iproutelib.ip_monitor_assert_stable_link_up(TEST_IFACE) def test_interface_ipv4_change(eth1_up): - with iproutelib.ip_monitor(object_type='link', dev=TEST_IFACE) as result: - with mainloop(): - _modify_interface( - ipv4_state={ - 'enabled': True, - 'dhcp': False, - 'address': [ - {'ip': IPV4_ADDRESS1, 'prefix-length': 24} - ] - } - ) - - assert len(iproutelib.get_non_up_events(result, dev='eth1')) == 0 + with mainloop(): + _modify_interface( + ipv4_state={ + 'enabled': True, + 'dhcp': False, + 'address': [ + {'ip': IPV4_ADDRESS1, 'prefix-length': 24} + ] + } + ) nm.nmclient.client(refresh=True) ipv4_current_state = _get_ipv4_current_state(TEST_IFACE) diff --git a/tests/integration/testlib/iproutelib.py b/tests/integration/testlib/iproutelib.py index 7a69c3f101..2b12b6442c 100644 --- a/tests/integration/testlib/iproutelib.py +++ b/tests/integration/testlib/iproutelib.py @@ -20,6 +20,8 @@ import threading import time +import six + TIMEOUT = 10 @@ -31,6 +33,18 @@ def __init__(self): self.popen = None +def ip_monitor_assert_stable_link_up(dev, timeout=10): + def decorator(func): + @six.wraps(func) + def wrapper_ip_monitor(*args, **kwargs): + with ip_monitor('link', dev, timeout) as result: + func(*args, **kwargs) + assert len(get_non_up_events(result, dev)) == 0, ('result: ' + + result.out) + return wrapper_ip_monitor + return decorator + + @contextmanager def ip_monitor(object_type, dev, timeout=10): result = IpMonitorResult()