pangolin_exporter

Форк
0
/
pg_statio_user_indexes_test.go 
109 строк · 3.5 Кб
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 TestPgStatioUserIndexesCollector(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
	inst := &instance{db: db}
32
	columns := []string{
33
		"schemaname",
34
		"relname",
35
		"indexrelname",
36
		"idx_blks_read",
37
		"idx_blks_hit",
38
	}
39
	rows := sqlmock.NewRows(columns).
40
		AddRow("public", "pgtest_accounts", "pgtest_accounts_pkey", 8, 9)
41

42
	mock.ExpectQuery(sanitizeQuery(statioUserIndexesQuery)).WillReturnRows(rows)
43

44
	ch := make(chan prometheus.Metric)
45
	go func() {
46
		defer close(ch)
47
		c := PGStatioUserIndexesCollector{}
48

49
		if err := c.Update(context.Background(), inst, ch); err != nil {
50
			t.Errorf("Error calling PGStatioUserIndexesCollector.Update: %s", err)
51
		}
52
	}()
53
	expected := []MetricResult{
54
		{labels: labelMap{"schemaname": "public", "relname": "pgtest_accounts", "indexrelname": "pgtest_accounts_pkey"}, value: 8, metricType: dto.MetricType_COUNTER},
55
		{labels: labelMap{"schemaname": "public", "relname": "pgtest_accounts", "indexrelname": "pgtest_accounts_pkey"}, value: 9, metricType: dto.MetricType_COUNTER},
56
	}
57
	convey.Convey("Metrics comparison", t, func() {
58
		for _, expect := range expected {
59
			m := readMetric(<-ch)
60
			convey.So(expect, convey.ShouldResemble, m)
61
		}
62
	})
63
	if err := mock.ExpectationsWereMet(); err != nil {
64
		t.Errorf("there were unfulfilled exceptions: %s", err)
65
	}
66
}
67

68
func TestPgStatioUserIndexesCollectorNull(t *testing.T) {
69
	db, mock, err := sqlmock.New()
70
	if err != nil {
71
		t.Fatalf("Error opening a stub db connection: %s", err)
72
	}
73
	defer db.Close()
74
	inst := &instance{db: db}
75
	columns := []string{
76
		"schemaname",
77
		"relname",
78
		"indexrelname",
79
		"idx_blks_read",
80
		"idx_blks_hit",
81
	}
82
	rows := sqlmock.NewRows(columns).
83
		AddRow(nil, nil, nil, nil, nil)
84

85
	mock.ExpectQuery(sanitizeQuery(statioUserIndexesQuery)).WillReturnRows(rows)
86

87
	ch := make(chan prometheus.Metric)
88
	go func() {
89
		defer close(ch)
90
		c := PGStatioUserIndexesCollector{}
91

92
		if err := c.Update(context.Background(), inst, ch); err != nil {
93
			t.Errorf("Error calling PGStatioUserIndexesCollector.Update: %s", err)
94
		}
95
	}()
96
	expected := []MetricResult{
97
		{labels: labelMap{"schemaname": "unknown", "relname": "unknown", "indexrelname": "unknown"}, value: 0, metricType: dto.MetricType_COUNTER},
98
		{labels: labelMap{"schemaname": "unknown", "relname": "unknown", "indexrelname": "unknown"}, value: 0, metricType: dto.MetricType_COUNTER},
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

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

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

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

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