pangolin_exporter
186 строк · 6.0 Кб
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.
13package collector
14
15import (
16"context"
17"testing"
18
19"github.com/DATA-DOG/go-sqlmock"
20"github.com/prometheus/client_golang/prometheus"
21dto "github.com/prometheus/client_model/go"
22"github.com/smartystreets/goconvey/convey"
23)
24
25func TestPgReplicationSlotCollectorActive(t *testing.T) {
26db, mock, err := sqlmock.New()
27if err != nil {
28t.Fatalf("Error opening a stub db connection: %s", err)
29}
30defer db.Close()
31
32inst := &instance{db: db}
33
34columns := []string{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
35rows := sqlmock.NewRows(columns).
36AddRow("test_slot", "physical", 5, 3, true)
37mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
38
39ch := make(chan prometheus.Metric)
40go func() {
41defer close(ch)
42c := PGReplicationSlotCollector{}
43
44if err := c.Update(context.Background(), inst, ch); err != nil {
45t.Errorf("Error calling PGPostmasterCollector.Update: %s", err)
46}
47}()
48
49expected := []MetricResult{
50{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 5, metricType: dto.MetricType_GAUGE},
51{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 3, metricType: dto.MetricType_GAUGE},
52{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 1, metricType: dto.MetricType_GAUGE},
53}
54
55convey.Convey("Metrics comparison", t, func() {
56for _, expect := range expected {
57m := readMetric(<-ch)
58convey.So(expect, convey.ShouldResemble, m)
59}
60})
61if err := mock.ExpectationsWereMet(); err != nil {
62t.Errorf("there were unfulfilled exceptions: %s", err)
63}
64}
65
66func TestPgReplicationSlotCollectorInActive(t *testing.T) {
67db, mock, err := sqlmock.New()
68if err != nil {
69t.Fatalf("Error opening a stub db connection: %s", err)
70}
71defer db.Close()
72
73inst := &instance{db: db}
74
75columns := []string{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
76rows := sqlmock.NewRows(columns).
77AddRow("test_slot", "physical", 6, 12, false)
78mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
79
80ch := make(chan prometheus.Metric)
81go func() {
82defer close(ch)
83c := PGReplicationSlotCollector{}
84
85if err := c.Update(context.Background(), inst, ch); err != nil {
86t.Errorf("Error calling PGReplicationSlotCollector.Update: %s", err)
87}
88}()
89
90expected := []MetricResult{
91{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 6, metricType: dto.MetricType_GAUGE},
92{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 0, metricType: dto.MetricType_GAUGE},
93}
94
95convey.Convey("Metrics comparison", t, func() {
96for _, expect := range expected {
97m := readMetric(<-ch)
98convey.So(expect, convey.ShouldResemble, m)
99}
100})
101if err := mock.ExpectationsWereMet(); err != nil {
102t.Errorf("there were unfulfilled exceptions: %s", err)
103}
104
105}
106
107func TestPgReplicationSlotCollectorActiveNil(t *testing.T) {
108db, mock, err := sqlmock.New()
109if err != nil {
110t.Fatalf("Error opening a stub db connection: %s", err)
111}
112defer db.Close()
113
114inst := &instance{db: db}
115
116columns := []string{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
117rows := sqlmock.NewRows(columns).
118AddRow("test_slot", "physical", 6, 12, nil)
119mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
120
121ch := make(chan prometheus.Metric)
122go func() {
123defer close(ch)
124c := PGReplicationSlotCollector{}
125
126if err := c.Update(context.Background(), inst, ch); err != nil {
127t.Errorf("Error calling PGReplicationSlotCollector.Update: %s", err)
128}
129}()
130
131expected := []MetricResult{
132{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 6, metricType: dto.MetricType_GAUGE},
133{labels: labelMap{"slot_name": "test_slot", "slot_type": "physical"}, value: 0, metricType: dto.MetricType_GAUGE},
134}
135
136convey.Convey("Metrics comparison", t, func() {
137for _, expect := range expected {
138m := readMetric(<-ch)
139convey.So(expect, convey.ShouldResemble, m)
140}
141})
142if err := mock.ExpectationsWereMet(); err != nil {
143t.Errorf("there were unfulfilled exceptions: %s", err)
144}
145}
146
147func TestPgReplicationSlotCollectorTestNilValues(t *testing.T) {
148db, mock, err := sqlmock.New()
149if err != nil {
150t.Fatalf("Error opening a stub db connection: %s", err)
151}
152defer db.Close()
153
154inst := &instance{db: db}
155
156columns := []string{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
157rows := sqlmock.NewRows(columns).
158AddRow(nil, nil, nil, nil, true)
159mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
160
161ch := make(chan prometheus.Metric)
162go func() {
163defer close(ch)
164c := PGReplicationSlotCollector{}
165
166if err := c.Update(context.Background(), inst, ch); err != nil {
167t.Errorf("Error calling PGReplicationSlotCollector.Update: %s", err)
168}
169}()
170
171expected := []MetricResult{
172{labels: labelMap{"slot_name": "unknown", "slot_type": "unknown"}, value: 0, metricType: dto.MetricType_GAUGE},
173{labels: labelMap{"slot_name": "unknown", "slot_type": "unknown"}, value: 0, metricType: dto.MetricType_GAUGE},
174{labels: labelMap{"slot_name": "unknown", "slot_type": "unknown"}, value: 1, metricType: dto.MetricType_GAUGE},
175}
176
177convey.Convey("Metrics comparison", t, func() {
178for _, expect := range expected {
179m := readMetric(<-ch)
180convey.So(expect, convey.ShouldResemble, m)
181}
182})
183if err := mock.ExpectationsWereMet(); err != nil {
184t.Errorf("there were unfulfilled exceptions: %s", err)
185}
186}
187