Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2.x tap tests optional ssl and compression via env variables #4356

Open
wants to merge 28 commits into
base: v2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ad4573e
reformat mysql_real_connect to oneliner
mirostauder Sep 13, 2023
37ac256
add use_ssl to TAP CommandLine
mirostauder Sep 24, 2023
ad843dd
adapt connections to use SSL according to envvar TAP_USE_SSL setting
mirostauder Jan 15, 2024
dd6f66e
add more logfiles to .gitignore
mirostauder Sep 24, 2023
2af1c4d
create tests_group_ssl-matrix
mirostauder Sep 24, 2023
552f9cc
Update CI-taptests-ssl.yml
mirostauder Sep 25, 2023
c65a616
minor tests_group_ssl-matrix fixes and data/helper files
mirostauder Sep 27, 2023
3f07d78
Update CI-taptests-ssl.yml
mirostauder Sep 25, 2023
9d4459e
Update CI-taptests-ssl.yml
mirostauder Sep 25, 2023
2804def
use /proc/self/cmdline to get tests path
mirostauder Sep 29, 2023
742667d
more TAP test fixes
mirostauder Jan 15, 2024
98b3e07
add cl.compression to CommadLine
mirostauder Oct 9, 2023
4cbf140
adapt connections to use Compression according to env TAP_COMPRESSION…
mirostauder Jan 15, 2024
0c1ebff
add get_env_int/bool helper functions and use them
mirostauder Oct 10, 2023
76ee898
make TAP .env files loading more verbose
mirostauder Oct 11, 2023
8d45f7f
more TAP test fixes
mirostauder Oct 11, 2023
1a67cb4
update tests/.env
mirostauder Oct 12, 2023
acdb918
use 'CommandLine cl' as a TAP test global
mirostauder Jan 15, 2024
8d9f5d4
fix declaration typo
mirostauder Oct 23, 2023
9284d8f
fix merge mistake
mirostauder Jan 16, 2024
5122894
global cl in new tap tests
mirostauder Jan 16, 2024
4c56628
fix plan
mirostauder Jan 17, 2024
5bf6196
fix bad merge
mirostauder Jan 17, 2024
71e67eb
fix plan
mirostauder Feb 1, 2024
11d77c3
Merge branch 'v2.x' into v2.x-tap_tests_opt_ssl
mirostauder Feb 6, 2024
a0132d3
fix string formating
mirostauder Feb 6, 2024
a5cf8f2
Merge branch 'v2.x' into v2.x-tap_tests_opt_ssl
mirostauder Mar 20, 2024
bbf9408
make CommandLine cl; a global
mirostauder Mar 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ oldcode/tests/connect_speed
#pidfile and errorlog
*.pid
*.log
*.keylog
*.log.*
*.err
proxy-audit.*
proxy-event*

#binary
src/proxysql
Expand Down
185 changes: 130 additions & 55 deletions test/tap/tap/command_line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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

#include "dotenv.h"
Expand All @@ -17,7 +18,12 @@ using nlohmann::json;
using dotenv::env;


CommandLine::CommandLine() {}
CommandLine::CommandLine() {
if (getEnv()) {
diag("Failed to get the required environmental variables.");
exit(-1);
}
}

CommandLine::~CommandLine() {
if (host)
Expand Down Expand Up @@ -125,12 +131,38 @@ int CommandLine::getEnv() {
{
// load environment
char temp[PATH_MAX];
ssize_t len = readlink("/proc/self/exe", temp, sizeof(temp));
std::string exe_path = (len > 0) ? std::string(temp, len) : std::string("");
ssize_t len = 0;
// ssize_t len = readlink("/proc/self/exe", temp, sizeof(temp));
{
FILE* fp = fopen("/proc/self/cmdline", "r"); // Linux
if (!fp)
fp = fopen("/proc/curproc/cmdline", "r"); // FreeBSD, needs procfs mounted
assert(fp); // MacOS ?
len = strlen(fgets(temp, PATH_MAX, fp));
assert(len);
fclose(fp);
}
std::string cmd_line = std::string(temp, len);
std::string cmd_path = cmd_line.substr(0, cmd_line.find_last_of('/'));
if (cmd_line.find('/') == std::string::npos)
cmd_path = ".";
std::string cmd_name = cmd_line.substr(cmd_line.find_last_of('/')+1);

// diag(">>> cmd_line = %s <<<", cmd_line.c_str());
// diag(">>> cmd_path = %s <<<", cmd_path.c_str());
// diag(">>> cmd_name = %s <<<", cmd_name.c_str());

len = strlen(realpath(cmd_path.c_str(), temp));
std::string exe_path = (len > 0) ? std::string(temp, len) + "/" + cmd_name : std::string("");
std::string exe_name = exe_path.substr(exe_path.find_last_of('/') + 1);
std::string dir_path = exe_path.substr(0, exe_path.find_last_of('/'));
std::string dir_name = dir_path.substr(dir_path.find_last_of('/') + 1);

// diag(">>> exe_path = %s <<<", exe_path.c_str());
// diag(">>> exe_name = %s <<<", exe_name.c_str());
// diag(">>> dir_path = %s <<<", dir_path.c_str());
// diag(">>> dir_name = %s <<<", dir_name.c_str());

env.load_dotenv((dir_path + "/.env").c_str(), true);
bool loaded1 = env.loaded;

Expand All @@ -140,28 +172,46 @@ int CommandLine::getEnv() {
env.load_dotenv((exe_path + ".env").c_str(), true);
bool loaded3 = env.loaded;

bool quiet = (bool) getenv("TAP_QUIET_ENVLOAD");
if (loaded1 && ! quiet)
bool verbose = get_env_bool("TAP_VERBOSE_ENVLOAD", 0);
if (loaded1 && verbose) {
diag("loaded: %s", (dir_path + "/.env").c_str());
if (loaded2 && ! quiet)
FILE* fp = fopen((dir_path + "/.env").c_str(), "r");
while (fgets(temp, PATH_MAX, fp))
printf("# %s", temp);
fclose(fp);
}
if (loaded2 && verbose) {
diag("loaded: %s", (dir_path + "/" + dir_name + ".env").c_str());
if (loaded3 && ! quiet)
FILE* fp = fopen((dir_path + "/" + dir_name + ".env").c_str(), "r");
while (fgets(temp, PATH_MAX, fp))
printf("# %s", temp);
fclose(fp);
}
if (loaded3 && verbose) {
diag("loaded: %s", (exe_path + ".env").c_str());
FILE* fp = fopen((exe_path + ".env").c_str(), "r");
while (fgets(temp, PATH_MAX, fp))
printf("# %s", temp);
fclose(fp);
}
}

int env_port = 0;
int env_int = 0;
{
// unprivileged test connection
value = getenv("TAP_HOST");
if (value)
replace_str_field(&this->host, value);

value = getenv("TAP_PORT");
if (value) {
env_port = strtol(value, NULL, 10);
if (env_port > 0 && env_port < 65536)
port = env_port;
}
// value = getenv("TAP_PORT");
// if (value) {
// env_int = strtol(value, NULL, 10);
// if (env_int > 0 && env_int < 65536)
// port = env_int;
// }
env_int = get_env_int("TAP_PORT", 0);
if (env_int > 0 && env_int < 65536)
port = env_int;

value = getenv("TAP_USERNAME");
if (value)
Expand All @@ -178,12 +228,15 @@ int CommandLine::getEnv() {
if (value)
replace_str_field(&this->root_host, value);

value = getenv("TAP_ROOTPORT");
if (value) {
env_port = strtol(value, NULL, 10);
if (env_port > 0 && env_port < 65536)
root_port = env_port;
}
// value = getenv("TAP_ROOTPORT");
// if (value) {
// env_int = strtol(value, NULL, 10);
// if (env_int > 0 && env_int < 65536)
// root_port = env_int;
// }
env_int = get_env_int("TAP_ROOTPORT", 0);
if (env_int > 0 && env_int < 65536)
root_port = env_int;

value = getenv("TAP_ROOTUSERNAME");
if (value)
Expand All @@ -200,12 +253,15 @@ int CommandLine::getEnv() {
if (value)
replace_str_field(&this->admin_host, value);

value = getenv("TAP_ADMINPORT");
if (value) {
env_port = strtol(value, NULL, 10);
if (env_port > 0 && env_port < 65536)
admin_port = env_port;
}
// value = getenv("TAP_ADMINPORT");
// if (value) {
// env_int = strtol(value, NULL, 10);
// if (env_int > 0 && env_int < 65536)
// admin_port = env_int;
// }
env_int = get_env_int("TAP_ADMINPORT", 0);
if (env_int > 0 && env_int < 65536)
admin_port = env_int;

value = getenv("TAP_ADMINUSERNAME");
if (value)
Expand All @@ -222,12 +278,15 @@ int CommandLine::getEnv() {
if (value)
replace_str_field(&this->mysql_host, value);

value = getenv("TAP_MYSQLPORT");
if (value) {
env_port = strtol(value, NULL, 10);
if (env_port > 0 && env_port < 65536)
mysql_port = env_port;
}
// value = getenv("TAP_MYSQLPORT");
// if (value) {
// env_int = strtol(value, NULL, 10);
// if (env_int > 0 && env_int < 65536)
// mysql_port = env_int;
// }
env_int = get_env_int("TAP_MYSQLPORT", 0);
if (env_int > 0 && env_int < 65536)
mysql_port = env_int;

value = getenv("TAP_MYSQLUSERNAME");
if (value)
Expand All @@ -238,29 +297,45 @@ int CommandLine::getEnv() {
replace_str_field(&this->mysql_password, value);
}


value = getenv("TAP_WORKDIR");
if (value)
replace_str_field(&this->workdir, value);

value = getenv("TAP_CLIENT_FLAGS");
if (value) {
char* end = NULL;
uint64_t env_c_flags = strtoul(value, &end, 10);

const char* errmsg { NULL };

if (env_c_flags == 0 && value == end) {
errmsg = "Invalid string to parse";
} else if (env_c_flags == ULONG_MAX && errno == ERANGE) {
errmsg = strerror(errno);
}

if (errmsg) {
fprintf(stderr, "Failed to parse env variable 'CLIENT_FLAGS' with error: '%s'\n", strerror(errno));
return -1;
} else {
this->client_flags = env_c_flags;
{
// various
value = getenv("TAP_WORKDIR");
if (value)
replace_str_field(&this->workdir, value);

// value = getenv("TAP_USE_SSL");
// if (value) {
// env_int = strtol(value, NULL, 0);
// use_ssl = (bool) env_int;
// }
use_ssl = get_env_bool("TAP_USE_SSL", 0);

// value = getenv("TAP_COMPRESSION");
// if (value) {
// env_int = strtol(value, NULL, 0);
// compression = (bool) env_int;
// }
compression = get_env_bool("TAP_COMPRESSION", 0);

value = getenv("TAP_CLIENT_FLAGS");
if (value) {
char* end = NULL;
uint64_t env_c_flags = strtoul(value, &end, 10);

const char* errmsg { NULL };

if (env_c_flags == 0 && value == end) {
errmsg = "Invalid string to parse";
} else if (env_c_flags == ULONG_MAX && errno == ERANGE) {
errmsg = strerror(errno);
}

if (errmsg) {
fprintf(stderr, "Failed to parse env variable 'CLIENT_FLAGS' with error: '%s'\n", strerror(errno));
return -1;
} else {
this->client_flags = env_c_flags;
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion test/tap/tap/command_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ class CommandLine {
char* mysql_username = strdup("root");
char* mysql_password = strdup("root");

// various
char* workdir = strdup("./");

bool use_ssl = 0;
bool compression = 0;
uint64_t client_flags = 0;

int getEnv();
Expand Down
4 changes: 3 additions & 1 deletion test/tap/tap/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,9 @@ string tap_curtime() {
return s;
}

int get_proxysql_cpu_usage(const CommandLine& cl, uint32_t intv, double& cpu_usage) {
int get_proxysql_cpu_usage(uint32_t intv, double& cpu_usage) {
// cl must be a TAP test global
extern CommandLine cl;
// check if proxysql process is consuming higher cpu than it should
MYSQL* proxysql_admin = mysql_init(NULL);
if (!mysql_real_connect(proxysql_admin, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) {
Expand Down
2 changes: 1 addition & 1 deletion test/tap/tap/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ std::string tap_curtime();
* 'ms' in the specified interval.
* @return 0 if success, -1 in case of error.
*/
int get_proxysql_cpu_usage(const CommandLine& cl, uint32_t intv, double& cpu_usage);
int get_proxysql_cpu_usage(uint32_t intv, double& cpu_usage);

/**
* @brief Helper struct holding connection options for helper functions creating MySQL connections.
Expand Down
8 changes: 3 additions & 5 deletions test/tap/tests/.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
TAP_ENV_VAR1=.env

# suppress env load messages
TAP_QUIET_ENVLOAD=1
# override the default for this PR
TAP_USERNAME=testuser
TAP_PASSWORD=testuser
# enable env load messages
# may break some tests parsing helpers output
#TAP_VERBOSE_ENVLOAD=1
39 changes: 31 additions & 8 deletions test/tap/tests/admin-listen_on_unix-t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

using std::string;

CommandLine cl;

std::string get_admin_mysql_ifaces(MYSQL *admin) {
std::string ret = "";
const char * query = (const char *)"SELECT variable_value FROM runtime_global_variables WHERE variable_name='admin-mysql_ifaces';";
Expand Down Expand Up @@ -41,21 +43,24 @@ std::string get_admin_mysql_ifaces(MYSQL *admin) {


int main(int argc, char** argv) {
CommandLine cl;

plan(13);

if (cl.getEnv()) {
diag("Failed to get the required environmental variables.");
return -1;
}
plan(2+2 + 13);

MYSQL* proxysql_admin = mysql_init(NULL);

diag("Connecting: cl.admin_username='%s' cl.use_ssl=%d cl.compression=%d", cl.admin_username, cl.use_ssl, cl.compression);
if (cl.use_ssl)
mysql_ssl_set(proxysql_admin, NULL, NULL, NULL, NULL, NULL);
if (cl.compression)
mysql_options(proxysql_admin, MYSQL_OPT_COMPRESS, NULL);
if (!mysql_real_connect(proxysql_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(proxysql_admin));
return EXIT_FAILURE;
} else {
const char * c = mysql_get_ssl_cipher(proxysql_admin);
ok(cl.use_ssl == 0 ? c == NULL : c != NULL, "Cipher: %s", c == NULL ? "NULL" : c);
ok(cl.compression == proxysql_admin->net.compress, "Compression: (%d)", proxysql_admin->net.compress);
}

{
std::string current = get_admin_mysql_ifaces(proxysql_admin);
char * expected = (char *)"0.0.0.0:6032;0.0.0.0:6031;/tmp/proxysql_admin.sock";
Expand All @@ -77,11 +82,22 @@ int main(int argc, char** argv) {

{
diag("Connecting on Unix Socket");

MYSQL* proxysql_admin2 = mysql_init(NULL);
diag("Connecting: cl.admin_username='%s' cl.use_ssl=%d cl.compression=%d", cl.admin_username, cl.use_ssl, cl.compression);
if (cl.use_ssl)
mysql_ssl_set(proxysql_admin2, NULL, NULL, NULL, NULL, NULL);
if (cl.compression)
mysql_options(proxysql_admin2, MYSQL_OPT_COMPRESS, NULL);
if (!mysql_real_connect(proxysql_admin2, NULL, cl.admin_username, cl.admin_password, NULL, 0, "/tmp/proxysql_admin.sock", 0)) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin2));
return EXIT_FAILURE;
} else {
const char * c = mysql_get_ssl_cipher(proxysql_admin2);
ok(cl.use_ssl == 0 ? c == NULL : c != NULL, "Cipher: %s", c == NULL ? "NULL" : c);
ok(cl.compression == proxysql_admin2->net.compress, "Compression: (%d)", proxysql_admin2->net.compress);
}

std::string current = get_admin_mysql_ifaces(proxysql_admin2);
char * expected = (char *)"0.0.0.0:6032;/tmp/proxysql_admin.sock";
ok(strcmp(current.c_str(),expected)==0, "Line: %d , Current admin-mysql_ifaces = %s . Expected = %s", __LINE__, current.c_str(), expected);
Expand All @@ -103,7 +119,14 @@ int main(int argc, char** argv) {
{
diag("Connecting on Unix Socket. It should fail");
MYSQL* proxysql_admin2 = mysql_init(NULL);
diag("Connecting: cl.admin_username='%s' cl.use_ssl=%d cl.compression=%d", cl.admin_username, cl.use_ssl, cl.compression);
if (cl.use_ssl)
mysql_ssl_set(proxysql_admin2, NULL, NULL, NULL, NULL, NULL);
if (cl.compression)
mysql_options(proxysql_admin2, MYSQL_OPT_COMPRESS, NULL);
MYSQL * ret = mysql_real_connect(proxysql_admin2, NULL, cl.admin_username, cl.admin_password, NULL, 0, "/tmp/proxysql_admin.sock", 0);
if (ret)
mysql_close(proxysql_admin2);
ok(ret == NULL, "Connection to Unix Socket should fail with error: %s", mysql_error(proxysql_admin2));
}

Expand Down
Loading
Loading