Skip to content

Commit

Permalink
Add test in readiness/probe_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
seongpyoHong committed Jul 20, 2023
1 parent 8e3edce commit 184708c
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/queue/readiness/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package readiness

import (
"bytes"
"context"
"errors"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -655,6 +658,30 @@ func TestKnTCPProbeSuccessThresholdIncludesFailure(t *testing.T) {
}
}

func TestGRPCSuccess(t *testing.T) {
port := 12345
if err := newTestGRPCServer(t, port); err != nil {
t.Errorf("Failed to create test grpc server: %v", err)
}

pb := NewProbe(&corev1.Probe{
PeriodSeconds: 1,
TimeoutSeconds: 5,
SuccessThreshold: 1,
FailureThreshold: 1,
ProbeHandler: corev1.ProbeHandler{
GRPC: &corev1.GRPCAction{
Port: int32(port),
Service: nil,
},
},
})

if !pb.ProbeContainer() {
t.Error("Probe failed. Expected success.")
}
}

func newTestServer(t *testing.T, h http.HandlerFunc) *url.URL {
t.Helper()

Expand All @@ -668,3 +695,32 @@ func newTestServer(t *testing.T, h http.HandlerFunc) *url.URL {

return u
}

func newTestGRPCServer(t *testing.T, port int) error {
t.Helper()
grpcAddr := fmt.Sprintf("127.0.0.1:%d", port)
lis, err := net.Listen("tcp", grpcAddr)
if err != nil {
t.Fatalf("failed to listen: %v", err)
}

s := grpc.NewServer()
grpc_health_v1.RegisterHealthServer(s, &grpcHealthServer{})

go func() {
if err := s.Serve(lis); err != nil {
t.Fatalf("Failed to run gRPC test server %v", err)
}
}()
t.Cleanup(s.Stop)

return nil
}

type grpcHealthServer struct {
grpc_health_v1.UnimplementedHealthServer
}

func (s *grpcHealthServer) Check(_ context.Context, _ *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
}

0 comments on commit 184708c

Please sign in to comment.