From: Razvan Becheriu Date: Wed, 21 Feb 2018 17:40:37 +0000 (+0200) Subject: fixed unit tests X-Git-Tag: kea5574_base~10^2~1^2~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=239f3c0a1176fb6e867ed4596f3e639f59d0b084;p=thirdparty%2Fkea.git fixed unit tests --- diff --git a/src/lib/dhcpsrv/cql_host_data_source.cc b/src/lib/dhcpsrv/cql_host_data_source.cc index 2fecda4c4b..cd786f08e8 100644 --- a/src/lib/dhcpsrv/cql_host_data_source.cc +++ b/src/lib/dhcpsrv/cql_host_data_source.cc @@ -267,6 +267,11 @@ public: static constexpr StatementTag INSERT_HOST = "INSERT_HOST"; + // Retrieves hosts informations, IPv6 reservations and both IPv4 and IPv6 + // options associated with the hosts. + static constexpr StatementTag GET_HOST = + "GET_HOST"; + // Retrieves host information, IPv6 reservations and both IPv4 and IPv6 // options associated with the host. static constexpr StatementTag GET_HOST_BY_HOST_ID = @@ -410,6 +415,7 @@ private: }; // CqlHostExchange constexpr StatementTag CqlHostExchange::INSERT_HOST; +constexpr StatementTag CqlHostExchange::GET_HOST; constexpr StatementTag CqlHostExchange::GET_HOST_BY_HOST_ID; constexpr StatementTag CqlHostExchange::GET_HOST_BY_IPV4_ADDRESS; constexpr StatementTag CqlHostExchange::GET_HOST_BY_IPV4_SUBNET_ID_AND_HOST_ID; @@ -461,6 +467,39 @@ StatementMap CqlHostExchange::tagged_statements_ = { "IF NOT EXISTS " }}, + {GET_HOST, + {GET_HOST, + "SELECT " + "id, " + "host_identifier, " + "host_identifier_type, " + "host_ipv4_subnet_id, " + "host_ipv6_subnet_id, " + "host_ipv4_address, " + "host_ipv4_next_server, " + "host_ipv4_server_hostname, " + "host_ipv4_boot_file_name, " + "hostname, " + "user_context, " + "host_ipv4_client_classes, " + "host_ipv6_client_classes, " + "reserved_ipv6_prefix_address, " + "reserved_ipv6_prefix_length, " + "reserved_ipv6_prefix_address_type, " + "iaid, " + "option_universe, " + "option_code, " + "option_value, " + "option_formatted_value, " + "option_space, " + "option_is_persistent, " + "option_client_class, " + "option_subnet_id, " + "option_user_context, " + "option_scope_id " + "FROM host_reservations " + }}, + {GET_HOST_BY_HOST_ID, {GET_HOST_BY_HOST_ID, "SELECT " @@ -1436,6 +1475,12 @@ public: virtual ConstHostCollection getAll4(const asiolink::IOAddress& address) const; + /// @brief Implementation of @ref CqlHostDataSource::getAllHosts() + /// + /// See @ref CqlHostDataSource::getAllHosts() for parameter details. + virtual ConstHostCollection + getAllHosts() const; + /// @brief Implementation of @ref CqlHostDataSource::getName() virtual std::string getName() const; @@ -1905,6 +1950,18 @@ CqlHostDataSourceImpl::getAll4(const asiolink::IOAddress& address) const { return (result); } +ConstHostCollection +CqlHostDataSourceImpl::getAllHosts() const { + + // Bind to array. + AnyArray where_values; + + // Run statement. + ConstHostCollection result = getHostCollection(CqlHostExchange::GET_HOST, where_values); + + return (result); +} + std::string CqlHostDataSourceImpl::getName() const { std::string name; @@ -2227,6 +2284,11 @@ CqlHostDataSource::get6(const SubnetID& subnet_id, return (impl_->get6(subnet_id, address)); } +ConstHostCollection +CqlHostDataSource::getAllHosts() const { + return (impl_->getAllHosts()); +} + std::string CqlHostDataSource::getType() const { return std::string("cql"); diff --git a/src/lib/dhcpsrv/cql_host_data_source.h b/src/lib/dhcpsrv/cql_host_data_source.h index f9dd970b15..da17a4af2c 100644 --- a/src/lib/dhcpsrv/cql_host_data_source.h +++ b/src/lib/dhcpsrv/cql_host_data_source.h @@ -312,6 +312,14 @@ public: get6(const SubnetID& subnet_id, const asiolink::IOAddress& address) const override; + /// @brief Returns a collection of all the hosts. + /// + /// This method may return multiple @ref Host objects. + /// + /// @return Collection of const @ref Host objects. + virtual ConstHostCollection + getAllHosts() const; + /// @brief Returns textual description of the backend. /// /// @return Description of the backend. diff --git a/src/lib/dhcpsrv/tests/cql_host_data_source_unittest.cc b/src/lib/dhcpsrv/tests/cql_host_data_source_unittest.cc index 314187cfb9..16ebc84b03 100644 --- a/src/lib/dhcpsrv/tests/cql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/cql_host_data_source_unittest.cc @@ -108,6 +108,69 @@ public: HostDataSourceFactory::create(validCqlConnectionString()); hdsptr_ = HostDataSourceFactory::getHostDataSourcePtr(); } + + /// @brief Returns number of IPv4 options currently stored in DB. + virtual int countDBOptions4() { + int result = 0; + + const CqlHostDataSource* cql_host_mgr = dynamic_cast(&(*hdsptr_)); + ConstHostCollection all = cql_host_mgr->getAllHosts(); + + for (ConstHostCollection::const_iterator it = all.begin(); + it != all.end(); ++it) { + ConstCfgOptionPtr cfg_option4 = (*it)->getCfgOption4(); + std::list option_spaces4 = cfg_option4->getOptionSpaceNames(); + std::list vendor_spaces4 = cfg_option4->getVendorIdsSpaceNames(); + option_spaces4.insert(option_spaces4.end(), vendor_spaces4.begin(), + vendor_spaces4.end()); + for (const std::string& space : option_spaces4) { + OptionContainerPtr options = cfg_option4->getAll(space); + result += options->size(); + } + } + + return (result); + } + + /// @brief Returns number of IPv6 options currently stored in DB. + virtual int countDBOptions6() { + int result = 0; + + const CqlHostDataSource* cql_host_mgr = dynamic_cast(&(*hdsptr_)); + ConstHostCollection all = cql_host_mgr->getAllHosts(); + + for (ConstHostCollection::const_iterator it = all.begin(); + it != all.end(); ++it) { + ConstCfgOptionPtr cfg_option6 = (*it)->getCfgOption6(); + std::list option_spaces6 = cfg_option6->getOptionSpaceNames(); + std::list vendor_spaces6 = cfg_option6->getVendorIdsSpaceNames(); + option_spaces6.insert(option_spaces6.end(), vendor_spaces6.begin(), + vendor_spaces6.end()); + for (const std::string& space : option_spaces6) { + OptionContainerPtr options = cfg_option6->getAll(space); + result += options->size(); + } + } + + return (result); + } + + /// @brief Returns number of IPv6 reservations currently stored in DB. + virtual int countDBReservations6() { + int result = 0; + + const CqlHostDataSource* cql_host_mgr = dynamic_cast(&(*hdsptr_)); + ConstHostCollection all = cql_host_mgr->getAllHosts(); + + for (ConstHostCollection::const_iterator it = all.begin(); + it != all.end(); ++it) { + IPv6ResrvRange reservations = (*it)->getIPv6Reservations(); + result += std::distance(reservations.first, reservations.second); + } + + return (result); + } + }; /// @brief Check that database can be opened diff --git a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc index 3ffd453036..4722a9e3f8 100644 --- a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc @@ -131,7 +131,7 @@ public: return (countRowsInTable("dhcp4_options")); } - /// @brief Returns number of IPv4 options currently stored in DB. + /// @brief Returns number of IPv6 options currently stored in DB. virtual int countDBOptions6() { return (countRowsInTable("dhcp6_options")); } diff --git a/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc b/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc index 1d0aac8eea..b896cca5e1 100644 --- a/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc @@ -129,7 +129,7 @@ public: return (countRowsInTable("dhcp4_options")); } - /// @brief Returns number of IPv4 options currently stored in DB. + /// @brief Returns number of IPv6 options currently stored in DB. virtual int countDBOptions6() { return (countRowsInTable("dhcp6_options")); }