1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
// Copyright (C) MongoDB, Inc. 2023-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package integration
import (
"context"
"fmt"
"testing"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/internal/integtest"
"go.mongodb.org/mongo-driver/internal/logger"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/mongo/options"
)
var ErrInvalidTruncation = fmt.Errorf("invalid truncation")
func clamTruncErr(mt *mtest.T, op string, want, got int) error {
mt.Helper()
return fmt.Errorf("%w: expected length %s %d, got %d", ErrInvalidTruncation, op, want, got)
}
func clamDefaultTruncLimitOp(ctx context.Context, mt *mtest.T, coll *mongo.Collection) {
mt.Helper()
const documentsSize = 100
// Construct an array of docs containing the document {"x" : "y"}
// repeated "documentSize" times.
docs := []interface{}{}
for i := 0; i < documentsSize; i++ {
docs = append(docs, bson.D{{"x", "y"}})
}
// Insert docs to a the collection.
_, err := coll.InsertMany(ctx, docs)
assert.Nil(mt, err, "InsertMany error: %v", err)
// Run find() on the collection.
_, err = coll.Find(ctx, bson.D{})
assert.Nil(mt, err, "Find error: %v", err)
}
func clamDefaultTruncLimitLogs(mt *mtest.T) []truncValidator {
mt.Helper()
const cmd = "command"
const rpl = "reply"
expTruncLen := len(logger.TruncationSuffix) + logger.DefaultMaxDocumentLength
validators := make([]truncValidator, 4)
// Insert started.
validators[0] = newTruncValidator(mt, cmd, func(cmd string) error {
if len(cmd) != expTruncLen {
return clamTruncErr(mt, "=", expTruncLen, len(cmd))
}
return nil
})
// Insert succeeded.
validators[1] = newTruncValidator(mt, rpl, func(cmd string) error {
if len(cmd) > expTruncLen {
return clamTruncErr(mt, "<=", expTruncLen, len(cmd))
}
return nil
})
// Find started, nothing to validate.
validators[2] = nil
// Find succeeded.
validators[3] = newTruncValidator(mt, rpl, func(cmd string) error {
if len(cmd) != expTruncLen {
return clamTruncErr(mt, "=", expTruncLen, len(cmd))
}
return nil
})
return validators
}
func clamExplicitTruncLimitOp(ctx context.Context, mt *mtest.T, coll *mongo.Collection) {
mt.Helper()
result := coll.Database().RunCommand(ctx, bson.D{{"hello", true}})
assert.Nil(mt, result.Err(), "RunCommand error: %v", result.Err())
}
func clamExplicitTruncLimitLogs(mt *mtest.T) []truncValidator {
mt.Helper()
const cmd = "command"
const rpl = "reply"
expTruncLen := len(logger.TruncationSuffix) + 5
validators := make([]truncValidator, 2)
// Hello started.
validators[0] = newTruncValidator(mt, cmd, func(cmd string) error {
if len(cmd) != expTruncLen {
return clamTruncErr(mt, "=", expTruncLen, len(cmd))
}
return nil
})
// Hello succeeded.
validators[1] = newTruncValidator(mt, rpl, func(cmd string) error {
if len(cmd) != expTruncLen {
return clamTruncErr(mt, "=", expTruncLen, len(cmd))
}
return nil
})
return validators
}
func clamExplicitTruncLimitFailOp(ctx context.Context, mt *mtest.T, coll *mongo.Collection) {
mt.Helper()
result := coll.Database().RunCommand(ctx, bson.D{{"notARealCommand", true}})
assert.NotNil(mt, result.Err(), "expected RunCommand error, got: %v", result.Err())
}
func clamExplicitTruncLimitFailLogs(mt *mtest.T) []truncValidator {
mt.Helper()
const fail = "failure"
expTruncLen := len(logger.TruncationSuffix) + 5
validators := make([]truncValidator, 2)
// Hello started, nothing to validate.
validators[0] = nil
// Hello failed.
validators[1] = newTruncValidator(mt, fail, func(cmd string) error {
if len(cmd) != expTruncLen {
return clamTruncErr(mt, "=", expTruncLen, len(cmd))
}
return nil
})
return validators
}
// clamMultiByteTrunc runs an operation to insert a very large document with the
// multi-byte character "界" repeated a large number of times. This repetition
// is done to categorically ensure that the truncation point is made somewhere
// within the multi-byte character. For example a typical insertion reply may
// look something like this:
//
// {"insert": "setuptest","ordered": true,"lsid": {"id": ...
//
// We have no control over how the "header" portion of this reply is formatted.
// Over time the server might support newer fields or change the formatting of
// existing fields. This means that the truncation point could be anywhere in
// the "header" portion of the reply. A large document lowers the likelihood of
// the truncation point being in the "header" portion of the reply.
func clamMultiByteTrunc(ctx context.Context, mt *mtest.T, coll *mongo.Collection) {
mt.Helper()
const multiByteCharStrLen = 50_000
const strToRepeat = "界"
// Repeat the string "strToRepeat" "multiByteCharStrLen" times.
multiByteCharStr := ""
for i := 0; i < multiByteCharStrLen; i++ {
multiByteCharStr += strToRepeat
}
_, err := coll.InsertOne(ctx, bson.D{{"x", multiByteCharStr}})
assert.Nil(mt, err, "InsertOne error: %v", err)
}
func clamMultiByteTruncLogs(mt *mtest.T) []truncValidator {
mt.Helper()
const cmd = "command"
const strToRepeat = "界"
validators := make([]truncValidator, 2)
// Insert started.
validators[0] = newTruncValidator(mt, cmd, func(cmd string) error {
// Remove the suffix from the command string.
cmd = cmd[:len(cmd)-len(logger.TruncationSuffix)]
// Get the last 3 bytes of the command string.
last3Bytes := cmd[len(cmd)-3:]
// Make sure the last 3 bytes are the multi-byte character.
if last3Bytes != strToRepeat {
return fmt.Errorf("expected last 3 bytes to be %q, got %q", strToRepeat, last3Bytes)
}
return nil
})
return validators
}
func TestCommandLoggingAndMonitoringProse(t *testing.T) {
t.Parallel()
mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
for _, tcase := range []struct {
// name is the name of the test case
name string
// collectionName is the name to assign the collection for
// processing the operations. This should be unique across test
// cases.
collectionName string
// maxDocumentLength is the maximum document length for a
// command message.
maxDocumentLength uint
// orderedLogValidators is a slice of log validators that should
// be 1-1 with the actual logs that are propagated by the
// LogSink. The order here matters, the first log will be
// validated by the 0th validator, the second log will be
// validated by the 1st validator, etc.
orderedLogValidators []truncValidator
// operation is the operation to perform on the collection that
// will result in log propagation. The logs created by
// "operation" will be validated against the
// "orderedLogValidators."
operation func(context.Context, *mtest.T, *mongo.Collection)
// Setup is a function that will be run before the test case.
// Operations performed in this function will not be logged.
setup func(context.Context, *mtest.T, *mongo.Collection)
}{
{
name: "1 Default truncation limit",
collectionName: "46a624c57c72463d90f88a733e7b28b4",
operation: clamDefaultTruncLimitOp,
orderedLogValidators: clamDefaultTruncLimitLogs(mt),
},
{
name: "2 Explicitly configured truncation limit",
collectionName: "540baa64dc854ca2a639627e2f0918df",
maxDocumentLength: 5,
operation: clamExplicitTruncLimitOp,
orderedLogValidators: clamExplicitTruncLimitLogs(mt),
},
{
name: "2 Explicitly configured truncation limit for failures",
collectionName: "aff43dfcaa1a4014b58aaa9606f5bd44",
maxDocumentLength: 5,
operation: clamExplicitTruncLimitFailOp,
orderedLogValidators: clamExplicitTruncLimitFailLogs(mt),
},
// The third test case is to ensure that a truncation point made
// within a multi-byte character is handled correctly. The
// chosen multi-byte character for this test is "界" (U+754C).
// This character is repeated a large number of times (50,000).
// We need to run this test 3 times to ensure that the
// truncation occurs at a middle point within the multi-byte
// character at least once (at most twice).
{
name: "3.1 Truncation with multi-byte codepoints",
collectionName: "5ed6d1b7-e358-438a-b067-e1d1dd10fee1",
maxDocumentLength: 20_000,
operation: clamMultiByteTrunc,
orderedLogValidators: clamMultiByteTruncLogs(mt),
},
{
name: "3.2 Truncation with multi-byte codepoints",
collectionName: "5ed6d1b7-e358-438a-b067-e1d1dd10fee1",
maxDocumentLength: 20_001,
operation: clamMultiByteTrunc,
orderedLogValidators: clamMultiByteTruncLogs(mt),
},
{
name: "3.3 Truncation with multi-byte codepoints",
collectionName: "5ed6d1b7-e358-438a-b067-e1d1dd10fee1",
maxDocumentLength: 20_002,
operation: clamMultiByteTrunc,
orderedLogValidators: clamMultiByteTruncLogs(mt),
},
} {
tcase := tcase
mt.Run(tcase.name, func(mt *mtest.T) {
mt.Parallel()
const deadline = 10 * time.Second
ctx := context.Background()
// Before the test case, we need to see if there is a
// setup function to run.
if tcase.setup != nil {
clientOpts := options.Client().ApplyURI(mtest.ClusterURI())
// Create a context with a deadline so that the
// test setup doesn't hang forever.
ctx, cancel := context.WithTimeout(ctx, deadline)
defer cancel()
integtest.AddTestServerAPIVersion(clientOpts)
client, err := mongo.Connect(ctx, clientOpts)
assert.Nil(mt, err, "Connect error in setup: %v", err)
coll := mt.CreateCollection(mtest.Collection{
Name: tcase.collectionName,
Client: client,
}, false)
tcase.setup(ctx, mt, coll)
}
// If there is no operation, then we don't need to run
// the test case.
if tcase.operation == nil {
return
}
// If there are no log validators, then we should error.
if len(tcase.orderedLogValidators) == 0 {
mt.Fatalf("no log validators provided")
}
sinkCtx, sinkCancel := context.WithDeadline(ctx, time.Now().Add(deadline))
defer sinkCancel()
validator := func(order int, _ int, _ string, keysAndValues ...interface{}) error {
// If the order exceeds the length of the
// "orderedCaseValidators," then throw an error.
if order >= len(tcase.orderedLogValidators) {
return fmt.Errorf("not enough expected cases to validate")
}
caseValidator := tcase.orderedLogValidators[order]
if caseValidator == nil {
return nil
}
return tcase.orderedLogValidators[order](keysAndValues...)
}
sink := newTestLogSink(sinkCtx, mt, len(tcase.orderedLogValidators), validator)
// Configure logging with a minimum severity level of
// "debug" for the "command" component without
// explicitly configuring the max document length.
loggerOpts := options.Logger().SetSink(sink).
SetComponentLevel(options.LogComponentCommand, options.LogLevelDebug)
// If the test case requires a maximum document length,
// then configure it.
if mdl := tcase.maxDocumentLength; mdl != 0 {
loggerOpts.SetMaxDocumentLength(mdl)
}
clientOpts := options.Client().SetLoggerOptions(loggerOpts).ApplyURI(mtest.ClusterURI())
integtest.AddTestServerAPIVersion(clientOpts)
client, err := mongo.Connect(context.Background(), clientOpts)
assert.Nil(mt, err, "Connect error: %v", err)
coll := mt.CreateCollection(mtest.Collection{
Name: tcase.collectionName,
Client: client,
}, false)
tcase.operation(ctx, mt, coll)
// Verify the logs.
if err := <-sink.errs(); err != nil {
mt.Fatalf("unexpected error: %v", err)
}
})
}
}
|