Skip to content

Commit

Permalink
Added test for 'COM_RESET_CONNECTION' and 'COM_CHANGE_USER' and refac…
Browse files Browse the repository at this point in the history
…tored multiple tests functions #2021
  • Loading branch information
JavierJF committed Dec 2, 2021
1 parent da8d343 commit 6188f91
Show file tree
Hide file tree
Showing 7 changed files with 1,399 additions and 152 deletions.
14 changes: 12 additions & 2 deletions test/tap/tap/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ CURL_DIR=$(DEPS_PATH)/curl/curl
CURL_IDIR=$(CURL_DIR)/include
CURL_LDIR=$(CURL_DIR)/lib/.libs

CURL_DIR=$(DEPS_PATH)/curl/curl
CURL_IDIR=$(CURL_DIR)/include
CURL_LDIR=$(CURL_DIR)/lib/.libs

IDIR=../../../include
LDIR=../../../lib

LIBPROXYSQLAR=$(LDIR)/libproxysql.a
INCLUDEDIRS=-I$(IDIR) -I$(JSON_IDIR) -I$(MARIADB_IDIR)

.PHONY: all
all: libtap.a

Expand All @@ -21,5 +31,5 @@ debug: OPT = -O0 -DDEBUG -ggdb
debug: libtap.a

libtap.a: tap.cpp tap.h command_line.cpp command_line.h utils.cpp utils.h
g++ -c tap.cpp command_line.cpp utils.cpp -std=c++11 -I$(JSON_IDIR) -I$(MARIADB_IDIR) $(OPT)
ar rcs libtap.a tap.o command_line.o utils.o
g++ -c tap.cpp command_line.cpp utils.cpp -std=c++11 $(INCLUDEDIRS) $(OPT)
ar rcs libtap.a tap.o command_line.o utils.o $(LIBPROXYSQLAR)
112 changes: 112 additions & 0 deletions test/tap/tap/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <algorithm>
#include <chrono>
#include <string>
#include <cstring>
#include <fcntl.h>
#include <iostream>
Expand All @@ -14,6 +15,13 @@
#include "tap.h"
#include "utils.h"

#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <iostream>

#include "proxysql_utils.h"

int show_variable(MYSQL *mysql, const std::string& var_name, std::string& var_value) {
char query[128];

Expand Down Expand Up @@ -673,3 +681,107 @@ int wait_for_replication(

return result;
}

MARIADB_CHARSET_INFO * proxysql_find_charset_collate(const char *collatename) {
MARIADB_CHARSET_INFO *c = (MARIADB_CHARSET_INFO *)mariadb_compiled_charsets;
do {
if (!strcasecmp(c->name, collatename)) {
return c;
}
++c;
} while (c[0].nr != 0);
return NULL;
}

int create_proxysql_user(
MYSQL* proxysql_admin,
const std::string& user,
const std::string& pass,
const std::string& attributes
) {
std::string t_del_user_query { "DELETE FROM mysql_users WHERE username='%s'" };
std::string del_user_query {};
string_format(t_del_user_query, del_user_query, user.c_str());

std::string t_insert_user {
"INSERT INTO mysql_users (username,password,active,attributes)"
" VALUES ('%s','%s',1,'%s')"
};
std::string insert_user {};
string_format(t_insert_user, insert_user, user.c_str(), pass.c_str(), attributes.c_str());

MYSQL_QUERY(proxysql_admin, del_user_query.c_str());
MYSQL_QUERY(proxysql_admin, insert_user.c_str());

return EXIT_SUCCESS;
}

int create_mysql_user(
MYSQL* mysql_server,
const std::string& user,
const std::string& pass
) {
const std::string t_drop_user_query { "DROP USER IF EXISTS %s@'%%'" };
std::string drop_user_query {};
string_format(t_drop_user_query, drop_user_query, user.c_str());

const std::string t_create_user_query {
"CREATE USER IF NOT EXISTS %s@'%%' IDENTIFIED WITH 'mysql_native_password' BY \"%s\""
};
std::string create_user_query {};
string_format(t_create_user_query, create_user_query, user.c_str(), pass.c_str());

const std::string t_grant_all_query { "GRANT ALL ON *.* TO %s@'%%'" };
std::string grant_all_query { };
string_format(t_grant_all_query, grant_all_query, user.c_str());

MYSQL_QUERY(mysql_server, drop_user_query.c_str());
MYSQL_QUERY(mysql_server, create_user_query.c_str());
MYSQL_QUERY(mysql_server, grant_all_query.c_str());

return EXIT_SUCCESS;
}

int create_extra_users(
MYSQL* proxysql_admin,
MYSQL* mysql_server,
const std::vector<user_config>& users_config
) {
std::vector<std::pair<std::string, std::string>> v_user_pass {};
std::transform(
std::begin(users_config),
std::end(users_config),
std::back_inserter(v_user_pass),
[](const user_config& u_config) {
return std::pair<std::string, std::string> {
std::get<0>(u_config),
std::get<1>(u_config)
};
}
);

// create the MySQL users
for (const auto& user_pass : v_user_pass) {
int c_user_res =
create_mysql_user(mysql_server, user_pass.first, user_pass.second);
if (c_user_res) {
return c_user_res;
}
}

// create the ProxySQL users
for (const auto& user_config : users_config) {
int c_p_user_res =
create_proxysql_user(
proxysql_admin,
std::get<0>(user_config),
std::get<1>(user_config),
std::get<2>(user_config)
);
if (c_p_user_res) {
return c_p_user_res;
}
}

return EXIT_SUCCESS;
}
53 changes: 53 additions & 0 deletions test/tap/tap/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,57 @@ int wait_for_replication(
MYSQL* proxy, MYSQL* proxy_admin, const std::string& check, uint32_t timeout, uint32_t reader_hg
);

/**
* NOTE: This is a duplicate of 'proxysql_find_charset_collate' in 'MySQL_Variables.h'. Including
* 'MySQL_Variables' is not a easy task due to its interdependeces with other ProxySQL modules.
*/
MARIADB_CHARSET_INFO * proxysql_find_charset_collate(const char *collatename);

/**
* @brief Creates the new supplied user in ProxySQL with the provided
* attributes.
*
* @param proxysql_admin An already opened connection to ProxySQL Admin.
* @param user The username of the user to be created.
* @param pass The password of the user to be created.
* @param attributes The 'attributes' value for the 'attributes' column
* for the user to be created.
*
* @return EXIT_SUCCESS in case of success, EXIT_FAILURE otherwise.
*/
int create_proxysql_user(
MYSQL* proxysql_admin, const std::string& user, const std::string& pass, const std::string& attributes
);

/**
* @brief Create a MySQL user for testing purposes in the server determined
* by supplied *already established* MySQL connection.
*
* @param mysql_server An already opened connection to a MySQL server.
* @param user The name of the user to be created.
* @param pass The password for the user to be created.
*
* @return EXIT_SUCCESS in case of success, EXIT_FAILURE otherwise.
*/
int create_mysql_user(MYSQL* mysql_server, const std::string& user, const std::string& pass);

using user_config = std::tuple<std::string, std::string, std::string>;

/**
* @brief Create the extra required users for the test in
* both MYSQL and ProxySQL.
*
* @param proxysql_admin An already opened connection to ProxySQL admin
* interface.
* @param mysql_server An already opened connection to a backend MySQL
* server.
* @param user_attributes The user attributes whose should be part of user
* configuration in ProxySQL side.
*
* @return EXIT_SUCCESS in case of success, EXIT_FAILURE otherwise.
*/
int create_extra_users(
MYSQL* proxysql_admin, MYSQL* mysql_server, const std::vector<user_config>& users_config
);

#endif // #define UTILS_H
3 changes: 0 additions & 3 deletions test/tap/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,5 @@ test_admin_prometheus_metrics_dump-t: test_admin_prometheus_metrics_dump-t.cpp $
create_connection_annotation: test_connection_annotation-t.cpp
g++ -DTEST_AURORA -DDEBUG test_connection_annotation-t.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) -std=c++11 $(OBJ) $(MYLIBS) -ltap -ldl $(STATIC_LIBS) -o test_connection_annotation-t -DGITVERSION=\"$(GIT_VERSION)\"

test_set_collation-t: test_set_collation-t.cpp $(TAP_LIBDIR)/libtap.a
g++ test_set_collation-t.cpp $(INCLUDEDIRS) $(LDIRS) $(OPT) -std=c++11 $(MYLIBS) -ltap -Wl,--no-as-needed -ldl -lpthread -o test_set_collation-t -DGITVERSION=\"$(GIT_VERSION)\"

setparser_test: setparser_test.cpp $(TAP_LIBDIR)/libtap.a $(RE2_PATH)/util/test.cc $(LDIR)/set_parser.cpp $(LIBPROXYSQLAR)
g++ -DDEBUG setparser_test.cpp $(RE2_PATH)/util/test.cc ../../../src/obj/proxysql_global.o $(INCLUDEDIRS) $(LDIRS) $(OPT) -std=c++11 -lproxysql $(MYLIBS) -ltap -ldl -lpthread $(WASAN) -o setparser_test -DGITVERSION=\"$(GIT_VERSION)\"
Loading

0 comments on commit 6188f91

Please sign in to comment.