pangolin_exporter
186 строк · 6.4 Кб
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"fmt"
18"testing"
19
20"github.com/DATA-DOG/go-sqlmock"
21"github.com/prometheus/client_golang/prometheus"
22dto "github.com/prometheus/client_model/go"
23"github.com/smartystreets/goconvey/convey"
24)
25
26var queryWithFlushedLSN = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "(flushed_lsn - '0/0') % (2^52)::bigint as flushed_lsn,\n")
27var queryWithNoFlushedLSN = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "")
28
29func TestPGStatWalReceiverCollectorWithFlushedLSN(t *testing.T) {
30db, mock, err := sqlmock.New()
31if err != nil {
32t.Fatalf("Error opening a stub db connection: %s", err)
33}
34defer db.Close()
35
36inst := &instance{db: db}
37infoSchemaColumns := []string{
38"column_name",
39}
40
41infoSchemaRows := sqlmock.NewRows(infoSchemaColumns).
42AddRow(
43"flushed_lsn",
44)
45
46mock.ExpectQuery(sanitizeQuery(pgStatWalColumnQuery)).WillReturnRows(infoSchemaRows)
47
48columns := []string{
49"upstream_host",
50"slot_name",
51"status",
52"receive_start_lsn",
53"receive_start_tli",
54"flushed_lsn",
55"received_tli",
56"last_msg_send_time",
57"last_msg_receipt_time",
58"latest_end_lsn",
59"latest_end_time",
60"upstream_node",
61}
62rows := sqlmock.NewRows(columns).
63AddRow(
64"foo",
65"bar",
66"stopping",
67int64(1200668684563608),
681687321285,
69int64(1200668684563609),
701687321280,
711687321275,
721687321276,
73int64(1200668684563610),
741687321277,
755,
76)
77
78mock.ExpectQuery(sanitizeQuery(queryWithFlushedLSN)).WillReturnRows(rows)
79
80ch := make(chan prometheus.Metric)
81go func() {
82defer close(ch)
83c := PGStatWalReceiverCollector{}
84
85if err := c.Update(context.Background(), inst, ch); err != nil {
86t.Errorf("Error calling PgStatWalReceiverCollector.Update: %s", err)
87}
88}()
89expected := []MetricResult{
90{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "stopping"}, value: 1200668684563608, metricType: dto.MetricType_COUNTER},
91{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "stopping"}, value: 1687321285, metricType: dto.MetricType_GAUGE},
92{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "stopping"}, value: 1200668684563609, metricType: dto.MetricType_COUNTER},
93{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "stopping"}, value: 1687321280, metricType: dto.MetricType_GAUGE},
94{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "stopping"}, value: 1687321275, metricType: dto.MetricType_COUNTER},
95{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "stopping"}, value: 1687321276, metricType: dto.MetricType_COUNTER},
96{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "stopping"}, value: 1200668684563610, metricType: dto.MetricType_COUNTER},
97{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "stopping"}, value: 1687321277, metricType: dto.MetricType_COUNTER},
98{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "stopping"}, value: 5, metricType: dto.MetricType_GAUGE},
99}
100convey.Convey("Metrics comparison", t, func() {
101for _, expect := range expected {
102m := readMetric(<-ch)
103convey.So(expect, convey.ShouldResemble, m)
104}
105})
106if err := mock.ExpectationsWereMet(); err != nil {
107t.Errorf("there were unfulfilled exceptions: %s", err)
108}
109
110}
111
112func TestPGStatWalReceiverCollectorWithNoFlushedLSN(t *testing.T) {
113db, mock, err := sqlmock.New()
114if err != nil {
115t.Fatalf("Error opening a stub db connection: %s", err)
116}
117defer db.Close()
118
119inst := &instance{db: db}
120infoSchemaColumns := []string{
121"column_name",
122}
123
124infoSchemaRows := sqlmock.NewRows(infoSchemaColumns)
125
126mock.ExpectQuery(sanitizeQuery(pgStatWalColumnQuery)).WillReturnRows(infoSchemaRows)
127
128columns := []string{
129"upstream_host",
130"slot_name",
131"status",
132"receive_start_lsn",
133"receive_start_tli",
134"received_tli",
135"last_msg_send_time",
136"last_msg_receipt_time",
137"latest_end_lsn",
138"latest_end_time",
139"upstream_node",
140}
141rows := sqlmock.NewRows(columns).
142AddRow(
143"foo",
144"bar",
145"starting",
146int64(1200668684563608),
1471687321285,
1481687321280,
1491687321275,
1501687321276,
151int64(1200668684563610),
1521687321277,
1535,
154)
155mock.ExpectQuery(sanitizeQuery(queryWithNoFlushedLSN)).WillReturnRows(rows)
156
157ch := make(chan prometheus.Metric)
158go func() {
159defer close(ch)
160c := PGStatWalReceiverCollector{}
161
162if err := c.Update(context.Background(), inst, ch); err != nil {
163t.Errorf("Error calling PgStatWalReceiverCollector.Update: %s", err)
164}
165}()
166expected := []MetricResult{
167{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "starting"}, value: 1200668684563608, metricType: dto.MetricType_COUNTER},
168{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "starting"}, value: 1687321285, metricType: dto.MetricType_GAUGE},
169{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "starting"}, value: 1687321280, metricType: dto.MetricType_GAUGE},
170{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "starting"}, value: 1687321275, metricType: dto.MetricType_COUNTER},
171{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "starting"}, value: 1687321276, metricType: dto.MetricType_COUNTER},
172{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "starting"}, value: 1200668684563610, metricType: dto.MetricType_COUNTER},
173{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "starting"}, value: 1687321277, metricType: dto.MetricType_COUNTER},
174{labels: labelMap{"upstream_host": "foo", "slot_name": "bar", "status": "starting"}, value: 5, metricType: dto.MetricType_GAUGE},
175}
176convey.Convey("Metrics comparison", t, func() {
177for _, expect := range expected {
178m := readMetric(<-ch)
179convey.So(expect, convey.ShouldResemble, m)
180}
181})
182if err := mock.ExpectationsWereMet(); err != nil {
183t.Errorf("there were unfulfilled exceptions: %s", err)
184}
185
186}
187