|
| 1 | +// Copyright 2025 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 | + |
| 19 | + "github.com/DATA-DOG/go-sqlmock" |
| 20 | + "github.com/prometheus/client_golang/prometheus" |
| 21 | + dto "github.com/prometheus/client_model/go" |
| 22 | + "github.com/smartystreets/goconvey/convey" |
| 23 | +) |
| 24 | + |
| 25 | +func TestPGStatProgressVacuumCollector(t *testing.T) { |
| 26 | + db, mock, err := sqlmock.New() |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("Error opening a stub db connection: %s", err) |
| 29 | + } |
| 30 | + defer db.Close() |
| 31 | + |
| 32 | + inst := &instance{db: db} |
| 33 | + |
| 34 | + columns := []string{ |
| 35 | + "datname", "relname", "phase", "heap_blks_total", "heap_blks_scanned", |
| 36 | + "heap_blks_vacuumed", "index_vacuum_count", "max_dead_tuples", "num_dead_tuples", |
| 37 | + } |
| 38 | + |
| 39 | + rows := sqlmock.NewRows(columns).AddRow( |
| 40 | + "postgres", "a_table", 3, 3000, 400, 200, 2, 500, 123) |
| 41 | + |
| 42 | + mock.ExpectQuery(sanitizeQuery(statProgressVacuumQuery)).WillReturnRows(rows) |
| 43 | + |
| 44 | + ch := make(chan prometheus.Metric) |
| 45 | + go func() { |
| 46 | + defer close(ch) |
| 47 | + c := PGStatProgressVacuumCollector{} |
| 48 | + |
| 49 | + if err := c.Update(context.Background(), inst, ch); err != nil { |
| 50 | + t.Errorf("Error calling PGStatProgressVacuumCollector.Update; %+v", err) |
| 51 | + } |
| 52 | + }() |
| 53 | + |
| 54 | + expected := []MetricResult{ |
| 55 | + {labels: labelMap{"datname": "postgres", "relname": "a_table", "phase": "initializing"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 56 | + {labels: labelMap{"datname": "postgres", "relname": "a_table", "phase": "scanning heap"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 57 | + {labels: labelMap{"datname": "postgres", "relname": "a_table", "phase": "vacuuming indexes"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 58 | + {labels: labelMap{"datname": "postgres", "relname": "a_table", "phase": "vacuuming heap"}, metricType: dto.MetricType_GAUGE, value: 1}, |
| 59 | + {labels: labelMap{"datname": "postgres", "relname": "a_table", "phase": "cleaning up indexes"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 60 | + {labels: labelMap{"datname": "postgres", "relname": "a_table", "phase": "truncating heap"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 61 | + {labels: labelMap{"datname": "postgres", "relname": "a_table", "phase": "performing final cleanup"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 62 | + {labels: labelMap{"datname": "postgres", "relname": "a_table"}, metricType: dto.MetricType_GAUGE, value: 3000}, |
| 63 | + {labels: labelMap{"datname": "postgres", "relname": "a_table"}, metricType: dto.MetricType_GAUGE, value: 400}, |
| 64 | + {labels: labelMap{"datname": "postgres", "relname": "a_table"}, metricType: dto.MetricType_GAUGE, value: 200}, |
| 65 | + {labels: labelMap{"datname": "postgres", "relname": "a_table"}, metricType: dto.MetricType_GAUGE, value: 2}, |
| 66 | + {labels: labelMap{"datname": "postgres", "relname": "a_table"}, metricType: dto.MetricType_GAUGE, value: 500}, |
| 67 | + {labels: labelMap{"datname": "postgres", "relname": "a_table"}, metricType: dto.MetricType_GAUGE, value: 123}, |
| 68 | + } |
| 69 | + |
| 70 | + convey.Convey("Metrics comparison", t, func() { |
| 71 | + for _, expect := range expected { |
| 72 | + m := readMetric(<-ch) |
| 73 | + convey.So(m, convey.ShouldResemble, expect) |
| 74 | + } |
| 75 | + }) |
| 76 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 77 | + t.Errorf("There were unfulfilled exceptions: %+v", err) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestPGStatProgressVacuumCollectorNullValues(t *testing.T) { |
| 82 | + db, mock, err := sqlmock.New() |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("Error opening a stub db connection: %s", err) |
| 85 | + } |
| 86 | + defer db.Close() |
| 87 | + |
| 88 | + inst := &instance{db: db} |
| 89 | + |
| 90 | + columns := []string{ |
| 91 | + "datname", "relname", "phase", "heap_blks_total", "heap_blks_scanned", |
| 92 | + "heap_blks_vacuumed", "index_vacuum_count", "max_dead_tuples", "num_dead_tuples", |
| 93 | + } |
| 94 | + |
| 95 | + rows := sqlmock.NewRows(columns).AddRow( |
| 96 | + "postgres", nil, nil, nil, nil, nil, nil, nil, nil) |
| 97 | + |
| 98 | + mock.ExpectQuery(sanitizeQuery(statProgressVacuumQuery)).WillReturnRows(rows) |
| 99 | + |
| 100 | + ch := make(chan prometheus.Metric) |
| 101 | + go func() { |
| 102 | + defer close(ch) |
| 103 | + c := PGStatProgressVacuumCollector{} |
| 104 | + |
| 105 | + if err := c.Update(context.Background(), inst, ch); err != nil { |
| 106 | + t.Errorf("Error calling PGStatProgressVacuumCollector.Update; %+v", err) |
| 107 | + } |
| 108 | + }() |
| 109 | + |
| 110 | + expected := []MetricResult{ |
| 111 | + {labels: labelMap{"datname": "postgres", "relname": "unknown", "phase": "initializing"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 112 | + {labels: labelMap{"datname": "postgres", "relname": "unknown", "phase": "scanning heap"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 113 | + {labels: labelMap{"datname": "postgres", "relname": "unknown", "phase": "vacuuming indexes"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 114 | + {labels: labelMap{"datname": "postgres", "relname": "unknown", "phase": "vacuuming heap"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 115 | + {labels: labelMap{"datname": "postgres", "relname": "unknown", "phase": "cleaning up indexes"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 116 | + {labels: labelMap{"datname": "postgres", "relname": "unknown", "phase": "truncating heap"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 117 | + {labels: labelMap{"datname": "postgres", "relname": "unknown", "phase": "performing final cleanup"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 118 | + {labels: labelMap{"datname": "postgres", "relname": "unknown"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 119 | + {labels: labelMap{"datname": "postgres", "relname": "unknown"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 120 | + {labels: labelMap{"datname": "postgres", "relname": "unknown"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 121 | + {labels: labelMap{"datname": "postgres", "relname": "unknown"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 122 | + {labels: labelMap{"datname": "postgres", "relname": "unknown"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 123 | + {labels: labelMap{"datname": "postgres", "relname": "unknown"}, metricType: dto.MetricType_GAUGE, value: 0}, |
| 124 | + } |
| 125 | + |
| 126 | + convey.Convey("Metrics comparison", t, func() { |
| 127 | + for _, expect := range expected { |
| 128 | + m := readMetric(<-ch) |
| 129 | + convey.So(expect, convey.ShouldResemble, m) |
| 130 | + } |
| 131 | + }) |
| 132 | + if err := mock.ExpectationsWereMet(); err != nil { |
| 133 | + t.Errorf("There were unfulfilled exceptions: %+v", err) |
| 134 | + } |
| 135 | +} |
0 commit comments