@@ -8,65 +8,127 @@ import (
8
8
9
9
"github.com/stretchr/testify/assert"
10
10
"go.mongodb.org/mongo-driver/bson"
11
+ "go.mongodb.org/mongo-driver/mongo"
11
12
12
13
"github.com/percona/mongodb_exporter/internal/tu"
13
14
)
14
15
15
- func TestListCollections (t * testing.T ) {
16
- t .Parallel ()
17
- ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
18
- defer cancel ()
19
-
20
- client := tu .DefaultTestClient (ctx , t )
21
-
22
- inDBs := []string {"testdb01" , "testdb02" }
23
- inColls := []string {"col01" , "col02" , "colxx" , "colyy" }
16
+ //nolint:gochecknoglobals
17
+ var (
18
+ testDBs = []string {"testdb01" , "testdb02" }
19
+ testColls = []string {"col01" , "col02" , "colxx" , "colyy" }
20
+ )
24
21
25
- defer func () {
26
- for _ , dbname := range inDBs {
27
- client .Database (dbname ).Drop (ctx ) //nolint:errcheck
28
- }
29
- }()
22
+ func setupDB (ctx context.Context , t * testing.T , client * mongo.Client ) {
23
+ t .Helper ()
30
24
31
- for _ , dbname := range inDBs {
32
- for _ , coll := range inColls {
25
+ for _ , dbname := range testDBs {
26
+ for _ , coll := range testColls {
33
27
for j := 0 ; j < 10 ; j ++ {
34
28
_ , err := client .Database (dbname ).Collection (coll ).InsertOne (ctx , bson.M {"f1" : j , "f2" : "2" })
35
29
assert .NoError (t , err )
36
30
}
37
31
}
38
32
}
33
+ }
34
+
35
+ func cleanupDB (ctx context.Context , client * mongo.Client ) {
36
+ for _ , dbname := range testDBs {
37
+ client .Database (dbname ).Drop (ctx ) //nolint:errcheck
38
+ }
39
+ }
39
40
40
- want := [] string { "admin" , "config" , "local" , "testdb01" , "testdb02" }
41
- allDBs , err := databases ( ctx , client , nil , nil )
42
- assert . NoError ( t , err )
43
- assert . Equal ( t , want , allDBs )
41
+ //nolint:paralleltest
42
+ func TestListDatabases ( t * testing. T ) {
43
+ ctx , cancel := context . WithTimeout ( context . Background (), 3 * time . Second )
44
+ defer cancel ( )
44
45
45
- want = []string {"col01" , "col02" , "colxx" }
46
- inNameSpaces := []string {inDBs [0 ] + ".col0" , inDBs [0 ] + ".colx" }
47
- colls , err := listCollections (ctx , client , inDBs [0 ], inNameSpaces )
48
- sort .Strings (colls )
46
+ client := tu .DefaultTestClient (ctx , t )
49
47
50
- assert . NoError ( t , err )
51
- assert . Equal ( t , want , colls )
48
+ setupDB ( ctx , t , client )
49
+ defer cleanupDB ( ctx , client )
52
50
53
- // Advanced filtering test
54
- wantNS := map [string ][]string {
55
- "testdb01" : {"col01" , "col02" , "colxx" , "colyy" },
56
- "testdb02" : {"col01" , "col02" },
57
- }
51
+ t .Run ("Empty filter in list" , func (t * testing.T ) {
52
+ want := []string {"testdb01" , "testdb02" }
53
+ allDBs , err := databases (ctx , client , nil , systemDBs )
54
+ assert .NoError (t , err )
55
+ assert .Equal (t , want , allDBs )
56
+ })
58
57
59
- // List all collections in testdb01 (inDBs[0]) but only col01 and col02 from testdb02.
60
- filterInNameSpaces := []string {inDBs [0 ], inDBs [1 ] + ".col01" , inDBs [1 ] + ".col02" }
61
- namespaces , err := listAllCollections (ctx , client , filterInNameSpaces , systemDBs )
62
- assert .NoError (t , err )
63
- assert .Equal (t , wantNS , namespaces )
58
+ t .Run ("One collection in list" , func (t * testing.T ) {
59
+ want := []string {"testdb01" }
60
+ allDBs , err := databases (ctx , client , []string {"testdb01.col1" }, systemDBs )
61
+ assert .NoError (t , err )
62
+ assert .Equal (t , want , allDBs )
63
+ })
64
+
65
+ t .Run ("Multiple namespaces in list" , func (t * testing.T ) {
66
+ want := []string {"testdb01" , "testdb02" }
67
+ allDBs , err := databases (ctx , client , []string {"testdb01" , "testdb02.col2" , "testdb02.col1" }, systemDBs )
68
+ assert .NoError (t , err )
69
+ assert .Equal (t , want , allDBs )
70
+ })
71
+ }
72
+
73
+ //nolint:paralleltest
74
+ func TestListCollections (t * testing.T ) {
75
+ ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
76
+ defer cancel ()
77
+
78
+ client := tu .DefaultTestClient (ctx , t )
79
+
80
+ setupDB (ctx , t , client )
81
+ defer cleanupDB (ctx , client )
82
+
83
+ t .Run ("Basic test" , func (t * testing.T ) {
84
+ want := []string {"admin" , "config" , "local" , "testdb01" , "testdb02" }
85
+ allDBs , err := databases (ctx , client , nil , nil )
86
+ assert .NoError (t , err )
87
+ assert .Equal (t , want , allDBs )
88
+ })
89
+
90
+ t .Run ("Filter in databases" , func (t * testing.T ) {
91
+ want := []string {"col01" , "col02" , "colxx" }
92
+ inNameSpaces := []string {testDBs [0 ] + ".col0" , testDBs [0 ] + ".colx" }
93
+ colls , err := listCollections (ctx , client , testDBs [0 ], inNameSpaces )
94
+ sort .Strings (colls )
95
+
96
+ assert .NoError (t , err )
97
+ assert .Equal (t , want , colls )
98
+ })
99
+
100
+ t .Run ("With namespaces list" , func (t * testing.T ) {
101
+ // Advanced filtering test
102
+ wantNS := map [string ][]string {
103
+ "testdb01" : {"col01" , "col02" , "colxx" , "colyy" },
104
+ "testdb02" : {"col01" , "col02" },
105
+ }
106
+ // List all collections in testdb01 (inDBs[0]) but only col01 and col02 from testdb02.
107
+ filterInNameSpaces := []string {testDBs [0 ], testDBs [1 ] + ".col01" , testDBs [1 ] + ".col02" }
108
+ namespaces , err := listAllCollections (ctx , client , filterInNameSpaces , systemDBs )
109
+ assert .NoError (t , err )
110
+ assert .Equal (t , wantNS , namespaces )
111
+ })
112
+
113
+ t .Run ("Empty namespaces list" , func (t * testing.T ) {
114
+ wantNS := map [string ][]string {
115
+ "testdb01" : {"col01" , "col02" , "colxx" , "colyy" },
116
+ "testdb02" : {"col01" , "col02" , "colxx" , "colyy" },
117
+ }
118
+ namespaces , err := listAllCollections (ctx , client , nil , systemDBs )
119
+ assert .NoError (t , err )
120
+ assert .Equal (t , wantNS , namespaces )
121
+ })
64
122
65
- count , err := nonSystemCollectionsCount (ctx , client , nil , nil )
66
- assert .NoError (t , err )
67
- assert .True (t , count == 8 )
123
+ t .Run ("Count basic" , func (t * testing.T ) {
124
+ count , err := nonSystemCollectionsCount (ctx , client , nil , nil )
125
+ assert .NoError (t , err )
126
+ assert .Equal (t , 8 , count )
127
+ })
68
128
69
- count , err = nonSystemCollectionsCount (ctx , client , nil , []string {inDBs [0 ] + ".col0" , inDBs [0 ] + ".colx" })
70
- assert .NoError (t , err )
71
- assert .Equal (t , 6 , count )
129
+ t .Run ("Filtered count" , func (t * testing.T ) {
130
+ count , err := nonSystemCollectionsCount (ctx , client , nil , []string {testDBs [0 ] + ".col0" , testDBs [0 ] + ".colx" })
131
+ assert .NoError (t , err )
132
+ assert .Equal (t , 6 , count )
133
+ })
72
134
}
0 commit comments