MXS-2427 Extend namedserverfilter test

Tests with two targets.
This commit is contained in:
Esa Korhonen
2019-04-29 17:07:26 +03:00
parent e9144219f5
commit 96b6acecff
2 changed files with 92 additions and 34 deletions

View File

@ -5,8 +5,8 @@ log_warning=1
[namedserverfilter]
type=filter
module=namedserverfilter
match=SELECT
server=server2
match01=^SELECT
target01=server2,server3
[MySQL Monitor]
type=monitor
@ -19,9 +19,7 @@ monitor_interval=1000
[RW Split Router]
type=service
router=readwritesplit
# Mixing the order of slaves will make server3 the first slave server
servers=server1,server3,server2,server4
servers=server1,server2,server3,server4
user=maxskysql
password=skysql
filters=namedserverfilter

View File

@ -6,40 +6,100 @@
* `match=SELECT` which should match any SELECT query.
*/
#include <iostream>
#include "testconnections.h"
#include <iostream>
using namespace std;
using std::cout;
using IdSet = std::set<int>;
int compare_server_id(TestConnections* test, char* node_id)
{
char str[1024];
int rval = 0;
if (find_field(test->maxscales->conn_rwsplit[0], "SELECT @@server_id", "@@server_id", str))
{
test->tprintf("Failed to query for @@server_id.\n");
rval = 1;
}
else if (strcmp(node_id, str))
{
test->tprintf("@@server_id is %s instead of %s\n", str, node_id);
rval = 1;
}
return rval;
}
bool check_server_id(MYSQL* conn, const IdSet& allowed_ids);
int main(int argc, char** argv)
{
TestConnections* test = new TestConnections(argc, argv);
test->repl->connect();
char server_id[1024];
TestConnections test(argc, argv);
test.repl->connect();
int server_count = test.repl->N;
if (server_count < 4)
{
test.expect(false, "Too few servers.");
return test.global_result;
}
sprintf(server_id, "%d", test->repl->get_server_id(1));
test->tprintf("Server ID of server2 is: %s\n", server_id);
test->add_result(test->maxscales->connect_rwsplit(0), "Test failed to connect to MaxScale.\n");
test->add_result(compare_server_id(test, server_id), "Test failed, server ID was not correct.\n");
int rval = test->global_result;
delete test;
return rval;
int server_ids[server_count];
cout << "Server id:s are:";
for (int i = 0; i < server_count; i++)
{
server_ids[i] = test.repl->get_server_id(i);
cout << " " << server_ids[i];
}
cout << ".\n";
auto maxconn = test.maxscales->open_rwsplit_connection(0);
test.try_query(maxconn, "SELECT 1;");
if (test.ok())
{
const char wrong_server[] = "Query went to wrong server.";
cout << "Testing with all servers on. Select-queries should go to servers " << server_ids[1]
<< " and " << server_ids[2] << ".\n";
IdSet allowed = {server_ids[1], server_ids[2]};
// With all servers on, the query should go to either 2 or 3. Test several times.
for (int i = 0; i < 5 && test.ok(); i++)
{
test.expect(check_server_id(maxconn, allowed), wrong_server);
}
auto test_server_down = [&](int node_to_stop, int allowed_node) {
test.repl->stop_node(node_to_stop);
test.maxscales->wait_for_monitor(1);
int stopped_id = server_ids[node_to_stop];
int allowed_id = server_ids[allowed_node];
cout << "Stopped server " << stopped_id << ".\n";
cout << "Select-queries should go to server " << allowed_id << " only.\n";
IdSet allowed_set = {allowed_id};
// Test that queries only go to the correct server.
for (int i = 0; i < 5 && test.ok(); i++)
{
test.expect(check_server_id(maxconn, allowed_set), "%s", wrong_server);
}
test.repl->start_node(node_to_stop, "");
cout << "Restarted server " << stopped_id << ".\n";
};
if (test.ok())
{
test_server_down(1, 2);
}
if (test.ok())
{
test_server_down(2, 1);
}
}
mysql_close(maxconn);
test.repl->disconnect();
return test.global_result;
}
bool check_server_id(MYSQL* conn, const IdSet& allowed_ids)
{
bool id_ok = false;
char str[100];
if (find_field(conn, "SELECT @@server_id", "@@server_id", str))
{
cout << "Failed to query for @@server_id: " << mysql_error(conn) << ".\n";
}
else
{
int queried_id = atoi(str);
if (allowed_ids.count(queried_id))
{
cout << "Query went to server " << queried_id << ".\n";
id_ok = true;
}
else
{
cout << "Queried unexpected server id " << queried_id << ".\n";
}
}
return id_ok;
}