aboutsummaryrefslogtreecommitdiff
path: root/libvirt
diff options
context:
space:
mode:
authorFlavio Castelli <fcastelli@suse.com>2017-03-13 15:32:04 +0100
committerAlvaro <alvaro.saurin@gmail.com>2017-03-14 13:23:13 +0100
commit339afdac28358bf7bc007354d778e5efc3b0358b (patch)
tree26ccb77861833829f722d3c8949e449b445e5bca /libvirt
parent88676415303d221ff6a7b518e56df72577b1b061 (diff)
downloadterraform-provider-libvirt-339afdac28358bf7bc007354d778e5efc3b0358b.tar
terraform-provider-libvirt-339afdac28358bf7bc007354d778e5efc3b0358b.tar.gz
Some code coverage for utils.go
Diffstat (limited to 'libvirt')
-rw-r--r--libvirt/utils.go7
-rw-r--r--libvirt/utils_test.go46
2 files changed, 51 insertions, 2 deletions
diff --git a/libvirt/utils.go b/libvirt/utils.go
index 0700b756..1f8bbfcd 100644
--- a/libvirt/utils.go
+++ b/libvirt/utils.go
@@ -26,6 +26,9 @@ func DiskLetterForIndex(i int) string {
return fmt.Sprintf("%s%c", DiskLetterForIndex(q-1), letter)
}
+var WAIT_SLEEP_INTERVAL time.Duration = 1 * time.Second
+var WAIT_TIMEOUT time.Duration = 5 * time.Minute
+
// wait for success and timeout after 5 minutes.
func WaitForSuccess(errorMessage string, f func() error) error {
start := time.Now()
@@ -36,8 +39,8 @@ func WaitForSuccess(errorMessage string, f func() error) error {
}
log.Printf("[DEBUG] %s. Re-trying.\n", err)
- time.Sleep(1 * time.Second)
- if time.Since(start) > 5*time.Minute {
+ time.Sleep(WAIT_SLEEP_INTERVAL)
+ if time.Since(start) > WAIT_TIMEOUT {
return fmt.Errorf("%s: %s", errorMessage, err)
}
}
diff --git a/libvirt/utils_test.go b/libvirt/utils_test.go
index 62000f34..cdcf05b9 100644
--- a/libvirt/utils_test.go
+++ b/libvirt/utils_test.go
@@ -1,8 +1,10 @@
package libvirt
import (
+ "errors"
"net"
"testing"
+ "time"
)
func TestDiskLetterForIndex(t *testing.T) {
@@ -32,3 +34,47 @@ func TestIPsRange(t *testing.T) {
t.Errorf("unexpected range start for '%s': %s", net, start)
}
}
+
+func TestWaitForSuccessEverythingFine(t *testing.T) {
+ wait_sleep := WAIT_SLEEP_INTERVAL
+ wait_timeout := WAIT_TIMEOUT
+ defer func() {
+ WAIT_SLEEP_INTERVAL = wait_sleep
+ WAIT_TIMEOUT = wait_timeout
+ }()
+
+ WAIT_TIMEOUT = 1 * time.Second
+ WAIT_SLEEP_INTERVAL = 1 * time.Nanosecond
+
+ err := WaitForSuccess(
+ "boom",
+ func() error {
+ return nil
+ })
+
+ if err != nil {
+ t.Errorf("unexpected error %v", err)
+ }
+}
+
+func TestWaitForSuccessBrokenFunction(t *testing.T) {
+ wait_sleep := WAIT_SLEEP_INTERVAL
+ wait_timeout := WAIT_TIMEOUT
+ defer func() {
+ WAIT_SLEEP_INTERVAL = wait_sleep
+ WAIT_TIMEOUT = wait_timeout
+ }()
+
+ WAIT_TIMEOUT = 1 * time.Second
+ WAIT_SLEEP_INTERVAL = 1 * time.Nanosecond
+
+ err := WaitForSuccess(
+ "boom",
+ func() error {
+ return errors.New("something went wrong")
+ })
+
+ if err == nil {
+ t.Error("expected error")
+ }
+}