From d9139bb436fc47489e0df7df7c414ac836a2debb Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Tue, 10 Feb 2015 10:49:54 +0200 Subject: [PATCH] Added more tests for the dbshard router. --- .../routing/dbshard/test/CMakeLists.txt | 7 +- .../routing/dbshard/test/test.cmake.in | 11 +- .../routing/dbshard/test/testdbshard2.c | 225 ++++++++++++++++++ server/test/MaxScale_test.cnf | 1 + 4 files changed, 240 insertions(+), 4 deletions(-) create mode 100644 server/modules/routing/dbshard/test/testdbshard2.c diff --git a/server/modules/routing/dbshard/test/CMakeLists.txt b/server/modules/routing/dbshard/test/CMakeLists.txt index 3fe6dd388..b28a6e882 100644 --- a/server/modules/routing/dbshard/test/CMakeLists.txt +++ b/server/modules/routing/dbshard/test/CMakeLists.txt @@ -1,6 +1,9 @@ if(MYSQLCLIENT_FOUND AND BUILD_TESTS) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/test.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/test.cmake @ONLY) add_executable(testdbshard testdbshard.c) target_link_libraries(testdbshard ${MYSQLCLIENT_LIBRARIES} ssl crypto dl z m rt pthread) - configure_file(${CMAKE_CURRENT_SOURCE_DIR}/test.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/test.cmake @ONLY) - add_test(NAME DBShardTest COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/test.cmake) + add_executable(testdbshard2 testdbshard2.c) + target_link_libraries(testdbshard2 ${MYSQLCLIENT_LIBRARIES} ssl crypto dl z m rt pthread) + add_test(NAME TestDBShard COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/test.cmake) + endif() diff --git a/server/modules/routing/dbshard/test/test.cmake.in b/server/modules/routing/dbshard/test/test.cmake.in index fe0a8cab3..2694037c3 100644 --- a/server/modules/routing/dbshard/test/test.cmake.in +++ b/server/modules/routing/dbshard/test/test.cmake.in @@ -4,7 +4,14 @@ foreach(VAR ${DBSHARD_TEST_PORTS}) endforeach() execute_process(COMMAND ${CMAKE_CURRENT_BINARY_DIR}/testdbshard @TEST_HOST@ @TEST_PORT_DB@ @TEST_USER@ @TEST_PASSWORD@ RESULT_VARIABLE RVAL) if(RVAL EQUAL 0) - message("Test passed.") + message("Test 1 passed.") else() - message(FATAL_ERROR "Test failed.") + message(FATAL_ERROR "Test 1 failed with code ${RVAL}.") +endif() + +execute_process(COMMAND ${CMAKE_CURRENT_BINARY_DIR}/testdbshard2 @TEST_HOST@ @TEST_PORT_DB@ @TEST_USER@ @TEST_PASSWORD@ RESULT_VARIABLE RVAL2) +if(RVAL2 EQUAL 0) + message("Test 2 passed.") +else() + message(FATAL_ERROR "Test 2 failed with code ${RVAL2}.") endif() diff --git a/server/modules/routing/dbshard/test/testdbshard2.c b/server/modules/routing/dbshard/test/testdbshard2.c new file mode 100644 index 000000000..00c0a5bf6 --- /dev/null +++ b/server/modules/routing/dbshard/test/testdbshard2.c @@ -0,0 +1,225 @@ +#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. + */ + + const unsigned int ports[4] = { + 3000, + 3001, + 3002, + 3003 + }; + + const char* srv_id[4] = { + "3000", + "3001", + "3002", + "3003" + }; + + const char* databases[4] = { + "db0", + "db1", + "db2", + "db3" + }; + + MYSQL* server; + MYSQL_RES *result,*shdres; + MYSQL_ROW row; + char *host = NULL,*username = NULL, *password = NULL; + char query[2048]; + unsigned int port,errnum,optval; + unsigned long *lengths; + int rval, i, j; + + 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; + + for(i = 0;i<4;i++) + { + + if((server = mysql_init(NULL)) == NULL){ + fprintf(stderr,"Error : Initialization of MySQL client failed.\n"); + rval = 1; + goto report; + } + optval = 1; + mysql_options(server,MYSQL_OPT_CONNECT_TIMEOUT,&optval); + + if(mysql_real_connect(server,host,username,password,NULL,ports[i],NULL,0) == NULL){ + fprintf(stderr, "Failed to connect to server on port %d: %s\n", + ports[i], + mysql_error(server)); + rval = 1; + goto report; + } + + sprintf(query,"STOP SLAVE",databases[i]); + if(mysql_real_query(server,query,strlen(query))) + { + fprintf(stderr, "Failed to stop slave in %d: %s.\n", + ports[i], + mysql_error(server)); + } + + + for(j = 0;j<4;j++) + { + sprintf(query,"DROP DATABASE IF EXISTS %s",databases[j]); + if(mysql_real_query(server,query,strlen(query))) + { + fprintf(stderr, "Failed to drop database in %d: %s.\n", + ports[i], + mysql_error(server)); + } + + } + mysql_close(server); + } + + for(i=0;i<4;i++) + { + if((server = mysql_init(NULL)) == NULL){ + fprintf(stderr,"Error : Initialization of MySQL client failed.\n"); + rval = 1; + goto report; + } + + mysql_options(server,MYSQL_OPT_CONNECT_TIMEOUT,&optval); + + if(mysql_real_connect(server,host,username,password,NULL,ports[i],NULL,0) == NULL){ + fprintf(stderr, "Failed to connect to server on port %d: %s\n", + ports[i], + mysql_error(server)); + rval = 1; + goto report; + } + + sprintf(query,"CREATE DATABASE %s",databases[i]); + if(mysql_real_query(server,query,strlen(query))) + { + fprintf(stderr, "Failed to create table in %d: %s.\n", + ports[i], + mysql_error(server)); + rval = 1; + goto report; + } + + sprintf(query,"DROP TABLE IF EXISTS %s.t1",databases[i]); + if(mysql_real_query(server,query,strlen(query))) + { + fprintf(stderr, "Failed to drop table in %d: %s.\n", + ports[i], + mysql_error(server)); + } + + + sprintf(query,"CREATE TABLE %s.t1 (id int)",databases[i]); + if(mysql_real_query(server,query,strlen(query))) + { + fprintf(stderr, "Failed to create table in %d: %s.\n", + ports[i], + mysql_error(server)); + rval = 1; + goto report; + } + + sprintf(query,"INSERT INTO %s.t1 values (%s)",databases[i],srv_id[i]); + if(mysql_real_query(server,query,strlen(query))) + { + fprintf(stderr, "Failed to insert values in %d: %s.\n", + ports[i], + mysql_error(server)); + rval = 1; + goto report; + } + + mysql_close(server); + } + + for(i = 0;i<4;i++) + { + + printf("Testing server on port %d through MaxScale.\n",ports[i]); + 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,databases[i],port,NULL,0) == NULL){ + fprintf(stderr, "Failed to connect to port %d using database %s: %s\n", + port, + databases[i], + mysql_error(server)); + rval = 1; + goto report; + } + + if(mysql_real_query(server,"SELECT id FROM t1",strlen("SELECT id FROM t1"))) + { + fprintf(stderr, "Failed to execute query in %d: %s.\n", + ports[i], + mysql_error(server)); + rval = 1; + goto report; + } + + result = mysql_store_result(server); + + while((row = mysql_fetch_row(result))) + { + if(strcmp(row[0],srv_id[i])) + { + fprintf(stderr, "Test failed in %d: Was expecting %s but got %s instead.\n", + ports[i],srv_id[i],row[0]); + rval = 1; + + } + } + + mysql_free_result(result); + + if(i > 0 && mysql_real_query(server,"START SLAVE",strlen("START SLAVE"))) + { + fprintf(stderr, "Failed to start slave in %d: %s.\n", + ports[i], + mysql_error(server)); + } + 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 32e052c91..77d7903f3 100644 --- a/server/test/MaxScale_test.cnf +++ b/server/test/MaxScale_test.cnf @@ -23,6 +23,7 @@ router=dbshard servers=server1,server2,server3,server4 user=maxuser passwd=maxpwd +auth_all_servers=1 [RW Split Hint Router] type=service