Skip to content

Commit 6049b11

Browse files
committed
Add replset_config_collector_test.
1 parent 54ded8c commit 6049b11

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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_myState myState
50+
# TYPE mongodb_myState untyped
51+
mongodb_myState 1
52+
# HELP mongodb_ok ok
53+
# TYPE mongodb_ok untyped
54+
mongodb_ok 1` + "\n")
55+
// Filter metrics for 2 reasons:
56+
// 1. The result is huge
57+
// 2. We need to check against know values. Don't use metrics that return counters like uptime
58+
// or counters like the number of transactions because they won't return a known value to compare
59+
filter := []string{
60+
// "mongodb_myState",
61+
// "mongodb_ok",
62+
}
63+
err := testutil.CollectAndCompare(c, expected, filter...)
64+
assert.NoError(t, err)
65+
}
66+
67+
func TestReplsetConfigCollectorNoSharding(t *testing.T) {
68+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
69+
defer cancel()
70+
71+
client := tu.TestClient(ctx, tu.MongoDBStandAlonePort, t)
72+
73+
ti := labelsGetterMock{}
74+
75+
c := &replSetGetConfigCollector{
76+
ctx: ctx,
77+
client: client,
78+
topologyInfo: ti,
79+
}
80+
81+
expected := strings.NewReader(``)
82+
err := testutil.CollectAndCompare(c, expected)
83+
assert.NoError(t, err)
84+
}

0 commit comments

Comments
 (0)