forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb_test.go
52 lines (41 loc) · 869 Bytes
/
mongodb_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// +build integration
package mongodb
import (
"context"
"log"
"math/rand"
"os"
"testing"
"time"
"github.com/influxdata/telegraf/testutil"
)
var server *Server
func testSetup(_ *testing.M) {
connectionString := os.Getenv("MONGODB_URL")
if connectionString == "" {
connectionString = "mongodb://127.0.0.1:27017"
}
m := &MongoDB{
Log: testutil.Logger{},
Servers: []string{connectionString},
}
err := m.Init()
if err != nil {
log.Fatalf("Failed to connect to MongoDB: %v", err)
}
server = m.clients[0]
}
func testTeardown(_ *testing.M) {
err := server.client.Disconnect(context.Background())
if err != nil {
log.Fatalf("failed to disconnect: %v", err)
}
}
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)
}