diff --git a/plugins/outputs/sql/sql_test.go b/plugins/outputs/sql/sql_test.go index 89d4d6898..c17fcafe1 100644 --- a/plugins/outputs/sql/sql_test.go +++ b/plugins/outputs/sql/sql_test.go @@ -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 @@ -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 { @@ -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) @@ -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) } } @@ -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 @@ -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") @@ -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 @@ -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, @@ -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" @@ -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) diff --git a/plugins/outputs/sql/testdata/mariadb/expected.sql b/plugins/outputs/sql/testdata/mariadb/expected.sql deleted file mode 100644 index 2bcf73f2f..000000000 --- a/plugins/outputs/sql/testdata/mariadb/expected.sql +++ /dev/null @@ -1,40 +0,0 @@ -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `bar` ( - `baz` int(11) DEFAULT NULL -); -/*!40101 SET character_set_client = @saved_cs_client */; -INSERT INTO `bar` VALUES (1); -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `metric three` ( - `timestamp` timestamp NOT NULL DEFAULT current_timestamp(), - `tag four` text DEFAULT NULL, - `string two` text DEFAULT NULL -); -/*!40101 SET character_set_client = @saved_cs_client */; -INSERT INTO `metric three` VALUES ('2021-05-17 22:04:45','tag4','string2'); -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `metric_one` ( - `timestamp` timestamp NOT NULL DEFAULT current_timestamp(), - `tag_one` text DEFAULT NULL, - `tag_two` text DEFAULT NULL, - `int64_one` int(11) DEFAULT NULL, - `int64_two` int(11) DEFAULT NULL, - `bool_one` tinyint(1) DEFAULT NULL, - `bool_two` tinyint(1) DEFAULT NULL, - `uint64_one` int(10) unsigned DEFAULT NULL, - `float64_one` double DEFAULT NULL -); -/*!40101 SET character_set_client = @saved_cs_client */; -INSERT INTO `metric_one` VALUES ('2021-05-17 22:04:45','tag1','tag2',1234,2345,1,0,1000000000,3.1415); -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `metric_two` ( - `timestamp` timestamp NOT NULL DEFAULT current_timestamp(), - `tag_three` text DEFAULT NULL, - `string_one` text DEFAULT NULL -); -/*!40101 SET character_set_client = @saved_cs_client */; -INSERT INTO `metric_two` VALUES ('2021-05-17 22:04:45','tag3','string1'); diff --git a/plugins/outputs/sql/testdata/mariadb/expected_metric_one.sql b/plugins/outputs/sql/testdata/mariadb/expected_metric_one.sql index 44e782127..648561462 100644 --- a/plugins/outputs/sql/testdata/mariadb/expected_metric_one.sql +++ b/plugins/outputs/sql/testdata/mariadb/expected_metric_one.sql @@ -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, diff --git a/plugins/outputs/sql/testdata/mariadb/expected_metric_three.sql b/plugins/outputs/sql/testdata/mariadb/expected_metric_three.sql index 8c75bcf8f..a13430504 100644 --- a/plugins/outputs/sql/testdata/mariadb/expected_metric_three.sql +++ b/plugins/outputs/sql/testdata/mariadb/expected_metric_three.sql @@ -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, diff --git a/plugins/outputs/sql/testdata/mariadb/expected_metric_two.sql b/plugins/outputs/sql/testdata/mariadb/expected_metric_two.sql index 92be4e8bc..f7c7d396e 100644 --- a/plugins/outputs/sql/testdata/mariadb/expected_metric_two.sql +++ b/plugins/outputs/sql/testdata/mariadb/expected_metric_two.sql @@ -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,