Skip to content

Commit

Permalink
fix(tests): fix TestHealthReporterRetry, TestValidateTokenStatic (#2038)
Browse files Browse the repository at this point in the history
Ref: SRX-AHGZTR
Ref: SRX-TI3JNJ
  • Loading branch information
AminSlk authored Oct 15, 2024
1 parent 5e45cc4 commit 4f76ce2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
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"},
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

0 comments on commit 4f76ce2

Please sign in to comment.