|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/DATA-DOG/go-sqlmock" |
| 8 | + "github.com/prometheus/client_golang/prometheus" |
| 9 | + dto "github.com/prometheus/client_model/go" |
| 10 | + "github.com/smartystreets/goconvey/convey" |
| 11 | +) |
| 12 | + |
| 13 | +func TestPgPostmasterCollector(t *testing.T) { |
| 14 | + db, mock, err := sqlmock.New() |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("Error opening a stub db connection: %s", err) |
| 17 | + } |
| 18 | + defer db.Close() |
| 19 | + |
| 20 | + mock.ExpectQuery(sanitizeQuery(pgPostmasterQuery)).WillReturnRows(sqlmock.NewRows([]string{"pg_postmaster_start_time"}). |
| 21 | + AddRow(1685739904)) |
| 22 | + |
| 23 | + ch := make(chan prometheus.Metric) |
| 24 | + go func() { |
| 25 | + defer close(ch) |
| 26 | + c := PGPostmasterCollector{} |
| 27 | + |
| 28 | + if err := c.Update(context.Background(), db, ch); err != nil { |
| 29 | + t.Errorf("Error calling PGPostmasterCollector.Update: %s", err) |
| 30 | + } |
| 31 | + }() |
| 32 | + |
| 33 | + expected := []MetricResult{ |
| 34 | + {labels: labelMap{}, value: 1685739904, metricType: dto.MetricType_GAUGE}, |
| 35 | + } |
| 36 | + convey.Convey("Metrics comparison", t, func() { |
| 37 | + for _, expect := range expected { |
| 38 | + m := readMetric(<-ch) |
| 39 | + convey.So(expect, convey.ShouldResemble, m) |
| 40 | + } |
| 41 | + }) |
| 42 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 43 | + t.Errorf("there were unfulfilled exceptions: %s", err) |
| 44 | + } |
| 45 | +} |
0 commit comments