|
| 1 | +package jd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/docker/go-connections/nat" |
| 9 | + tc "github.com/testcontainers/testcontainers-go" |
| 10 | + tcwait "github.com/testcontainers/testcontainers-go/wait" |
| 11 | + "google.golang.org/grpc" |
| 12 | + "google.golang.org/grpc/credentials" |
| 13 | + "google.golang.org/grpc/credentials/insecure" |
| 14 | + "google.golang.org/grpc/health/grpc_health_v1" |
| 15 | + |
| 16 | + "github.com/smartcontractkit/chainlink-testing-framework/framework" |
| 17 | +) |
| 18 | + |
| 19 | +// GRPCHealthStrategy implements a wait strategy for gRPC health checks |
| 20 | +type GRPCHealthStrategy struct { |
| 21 | + Port nat.Port |
| 22 | + PollInterval time.Duration |
| 23 | + timeout time.Duration |
| 24 | +} |
| 25 | + |
| 26 | +// NewGRPCHealthStrategy creates a new gRPC health check wait strategy |
| 27 | +func NewGRPCHealthStrategy(port nat.Port) *GRPCHealthStrategy { |
| 28 | + return &GRPCHealthStrategy{ |
| 29 | + Port: port, |
| 30 | + PollInterval: 200 * time.Millisecond, |
| 31 | + timeout: 3 * time.Minute, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// WithTimeout sets the timeout for the gRPC health check strategy |
| 36 | +func (g *GRPCHealthStrategy) WithTimeout(timeout time.Duration) *GRPCHealthStrategy { |
| 37 | + g.timeout = timeout |
| 38 | + return g |
| 39 | +} |
| 40 | + |
| 41 | +// WithPollInterval sets the poll interval for the gRPC health check strategy |
| 42 | +func (g *GRPCHealthStrategy) WithPollInterval(interval time.Duration) *GRPCHealthStrategy { |
| 43 | + g.PollInterval = interval |
| 44 | + return g |
| 45 | +} |
| 46 | + |
| 47 | +// WaitUntilReady implements Strategy.WaitUntilReady |
| 48 | +func (g *GRPCHealthStrategy) WaitUntilReady(ctx context.Context, target tcwait.StrategyTarget) error { |
| 49 | + ctx, cancel := context.WithTimeout(ctx, g.timeout) |
| 50 | + defer cancel() |
| 51 | + |
| 52 | + for { |
| 53 | + select { |
| 54 | + case <-ctx.Done(): |
| 55 | + return ctx.Err() |
| 56 | + case <-time.After(g.PollInterval): |
| 57 | + // Check if container is still running |
| 58 | + state, err := target.State(ctx) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + if !state.Running { |
| 63 | + return fmt.Errorf("container is not running: %s", state.Status) |
| 64 | + } |
| 65 | + |
| 66 | + // Get host and port |
| 67 | + host, err := framework.GetHost(target.(tc.Container)) //nolint:contextcheck //don't want modify the signature of GetHost() yet |
| 68 | + if err != nil { |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + mappedPort, err := target.MappedPort(ctx, g.Port) |
| 73 | + if err != nil { |
| 74 | + continue |
| 75 | + } |
| 76 | + |
| 77 | + // Attempt gRPC health check |
| 78 | + address := fmt.Sprintf("%s:%s", host, mappedPort.Port()) |
| 79 | + if err := g.checkHealth(ctx, address); err == nil { |
| 80 | + return nil |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// checkHealth performs the actual gRPC health check |
| 87 | +func (g *GRPCHealthStrategy) checkHealth(ctx context.Context, address string) error { |
| 88 | + // Create a short timeout for the individual check |
| 89 | + checkCtx, cancel := context.WithTimeout(ctx, 1*time.Second) |
| 90 | + defer cancel() |
| 91 | + |
| 92 | + // Use plaintext/insecure connection (standard for local testing and health checks) |
| 93 | + return g.tryHealthCheck(checkCtx, address, insecure.NewCredentials()) |
| 94 | +} |
| 95 | + |
| 96 | +// tryHealthCheck attempts a health check with specific credentials |
| 97 | +func (g *GRPCHealthStrategy) tryHealthCheck(ctx context.Context, address string, creds credentials.TransportCredentials) error { |
| 98 | + // Build dial options similar to the working JD connection code |
| 99 | + opts := []grpc.DialOption{ |
| 100 | + grpc.WithTransportCredentials(creds), |
| 101 | + } |
| 102 | + |
| 103 | + // Create the gRPC client connection |
| 104 | + conn, err := grpc.NewClient(address, opts...) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + defer func() { _ = conn.Close() }() |
| 109 | + |
| 110 | + // Create health check client |
| 111 | + healthClient := grpc_health_v1.NewHealthClient(conn) |
| 112 | + |
| 113 | + // Perform health check |
| 114 | + resp, err := healthClient.Check(ctx, &grpc_health_v1.HealthCheckRequest{}) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + if resp.Status != grpc_health_v1.HealthCheckResponse_SERVING { |
| 120 | + return fmt.Errorf("service not serving, status: %v", resp.Status) |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
0 commit comments