@@ -25,7 +25,10 @@ import (
2525 "github.com/prometheus/client_golang/prometheus/testutil"
2626 "github.com/prometheus/common/promslog"
2727 "github.com/stretchr/testify/assert"
28+ "github.com/stretchr/testify/require"
2829 "go.mongodb.org/mongo-driver/bson"
30+ "go.mongodb.org/mongo-driver/mongo"
31+ "go.mongodb.org/mongo-driver/mongo/options"
2932
3033 "github.com/percona/mongodb_exporter/internal/tu"
3134)
@@ -93,3 +96,61 @@ mongodb_collstats_storageStats_capped{collection="testcol_02",database="testdb"}
9396 err := testutil .CollectAndCompare (c , expected , filter ... )
9497 assert .NoError (t , err )
9598}
99+
100+ func TestCollStatsForFakeCountType (t * testing.T ) {
101+ t .Parallel ()
102+ ctx , cancel := context .WithTimeout (t .Context (), 3 * time .Second )
103+ defer cancel ()
104+
105+ client := tu .DefaultTestClient (ctx , t )
106+
107+ database := client .Database ("testdb" )
108+ database .Drop (ctx ) //nolint
109+
110+ defer func () {
111+ err := database .Drop (ctx )
112+ require .NoError (t , err )
113+ }()
114+
115+ collName := "test_collection_account"
116+ coll := database .Collection (collName )
117+
118+ _ , err := coll .InsertOne (ctx , bson.M {"account_id" : 1 , "count" : 10 })
119+ require .NoError (t , err )
120+ _ , err = coll .InsertOne (ctx , bson.M {"account_id" : 2 , "count" : 20 })
121+ require .NoError (t , err )
122+
123+ indexModel := mongo.IndexModel {
124+ Keys : bson.D {{Key : "account_id" , Value : 1 }},
125+ Options : options .Index ().SetName ("test_index_account" ),
126+ }
127+ _ , err = coll .Indexes ().CreateOne (ctx , indexModel )
128+ require .NoError (t , err )
129+
130+ indexModel = mongo.IndexModel {
131+ Keys : bson.D {{Key : "count" , Value : 1 }},
132+ Options : options .Index ().SetName ("test_index_count" ),
133+ }
134+ _ , err = coll .Indexes ().CreateOne (ctx , indexModel )
135+ require .NoError (t , err )
136+
137+ ti := labelsGetterMock {}
138+
139+ collection := []string {"testdb.test_collection_account" }
140+ logger := promslog .New (& promslog.Config {})
141+ c := newCollectionStatsCollector (ctx , client , logger , false , ti , collection , false )
142+
143+ expected := strings .NewReader (`
144+ # HELP mongodb_collstats_storageStats_indexSizes collstats.storageStats.indexSizes
145+ # TYPE mongodb_collstats_storageStats_indexSizes untyped
146+ mongodb_collstats_storageStats_indexSizes{collection="test_collection_account",database="testdb",index_name="_id_"} 4096
147+ mongodb_collstats_storageStats_indexSizes{collection="test_collection_account",database="testdb",index_name="test_index_account"} 20480
148+ mongodb_collstats_storageStats_indexSizes{collection="test_collection_account",database="testdb",index_name="test_index_count"} 20480
149+ ` )
150+
151+ filter := []string {
152+ "mongodb_collstats_storageStats_indexSizes" ,
153+ }
154+ err = testutil .CollectAndCompare (c , expected , filter ... )
155+ require .NoError (t , err )
156+ }
0 commit comments