pangolin_exporter
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.
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 TestPgStatioUserIndexesCollector(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()
31inst := &instance{db: db}
32columns := []string{
33"schemaname",
34"relname",
35"indexrelname",
36"idx_blks_read",
37"idx_blks_hit",
38}
39rows := sqlmock.NewRows(columns).
40AddRow("public", "pgtest_accounts", "pgtest_accounts_pkey", 8, 9)
41
42mock.ExpectQuery(sanitizeQuery(statioUserIndexesQuery)).WillReturnRows(rows)
43
44ch := make(chan prometheus.Metric)
45go func() {
46defer close(ch)
47c := PGStatioUserIndexesCollector{}
48
49if err := c.Update(context.Background(), inst, ch); err != nil {
50t.Errorf("Error calling PGStatioUserIndexesCollector.Update: %s", err)
51}
52}()
53expected := []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}
57convey.Convey("Metrics comparison", t, func() {
58for _, expect := range expected {
59m := readMetric(<-ch)
60convey.So(expect, convey.ShouldResemble, m)
61}
62})
63if err := mock.ExpectationsWereMet(); err != nil {
64t.Errorf("there were unfulfilled exceptions: %s", err)
65}
66}
67
68func TestPgStatioUserIndexesCollectorNull(t *testing.T) {
69db, mock, err := sqlmock.New()
70if err != nil {
71t.Fatalf("Error opening a stub db connection: %s", err)
72}
73defer db.Close()
74inst := &instance{db: db}
75columns := []string{
76"schemaname",
77"relname",
78"indexrelname",
79"idx_blks_read",
80"idx_blks_hit",
81}
82rows := sqlmock.NewRows(columns).
83AddRow(nil, nil, nil, nil, nil)
84
85mock.ExpectQuery(sanitizeQuery(statioUserIndexesQuery)).WillReturnRows(rows)
86
87ch := make(chan prometheus.Metric)
88go func() {
89defer close(ch)
90c := PGStatioUserIndexesCollector{}
91
92if err := c.Update(context.Background(), inst, ch); err != nil {
93t.Errorf("Error calling PGStatioUserIndexesCollector.Update: %s", err)
94}
95}()
96expected := []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}
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