Skip to content

Commit

Permalink
test(outputs.sql): Fix integration tests (influxdata#16543)
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan authored Feb 25, 2025
1 parent cb71790 commit edb28de
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 75 deletions.
57 changes: 25 additions & 32 deletions plugins/outputs/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ func TestMysqlIntegration(t *testing.T) {
wait.ForLog("mariadbd: ready for connections.").WithOccurrence(2),
),
}
err = container.Start()
require.NoError(t, err, "failed to start container")
require.NoError(t, container.Start(), "failed to start container")
defer container.Terminate()

// use the plugin to write to the database
Expand All @@ -203,19 +202,15 @@ func TestMysqlIntegration(t *testing.T) {
p.InitSQL = "SET sql_mode='ANSI_QUOTES';"

require.NoError(t, p.Connect())
require.NoError(t, p.Write(
testMetrics,
))
require.NoError(t, p.Write(testMetrics))

cases := []struct {
expectedFile string
}{
{"./testdata/mariadb/expected_metric_one.sql"},
{"./testdata/mariadb/expected_metric_two.sql"},
{"./testdata/mariadb/expected_metric_three.sql"},
files := []string{
"./testdata/mariadb/expected_metric_one.sql",
"./testdata/mariadb/expected_metric_two.sql",
"./testdata/mariadb/expected_metric_three.sql",
}
for _, tc := range cases {
expected, err := os.ReadFile(tc.expectedFile)
for _, fn := range files {
expected, err := os.ReadFile(fn)
require.NoError(t, err)

require.Eventually(t, func() bool {
Expand All @@ -224,7 +219,8 @@ func TestMysqlIntegration(t *testing.T) {
"-c",
"mariadb-dump --user=" + username +
" --password=" + password +
" --compact --skip-opt " +
" --compact" +
" --skip-opt " +
dbname,
})
require.NoError(t, err)
Expand All @@ -234,7 +230,7 @@ func TestMysqlIntegration(t *testing.T) {
require.NoError(t, err)

return bytes.Contains(b, expected)
}, 10*time.Second, 500*time.Millisecond, tc.expectedFile)
}, 10*time.Second, 500*time.Millisecond, fn)
}
}

Expand Down Expand Up @@ -271,8 +267,7 @@ func TestPostgresIntegration(t *testing.T) {
wait.ForLog("database system is ready to accept connections").WithOccurrence(2),
),
}
err = container.Start()
require.NoError(t, err, "failed to start container")
require.NoError(t, container.Start(), "failed to start container")
defer container.Terminate()

// use the plugin to write to the database
Expand All @@ -290,9 +285,7 @@ func TestPostgresIntegration(t *testing.T) {

require.NoError(t, p.Connect())
defer p.Close()
require.NoError(t, p.Write(
testMetrics,
))
require.NoError(t, p.Write(testMetrics))
require.NoError(t, p.Close())

expected, err := os.ReadFile("./testdata/postgres/expected.sql")
Expand All @@ -304,10 +297,7 @@ func TestPostgresIntegration(t *testing.T) {
"-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
Expand Down Expand Up @@ -340,15 +330,20 @@ func TestClickHouseIntegration(t *testing.T) {
// initdb/init.sql creates this database
const dbname = "foo"

// default username for clickhouse is default
const username = "default"
// username for connecting to clickhouse
const username = "clickhouse"

password := pwgen(32)
outDir := t.TempDir()

servicePort := "9000"
container := testutil.Container{
Image: "clickhouse",
ExposedPorts: []string{servicePort, "8123"},
Env: map[string]string{
"CLICKHOUSE_USER": "clickhouse",
"CLICKHOUSE_PASSWORD": password,
},
Files: map[string]string{
"/docker-entrypoint-initdb.d/script.sql": initdb,
"/etc/clickhouse-server/config.d/enable_stdout_log.xml": logConfig,
Expand All @@ -360,14 +355,13 @@ func TestClickHouseIntegration(t *testing.T) {
wait.ForLog("Ready for connections"),
),
}
err = container.Start()
require.NoError(t, err, "failed to start container")
require.NoError(t, container.Start(), "failed to start container")
defer container.Terminate()

// use the plugin to write to the database
// host, port, username, password, dbname
address := fmt.Sprintf("tcp://%v:%v/%v?username=%v",
container.Address, container.Ports[servicePort], dbname, username)
address := fmt.Sprintf("tcp://%s:%s/%s?username=%s&password=%s",
container.Address, container.Ports[servicePort], dbname, username, password)
p := newSQL()
p.Log = testutil.Logger{}
p.Driver = "clickhouse"
Expand Down Expand Up @@ -402,9 +396,8 @@ func TestClickHouseIntegration(t *testing.T) {
" --user=" + username +
" --database=" + dbname +
" --format=TabSeparatedRaw" +
" --multiquery --query=" +
"\"SELECT * FROM \\\"" + tc.table + "\\\";" +
"SHOW CREATE TABLE \\\"" + tc.table + "\\\"\"",
" --multiquery" +
` --query="SELECT * FROM \"` + tc.table + `\"; SHOW CREATE TABLE \"` + tc.table + `\""`,
})
require.NoError(t, err)
b, err := io.ReadAll(out)
Expand Down
40 changes: 0 additions & 40 deletions plugins/outputs/sql/testdata/mariadb/expected.sql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!40101 SET character_set_client = utf8 */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `metric_one` (
`timestamp` timestamp NULL DEFAULT NULL,
`tag_one` text DEFAULT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!40101 SET character_set_client = utf8 */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `metric three` (
`timestamp` timestamp NULL DEFAULT NULL,
`tag four` text DEFAULT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!40101 SET character_set_client = utf8 */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `metric_two` (
`timestamp` timestamp NULL DEFAULT NULL,
`tag_three` text DEFAULT NULL,
Expand Down

0 comments on commit edb28de

Please sign in to comment.