diff options
Diffstat (limited to '')
-rw-r--r-- | test-dnsdistlbpolicies_cc.cc | 347 |
1 files changed, 187 insertions, 160 deletions
diff --git a/test-dnsdistlbpolicies_cc.cc b/test-dnsdistlbpolicies_cc.cc index c7e638b..401108f 100644 --- a/test-dnsdistlbpolicies_cc.cc +++ b/test-dnsdistlbpolicies_cc.cc @@ -1,5 +1,8 @@ +#ifndef BOOST_TEST_DYN_LINK #define BOOST_TEST_DYN_LINK +#endif + #define BOOST_TEST_NO_MAIN #include <boost/test/unit_test.hpp> @@ -20,8 +23,6 @@ DNSDistSNMPAgent* g_snmpAgent{nullptr}; #if BENCH_POLICIES bool g_verbose{true}; -bool g_syslog{true}; -bool g_logtimestamps{false}; #include "dnsdist-rings.hh" Rings g_rings; GlobalStateHolder<NetmaskTree<DynBlock>> g_dynblockNMG; @@ -34,52 +35,26 @@ std::vector<std::unique_ptr<ClientState>> g_frontends; /* add stub implementations, we don't want to include the corresponding object files and their dependencies */ -#ifdef HAVE_DNS_OVER_HTTPS -std::unordered_map<std::string, std::string> DOHUnit::getHTTPHeaders() const -{ - return {}; -} - -std::string DOHUnit::getHTTPPath() const -{ - return ""; -} - -std::string DOHUnit::getHTTPHost() const -{ - return ""; -} - -std::string DOHUnit::getHTTPScheme() const -{ - return ""; -} - -std::string DOHUnit::getHTTPQueryString() const -{ - return ""; -} - -void DOHUnit::setHTTPResponse(uint16_t statusCode, PacketBuffer&& body_, const std::string& contentType_) -{ -} -#endif /* HAVE_DNS_OVER_HTTPS */ - -void handleDOHTimeout(DOHUnitUniquePtr&& oldDU) +// NOLINTNEXTLINE(readability-convert-member-functions-to-static): this is a stub, the real one is not that simple.. +bool TLSFrontend::setupTLS() { + return true; } +// NOLINTNEXTLINE(readability-convert-member-functions-to-static): this is a stub, the real one is not that simple.. std::string DNSQuestion::getTrailingData() const { return ""; } +// NOLINTNEXTLINE(readability-convert-member-functions-to-static): this is a stub, the real one is not that simple.. bool DNSQuestion::setTrailingData(const std::string& tail) { return false; } -bool DNSDistSNMPAgent::sendDNSTrap(const DNSQuestion& dq, const std::string& reason) +// NOLINTNEXTLINE(readability-convert-member-functions-to-static): this is a stub, the real one is not that simple.. +bool DNSDistSNMPAgent::sendDNSTrap(const DNSQuestion& dnsQuestion, const std::string& reason) { return false; } @@ -88,16 +63,18 @@ void setLuaNoSideEffect() { } -DNSAction::Action SpoofAction::operator()(DNSQuestion* dq, std::string* ruleresult) const +DNSAction::Action SpoofAction::operator()(DNSQuestion* dnsQuestion, std::string* ruleresult) const { return DNSAction::Action::None; } -bool setupDoTProtocolNegotiation(std::shared_ptr<TLSCtx>&) +bool setupDoTProtocolNegotiation(std::shared_ptr<TLSCtx>& tlsCtx) { + (void)tlsCtx; return true; } +// NOLINTNEXTLINE(performance-unnecessary-value-param): this is a stub, the real one is not that simple and the performance does not matter void responderThread(std::shared_ptr<DownstreamState> dss) { } @@ -112,14 +89,14 @@ static DNSQuestion getDQ(const DNSName* providedName = nullptr) static InternalQueryState ids; ids.origDest = ComboAddress("127.0.0.1:53"); ids.origRemote = ComboAddress("192.0.2.1:42"); - ids.qname = providedName ? *providedName : qname; + ids.qname = providedName != nullptr ? *providedName : qname; ids.qtype = QType::A; ids.qclass = QClass::IN; ids.protocol = dnsdist::Protocol::DoUDP; ids.queryRealTime.start(); - DNSQuestion dq(ids, packet); - return dq; + DNSQuestion dnsQuestion(ids, packet); + return dnsQuestion; } static void benchPolicy(const ServerPolicy& pol) @@ -131,11 +108,11 @@ static void benchPolicy(const ServerPolicy& pol) std::vector<DNSName> names; names.reserve(1000); for (size_t idx = 0; idx < 1000; idx++) { - names.push_back(DNSName("powerdns-" + std::to_string(idx) + ".com.")); + names.emplace_back("powerdns-" + std::to_string(idx) + ".com."); } ServerPolicy::NumberedServerVector servers; for (size_t idx = 1; idx <= 10; idx++) { - servers.push_back({ idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")) }); + servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53"))); servers.at(idx - 1).second->setUp(); /* we need to have a weight of at least 1000 to get an optimal repartition with the consistent hashing algo */ servers.at(idx - 1).second->setWeight(1000); @@ -146,12 +123,12 @@ static void benchPolicy(const ServerPolicy& pol) StopWatch sw; sw.start(); for (size_t idx = 0; idx < 1000; idx++) { - for (const auto& name : names) { - auto dq = getDQ(&name); - auto server = pol.getSelectedBackend(servers, dq); - } + for (const auto& name : names) { + auto dnsQuestion = getDQ(&name); + auto server = pol.getSelectedBackend(servers, dnsQuestion); + } } - cerr<<pol.name<<" took "<<std::to_string(sw.udiff())<<" us for "<<names.size()<<endl; + cerr << pol.name << " took " << std::to_string(sw.udiff()) << " us for " << names.size() << endl; g_verbose = existingVerboseValue; #endif /* BENCH_POLICIES */ @@ -167,49 +144,51 @@ static void resetLuaContext() BOOST_AUTO_TEST_SUITE(dnsdistlbpolicies) -BOOST_AUTO_TEST_CASE(test_firstAvailable) { - auto dq = getDQ(); +BOOST_AUTO_TEST_CASE(test_firstAvailable) +{ + auto dnsQuestion = getDQ(); ServerPolicy pol{"firstAvailable", firstAvailable, false}; ServerPolicy::NumberedServerVector servers; - servers.push_back({ 1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53")) }); + servers.emplace_back(1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53"))); /* servers start as 'down' */ - auto server = pol.getSelectedBackend(servers, dq); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_CHECK(server == nullptr); /* mark the server as 'up' */ servers.at(0).second->setUp(); - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_CHECK(server != nullptr); /* add a second server, we should still get the first one */ - servers.push_back({ 2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53")) }); - server = pol.getSelectedBackend(servers, dq); + servers.emplace_back(2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53"))); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(server != nullptr); BOOST_CHECK(server == servers.at(0).second); /* mark the first server as 'down', second as 'up' */ servers.at(0).second->setDown(); servers.at(1).second->setUp(); - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(server != nullptr); BOOST_CHECK(server == servers.at(1).second); benchPolicy(pol); } -BOOST_AUTO_TEST_CASE(test_firstAvailableWithOrderAndQPS) { - auto dq = getDQ(); +BOOST_AUTO_TEST_CASE(test_firstAvailableWithOrderAndQPS) +{ + auto dnsQuestion = getDQ(); size_t qpsLimit = 10; ServerPolicy pol{"firstAvailable", firstAvailable, false}; ServerPolicy::NumberedServerVector servers; - servers.push_back({ 1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53")) }); - servers.push_back({ 2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53")) }); + servers.emplace_back(1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53"))); + servers.emplace_back(2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53"))); /* Second server has a higher order, so most queries should be routed to the first (remember that we need to keep them ordered!). - However the first server has a QPS limit at 10 qps, so any query above that should be routed + However the first server has a QPS limit at 10 qps, so any query above that should be routed to the second server. */ servers.at(0).second->d_config.order = 1; servers.at(1).second->d_config.order = 2; @@ -221,7 +200,7 @@ BOOST_AUTO_TEST_CASE(test_firstAvailableWithOrderAndQPS) { /* the first queries under the QPS limit should be sent to the first server */ for (size_t idx = 0; idx < qpsLimit; idx++) { - auto server = pol.getSelectedBackend(servers, dq); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(server != nullptr); BOOST_CHECK(server == servers.at(0).second); server->incQueriesCount(); @@ -229,65 +208,66 @@ BOOST_AUTO_TEST_CASE(test_firstAvailableWithOrderAndQPS) { /* then to the second server */ for (size_t idx = 0; idx < 100; idx++) { - auto server = pol.getSelectedBackend(servers, dq); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(server != nullptr); BOOST_CHECK(server == servers.at(1).second); server->incQueriesCount(); } } -BOOST_AUTO_TEST_CASE(test_roundRobin) { - auto dq = getDQ(); +BOOST_AUTO_TEST_CASE(test_roundRobin) +{ + auto dnsQuestion = getDQ(); ServerPolicy pol{"roundrobin", roundrobin, false}; ServerPolicy::NumberedServerVector servers; /* selecting a server on an empty server list */ g_roundrobinFailOnNoServer = false; - auto server = pol.getSelectedBackend(servers, dq); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_CHECK(server == nullptr); - servers.push_back({ 1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53")) }); + servers.emplace_back(1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53"))); /* servers start as 'down' but the RR policy returns a server unless g_roundrobinFailOnNoServer is set */ g_roundrobinFailOnNoServer = true; - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_CHECK(server == nullptr); g_roundrobinFailOnNoServer = false; - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_CHECK(server != nullptr); /* mark the server as 'up' */ servers.at(0).second->setUp(); - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_CHECK(server != nullptr); /* add a second server, we should get the first one then the second one */ - servers.push_back({ 2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53")) }); + servers.emplace_back(2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53"))); servers.at(1).second->setUp(); - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(server != nullptr); BOOST_CHECK(server == servers.at(0).second); - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(server != nullptr); BOOST_CHECK(server == servers.at(1).second); /* mark the first server as 'down', second as 'up' */ servers.at(0).second->setDown(); servers.at(1).second->setUp(); - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(server != nullptr); BOOST_CHECK(server == servers.at(1).second); std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap; /* mark all servers 'up' */ - for (auto& s : servers) { - s.second->setUp(); - serversMap[s.second] = 0; + for (auto& serv : servers) { + serv.second->setUp(); + serversMap[serv.second] = 0; } for (size_t idx = 0; idx < 1000; idx++) { - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } @@ -301,32 +281,33 @@ BOOST_AUTO_TEST_CASE(test_roundRobin) { benchPolicy(pol); } -BOOST_AUTO_TEST_CASE(test_leastOutstanding) { - auto dq = getDQ(); +BOOST_AUTO_TEST_CASE(test_leastOutstanding) +{ + auto dnsQuestion = getDQ(); ServerPolicy pol{"leastOutstanding", leastOutstanding, false}; ServerPolicy::NumberedServerVector servers; - servers.push_back({ 1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53")) }); + servers.emplace_back(1, std::make_shared<DownstreamState>(ComboAddress("192.0.2.1:53"))); /* servers start as 'down' */ - auto server = pol.getSelectedBackend(servers, dq); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_CHECK(server == nullptr); /* mark the server as 'up' */ servers.at(0).second->setUp(); - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_CHECK(server != nullptr); /* add a second server, we should still get the first one */ - servers.push_back({ 2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53")) }); - server = pol.getSelectedBackend(servers, dq); + servers.emplace_back(2, std::make_shared<DownstreamState>(ComboAddress("192.0.2.2:53"))); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(server != nullptr); BOOST_CHECK(server == servers.at(0).second); /* mark the first server as 'down', second as 'up' */ servers.at(0).second->setDown(); servers.at(1).second->setUp(); - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(server != nullptr); BOOST_CHECK(server == servers.at(1).second); @@ -334,21 +315,22 @@ BOOST_AUTO_TEST_CASE(test_leastOutstanding) { servers.at(0).second->setUp(); servers.at(0).second->outstanding = 42; servers.at(1).second->setUp(); - server = pol.getSelectedBackend(servers, dq); + server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(server != nullptr); BOOST_CHECK(server == servers.at(1).second); benchPolicy(pol); } -BOOST_AUTO_TEST_CASE(test_wrandom) { - auto dq = getDQ(); +BOOST_AUTO_TEST_CASE(test_wrandom) +{ + auto dnsQuestion = getDQ(); ServerPolicy pol{"wrandom", wrandom, false}; ServerPolicy::NumberedServerVector servers; std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap; for (size_t idx = 1; idx <= 10; idx++) { - servers.push_back({ idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")) }); + servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53"))); serversMap[servers.at(idx - 1).second] = 0; servers.at(idx - 1).second->setUp(); } @@ -356,7 +338,7 @@ BOOST_AUTO_TEST_CASE(test_wrandom) { benchPolicy(pol); for (size_t idx = 0; idx < 1000; idx++) { - auto server = pol.getSelectedBackend(servers, dq); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } @@ -381,10 +363,10 @@ BOOST_AUTO_TEST_CASE(test_wrandom) { BOOST_CHECK_EQUAL(entry.first->d_config.d_weight, 1); } /* change the weight of the last server to 100, default is 1 */ - servers.at(servers.size()-1).second->d_config.d_weight = 100; + servers.at(servers.size() - 1).second->d_config.d_weight = 100; for (size_t idx = 0; idx < 1000; idx++) { - auto server = pol.getSelectedBackend(servers, dq); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } @@ -396,25 +378,26 @@ BOOST_AUTO_TEST_CASE(test_wrandom) { totalW += entry.first->d_config.d_weight; } BOOST_CHECK_EQUAL(total, 1000U); - auto last = servers.at(servers.size()-1).second; + auto last = servers.at(servers.size() - 1).second; const auto got = serversMap[last]; - float expected = (1000 * 1.0 * last->d_config.d_weight) / totalW; + float expected = static_cast<float>(1000 * 1.0 * last->d_config.d_weight) / static_cast<float>(totalW); BOOST_CHECK_GT(got, expected / 2); BOOST_CHECK_LT(got, expected * 2); } -BOOST_AUTO_TEST_CASE(test_whashed) { +BOOST_AUTO_TEST_CASE(test_whashed) +{ std::vector<DNSName> names; names.reserve(1000); for (size_t idx = 0; idx < 1000; idx++) { - names.push_back(DNSName("powerdns-" + std::to_string(idx) + ".com.")); + names.emplace_back("powerdns-" + std::to_string(idx) + ".com."); } ServerPolicy pol{"whashed", whashed, false}; ServerPolicy::NumberedServerVector servers; std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap; for (size_t idx = 1; idx <= 10; idx++) { - servers.push_back({ idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")) }); + servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53"))); serversMap[servers.at(idx - 1).second] = 0; servers.at(idx - 1).second->setUp(); } @@ -422,8 +405,8 @@ BOOST_AUTO_TEST_CASE(test_whashed) { benchPolicy(pol); for (const auto& name : names) { - auto dq = getDQ(&name); - auto server = pol.getSelectedBackend(servers, dq); + auto dnsQuestion = getDQ(&name); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } @@ -445,10 +428,10 @@ BOOST_AUTO_TEST_CASE(test_whashed) { /* request 1000 times the same name, we should go to the same server every time */ { - auto dq = getDQ(&names.at(0)); - auto server = pol.getSelectedBackend(servers, dq); + auto dnsQuestion = getDQ(&names.at(0)); + auto server = pol.getSelectedBackend(servers, dnsQuestion); for (size_t idx = 0; idx < 1000; idx++) { - BOOST_CHECK(pol.getSelectedBackend(servers, dq) == server); + BOOST_CHECK(pol.getSelectedBackend(servers, dnsQuestion) == server); } } @@ -458,11 +441,11 @@ BOOST_AUTO_TEST_CASE(test_whashed) { BOOST_CHECK_EQUAL(entry.first->d_config.d_weight, 1); } /* change the weight of the last server to 100, default is 1 */ - servers.at(servers.size()-1).second->setWeight(100); + servers.at(servers.size() - 1).second->setWeight(100); for (const auto& name : names) { - auto dq = getDQ(&name); - auto server = pol.getSelectedBackend(servers, dq); + auto dnsQuestion = getDQ(&name); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } @@ -474,28 +457,29 @@ BOOST_AUTO_TEST_CASE(test_whashed) { totalW += entry.first->d_config.d_weight; } BOOST_CHECK_EQUAL(total, names.size()); - auto last = servers.at(servers.size()-1).second; + auto last = servers.at(servers.size() - 1).second; const auto got = serversMap[last]; - float expected = (names.size() * 1.0 * last->d_config.d_weight) / totalW; + float expected = static_cast<float>(static_cast<double>(names.size()) * 1.0 * last->d_config.d_weight) / static_cast<float>(totalW); BOOST_CHECK_GT(got, expected / 2); BOOST_CHECK_LT(got, expected * 2); } -BOOST_AUTO_TEST_CASE(test_chashed) { +BOOST_AUTO_TEST_CASE(test_chashed) +{ bool existingVerboseValue = g_verbose; g_verbose = false; std::vector<DNSName> names; names.reserve(1000); for (size_t idx = 0; idx < 1000; idx++) { - names.push_back(DNSName("powerdns-" + std::to_string(idx) + ".com.")); + names.emplace_back("powerdns-" + std::to_string(idx) + ".com."); } ServerPolicy pol{"chashed", chashed, false}; ServerPolicy::NumberedServerVector servers; std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap; for (size_t idx = 1; idx <= 10; idx++) { - servers.push_back({ idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")) }); + servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53"))); serversMap[servers.at(idx - 1).second] = 0; servers.at(idx - 1).second->setUp(); /* we need to have a weight of at least 1000 to get an optimal repartition with the consistent hashing algo */ @@ -507,8 +491,8 @@ BOOST_AUTO_TEST_CASE(test_chashed) { benchPolicy(pol); for (const auto& name : names) { - auto dq = getDQ(&name); - auto server = pol.getSelectedBackend(servers, dq); + auto dnsQuestion = getDQ(&name); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } @@ -530,10 +514,10 @@ BOOST_AUTO_TEST_CASE(test_chashed) { /* request 1000 times the same name, we should go to the same server every time */ { - auto dq = getDQ(&names.at(0)); - auto server = pol.getSelectedBackend(servers, dq); + auto dnsQuestion = getDQ(&names.at(0)); + auto server = pol.getSelectedBackend(servers, dnsQuestion); for (size_t idx = 0; idx < 1000; idx++) { - BOOST_CHECK(pol.getSelectedBackend(servers, dq) == server); + BOOST_CHECK(pol.getSelectedBackend(servers, dnsQuestion) == server); } } @@ -543,11 +527,11 @@ BOOST_AUTO_TEST_CASE(test_chashed) { BOOST_CHECK_EQUAL(entry.first->d_config.d_weight, 1000); } /* change the weight of the last server to 100000, others stay at 1000 */ - servers.at(servers.size()-1).second->setWeight(100000); + servers.at(servers.size() - 1).second->setWeight(100000); for (const auto& name : names) { - auto dq = getDQ(&name); - auto server = pol.getSelectedBackend(servers, dq); + auto dnsQuestion = getDQ(&name); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } @@ -559,20 +543,21 @@ BOOST_AUTO_TEST_CASE(test_chashed) { totalW += entry.first->d_config.d_weight; } BOOST_CHECK_EQUAL(total, names.size()); - auto last = servers.at(servers.size()-1).second; + auto last = servers.at(servers.size() - 1).second; const auto got = serversMap[last]; - float expected = (names.size() * 1.0 * last->d_config.d_weight) / totalW; + float expected = static_cast<float>(static_cast<double>(names.size()) * 1.0 * last->d_config.d_weight) / static_cast<float>(totalW); BOOST_CHECK_GT(got, expected / 2); BOOST_CHECK_LT(got, expected * 2); g_verbose = existingVerboseValue; } -BOOST_AUTO_TEST_CASE(test_lua) { +BOOST_AUTO_TEST_CASE(test_lua) +{ std::vector<DNSName> names; names.reserve(1000); for (size_t idx = 0; idx < 1000; idx++) { - names.push_back(DNSName("powerdns-" + std::to_string(idx) + ".com.")); + names.emplace_back("powerdns-" + std::to_string(idx) + ".com."); } static const std::string policySetupStr = R"foo( @@ -585,9 +570,9 @@ BOOST_AUTO_TEST_CASE(test_lua) { setServerPolicyLua("luaroundrobin", luaroundrobin) )foo"; resetLuaContext(); - g_lua.lock()->writeFunction("setServerPolicyLua", [](string name, ServerPolicy::policyfunc_t policy) { - g_policy.setState(ServerPolicy{name, policy, true}); - }); + g_lua.lock()->writeFunction("setServerPolicyLua", [](const string& name, const ServerPolicy::policyfunc_t& policy) { + g_policy.setState(ServerPolicy{name, policy, true}); + }); g_lua.lock()->executeCode(policySetupStr); { @@ -595,15 +580,15 @@ BOOST_AUTO_TEST_CASE(test_lua) { ServerPolicy::NumberedServerVector servers; std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap; for (size_t idx = 1; idx <= 10; idx++) { - servers.push_back({ idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")) }); + servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53"))); serversMap[servers.at(idx - 1).second] = 0; servers.at(idx - 1).second->setUp(); } BOOST_REQUIRE_EQUAL(servers.size(), 10U); for (const auto& name : names) { - auto dq = getDQ(&name); - auto server = pol.getSelectedBackend(servers, dq); + auto dnsQuestion = getDQ(&name); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } @@ -624,11 +609,12 @@ BOOST_AUTO_TEST_CASE(test_lua) { #ifdef LUAJIT_VERSION -BOOST_AUTO_TEST_CASE(test_lua_ffi_rr) { +BOOST_AUTO_TEST_CASE(test_lua_ffi_rr) +{ std::vector<DNSName> names; names.reserve(1000); for (size_t idx = 0; idx < 1000; idx++) { - names.push_back(DNSName("powerdns-" + std::to_string(idx) + ".com.")); + names.emplace_back("powerdns-" + std::to_string(idx) + ".com."); } static const std::string policySetupStr = R"foo( @@ -645,9 +631,9 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_rr) { )foo"; resetLuaContext(); g_lua.lock()->executeCode(getLuaFFIWrappers()); - g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { - g_policy.setState(ServerPolicy(name, policy)); - }); + g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](const string& name, const ServerPolicy::ffipolicyfunc_t& policy) { + g_policy.setState(ServerPolicy(name, policy)); + }); g_lua.lock()->executeCode(policySetupStr); { @@ -655,15 +641,15 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_rr) { ServerPolicy::NumberedServerVector servers; std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap; for (size_t idx = 1; idx <= 10; idx++) { - servers.push_back({ idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")) }); + servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53"))); serversMap[servers.at(idx - 1).second] = 0; servers.at(idx - 1).second->setUp(); } BOOST_REQUIRE_EQUAL(servers.size(), 10U); for (const auto& name : names) { - auto dq = getDQ(&name); - auto server = pol.getSelectedBackend(servers, dq); + auto dnsQuestion = getDQ(&name); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } @@ -682,11 +668,50 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_rr) { resetLuaContext(); } -BOOST_AUTO_TEST_CASE(test_lua_ffi_hashed) { +BOOST_AUTO_TEST_CASE(test_lua_ffi_no_server_available) +{ + DNSName dnsName("powerdns.com."); + static const std::string policySetupStr = R"foo( + local ffi = require("ffi") + local C = ffi.C + local counter = 0 + function ffipolicy(servers_list, dq) + local serversCount = tonumber(C.dnsdist_ffi_servers_list_get_count(servers_list)) + -- return clearly out of bounds value to indicate that no server can be used + return serversCount + 100 + end + + setServerPolicyLuaFFI("FFI policy", ffipolicy) + )foo"; + resetLuaContext(); + g_lua.lock()->executeCode(getLuaFFIWrappers()); + g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](const string& policyName, ServerPolicy::ffipolicyfunc_t policy) { + g_policy.setState(ServerPolicy(policyName, std::move(policy))); + }); + g_lua.lock()->executeCode(policySetupStr); + + { + ServerPolicy pol = g_policy.getCopy(); + ServerPolicy::NumberedServerVector servers; + for (size_t idx = 1; idx <= 10; idx++) { + servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53"))); + servers.at(idx - 1).second->setUp(); + } + BOOST_REQUIRE_EQUAL(servers.size(), 10U); + + auto dnsQuestion = getDQ(&dnsName); + auto server = pol.getSelectedBackend(servers, dnsQuestion); + BOOST_REQUIRE(server == nullptr); + } + resetLuaContext(); +} + +BOOST_AUTO_TEST_CASE(test_lua_ffi_hashed) +{ std::vector<DNSName> names; names.reserve(1000); for (size_t idx = 0; idx < 1000; idx++) { - names.push_back(DNSName("powerdns-" + std::to_string(idx) + ".com.")); + names.emplace_back("powerdns-" + std::to_string(idx) + ".com."); } static const std::string policySetupStr = R"foo( @@ -702,9 +727,9 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_hashed) { )foo"; resetLuaContext(); g_lua.lock()->executeCode(getLuaFFIWrappers()); - g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { - g_policy.setState(ServerPolicy(name, policy)); - }); + g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](const string& name, const ServerPolicy::ffipolicyfunc_t& policy) { + g_policy.setState(ServerPolicy(name, policy)); + }); g_lua.lock()->executeCode(policySetupStr); { @@ -712,15 +737,15 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_hashed) { ServerPolicy::NumberedServerVector servers; std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap; for (size_t idx = 1; idx <= 10; idx++) { - servers.push_back({ idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")) }); + servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53"))); serversMap[servers.at(idx - 1).second] = 0; servers.at(idx - 1).second->setUp(); } BOOST_REQUIRE_EQUAL(servers.size(), 10U); for (const auto& name : names) { - auto dq = getDQ(&name); - auto server = pol.getSelectedBackend(servers, dq); + auto dnsQuestion = getDQ(&name); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } @@ -739,11 +764,12 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_hashed) { resetLuaContext(); } -BOOST_AUTO_TEST_CASE(test_lua_ffi_whashed) { +BOOST_AUTO_TEST_CASE(test_lua_ffi_whashed) +{ std::vector<DNSName> names; names.reserve(1000); for (size_t idx = 0; idx < 1000; idx++) { - names.push_back(DNSName("powerdns-" + std::to_string(idx) + ".com.")); + names.emplace_back("powerdns-" + std::to_string(idx) + ".com."); } static const std::string policySetupStr = R"foo( @@ -757,9 +783,9 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_whashed) { )foo"; resetLuaContext(); g_lua.lock()->executeCode(getLuaFFIWrappers()); - g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { - g_policy.setState(ServerPolicy(name, policy)); - }); + g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](const string& name, const ServerPolicy::ffipolicyfunc_t& policy) { + g_policy.setState(ServerPolicy(name, policy)); + }); g_lua.lock()->executeCode(policySetupStr); { @@ -767,15 +793,15 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_whashed) { ServerPolicy::NumberedServerVector servers; std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap; for (size_t idx = 1; idx <= 10; idx++) { - servers.push_back({ idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")) }); + servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53"))); serversMap[servers.at(idx - 1).second] = 0; servers.at(idx - 1).second->setUp(); } BOOST_REQUIRE_EQUAL(servers.size(), 10U); for (const auto& name : names) { - auto dq = getDQ(&name); - auto server = pol.getSelectedBackend(servers, dq); + auto dnsQuestion = getDQ(&name); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } @@ -794,14 +820,15 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_whashed) { resetLuaContext(); } -BOOST_AUTO_TEST_CASE(test_lua_ffi_chashed) { +BOOST_AUTO_TEST_CASE(test_lua_ffi_chashed) +{ bool existingVerboseValue = g_verbose; g_verbose = false; std::vector<DNSName> names; names.reserve(1000); for (size_t idx = 0; idx < 1000; idx++) { - names.push_back(DNSName("powerdns-" + std::to_string(idx) + ".com.")); + names.emplace_back("powerdns-" + std::to_string(idx) + ".com."); } static const std::string policySetupStr = R"foo( @@ -815,9 +842,9 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_chashed) { )foo"; resetLuaContext(); g_lua.lock()->executeCode(getLuaFFIWrappers()); - g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](string name, ServerPolicy::ffipolicyfunc_t policy) { - g_policy.setState(ServerPolicy(name, policy)); - }); + g_lua.lock()->writeFunction("setServerPolicyLuaFFI", [](const string& name, const ServerPolicy::ffipolicyfunc_t& policy) { + g_policy.setState(ServerPolicy(name, policy)); + }); g_lua.lock()->executeCode(policySetupStr); { @@ -825,7 +852,7 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_chashed) { ServerPolicy::NumberedServerVector servers; std::map<std::shared_ptr<DownstreamState>, uint64_t> serversMap; for (size_t idx = 1; idx <= 10; idx++) { - servers.push_back({ idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53")) }); + servers.emplace_back(idx, std::make_shared<DownstreamState>(ComboAddress("192.0.2." + std::to_string(idx) + ":53"))); serversMap[servers.at(idx - 1).second] = 0; servers.at(idx - 1).second->setUp(); /* we need to have a weight of at least 1000 to get an optimal repartition with the consistent hashing algo */ @@ -836,8 +863,8 @@ BOOST_AUTO_TEST_CASE(test_lua_ffi_chashed) { BOOST_REQUIRE_EQUAL(servers.size(), 10U); for (const auto& name : names) { - auto dq = getDQ(&name); - auto server = pol.getSelectedBackend(servers, dq); + auto dnsQuestion = getDQ(&name); + auto server = pol.getSelectedBackend(servers, dnsQuestion); BOOST_REQUIRE(serversMap.count(server) == 1); ++serversMap[server]; } |