forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsql_test.go
329 lines (295 loc) · 7.92 KB
/
sql_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package sql
import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
func TestSqlQuote(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
}
func TestSqlCreateStatement(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
}
func TestSqlInsertStatement(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
}
func pwgen(n int) string {
charset := []byte("abcdedfghijklmnopqrstABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
nchars := len(charset)
buffer := make([]byte, n)
for i := range buffer {
buffer[i] = charset[rand.Intn(nchars)]
}
return string(buffer)
}
func stableMetric(
name string,
tags []telegraf.Tag,
fields []telegraf.Field,
tm time.Time,
tp ...telegraf.ValueType,
) telegraf.Metric {
// We want to compare the output of this plugin with expected
// output. Maps don't preserve order so comparison fails. There's
// no metric constructor that takes a slice of tag and slice of
// field, just the one that takes maps.
//
// To preserve order, construct the metric without tags and fields
// and then add them using AddTag and AddField. Those are stable.
m := metric.New(name, map[string]string{}, map[string]interface{}{}, tm, tp...)
for _, tag := range tags {
m.AddTag(tag.Key, tag.Value)
}
for _, field := range fields {
m.AddField(field.Key, field.Value)
}
return m
}
var (
// 2021-05-17T22:04:45+00:00
// or 2021-05-17T16:04:45-06:00
ts = time.Unix(1621289085, 0).UTC()
testMetrics = []telegraf.Metric{
stableMetric(
"metric_one",
[]telegraf.Tag{
{
Key: "tag_one",
Value: "tag1",
},
{
Key: "tag_two",
Value: "tag2",
},
},
[]telegraf.Field{
{
Key: "int64_one",
Value: int64(1234),
},
{
Key: "int64_two",
Value: int64(2345),
},
},
ts,
),
stableMetric(
"metric_two",
[]telegraf.Tag{
{
Key: "tag_three",
Value: "tag3",
},
},
[]telegraf.Field{
{
Key: "string_one",
Value: "string1",
},
},
ts,
),
stableMetric( //test spaces in metric, tag, and field names
"metric three",
[]telegraf.Tag{
{
Key: "tag four",
Value: "tag4",
},
},
[]telegraf.Field{
{
Key: "string two",
Value: "string2",
},
},
ts,
),
}
)
func TestMysqlIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
initdb, err := filepath.Abs("testdata/mariadb/initdb")
require.NoError(t, err)
// initdb/script.sql creates this database
const dbname = "foo"
// The mariadb image lets you set the root password through an env
// var. We'll use root to insert and query test data.
const username = "root"
password := pwgen(32)
outDir, err := ioutil.TempDir("", "tg-mysql-*")
require.NoError(t, err)
defer os.RemoveAll(outDir)
ctx := context.Background()
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "mariadb",
Env: map[string]string{
"MARIADB_ROOT_PASSWORD": password,
},
BindMounts: map[string]string{
initdb: "/docker-entrypoint-initdb.d",
outDir: "/out",
},
ExposedPorts: []string{"3306/tcp"},
WaitingFor: wait.ForListeningPort("3306/tcp"),
},
Started: true,
}
mariadbContainer, err := testcontainers.GenericContainer(ctx, req)
require.NoError(t, err, "starting container failed")
defer func() {
require.NoError(t, mariadbContainer.Terminate(ctx), "terminating container failed")
}()
// Get the connection details from the container
host, err := mariadbContainer.Host(ctx)
require.NoError(t, err, "getting container host address failed")
require.NotEmpty(t, host)
natPort, err := mariadbContainer.MappedPort(ctx, "3306/tcp")
require.NoError(t, err, "getting container host port failed")
port := natPort.Port()
require.NotEmpty(t, port)
//use the plugin to write to the database
address := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v",
username, password, host, port, dbname,
)
p := newSQL()
p.Log = testutil.Logger{}
p.Driver = "mysql"
p.DataSourceName = address
//p.Convert.Timestamp = "TEXT" //disable mysql default current_timestamp()
p.InitSQL = "SET sql_mode='ANSI_QUOTES';"
require.NoError(t, p.Connect())
require.NoError(t, p.Write(
testMetrics,
))
//dump the database
var rc int
rc, err = mariadbContainer.Exec(ctx, []string{
"bash",
"-c",
"mariadb-dump --user=" + username +
" --password=" + password +
" --compact --skip-opt " +
dbname +
" > /out/dump",
})
require.NoError(t, err)
require.Equal(t, 0, rc)
dumpfile := filepath.Join(outDir, "dump")
require.FileExists(t, dumpfile)
//compare the dump to what we expected
expected, err := ioutil.ReadFile("testdata/mariadb/expected.sql")
require.NoError(t, err)
actual, err := ioutil.ReadFile(dumpfile)
require.NoError(t, err)
require.Equal(t, string(expected), string(actual))
}
func TestPostgresIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
initdb, err := filepath.Abs("testdata/postgres/initdb")
require.NoError(t, err)
// initdb/init.sql creates this database
const dbname = "foo"
// default username for postgres is postgres
const username = "postgres"
password := pwgen(32)
outDir, err := ioutil.TempDir("", "tg-postgres-*")
require.NoError(t, err)
defer os.RemoveAll(outDir)
ctx := context.Background()
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "postgres",
Env: map[string]string{
"POSTGRES_PASSWORD": password,
},
BindMounts: map[string]string{
initdb: "/docker-entrypoint-initdb.d",
outDir: "/out",
},
ExposedPorts: []string{"5432/tcp"},
WaitingFor: wait.ForListeningPort("5432/tcp"),
},
Started: true,
}
cont, err := testcontainers.GenericContainer(ctx, req)
require.NoError(t, err, "starting container failed")
defer func() {
require.NoError(t, cont.Terminate(ctx), "terminating container failed")
}()
// Get the connection details from the container
host, err := cont.Host(ctx)
require.NoError(t, err, "getting container host address failed")
require.NotEmpty(t, host)
natPort, err := cont.MappedPort(ctx, "5432/tcp")
require.NoError(t, err, "getting container host port failed")
port := natPort.Port()
require.NotEmpty(t, port)
//use the plugin to write to the database
// host, port, username, password, dbname
address := fmt.Sprintf("postgres://%v:%v@%v:%v/%v",
username, password, host, port, dbname,
)
p := newSQL()
p.Log = testutil.Logger{}
p.Driver = "pgx"
p.DataSourceName = address
require.NoError(t, p.Connect())
require.NoError(t, p.Write(
testMetrics,
))
//dump the database
//psql -u postgres
var rc int
rc, err = cont.Exec(ctx, []string{
"bash",
"-c",
"pg_dump" +
" --username=" + username +
//" --password=" + password +
// " --compact --skip-opt " +
" --no-comments" +
//" --data-only" +
" " + dbname +
// pg_dump's output has comments that include build info
// of postgres and pg_dump. The build info changes with
// each release. To prevent these changes from causing the
// test to fail, we strip out comments. Also strip out
// blank lines.
"|grep -E -v '(^--|^$)'" +
" > /out/dump 2>&1",
})
require.NoError(t, err)
require.Equal(t, 0, rc)
dumpfile := filepath.Join(outDir, "dump")
require.FileExists(t, dumpfile)
//compare the dump to what we expected
expected, err := ioutil.ReadFile("testdata/postgres/expected.sql")
require.NoError(t, err)
actual, err := ioutil.ReadFile(dumpfile)
require.NoError(t, err)
require.Equal(t, string(expected), string(actual))
}