Skip to content

Commit 22b9fe3

Browse files
committed
Add replset_config_collector_test.
1 parent 54ded8c commit 22b9fe3

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
"strings"
22+
"testing"
23+
"time"
24+
25+
"github.com/prometheus/client_golang/prometheus/testutil"
26+
"github.com/sirupsen/logrus"
27+
"github.com/stretchr/testify/assert"
28+
29+
"github.com/percona/mongodb_exporter/internal/tu"
30+
)
31+
32+
func TestReplsetConfigCollector(t *testing.T) {
33+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
34+
defer cancel()
35+
36+
client := tu.DefaultTestClient(ctx, t)
37+
38+
ti := labelsGetterMock{}
39+
40+
c := &replSetGetConfigCollector{
41+
ctx: ctx,
42+
client: client,
43+
logger: logrus.New(),
44+
topologyInfo: ti,
45+
}
46+
47+
// The last \n at the end of this string is important
48+
expected := strings.NewReader(`
49+
# HELP mongodb_cfg_protocolVersion cfg.
50+
# TYPE mongodb_cfg_protocolVersion untyped
51+
mongodb_cfg_protocolVersion 1` + "\n")
52+
// Filter metrics for 2 reasons:
53+
// 1. The result is huge
54+
// 2. We need to check against know values. Don't use metrics that return counters like uptime
55+
// or counters like the number of transactions because they won't return a known value to compare
56+
filter := []string{
57+
"mongodb_cfg_protocolVersion",
58+
}
59+
err := testutil.CollectAndCompare(c, expected, filter...)
60+
assert.NoError(t, err)
61+
}
62+
63+
func TestReplsetConfigCollectorNoSharding(t *testing.T) {
64+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
65+
defer cancel()
66+
67+
client := tu.TestClient(ctx, tu.MongoDBStandAlonePort, t)
68+
69+
ti := labelsGetterMock{}
70+
71+
c := &replSetGetConfigCollector{
72+
ctx: ctx,
73+
client: client,
74+
topologyInfo: ti,
75+
}
76+
77+
expected := strings.NewReader(``)
78+
err := testutil.CollectAndCompare(c, expected)
79+
assert.NoError(t, err)
80+
}

0 commit comments

Comments
 (0)