2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-22 09:57:41 +00:00

[#2129] Made stat test tools more generic

This commit is contained in:
Francis Dupont 2021-10-11 16:40:48 +02:00
parent ad5591fa6e
commit b7a3c2d9b0
6 changed files with 84 additions and 52 deletions

View File

@ -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])

View File

@ -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 =

View File

@ -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 <cc/data.h>
#include <d2srv/d2_stats.h>
#include <d2srv/d2_tsig_key.h>
#include <stats/stats_mgr.h>
#include <stats/testutils/stats_test_utils.h>
#include <gtest/gtest.h>
@ -18,8 +18,10 @@ namespace isc {
namespace d2 {
namespace test {
/// @brief Type of name x value for statistics.
typedef std::map<std::string, int64_t> 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

View File

@ -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)

View File

@ -0,0 +1 @@
EXTRA_DIST = stats_test_utils.h

View File

@ -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