summaryrefslogtreecommitdiffstats
path: root/src/lib/stats
diff options
context:
space:
mode:
authorFrancis Dupont <fdupont@isc.org>2021-10-11 16:40:48 +0200
committerFrancis Dupont <fdupont@isc.org>2021-10-11 17:36:57 +0200
commitb7a3c2d9b0869b51c87c29f1bb49598532d8b064 (patch)
treeac19f30ce92cd00fd29c3a9ef31dd2ef74d6c5e9 /src/lib/stats
parent[#2125] Typos (diff)
downloadkea-b7a3c2d9b0869b51c87c29f1bb49598532d8b064.tar.xz
kea-b7a3c2d9b0869b51c87c29f1bb49598532d8b064.zip
[#2129] Made stat test tools more generic
Diffstat (limited to 'src/lib/stats')
-rw-r--r--src/lib/stats/Makefile.am2
-rw-r--r--src/lib/stats/testutils/Makefile.am1
-rw-r--r--src/lib/stats/testutils/stats_test_utils.h64
3 files changed, 66 insertions, 1 deletions
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 <cc/data.h>
+#include <stats/stats_mgr.h>
+
+#include <gtest/gtest.h>
+
+namespace isc {
+namespace stats {
+namespace test {
+
+/// @brief Type of name x value for statistics.
+typedef std::map<std::string, int64_t> 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