From 9cc0c3eed756f372c9d645ea41592406ce878f8a Mon Sep 17 00:00:00 2001
From: Alban Bedel <albeu@free.fr>
Date: Sat, 5 Oct 2019 23:19:50 +0200
Subject: [PATCH] ifconfig.py: Wait for interface state to really change

With some interfaces, for example bond vif, it take some time for the
state change to really happen. Because of this later code, like
starting the dhcp client, might not work as expected as get_state()
still report the old state.

To cope with this add an extra 'wait' argument to set_state() that
give how long we should wait for the state to change. If the state
is not reached an exception is raised, per default we wait up to 10
seconds which should be more than enought. The old behavior is still
possible by setting the wait time to 0.

Signed-off-by: Alban Bedel <albeu@free.fr>
---
 python/vyos/ifconfig.py | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/python/vyos/ifconfig.py b/python/vyos/ifconfig.py
index a77cde5..8633ec4 100644
--- a/python/vyos/ifconfig.py
+++ b/python/vyos/ifconfig.py
@@ -277,7 +277,7 @@ class Interface:
         return self._read_sysfs('/sys/class/net/{}/operstate'
                                 .format(self._ifname))
 
-    def set_state(self, state):
+    def set_state(self, state, wait=10.0):
         """
         Enable (up) / Disable (down) an interface
 
@@ -294,6 +294,13 @@ class Interface:
         # to up/down an interface via sysfs
         cmd = 'ip link set dev {} {}'.format(self._ifname, state)
         self._cmd(cmd)
+        if wait > 0:
+            while wait > 0:
+                if self.get_state() == state:
+                    return
+                sleep(0.01)
+                wait -= 0.01
+            raise Exception('Interface failed to change state')
 
     def set_proxy_arp(self, enable):
         """
-- 
2.20.1

