Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(service wait): Wrong check for error window #661

Merged
merged 1 commit into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/wait/wait_for_ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type waitForReadyConfig struct {
type WaitForReady interface {

// Wait on resource the resource with this name until a given timeout
// and write event messages for unknown event to the status writer
// and write event messages for unknown event to the status writer.
// Returns an error (if any) and the overall time it took to wait
Wait(name string, timeout time.Duration, msgCallback MessageCallback) (error, time.Duration)
}

Expand Down Expand Up @@ -161,9 +162,10 @@ func (w *waitForReadyConfig) waitForReadyCondition(start time.Time, name string,
// to a true condition even after the condition went to false. If this is not the case within
// this window, then an error is returned.
// If there is already a timer running, we just log.
if errorTimer != nil {
if errorTimer == nil {
err := fmt.Errorf("%s: %s", cond.Reason, cond.Message)
errorTimer = time.AfterFunc(errorWindow, func() {
errChan <- fmt.Errorf("%s: %s", cond.Reason, cond.Message)
errChan <- err
})
}
}
Expand Down
50 changes: 30 additions & 20 deletions pkg/wait/wait_for_ready_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
type waitForReadyTestCase struct {
events []watch.Event
timeout time.Duration
errorExpected bool
errorText string
messagesExpected []string
}

Expand All @@ -54,12 +54,16 @@ func TestAddWaitForReady(t *testing.T) {
})
close(fakeWatchApi.eventChan)

if !tc.errorExpected && err != nil {
if tc.errorText == "" && err != nil {
t.Errorf("%d: Error received %v", i, err)
continue
}
if tc.errorExpected && err == nil {
t.Errorf("%d: No error but expected one", i)
if tc.errorText != "" {
if err == nil {
t.Errorf("%d: No error but expected one", i)
} else {
assert.ErrorContains(t, err, tc.errorText)
}
}

// check messages
Expand All @@ -75,20 +79,34 @@ func TestAddWaitForReady(t *testing.T) {
// Test cases which consists of a series of events to send and the expected behaviour.
func prepareTestCases(name string) []waitForReadyTestCase {
return []waitForReadyTestCase{
tc(peNormal, name, false),
tc(peError, name, true),
tc(peWrongGeneration, name, true),
tc(peTimeout, name, true),
tc(peReadyFalseWithinErrorWindow, name, false),
errorTest(name),
tc(peNormal, name, time.Second, ""),
tc(peWrongGeneration, name, 1*time.Second, "timeout"),
tc(peTimeout, name, time.Second, "timeout"),
tc(peReadyFalseWithinErrorWindow, name, time.Second, ""),
}
}

func tc(f func(name string) (evts []watch.Event, nrMessages int), name string, isError bool) waitForReadyTestCase {
func errorTest(name string) waitForReadyTestCase {
events := []watch.Event{
{watch.Added, CreateTestServiceWithConditions(name, corev1.ConditionUnknown, corev1.ConditionUnknown, "", "msg1")},
{watch.Modified, CreateTestServiceWithConditions(name, corev1.ConditionFalse, corev1.ConditionTrue, "FakeError", "Test Error")},
}

return waitForReadyTestCase{
events: events,
timeout: 3 * time.Second,
errorText: "FakeError",
messagesExpected: []string{"msg1", "Test Error"},
}
}

func tc(f func(name string) (evts []watch.Event, nrMessages int), name string, timeout time.Duration, errorTxt string) waitForReadyTestCase {
events, nrMsgs := f(name)
return waitForReadyTestCase{
events,
time.Second,
isError,
timeout,
errorTxt,
pMessages(nrMsgs),
}
}
Expand All @@ -110,14 +128,6 @@ func peNormal(name string) ([]watch.Event, int) {
}, len(messages)
}

func peError(name string) ([]watch.Event, int) {
messages := pMessages(1)
return []watch.Event{
{watch.Added, CreateTestServiceWithConditions(name, corev1.ConditionUnknown, corev1.ConditionUnknown, "", messages[0])},
{watch.Modified, CreateTestServiceWithConditions(name, corev1.ConditionFalse, corev1.ConditionTrue, "FakeError", "")},
}, len(messages)
}

func peTimeout(name string) ([]watch.Event, int) {
messages := pMessages(1)
return []watch.Event{
Expand Down