From ea9aa38d23e56cf65fd9370193c310d8f8c189c0 Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Fri, 18 Nov 2022 07:22:46 -0700 Subject: [PATCH] test(outputs.sql): do not write to file during testing (#12203) --- plugins/outputs/sql/sql_test.go | 111 +++++++++--------- .../testdata/mariadb/expected_metric_one.sql | 14 +++ .../mariadb/expected_metric_three.sql | 8 ++ .../testdata/mariadb/expected_metric_two.sql | 8 ++ 4 files changed, 86 insertions(+), 55 deletions(-) create mode 100644 plugins/outputs/sql/testdata/mariadb/expected_metric_one.sql create mode 100644 plugins/outputs/sql/testdata/mariadb/expected_metric_three.sql create mode 100644 plugins/outputs/sql/testdata/mariadb/expected_metric_two.sql diff --git a/plugins/outputs/sql/sql_test.go b/plugins/outputs/sql/sql_test.go index 086baa1c2..aedb8eb7c 100644 --- a/plugins/outputs/sql/sql_test.go +++ b/plugins/outputs/sql/sql_test.go @@ -209,31 +209,35 @@ func TestMysqlIntegration(t *testing.T) { testMetrics, )) - //dump the database - var rc int - rc, _, err = container.Exec([]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.Eventually(t, func() bool { - _, err := os.Stat(dumpfile) - return !os.IsNotExist(err) - }, 5*time.Second, 10*time.Millisecond) + cases := []struct { + expectedFile 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) + require.NoError(t, err) - //compare the dump to what we expected - expected, err := os.ReadFile("testdata/mariadb/expected.sql") - require.NoError(t, err) - actual, err := os.ReadFile(dumpfile) - require.NoError(t, err) - require.Equal(t, string(expected), string(actual)) + require.Eventually(t, func() bool { + rc, out, err := container.Exec([]string{ + "bash", + "-c", + "mariadb-dump --user=" + username + + " --password=" + password + + " --compact --skip-opt " + + dbname, + }) + require.NoError(t, err) + require.Equal(t, 0, rc) + + bytes, err := io.ReadAll(out) + require.NoError(t, err) + + return strings.Contains(string(bytes), string(expected)) + }, 5*time.Second, 500*time.Millisecond) + } } func TestPostgresIntegration(t *testing.T) { @@ -291,38 +295,35 @@ func TestPostgresIntegration(t *testing.T) { testMetrics, )) - //dump the database - //psql -u postgres - var rc int - rc, _, err = container.Exec([]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", - }) + expected, err := os.ReadFile("./testdata/postgres/expected.sql") 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 := os.ReadFile("testdata/postgres/expected.sql") - require.NoError(t, err) - actual, err := os.ReadFile(dumpfile) - require.NoError(t, err) - require.Equal(t, string(expected), string(actual)) + require.Eventually(t, func() bool { + rc, out, err := container.Exec([]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 '(^--|^$)'", + }) + require.NoError(t, err) + require.Equal(t, 0, rc) + + bytes, err := io.ReadAll(out) + require.NoError(t, err) + + return strings.Contains(string(bytes), string(expected)) + }, 5*time.Second, 500*time.Millisecond) } func TestClickHouseIntegration(t *testing.T) { @@ -406,6 +407,6 @@ func TestClickHouseIntegration(t *testing.T) { bytes, err := io.ReadAll(out) require.NoError(t, err) return strings.Contains(string(bytes), tc.expected) - }, 5*time.Second, 10*time.Millisecond) + }, 5*time.Second, 500*time.Millisecond) } } diff --git a/plugins/outputs/sql/testdata/mariadb/expected_metric_one.sql b/plugins/outputs/sql/testdata/mariadb/expected_metric_one.sql new file mode 100644 index 000000000..c1cd22f69 --- /dev/null +++ b/plugins/outputs/sql/testdata/mariadb/expected_metric_one.sql @@ -0,0 +1,14 @@ +/*!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); diff --git a/plugins/outputs/sql/testdata/mariadb/expected_metric_three.sql b/plugins/outputs/sql/testdata/mariadb/expected_metric_three.sql new file mode 100644 index 000000000..62fef63d8 --- /dev/null +++ b/plugins/outputs/sql/testdata/mariadb/expected_metric_three.sql @@ -0,0 +1,8 @@ +/*!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'); diff --git a/plugins/outputs/sql/testdata/mariadb/expected_metric_two.sql b/plugins/outputs/sql/testdata/mariadb/expected_metric_two.sql new file mode 100644 index 000000000..535a22d87 --- /dev/null +++ b/plugins/outputs/sql/testdata/mariadb/expected_metric_two.sql @@ -0,0 +1,8 @@ +/*!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');