|
| 1 | +// mongodb_exporter |
| 2 | +// Copyright (C) 2017 Percona LLC |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package exporter |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/prometheus/client_golang/prometheus/testutil" |
| 27 | + "github.com/sirupsen/logrus" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + "go.mongodb.org/mongo-driver/bson" |
| 30 | + |
| 31 | + "github.com/percona/mongodb_exporter/internal/tu" |
| 32 | +) |
| 33 | + |
| 34 | +func TestDBStatsCollector(t *testing.T) { |
| 35 | + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) |
| 36 | + defer cancel() |
| 37 | + |
| 38 | + client := tu.DefaultTestClient(ctx, t) |
| 39 | + |
| 40 | + database := client.Database("testdb") |
| 41 | + database.Drop(ctx) //nolint |
| 42 | + |
| 43 | + defer func() { |
| 44 | + err := database.Drop(ctx) |
| 45 | + assert.NoError(t, err) |
| 46 | + }() |
| 47 | + |
| 48 | + for i := 0; i < 3; i++ { |
| 49 | + coll := fmt.Sprintf("testcol_%02d", i) |
| 50 | + for j := 0; j < 10; j++ { |
| 51 | + _, err := database.Collection(coll).InsertOne(ctx, bson.M{"f1": j, "f2": "2"}) |
| 52 | + assert.NoError(t, err) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + ti := labelsGetterMock{} |
| 57 | + |
| 58 | + c := &dbstatsCollector{ |
| 59 | + client: client, |
| 60 | + logger: logrus.New(), |
| 61 | + topologyInfo: ti, |
| 62 | + } |
| 63 | + |
| 64 | + // The last \n at the end of this string is important |
| 65 | + expected := strings.NewReader(` |
| 66 | +# HELP mongodb_dbstats_testdb_avgObjSize dbstats_testdb. |
| 67 | +# TYPE mongodb_dbstats_testdb_avgObjSize untyped |
| 68 | +mongodb_dbstats_testdb_avgObjSize 40 |
| 69 | +# HELP mongodb_dbstats_testdb_collections dbstats_testdb. |
| 70 | +# TYPE mongodb_dbstats_testdb_collections untyped |
| 71 | +mongodb_dbstats_testdb_collections 3 |
| 72 | +# HELP mongodb_dbstats_testdb_dataSize dbstats_testdb. |
| 73 | +# TYPE mongodb_dbstats_testdb_dataSize untyped |
| 74 | +mongodb_dbstats_testdb_dataSize 1200 |
| 75 | +# HELP mongodb_dbstats_testdb_indexSize dbstats_testdb. |
| 76 | +# TYPE mongodb_dbstats_testdb_indexSize untyped |
| 77 | +mongodb_dbstats_testdb_indexSize 12288 |
| 78 | +# HELP mongodb_dbstats_testdb_indexes dbstats_testdb. |
| 79 | +# TYPE mongodb_dbstats_testdb_indexes untyped |
| 80 | +mongodb_dbstats_testdb_indexes 3 |
| 81 | +# HELP mongodb_dbstats_testdb_objects dbstats_testdb. |
| 82 | +# TYPE mongodb_dbstats_testdb_objects untyped |
| 83 | +mongodb_dbstats_testdb_objects 30 |
| 84 | +# HELP mongodb_dbstats_testdb_ok dbstats_testdb. |
| 85 | +# TYPE mongodb_dbstats_testdb_ok untyped |
| 86 | +mongodb_dbstats_testdb_ok 1 |
| 87 | +# HELP mongodb_dbstats_testdb_storageSize dbstats_testdb. |
| 88 | +# TYPE mongodb_dbstats_testdb_storageSize untyped |
| 89 | +mongodb_dbstats_testdb_storageSize 12288 |
| 90 | +# HELP mongodb_dbstats_testdb_views dbstats_testdb. |
| 91 | +# TYPE mongodb_dbstats_testdb_views untyped |
| 92 | +mongodb_dbstats_testdb_views 0` + |
| 93 | + "\n") |
| 94 | + |
| 95 | + // Filter metrics for 2 reasons: |
| 96 | + // 1. The result is huge |
| 97 | + // 2. We need to check against know values. Don't use metrics that return counters like uptime |
| 98 | + // or counters like the number of transactions because they won't return a known value to compare |
| 99 | + filter := []string{ |
| 100 | + "mongodb_dbstats_testdb_avgObjSize", |
| 101 | + "mongodb_dbstats_testdb_collections", |
| 102 | + "mongodb_dbstats_testdb_dataSize", |
| 103 | + "mongodb_dbstats_testdb_indexSize", |
| 104 | + "mongodb_dbstats_testdb_indexes", |
| 105 | + "mongodb_dbstats_testdb_objects", |
| 106 | + "mongodb_dbstats_testdb_views", |
| 107 | + "mongodb_dbstats_testdb_storageSize", |
| 108 | + "mongodb_dbstats_testdb_ok", |
| 109 | + } |
| 110 | + err := testutil.CollectAndCompare(c, expected, filter...) |
| 111 | + assert.NoError(t, err) |
| 112 | +} |
0 commit comments