diff --git a/pkg/auth/azure_test.go b/pkg/auth/azure_test.go index 058ebc35f..66645ffa0 100644 --- a/pkg/auth/azure_test.go +++ b/pkg/auth/azure_test.go @@ -44,6 +44,17 @@ func (e errMatcher) Is(err error) bool { return e.Error() == err.Error() } +type prefixErrMatcher struct { + prefix string +} + +func (e prefixErrMatcher) Error() string { + return e.prefix +} + +func (e prefixErrMatcher) Is(err error) bool { + return strings.HasPrefix(err.Error(), e.prefix) +} func TestValidateTokenStatic(t *testing.T) { tcs := []struct { Name string @@ -54,7 +65,7 @@ func TestValidateTokenStatic(t *testing.T) { { Name: "Not a token", Token: "asdf", - ExpectedError: errMatcher{"Failed to parse the JWT.\nError: token is malformed: token contains an invalid number of segments"}, + ExpectedError: prefixErrMatcher{"Failed to parse the JWT."}, }, { Name: "Not initialized", @@ -65,7 +76,7 @@ func TestValidateTokenStatic(t *testing.T) { { Name: "Not a token 2", Token: "asdf.asdf.asdf", - ExpectedError: errMatcher{"Failed to parse the JWT.\nError: token is malformed: could not JSON decode header: invalid character 'j' looking for beginning of value"}, + ExpectedError: prefixErrMatcher{"Failed to parse the JWT."}, }, { Name: "Kid not present", diff --git a/pkg/setup/health_test.go b/pkg/setup/health_test.go index 3b0736389..ef7b27fcc 100644 --- a/pkg/setup/health_test.go +++ b/pkg/setup/health_test.go @@ -213,12 +213,14 @@ background_job_ready{name="a"} 0 } type mockBackoff struct { - called uint - resetted uint + called uint + resetted uint + backOffcalled chan bool } func (b *mockBackoff) NextBackOff() time.Duration { b.called = b.called + 1 + b.backOffcalled <- true return 1 * time.Nanosecond } @@ -238,14 +240,16 @@ func TestHealthReporterRetry(t *testing.T) { ExpectResetCalled uint } tcs := []struct { - Name string + Name string + BackoffChanLength uint Steps []step ExpectError error }{ { - Name: "reports healthy", + Name: "reports healthy", + BackoffChanLength: 1, Steps: []step{ { ReportHealth: HealthReady, @@ -256,7 +260,8 @@ func TestHealthReporterRetry(t *testing.T) { }, }, { - Name: "reports unhealthy if there is an error", + Name: "reports unhealthy if there is an error", + BackoffChanLength: 1, Steps: []step{ { ReturnError: fmt.Errorf("no"), @@ -267,7 +272,8 @@ func TestHealthReporterRetry(t *testing.T) { }, }, { - Name: "doesnt retry permanent errors", + Name: "doesnt retry permanent errors", + BackoffChanLength: 1, Steps: []step{ { ReturnError: Permanent(fmt.Errorf("no")), @@ -279,7 +285,8 @@ func TestHealthReporterRetry(t *testing.T) { ExpectError: errMatcher{"no"}, }, { - Name: "retries some times and resets once it's healthy", + Name: "retries some times and resets once it's healthy", + BackoffChanLength: 3, Steps: []step{ { ReturnError: fmt.Errorf("no"), @@ -314,7 +321,9 @@ func TestHealthReporterRetry(t *testing.T) { t.Run(tc.Name, func(t *testing.T) { stepCh := make(chan step) stateChange := make(chan struct{}, len(tc.Steps)) - bo := &mockBackoff{} + bo := &mockBackoff{ + backOffcalled: make(chan bool, tc.BackoffChanLength), + } hs := HealthServer{} hs.BackOffFactory = func() backoff.BackOff { return bo } ctx, cancel := context.WithCancel(context.Background()) @@ -341,6 +350,9 @@ func TestHealthReporterRetry(t *testing.T) { for _, st := range tc.Steps { stepCh <- st <-stateChange + if st.ReturnError != nil && !st.ExpectReady && tc.ExpectError == nil { + <-bo.backOffcalled + } ready := hs.IsReady("a") if st.ExpectReady != ready { t.Errorf("expected ready status to %t but got %t", st.ExpectReady, ready)