|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/prometheus/client_golang/prometheus" |
| 7 | + dto "github.com/prometheus/client_model/go" |
| 8 | + "github.com/smartystreets/goconvey/convey" |
| 9 | + "gopkg.in/DATA-DOG/go-sqlmock.v1" |
| 10 | +) |
| 11 | + |
| 12 | +func TestScrapeSlaveStatus(t *testing.T) { |
| 13 | + db, mock, err := sqlmock.New() |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("error opening a stub database connection: %s", err) |
| 16 | + } |
| 17 | + defer db.Close() |
| 18 | + |
| 19 | + columns := []string{"Master_Host", "Read_Master_Log_Pos", "Slave_IO_Running", "Slave_SQL_Running", "Seconds_Behind_Master"} |
| 20 | + rows := sqlmock.NewRows(columns). |
| 21 | + AddRow("127.0.0.1", "1", "Connecting", "Yes", "2") |
| 22 | + mock.ExpectQuery(sanitizeQuery(slaveStatusQuery)).WillReturnRows(rows) |
| 23 | + |
| 24 | + ch := make(chan prometheus.Metric) |
| 25 | + go func() { |
| 26 | + if err = ScrapeSlaveStatus(db, ch); err != nil { |
| 27 | + t.Errorf("error calling function on test: %s", err) |
| 28 | + } |
| 29 | + close(ch) |
| 30 | + }() |
| 31 | + |
| 32 | + counterExpected := []MetricResult{ |
| 33 | + {labels: labelMap{}, value: 1, metricType: dto.MetricType_UNTYPED}, |
| 34 | + {labels: labelMap{}, value: 0, metricType: dto.MetricType_UNTYPED}, |
| 35 | + {labels: labelMap{}, value: 1, metricType: dto.MetricType_UNTYPED}, |
| 36 | + {labels: labelMap{}, value: 2, metricType: dto.MetricType_UNTYPED}, |
| 37 | + } |
| 38 | + convey.Convey("Metrics comparison", t, func() { |
| 39 | + for _, expect := range counterExpected { |
| 40 | + got := readMetric(<-ch) |
| 41 | + convey.So(got, convey.ShouldResemble, expect) |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + // Ensure all SQL queries were executed |
| 46 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 47 | + t.Errorf("there were unfulfilled expections: %s", err) |
| 48 | + } |
| 49 | +} |
0 commit comments