1// Copyright 2018 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 language governing permissions and 12// limitations under the License. 13 14package collector 15 16import ( 17 "context" 18 "testing" 19 20 "github.com/DATA-DOG/go-sqlmock" 21 "github.com/prometheus/client_golang/prometheus" 22 dto "github.com/prometheus/client_model/go" 23 "github.com/smartystreets/goconvey/convey" 24) 25 26func TestScrapePerfIndexIOWaits(t *testing.T) { 27 db, mock, err := sqlmock.New() 28 if err != nil { 29 t.Fatalf("error opening a stub database connection: %s", err) 30 } 31 defer db.Close() 32 33 columns := []string{"OBJECT_SCHEMA", "OBJECT_NAME", "INDEX_NAME", "COUNT_FETCH", "COUNT_INSERT", "COUNT_UPDATE", "COUNT_DELETE", "SUM_TIMER_FETCH", "SUM_TIMER_INSERT", "SUM_TIMER_UPDATE", "SUM_TIMER_DELETE"} 34 rows := sqlmock.NewRows(columns). 35 // Note, timers are in picoseconds. 36 AddRow("database", "table", "index", "10", "11", "12", "13", "14000000000000", "15000000000000", "16000000000000", "17000000000000"). 37 AddRow("database", "table", "NONE", "20", "21", "22", "23", "24000000000000", "25000000000000", "26000000000000", "27000000000000") 38 mock.ExpectQuery(sanitizeQuery(perfIndexIOWaitsQuery)).WillReturnRows(rows) 39 40 ch := make(chan prometheus.Metric) 41 go func() { 42 if err = (ScrapePerfIndexIOWaits{}).Scrape(context.Background(), db, ch); err != nil { 43 t.Errorf("error calling function on test: %s", err) 44 } 45 close(ch) 46 }() 47 48 metricExpected := []MetricResult{ 49 {labels: labelMap{"schema": "database", "name": "table", "index": "index", "operation": "fetch"}, value: 10, metricType: dto.MetricType_COUNTER}, 50 {labels: labelMap{"schema": "database", "name": "table", "index": "index", "operation": "update"}, value: 12, metricType: dto.MetricType_COUNTER}, 51 {labels: labelMap{"schema": "database", "name": "table", "index": "index", "operation": "delete"}, value: 13, metricType: dto.MetricType_COUNTER}, 52 {labels: labelMap{"schema": "database", "name": "table", "index": "index", "operation": "fetch"}, value: 14, metricType: dto.MetricType_COUNTER}, 53 {labels: labelMap{"schema": "database", "name": "table", "index": "index", "operation": "update"}, value: 16, metricType: dto.MetricType_COUNTER}, 54 {labels: labelMap{"schema": "database", "name": "table", "index": "index", "operation": "delete"}, value: 17, metricType: dto.MetricType_COUNTER}, 55 {labels: labelMap{"schema": "database", "name": "table", "index": "NONE", "operation": "fetch"}, value: 20, metricType: dto.MetricType_COUNTER}, 56 {labels: labelMap{"schema": "database", "name": "table", "index": "NONE", "operation": "insert"}, value: 21, metricType: dto.MetricType_COUNTER}, 57 {labels: labelMap{"schema": "database", "name": "table", "index": "NONE", "operation": "update"}, value: 22, metricType: dto.MetricType_COUNTER}, 58 {labels: labelMap{"schema": "database", "name": "table", "index": "NONE", "operation": "delete"}, value: 23, metricType: dto.MetricType_COUNTER}, 59 {labels: labelMap{"schema": "database", "name": "table", "index": "NONE", "operation": "fetch"}, value: 24, metricType: dto.MetricType_COUNTER}, 60 {labels: labelMap{"schema": "database", "name": "table", "index": "NONE", "operation": "insert"}, value: 25, metricType: dto.MetricType_COUNTER}, 61 {labels: labelMap{"schema": "database", "name": "table", "index": "NONE", "operation": "update"}, value: 26, metricType: dto.MetricType_COUNTER}, 62 {labels: labelMap{"schema": "database", "name": "table", "index": "NONE", "operation": "delete"}, value: 27, metricType: dto.MetricType_COUNTER}, 63 } 64 convey.Convey("Metrics comparison", t, func() { 65 for _, expect := range metricExpected { 66 got := readMetric(<-ch) 67 convey.So(got, convey.ShouldResemble, expect) 68 } 69 }) 70 71 // Ensure all SQL queries were executed 72 if err := mock.ExpectationsWereMet(); err != nil { 73 t.Errorf("there were unfulfilled exceptions: %s", err) 74 } 75} 76