|
| 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 | + "sort" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/percona/exporter_shared/helpers" |
| 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 | + expected := []string{ |
| 65 | + "# HELP mongodb_dbstats_collections dbstats.", |
| 66 | + "# TYPE mongodb_dbstats_collections untyped", |
| 67 | + "mongodb_dbstats_collections{database=\"testdb\"} 3", |
| 68 | + "# HELP mongodb_dbstats_dataSize dbstats.", |
| 69 | + "# TYPE mongodb_dbstats_dataSize untyped", |
| 70 | + "mongodb_dbstats_dataSize{database=\"testdb\"} 1200", |
| 71 | + "# HELP mongodb_dbstats_indexSize dbstats.", |
| 72 | + "# TYPE mongodb_dbstats_indexSize untyped", |
| 73 | + "mongodb_dbstats_indexSize{database=\"testdb\"} 12288", |
| 74 | + "# HELP mongodb_dbstats_indexes dbstats.", |
| 75 | + "# TYPE mongodb_dbstats_indexes untyped", |
| 76 | + "mongodb_dbstats_indexes{database=\"testdb\"} 3", |
| 77 | + "# HELP mongodb_dbstats_objects dbstats.", |
| 78 | + "# TYPE mongodb_dbstats_objects untyped", |
| 79 | + "mongodb_dbstats_objects{database=\"testdb\"} 30", |
| 80 | + } |
| 81 | + |
| 82 | + metrics := helpers.CollectMetrics(c) |
| 83 | + actualMetrics := helpers.ReadMetrics(metrics) |
| 84 | + filters := []string{ |
| 85 | + "mongodb_dbstats_collections", |
| 86 | + "mongodb_dbstats_dataSize", |
| 87 | + "mongodb_dbstats_indexSize", |
| 88 | + "mongodb_dbstats_indexes", |
| 89 | + "mongodb_dbstats_objects", |
| 90 | + } |
| 91 | + labels := map[string]string{ |
| 92 | + "database": "testdb", |
| 93 | + } |
| 94 | + actualMetrics = filterMetricsWithLabels(actualMetrics, |
| 95 | + filters, |
| 96 | + labels) |
| 97 | + actualLines := helpers.Format(helpers.WriteMetrics(actualMetrics)) |
| 98 | + sort.Strings(actualLines) |
| 99 | + sort.Strings(expected) |
| 100 | + assert.Equal(t, expected, actualLines) |
| 101 | +} |
0 commit comments