diff --git a/internal/controlplane/handlers.go b/internal/controlplane/handlers.go index 011be38fcb..fed9447482 100644 --- a/internal/controlplane/handlers.go +++ b/internal/controlplane/handlers.go @@ -25,6 +25,9 @@ package controlplane import ( "context" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" ) @@ -32,11 +35,9 @@ import ( const PaginationLimit = 10 // CheckHealth is a simple health check for monitoring -// The lintcheck is disabled because the unused-receiver is required by -// the implementation. UnimplementedHealthServiceServer is initialized -// within the Server struct -// -//revive:disable:unused-receiver func (s *Server) CheckHealth(_ context.Context, _ *pb.CheckHealthRequest) (*pb.CheckHealthResponse, error) { + if err := s.store.CheckHealth(); err != nil { + return nil, status.Errorf(codes.Internal, "failed to check health: %v", err) + } return &pb.CheckHealthResponse{Status: "OK"}, nil } diff --git a/internal/controlplane/handlers_test.go b/internal/controlplane/handlers_test.go index ffd39e8f74..bb5a22dba1 100644 --- a/internal/controlplane/handlers_test.go +++ b/internal/controlplane/handlers_test.go @@ -18,13 +18,25 @@ import ( "context" "testing" + "github.com/golang/mock/gomock" + + mockdb "github.com/stacklok/minder/database/mock" pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" ) func TestCheckHealth(t *testing.T) { t.Parallel() - server := Server{} + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockStore := mockdb.NewMockStore(ctrl) + + mockStore.EXPECT().CheckHealth().Return(nil) + + server := Server{ + store: mockStore, + } response, err := server.CheckHealth(context.Background(), &pb.CheckHealthRequest{}) if err != nil { t.Errorf("Error in CheckHealth: %v", err) diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.go index 82f0b677f9..440af1e2c9 100644 --- a/internal/controlplane/server_test.go +++ b/internal/controlplane/server_test.go @@ -18,7 +18,6 @@ package controlplane import ( "context" "log" - "net" "net/http" "net/http/httptest" "os" @@ -29,7 +28,6 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/test/bufconn" mockdb "github.com/stacklok/minder/database/mock" @@ -69,20 +67,6 @@ func init() { // It would be nice if we could Close() the httpServer, but we leak it in the test instead } -func bufDialer(context.Context, string) (net.Conn, error) { - return lis.Dial() -} - -func getgRPCConnection() (*grpc.ClientConn, error) { - conn, err := grpc.DialContext(context.Background(), "bufnet", - grpc.WithContextDialer(bufDialer), - grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - return nil, err - } - return conn, nil -} - func newDefaultServer(t *testing.T, mockStore *mockdb.MockStore) *Server { t.Helper() @@ -122,22 +106,6 @@ func generateTokenKey(t *testing.T) string { return tokenKeyPath } -func TestHealth(t *testing.T) { - t.Parallel() - - conn, err := getgRPCConnection() - if err != nil { - t.Fatalf("Failed to dial bufnet: %v", err) - } - defer conn.Close() - - client := pb.NewHealthServiceClient(conn) - _, err = client.CheckHealth(context.Background(), &pb.CheckHealthRequest{}) - if err != nil { - t.Fatalf("Failed to get health: %v", err) - } -} - func TestWebhook(t *testing.T) { t.Parallel()