From 8ebd7e48ae58bf02fa40b4e1ae0bea801db41f52 Mon Sep 17 00:00:00 2001 From: Wazir Ahmed Date: Wed, 6 Aug 2025 13:26:05 +0530 Subject: [PATCH] TAP: Move init_mysql_conn() and run_q() to utils Signed-off-by: Wazir Ahmed --- test/tap/tap/utils.cpp | 35 +++++++++++++++++++ test/tap/tap/utils.h | 3 ++ test/tap/tests/admin_various_commands2-t.cpp | 4 --- test/tap/tests/admin_various_commands3-t.cpp | 4 --- test/tap/tests/firewall_commands1-t.cpp | 4 --- test/tap/tests/kill_connection-t.cpp | 4 --- test/tap/tests/kill_connection2-t.cpp | 4 --- test/tap/tests/kill_connection3-t.cpp | 4 --- test/tap/tests/mysql-fast_forward-t.cpp | 4 --- test/tap/tests/mysql-mirror1-t.cpp | 5 --- .../mysql-protocol_compression_level-t.cpp | 29 +++------------ test/tap/tests/test_cluster1-t.cpp | 5 --- 12 files changed, 43 insertions(+), 62 deletions(-) diff --git a/test/tap/tap/utils.cpp b/test/tap/tap/utils.cpp index 89a522ec7..7fbe4d074 100644 --- a/test/tap/tap/utils.cpp +++ b/test/tap/tap/utils.cpp @@ -2316,3 +2316,38 @@ bool get_env_bool(const char* envname, bool envdefault) { return (bool) res; }; + +MYSQL* init_mysql_conn(char* host, int port, char* user, char* pass, bool ssl, bool cmp) { + diag("Creating MySQL conn host=\"%s\" port=\"%d\" user=\"%s\" ssl=\"%d\" cmp=\"%d\"", host, port, user, ssl, cmp); + + MYSQL* mysql = mysql_init(NULL); + + if (!mysql) { + return nullptr; + } + if (cmp) { + if (mysql_options(mysql, MYSQL_OPT_COMPRESS, nullptr)) { + return nullptr; + } + } + + int cflags = 0; + + if (ssl) { + if (mysql_ssl_set(mysql, NULL, NULL, NULL, NULL, NULL)) { + return nullptr; + } + cflags |= CLIENT_SSL; + } + + if (!mysql_real_connect(mysql, host, user, pass, NULL, port, NULL, cflags)) { + return nullptr; + } + + return mysql; +} + +int run_q(MYSQL *mysql, const char *q) { + MYSQL_QUERY_T(mysql,q); + return 0; +} diff --git a/test/tap/tap/utils.h b/test/tap/tap/utils.h index f492431e2..36dc54c17 100644 --- a/test/tap/tap/utils.h +++ b/test/tap/tap/utils.h @@ -1002,4 +1002,7 @@ const char* get_env_str(const char* envname, const char* envdefault); int get_env_int(const char* envname, int envdefault); bool get_env_bool(const char* envname, bool envdefault); +MYSQL* init_mysql_conn(char* host, int port, char* user, char* pass, bool ssl=false, bool cmp=false); +int run_q(MYSQL *mysql, const char *q); + #endif // #define UTILS_H diff --git a/test/tap/tests/admin_various_commands2-t.cpp b/test/tap/tests/admin_various_commands2-t.cpp index 94081b1e6..a412abc27 100644 --- a/test/tap/tests/admin_various_commands2-t.cpp +++ b/test/tap/tests/admin_various_commands2-t.cpp @@ -103,10 +103,6 @@ void add_commands_set1(std::vector& queries, std::string m, bool wi } } -int run_q(MYSQL *mysql, const char *q) { - MYSQL_QUERY(mysql,q); - return 0; -} int main() { CommandLine cl; diff --git a/test/tap/tests/admin_various_commands3-t.cpp b/test/tap/tests/admin_various_commands3-t.cpp index db86720d8..ad1c763ea 100644 --- a/test/tap/tests/admin_various_commands3-t.cpp +++ b/test/tap/tests/admin_various_commands3-t.cpp @@ -37,10 +37,6 @@ std::vector vals = { 100, 345, 800 }; std::vector queries = {}; -int run_q(MYSQL *mysql, const char *q) { - MYSQL_QUERY(mysql,q); - return 0; -} int main() { CommandLine cl; diff --git a/test/tap/tests/firewall_commands1-t.cpp b/test/tap/tests/firewall_commands1-t.cpp index 6aa56cf61..366bcd1e0 100644 --- a/test/tap/tests/firewall_commands1-t.cpp +++ b/test/tap/tests/firewall_commands1-t.cpp @@ -51,10 +51,6 @@ std::vector queries = { "LOAD MYSQL FIREWALL TO RUNTIME", }; -int run_q(MYSQL *mysql, const char *q) { - MYSQL_QUERY(mysql,q); - return 0; -} int main() { CommandLine cl; diff --git a/test/tap/tests/kill_connection-t.cpp b/test/tap/tests/kill_connection-t.cpp index 5f964a959..b6e2c76ef 100644 --- a/test/tap/tests/kill_connection-t.cpp +++ b/test/tap/tests/kill_connection-t.cpp @@ -22,10 +22,6 @@ This test verifies a variety of things: const int NUM_CONNS = 5; -int run_q(MYSQL *mysql, const char *q) { - MYSQL_QUERY(mysql,q); - return 0; -} int main(int argc, char** argv) { CommandLine cl; diff --git a/test/tap/tests/kill_connection2-t.cpp b/test/tap/tests/kill_connection2-t.cpp index 947c9d9cf..58989cafa 100644 --- a/test/tap/tests/kill_connection2-t.cpp +++ b/test/tap/tests/kill_connection2-t.cpp @@ -18,10 +18,6 @@ This test verifies that client connections are dropped because of: const int NUM_CONNS = 5; -int run_q(MYSQL *mysql, const char *q) { - MYSQL_QUERY(mysql,q); - return 0; -} MYSQL * conns[NUM_CONNS]; unsigned long mythreadid[NUM_CONNS]; diff --git a/test/tap/tests/kill_connection3-t.cpp b/test/tap/tests/kill_connection3-t.cpp index 1c023ffb5..e4230f014 100644 --- a/test/tap/tests/kill_connection3-t.cpp +++ b/test/tap/tests/kill_connection3-t.cpp @@ -18,10 +18,6 @@ This test verifies that client connections are dropped because of: const int NUM_CONNS = 35; -int run_q(MYSQL *mysql, const char *q) { - MYSQL_QUERY(mysql,q); - return 0; -} MYSQL * conns[NUM_CONNS]; unsigned long mythreadid[NUM_CONNS]; diff --git a/test/tap/tests/mysql-fast_forward-t.cpp b/test/tap/tests/mysql-fast_forward-t.cpp index 20b1b6df4..4a37ad0e9 100644 --- a/test/tap/tests/mysql-fast_forward-t.cpp +++ b/test/tap/tests/mysql-fast_forward-t.cpp @@ -18,10 +18,6 @@ This test verifies that client connections are dropped because of: const int NUM_CONNS = 35; const int RPI = 50; -int run_q(MYSQL *mysql, const char *q) { - MYSQL_QUERY(mysql,q); - return 0; -} MYSQL * conns[NUM_CONNS]; unsigned long mythreadid[NUM_CONNS]; diff --git a/test/tap/tests/mysql-mirror1-t.cpp b/test/tap/tests/mysql-mirror1-t.cpp index 54e63488f..12fe407c1 100644 --- a/test/tap/tests/mysql-mirror1-t.cpp +++ b/test/tap/tests/mysql-mirror1-t.cpp @@ -24,11 +24,6 @@ This test also triggers: const int NUM_CONNS = 15; const int RPI = 20; // rows per insert -int run_q(MYSQL *mysql, const char *q) { - MYSQL_QUERY(mysql,q); - return 0; -} - MYSQL * conns[NUM_CONNS]; unsigned long mythreadid[NUM_CONNS]; diff --git a/test/tap/tests/mysql-protocol_compression_level-t.cpp b/test/tap/tests/mysql-protocol_compression_level-t.cpp index 354d11744..307db5382 100644 --- a/test/tap/tests/mysql-protocol_compression_level-t.cpp +++ b/test/tap/tests/mysql-protocol_compression_level-t.cpp @@ -88,25 +88,6 @@ uint64_t measure_avg_query_time( return avg; } -MYSQL* init_mysql_conn(char* host, char* user, char* pass, int port, bool cmp=false) { - diag("Creating MySQL conn host=\"%s\" port=\"%d\" cmp=\"%d\"", user, port, cmp); - - MYSQL* mysql = mysql_init(NULL); - - if (!mysql) { - return nullptr; - } - if (cmp) { - if (mysql_options(mysql, MYSQL_OPT_COMPRESS, nullptr)) { - return nullptr; - } - } - if (!mysql_real_connect(mysql, host, user, pass, NULL, port, NULL, 0)) { - return nullptr; - } - - return mysql; -} const char version_comment_query[] { "select @@version_comment limit 1" }; @@ -140,13 +121,13 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - MYSQL* admin = init_mysql_conn(cl.host, cl.admin_username, cl.admin_password, cl.admin_port); + MYSQL* admin = init_mysql_conn(cl.host, cl.admin_port, cl.admin_username, cl.admin_password); if (!admin) { fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(admin)); return EXIT_FAILURE; } - MYSQL* proxy = init_mysql_conn(cl.host, cl.username, cl.password, cl.port); + MYSQL* proxy = init_mysql_conn(cl.host, cl.port, cl.username, cl.password); if (!proxy) { fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(admin)); return EXIT_FAILURE; @@ -185,17 +166,17 @@ int main(int argc, char** argv) { " (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4) as u4;" ); - MYSQL* proxy_cmp = init_mysql_conn(cl.host, cl.username, cl.password, cl.port, true); + MYSQL* proxy_cmp = init_mysql_conn(cl.host, cl.port, cl.username, cl.password, false, true); if (!proxy_cmp) { fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxy_cmp)); return EXIT_FAILURE; } - MYSQL* mysql = init_mysql_conn(cl.host, cl.username, cl.password, cl.mysql_port, false); + MYSQL* mysql = init_mysql_conn(cl.host, cl.mysql_port, cl.username, cl.password, false, false); if (!mysql) { fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(mysql)); return EXIT_FAILURE; } - MYSQL* mysql_cmp = init_mysql_conn(cl.host, cl.username, cl.password, cl.mysql_port, true); + MYSQL* mysql_cmp = init_mysql_conn(cl.host, cl.mysql_port, cl.username, cl.password, false, true); if (!mysql_cmp) { fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(mysql_cmp)); return EXIT_FAILURE; diff --git a/test/tap/tests/test_cluster1-t.cpp b/test/tap/tests/test_cluster1-t.cpp index 089c4bd82..2495ae9cb 100644 --- a/test/tap/tests/test_cluster1-t.cpp +++ b/test/tap/tests/test_cluster1-t.cpp @@ -26,11 +26,6 @@ * 127.0.0.1:26009 : satellite node6 */ -int run_q(MYSQL *mysql, const char *q) { - MYSQL_QUERY(mysql,q); - return 0; -} - void get_time(std::string& s) { time_t __timer; char __buffer[30];