MCPcopy
hub / github.com/grpc/grpc-go / TestMetadataTruncationAccountsKey

Method TestMetadataTruncationAccountsKey

gcp/observability/logging_test.go:1120–1238  ·  view source on GitHub ↗

TestMetadataTruncationAccountsKey tests that the metadata truncation takes into account both the key and value of metadata. It configures an observability system with a maximum byte length for metadata, which is greater than just the byte length of the metadata value but less than the byte length of

(t *testing.T)

Source from the content-addressed store, hash-verified

1118// byte length of the metadata key + metadata value. Thus, in the ClientHeader
1119// logging event, no metadata should be logged.
1120func (s) TestMetadataTruncationAccountsKey(t *testing.T) {
1121 fle := &fakeLoggingExporter{
1122 t: t,
1123 }
1124 defer func(ne func(ctx context.Context, config *config) (loggingExporter, error)) {
1125 newLoggingExporter = ne
1126 }(newLoggingExporter)
1127
1128 newLoggingExporter = func(_ context.Context, _ *config) (loggingExporter, error) {
1129 return fle, nil
1130 }
1131
1132 const mdValue = "value"
1133 configMetadataLimit := &config{
1134 ProjectID: "fake",
1135 CloudLogging: &cloudLogging{
1136 ClientRPCEvents: []clientRPCEvents{
1137 {
1138 Methods: []string{"*"},
1139 MaxMetadataBytes: len(mdValue) + 1,
1140 },
1141 },
1142 },
1143 }
1144
1145 cleanup, err := setupObservabilitySystemWithConfig(configMetadataLimit)
1146 if err != nil {
1147 t.Fatalf("error setting up observability %v", err)
1148 }
1149 defer cleanup()
1150
1151 ss := &stubserver.StubServer{
1152 UnaryCallF: func(_ context.Context, _ *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
1153 return &testpb.SimpleResponse{}, nil
1154 },
1155 }
1156 if err := ss.Start(nil); err != nil {
1157 t.Fatalf("Error starting endpoint server: %v", err)
1158 }
1159 defer ss.Stop()
1160
1161 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
1162 defer cancel()
1163
1164 // the set config MaxMetadataBytes is in between len(mdValue) and len("key")
1165 // + len(mdValue), and thus shouldn't log this metadata entry.
1166 md := metadata.MD{
1167 "key": []string{mdValue},
1168 }
1169 ctx = metadata.NewOutgoingContext(ctx, md)
1170 if _, err := ss.Client.UnaryCall(ctx, &testpb.SimpleRequest{Payload: &testpb.Payload{Body: []byte("00000")}}); err != nil {
1171 t.Fatalf("Unexpected error from UnaryCall: %v", err)
1172 }
1173
1174 grpcLogEntriesWant := []*grpcLogEntry{
1175 {
1176 Type: eventTypeClientHeader,
1177 Logger: loggerClient,

Callers

nothing calls this directly

Calls 9

StartMethod · 0.95
StopMethod · 0.95
NewOutgoingContextFunction · 0.92
cmpLoggingEntryListFunction · 0.85
FatalfMethod · 0.65
UnaryCallMethod · 0.65
LockMethod · 0.45
UnlockMethod · 0.45

Tested by

no test coverage detected