diff --git a/configure.ac b/configure.ac index 5564c9eab7..c2d2b5073d 100644 --- a/configure.ac +++ b/configure.ac @@ -1722,7 +1722,7 @@ AC_CONFIG_FILES([src/lib/process/Makefile]) AC_CONFIG_FILES([src/lib/process/tests/Makefile]) AC_CONFIG_FILES([src/lib/process/testutils/Makefile]) AC_CONFIG_FILES([src/lib/stats/Makefile]) -AC_CONFIG_FILES([src/lib/stats/tests/Makefile]) +AC_CONFIG_FILES([src/lib/stats/testutils/Makefile]) AC_CONFIG_FILES([src/lib/testutils/Makefile]) AC_CONFIG_FILES([src/lib/testutils/dhcp_test_lib.sh], [chmod +x src/lib/testutils/dhcp_test_lib.sh]) diff --git a/src/lib/d2srv/testutils/stats_test_utils.cc b/src/lib/d2srv/testutils/stats_test_utils.cc index c01c461cfd..262879b421 100644 --- a/src/lib/d2srv/testutils/stats_test_utils.cc +++ b/src/lib/d2srv/testutils/stats_test_utils.cc @@ -26,22 +26,7 @@ D2StatTest::~D2StatTest() { } void -D2StatTest::checkStat(const string& name, const int64_t expected_value) { - ObservationPtr obs = StatsMgr::instance().getObservation(name); - ASSERT_TRUE(obs) << " stat: " << name << " not found "; - ASSERT_EQ(expected_value, obs->getInteger().first) - << " stat: " << name << " value wrong"; -} - -void -D2StatTest::checkStats(const StatMap& expected_stats) { - for (const auto& it : expected_stats) { - checkStat(it.first, it.second); - } -} - -void -D2StatTest::checkStats(const string& key_name, const StatMap& expected_stats) { +checkStats(const string& key_name, const StatMap& expected_stats) { StatMap key_stats; for (const auto& it : expected_stats) { const string& stat_name = diff --git a/src/lib/d2srv/testutils/stats_test_utils.h b/src/lib/d2srv/testutils/stats_test_utils.h index 4b32b9c090..ccdad02acb 100644 --- a/src/lib/d2srv/testutils/stats_test_utils.h +++ b/src/lib/d2srv/testutils/stats_test_utils.h @@ -4,13 +4,13 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. -#ifndef STATS_TEST_UTILS_H -#define STATS_TEST_UTILS_H +#ifndef D2_STATS_TEST_UTILS_H +#define D2_STATS_TEST_UTILS_H #include #include #include -#include +#include #include @@ -18,8 +18,10 @@ namespace isc { namespace d2 { namespace test { -/// @brief Type of name x value for statistics. -typedef std::map StatMap; +/// @brief Import statistic test utils. +using isc::stats::test::StatMap; +using isc::stats::test::checkStat; +using isc::stats::test::checkStats; /// @brief Test class with utility functions to test statistics. class D2StatTest { @@ -29,38 +31,18 @@ public: /// @brief Destructor. virtual ~D2StatTest(); - - /// @brief Compares a statistic to an expected value. - /// - /// Attempt to fetch the named statistic from the StatsMgr and if - /// found, compare its observed value to the given value. - /// Fails if the stat is not found or if the values do not match. - /// - /// @param name StatsMgr name for the statistic to check. - /// @param expected_value expected value of the statistic. - void checkStat(const std::string& name, const int64_t expected_value); - - /// @brief Compares StatsMgr statistics against expected values. - /// - /// Iterates over a list of statistic names and expected values, attempting - /// to fetch each from the StatsMgr and if found, compare its observed - /// value to the expected value. Fails if any of the expected stats are not - /// found or if the values do not match. - /// - /// @param expected_stats Map of expected static names and values. - void checkStats(const StatMap& expected_stats); - - /// @brief Compares StatsMgr key statistics against expected values. - /// - /// Prepend key part of names before calling checkStats simpler variant. - /// - /// @param key_name Name of the key. - /// @param expected_stats Map of expected static names and values. - void checkStats(const std::string& key_name, const StatMap& expected_stats); }; +/// @brief Compares StatsMgr key statistics against expected values. +/// +/// Prepend key part of names before calling checkStats simpler variant. +/// +/// @param key_name Name of the key. +/// @param expected_stats Map of expected static names and values. +void checkStats(const std::string& key_name, const StatMap& expected_stats); + } } } -#endif // STATS_TEST_UTILS_H +#endif // D2_STATS_TEST_UTILS_H diff --git a/src/lib/stats/Makefile.am b/src/lib/stats/Makefile.am index aa7568a769..37f3e41f15 100644 --- a/src/lib/stats/Makefile.am +++ b/src/lib/stats/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = . tests +SUBDIRS = . tests testutils AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib AM_CPPFLAGS += $(BOOST_INCLUDES) diff --git a/src/lib/stats/testutils/Makefile.am b/src/lib/stats/testutils/Makefile.am new file mode 100644 index 0000000000..009257f6b6 --- /dev/null +++ b/src/lib/stats/testutils/Makefile.am @@ -0,0 +1 @@ +EXTRA_DIST = stats_test_utils.h diff --git a/src/lib/stats/testutils/stats_test_utils.h b/src/lib/stats/testutils/stats_test_utils.h new file mode 100644 index 0000000000..808dced39a --- /dev/null +++ b/src/lib/stats/testutils/stats_test_utils.h @@ -0,0 +1,64 @@ +// Copyright (C) 2020-2021 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#ifndef STATS_TEST_UTILS_H +#define STATS_TEST_UTILS_H + +#include +#include + +#include + +namespace isc { +namespace stats { +namespace test { + +/// @brief Type of name x value for statistics. +typedef std::map StatMap; + +/// @brief Compares a statistic to an expected value. +/// +/// Attempt to fetch the named statistic from the StatsMgr and if +/// found, compare its observed value to the given value. +/// Fails if the stat is not found or if the values do not match. +/// +/// @param name StatsMgr name for the statistic to check. +/// @param expected_value expected value of the statistic. +inline void checkStat(const std::string& name, const int64_t expected_value) { + using namespace isc::stats; + ObservationPtr obs = StatsMgr::instance().getObservation(name); + ASSERT_TRUE(obs) << " stat: " << name << " not found "; + ASSERT_EQ(expected_value, obs->getInteger().first) + << " stat: " << name << " value wrong"; +} + +/// @brief Check if a statistic does not exists. +/// +/// @param name StatsMgr name for the statistic to check. +inline void checkNoStat(const std::string& name) { + using namespace isc::stats; + EXPECT_FALSE(StatsMgr::instance().getObservation(name)); +} + +/// @brief Compares StatsMgr statistics against expected values. +/// +/// Iterates over a list of statistic names and expected values, attempting +/// to fetch each from the StatsMgr and if found, compare its observed +/// value to the expected value. Fails if any of the expected stats are not +/// found or if the values do not match. +/// +/// @param expected_stats Map of expected static names and values. +inline void checkStats(const StatMap& expected_stats) { + for (const auto& it : expected_stats) { + checkStat(it.first, it.second); + } +} + +} +} +} + +#endif // STATS_TEST_UTILS_H