From 45f6ad8f650d6a5266e1a9347bcc032f64daea13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Tue, 6 Jul 2021 16:13:02 +0200 Subject: [PATCH] Adding test for SHOW TABLE STATUS in Admin #3511 --- lib/ProxySQL_Admin.cpp | 14 ++- test/tap/tests/admin_show_table_status-t.cpp | 90 ++++++++++++++++++++ 2 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 test/tap/tests/admin_show_table_status-t.cpp diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 549b84764..fbe47c72e 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -3386,7 +3386,7 @@ SQLite3_result * ProxySQL_Admin::generate_show_table_status(const char *tablenam tn[j]=0; SQLite3_result *resultset=NULL; char *q1=(char *)"PRAGMA table_info(%s)"; - char *q2=(char *)malloc(strlen(q1)+strlen(tn)); + char *q2=(char *)malloc(strlen(q1)+strlen(tn)+32); sprintf(q2,q1,tn); int affected_rows; int cols; @@ -3415,7 +3415,6 @@ SQLite3_result * ProxySQL_Admin::generate_show_table_status(const char *tablenam *err=strdup((char *)"Table does not exist"); return NULL; } - free(q2); SQLite3_result *result=new SQLite3_result(18); result->add_column_definition(SQLITE_TEXT,"Name"); result->add_column_definition(SQLITE_TEXT,"Engine"); @@ -3439,7 +3438,14 @@ SQLite3_result * ProxySQL_Admin::generate_show_table_status(const char *tablenam pta[1]=(char *)"SQLite"; pta[2]=(char *)"10"; pta[3]=(char *)"Dynamic"; - pta[4]=(char *)"10"; + delete resultset; + sprintf(q2,"SELECT COUNT(*) FROM %s",tn); + admindb->execute_statement(q2, &error , &cols , &affected_rows , &resultset); + char buf[20]; + sprintf(buf,"%d",resultset->rows_count); + pta[4]=buf; + delete resultset; + free(q2); pta[5]=(char *)"0"; pta[6]=(char *)"0"; pta[7]=(char *)"0"; @@ -4008,7 +4014,7 @@ void admin_session_handler(MySQL_Session *sess, void *_pa, PtrSize_t *pkt) { query_length=strlen(query)+1; goto __run_query; } - if (!strncmp("show table status like '", query_no_space, strlen("show table status like '"))) { + if (!strncasecmp("show table status like '", query_no_space, strlen("show table status like '"))) { char *strA=query_no_space+24; int strAl=strlen(strA); if (strAl<2) { // error diff --git a/test/tap/tests/admin_show_table_status-t.cpp b/test/tap/tests/admin_show_table_status-t.cpp new file mode 100644 index 000000000..ca191ac86 --- /dev/null +++ b/test/tap/tests/admin_show_table_status-t.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "tap.h" +#include "command_line.h" +#include "utils.h" + +using std::string; + +/* this test: + * enables mysql-have_ssl + * retrieves all tables in the most important schemas +*/ + +int main() { + CommandLine cl; + + if (cl.getEnv()) { + diag("Failed to get the required environmental variables."); + return -1; + } + + + MYSQL* proxysql_admin = mysql_init(NULL); + // Initialize connections + if (!proxysql_admin) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin)); + return -1; + } + + if (!mysql_real_connect(proxysql_admin, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin)); + return -1; + } + + MYSQL_QUERY(proxysql_admin, "SET mysql-have_ssl='true'"); + MYSQL_QUERY(proxysql_admin, "SET mysql-have_compress='true'"); + MYSQL_QUERY(proxysql_admin, "LOAD MYSQL VARIABLES TO RUNTIME"); + + std::vector tables; + std::string q = "SHOW TABLES"; + MYSQL_QUERY(proxysql_admin, q.c_str()); + + MYSQL_RES* proxy_res = mysql_store_result(proxysql_admin); + MYSQL_ROW row; + while ((row = mysql_fetch_row(proxy_res))) { + std::string table(row[0]); + tables.push_back(table); + } + mysql_free_result(proxy_res); + mysql_close(proxysql_admin); + std::vector queries = { + "show table status like '%s'", + "show TABLE status like '%s'", + "SHOW table status like '%s'", + "show TABLE status LIKE '%s'", + }; + plan(tables.size()*queries.size()); + + + for (std::vector::iterator it = tables.begin(); it != tables.end(); it++) { + MYSQL* proxysql_admin = mysql_init(NULL); // redefined locally + mysql_ssl_set(proxysql_admin, NULL, NULL, NULL, NULL, NULL); + if (!mysql_real_connect(proxysql_admin, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, CLIENT_SSL|CLIENT_COMPRESS)) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin)); + return -1; + } + char *query = (char *) malloc(strlen(queries[0]) + it->length() + 8); + for (std::vector::iterator it2 = queries.begin(); it2 != queries.end(); it2++) { + sprintf(query,*it2, it->c_str()); + MYSQL_QUERY(proxysql_admin, query); + MYSQL_RES* proxy_res = mysql_store_result(proxysql_admin); + unsigned long rows = proxy_res->row_count; + ok(rows = 1 , "SHOW TABLE STATUS %s generated %lu row(s)", it->c_str(), rows); + mysql_free_result(proxy_res); + } + free(query); + mysql_close(proxysql_admin); + } + + return exit_status(); +}