Made WaitForReplicas and EnsureDesiredReplicas use PollImmediate and improved logging.

This commit is contained in:
Andrzej Wasylkowski 2017-06-01 12:13:07 +02:00
parent 30b3472f89
commit c12f4978c2
2 changed files with 25 additions and 19 deletions

View File

@ -332,27 +332,33 @@ func (rc *ResourceConsumer) GetReplicas() int {
} }
func (rc *ResourceConsumer) WaitForReplicas(desiredReplicas int) { func (rc *ResourceConsumer) WaitForReplicas(desiredReplicas int) {
timeout := 15 * time.Minute duration := 15 * time.Minute
for start := time.Now(); time.Since(start) < timeout; time.Sleep(20 * time.Second) { interval := 20 * time.Second
if desiredReplicas == rc.GetReplicas() { err := wait.PollImmediate(interval, duration, func() (bool, error) {
framework.Logf("%s: current replicas number is equal to desired replicas number: %d", rc.kind, desiredReplicas) replicas := rc.GetReplicas()
return framework.Logf("waiting for %d replicas (current: %d)", desiredReplicas, replicas)
} else { return replicas == desiredReplicas, nil // Expected number of replicas found. Exit.
framework.Logf("%s: current replicas number %d waiting to be %d", rc.kind, rc.GetReplicas(), desiredReplicas) })
} framework.ExpectNoErrorWithOffset(1, err, "timeout waiting %v for %d replicas", duration, desiredReplicas)
}
framework.Failf("timeout waiting %v for pods size to be %d", timeout, desiredReplicas)
} }
func (rc *ResourceConsumer) EnsureDesiredReplicas(desiredReplicas int, timeout time.Duration) { func (rc *ResourceConsumer) EnsureDesiredReplicas(desiredReplicas int, duration time.Duration) {
for start := time.Now(); time.Since(start) < timeout; time.Sleep(10 * time.Second) { interval := 10 * time.Second
actual := rc.GetReplicas() err := wait.PollImmediate(interval, duration, func() (bool, error) {
if desiredReplicas != actual { replicas := rc.GetReplicas()
framework.Failf("Number of replicas has changed: expected %v, got %v", desiredReplicas, actual) framework.Logf("expecting there to be %d replicas (are: %d)", desiredReplicas, replicas)
if replicas != desiredReplicas {
return false, fmt.Errorf("number of replicas changed unexpectedly")
} else {
return false, nil // Expected number of replicas found. Continue polling until timeout.
} }
framework.Logf("Number of replicas is as expected") })
// The call above always returns an error, but if it is timeout, it's OK (condition satisfied all the time).
if err == wait.ErrWaitTimeout {
framework.Logf("Number of replicas was stable over %v", duration)
return
} }
framework.Logf("Number of replicas was stable over %v", timeout) framework.ExpectNoErrorWithOffset(1, err)
} }
// Pause stops background goroutines responsible for consuming resources. // Pause stops background goroutines responsible for consuming resources.

View File

@ -294,7 +294,7 @@ func Failf(format string, args ...interface{}) {
func FailfWithOffset(offset int, format string, args ...interface{}) { func FailfWithOffset(offset int, format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...) msg := fmt.Sprintf(format, args...)
log("INFO", msg) log("INFO", msg)
Fail(nowStamp()+": "+msg, 1 + offset) Fail(nowStamp()+": "+msg, 1+offset)
} }
func Skipf(format string, args ...interface{}) { func Skipf(format string, args ...interface{}) {
@ -1932,7 +1932,7 @@ func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) {
if err != nil { if err != nil {
Logf("Unexpected error occurred: %v", err) Logf("Unexpected error occurred: %v", err)
} }
ExpectWithOffset(1 + offset, err).NotTo(HaveOccurred(), explain...) ExpectWithOffset(1+offset, err).NotTo(HaveOccurred(), explain...)
} }
func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interface{}) { func ExpectNoErrorWithRetries(fn func() error, maxRetries int, explain ...interface{}) {