Skip to content

Commit 6ecb3e5

Browse files
authored
Merge pull request #178 from Sticksman/add-collector-test
Add collector test
2 parents decd013 + 0d86c3e commit 6ecb3e5

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

Diff for: collector_test.go

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2024 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific langu
12+
package main
13+
14+
import (
15+
"testing"
16+
17+
"log/slog"
18+
19+
"github.com/DATA-DOG/go-sqlmock"
20+
"github.com/prometheus/client_golang/prometheus"
21+
dto "github.com/prometheus/client_model/go"
22+
"github.com/smartystreets/goconvey/convey"
23+
)
24+
25+
type labelMap map[string]string
26+
27+
type MetricResult struct {
28+
labels labelMap
29+
value float64
30+
metricType dto.MetricType
31+
}
32+
33+
func readMetric(m prometheus.Metric) MetricResult {
34+
pb := &dto.Metric{}
35+
m.Write(pb)
36+
labels := make(labelMap, len(pb.Label))
37+
for _, v := range pb.Label {
38+
labels[v.GetName()] = v.GetValue()
39+
}
40+
if pb.Gauge != nil {
41+
return MetricResult{labels: labels, value: pb.GetGauge().GetValue(), metricType: dto.MetricType_GAUGE}
42+
}
43+
if pb.Counter != nil {
44+
return MetricResult{labels: labels, value: pb.GetCounter().GetValue(), metricType: dto.MetricType_COUNTER}
45+
}
46+
if pb.Untyped != nil {
47+
return MetricResult{labels: labels, value: pb.GetUntyped().GetValue(), metricType: dto.MetricType_UNTYPED}
48+
}
49+
panic("Unsupported metric type")
50+
}
51+
52+
func TestQueryShowList(t *testing.T) {
53+
db, mock, err := sqlmock.New()
54+
if err != nil {
55+
t.Fatalf("Error opening a stub db connection: %s", err)
56+
}
57+
defer db.Close()
58+
59+
rows := sqlmock.NewRows([]string{"key", "value"}).
60+
AddRow("dns_queries", -1).
61+
AddRow("databases", 1).
62+
AddRow("pools", 0).
63+
AddRow("users", 2)
64+
65+
mock.ExpectQuery("SHOW LISTS;").WillReturnRows(rows)
66+
logger := &slog.Logger{}
67+
68+
ch := make(chan prometheus.Metric)
69+
go func() {
70+
defer close(ch)
71+
if err := queryShowLists(ch, db, logger); err != nil {
72+
t.Errorf("Error running queryShowList: %s", err)
73+
}
74+
}()
75+
76+
expected := []MetricResult{
77+
{labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: -1},
78+
{labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: 1},
79+
{labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: 0},
80+
{labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: 2},
81+
}
82+
83+
convey.Convey("Metrics comparison", t, func() {
84+
for _, expect := range expected {
85+
m := readMetric(<-ch)
86+
convey.So(expect, convey.ShouldResemble, m)
87+
}
88+
})
89+
if err := mock.ExpectationsWereMet(); err != nil {
90+
t.Errorf("there were unfulfilled exceptions: %s", err)
91+
}
92+
}
93+
94+
func TestQueryShowConfig(t *testing.T) {
95+
db, mock, err := sqlmock.New()
96+
if err != nil {
97+
t.Fatalf("Error opening a stub db connection: %s", err)
98+
}
99+
defer db.Close()
100+
101+
rows := sqlmock.NewRows([]string{"key", "value", "default", "changeable"}).
102+
AddRow("max_client_conn", 1900, 100, true).
103+
AddRow("max_user_connections", 100, 100, true).
104+
AddRow("auth_type", "md5", "md5", true).
105+
AddRow("client_tls_ciphers", "default", "default", "yes")
106+
107+
mock.ExpectQuery("SHOW CONFIG;").WillReturnRows(rows)
108+
logger := &slog.Logger{}
109+
110+
ch := make(chan prometheus.Metric)
111+
go func() {
112+
defer close(ch)
113+
if err := queryShowConfig(ch, db, logger); err != nil {
114+
t.Errorf("Error running queryShowConfig: %s", err)
115+
}
116+
}()
117+
118+
expected := []MetricResult{
119+
{labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: 1900},
120+
{labels: labelMap{}, metricType: dto.MetricType_GAUGE, value: 100},
121+
}
122+
convey.Convey("Metrics comparison", t, func() {
123+
for _, expect := range expected {
124+
m := readMetric(<-ch)
125+
convey.So(expect, convey.ShouldResemble, m)
126+
}
127+
})
128+
if err := mock.ExpectationsWereMet(); err != nil {
129+
t.Errorf("there were unfulfilled exceptions: %s", err)
130+
}
131+
}

Diff for: go.mod

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,31 @@ module github.com/prometheus-community/pgbouncer_exporter
33
go 1.22
44

55
require (
6+
github.com/DATA-DOG/go-sqlmock v1.5.2
67
github.com/alecthomas/kingpin/v2 v2.4.0
78
github.com/lib/pq v1.10.9
89
github.com/prometheus/client_golang v1.20.4
10+
github.com/prometheus/client_model v0.6.1
911
github.com/prometheus/common v0.60.0
1012
github.com/prometheus/exporter-toolkit v0.13.0
13+
github.com/smartystreets/goconvey v1.8.1
1114
)
1215

1316
require (
1417
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
1518
github.com/beorn7/perks v1.0.1 // indirect
1619
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1720
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
21+
github.com/gopherjs/gopherjs v1.17.2 // indirect
1822
github.com/jpillora/backoff v1.0.0 // indirect
23+
github.com/jtolds/gls v4.20.0+incompatible // indirect
1924
github.com/klauspost/compress v1.17.9 // indirect
2025
github.com/mdlayher/socket v0.4.1 // indirect
2126
github.com/mdlayher/vsock v1.2.1 // indirect
2227
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2328
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
24-
github.com/prometheus/client_model v0.6.1 // indirect
2529
github.com/prometheus/procfs v0.15.1 // indirect
30+
github.com/smarty/assertions v1.15.0 // indirect
2631
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
2732
golang.org/x/crypto v0.27.0 // indirect
2833
golang.org/x/net v0.29.0 // indirect

Diff for: go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
2+
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
13
github.com/alecthomas/kingpin/v2 v2.4.0 h1:f48lwail6p8zpO1bC4TxtqACaGqHYA22qkHjHpqDjYY=
24
github.com/alecthomas/kingpin/v2 v2.4.0/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE=
35
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
@@ -14,8 +16,13 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1416
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
1517
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1618
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
19+
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
20+
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
1721
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
1822
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
23+
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
24+
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
25+
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
1926
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
2027
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
2128
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
@@ -48,6 +55,10 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
4855
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
4956
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
5057
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
58+
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
59+
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
60+
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
61+
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
5162
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5263
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
5364
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=

0 commit comments

Comments
 (0)