|
| 1 | +// Copyright 2025 Redpanda Data, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.md |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0 |
| 9 | + |
| 10 | +package connections |
| 11 | + |
| 12 | +import ( |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + adminv2 "buf.build/gen/go/redpandadata/core/protocolbuffers/go/redpanda/core/admin/v2" |
| 17 | + |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | + "google.golang.org/protobuf/types/known/durationpb" |
| 20 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 21 | +) |
| 22 | + |
| 23 | +func TestParseConnection(t *testing.T) { |
| 24 | + protoConn := &adminv2.KafkaConnection{ |
| 25 | + NodeId: 2, |
| 26 | + ShardId: 4, |
| 27 | + Uid: "36338ca5-86b7-4478-ad23-32d49cfaef61", |
| 28 | + State: adminv2.KafkaConnectionState_KAFKA_CONNECTION_STATE_OPEN, |
| 29 | + OpenTime: timestamppb.New(time.Date(2025, 10, 23, 1, 2, 3, 0, time.UTC)), |
| 30 | + CloseTime: nil, |
| 31 | + AuthenticationInfo: &adminv2.AuthenticationInfo{ |
| 32 | + State: adminv2.AuthenticationState_AUTHENTICATION_STATE_SUCCESS, |
| 33 | + Mechanism: adminv2.AuthenticationMechanism_AUTHENTICATION_MECHANISM_MTLS, |
| 34 | + UserPrincipal: "someone", |
| 35 | + }, |
| 36 | + TlsInfo: &adminv2.TLSInfo{ |
| 37 | + Enabled: true, |
| 38 | + }, |
| 39 | + ListenerName: "external", |
| 40 | + Source: &adminv2.Source{ |
| 41 | + IpAddress: "4.2.2.1", |
| 42 | + Port: 49722, |
| 43 | + }, |
| 44 | + ClientId: "a-unique-client-id", |
| 45 | + ClientSoftwareName: "some-library", |
| 46 | + ClientSoftwareVersion: "v0.0.1", |
| 47 | + GroupId: "group-a", |
| 48 | + ApiVersions: map[int32]int32{0: 4, 9: 11}, |
| 49 | + IdleDuration: durationpb.New(100 * time.Millisecond), |
| 50 | + InFlightRequests: &adminv2.InFlightRequests{ |
| 51 | + SampledInFlightRequests: []*adminv2.InFlightRequests_Request{ |
| 52 | + { |
| 53 | + ApiKey: 0, // PRODUCE |
| 54 | + InFlightDuration: durationpb.New(40 * time.Millisecond), |
| 55 | + }, |
| 56 | + }, |
| 57 | + HasMoreRequests: true, |
| 58 | + }, |
| 59 | + TotalRequestStatistics: &adminv2.RequestStatistics{ |
| 60 | + ProduceBytes: 10000, |
| 61 | + FetchBytes: 2000, |
| 62 | + RequestCount: 200, |
| 63 | + ProduceBatchCount: 10, |
| 64 | + }, |
| 65 | + RecentRequestStatistics: &adminv2.RequestStatistics{ |
| 66 | + ProduceBytes: 1000, |
| 67 | + FetchBytes: 200, |
| 68 | + RequestCount: 20, |
| 69 | + ProduceBatchCount: 1, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + out := parseConnection(protoConn) |
| 74 | + |
| 75 | + // Verify basic fields |
| 76 | + require.Equal(t, int32(2), out.NodeID) |
| 77 | + require.Equal(t, uint32(4), out.ShardID) |
| 78 | + require.Equal(t, "36338ca5-86b7-4478-ad23-32d49cfaef61", out.UID) |
| 79 | + require.Equal(t, "OPEN", out.State) |
| 80 | + require.Equal(t, "2025-10-23T01:02:03Z", out.OpenTime) |
| 81 | + require.Nil(t, out.CloseTime) |
| 82 | + require.NotEmpty(t, out.ConnectionDuration) |
| 83 | + require.True(t, out.TLSEnabled) |
| 84 | + require.Equal(t, "group-a", out.GroupID) |
| 85 | + require.Equal(t, "100ms", out.IdleDuration) |
| 86 | + |
| 87 | + // Verify authentication |
| 88 | + require.Equal(t, &Authentication{ |
| 89 | + State: "SUCCESS", |
| 90 | + Mechanism: "MTLS", |
| 91 | + UserPrincipal: "someone", |
| 92 | + }, out.Authentication) |
| 93 | + |
| 94 | + // Verify client |
| 95 | + require.Equal(t, &Client{ |
| 96 | + IP: "4.2.2.1", |
| 97 | + Port: 49722, |
| 98 | + ID: "a-unique-client-id", |
| 99 | + SoftwareName: "some-library", |
| 100 | + SoftwareVersion: "v0.0.1", |
| 101 | + }, out.Client) |
| 102 | + |
| 103 | + // Verify API versions (order-independent check) |
| 104 | + require.ElementsMatch(t, []APIVersion{ |
| 105 | + {API: "PRODUCE", Version: 4}, |
| 106 | + {API: "OFFSET_FETCH", Version: 11}, |
| 107 | + }, out.APIVersions) |
| 108 | + |
| 109 | + // Verify active requests |
| 110 | + require.Equal(t, &ActiveRequests{ |
| 111 | + SampledRequests: []SampledRequest{ |
| 112 | + {API: "PRODUCE", Duration: "40ms"}, |
| 113 | + }, |
| 114 | + HasMoreRequests: true, |
| 115 | + }, out.ActiveRequests) |
| 116 | + |
| 117 | + // Verify statistics |
| 118 | + require.Equal(t, &RequestStatistics{ |
| 119 | + ProduceBytes: "10000", |
| 120 | + FetchBytes: "2000", |
| 121 | + RequestCount: "200", |
| 122 | + ProduceBatchCount: "10", |
| 123 | + }, out.RequestStatisticsAll) |
| 124 | + |
| 125 | + require.Equal(t, &RequestStatistics{ |
| 126 | + ProduceBytes: "1000", |
| 127 | + FetchBytes: "200", |
| 128 | + RequestCount: "20", |
| 129 | + ProduceBatchCount: "1", |
| 130 | + }, out.RequestStatistics1m) |
| 131 | +} |
0 commit comments