Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
aomerk committed Jul 6, 2023
1 parent 36b3548 commit e53d863
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 71 deletions.
2 changes: 1 addition & 1 deletion clients/agentgrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (client *client) Close() error {

func isHealthCheckSuccess(invokeErr error, resp *protocol.HealthCheckResponse) bool {
isUnimplemented := invokeErr != nil && status.Code(invokeErr) == codes.Unimplemented
isHealthyResponse := resp.Status == protocol.HealthCheckResponse_SUCCESS
isHealthyResponse := resp != nil && resp.Status == protocol.HealthCheckResponse_SUCCESS
return isUnimplemented || isHealthyResponse
}

Expand Down
61 changes: 61 additions & 0 deletions clients/agentgrpc/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package agentgrpc

import (
"errors"
"testing"

"github.com/forta-network/forta-core-go/protocol"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func TestIsHealthCheckSuccess(t *testing.T) {
// Test case 1: Successful health check
resp := &protocol.HealthCheckResponse{
Status: protocol.HealthCheckResponse_SUCCESS,
}
success := isHealthCheckSuccess(nil, resp)
assert.True(t, success, "Expected health check to be successful")

// Test case 2: Unimplemented method error
invokeErr := status.New(codes.Unimplemented, "").Err()
success = isHealthCheckSuccess(invokeErr, new(protocol.HealthCheckResponse))
assert.True(t, success, "Expected health check to be successful (unimplemented)")

// Test case 3: Other invocation error
invokeErr = errors.New("other error")
success = isHealthCheckSuccess(invokeErr, nil)
assert.False(t, success, "Expected health check to be unsuccessful")
}

func TestExtractHealthCheckError(t *testing.T) {
// Test case 1: No errors
resp := &protocol.HealthCheckResponse{}
err := extractHealthCheckError(nil, resp)
assert.Nil(t, err, "Expected no error")

// Test case 2: Invocation error
invokeErr := errors.New("invoke error")
expectedError := multierror.Append(invokeErr).Error()
err = extractHealthCheckError(invokeErr, new(protocol.HealthCheckResponse))
assert.EqualError(t, err, expectedError, "Unexpected error")

// Test case 3: Response errors
resp = &protocol.HealthCheckResponse{
Errors: []*protocol.Error{
{Message: "error 1"},
{Message: "error 2"},
},
}
err = extractHealthCheckError(nil, resp)
expectedError = "2 errors occurred:\n\t* error 1\n\t* error 2\n\n"
t.Log(err)
assert.EqualError(t, err, expectedError, "Unexpected error")

// Test case 4: Invocation and response errors
err = extractHealthCheckError(invokeErr, resp)
expectedError = "3 errors occurred:\n\t* invoke error\n\t* error 1\n\t* error 2\n\n"
assert.EqualError(t, err, expectedError, "Unexpected error")
}
74 changes: 4 additions & 70 deletions services/components/botio/bot_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,25 +267,11 @@ func (s *BotClientSuite) TestHealthCheck() {
<-s.botClient.Initialized()

ctx := context.Background()
resp := &protocol.HealthCheckResponse{
Status: protocol.HealthCheckResponse_SUCCESS,
}

// Mock HealthCheckAttempt() call
s.lifecycleMetrics.EXPECT().HealthCheckAttempt(gomock.Any())

// Use Do() to modify the request parameter
s.botGrpc.EXPECT().
Invoke(ctx, agentgrpc.MethodHealthCheck, gomock.Any(), gomock.AssignableToTypeOf(resp)).
Do(
func(ctx context.Context, method agentgrpc.Method, actualReq, resp interface{}, _ ...interface{}) {
// Modify the actualReq parameter
if resp, ok := resp.(*protocol.HealthCheckResponse); ok {
resp.Status = protocol.HealthCheckResponse_SUCCESS
}
},
).
Return(nil)
s.botGrpc.EXPECT().DoHealthCheck(ctx).Return(nil)

// Mock HealthCheckSuccess() call
s.lifecycleMetrics.EXPECT().HealthCheckSuccess(gomock.Any())
Expand All @@ -311,69 +297,17 @@ func (s *BotClientSuite) TestHealthCheck_WithError() {
<-s.botClient.Initialized()

ctx := context.Background()
response := &protocol.HealthCheckResponse{
Status: protocol.HealthCheckResponse_ERROR,
Errors: []*protocol.Error{
{
Message: "Error 1",
},
{
Message: "Error 2",
},
},
}

// Mock HealthCheckAttempt() call
s.lifecycleMetrics.EXPECT().HealthCheckAttempt(gomock.Any())

err := fmt.Errorf("health check error")
// Use Do() to modify the request parameter
s.botGrpc.EXPECT().
Invoke(ctx, agentgrpc.MethodHealthCheck, gomock.Any(), gomock.AssignableToTypeOf(response)).
Do(
func(ctx context.Context, method agentgrpc.Method, actualReq, resp interface{}, _ ...interface{}) {
// Modify the actualReq parameter
if resp, ok := resp.(*protocol.HealthCheckResponse); ok {
resp.Status = response.Status
resp.Errors = response.Errors
}
},
).
Return(nil)
s.botGrpc.EXPECT().DoHealthCheck(ctx).Return(err)

// Mock HealthCheckError() call
s.lifecycleMetrics.EXPECT().HealthCheckError(gomock.Any(), gomock.Any())

// Execute the method
result := s.botClient.doHealthCheck(ctx, s.lg)

s.r.False(result, "Expected healthCheck to return false")
}

func (s *BotClientSuite) TestHealthCheck_WithInvokeError() {
s.botGrpc.EXPECT().Initialize(gomock.Any(), gomock.Any()).Return(
&protocol.InitializeResponse{
}, nil,
).AnyTimes()

s.lifecycleMetrics.EXPECT().ClientDial(s.botClient.configUnsafe)
s.lifecycleMetrics.EXPECT().StatusAttached(s.botClient.configUnsafe)
s.lifecycleMetrics.EXPECT().StatusInitialized(s.botClient.configUnsafe)

s.botClient.Initialize()

<-s.botClient.Initialized()

ctx := context.Background()

invokeErr := fmt.Errorf("failed to invoke method")
// Mock HealthCheckAttempt() call
s.lifecycleMetrics.EXPECT().HealthCheckAttempt(gomock.Any())

// Use Do() to modify the request parameter
s.botGrpc.EXPECT().Invoke(ctx, agentgrpc.MethodHealthCheck, gomock.Any(), gomock.Any()).Return(invokeErr)

// Mock HealthCheckError() call
s.lifecycleMetrics.EXPECT().HealthCheckError(gomock.Not(gomock.Nil()), gomock.Any())
s.lifecycleMetrics.EXPECT().HealthCheckError(gomock.Any(), gomock.Any())

// Execute the method
result := s.botClient.doHealthCheck(ctx, s.lg)
Expand Down

0 comments on commit e53d863

Please sign in to comment.