int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;
    int i;

    Test->read_env();
    Test->print_env();

    printf("Connecting to all MaxScale services\n");
    fflush(stdout);
    global_result += Test->connect_maxscale();

    printf("executing show status 1000 times\n");
    fflush(stdout);


    for (i = 0; i < 1000; i++)  {
        global_result += execute_query(Test->conn_rwsplit, (char *) "show status");
    }
    for (i = 0; i < 1000; i++)  {
        global_result += execute_query(Test->conn_slave, (char *) "show status");
    }
    for (i = 0; i < 1000; i++)  {
        global_result += execute_query(Test->conn_master, (char *) "show status");
    }

    Test->close_maxscale_connections();

    check_maxscale_alive();

    Test->copy_all_logs();
    return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;

    Test->read_env();
    Test->print_env();


    Test->binlog_cmd_option = 2;
    Test->start_binlog();

    Test->repl->connect();

    create_t1(Test->repl->nodes[0]);
    global_result += insert_into_t1(Test->repl->nodes[0], 4);
    printf("Sleeping to let replication happen\n"); fflush(stdout);
    sleep(30);

    for (int i = 0; i < Test->repl->N; i++) {
        printf("Checking data from node %d (%s)\n", i, Test->repl->IP[i]); fflush(stdout);
        global_result += select_from_t1(Test->repl->nodes[i], 4);
    }

    Test->repl->close_connections();


    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;

    Test->read_env();
    Test->print_env();

    printf("Connecting to RWSplit %s\n", Test->maxscale_IP);
    Test->connect_rwsplit();

    printf("Setup firewall to block mysql on master\n"); fflush(stdout);
    Test->repl->block_node(0);

    printf("Trying query to RWSplit, expecting failure, but not a crash\n"); fflush(stdout);
    execute_query(Test->conn_rwsplit, (char *) "show processlist;");

    printf("Setup firewall back to allow mysql\n"); fflush(stdout);
    Test->repl->unblock_node(0);

    sleep(10);

    global_result += check_maxscale_alive();

    Test->close_rwsplit();


    printf("Reconnecting and trying query to RWSplit\n"); fflush(stdout);
    Test->connect_rwsplit();
    global_result += execute_query(Test->conn_rwsplit, (char *) "show processlist;");
    Test->close_rwsplit();

    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    int global_result = 0;
    int OldMaster;
    int NewMaster;

    if (argc !=3) {
        printf("Usage: change_master NewMasterNode OldMasterNode\n");
        exit(1);
    }
    TestConnections * Test = new TestConnections(argc, argv);
    Test->read_env();
    Test->print_env();

    sscanf(argv[1], "%d", &NewMaster);
    sscanf(argv[2], "%d", &OldMaster);

    printf("Changing master from node %d (%s) to node %d (%s)\n", OldMaster, Test->repl->IP[OldMaster], NewMaster, Test->repl->IP[NewMaster]);

    Test->repl->connect();
    Test->repl->change_master(NewMaster, OldMaster);
    Test->repl->close_connections();

    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;

    Test->read_env();
    Test->print_env();

    Test->connect_maxscale();

    printf("Trying SELECT @a:=@a+1 as a, test.b FROM test\n"); fflush(stdout);
    global_result += execute_query(Test->conn_rwsplit, "DROP TABLE IF EXISTS test; CREATE TABLE test (b integer);");
    for (int i=0; i<10000;i++) {execute_query(Test->conn_rwsplit, "insert into test value(2);");}
    if (execute_query(Test->conn_rwsplit, "SELECT @a:=@a+1 as a, test.b FROM test;") == 0) {
        printf("Query succeded, but expected to fail. Test FAILED!\n"); fflush(stdout);
        global_result++;
    }
    printf("Trying USE test\n"); fflush(stdout);
    global_result += execute_query(Test->conn_rwsplit, "USE test");

    global_result += execute_query(Test->conn_rwsplit, "DROP TABLE IF EXISTS test;");

    printf("Checking if MaxScale alive\n"); fflush(stdout);
    Test->close_maxscale_connections();

    printf("Checking logs\n"); fflush(stdout);
    global_result    += check_log_err((char *) "Warning : The query can't be routed to all backend servers because it includes SELECT and SQL variable modifications which is not supported", TRUE);
    global_result    += check_log_err((char *) "SELECT with session data modification is not supported if configuration parameter use_sql_variables_in=all", TRUE);

    global_result += check_maxscale_alive();

    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;
    int i;
    int N=4;

    Test->read_env();
    Test->print_env();


    for (i = 0; i < 4; i++) {
        Test->repl->connect();
        if (Test->connect_maxscale() !=0 ) {
            printf("Error connecting to MaxScale\n");
            exit(1);
        }

        global_result += insert_select(Test, N);

        printf("Creating database test1\n"); fflush(stdout);
        global_result += execute_query(Test->conn_rwsplit, "DROP TABLE t1");
        global_result += execute_query(Test->conn_rwsplit, "DROP DATABASE IF EXISTS test1;");
        global_result += execute_query(Test->conn_rwsplit, "CREATE DATABASE test1;");
        sleep(5);

        printf("Testing with database 'test1'\n");fflush(stdout);
        global_result += use_db(Test, (char *) "test1");
        global_result += insert_select(Test, N);

        global_result += check_t1_table(Test, FALSE, (char *) "test");
        global_result += check_t1_table(Test, TRUE, (char *) "test1");



        printf("Trying queries with syntax errors\n");fflush(stdout);
        execute_query(Test->conn_rwsplit, "DROP DATABASE I EXISTS test1;");
        execute_query(Test->conn_rwsplit, "CREATE TABLE ");

        execute_query(Test->conn_master, "DROP DATABASE I EXISTS test1;");
        execute_query(Test->conn_master, "CREATE TABLE ");

        execute_query(Test->conn_slave, "DROP DATABASE I EXISTS test1;");
        execute_query(Test->conn_slave, "CREATE TABLE ");

        // close connections
        Test->close_maxscale_connections();
        Test->repl->close_connections();

    }

    global_result += check_maxscale_alive();

    if (global_result == 0) {printf("PASSED!!\n");} else {printf("FAILED!!\n");}
    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;

    Test->print_env();
    global_result += execute_maxadmin_command_print(Test->maxscale_IP, (char *) "admin", Test->maxadmin_password, (char *) "show servers");

    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;

    Test->print_env();

    Test->connect_maxscale();

    printf("RWSplit: \n"); fflush(stdout);
    global_result = execute_query(Test->conn_rwsplit, (char *) "SET OPTION SQL_QUOTE_SHOW_CREATE = 1;");
    printf("ReadConn master: \n"); fflush(stdout);
    global_result = execute_query(Test->conn_master, (char *) "SET OPTION SQL_QUOTE_SHOW_CREATE = 1;");
    printf("readConn slave: \n"); fflush(stdout);
    global_result = execute_query(Test->conn_slave, (char *) "SET OPTION SQL_QUOTE_SHOW_CREATE = 1;");

    Test->close_maxscale_connections();

    global_result += check_maxscale_alive();

    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;
    int i;

    Test->read_env();
    Test->print_env();

    Test->connect_maxscale();

    printf("Trying \n");  fflush(stdout);

    char serverid1[1024];
    char serverid2[1024];

    if ( (
             find_field(
                 Test->conn_rwsplit, sel3,
                 "@@server_id", &serverid1[0])
             != 0 ) || (
             find_field(
                 Test->conn_rwsplit, sel4,
                 "@@server_id", &serverid2[0])
             != 0 )) {
        printf("@@server_id field not found!!\n");
        exit(1);
    } else {
        printf("'%s' to RWSplit gave @@server_id %s\n", sel3, serverid1);
        printf("'%s' directly to master gave @@server_id %s\n", sel4, serverid2);
        if (strcmp(serverid1, serverid2) !=0 ) {
            global_result++;
            printf("server_id are different depending in which order terms are in SELECT\n");
        }
    }

    if ( (
             find_field(
                 Test->conn_rwsplit, sel1,
                 "@@hostname", &serverid1[0])
             != 0 ) || (
             find_field(
                 Test->conn_rwsplit, sel2,
                 "@@hostname", &serverid2[0])
             != 0 )) {
        printf("@@hostname field not found!!\n");
        exit(1);
    } else {
        printf("'%s' to RWSplit gave @@hostname %s\n", sel1, serverid1);
        printf("'%s' to RWSplit gave @@hostname %s\n", sel2, serverid2);
        if (strcmp(serverid1, serverid2) !=0 ) {
            global_result++;
            printf("hostname are different depending in which order terms are in SELECT\n");
        }
    }

    Test->close_maxscale_connections();

    global_result += check_maxscale_alive();

    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;
    int N=4;
    char str[1024];

    Test->read_env();
    Test->print_env();

    Test->connect_maxscale();
    Test->repl->connect();

    printf("Create t1\n"); fflush(stdout);
    create_t1(Test->conn_rwsplit);
    printf("Insert data into t1\n"); fflush(stdout);
    insert_into_t1(Test->conn_rwsplit, N);
    printf("Sleeping to let replication happen\n");fflush(stdout);
    sleep(30);

    printf("Copying data from t1 to file\n");fflush(stdout);
    sprintf(str, "ssh -i %s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no %s@%s '%s rm /tmp/t*.csv; %s chmod 777 -R /tmp'", Test->repl->sshkey[0], Test->repl->access_user, Test->repl->IP[0], Test->repl->access_sudo, Test->repl->access_sudo);
    printf("%s\n", str);
    system(str);

    printf("RWSplit:\n");fflush(stdout);
    global_result += execute_query(Test->conn_rwsplit, (char *) "SELECT * INTO OUTFILE '/tmp/t1.csv' FROM t1;");
    printf("ReadsConn master:\n");fflush(stdout);
    global_result += execute_query(Test->conn_master, (char *) "SELECT * INTO OUTFILE '/tmp/t2.csv' FROM t1;");
    printf("ReadsConn slave:\n");fflush(stdout);
    global_result += execute_query(Test->conn_slave, (char *) "SELECT * INTO OUTFILE '/tmp/t3.csv' FROM t1;");

    printf("Copying t1.cvs from Maxscale machine:\n");fflush(stdout);
    sprintf(str, "scp -i %s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no %s@%s:/tmp/t1.csv ./", Test->repl->sshkey[0], Test->repl->access_user, Test->repl->IP[0]);
    printf("%s\n", str);
    system(str);

    MYSQL *srv[2];

    srv[0] = Test->conn_rwsplit;
    srv[1] = Test->conn_master;
    for (int i=0; i<2; i++) {
        printf("Dropping t1 \n");fflush(stdout);
        global_result += execute_query(Test->conn_rwsplit, (char *) "DROP TABLE t1;");
        printf("Sleeping to let replication happen\n");fflush(stdout);
        sleep(100);
        printf("Create t1\n"); fflush(stdout);
        create_t1(Test->conn_rwsplit);
        printf("Loading data to t1 from file\n");fflush(stdout);
        global_result += execute_query(srv[i], (char *) "LOAD DATA LOCAL INFILE 't1.csv' INTO TABLE t1;");

        printf("Sleeping to let replication happen\n");fflush(stdout);
        sleep(100);
        printf("SELECT: rwsplitter\n");fflush(stdout);
        global_result += select_from_t1(Test->conn_rwsplit, N);
        printf("SELECT: master\n");fflush(stdout);
        global_result += select_from_t1(Test->conn_master, N);
        printf("SELECT: slave\n");fflush(stdout);
        global_result += select_from_t1(Test->conn_slave, N);
        printf("Sleeping to let replication happen\n");fflush(stdout);
        /*sleep(100);
        for (int i=0; i<Test->repl->N; i++) {
            printf("SELECT: directly from node %d\n", i);fflush(stdout);
            global_result += select_from_t1(Test->repl->nodes[i], N);
        }*/
    }



    Test->repl->close_connections();
    global_result += check_maxscale_alive();

    Test->copy_all_logs(); return(global_result);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);

    Test->read_env();
    Test->print_env();
    Test->repl->connect();

    const int TestConnNum = 100;
    MYSQL *conn[TestConnNum];
    int i;
    int conn_num;
    int res = 0;

    MYSQL * backend_conn;
    for (i = 0; i < Test->repl->N; i++) {
        backend_conn = open_conn(Test->repl->port[i], Test->repl->IP[i], Test->repl->user_name, Test->repl->password, Test->repl->ssl);
        execute_query(backend_conn, "SET GLOBAL max_connections = 200;");
        mysql_close(backend_conn);
    }

    printf("Creating %d connections to RWSplit router\n", TestConnNum);
    for (i=0; i<TestConnNum; i++){
        conn[i] = Test->open_rwsplit_connection();
    }
    printf("Waiting 5 seconds\n");
    sleep(5);

    int ConnFloor = floor((float)TestConnNum / (Test->repl->N - 1));
    int ConnCell = ceil((float)TestConnNum / (Test->repl->N - 1));
    int TotalConn = 0;

    printf("Checking connections to Master: should be %d\n", TestConnNum);
    conn_num = get_conn_num(Test->repl->nodes[0], Test->maxscale_IP, (char *) "test");
    if (conn_num != TestConnNum) {
        res++;
        printf("FAILED: number of connections to Master is %d\n", conn_num);
    }

    printf("Number of connections to each slave should be between %d and %d\n", ConnFloor, ConnCell);
    printf("Checking connections to each node\n");
    for (int i = 1; i < Test->repl->N; i++) {
        conn_num = get_conn_num(Test->repl->nodes[i], Test->maxscale_IP, (char *) "test");
        TotalConn += conn_num;
        printf("Connections to node %d (%s):\t%d\n", i, Test->repl->IP[i], conn_num);
        if ((conn_num > ConnCell) || (conn_num < ConnFloor)) {
            res++;
            printf("FAILED: wrong number of connections to node %d\n", i);
        }
    }

    printf("Total number of connections %d\n", TotalConn);
    if (TotalConn != TestConnNum) {
        res++;
        printf("FAILED: total number of connections is wrong\n");

    }

    for (i=0; i<TestConnNum; i++) { mysql_close(conn[i]); }

    Test->copy_all_logs(); return(res);
}
int main(int argc, char *argv[])
{
    TestConnections * Test = new TestConnections(argc, argv);
    int global_result = 0;
    int i;

    Test->read_env();
    Test->print_env();

    printf("Connecting to Maxscale %s\n", Test->maxscale_IP);

    char sys1[4096];

    printf("Connecting to Maxscale %s to check its behaviour in case of blocking all bacxkends\n", Test->maxscale_IP);
    Test->connect_maxscale();

    for (i = 0; i < Test->repl->N; i++) {
        printf("Setup firewall to block mysql on node %d\n", i); fflush(stdout);
        Test->repl->block_node(i); fflush(stdout);
    }

    pid_t pid = fork();
    if (!pid) {
        Test->restart_maxscale(); fflush(stdout);
    } else {

        printf("Waiting 20 seconds\n"); fflush(stdout);
        sleep(20);

        printf("Checking if MaxScale is alive by connecting to MaxAdmin\n"); fflush(stdout);
        global_result += execute_maxadmin_command(Test->maxscale_IP, (char *) "admin", Test->maxadmin_password, (char* ) "show servers");

        for (i = 0; i < Test->repl->N; i++) {
            printf("Setup firewall back to allow mysql on node %d\n", i); fflush(stdout);
            Test->repl->unblock_node(i);fflush(stdout);
        }

        printf("Sleeping 60 seconds\n"); fflush(stdout);
        sleep(60);

        printf("Checking Maxscale is alive\n"); fflush(stdout);
        global_result += check_maxscale_alive(); fflush(stdout);
        if (global_result !=0) {
            printf("MaxScale is not alive\n");
        } else {
            printf("MaxScale is still alive\n");
        }

        Test->close_maxscale_connections(); fflush(stdout);

        printf("Reconnecting and trying query to RWSplit\n"); fflush(stdout);
        Test->connect_maxscale();
        global_result += execute_query(Test->conn_rwsplit, (char *) "show processlist;");
        printf("Trying query to ReadConn master\n"); fflush(stdout);
        global_result += execute_query(Test->conn_master, (char *) "show processlist;");
        printf("Trying query to ReadConn slave\n"); fflush(stdout);
        global_result += execute_query(Test->conn_slave, (char *) "show processlist;");
        Test->close_maxscale_connections();

        Test->copy_all_logs(); return(global_result);
    }
}