|
| 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 TestPgReplicationSlotCollectorActive(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 | + columns := []string{"slot_name", "current_wal_lsn", "confirmed_flush_lsn", "active"} |
| 21 | + rows := sqlmock.NewRows(columns). |
| 22 | + AddRow("test_slot", 5, 3, true) |
| 23 | + mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows) |
| 24 | + |
| 25 | + ch := make(chan prometheus.Metric) |
| 26 | + go func() { |
| 27 | + defer close(ch) |
| 28 | + c := PGReplicationSlotCollector{} |
| 29 | + |
| 30 | + if err := c.Update(context.Background(), db, ch); err != nil { |
| 31 | + t.Errorf("Error calling PGPostmasterCollector.Update: %s", err) |
| 32 | + } |
| 33 | + }() |
| 34 | + |
| 35 | + expected := []MetricResult{ |
| 36 | + {labels: labelMap{"slot_name": "test_slot"}, value: 5, metricType: dto.MetricType_GAUGE}, |
| 37 | + {labels: labelMap{"slot_name": "test_slot"}, value: 3, metricType: dto.MetricType_GAUGE}, |
| 38 | + {labels: labelMap{"slot_name": "test_slot"}, value: 1, metricType: dto.MetricType_GAUGE}, |
| 39 | + } |
| 40 | + |
| 41 | + convey.Convey("Metrics comparison", t, func() { |
| 42 | + for _, expect := range expected { |
| 43 | + m := readMetric(<-ch) |
| 44 | + convey.So(expect, convey.ShouldResemble, m) |
| 45 | + } |
| 46 | + }) |
| 47 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 48 | + t.Errorf("there were unfulfilled exceptions: %s", err) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestPgReplicationSlotCollectorInActive(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 | + columns := []string{"slot_name", "current_wal_lsn", "confirmed_flush_lsn", "active"} |
| 60 | + rows := sqlmock.NewRows(columns). |
| 61 | + AddRow("test_slot", 6, 12, false) |
| 62 | + mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows) |
| 63 | + |
| 64 | + ch := make(chan prometheus.Metric) |
| 65 | + go func() { |
| 66 | + defer close(ch) |
| 67 | + c := PGReplicationSlotCollector{} |
| 68 | + |
| 69 | + if err := c.Update(context.Background(), db, ch); err != nil { |
| 70 | + t.Errorf("Error calling PGPostmasterCollector.Update: %s", err) |
| 71 | + } |
| 72 | + }() |
| 73 | + |
| 74 | + expected := []MetricResult{ |
| 75 | + {labels: labelMap{"slot_name": "test_slot"}, value: 6, metricType: dto.MetricType_GAUGE}, |
| 76 | + {labels: labelMap{"slot_name": "test_slot"}, value: 0, metricType: dto.MetricType_GAUGE}, |
| 77 | + } |
| 78 | + |
| 79 | + convey.Convey("Metrics comparison", t, func() { |
| 80 | + for _, expect := range expected { |
| 81 | + m := readMetric(<-ch) |
| 82 | + convey.So(expect, convey.ShouldResemble, m) |
| 83 | + } |
| 84 | + }) |
| 85 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 86 | + t.Errorf("there were unfulfilled exceptions: %s", err) |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments