Skip to content

Commit

Permalink
1. Added tap test.
Browse files Browse the repository at this point in the history
2. Saving to config file code correction
  • Loading branch information
yashwantsahu20 committed Oct 17, 2024
1 parent 2dae44a commit 7e28df7
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/ProxySQL_Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,12 +996,10 @@ int ProxySQL_Config::Write_MySQL_Servers_to_configfile(std::string& data) {
addField(data, "multiplex", r->fields[5], "");
addField(data, "connection_warming", r->fields[6], "");
addField(data, "throttle_connections_per_sec", r->fields[7], "");
addField(data, "ignore_session_variables", r->fields[8], "");
addField(data, "hostgroup_settings", r->fields[9], "");
addField(data, "servers_defaults", r->fields[10], "");
addField(data, "min_lag_ms", r->fields[11], "");
addField(data, "lag_num_checks", r->fields[12], "");
addField(data, "comment", r->fields[13]);
addField(data, "ignore_session_variables", r->fields[8]);
addField(data, "hostgroup_settings", r->fields[9]);
addField(data, "servers_defaults", r->fields[10]);
addField(data, "comment", r->fields[11]);

data += "\t}";
isNext = true;
Expand Down
123 changes: 123 additions & 0 deletions test/tap/tests/mysql_hostgroup_attributes_config_file-t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* @file mysql_hostgroup_attributes_config_file-t.cpp
* @brief Reading and saving 'mysql_hostgroup_attributes' table from configuration file:
* 1. Correct values should be inserted into 'mysql_hostgroup_attributes' table.
* 2. Correct saving of mysql_hostgroup_attributes into config file.
* 3. Delete all the values from 'mysql_hostgroup_attributes' table
* 4. Correct load of 'mysql_hostgroup_attributes' table values from config file
*/

#include <cstring>
#include <string>
#include <fstream>
#include <unistd.h>

#include "mysql.h"
#include "mysqld_error.h"

#include "json.hpp"

#include "tap.h"
#include "utils.h"
#include "command_line.h"

using nlohmann::json;
using std::string;
using std::fstream;

int save_and_read_mysql_hostgroup_attributes_from_config(MYSQL* admin) {
json j_servers_defaults { { "weight", 100 }, { "max_connections", 10000 }, { "use_ssl", 1}, };

// To run this test locally copy a ProxySQL config file into this path.
string save_config_query = {"SAVE CONFIG TO FILE /var/lib/jenkins//scripts/"
"docker-mysql-proxysql/conf/proxysql/proxysql.cnf"};

const string INSERT_QUERY {
"INSERT INTO mysql_hostgroup_attributes (hostgroup_id, servers_defaults, comment)"
" VALUES (0, '" + j_servers_defaults.dump() + "', 'read config test')"
};

MYSQL_QUERY_T(admin, "DELETE FROM mysql_hostgroup_attributes");
MYSQL_QUERY_T(admin, INSERT_QUERY.c_str());
MYSQL_QUERY_T(admin, save_config_query.c_str());
MYSQL_QUERY_T(admin, "DELETE FROM mysql_hostgroup_attributes");
MYSQL_QUERY_T(admin, "LOAD MYSQL SERVERS FROM CONFIG;");

diag("Checking that config saved value matches INSERTED");
MYSQL_QUERY_T(admin, "SELECT servers_defaults FROM mysql_hostgroup_attributes WHERE hostgroup_id=0");

const auto extract_json_result = [] (MYSQL* admin) {
json j_result {};
MYSQL_RES* myres = mysql_store_result(admin);
MYSQL_ROW myrow = mysql_fetch_row(myres);

if (myrow && myrow[0]) {
try {
j_result = json::parse(myrow[0]);
} catch (const std::exception& e) {
diag("ERROR: Failed to parse retrieved 'servers_defaults' - '%s'", e.what());
}
}

mysql_free_result(myres);

return j_result;
};

json j_config_servers_defaults = extract_json_result(admin);

ok(
j_servers_defaults == j_config_servers_defaults,
"INSERTED 'servers_defaults' should match config value - Exp: `%s`, Act: `%s`",
j_servers_defaults.dump().c_str(), j_config_servers_defaults.dump().c_str()
);

MYSQL_QUERY_T(admin, "DELETE FROM mysql_hostgroup_attributes");
MYSQL_QUERY_T(admin, save_config_query.c_str());
MYSQL_QUERY_T(admin, "LOAD MYSQL SERVERS FROM CONFIG;");
diag("Checking that config saved value matches INSERTED");
MYSQL_QUERY_T(admin, "SELECT servers_defaults FROM mysql_hostgroup_attributes WHERE hostgroup_id=0");

j_config_servers_defaults = extract_json_result(admin);
json j_empty = {};

ok(
j_empty == j_config_servers_defaults,
"mysql_hostgroup_attributes should be empty. j_config_servers_defaults: `%s`",
j_config_servers_defaults.dump().c_str()
);

return EXIT_SUCCESS;
}

int main(int, char**) {
plan(2);

CommandLine cl;

if (cl.getEnv()) {
diag("Failed to get the required environmental variables.");
return EXIT_FAILURE;
}

MYSQL* admin = mysql_init(NULL);

if (!mysql_real_connect(admin, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(admin));
return EXIT_FAILURE;
}

// Cleanup
MYSQL_QUERY_T(admin, "DROP TABLE IF EXISTS mysql_hostgroup_attributes_0508");
MYSQL_QUERY_T(admin, "CREATE TABLE mysql_hostgroup_attributes_0508 AS SELECT * FROM mysql_hostgroup_attributes");
MYSQL_QUERY_T(admin, "DELETE FROM mysql_hostgroup_attributes");

save_and_read_mysql_hostgroup_attributes_from_config(admin);

cleanup:

MYSQL_QUERY_T(admin, "DELETE FROM mysql_hostgroup_attributes");
MYSQL_QUERY_T(admin, "INSERT INTO mysql_hostgroup_attributes SELECT * FROM mysql_hostgroup_attributes_0508");
mysql_close(admin);
return exit_status();
}

0 comments on commit 7e28df7

Please sign in to comment.