|
| 1 | +package grpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "google.golang.org/grpc" |
| 12 | + "google.golang.org/grpc/credentials/insecure" |
| 13 | + healthpb "google.golang.org/grpc/health/grpc_health_v1" |
| 14 | + |
| 15 | + "github.com/aquasecurity/tracee/pkg/ebpf/heartbeat" |
| 16 | + "github.com/aquasecurity/tracee/pkg/server" |
| 17 | +) |
| 18 | + |
| 19 | +func TestHealthService_Check(t *testing.T) { |
| 20 | + tempDir, err := os.MkdirTemp("", "tracee-health-tests") |
| 21 | + require.NoError(t, err) |
| 22 | + defer os.RemoveAll(tempDir) |
| 23 | + |
| 24 | + unixSock := tempDir + "/tracee.sock" |
| 25 | + defer os.Remove(unixSock) |
| 26 | + |
| 27 | + ctx, cancel := context.WithCancel(context.Background()) |
| 28 | + // Don't cancel context until test is done to avoid closing heartbeat |
| 29 | + defer cancel() |
| 30 | + |
| 31 | + // Initialize heartbeat for testing |
| 32 | + // Use a background context that won't be cancelled to keep heartbeat alive |
| 33 | + bgCtx := context.Background() |
| 34 | + heartbeat.Init(bgCtx, 1*time.Second, 2*time.Second) |
| 35 | + instance := heartbeat.GetInstance() |
| 36 | + require.NotNil(t, instance) |
| 37 | + instance.SetCallback(server.InvokeHeartbeat) |
| 38 | + instance.Start() |
| 39 | + |
| 40 | + // In tests, manually send pulses since uprobe isn't attached |
| 41 | + pulseCtx, pulseCancel := context.WithCancel(ctx) |
| 42 | + defer pulseCancel() |
| 43 | + go func() { |
| 44 | + ticker := time.NewTicker(500 * time.Millisecond) |
| 45 | + defer ticker.Stop() |
| 46 | + for { |
| 47 | + select { |
| 48 | + case <-ticker.C: |
| 49 | + safeSendPulse() |
| 50 | + case <-pulseCtx.Done(): |
| 51 | + return |
| 52 | + } |
| 53 | + } |
| 54 | + }() |
| 55 | + |
| 56 | + grpcServer := New("unix", unixSock) |
| 57 | + grpcServer.EnableHealthService() |
| 58 | + go grpcServer.Start(ctx, nil, nil) |
| 59 | + |
| 60 | + // Wait for server to start |
| 61 | + require.Eventually(t, func() bool { |
| 62 | + _, err := os.Stat(unixSock) |
| 63 | + return err == nil |
| 64 | + }, 2*time.Second, 10*time.Millisecond) |
| 65 | + |
| 66 | + // Create health client |
| 67 | + conn, err := grpc.NewClient("unix:"+unixSock, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 68 | + require.NoError(t, err) |
| 69 | + defer conn.Close() |
| 70 | + |
| 71 | + healthClient := healthpb.NewHealthClient(conn) |
| 72 | + |
| 73 | + // Send initial pulse immediately |
| 74 | + safeSendPulse() |
| 75 | + |
| 76 | + // Wait for health service monitor to poll and update status (polls every 500ms) |
| 77 | + require.Eventually(t, func() bool { |
| 78 | + resp, err := healthClient.Check(ctx, &healthpb.HealthCheckRequest{}) |
| 79 | + return err == nil && resp.Status == healthpb.HealthCheckResponse_SERVING |
| 80 | + }, 2*time.Second, 100*time.Millisecond, "health service should become SERVING") |
| 81 | + |
| 82 | + // Test overall health (empty service name) |
| 83 | + t.Run("overall health check", func(t *testing.T) { |
| 84 | + resp, err := healthClient.Check(ctx, &healthpb.HealthCheckRequest{}) |
| 85 | + require.NoError(t, err) |
| 86 | + assert.Equal(t, healthpb.HealthCheckResponse_SERVING, resp.Status) |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +// safeSendPulse safely sends a pulse, recovering from panics if the channel is closed or instance is nil |
| 91 | +func safeSendPulse() { |
| 92 | + defer func() { |
| 93 | + recover(); |
| 94 | + }() |
| 95 | + if instance := heartbeat.GetInstance(); instance != nil { |
| 96 | + heartbeat.SendPulse() |
| 97 | + } |
| 98 | +} |
0 commit comments