Fix connect_to_nonexisting_db

The test dropped the 'test' database which is heavily used both for
checking functionality and running tests. A better alternative is to use a
custom database that is only used by this test.
This commit is contained in:
Markus Mäkelä
2017-06-30 18:41:13 +03:00
parent 322fae8326
commit 31504585fc

View File

@ -1,66 +1,85 @@
/** /**
* @file connect_to_nonexisting_db.cpp Tries to connect to non existing DB, expects no crash * @file connect_to_nonexisting_db.cpp Tries to connect to non existing DB, expects no crash
*/ */
// some relations to bug#425
#include <iostream>
#include "testconnections.h" #include "testconnections.h"
#include "sql_t1.h" #include "sql_t1.h"
using namespace std; bool try_connect(TestConnections& test)
{
const char* ip = test.maxscale_IP;
const char* user = test.maxscale_user;
const char* pw = test.maxscale_password;
const char* db = "test_db";
MYSQL* rwsplit = open_conn_db(test.rwsplit_port, ip, db, user, pw, false);
MYSQL* master = open_conn_db(test.readconn_master_port, ip, db, user, pw, false);
MYSQL* slave = open_conn_db(test.readconn_slave_port, ip, db, user, pw, false);
bool rval = false;
if (rwsplit && master && slave &&
execute_query(rwsplit, "SELECT 1") == 0 &&
execute_query(master, "SELECT 1") == 0 &&
execute_query(slave, "SELECT 1") == 0)
{
rval = true;
}
mysql_close(rwsplit);
mysql_close(master);
mysql_close(slave);
return rval;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
TestConnections * Test = new TestConnections(argc, argv); TestConnections test(argc, argv);
Test->set_timeout(30);
Test->tprintf("Connecting to RWSplit\n"); test.tprintf("Connecting to RWSplit");
Test->conn_rwsplit = open_conn_no_db(Test->rwsplit_port, Test->maxscale_IP, Test->maxscale_user,
Test->maxscale_password, Test->ssl); test.set_timeout(30);
if (Test->conn_rwsplit == NULL) MYSQL* conn = open_conn_no_db(test.rwsplit_port, test.maxscale_IP, test.maxscale_user,
{ test.maxscale_password, test.ssl);
Test->add_result(1, "Error connecting to MaxScale\n"); test.add_result(conn == NULL, "Error connecting to MaxScale");
delete Test;
return 1; test.tprintf("Removing 'test_db' DB");
} execute_query(conn, "DROP DATABASE IF EXISTS test_db");
Test->tprintf("Removing 'test' DB\n"); test.tprintf("Closing connections and waiting 5 seconds");
execute_query(Test->conn_rwsplit, (char *) "DROP DATABASE IF EXISTS test;"); mysql_close(conn);
Test->tprintf("Closing connections and waiting 5 seconds\n"); test.stop_timeout();
Test->close_rwsplit();
sleep(5); sleep(5);
Test->tprintf("Connection to non-existing DB (all routers)\n"); test.set_timeout(30);
Test->connect_maxscale(); test.tprintf("Connection to non-existing DB (all routers)");
Test->close_maxscale_connections(); test.add_result(try_connect(test), "Connection with dropped database should fail");
Test->tprintf("Connecting to RWSplit again to recreate 'test' db\n"); test.tprintf("Connecting to RWSplit again to recreate 'test_db' db");
Test->conn_rwsplit = open_conn_no_db(Test->rwsplit_port, Test->maxscale_IP, Test->maxscale_user, conn = open_conn_no_db(test.rwsplit_port, test.maxscale_IP, test.maxscale_user,
Test->maxscale_password, Test->ssl); test.maxscale_password, test.ssl);
if (Test->conn_rwsplit == NULL) test.add_result(conn == NULL, "Error connecting to MaxScale");
{
printf("Error connecting to MaxScale\n");
delete Test;
return 1;
}
Test->tprintf("Creating and selecting 'test' DB\n"); test.tprintf("Creating and selecting 'test_db' DB");
Test->try_query(Test->conn_rwsplit, (char *) "CREATE DATABASE test; USE test"); test.try_query(conn, "CREATE DATABASE test_db");
Test->tprintf("Creating 't1' table\n"); test.try_query(conn, "USE test_db");
Test->add_result(create_t1(Test->conn_rwsplit), "Error creation 't1'\n"); test.tprintf("Creating 't1' table");
Test->close_rwsplit(); test.add_result(create_t1(conn), "Error creation 't1'");
mysql_close(conn);
Test->tprintf("Reconnectiong\n"); test.tprintf("Reconnectiong");
Test->add_result(Test->connect_maxscale(), "error connection to Maxscale\n"); test.add_result(!try_connect(test), "Error connecting to Maxscale");
Test->tprintf("Trying simple operations with t1 \n");
Test->try_query(Test->conn_rwsplit, (char *) "INSERT INTO t1 (x1, fl) VALUES(0, 1);");
Test->set_timeout(240);
Test->add_result(execute_select_query_and_check(Test->conn_rwsplit, (char *) "SELECT * FROM t1;", 1),
"Error execution SELECT * FROM t1;\n");
Test->close_maxscale_connections();
int rval = Test->global_result; test.tprintf("Trying simple operations with t1 ");
delete Test; conn = open_conn_no_db(test.rwsplit_port, test.maxscale_IP, test.maxscale_user,
return rval; test.maxscale_password, test.ssl);
test.try_query(conn, "USE test_db");
test.try_query(conn, "INSERT INTO t1 (x1, fl) VALUES(0, 1)");
test.set_timeout(60);
test.add_result(execute_select_query_and_check(conn, "SELECT * FROM t1", 1),
"Error execution SELECT * FROM t1;");
mysql_close(conn);
return test.global_result;
} }