MXS-2556 Add Server type to new config mechanism

- Add test as well
- Sort test so that the types are tested in alphabetical order
This commit is contained in:
Johan Wikman 2019-06-10 17:55:07 +03:00
parent b2f44cefe7
commit 77197d2ce1
3 changed files with 152 additions and 27 deletions

View File

@ -665,6 +665,34 @@ private:
value_type m_default_value;
};
/**
* ParamServer
*/
class ParamServer : public Param
{
public:
using value_type = SERVER*;
ParamServer(Specification* pSpecification,
const char* zName,
const char* zDescription)
: Param(pSpecification, zName, zDescription, Param::MANDATORY, MXS_MODULE_PARAM_SERVER)
{
}
std::string type() const override;
std::string default_to_string() const override;
bool validate(const std::string& value_as_string, std::string* pMessage) const override;
bool set(Type& value, const std::string& value_as_string) const override;
bool from_string(const std::string& value, value_type* pValue,
std::string* pMessage = nullptr) const;
std::string to_string(value_type value) const;
};
/**
* ParamSize
*/
@ -1249,6 +1277,18 @@ inline Size::value_type operator/(const Size& lhs, Size::value_type rhs)
return lhs.get() / rhs;
}
/**
* Server
*/
class Server : public ConcreteType<Server, ParamServer>
{
public:
Server(Configuration* pConfiguration, const ParamServer* pParam)
: ConcreteType<Server, ParamServer>(pConfiguration, pParam)
{
}
};
/**
* String
*/

View File

@ -672,6 +672,62 @@ void ParamPath::populate(MXS_MODULE_PARAM& param) const
param.options |= m_options;
}
/**
* ParamServer
*/
std::string ParamServer::type() const
{
return "server";
}
std::string ParamServer::default_to_string() const
{
return "";
}
bool ParamServer::validate(const std::string& value_as_string, std::string* pMessage) const
{
value_type value;
return from_string(value_as_string, &value, pMessage);
}
bool ParamServer::set(Type& value, const std::string& value_as_string) const
{
mxb_assert(&value.parameter() == this);
Server& server_value = static_cast<Server&>(value);
value_type x;
bool valid = from_string(value_as_string, &x);
if (valid)
{
server_value.set(x);
}
return valid;
}
bool ParamServer::from_string(const std::string& value_as_string,
value_type* pValue,
std::string* pMessage) const
{
*pValue = SERVER::find_by_unique_name(value_as_string);
if (!*pValue && pMessage)
{
*pMessage = "Unknown server: ";
*pMessage += value_as_string;
}
return *pValue;
}
std::string ParamServer::to_string(value_type value) const
{
return value->name();
}
/**
* ParamSize
*/

View File

@ -19,6 +19,8 @@
#include <iostream>
#include <maxbase/log.hh>
#include <maxscale/config2.hh>
#include "../internal/server.hh"
#include "test_utils.hh"
using namespace std;
@ -46,11 +48,6 @@ config::ParamCount
"count_parameter",
"Specifies the cardinality of something.");
config::ParamInteger
param_integer(&specification,
"integer_parameter",
"Specifies a number.");
config::ParamDuration<std::chrono::seconds>
param_duration_1(&specification,
"duration_parameter_1",
@ -78,12 +75,22 @@ param_enum(&specification,
{ENUM_TWO, "two"}
});
config::ParamInteger
param_integer(&specification,
"integer_parameter",
"Specifies a number.");
config::ParamPath
param_path(&specification,
"path_parameter",
"Specifies the path of something.",
config::ParamPath::F);
config::ParamServer
param_server(&specification,
"server_parameter",
"Specifies a server.");
config::ParamSize
param_size(&specification,
"size_parameter",
@ -236,6 +243,24 @@ int test_enum(config::Enum<Enum>& value)
return test(value, entries, elements_in_array(entries));
}
int test_integer(config::Integer& value)
{
static const TestEntry<config::Integer::value_type> entries[] =
{
{"0", true, 0 },
{"-1", true, -1 },
{"1", true, 1 },
{"-2147483648", true, -2147483648},
{"2147483647", true, 2147483647 },
{"-2147483649", false},
{"2147483648", false},
{"0x10", false},
};
return test(value, entries, elements_in_array(entries));
}
int test_path(config::Path& value)
{
static char path[PATH_MAX];
@ -252,6 +277,25 @@ int test_path(config::Path& value)
return test(value, entries, elements_in_array(entries));
}
int test_server(config::Server& value)
{
MXS_CONFIG_PARAMETER params1;
params1.set(CN_PROTOCOL, "mariadbbackend");
params1.set(CN_PERSISTMAXTIME, "0");
params1.set(CN_RANK, "primary");
std::unique_ptr<Server> sServer1(Server::server_alloc("TheServer1", params1));
mxb_assert(sServer1.get());
const TestEntry<config::Server::value_type> entries[] =
{
{ "TheServer1", true, sServer1.get() },
{ "TheServer0", false },
};
return test(value, entries, elements_in_array(entries));
}
int test_size(config::Size& value)
{
static const TestEntry<config::Size::value_type> entries[] =
@ -282,27 +326,9 @@ int test_string(config::String& value)
return test(value, entries, elements_in_array(entries));
}
int test_integer(config::Integer& value)
{
static const TestEntry<config::Integer::value_type> entries[] =
{
{"0", true, 0 },
{"-1", true, -1 },
{"1", true, 1 },
{"-2147483648", true, -2147483648},
{"2147483647", true, 2147483647 },
{"-2147483649", false},
{"2147483648", false},
{"0x10", false},
};
return test(value, entries, elements_in_array(entries));
}
int main()
{
mxb::Log log;
init_test_env();
for_each(specification.cbegin(), specification.cend(), [](const config::Specification::value_type& p) {
cout << p.second->documentation() << endl;
@ -331,17 +357,20 @@ int main()
config::Enum<Enum> value_enum(&configuration, &param_enum);
nErrors += test_enum(value_enum);
config::Integer value_integer(&configuration, &param_integer);
nErrors += test_integer(value_integer);
config::Path value_path(&configuration, &param_path);
nErrors += test_path(value_path);
config::Server value_server(&configuration, &param_server);
nErrors += test_server(value_server);
config::Size value_size(&configuration, &param_size);
nErrors += test_size(value_size);
config::String value_string(&configuration, &param_string);
nErrors += test_string(value_string);
config::Integer value_integer(&configuration, &param_integer);
nErrors += test_integer(value_integer);
return nErrors ? EXIT_FAILURE : EXIT_SUCCESS;
}