pangolin_exporter

Форк
0
/
pg_replication_slot_test.go 
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.
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 TestPgReplicationSlotCollectorActive(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{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
35
	rows := sqlmock.NewRows(columns).
36
		AddRow("test_slot", "physical", 5, 3, true)
37
	mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
38

39
	ch := make(chan prometheus.Metric)
40
	go func() {
41
		defer close(ch)
42
		c := PGReplicationSlotCollector{}
43

44
		if err := c.Update(context.Background(), inst, ch); err != nil {
45
			t.Errorf("Error calling PGPostmasterCollector.Update: %s", err)
46
		}
47
	}()
48

49
	expected := []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

55
	convey.Convey("Metrics comparison", t, func() {
56
		for _, expect := range expected {
57
			m := readMetric(<-ch)
58
			convey.So(expect, convey.ShouldResemble, m)
59
		}
60
	})
61
	if err := mock.ExpectationsWereMet(); err != nil {
62
		t.Errorf("there were unfulfilled exceptions: %s", err)
63
	}
64
}
65

66
func TestPgReplicationSlotCollectorInActive(t *testing.T) {
67
	db, mock, err := sqlmock.New()
68
	if err != nil {
69
		t.Fatalf("Error opening a stub db connection: %s", err)
70
	}
71
	defer db.Close()
72

73
	inst := &instance{db: db}
74

75
	columns := []string{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
76
	rows := sqlmock.NewRows(columns).
77
		AddRow("test_slot", "physical", 6, 12, false)
78
	mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
79

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

85
		if err := c.Update(context.Background(), inst, ch); err != nil {
86
			t.Errorf("Error calling PGReplicationSlotCollector.Update: %s", err)
87
		}
88
	}()
89

90
	expected := []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

95
	convey.Convey("Metrics comparison", t, func() {
96
		for _, expect := range expected {
97
			m := readMetric(<-ch)
98
			convey.So(expect, convey.ShouldResemble, m)
99
		}
100
	})
101
	if err := mock.ExpectationsWereMet(); err != nil {
102
		t.Errorf("there were unfulfilled exceptions: %s", err)
103
	}
104

105
}
106

107
func TestPgReplicationSlotCollectorActiveNil(t *testing.T) {
108
	db, mock, err := sqlmock.New()
109
	if err != nil {
110
		t.Fatalf("Error opening a stub db connection: %s", err)
111
	}
112
	defer db.Close()
113

114
	inst := &instance{db: db}
115

116
	columns := []string{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
117
	rows := sqlmock.NewRows(columns).
118
		AddRow("test_slot", "physical", 6, 12, nil)
119
	mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
120

121
	ch := make(chan prometheus.Metric)
122
	go func() {
123
		defer close(ch)
124
		c := PGReplicationSlotCollector{}
125

126
		if err := c.Update(context.Background(), inst, ch); err != nil {
127
			t.Errorf("Error calling PGReplicationSlotCollector.Update: %s", err)
128
		}
129
	}()
130

131
	expected := []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

136
	convey.Convey("Metrics comparison", t, func() {
137
		for _, expect := range expected {
138
			m := readMetric(<-ch)
139
			convey.So(expect, convey.ShouldResemble, m)
140
		}
141
	})
142
	if err := mock.ExpectationsWereMet(); err != nil {
143
		t.Errorf("there were unfulfilled exceptions: %s", err)
144
	}
145
}
146

147
func TestPgReplicationSlotCollectorTestNilValues(t *testing.T) {
148
	db, mock, err := sqlmock.New()
149
	if err != nil {
150
		t.Fatalf("Error opening a stub db connection: %s", err)
151
	}
152
	defer db.Close()
153

154
	inst := &instance{db: db}
155

156
	columns := []string{"slot_name", "slot_type", "current_wal_lsn", "confirmed_flush_lsn", "active"}
157
	rows := sqlmock.NewRows(columns).
158
		AddRow(nil, nil, nil, nil, true)
159
	mock.ExpectQuery(sanitizeQuery(pgReplicationSlotQuery)).WillReturnRows(rows)
160

161
	ch := make(chan prometheus.Metric)
162
	go func() {
163
		defer close(ch)
164
		c := PGReplicationSlotCollector{}
165

166
		if err := c.Update(context.Background(), inst, ch); err != nil {
167
			t.Errorf("Error calling PGReplicationSlotCollector.Update: %s", err)
168
		}
169
	}()
170

171
	expected := []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

177
	convey.Convey("Metrics comparison", t, func() {
178
		for _, expect := range expected {
179
			m := readMetric(<-ch)
180
			convey.So(expect, convey.ShouldResemble, m)
181
		}
182
	})
183
	if err := mock.ExpectationsWereMet(); err != nil {
184
		t.Errorf("there were unfulfilled exceptions: %s", err)
185
	}
186
}
187

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

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

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

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