From e6fca9b50d10a9d0904df2f5b4d60b7bd614771f Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Wed, 17 Dec 2014 13:44:44 +0200 Subject: [PATCH] Added a simple test for dbshard router. The test sets a session variable and queries a union of this variable and a value in each of the databases. --- macros.cmake | 3 + server/modules/routing/CMakeLists.txt | 1 + .../routing/dbshard/test/CMakeLists.txt | 5 + .../routing/dbshard/test/testdbshard.c | 208 ++++++++++++++++++ server/test/MaxScale_test.cnf | 13 ++ 5 files changed, 230 insertions(+) create mode 100644 server/modules/routing/dbshard/test/CMakeLists.txt create mode 100644 server/modules/routing/dbshard/test/testdbshard.c diff --git a/macros.cmake b/macros.cmake index 09e0e8146..8431933e9 100644 --- a/macros.cmake +++ b/macros.cmake @@ -32,6 +32,9 @@ macro(set_variables) # port of read/write split router module set(TEST_PORT_RW "4006" CACHE STRING "port of read/write split router module") + # port of dbshard router module + set(TEST_PORT_DB "4010" CACHE STRING "port of dbshard router module") + # port of read/write split router module with hints set(TEST_PORT_RW_HINT "4006" CACHE STRING "port of read/write split router module with hints") diff --git a/server/modules/routing/CMakeLists.txt b/server/modules/routing/CMakeLists.txt index f4be2ffca..4fe543400 100644 --- a/server/modules/routing/CMakeLists.txt +++ b/server/modules/routing/CMakeLists.txt @@ -23,6 +23,7 @@ target_link_libraries(cli log_manager utils) install(TARGETS cli DESTINATION modules) add_subdirectory(readwritesplit) +add_subdirectory(dbshard/test) if(BUILD_BINLOG) add_subdirectory(binlog) endif() diff --git a/server/modules/routing/dbshard/test/CMakeLists.txt b/server/modules/routing/dbshard/test/CMakeLists.txt new file mode 100644 index 000000000..4c41a3bad --- /dev/null +++ b/server/modules/routing/dbshard/test/CMakeLists.txt @@ -0,0 +1,5 @@ +if(MYSQLCLIENT_FOUND AND BUILD_TESTS) + add_executable(testdbshard testdbshard.c) + target_link_libraries(testdbshard ${MYSQLCLIENT_LIBRARIES} ssl crypto dl z m rt pthread) + add_test(NAME DBShardTest COMMAND $ ${TEST_HOST} 3000 ${TEST_USER} ${TEST_PASSWORD}) +endif() diff --git a/server/modules/routing/dbshard/test/testdbshard.c b/server/modules/routing/dbshard/test/testdbshard.c new file mode 100644 index 000000000..b9eb852b3 --- /dev/null +++ b/server/modules/routing/dbshard/test/testdbshard.c @@ -0,0 +1,208 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + + /** + * This test sets a session variable, creates tables in each of the shards + * and inserts into them a single value while querying the session variable. + * This will show if the value in the session variable in the shard is set and + * if it is the same in all the shards. + * + * The test fails if any of the session variables is not set or differ from the original value. + */ + + + MYSQL* server; + MYSQL_RES *result,*shdres; + MYSQL_ROW row; + char *host = NULL,*username = NULL, *password = NULL; + char query[2048]; + unsigned int port,errnum; + unsigned long *lengths; + int rval; + + if(argc < 5) + { + fprintf(stderr,"Usage: %s \n",argv[0]); + return 1; + } + + host = strdup(argv[1]); + port = atoi(argv[2]); + username = strdup(argv[3]); + password = strdup(argv[4]); + rval = 0; + + if((server = mysql_init(NULL)) == NULL){ + fprintf(stderr,"Error : Initialization of MySQL client failed.\n"); + rval = 1; + goto report; + } + + if(mysql_real_connect(server,host,username,password,NULL,port,NULL,0) == NULL){ + fprintf(stderr, "Failed to connect to database: %s\n", + mysql_error(server)); + rval = 1; + goto report; + } + + if(mysql_real_query(server, + "SET @test=123", + strlen("SET @test=123"))) + { + fprintf(stderr, "Failed to set session variable: %s.\n", + mysql_error(server)); + rval = 1; + goto report; + } + + if((result = mysql_list_dbs(server,NULL)) == NULL){ + fprintf(stderr, "Failed to query databases: %s\n", + mysql_error(server)); + rval = 1; + goto report; + } + + if(mysql_field_count(server) != 1) + { + fprintf(stderr, "SHOW DATABASES returned an unexpected result.\n"); + rval = 1; + goto report; + } + + while((row = mysql_fetch_row(result))) + { + char* dbname = strdup(row[0]); + printf("Testing database %-32s",dbname); + sprintf(query,"DROP TABLE IF EXISTS %s.t1",dbname); + + if(mysql_real_query(server,(const char*)query,strlen(query))) + { + errnum = mysql_errno(server); + + if(errnum != ER_DBACCESS_DENIED_ERROR && + errnum != ER_ACCESS_DENIED_ERROR) + { + fprintf(stderr, "DROP TABLE failed in %s: %d: %s.\n",dbname,mysql_errno(server),mysql_error(server)); + } + printf("NO PERMISSION\n"); + continue; + } + + + sprintf(query,"CREATE TABLE %s.t1 (id INT)",dbname); + + if(mysql_real_query(server,(const char*)query,strlen(query))) + { + errnum = mysql_errno(server); + if( errnum == ER_TABLEACCESS_DENIED_ERROR) + { + sprintf(query,"DROP TABLE IF EXISTS %s.t1",dbname); + mysql_real_query(server,(const char*)query,strlen(query)); + printf("NO PERMISSION\n"); + continue; + } + + fprintf(stderr, "CREATE TABLE failed in %s: %d: %s.\n", + dbname,mysql_errno(server),mysql_error(server)); + rval = 1; + goto report; + } + + sprintf(query,"INSERT INTO %s.t1 VALUES (1);",dbname); + + if(mysql_real_query(server,(const char*)query,strlen(query))) + { + fprintf(stderr, "Query to server failed: %d: %s.\n", + mysql_errno(server),mysql_error(server)); + rval = 1; + goto report; + } + + sprintf(query,"SELECT ID FROM %s.t1 UNION SELECT @test",dbname); + + if(mysql_real_query(server,(const char*)query,strlen(query))) + { + fprintf(stderr, "Query to server failed: %d: %s.\n", + mysql_errno(server),mysql_error(server)); + rval = 1; + goto report; + } + + if((shdres = mysql_store_result(server)) == NULL) + { + fprintf(stderr, "Failed to fetch result set: %d: %s\n", + mysql_errno(server),mysql_error(server)); + rval = 1; + goto report; + } + + if(mysql_field_count(server) != 1) + { + fprintf(stderr, "Returned field count value did not match the expected value.\n"); + rval = 1; + goto report; + + } + + /**Fetch the two rows, the inserted value and the session variable*/ + if(mysql_fetch_row(shdres) == NULL || + (row = mysql_fetch_row(shdres)) == NULL ) + { + fprintf(stderr, "Number of returned rows did not match the expected value.\n"); + rval = 1; + goto report; + } + + if((lengths = mysql_fetch_lengths(shdres)) == NULL) + { + fprintf(stderr, "Failed to retrieve row lengths: %d: %s.\n", + mysql_errno(server),mysql_error(server)); + rval = 1; + goto report; + } + if(lengths[0] != 3 || strcmp(row[0],"123")) + { + + rval = 1; + printf(" FAILED\n"); + printf( "Reason: Session variable was %s instead of \"123\".\n",row[0]); + } + else + { + printf("OK\n"); + } + + + sprintf(query,"DROP TABLE %s.t1;",dbname); + + if(mysql_real_query(server,(const char*)query,strlen(query))) + { + fprintf(stderr, "Query to server failed: %s.\n",mysql_error(server)); + rval = 1; + goto report; + } + + free(dbname); + mysql_free_result(shdres); + } + mysql_free_result(result); + + mysql_close(server); + + report: + + if(rval){ + printf("\nTest failed: Errors during test run.\n"); + } + free(host); + free(username); + free(password); + return rval; +} diff --git a/server/test/MaxScale_test.cnf b/server/test/MaxScale_test.cnf index 7c2e8972e..494f718a2 100644 --- a/server/test/MaxScale_test.cnf +++ b/server/test/MaxScale_test.cnf @@ -57,6 +57,13 @@ user=maxuser passwd=maxpwd filters=Hint +[DBShard Router] +type=service +router=dbshard +servers=server1,server2,server3,server4 +user=maxuser +passwd=maxpwd + [RW Split Hint Router] type=service router=readwritesplit @@ -109,6 +116,12 @@ service=RW Split Router protocol=MySQLClient port=4006 +[DBShard Listener] +type=listener +service=DBShard Router +protocol=MySQLClient +port=4010 + [RW Split Hint Listener] type=listener service=RW Split Hint Router