test/infamy: Generalize wait functionality into "until"

This commit is contained in:
Tobias Waldekranz
2023-11-29 16:57:48 +01:00
committed by Mattias Walström
parent f7e9c44516
commit 5f299ee151
6 changed files with 19 additions and 34 deletions
@@ -3,7 +3,8 @@
import copy
import infamy
import infamy.iface as iface
from infamy.wait import wait_mac_address
from infamy.util import until
with infamy.Test() as test:
with test.step("Initialize"):
@@ -37,6 +38,6 @@ with infamy.Test() as test:
del i["phys-address"]
break
target.put_diff_dicts("ietf-interfaces", running, new)
wait_mac_address(target, tport, pmac)
until(lambda: iface.get_phys_address(target, tport) == pmac)
test.succeed()
+5 -2
View File
@@ -1,8 +1,11 @@
#!/usr/bin/env python3
import infamy
import infamy.iface as iface
import copy
from infamy.wait import wait_links
from infamy import until
def test_ping(hport, should_pass):
with infamy.IsolatedMacVlan(hport) as ns:
pingtest = ns.runsh("""
@@ -55,7 +58,7 @@ with infamy.Test() as test:
})
with test.step("Waiting for links to come up"):
wait_links(target, [tport])
until(lambda: iface.get_oper_up(target, tport))
with test.step("Ping 10.0.0.2 from VLAN 10 on host:data with IP 10.0.0.1"):
_, hport = env.ltop.xlate("host", "data")
+1
View File
@@ -4,6 +4,7 @@ from .env import Env
from .netns import IsolatedMacVlan
from .sniffer import Sniffer
from .tap import Test
from .util import until
def std_topology(name):
return os.path.realpath(
-4
View File
@@ -46,10 +46,6 @@ def get_phys_address(target, iface):
"""Fetch interface MAC address (operational status)"""
return _iface_get_param(target, iface, "phys-address")
def is_phys_address(target,iface,addr):
"""Check if MAC address is a specific value"""
return get_phys_address(target,iface) == addr
def get_oper_up(target,iface):
state=get_oper_status(target,iface)
return state == "up"
+10
View File
@@ -0,0 +1,10 @@
import time
def until(fn, attempts=10, interval=1):
for attempt in range(attempts):
if fn():
return
time.sleep(interval)
raise Exception("Expected condition did not materialize")
-26
View File
@@ -1,26 +0,0 @@
import time
import infamy.iface
def wait(func, *args):
timeout = 10
while(timeout>0):
if len(args) == 0:
f=func()
else:
f=func(*args)
if(f):
return True
timeout-=1
time.sleep(1)
return False
def wait_links(target, ifaces):
for i in ifaces:
if not wait(infamy.iface.get_oper_up, target, i):
raise Exception("Interface did not come up in time.")
def wait_mac_address(target, iface, mac):
if not wait(infamy.iface.is_phys_address, target, iface, mac):
raise Exception("Failed waiting for MAC address")