@@ -20,7 +20,7 @@ import (
2020
2121type Handler func (w http.ResponseWriter , r * http.Request )
2222
23- func BasicAuth (basicAuth lib.BasicAuth , pass Handler ) Handler {
23+ func BasicAuthHandler (basicAuth lib.BasicAuth , pass Handler ) Handler {
2424
2525 validate := func (basicAuth lib.BasicAuth , username , password string ) bool {
2626 if username == basicAuth .Username && password == basicAuth .Password {
@@ -88,7 +88,7 @@ func couchdbResponse(t *testing.T, versionSuffix string) Handler {
8888
8989func performCouchdbStatsTest (t * testing.T , couchdbVersion string , expectedMetricsCount int , expectedGetRequestCount float64 , expectedDiskSize float64 ) {
9090 basicAuth := lib.BasicAuth {Username : "username" , Password : "password" }
91- handler := http .HandlerFunc (BasicAuth (basicAuth , couchdbResponse (t , couchdbVersion )))
91+ handler := http .HandlerFunc (BasicAuthHandler (basicAuth , couchdbResponse (t , couchdbVersion )))
9292 server := httptest .NewServer (handler )
9393
9494 e := lib .NewExporter (server .URL , basicAuth , []string {"example" , "another-example" }, true )
@@ -109,12 +109,18 @@ func performCouchdbStatsTest(t *testing.T, couchdbVersion string, expectedMetric
109109 t .Errorf ("got more metrics (%d) as expected (%d)" , metricsCount , expectedMetricsCount )
110110 }
111111
112- actualGetRequestCount := testutil .GetGaugeValue (metricFamilies , "couchdb_httpd_request_methods" , "method" , "GET" )
112+ actualGetRequestCount , err := testutil .GetGaugeValue (metricFamilies , "couchdb_httpd_request_methods" , "method" , "GET" )
113+ if err != nil {
114+ t .Error (err )
115+ }
113116 if expectedGetRequestCount != actualGetRequestCount {
114117 t .Errorf ("expected %f GET requests, but got %f instead" , expectedGetRequestCount , actualGetRequestCount )
115118 }
116119
117- actualDiskSize := testutil .GetGaugeValue (metricFamilies , "couchdb_database_disk_size" , "db_name" , "example" )
120+ actualDiskSize , err := testutil .GetGaugeValue (metricFamilies , "couchdb_database_disk_size" , "db_name" , "example" )
121+ if err != nil {
122+ t .Error (err )
123+ }
118124 if expectedDiskSize != actualDiskSize {
119125 t .Errorf ("expected %f disk size, but got %f instead" , expectedDiskSize , actualDiskSize )
120126 }
@@ -139,9 +145,18 @@ func TestCouchdbStatsV1Integration(t *testing.T) {
139145 t .Error (err )
140146 }
141147 dbUrl := fmt .Sprintf ("http://%s" , dbAddress )
148+ basicAuth := lib.BasicAuth {Username : "root" , Password : "a-secret" }
149+
150+ client := lib .NewCouchdbClient (dbUrl , basicAuth , true )
151+ databases := []string {"v1_testdb1" , "v1_testdb2" }
152+ for _ , db := range databases {
153+ _ , err = client .Request ("PUT" , fmt .Sprintf ("%s/%s" , client .BaseUri , db ), nil )
154+ if err != nil {
155+ t .Error (err )
156+ }
157+ }
142158
143159 t .Run ("node_up" , func (t * testing.T ) {
144- basicAuth := lib.BasicAuth {Username : "root" , Password : "a-secret" }
145160 e := lib .NewExporter (dbUrl , basicAuth , []string {}, true )
146161
147162 ch := make (chan prometheus.Metric )
@@ -153,19 +168,52 @@ func TestCouchdbStatsV1Integration(t *testing.T) {
153168 metricFamilies := testutil .CollectMetrics (ch , false )
154169
155170 nodeName := "master"
156- actualNodeUp := testutil .GetGaugeValue (metricFamilies , "couchdb_httpd_node_up" , "node_name" , nodeName )
171+ actualNodeUp , err := testutil .GetGaugeValue (metricFamilies , "couchdb_httpd_node_up" , "node_name" , nodeName )
172+ if err != nil {
173+ t .Error (err )
174+ }
157175 if actualNodeUp != 1 {
158176 t .Errorf ("Expected node '%s' at '%s' to be available." , nodeName , dbUrl )
159177 }
160178 })
179+
180+ t .Run ("_all_dbs" , func (t * testing.T ) {
181+ e := lib .NewExporter (dbUrl , basicAuth , []string {"_all_dbs" }, true )
182+
183+ ch := make (chan prometheus.Metric )
184+ go func () {
185+ defer close (ch )
186+ e .Collect (ch )
187+ }()
188+
189+ metricFamilies := testutil .CollectMetrics (ch , false )
190+
191+ for _ , db := range databases {
192+ databaseDataSize , err := testutil .GetGaugeValue (metricFamilies , "couchdb_database_data_size" , "db_name" , db )
193+ if err != nil {
194+ log .Println (err )
195+ t .Errorf ("Expected stats to be collected for database '%s'." , db )
196+ }
197+ if databaseDataSize != 0 {
198+ t .Errorf ("Expected database data size to be 0 for database '%s'." , db )
199+ }
200+ }
201+ })
202+
203+ for _ , db := range databases {
204+ _ , err = client .Request ("DELETE" , fmt .Sprintf ("%s/%s" , client .BaseUri , db ), nil )
205+ if err != nil {
206+ t .Error (err )
207+ }
208+ }
161209}
162210
163- func membership (t * testing.T , basicAuth lib.BasicAuth ) (func (address string ) (bool , error )) {
211+ func awaitMembership (t * testing.T , basicAuth lib.BasicAuth ) (func (address string ) (bool , error )) {
164212 time .Sleep (5 * time .Second )
165213
166214 return func (address string ) (bool , error ) {
167215 dbUrl := fmt .Sprintf ("http://%s" , address )
168- c := lib .NewCouchdbClient (dbUrl , basicAuth , [] string {}, true )
216+ c := lib .NewCouchdbClient (dbUrl , basicAuth , true )
169217 nodeNames , err := c .GetNodeNames ()
170218 if err != nil {
171219 if err , ok := err .(net.Error ); ok && (err .Timeout () || err .Temporary ()) {
@@ -198,11 +246,20 @@ func TestCouchdbStatsV2Integration(t *testing.T) {
198246 dbUrl := fmt .Sprintf ("http://%s" , dbAddress )
199247 basicAuth := lib.BasicAuth {Username : "root" , Password : "a-secret" }
200248
201- err = cluster_config .AwaitNodes ([]string {dbAddress }, membership (t , basicAuth ))
249+ err = cluster_config .AwaitNodes ([]string {dbAddress }, awaitMembership (t , basicAuth ))
202250 if err != nil {
203251 t .Error (err )
204252 }
205253
254+ client := lib .NewCouchdbClient (dbUrl , basicAuth , true )
255+ databases := []string {"v2_testdb1" , "v2_testdb2" }
256+ for _ , db := range databases {
257+ _ , err = client .Request ("PUT" , fmt .Sprintf ("%s/%s" , client .BaseUri , db ), nil )
258+ if err != nil {
259+ t .Error (err )
260+ }
261+ }
262+
206263 t .Run ("node_up" , func (t * testing.T ) {
207264 e := lib .NewExporter (dbUrl , basicAuth , []string {}, true )
208265
@@ -215,12 +272,42 @@ func TestCouchdbStatsV2Integration(t *testing.T) {
215272 metricFamilies := testutil .CollectMetrics (ch , false )
216273
217274218- actualNodeUp := testutil .GetGaugeValue (metricFamilies , "couchdb_httpd_node_up" , "node_name" , nodeName )
275+ actualNodeUp , err := testutil .GetGaugeValue (metricFamilies , "couchdb_httpd_node_up" , "node_name" , nodeName )
276+ if err != nil {
277+ t .Error (err )
278+ }
219279 if actualNodeUp != 1 {
220280 t .Errorf ("Expected node '%s' at '%s' to be available." , nodeName , dbUrl )
221281 }
222282 })
223283
224- // <tear-down code>
225- // (nothing to do)
284+ t .Run ("_all_dbs" , func (t * testing.T ) {
285+ e := lib .NewExporter (dbUrl , basicAuth , []string {"_all_dbs" }, true )
286+
287+ ch := make (chan prometheus.Metric )
288+ go func () {
289+ defer close (ch )
290+ e .Collect (ch )
291+ }()
292+
293+ metricFamilies := testutil .CollectMetrics (ch , false )
294+
295+ for _ , db := range databases {
296+ databaseDataSize , err := testutil .GetGaugeValue (metricFamilies , "couchdb_database_data_size" , "db_name" , db )
297+ if err != nil {
298+ log .Println (err )
299+ t .Errorf ("Expected stats to be collected for database '%s'." , db )
300+ }
301+ if databaseDataSize != 0 {
302+ t .Errorf ("Expected database data size to be 0 for database '%s'." , db )
303+ }
304+ }
305+ })
306+
307+ for _ , db := range databases {
308+ _ , err = client .Request ("DELETE" , fmt .Sprintf ("%s/%s" , client .BaseUri , db ), nil )
309+ if err != nil {
310+ t .Error (err )
311+ }
312+ }
226313}
0 commit comments