@@ -30,40 +30,89 @@ import (
30
30
)
31
31
32
32
func TestGeneralCollector (t * testing.T ) {
33
- ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
34
- defer cancel ()
33
+ t .Parallel ()
34
+ t .Run ("mongod cluster role" , func (t * testing.T ) {
35
+ t .Parallel ()
36
+ ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
37
+ defer cancel ()
35
38
36
- client := tu .DefaultTestClient (ctx , t )
37
- c := newGeneralCollector (ctx , client , logrus .New ())
39
+ client := tu .DefaultTestClient (ctx , t )
40
+ nodeType , _ := getNodeType (ctx , client )
41
+ c := newGeneralCollector (ctx , client , nodeType , logrus .New ())
38
42
39
- filter := []string {
40
- "collector_scrape_time_ms" ,
41
- }
42
- count := testutil .CollectAndCount (c , filter ... )
43
- assert .Equal (t , len (filter ), count , "Meta-metric for collector is missing" )
43
+ filter := []string {
44
+ "collector_scrape_time_ms" ,
45
+ }
46
+ count := testutil .CollectAndCount (c , filter ... )
47
+ assert .Equal (t , len (filter ), count , "Meta-metric for collector is missing" )
44
48
45
- // The last \n at the end of this string is important
46
- expected := strings .NewReader (`
49
+ // The last \n at the end of this string is important
50
+ expected := strings .NewReader (`
47
51
# HELP mongodb_up Whether MongoDB is up.
48
52
# TYPE mongodb_up gauge
49
- mongodb_up 1
53
+ mongodb_up {cluster_role="mongod"} 1
50
54
` + "\n " )
51
- filter = []string {
52
- "mongodb_up" ,
53
- }
54
- err := testutil .CollectAndCompare (c , expected , filter ... )
55
- require .NoError (t , err )
55
+ filter = []string {
56
+ "mongodb_up" ,
57
+ }
58
+ err := testutil .CollectAndCompare (c , expected , filter ... )
59
+ require .NoError (t , err )
56
60
57
- assert .NoError (t , client .Disconnect (ctx ))
61
+ assert .NoError (t , client .Disconnect (ctx ))
58
62
59
- expected = strings .NewReader (`
63
+ expected = strings .NewReader (`
60
64
# HELP mongodb_up Whether MongoDB is up.
61
65
# TYPE mongodb_up gauge
62
- mongodb_up 0
66
+ mongodb_up {cluster_role="mongod"} 0
63
67
` + "\n " )
64
- filter = []string {
65
- "mongodb_up" ,
66
- }
67
- err = testutil .CollectAndCompare (c , expected , filter ... )
68
- require .NoError (t , err )
68
+ filter = []string {
69
+ "mongodb_up" ,
70
+ }
71
+ err = testutil .CollectAndCompare (c , expected , filter ... )
72
+ require .NoError (t , err )
73
+ })
74
+
75
+ t .Run ("mongos cluster role" , func (t * testing.T ) {
76
+ t .Parallel ()
77
+ ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
78
+ defer cancel ()
79
+
80
+ port , err := tu .PortForContainer ("mongos" )
81
+ require .NoError (t , err )
82
+ client := tu .TestClient (ctx , port , t )
83
+
84
+ nodeType , _ := getNodeType (ctx , client )
85
+ c := newGeneralCollector (ctx , client , nodeType , logrus .New ())
86
+
87
+ filter := []string {
88
+ "collector_scrape_time_ms" ,
89
+ }
90
+ count := testutil .CollectAndCount (c , filter ... )
91
+ assert .Equal (t , len (filter ), count , "Meta-metric for collector is missing" )
92
+
93
+ // The last \n at the end of this string is important
94
+ expected := strings .NewReader (`
95
+ # HELP mongodb_up Whether MongoDB is up.
96
+ # TYPE mongodb_up gauge
97
+ mongodb_up {cluster_role="mongos"} 1
98
+ ` + "\n " )
99
+ filter = []string {
100
+ "mongodb_up" ,
101
+ }
102
+ err = testutil .CollectAndCompare (c , expected , filter ... )
103
+ require .NoError (t , err )
104
+
105
+ assert .NoError (t , client .Disconnect (ctx ))
106
+
107
+ expected = strings .NewReader (`
108
+ # HELP mongodb_up Whether MongoDB is up.
109
+ # TYPE mongodb_up gauge
110
+ mongodb_up {cluster_role="mongos"} 0
111
+ ` + "\n " )
112
+ filter = []string {
113
+ "mongodb_up" ,
114
+ }
115
+ err = testutil .CollectAndCompare (c , expected , filter ... )
116
+ require .NoError (t , err )
117
+ })
69
118
}
0 commit comments