|
| 1 | +package mongod |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/prometheus/client_golang/prometheus" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "go.mongodb.org/mongo-driver/bson" |
| 11 | + |
| 12 | + "github.com/percona/mongodb_exporter/shared" |
| 13 | + "github.com/percona/mongodb_exporter/testutils" |
| 14 | +) |
| 15 | + |
| 16 | +func TestGetDatabaseProfilerStatsDecodesFine(t *testing.T) { |
| 17 | + // setup |
| 18 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 19 | + defer cancel() |
| 20 | + client := testutils.MustGetConnectedReplSetClient(ctx, t) |
| 21 | + defer client.Disconnect(ctx) |
| 22 | + |
| 23 | + // enable profiling and run a query |
| 24 | + db := client.Database("test-profile") |
| 25 | + assert.NotNil(t, db) |
| 26 | + ok := db.RunCommand(ctx, bson.D{{"profile", 2}}) |
| 27 | + assert.NoErrorf(t, ok.Err(), "failed to enable profiling") |
| 28 | + coll := db.Collection("test") |
| 29 | + assert.NotNil(t, coll) |
| 30 | + _, err := coll.InsertOne(ctx, bson.M{}) |
| 31 | + assert.NoErrorf(t, err, "failed to run a profiled find") |
| 32 | + |
| 33 | + // run |
| 34 | + loopback := int64(10) // seconds. |
| 35 | + threshold := int64(0) // milliseconds. |
| 36 | + stats := GetDatabaseProfilerStats(client, loopback, threshold) |
| 37 | + |
| 38 | + // test |
| 39 | + assert.NotNil(t, stats) |
| 40 | + assert.Truef(t, len(stats.Members) >= 1, "expected at least one slow query") |
| 41 | +} |
| 42 | + |
| 43 | +func TestGetDatabaseProfilerStatsMetrics(t *testing.T) { |
| 44 | + if testing.Short() { |
| 45 | + t.Skip("-short is passed, skipping functional test") |
| 46 | + } |
| 47 | + |
| 48 | + // setup |
| 49 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 50 | + defer cancel() |
| 51 | + client := testutils.MustGetConnectedReplSetClient(ctx, t) |
| 52 | + defer client.Disconnect(ctx) |
| 53 | + |
| 54 | + // enable profiling and run a query |
| 55 | + db := client.Database("test-profile") |
| 56 | + assert.NotNil(t, db) |
| 57 | + ok := db.RunCommand(ctx, bson.D{{"profile", 2}}) |
| 58 | + assert.NoErrorf(t, ok.Err(), "failed to enable profiling") |
| 59 | + coll := db.Collection("test") |
| 60 | + assert.NotNil(t, coll) |
| 61 | + _, err := coll.InsertOne(ctx, bson.M{}) |
| 62 | + assert.NoErrorf(t, err, "failed to run a profiled find") |
| 63 | + |
| 64 | + // run |
| 65 | + loopback := int64(10) // seconds. |
| 66 | + threshold := int64(1) // milliseconds. |
| 67 | + stats := GetDatabaseProfilerStats(client, loopback, threshold) |
| 68 | + |
| 69 | + // test |
| 70 | + assert.NotNil(t, stats) |
| 71 | + metricCh := make(chan prometheus.Metric) |
| 72 | + go func() { |
| 73 | + stats.Export(metricCh) |
| 74 | + close(metricCh) |
| 75 | + }() |
| 76 | + |
| 77 | + var metricsCount int |
| 78 | + for range metricCh { |
| 79 | + metricsCount++ |
| 80 | + } |
| 81 | + assert.Truef(t, metricsCount >= 1, "expected at least one slow query metric") |
| 82 | +} |
| 83 | + |
| 84 | +func TestGetDatabaseCurrentOpStatsDecodesFine(t *testing.T) { |
| 85 | + // setup |
| 86 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 87 | + defer cancel() |
| 88 | + client := testutils.MustGetConnectedReplSetClient(ctx, t) |
| 89 | + defer client.Disconnect(ctx) |
| 90 | + |
| 91 | + // GetDatabaseCurrentOpStats requires MongoDB 3.6+ |
| 92 | + // Skip this test if the version does not match. |
| 93 | + buildInfo, err := shared.GetBuildInfo(client) |
| 94 | + assert.NoErrorf(t, err, "failed to check MongoDB version") |
| 95 | + if buildInfo.VersionArray[0] < 3 || (buildInfo.VersionArray[0] == 3 && buildInfo.VersionArray[1] < 6) { |
| 96 | + t.Skip("MongoDB is not 3.6+, skipping test that requires $currentOp") |
| 97 | + } |
| 98 | + |
| 99 | + // run |
| 100 | + threshold := int64(1) // milliseconds. |
| 101 | + stats := GetDatabaseCurrentOpStats(client, threshold) |
| 102 | + |
| 103 | + // test |
| 104 | + assert.NotNil(t, stats) |
| 105 | +} |
0 commit comments