|
| 1 | +// Copyright 2023 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 | +package collector |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "testing" |
| 18 | + "time" |
| 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 | + |
| 26 | +func TestPGStatWALCollector(t *testing.T) { |
| 27 | + db, mock, err := sqlmock.New() |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("Error opening a stub db connection: %s", err) |
| 30 | + } |
| 31 | + defer db.Close() |
| 32 | + |
| 33 | + inst := &instance{db: db} |
| 34 | + |
| 35 | + columns := []string{ |
| 36 | + "wal_records", // bigint |
| 37 | + "wal_fpi", // bigint |
| 38 | + "wal_bytes", // numeric |
| 39 | + "wal_buffers_full", // bigint |
| 40 | + "wal_write", // bigint |
| 41 | + "wal_sync", // bigint |
| 42 | + "wal_write_time", // double precision |
| 43 | + "wal_sync_time", // double precision |
| 44 | + "stats_reset", // timestamp with time zone |
| 45 | + } |
| 46 | + |
| 47 | + srT, err := time.Parse("2006-01-02 15:04:05.00000-07", "2023-05-25 17:10:42.81132-07") |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("Error parsing time: %s", err) |
| 50 | + } |
| 51 | + |
| 52 | + rows := sqlmock.NewRows(columns). |
| 53 | + AddRow(354, 4945, 289097744, 1242257, int64(3275602074), 89320867, 450.123439, 1234.5678, srT) |
| 54 | + mock.ExpectQuery(sanitizeQuery(statWALQuery(columns))).WillReturnRows(rows) |
| 55 | + |
| 56 | + ch := make(chan prometheus.Metric) |
| 57 | + go func() { |
| 58 | + defer close(ch) |
| 59 | + c := PGStatWALCollector{} |
| 60 | + |
| 61 | + if err := c.Update(context.Background(), inst, ch); err != nil { |
| 62 | + t.Errorf("Error calling PGStatWALCollector.Update: %s", err) |
| 63 | + } |
| 64 | + }() |
| 65 | + |
| 66 | + expected := []MetricResult{ |
| 67 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 354}, |
| 68 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 4945}, |
| 69 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 289097744}, |
| 70 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 1242257}, |
| 71 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 3275602074}, |
| 72 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 89320867}, |
| 73 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 450.123439}, |
| 74 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 1234.5678}, |
| 75 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 1685059842}, |
| 76 | + } |
| 77 | + |
| 78 | + convey.Convey("Metrics comparison", t, func() { |
| 79 | + for _, expect := range expected { |
| 80 | + m := readMetric(<-ch) |
| 81 | + convey.So(expect, convey.ShouldResemble, m) |
| 82 | + } |
| 83 | + }) |
| 84 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 85 | + t.Errorf("there were unfulfilled exceptions: %s", err) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestPGStatWALCollectorNullValues(t *testing.T) { |
| 90 | + db, mock, err := sqlmock.New() |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("Error opening a stub db connection: %s", err) |
| 93 | + } |
| 94 | + defer db.Close() |
| 95 | + |
| 96 | + inst := &instance{db: db} |
| 97 | + columns := []string{ |
| 98 | + "wal_records", // bigint |
| 99 | + "wal_fpi", // bigint |
| 100 | + "wal_bytes", // numeric |
| 101 | + "wal_buffers_full", // bigint |
| 102 | + "wal_write", // bigint |
| 103 | + "wal_sync", // bigint |
| 104 | + "wal_write_time", // double precision |
| 105 | + "wal_sync_time", // double precision |
| 106 | + "stats_reset", // timestamp with time zone |
| 107 | + } |
| 108 | + |
| 109 | + rows := sqlmock.NewRows(columns). |
| 110 | + AddRow(nil, nil, nil, nil, nil, nil, nil, nil, nil) |
| 111 | + mock.ExpectQuery(sanitizeQuery(statWALQuery(columns))).WillReturnRows(rows) |
| 112 | + |
| 113 | + ch := make(chan prometheus.Metric) |
| 114 | + go func() { |
| 115 | + defer close(ch) |
| 116 | + c := PGStatWALCollector{} |
| 117 | + |
| 118 | + if err := c.Update(context.Background(), inst, ch); err != nil { |
| 119 | + t.Errorf("Error calling PGStatWALCollector.Update: %s", err) |
| 120 | + } |
| 121 | + }() |
| 122 | + |
| 123 | + expected := []MetricResult{ |
| 124 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0}, |
| 125 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0}, |
| 126 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0}, |
| 127 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0}, |
| 128 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0}, |
| 129 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0}, |
| 130 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0}, |
| 131 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0}, |
| 132 | + {labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0}, |
| 133 | + } |
| 134 | + |
| 135 | + convey.Convey("Metrics comparison", t, func() { |
| 136 | + for _, expect := range expected { |
| 137 | + m := readMetric(<-ch) |
| 138 | + convey.So(expect, convey.ShouldResemble, m) |
| 139 | + } |
| 140 | + }) |
| 141 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 142 | + t.Errorf("there were unfulfilled exceptions: %s", err) |
| 143 | + } |
| 144 | +} |
0 commit comments