test(outputs.sql): Fix integration tests (#16543)

This commit is contained in:
Sven Rebhan 2025-02-25 10:21:24 +01:00 committed by GitHub
parent cb71790cf7
commit edb28dea07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 28 additions and 75 deletions

View File

@ -188,8 +188,7 @@ func TestMysqlIntegration(t *testing.T) {
wait.ForLog("mariadbd: ready for connections.").WithOccurrence(2), wait.ForLog("mariadbd: ready for connections.").WithOccurrence(2),
), ),
} }
err = container.Start() require.NoError(t, container.Start(), "failed to start container")
require.NoError(t, err, "failed to start container")
defer container.Terminate() defer container.Terminate()
// use the plugin to write to the database // use the plugin to write to the database
@ -203,19 +202,15 @@ func TestMysqlIntegration(t *testing.T) {
p.InitSQL = "SET sql_mode='ANSI_QUOTES';" p.InitSQL = "SET sql_mode='ANSI_QUOTES';"
require.NoError(t, p.Connect()) require.NoError(t, p.Connect())
require.NoError(t, p.Write( require.NoError(t, p.Write(testMetrics))
testMetrics,
))
cases := []struct { files := []string{
expectedFile string "./testdata/mariadb/expected_metric_one.sql",
}{ "./testdata/mariadb/expected_metric_two.sql",
{"./testdata/mariadb/expected_metric_one.sql"}, "./testdata/mariadb/expected_metric_three.sql",
{"./testdata/mariadb/expected_metric_two.sql"},
{"./testdata/mariadb/expected_metric_three.sql"},
} }
for _, tc := range cases { for _, fn := range files {
expected, err := os.ReadFile(tc.expectedFile) expected, err := os.ReadFile(fn)
require.NoError(t, err) require.NoError(t, err)
require.Eventually(t, func() bool { require.Eventually(t, func() bool {
@ -224,7 +219,8 @@ func TestMysqlIntegration(t *testing.T) {
"-c", "-c",
"mariadb-dump --user=" + username + "mariadb-dump --user=" + username +
" --password=" + password + " --password=" + password +
" --compact --skip-opt " + " --compact" +
" --skip-opt " +
dbname, dbname,
}) })
require.NoError(t, err) require.NoError(t, err)
@ -234,7 +230,7 @@ func TestMysqlIntegration(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
return bytes.Contains(b, expected) 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), wait.ForLog("database system is ready to accept connections").WithOccurrence(2),
), ),
} }
err = container.Start() require.NoError(t, container.Start(), "failed to start container")
require.NoError(t, err, "failed to start container")
defer container.Terminate() defer container.Terminate()
// use the plugin to write to the database // use the plugin to write to the database
@ -290,9 +285,7 @@ func TestPostgresIntegration(t *testing.T) {
require.NoError(t, p.Connect()) require.NoError(t, p.Connect())
defer p.Close() defer p.Close()
require.NoError(t, p.Write( require.NoError(t, p.Write(testMetrics))
testMetrics,
))
require.NoError(t, p.Close()) require.NoError(t, p.Close())
expected, err := os.ReadFile("./testdata/postgres/expected.sql") expected, err := os.ReadFile("./testdata/postgres/expected.sql")
@ -304,10 +297,7 @@ func TestPostgresIntegration(t *testing.T) {
"-c", "-c",
"pg_dump" + "pg_dump" +
" --username=" + username + " --username=" + username +
// " --password=" + password +
// " --compact --skip-opt " +
" --no-comments" + " --no-comments" +
// " --data-only" +
" " + dbname + " " + dbname +
// pg_dump's output has comments that include build info // pg_dump's output has comments that include build info
// of postgres and pg_dump. The build info changes with // 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 // initdb/init.sql creates this database
const dbname = "foo" const dbname = "foo"
// default username for clickhouse is default // username for connecting to clickhouse
const username = "default" const username = "clickhouse"
password := pwgen(32)
outDir := t.TempDir() outDir := t.TempDir()
servicePort := "9000" servicePort := "9000"
container := testutil.Container{ container := testutil.Container{
Image: "clickhouse", Image: "clickhouse",
ExposedPorts: []string{servicePort, "8123"}, ExposedPorts: []string{servicePort, "8123"},
Env: map[string]string{
"CLICKHOUSE_USER": "clickhouse",
"CLICKHOUSE_PASSWORD": password,
},
Files: map[string]string{ Files: map[string]string{
"/docker-entrypoint-initdb.d/script.sql": initdb, "/docker-entrypoint-initdb.d/script.sql": initdb,
"/etc/clickhouse-server/config.d/enable_stdout_log.xml": logConfig, "/etc/clickhouse-server/config.d/enable_stdout_log.xml": logConfig,
@ -360,14 +355,13 @@ func TestClickHouseIntegration(t *testing.T) {
wait.ForLog("Ready for connections"), wait.ForLog("Ready for connections"),
), ),
} }
err = container.Start() require.NoError(t, container.Start(), "failed to start container")
require.NoError(t, err, "failed to start container")
defer container.Terminate() defer container.Terminate()
// use the plugin to write to the database // use the plugin to write to the database
// host, port, username, password, dbname // host, port, username, password, dbname
address := fmt.Sprintf("tcp://%v:%v/%v?username=%v", address := fmt.Sprintf("tcp://%s:%s/%s?username=%s&password=%s",
container.Address, container.Ports[servicePort], dbname, username) container.Address, container.Ports[servicePort], dbname, username, password)
p := newSQL() p := newSQL()
p.Log = testutil.Logger{} p.Log = testutil.Logger{}
p.Driver = "clickhouse" p.Driver = "clickhouse"
@ -402,9 +396,8 @@ func TestClickHouseIntegration(t *testing.T) {
" --user=" + username + " --user=" + username +
" --database=" + dbname + " --database=" + dbname +
" --format=TabSeparatedRaw" + " --format=TabSeparatedRaw" +
" --multiquery --query=" + " --multiquery" +
"\"SELECT * FROM \\\"" + tc.table + "\\\";" + ` --query="SELECT * FROM \"` + tc.table + `\"; SHOW CREATE TABLE \"` + tc.table + `\""`,
"SHOW CREATE TABLE \\\"" + tc.table + "\\\"\"",
}) })
require.NoError(t, err) require.NoError(t, err)
b, err := io.ReadAll(out) b, err := io.ReadAll(out)

View File

@ -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');

View File

@ -1,4 +1,4 @@
/*!40101 SET character_set_client = utf8 */; /*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `metric_one` ( CREATE TABLE `metric_one` (
`timestamp` timestamp NULL DEFAULT NULL, `timestamp` timestamp NULL DEFAULT NULL,
`tag_one` text DEFAULT NULL, `tag_one` text DEFAULT NULL,

View File

@ -1,4 +1,4 @@
/*!40101 SET character_set_client = utf8 */; /*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `metric three` ( CREATE TABLE `metric three` (
`timestamp` timestamp NULL DEFAULT NULL, `timestamp` timestamp NULL DEFAULT NULL,
`tag four` text DEFAULT NULL, `tag four` text DEFAULT NULL,

View File

@ -1,4 +1,4 @@
/*!40101 SET character_set_client = utf8 */; /*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `metric_two` ( CREATE TABLE `metric_two` (
`timestamp` timestamp NULL DEFAULT NULL, `timestamp` timestamp NULL DEFAULT NULL,
`tag_three` text DEFAULT NULL, `tag_three` text DEFAULT NULL,