forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql_extensible_test.go
224 lines (190 loc) · 4.89 KB
/
postgresql_extensible_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package postgresql_extensible
import (
"fmt"
"testing"
"github.com/influxdata/telegraf/plugins/inputs/postgresql"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func queryRunner(t *testing.T, q query) *testutil.Accumulator {
p := &Postgresql{
Service: postgresql.Service{
Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
),
},
Databases: []string{"postgres"},
Query: q,
}
var acc testutil.Accumulator
p.Start(&acc)
require.NoError(t, acc.GatherError(p.Gather))
return &acc
}
func TestPostgresqlGeneratesMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
acc := queryRunner(t, query{{
Sqlquery: "select * from pg_stat_database",
Version: 901,
Withdbname: false,
Tagvalue: "",
}})
intMetrics := []string{
"xact_commit",
"xact_rollback",
"blks_read",
"blks_hit",
"tup_returned",
"tup_fetched",
"tup_inserted",
"tup_updated",
"tup_deleted",
"conflicts",
"temp_files",
"temp_bytes",
"deadlocks",
"numbackends",
"datid",
}
int32Metrics := []string{}
floatMetrics := []string{
"blk_read_time",
"blk_write_time",
}
stringMetrics := []string{
"datname",
}
metricsCounted := 0
for _, metric := range intMetrics {
assert.True(t, acc.HasInt64Field("postgresql", metric))
metricsCounted++
}
for _, metric := range int32Metrics {
assert.True(t, acc.HasInt32Field("postgresql", metric))
metricsCounted++
}
for _, metric := range floatMetrics {
assert.True(t, acc.HasFloatField("postgresql", metric))
metricsCounted++
}
for _, metric := range stringMetrics {
assert.True(t, acc.HasStringField("postgresql", metric))
metricsCounted++
}
assert.True(t, metricsCounted > 0)
assert.Equal(t, len(floatMetrics)+len(intMetrics)+len(int32Metrics)+len(stringMetrics), metricsCounted)
}
func TestPostgresqlQueryOutputTests(t *testing.T) {
const measurement = "postgresql"
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
examples := map[string]func(*testutil.Accumulator){
"SELECT 10.0::float AS myvalue": func(acc *testutil.Accumulator) {
v, found := acc.FloatField(measurement, "myvalue")
assert.True(t, found)
assert.Equal(t, 10.0, v)
},
"SELECT 10.0 AS myvalue": func(acc *testutil.Accumulator) {
v, found := acc.StringField(measurement, "myvalue")
assert.True(t, found)
assert.Equal(t, "10.0", v)
},
"SELECT 'hello world' AS myvalue": func(acc *testutil.Accumulator) {
v, found := acc.StringField(measurement, "myvalue")
assert.True(t, found)
assert.Equal(t, "hello world", v)
},
"SELECT true AS myvalue": func(acc *testutil.Accumulator) {
v, found := acc.BoolField(measurement, "myvalue")
assert.True(t, found)
assert.Equal(t, true, v)
},
}
for q, assertions := range examples {
acc := queryRunner(t, query{{
Sqlquery: q,
Version: 901,
Withdbname: false,
Tagvalue: "",
}})
assertions(acc)
}
}
func TestPostgresqlFieldOutput(t *testing.T) {
const measurement = "postgresql"
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
acc := queryRunner(t, query{{
Sqlquery: "select * from pg_stat_database",
Version: 901,
Withdbname: false,
Tagvalue: "",
}})
intMetrics := []string{
"xact_commit",
"xact_rollback",
"blks_read",
"blks_hit",
"tup_returned",
"tup_fetched",
"tup_inserted",
"tup_updated",
"tup_deleted",
"conflicts",
"temp_files",
"temp_bytes",
"deadlocks",
"numbackends",
"datid",
}
int32Metrics := []string{}
floatMetrics := []string{
"blk_read_time",
"blk_write_time",
}
stringMetrics := []string{
"datname",
}
for _, field := range intMetrics {
_, found := acc.Int64Field(measurement, field)
assert.True(t, found, fmt.Sprintf("expected %s to be an integer", field))
}
for _, field := range int32Metrics {
_, found := acc.Int32Field(measurement, field)
assert.True(t, found, fmt.Sprintf("expected %s to be an int32", field))
}
for _, field := range floatMetrics {
_, found := acc.FloatField(measurement, field)
assert.True(t, found, fmt.Sprintf("expected %s to be a float64", field))
}
for _, field := range stringMetrics {
_, found := acc.StringField(measurement, field)
assert.True(t, found, fmt.Sprintf("expected %s to be a str", field))
}
}
func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
p := &Postgresql{
Service: postgresql.Service{
Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
),
},
}
var acc testutil.Accumulator
require.NoError(t, p.Start(&acc))
require.NoError(t, acc.GatherError(p.Gather))
assert.NotEmpty(t, p.IgnoredColumns())
for col := range p.IgnoredColumns() {
assert.False(t, acc.HasMeasurement(col))
}
}