From 9503b92d36cbe9eedefdfbab774164640fa597cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Tue, 4 Aug 2020 19:17:57 +0200 Subject: [PATCH] Added simple test for checking GTID forwarding to client --- test/tap/tests/Makefile | 3 + test/tap/tests/test_gtid_forwarding-t.cpp | 117 ++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 test/tap/tests/test_gtid_forwarding-t.cpp diff --git a/test/tap/tests/Makefile b/test/tap/tests/Makefile index 1cab6ed7e..e5e06bbe0 100644 --- a/test/tap/tests/Makefile +++ b/test/tap/tests/Makefile @@ -103,3 +103,6 @@ aurora: aurora.cpp $(TAP_LIBDIR)/libtap.a test_tokenizer-t: test_tokenizer-t.cpp $(TAP_LIBDIR)/libtap.a g++ test_tokenizer-t.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) -std=c++11 $(MYLIBS) -lproxysql -ltap -Wl,--no-as-needed -ldl -lpthread -o test_tokenizer-t -DGITVERSION=\"$(GIT_VERSION)\" + +test_gtid_forwarding-t: test_gtid_forwarding-t.cpp $(TAP_LIBDIR)/libtap.a + g++ test_gtid_forwarding-t.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) -std=c++11 $(MYLIBS) -ltap -Wl,--no-as-needed -ldl -lpthread -o test_gtid_forwarding-t -DGITVERSION=\"$(GIT_VERSION)\" diff --git a/test/tap/tests/test_gtid_forwarding-t.cpp b/test/tap/tests/test_gtid_forwarding-t.cpp new file mode 100644 index 000000000..e3f7976ce --- /dev/null +++ b/test/tap/tests/test_gtid_forwarding-t.cpp @@ -0,0 +1,117 @@ +/** + * @file test_gtid_forwarding-t.cpp + * @brief This test file checks several simple functionalities: + * - That GTIDs are properly forwarded to the client. + * - That GTIDs format is valid. + * - That GTIDs incremental sequence number is indeed incremental. + * - That resulsets from simple queries is not broken. + */ + +#include +#include +#include + +#include +#include + +#include +#include + +#include "tap.h" +#include "command_line.h" +#include "utils.h" +#include "proxysql_utils.h" +#include "re2/re2.h" + +int main(int, char**) { + CommandLine cl; + + if (cl.getEnv()) { + diag("Failed to get the required environmental variables."); + return -1; + } + + MYSQL* proxysql_mysql = mysql_init(NULL); + + // Initialize connections + if (!proxysql_mysql) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_mysql)); + return -1; + } + + if (!mysql_real_connect(proxysql_mysql, cl.host, cl.username, cl.password, NULL, cl.port, NULL, 0)) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_mysql)); + return -1; + } + + MYSQL_QUERY(proxysql_mysql, "CREATE DATABASE IF NOT EXISTS test"); + MYSQL_QUERY(proxysql_mysql, "CREATE TABLE IF NOT EXISTS test.gtid_forwarding_test (id INT NOT NULL)"); + MYSQL_QUERY(proxysql_mysql, "SET SESSION_TRACK_GTIDS=OWN_GTID"); + + uint last_id { 0 }; + + for (uint32_t i = 0; i < 1000; i++) { + // Simple select to verify resultset is being returned properly + MYSQL_QUERY(proxysql_mysql, "SELECT 1"); + MYSQL_RES* select_res = mysql_store_result(proxysql_mysql); + int field_count = mysql_field_count(proxysql_mysql); + int row_count = mysql_num_rows(select_res); + + if (field_count == 1 && row_count == 1) { + MYSQL_ROW row = mysql_fetch_row(select_res); + ok(atoi(row[0]) == 1, "Resulset from simple query 'SELECT 1' should be well-formed."); + } else { + ok(false, "Resultset from simple query 'SELECT 1' query have an invalid number of fields."); + } + + mysql_free_result(select_res); + + const char* t_insert_query = "INSERT INTO test.gtid_forwarding_test VALUES (%i)"; + std::string insert_query {}; + string_format(t_insert_query, insert_query, i); + MYSQL_QUERY(proxysql_mysql, insert_query.c_str()); + + std::string s_gtid_uuid {}; + + // Read the returned GTID + if (proxysql_mysql->server_status & SERVER_SESSION_STATE_CHANGED) { + const char *data { nullptr }; + size_t length { 0 }; + char gtid_uuid[128] = { 0 }; + + if (mysql_session_track_get_first(proxysql_mysql, SESSION_TRACK_GTIDS, &data, &length) == 0) { + if (length >= (sizeof(gtid_uuid) - 1)) { + length = sizeof(gtid_uuid) - 1; + } + if (memcmp(gtid_uuid, data, length)) { + memcpy(gtid_uuid, data, length); + gtid_uuid[length] = 0; + } + } + + if (gtid_uuid[0] != 0) { + s_gtid_uuid = gtid_uuid; + } + } + + // Verify the received GTID + if (!s_gtid_uuid.empty()) { + std::string s_id {}; + ok(re2::RE2::FullMatch(s_gtid_uuid, "[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}:([0-9]*)", &s_id), "'UIID' should have a valid format - %s", s_gtid_uuid.c_str()); + + // Check the incremental id. + uint new_id = (std::atoi(s_id.c_str())); + ok(last_id < new_id, "Last incremental id must be smaller than newer one: %d < %d", last_id, new_id); + + last_id = new_id; + } else { + ok(false, "'UUID' Should never be empty"); + break; + } + } + + MYSQL_QUERY(proxysql_mysql, "DROP TABLE test.gtid_forwarding_test"); + mysql_close(proxysql_mysql); + + return exit_status(); +} \ No newline at end of file