Skip to content

Commit 9bea3a9

Browse files
committed
Merge write tests into one table-based test
1 parent fe791fa commit 9bea3a9

File tree

1 file changed

+57
-180
lines changed

1 file changed

+57
-180
lines changed

plugins/outputs/mongodb/mongodb_test.go

Lines changed: 57 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -412,173 +412,24 @@ func TestConnectAndWriteX509AuthFailIntegration(t *testing.T) {
412412
}
413413
}
414414

415-
func TestWriteIndividualIntegration(t *testing.T) {
415+
func TestWriteIntegration(t *testing.T) {
416416
if testing.Short() {
417417
t.Skip("Skipping integration test in short mode")
418418
}
419419

420-
// Setup the container
421-
servicePort := "27017"
422-
container := testutil.Container{
423-
Image: "mongo",
424-
ExposedPorts: []string{servicePort},
425-
WaitingFor: wait.ForLog("Waiting for connections"),
426-
Quiet: true,
427-
}
428-
require.NoError(t, container.Start(), "failed to start container")
429-
defer container.Terminate()
430-
431-
// Setup and start the plugin
432-
plugin := &MongoDB{
433-
Dsn: "mongodb://" + container.Address + ":" + container.Ports[servicePort],
434-
MetricDatabase: "telegraf_test",
435-
}
436-
require.NoError(t, plugin.Init())
437-
require.NoError(t, plugin.Connect())
438-
defer plugin.Close()
439-
440-
// Setup the input metrics and expected results
441-
input := []telegraf.Metric{
442-
metric.New(
443-
"test1",
444-
map[string]string{"source": "foo"},
445-
map[string]interface{}{"value": 1},
446-
time.Unix(0, 0),
447-
),
448-
metric.New(
449-
"test1",
450-
map[string]string{"source": "foo"},
451-
map[string]interface{}{"value": 2},
452-
time.Unix(10, 0),
453-
),
454-
metric.New(
455-
"test1",
456-
map[string]string{"source": "foo"},
457-
map[string]interface{}{"value": 3},
458-
time.Unix(20, 0),
459-
),
460-
metric.New(
461-
"test2",
462-
map[string]string{"source": "bar"},
463-
map[string]interface{}{"value": 10},
464-
time.Unix(0, 10),
465-
),
466-
metric.New(
467-
"test2",
468-
map[string]string{"source": "bar"},
469-
map[string]interface{}{"value": 20},
470-
time.Unix(10, 20),
471-
),
472-
metric.New(
473-
"test2",
474-
map[string]string{"source": "bar"},
475-
map[string]interface{}{"value": 30},
476-
time.Unix(20, 30),
477-
),
478-
metric.New(
479-
"test2",
480-
map[string]string{"source": "bar"},
481-
map[string]interface{}{"value": 40},
482-
time.Unix(30, 40),
483-
),
484-
}
485-
expected := map[string][]bson.D{
486-
"test1": {
487-
bson.D{
488-
primitive.E{Key: "timestamp", Value: primitive.DateTime(0)},
489-
primitive.E{Key: "tags", Value: bson.D{primitive.E{Key: "source", Value: "foo"}}},
490-
primitive.E{Key: "value", Value: int64(1)},
491-
},
492-
bson.D{
493-
primitive.E{Key: "timestamp", Value: primitive.DateTime(10000)},
494-
primitive.E{Key: "tags", Value: bson.D{primitive.E{Key: "source", Value: "foo"}}},
495-
primitive.E{Key: "value", Value: int64(2)},
496-
},
497-
bson.D{
498-
primitive.E{Key: "timestamp", Value: primitive.DateTime(20000)},
499-
primitive.E{Key: "tags", Value: bson.D{primitive.E{Key: "source", Value: "foo"}}},
500-
primitive.E{Key: "value", Value: int64(3)},
501-
},
420+
tests := []struct {
421+
name string
422+
batch bool
423+
}{
424+
{
425+
name: "individual",
502426
},
503-
"test2": {
504-
bson.D{
505-
primitive.E{Key: "timestamp", Value: primitive.DateTime(0)},
506-
primitive.E{Key: "tags", Value: bson.D{primitive.E{Key: "source", Value: "bar"}}},
507-
primitive.E{Key: "value", Value: int64(10)},
508-
},
509-
bson.D{
510-
primitive.E{Key: "timestamp", Value: primitive.DateTime(10000)},
511-
primitive.E{Key: "tags", Value: bson.D{primitive.E{Key: "source", Value: "bar"}}},
512-
primitive.E{Key: "value", Value: int64(20)},
513-
},
514-
bson.D{
515-
primitive.E{Key: "timestamp", Value: primitive.DateTime(20000)},
516-
primitive.E{Key: "tags", Value: bson.D{primitive.E{Key: "source", Value: "bar"}}},
517-
primitive.E{Key: "value", Value: int64(30)},
518-
},
519-
bson.D{
520-
primitive.E{Key: "timestamp", Value: primitive.DateTime(30000)},
521-
primitive.E{Key: "tags", Value: bson.D{primitive.E{Key: "source", Value: "bar"}}},
522-
primitive.E{Key: "value", Value: int64(40)},
523-
},
427+
{
428+
name: "batch",
429+
batch: true,
524430
},
525431
}
526432

527-
// Write the metrics
528-
require.NoError(t, plugin.Write(input))
529-
530-
// Check the database and collections
531-
client := plugin.client
532-
databases, err := client.ListDatabaseNames(t.Context(), bson.D{})
533-
require.NoError(t, err)
534-
require.Contains(t, databases, "telegraf_test")
535-
536-
database := client.Database("telegraf_test")
537-
require.NotNil(t, database)
538-
collections, err := database.ListCollectionNames(t.Context(), bson.D{})
539-
require.NoError(t, err)
540-
541-
// Read the metrics from the database and compare
542-
for expectedCollection, expectedDocuments := range expected {
543-
require.Contains(t, collections, expectedCollection)
544-
545-
c := database.Collection(expectedCollection)
546-
projection := bson.D{primitive.E{Key: "_id", Value: 0}}
547-
cur, err := c.Find(t.Context(), bson.D{}, options.Find().SetProjection(projection))
548-
require.NoError(t, err)
549-
550-
var documents []bson.D
551-
require.NoError(t, cur.All(t.Context(), &documents))
552-
require.ElementsMatchf(t, expectedDocuments, documents, "mismatch in collection %q", expectedCollection)
553-
}
554-
}
555-
556-
func TestWriteBatchIntegration(t *testing.T) {
557-
if testing.Short() {
558-
t.Skip("Skipping integration test in short mode")
559-
}
560-
561-
// Setup the container
562-
servicePort := "27017"
563-
container := testutil.Container{
564-
Image: "mongo",
565-
ExposedPorts: []string{servicePort},
566-
WaitingFor: wait.ForLog("Waiting for connections"),
567-
Quiet: true,
568-
}
569-
require.NoError(t, container.Start(), "failed to start container")
570-
defer container.Terminate()
571-
572-
// Setup and start the plugin
573-
plugin := &MongoDB{
574-
Dsn: "mongodb://" + container.Address + ":" + container.Ports[servicePort],
575-
MetricDatabase: "telegraf_test",
576-
WriteBatch: true,
577-
}
578-
require.NoError(t, plugin.Init())
579-
require.NoError(t, plugin.Connect())
580-
defer plugin.Close()
581-
582433
// Setup the input metrics and expected results
583434
input := []telegraf.Metric{
584435
metric.New(
@@ -624,6 +475,7 @@ func TestWriteBatchIntegration(t *testing.T) {
624475
time.Unix(30, 40),
625476
),
626477
}
478+
627479
expected := map[string][]bson.D{
628480
"test1": {
629481
bson.D{
@@ -666,32 +518,57 @@ func TestWriteBatchIntegration(t *testing.T) {
666518
},
667519
}
668520

669-
// Write the metrics
670-
require.NoError(t, plugin.Write(input))
521+
for _, tt := range tests {
522+
t.Run(tt.name, func(t *testing.T) {
523+
// Setup the container
524+
servicePort := "27017"
525+
container := testutil.Container{
526+
Image: "mongo",
527+
ExposedPorts: []string{servicePort},
528+
WaitingFor: wait.ForLog("Waiting for connections"),
529+
Quiet: true,
530+
}
531+
require.NoError(t, container.Start(), "failed to start container")
532+
defer container.Terminate()
671533

672-
// Check the database and collections
673-
client := plugin.client
674-
databases, err := client.ListDatabaseNames(t.Context(), bson.D{})
675-
require.NoError(t, err)
676-
require.Contains(t, databases, "telegraf_test")
534+
// Setup and start the plugin
535+
plugin := &MongoDB{
536+
Dsn: "mongodb://" + container.Address + ":" + container.Ports[servicePort],
537+
MetricDatabase: "telegraf_test",
538+
WriteBatch: tt.batch,
539+
}
540+
require.NoError(t, plugin.Init())
541+
require.NoError(t, plugin.Connect())
542+
defer plugin.Close()
677543

678-
database := client.Database("telegraf_test")
679-
require.NotNil(t, database)
680-
collections, err := database.ListCollectionNames(t.Context(), bson.D{})
681-
require.NoError(t, err)
544+
// Write the metrics
545+
require.NoError(t, plugin.Write(input))
682546

683-
// Read the metrics from the database and compare
684-
for expectedCollection, expectedDocuments := range expected {
685-
require.Contains(t, collections, expectedCollection)
547+
// Check the database and collections
548+
client := plugin.client
549+
databases, err := client.ListDatabaseNames(t.Context(), bson.D{})
550+
require.NoError(t, err)
551+
require.Contains(t, databases, "telegraf_test")
686552

687-
c := database.Collection(expectedCollection)
688-
projection := bson.D{primitive.E{Key: "_id", Value: 0}}
689-
cur, err := c.Find(t.Context(), bson.D{}, options.Find().SetProjection(projection))
690-
require.NoError(t, err)
553+
database := client.Database("telegraf_test")
554+
require.NotNil(t, database)
555+
collections, err := database.ListCollectionNames(t.Context(), bson.D{})
556+
require.NoError(t, err)
691557

692-
var documents []bson.D
693-
require.NoError(t, cur.All(t.Context(), &documents))
694-
require.ElementsMatchf(t, expectedDocuments, documents, "mismatch in collection %q", expectedCollection)
558+
// Read the metrics from the database and compare
559+
for expectedCollection, expectedDocuments := range expected {
560+
require.Contains(t, collections, expectedCollection)
561+
562+
c := database.Collection(expectedCollection)
563+
projection := bson.D{primitive.E{Key: "_id", Value: 0}}
564+
cur, err := c.Find(t.Context(), bson.D{}, options.Find().SetProjection(projection))
565+
require.NoError(t, err)
566+
567+
var documents []bson.D
568+
require.NoError(t, cur.All(t.Context(), &documents))
569+
require.ElementsMatchf(t, expectedDocuments, documents, "mismatch in collection %q", expectedCollection)
570+
}
571+
})
695572
}
696573
}
697574

0 commit comments

Comments
 (0)