diff --git a/maxscale-system-test/maxtest/include/maxtest/envv.h b/maxscale-system-test/maxtest/include/maxtest/envv.h index 76ffdcdad..c3c437906 100644 --- a/maxscale-system-test/maxtest/include/maxtest/envv.h +++ b/maxscale-system-test/maxtest/include/maxtest/envv.h @@ -1,17 +1,19 @@ #pragma once -#include -#include -#include +#include /** - * @brief readenv Read enviromental variable, if emtpy - set dafault + * Read enviroment variable value. If variable is not set, set it to the given default value and return the + * written value. + * * @param name Name of the variable - * @param format Default value + * @param format Default value format string * @return Enviromental variable value */ char * readenv(const char * name, const char *format, ...); +std::string envvar_read_write_def_str(const char* name, const char* format, ...); + /** * @brief readenv_int Read integer value of enviromental variable, if empty - set dafault * @param name Name of the variable diff --git a/maxscale-system-test/maxtest/include/maxtest/mariadb_nodes.h b/maxscale-system-test/maxtest/include/maxtest/mariadb_nodes.h index 75202a76b..e9b45b282 100644 --- a/maxscale-system-test/maxtest/include/maxtest/mariadb_nodes.h +++ b/maxscale-system-test/maxtest/include/maxtest/mariadb_nodes.h @@ -144,12 +144,12 @@ public: /** * @brief make_snapshot_command Command line to create a snapshot of all VMs */ - char* take_snapshot_command; + const char* take_snapshot_command; /** * @brief revert_snapshot_command Command line to revert a snapshot of all VMs */ - char* revert_snapshot_command; + const char* revert_snapshot_command; int connect(int i, const std::string& db = "test"); int connect(const std::string& db = "test"); diff --git a/maxscale-system-test/maxtest/include/maxtest/testconnections.h b/maxscale-system-test/maxtest/include/maxtest/testconnections.h index 798ec1fae..fcd3eaac8 100644 --- a/maxscale-system-test/maxtest/include/maxtest/testconnections.h +++ b/maxscale-system-test/maxtest/include/maxtest/testconnections.h @@ -624,18 +624,18 @@ private: std::string m_labels; /**< Test labels */ std::string m_mdbci_labels; /**< Labels for MDBCI */ - char* m_mdbci_config_name; /**< Name of MDBCI VMs set */ - char* m_mdbci_vm_path; /**< Path to directory with MDBCI VMs descriptions */ - char* m_mdbci_template; /**< Name of mdbci VMs tempate file */ - char* m_target; /**< Name of Maxscale repository in the CI */ + std::string m_mdbci_config_name; /**< Name of MDBCI VMs set */ + std::string m_mdbci_vm_path; /**< Path to directory with MDBCI VMs descriptions */ + std::string m_mdbci_template; /**< Name of mdbci VMs tempate file */ + std::string m_target; /**< Name of Maxscale repository in the CI */ /** * Command to copy log files from node virtual machines (should handle one parameter: IP address of * virtual machine to kill) */ - char* m_get_logs_command; + std::string m_get_logs_command; - char* m_take_snapshot_command; /**< Command line to create a snapshot of all VMs */ - char* m_revert_snapshot_command; /**< Command line to revert a snapshot of all VMs */ + std::string m_take_snapshot_command; /**< Command line to create a snapshot of all VMs */ + std::string m_revert_snapshot_command; /**< Command line to revert a snapshot of all VMs */ bool m_enable_timeouts {true}; /**< Whether timeouts are enabled or not */ bool m_local_maxscale {false}; /**< MaxScale runs locally, specified using -l. */ diff --git a/maxscale-system-test/maxtest/src/envv.cpp b/maxscale-system-test/maxtest/src/envv.cpp index bcd60cb56..87a0ed53e 100644 --- a/maxscale-system-test/maxtest/src/envv.cpp +++ b/maxscale-system-test/maxtest/src/envv.cpp @@ -1,7 +1,10 @@ -#include -#include #include "envv.h" +#include +#include + +using std::string; + char * readenv(const char * name, const char *format, ...) { char * env = getenv(name); @@ -28,6 +31,35 @@ char * readenv(const char * name, const char *format, ...) return env; } +string envvar_read_write_def_str(const char* name, const char* format, ...) +{ + string rval; + const char* old_value = getenv(name); + if (old_value) + { + rval = old_value; + } + else if (format) + { + va_list valist; + va_start(valist, format); + int bytes_required = vsnprintf(nullptr, 0, format, valist); + va_end(valist); + + if (bytes_required >= 0) + { + int buflen = bytes_required + 1; + char buf[buflen]; + va_start(valist, format); + vsnprintf(buf, buflen, format, valist); + va_end(valist); + setenv(name, buf, 1); + rval = buf; + } + } + return rval; +} + int readenv_int(const char * name, int def) { int x; diff --git a/maxscale-system-test/maxtest/src/testconnections.cpp b/maxscale-system-test/maxtest/src/testconnections.cpp index 52f5fc3a6..c5dd4aeaa 100644 --- a/maxscale-system-test/maxtest/src/testconnections.cpp +++ b/maxscale-system-test/maxtest/src/testconnections.cpp @@ -340,8 +340,7 @@ TestConnections::TestConnections(int argc, char* argv[]) } } - m_get_logs_command = (char *) malloc(strlen(test_dir) + 14); - sprintf(m_get_logs_command, "%s/get_logs.sh", test_dir); + m_get_logs_command = (string)test_dir + "/get_logs.sh"; sprintf(ssl_options, "--ssl-cert=%s/ssl-cert/client-cert.pem --ssl-key=%s/ssl-cert/client-key.pem", @@ -363,8 +362,8 @@ TestConnections::TestConnections(int argc, char* argv[]) repl = new Mariadb_nodes("node", test_dir, verbose, network_config); repl->setup(); repl->use_ipv6 = use_ipv6; - repl->take_snapshot_command = m_take_snapshot_command; - repl->revert_snapshot_command = m_revert_snapshot_command; + repl->take_snapshot_command = m_take_snapshot_command.c_str(); + repl->revert_snapshot_command = m_revert_snapshot_command.c_str(); repl_future = std::async(std::launch::async, &Mariadb_nodes::check_nodes, repl); } else @@ -377,8 +376,8 @@ TestConnections::TestConnections(int argc, char* argv[]) galera = new Galera_nodes("galera", test_dir, verbose, network_config); galera->setup(); galera->use_ipv6 = false; - galera->take_snapshot_command = m_take_snapshot_command; - galera->revert_snapshot_command = m_revert_snapshot_command; + galera->take_snapshot_command = m_take_snapshot_command.c_str(); + galera->revert_snapshot_command = m_revert_snapshot_command.c_str(); galera_future = std::async(std::launch::async, &Galera_nodes::check_nodes, galera); } else @@ -391,8 +390,8 @@ TestConnections::TestConnections(int argc, char* argv[]) clustrix = new Clustrix_nodes("clustrix", test_dir, verbose, network_config); clustrix->setup(); clustrix->use_ipv6 = false; - clustrix->take_snapshot_command = m_take_snapshot_command; - clustrix->revert_snapshot_command = m_revert_snapshot_command; + clustrix->take_snapshot_command = m_take_snapshot_command.c_str(); + clustrix->revert_snapshot_command = m_revert_snapshot_command.c_str(); clustrix->fix_replication(); } else @@ -426,7 +425,7 @@ TestConnections::TestConnections(int argc, char* argv[]) if (reinstall_maxscales()) { - tprintf("Failed to install Maxscale: target is %s", m_target); + tprintf("Failed to install Maxscale: target is %s", m_target.c_str()); exit(MDBCI_FAUILT); } } @@ -647,21 +646,21 @@ void TestConnections::expect(bool result, const char* format, ...) void TestConnections::read_mdbci_info() { - m_mdbci_vm_path = readenv("MDBCI_VM_PATH", "%s/vms/", getenv("HOME")); + m_mdbci_vm_path = envvar_read_write_def_str("MDBCI_VM_PATH", "%s/vms/", getenv("HOME")); - if (system((std::string("mkdir -p ") + - std::string(m_mdbci_vm_path)).c_str())) + string cmd = "mkdir -p " + m_mdbci_vm_path; + if (system(cmd.c_str())) { - tprintf("Unable to create MDBCI VMs direcory '%s', exiting", m_mdbci_vm_path); + tprintf("Unable to create MDBCI VMs direcory '%s', exiting", m_mdbci_vm_path.c_str()); exit(MDBCI_FAUILT); } - m_mdbci_template = readenv("template", "default"); - m_target = readenv("target", "develop"); + m_mdbci_template = envvar_read_write_def_str("template", "default"); + m_target = envvar_read_write_def_str("target", "develop"); - m_mdbci_config_name = readenv("mdbci_config_name", "local"); - vm_path = std::string(m_mdbci_vm_path) + "/" + std::string(m_mdbci_config_name); + m_mdbci_config_name = envvar_read_write_def_str("mdbci_config_name", "local"); + vm_path = m_mdbci_vm_path + "/" + m_mdbci_config_name; - if (m_mdbci_config_name != NULL) + if (!m_mdbci_config_name.empty()) { std::ifstream nc_file; nc_file.open(vm_path + "_network_config"); @@ -714,10 +713,12 @@ void TestConnections::read_env() smoke = readenv_bool("smoke", false); threads = readenv_int("threads", 4); m_use_snapshots = readenv_bool("use_snapshots", false); - m_take_snapshot_command = readenv("take_snapshot_command", - "mdbci snapshot take --path-to-nodes %s --snapshot-name ", m_mdbci_config_name); - m_revert_snapshot_command = readenv("revert_snapshot_command", - "mdbci snapshot revert --path-to-nodes %s --snapshot-name ", m_mdbci_config_name); + m_take_snapshot_command = envvar_read_write_def_str( + "take_snapshot_command", "mdbci snapshot take --path-to-nodes %s --snapshot-name ", + m_mdbci_config_name.c_str()); + m_revert_snapshot_command = envvar_read_write_def_str( + "revert_snapshot_command", "mdbci snapshot revert --path-to-nodes %s --snapshot-name ", + m_mdbci_config_name.c_str()); no_vm_revert = readenv_bool("no_vm_revert", true); } @@ -2175,15 +2176,15 @@ void TestConnections::check_current_connections(int m, int value) int TestConnections::take_snapshot(char* snapshot_name) { - char str[strlen(m_take_snapshot_command) + strlen(snapshot_name) + 2]; - sprintf(str, "%s %s", m_take_snapshot_command, snapshot_name); + char str[m_take_snapshot_command.length() + strlen(snapshot_name) + 2]; + sprintf(str, "%s %s", m_take_snapshot_command.c_str(), snapshot_name); return call_system(str); } int TestConnections::revert_snapshot(char* snapshot_name) { - char str[strlen(m_revert_snapshot_command) + strlen(snapshot_name) + 2]; - sprintf(str, "%s %s", m_revert_snapshot_command, snapshot_name); + char str[m_revert_snapshot_command.length() + strlen(snapshot_name) + 2]; + sprintf(str, "%s %s", m_revert_snapshot_command.c_str(), snapshot_name); return call_system(str); } @@ -2203,10 +2204,8 @@ bool TestConnections::test_bad_config(int m, const char* config) int TestConnections::call_mdbci(const char * options) { struct stat buf; - if (stat( - (m_mdbci_vm_path + std::string("/") + m_mdbci_config_name).c_str(), - &buf) - ) + string filepath = m_mdbci_vm_path + "/" + m_mdbci_config_name; + if (stat(filepath.c_str(), &buf)) { if (process_mdbci_template()) { @@ -2216,7 +2215,7 @@ int TestConnections::call_mdbci(const char * options) if (system((std::string("mdbci --override --template ") + vm_path + std::string(".json generate ") + - std::string(m_mdbci_config_name)).c_str() )) + m_mdbci_config_name).c_str() )) { tprintf("MDBCI failed to generate virtual machines description"); return 1; @@ -2233,21 +2232,19 @@ int TestConnections::call_mdbci(const char * options) } if (system((std::string("mdbci up ") + - std::string(m_mdbci_config_name) + - std::string(" --labels ") + - m_mdbci_labels + - std::string(" ") + - std::string(options)).c_str() )) + m_mdbci_config_name + + std::string(" --labels ") + + m_mdbci_labels + + std::string(" ") + + std::string(options)).c_str() )) { tprintf("MDBCI failed to bring up virtual machines"); return 1; } std::string team_keys = readenv("team_keys", "~/.ssh/id_rsa.pub"); - system((std::string("mdbci public_keys --key ") + - team_keys + - std::string(" ") + - std::string(m_mdbci_config_name)).c_str() ); + string cmd = "mdbci public_keys --key " + team_keys + " " + m_mdbci_config_name; + system(cmd.c_str()); read_env(); if (repl) { @@ -2288,9 +2285,9 @@ int TestConnections::process_mdbci_template() } std::string name = std::string(test_dir) + - std::string("/mdbci/templates/") + - std::string(m_mdbci_template) + - std::string(".json.template"); + std::string("/mdbci/templates/") + + m_mdbci_template + + std::string(".json.template"); std::string sys = std::string("envsubst < ") + name + @@ -2327,10 +2324,7 @@ std::string dump_status(const StringSet& current, const StringSet& expected) } int TestConnections::reinstall_maxscales() { - char sys[strlen(m_target) + - strlen(m_mdbci_config_name) + - strlen(maxscales->prefix) + - 70]; + char sys[m_target.length() + m_mdbci_config_name.length() + strlen(maxscales->prefix) + 70]; for (int i = 0; i < maxscales->N; i++) { printf("Installing Maxscale on node %d\n", i); @@ -2339,7 +2333,7 @@ int TestConnections::reinstall_maxscales() maxscales->ssh_node(i, "yum clean all", true); sprintf(sys, "mdbci install_product --product maxscale_ci --product-version %s %s/%s_%03d", - m_target, m_mdbci_config_name, maxscales->prefix, i); + m_target.c_str(), m_mdbci_config_name.c_str(), maxscales->prefix, i); if (system(sys)) { return 1;