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:
@ -665,6 +665,34 @@ private:
|
|||||||
value_type m_default_value;
|
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
|
* ParamSize
|
||||||
*/
|
*/
|
||||||
@ -1249,6 +1277,18 @@ inline Size::value_type operator/(const Size& lhs, Size::value_type rhs)
|
|||||||
return lhs.get() / 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
|
* String
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -672,6 +672,62 @@ void ParamPath::populate(MXS_MODULE_PARAM& param) const
|
|||||||
param.options |= m_options;
|
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
|
* ParamSize
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -19,6 +19,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <maxbase/log.hh>
|
#include <maxbase/log.hh>
|
||||||
#include <maxscale/config2.hh>
|
#include <maxscale/config2.hh>
|
||||||
|
#include "../internal/server.hh"
|
||||||
|
#include "test_utils.hh"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -46,11 +48,6 @@ config::ParamCount
|
|||||||
"count_parameter",
|
"count_parameter",
|
||||||
"Specifies the cardinality of something.");
|
"Specifies the cardinality of something.");
|
||||||
|
|
||||||
config::ParamInteger
|
|
||||||
param_integer(&specification,
|
|
||||||
"integer_parameter",
|
|
||||||
"Specifies a number.");
|
|
||||||
|
|
||||||
config::ParamDuration<std::chrono::seconds>
|
config::ParamDuration<std::chrono::seconds>
|
||||||
param_duration_1(&specification,
|
param_duration_1(&specification,
|
||||||
"duration_parameter_1",
|
"duration_parameter_1",
|
||||||
@ -78,12 +75,22 @@ param_enum(&specification,
|
|||||||
{ENUM_TWO, "two"}
|
{ENUM_TWO, "two"}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
config::ParamInteger
|
||||||
|
param_integer(&specification,
|
||||||
|
"integer_parameter",
|
||||||
|
"Specifies a number.");
|
||||||
|
|
||||||
config::ParamPath
|
config::ParamPath
|
||||||
param_path(&specification,
|
param_path(&specification,
|
||||||
"path_parameter",
|
"path_parameter",
|
||||||
"Specifies the path of something.",
|
"Specifies the path of something.",
|
||||||
config::ParamPath::F);
|
config::ParamPath::F);
|
||||||
|
|
||||||
|
config::ParamServer
|
||||||
|
param_server(&specification,
|
||||||
|
"server_parameter",
|
||||||
|
"Specifies a server.");
|
||||||
|
|
||||||
config::ParamSize
|
config::ParamSize
|
||||||
param_size(&specification,
|
param_size(&specification,
|
||||||
"size_parameter",
|
"size_parameter",
|
||||||
@ -236,6 +243,24 @@ int test_enum(config::Enum<Enum>& value)
|
|||||||
return test(value, entries, elements_in_array(entries));
|
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)
|
int test_path(config::Path& value)
|
||||||
{
|
{
|
||||||
static char path[PATH_MAX];
|
static char path[PATH_MAX];
|
||||||
@ -252,6 +277,25 @@ int test_path(config::Path& value)
|
|||||||
return test(value, entries, elements_in_array(entries));
|
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)
|
int test_size(config::Size& value)
|
||||||
{
|
{
|
||||||
static const TestEntry<config::Size::value_type> entries[] =
|
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));
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
mxb::Log log;
|
init_test_env();
|
||||||
|
|
||||||
for_each(specification.cbegin(), specification.cend(), [](const config::Specification::value_type& p) {
|
for_each(specification.cbegin(), specification.cend(), [](const config::Specification::value_type& p) {
|
||||||
cout << p.second->documentation() << endl;
|
cout << p.second->documentation() << endl;
|
||||||
@ -331,17 +357,20 @@ int main()
|
|||||||
config::Enum<Enum> value_enum(&configuration, ¶m_enum);
|
config::Enum<Enum> value_enum(&configuration, ¶m_enum);
|
||||||
nErrors += test_enum(value_enum);
|
nErrors += test_enum(value_enum);
|
||||||
|
|
||||||
|
config::Integer value_integer(&configuration, ¶m_integer);
|
||||||
|
nErrors += test_integer(value_integer);
|
||||||
|
|
||||||
config::Path value_path(&configuration, ¶m_path);
|
config::Path value_path(&configuration, ¶m_path);
|
||||||
nErrors += test_path(value_path);
|
nErrors += test_path(value_path);
|
||||||
|
|
||||||
|
config::Server value_server(&configuration, ¶m_server);
|
||||||
|
nErrors += test_server(value_server);
|
||||||
|
|
||||||
config::Size value_size(&configuration, ¶m_size);
|
config::Size value_size(&configuration, ¶m_size);
|
||||||
nErrors += test_size(value_size);
|
nErrors += test_size(value_size);
|
||||||
|
|
||||||
config::String value_string(&configuration, ¶m_string);
|
config::String value_string(&configuration, ¶m_string);
|
||||||
nErrors += test_string(value_string);
|
nErrors += test_string(value_string);
|
||||||
|
|
||||||
config::Integer value_integer(&configuration, ¶m_integer);
|
|
||||||
nErrors += test_integer(value_integer);
|
|
||||||
|
|
||||||
return nErrors ? EXIT_FAILURE : EXIT_SUCCESS;
|
return nErrors ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user