telegraf/plugins/inputs/mongodb/mongodb_test.go

53 lines
869 B
Go
Raw Normal View History

2015-07-07 09:20:11 +08:00
// +build integration
package mongodb
import (
2021-07-23 04:50:23 +08:00
"context"
2015-07-07 09:20:11 +08:00
"log"
"math/rand"
"os"
"testing"
"time"
2021-07-23 04:50:23 +08:00
"github.com/influxdata/telegraf/testutil"
2015-07-07 09:20:11 +08:00
)
var server *Server
2021-07-23 04:50:23 +08:00
func testSetup(_ *testing.M) {
connectionString := os.Getenv("MONGODB_URL")
if connectionString == "" {
connectionString = "mongodb://127.0.0.1:27017"
2015-07-07 09:20:11 +08:00
}
2021-07-23 04:50:23 +08:00
m := &MongoDB{
Log: testutil.Logger{},
Servers: []string{connectionString},
2015-07-07 09:20:11 +08:00
}
2021-07-23 04:50:23 +08:00
err := m.Init()
2015-07-07 09:20:11 +08:00
if err != nil {
2021-07-23 04:50:23 +08:00
log.Fatalf("Failed to connect to MongoDB: %v", err)
2015-07-07 09:20:11 +08:00
}
2021-07-23 04:50:23 +08:00
server = m.clients[0]
2015-07-07 09:20:11 +08:00
}
2021-07-23 04:50:23 +08:00
func testTeardown(_ *testing.M) {
err := server.client.Disconnect(context.Background())
if err != nil {
log.Fatalf("failed to disconnect: %v", err)
}
2015-07-07 09:20:11 +08:00
}
func TestMain(m *testing.M) {
// seed randomness for use with tests
rand.Seed(time.Now().UTC().UnixNano())
testSetup(m)
res := m.Run()
testTeardown(m)
os.Exit(res)
}