Fix explicit server allocation

The test cases allocated servers in a way that doesn't comfortably suit
the way the servers are now allocated. Adding a helper C++ class to load
module defaults makes it easier to do explicit server initialization in
tests.

The binlogrouter was also fixed in this commit as it uses servers much
like a test would use.
This commit is contained in:
Markus Mäkelä 2018-07-14 23:23:56 +03:00
parent df94ef990c
commit 4e8ac8dd4f
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
5 changed files with 118 additions and 15 deletions

View File

@ -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 <maxscale/config.h>
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<std::pair<const char*, const char*>> list,
const MXS_MODULE_PARAM* module_params = NULL);
~ParamList();
MXS_CONFIG_PARAMETER* params();
private:
CONFIG_CONTEXT m_ctx = {(char*)""};
};
}

View File

@ -16,6 +16,7 @@
*/
#include <maxscale/config.h>
#include <maxscale/config.hh>
#include <ctype.h>
#include <ftw.h>
@ -4309,3 +4310,32 @@ bool config_parse_disk_space_threshold(MxsDiskSpaceThreshold* pDisk_space_thresh
return success;
}
namespace maxscale
{
ParamList::ParamList(std::initializer_list<std::pair<const char*, const char*>> 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;
}
}

View File

@ -37,11 +37,20 @@
#include <maxscale/server.h>
#include <maxscale/log_manager.h>
#include <maxscale/paths.h>
#include <maxscale/config.hh>
// 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();

View File

@ -38,6 +38,7 @@
#include <maxscale/alloc.h>
#include <maxscale/atomic.h>
#include <maxscale/config.hh>
#include <maxscale/dcb.h>
#include <maxscale/housekeeper.h>
#include <maxscale/log_manager.h>
@ -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",

View File

@ -52,6 +52,7 @@
// This isn't really a clean way of testing
#include "../../../../core/internal/service.h"
#include <maxscale/config.hh>
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");