diff --git a/include/maxscale/config.hh b/include/maxscale/config.hh new file mode 100644 index 000000000..11c844419 --- /dev/null +++ b/include/maxscale/config.hh @@ -0,0 +1,38 @@ +#pragma once +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2022-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include + +namespace maxscale +{ + +// Helper class for allocating temporary configuration parameters +class ParamList +{ +public: + ParamList(const ParamList&) = delete; + ParamList& operator=(const ParamList&) = delete; + + ParamList(std::initializer_list> list, + const MXS_MODULE_PARAM* module_params = NULL); + + ~ParamList(); + + MXS_CONFIG_PARAMETER* params(); + +private: + CONFIG_CONTEXT m_ctx = {(char*)""}; +}; + +} diff --git a/server/core/config.cc b/server/core/config.cc index 8403483d6..f8e76c2e1 100644 --- a/server/core/config.cc +++ b/server/core/config.cc @@ -16,6 +16,7 @@ */ #include +#include #include #include @@ -4309,3 +4310,32 @@ bool config_parse_disk_space_threshold(MxsDiskSpaceThreshold* pDisk_space_thresh return success; } + +namespace maxscale +{ + +ParamList::ParamList(std::initializer_list> list, + const MXS_MODULE_PARAM* module_params) +{ + for (auto&& a : list) + { + config_add_param(&m_ctx, a.first, a.second); + } + + if (module_params) + { + config_add_defaults(&m_ctx, module_params); + } +} + +ParamList::~ParamList() +{ + config_parameter_free(m_ctx.parameters); +} + +MXS_CONFIG_PARAMETER* ParamList::params() +{ + return m_ctx.parameters; +} + +} diff --git a/server/core/test/test_server.cc b/server/core/test/test_server.cc index 37ecc7f8f..a07f3ce26 100644 --- a/server/core/test/test_server.cc +++ b/server/core/test/test_server.cc @@ -37,11 +37,20 @@ #include #include #include +#include // This is pretty ugly but it's required to test internal functions #include "../config.cc" #include "../server.cc" +static mxs::ParamList params( +{ + {"address", "127.0.0.1"}, + {"port", "9876"}, + {"protocol", "HTTPD"}, + {"authenticator", "NullAuthAllow"} +}, config_server_params); + /** * test1 Allocate a server and do lots of other things * @@ -55,14 +64,10 @@ test1() /* Server tests */ ss_dfprintf(stderr, "testserver : creating server called MyServer"); - set_libdir(MXS_STRDUP_A("../../modules/authenticator/NullAuthAllow/")); - server = server_alloc("uniquename", "127.0.0.1", 9876, "HTTPD", "NullAuthAllow"); + server = server_alloc("uniquename", params.params()); ss_info_dassert(server, "Allocating the server should not fail"); mxs_log_flush_sync(); - //ss_info_dassert(NULL != service, "New server with valid protocol and port must not be null"); - //ss_info_dassert(0 != service_isvalid(service), "Service must be valid after creation"); - char buf[120]; ss_dfprintf(stderr, "\t..done\nTest Parameter for Server."); ss_info_dassert(!server_get_parameter(server, "name", buf, sizeof(buf)), "Parameter should be null when not set"); @@ -123,6 +128,7 @@ bool test_load_config(const char *input, SERVER *server) { CONFIG_CONTEXT *obj = ccontext.next; MXS_CONFIG_PARAMETER *param = obj->parameters; + config_add_defaults(obj, config_server_params); TEST(strcmp(obj->object, server->name) == 0, "Server names differ"); TEST(strcmp(server->address, config_get_param(param, "address")->value) == 0, "Server addresses differ"); @@ -130,7 +136,7 @@ bool test_load_config(const char *input, SERVER *server) TEST(strcmp(server->authenticator, config_get_param(param, "authenticator")->value) == 0, "Server authenticators differ"); TEST(server->port == atoi(config_get_param(param, "port")->value), "Server ports differ"); - TEST(create_new_server(obj) == 0, "Failed to create server from loaded config"); + TEST(server_alloc(obj->object, obj->parameters), "Failed to create server from loaded config"); duplicate_context_finish(&dcontext); config_context_free(obj); } @@ -146,7 +152,7 @@ bool test_serialize() char old_config_name[] = "serialized-server.cnf.old"; char *persist_dir = MXS_STRDUP_A("./"); set_config_persistdir(persist_dir); - SERVER *server = server_alloc(name, "127.0.0.1", 9876, "HTTPD", "NullAuthAllow"); + SERVER *server = server_alloc(name, params.params()); TEST(server, "Server allocation failed"); /** Make sure the files don't exist */ @@ -178,6 +184,16 @@ bool test_serialize() int main(int argc, char **argv) { + /** + * Prepare test environment by pre-loading modules. This prevents the server + * allocation from failing if multiple modules from different directories are + * loaded in one core function call. + */ + set_libdir(MXS_STRDUP_A("../../modules/authenticator/NullAuthAllow/")); + load_module("NullAuthAllow", MODULE_AUTHENTICATOR); + set_libdir(MXS_STRDUP_A("../../modules/protocol/HTTPD/")); + load_module("HTTPD", MODULE_PROTOCOL); + int result = 0; result += test1(); diff --git a/server/modules/routing/binlogrouter/blr.cc b/server/modules/routing/binlogrouter/blr.cc index 2d5c9a8f7..a27437015 100644 --- a/server/modules/routing/binlogrouter/blr.cc +++ b/server/modules/routing/binlogrouter/blr.cc @@ -38,6 +38,7 @@ #include #include +#include #include #include #include @@ -762,12 +763,19 @@ static MXS_ROUTER* createInstance(SERVICE *service, MXS_CONFIG_PARAMETER* params /* Dynamically allocate master_host server struct, not written in any cnf file */ if (service->dbref == NULL) { - SERVER *server; - SSL_LISTENER *ssl_cfg; - server = server_alloc("binlog_router_master_host", - "_none_", 3306, - "MySQLBackend", - "MySQLBackendAuth"); + // Declared in config.cc and needs to be removed if/when blr is refactored + extern const MXS_MODULE_PARAM config_server_params[]; + + mxs::ParamList p( + { + {"address", "_none_"}, + {"port", "3306"}, + {"protocol", "MySQLBackend"}, + {"authenticator", "MySQLBackendAuth"} + }, config_server_params); + + SERVER* server = server_alloc("binlog_router_master_host", p.params()); + if (server == NULL) { MXS_ERROR("%s: Error for server_alloc in createInstance", diff --git a/server/modules/routing/binlogrouter/test/testbinlog.cc b/server/modules/routing/binlogrouter/test/testbinlog.cc index 7e00bb81c..30ebaeff2 100644 --- a/server/modules/routing/binlogrouter/test/testbinlog.cc +++ b/server/modules/routing/binlogrouter/test/testbinlog.cc @@ -52,6 +52,7 @@ // This isn't really a clean way of testing #include "../../../../core/internal/service.h" +#include static void printVersion(const char *progname); static void printUsage(const char *progname); @@ -126,8 +127,18 @@ int main(int argc, char **argv) service_add_parameters(service, &p); } - SERVER* server = server_alloc("binlog_router_master_host", "_none_", 3306, - "MySQLBackend", "MySQLBackendAuth"); + // Declared in config.cc and needs to be removed if/when blr is refactored + extern const MXS_MODULE_PARAM config_server_params[]; + + mxs::ParamList p( + { + {"address", "_none_"}, + {"port", "3306"}, + {"protocol", "MySQLBackend"}, + {"authenticator", "MySQLBackendAuth"} + }, config_server_params); + + SERVER* server = server_alloc("binlog_router_master_host", p.params()); if (server == NULL) { printf("Failed to allocate 'server' object\n");