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(tests): fix TestHealthReporterRetry, TestValidateTokenStatic #2038

Merged
merged 4 commits into from
Oct 15, 2024
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
15 changes: 13 additions & 2 deletions pkg/auth/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"},
AminSlk marked this conversation as resolved.
Show resolved Hide resolved
ExpectedError: prefixErrMatcher{"Failed to parse the JWT."},
},
{
Name: "Not initialized",
Expand All @@ -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",
Expand Down
28 changes: 20 additions & 8 deletions pkg/setup/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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,
Expand All @@ -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"),
Expand All @@ -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")),
Expand All @@ -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"),
Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand Down
Loading