diff --git a/exporter/replset_config_collector_test.go b/exporter/replset_config_collector_test.go new file mode 100644 index 000000000..1bdc66d0f --- /dev/null +++ b/exporter/replset_config_collector_test.go @@ -0,0 +1,80 @@ +// mongodb_exporter +// Copyright (C) 2017 Percona LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package exporter + +import ( + "context" + "strings" + "testing" + "time" + + "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + + "github.com/percona/mongodb_exporter/internal/tu" +) + +func TestReplsetConfigCollector(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + client := tu.DefaultTestClient(ctx, t) + + ti := labelsGetterMock{} + + c := &replSetGetConfigCollector{ + ctx: ctx, + client: client, + logger: logrus.New(), + topologyInfo: ti, + } + + // The last \n at the end of this string is important + expected := strings.NewReader(` +# HELP mongodb_cfg_protocolVersion cfg. +# TYPE mongodb_cfg_protocolVersion untyped +mongodb_cfg_protocolVersion 1` + "\n") + // Filter metrics for 2 reasons: + // 1. The result is huge + // 2. We need to check against know values. Don't use metrics that return counters like uptime + // or counters like the number of transactions because they won't return a known value to compare + filter := []string{ + "mongodb_cfg_protocolVersion", + } + err := testutil.CollectAndCompare(c, expected, filter...) + assert.NoError(t, err) +} + +func TestReplsetConfigCollectorNoSharding(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + client := tu.TestClient(ctx, tu.MongoDBStandAlonePort, t) + + ti := labelsGetterMock{} + + c := &replSetGetConfigCollector{ + ctx: ctx, + client: client, + topologyInfo: ti, + } + + expected := strings.NewReader(``) + err := testutil.CollectAndCompare(c, expected) + assert.NoError(t, err) +}