pangolin_exporter

Форк
0
/
pg_stat_walreceiver_test.go 
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.
13
package collector
14

15
import (
16
	"context"
17
	"fmt"
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

26
var queryWithFlushedLSN = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "(flushed_lsn - '0/0') % (2^52)::bigint as flushed_lsn,\n")
27
var queryWithNoFlushedLSN = fmt.Sprintf(pgStatWalReceiverQueryTemplate, "")
28

29
func TestPGStatWalReceiverCollectorWithFlushedLSN(t *testing.T) {
30
	db, mock, err := sqlmock.New()
31
	if err != nil {
32
		t.Fatalf("Error opening a stub db connection: %s", err)
33
	}
34
	defer db.Close()
35

36
	inst := &instance{db: db}
37
	infoSchemaColumns := []string{
38
		"column_name",
39
	}
40

41
	infoSchemaRows := sqlmock.NewRows(infoSchemaColumns).
42
		AddRow(
43
			"flushed_lsn",
44
		)
45

46
	mock.ExpectQuery(sanitizeQuery(pgStatWalColumnQuery)).WillReturnRows(infoSchemaRows)
47

48
	columns := []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
	}
62
	rows := sqlmock.NewRows(columns).
63
		AddRow(
64
			"foo",
65
			"bar",
66
			"stopping",
67
			int64(1200668684563608),
68
			1687321285,
69
			int64(1200668684563609),
70
			1687321280,
71
			1687321275,
72
			1687321276,
73
			int64(1200668684563610),
74
			1687321277,
75
			5,
76
		)
77

78
	mock.ExpectQuery(sanitizeQuery(queryWithFlushedLSN)).WillReturnRows(rows)
79

80
	ch := make(chan prometheus.Metric)
81
	go func() {
82
		defer close(ch)
83
		c := PGStatWalReceiverCollector{}
84

85
		if err := c.Update(context.Background(), inst, ch); err != nil {
86
			t.Errorf("Error calling PgStatWalReceiverCollector.Update: %s", err)
87
		}
88
	}()
89
	expected := []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
	}
100
	convey.Convey("Metrics comparison", t, func() {
101
		for _, expect := range expected {
102
			m := readMetric(<-ch)
103
			convey.So(expect, convey.ShouldResemble, m)
104
		}
105
	})
106
	if err := mock.ExpectationsWereMet(); err != nil {
107
		t.Errorf("there were unfulfilled exceptions: %s", err)
108
	}
109

110
}
111

112
func TestPGStatWalReceiverCollectorWithNoFlushedLSN(t *testing.T) {
113
	db, mock, err := sqlmock.New()
114
	if err != nil {
115
		t.Fatalf("Error opening a stub db connection: %s", err)
116
	}
117
	defer db.Close()
118

119
	inst := &instance{db: db}
120
	infoSchemaColumns := []string{
121
		"column_name",
122
	}
123

124
	infoSchemaRows := sqlmock.NewRows(infoSchemaColumns)
125

126
	mock.ExpectQuery(sanitizeQuery(pgStatWalColumnQuery)).WillReturnRows(infoSchemaRows)
127

128
	columns := []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
	}
141
	rows := sqlmock.NewRows(columns).
142
		AddRow(
143
			"foo",
144
			"bar",
145
			"starting",
146
			int64(1200668684563608),
147
			1687321285,
148
			1687321280,
149
			1687321275,
150
			1687321276,
151
			int64(1200668684563610),
152
			1687321277,
153
			5,
154
		)
155
	mock.ExpectQuery(sanitizeQuery(queryWithNoFlushedLSN)).WillReturnRows(rows)
156

157
	ch := make(chan prometheus.Metric)
158
	go func() {
159
		defer close(ch)
160
		c := PGStatWalReceiverCollector{}
161

162
		if err := c.Update(context.Background(), inst, ch); err != nil {
163
			t.Errorf("Error calling PgStatWalReceiverCollector.Update: %s", err)
164
		}
165
	}()
166
	expected := []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
	}
176
	convey.Convey("Metrics comparison", t, func() {
177
		for _, expect := range expected {
178
			m := readMetric(<-ch)
179
			convey.So(expect, convey.ShouldResemble, m)
180
		}
181
	})
182
	if err := mock.ExpectationsWereMet(); err != nil {
183
		t.Errorf("there were unfulfilled exceptions: %s", err)
184
	}
185

186
}
187

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.