Skip to content

Commit

Permalink
Add replset_config_collector_test.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshi committed Sep 20, 2021
1 parent 54ded8c commit 22b9fe3
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions exporter/replset_config_collector_test.go
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

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)
}

0 comments on commit 22b9fe3

Please sign in to comment.