From e5b2079617814e754b0f24ffaf731134bce07f3a Mon Sep 17 00:00:00 2001 From: Zhongcheng Lao Date: Mon, 4 Nov 2019 17:18:09 +0800 Subject: [PATCH] Adds timeout when waiting for IP and stopped Signed-off-by: Zhongcheng Lao --- drivers/hyperv/hyperv.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/hyperv/hyperv.go b/drivers/hyperv/hyperv.go index 84953347cc..4e5db86389 100644 --- a/drivers/hyperv/hyperv.go +++ b/drivers/hyperv/hyperv.go @@ -2,6 +2,7 @@ package hyperv import ( "encoding/json" + "errors" "fmt" "net" "os" @@ -42,6 +43,7 @@ const ( defaultVLanID = 0 defaultDisableDynamicMemory = false defaultSwitchID = "c08cb7b8-9b3c-408e-8e30-5e16a3aeb444" + defaultTimeout = time.Minute * 10 ) // NewDriver creates a new Hyper-v driver with default settings. @@ -340,12 +342,17 @@ func (d *Driver) chooseVirtualSwitch() (string, error) { func (d *Driver) waitForIP() (string, error) { log.Infof("Waiting for host to start...") + start := time.Now() for { ip, _ := d.GetIP() if ip != "" { return ip, nil } + if time.Since(start) >= defaultTimeout { + return "", errors.New("timeout waiting for IP") + } + time.Sleep(1 * time.Second) } } @@ -354,6 +361,7 @@ func (d *Driver) waitForIP() (string, error) { func (d *Driver) waitStopped() error { log.Infof("Waiting for host to stop...") + start := time.Now() for { s, err := d.GetState() if err != nil { @@ -364,6 +372,10 @@ func (d *Driver) waitStopped() error { return nil } + if time.Since(start) >= defaultTimeout { + return errors.New("timeout waiting for stopped") + } + time.Sleep(1 * time.Second) } }