Skip to content

Commit

Permalink
[OMON-445] Make test more scope oriented (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn authored Apr 13, 2021
1 parent b3b7246 commit 779be58
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 202 deletions.
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ endif()
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

find_package(Boost REQUIRED COMPONENTS unit_test_framework program_options system filesystem)
find_package(Git QUIET)
find_package(ApMon MODULE)
find_package(CURL MODULE)
find_package(RdKafka CONFIG)
Expand Down Expand Up @@ -216,15 +215,14 @@ set_target_properties(8-DbFiller PROPERTIES OUTPUT_NAME "o2-monitoring-dbfiller"
enable_testing()

set(TEST_SRCS
test/testMonitoring.cxx
test/testMonitoringFactory.cxx
test/testDerived.cxx
test/testMetric.cxx
test/testProcessDetails.cxx
test/testProcessMonitor.cxx
test/testInfluxDb.cxx
test/testNoop.cxx
test/testRegexMatch.cxx
test/testMonitoring.cxx
test/testVerbosity.cxx
)

if(ApMon_FOUND)
Expand Down
40 changes: 0 additions & 40 deletions test/testApMon.cxx

This file was deleted.

2 changes: 1 addition & 1 deletion test/testDerived.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <vector>
#include <iostream>

#define BOOST_TEST_MODULE Test Monitoring DerivedMetrics
#define BOOST_TEST_MODULE Monitoring DerivedMetrics
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

Expand Down
75 changes: 0 additions & 75 deletions test/testInfluxDb.cxx

This file was deleted.

111 changes: 83 additions & 28 deletions test/testMonitoring.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

#include <chrono>
#include <vector>

#define BOOST_TEST_MODULE Test Monitoring Colletor
#define BOOST_TEST_MODULE Monitoring Interface
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <boost/lexical_cast.hpp>
#include "Monitoring/MonitoringFactory.h"

#include "../include/Monitoring/MonitoringFactory.h"
using namespace std::string_literals;

namespace o2
{
Expand All @@ -25,39 +22,97 @@ namespace monitoring
namespace Test
{

using Monitoring = o2::monitoring::MonitoringFactory;
auto monitoring = MonitoringFactory::Get("influxdb-stdout://");
std::stringstream coutRedirect;
std::streambuf* coutBuffer;

void enableRedirect()
{
coutBuffer = std::cout.rdbuf(coutRedirect.rdbuf());
}

BOOST_AUTO_TEST_CASE(createMonitoring)
void disableRedirect()
{
auto monitoring = Monitoring::Get("stdout://");
coutRedirect.str(std::string());
std::cout.rdbuf(coutBuffer);
}

int intMetric = 10;
std::string stringMetric("monitoringString");
double doubleMetric = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
void removeRandomValues(std::string& metric)
{
metric.erase(metric.rfind(' ')); // remove timestamp
auto begHost = metric.find(",hostname");
auto endHost = metric.find(",", begHost+1);
metric.erase(begHost, endHost-begHost);
}

monitoring->addGlobalTag("name", "Readout");
monitoring->addGlobalTag(tags::Key::Name, tags::Value::Readout);
BOOST_AUTO_TEST_CASE(parseDataPoints)
{
enableRedirect();

monitoring->send(Metric{"card"}
.addValue(40.217773, "temperature")
.addValue(0, "droppedPackets")
.addValue(0.768170, "ctpClock")
.addValue(240.471130, "localClock")
.addValue(0, "totalPacketsPerSecond")
.addTag(tags::Key::ID, 3)
.addTag(tags::Key::Type, tags::Value::CRU)
);
std::string expected1 = "card,id=3,type=CRU temperature=40.2178,droppedPackets=0i,ctpClock=0.76817,localClock=240.471,totalPacketsPerSecond=0i";
std::string returned1 = coutRedirect.str();

monitoring->send({intMetric, "myCrazyMetricI"});
monitoring->send({stringMetric, "myCrazyMetricS"});
monitoring->send({doubleMetric, "myCrazyMetricD"});
disableRedirect();
removeRandomValues(returned1);
BOOST_CHECK(expected1 == returned1);

enableRedirect();
monitoring->send(Metric{"link"}
.addValue("GBT/GBT"s, "gbtMode")
.addValue("None"s, "loopback")
.addValue("TTC:CTP"s, "gbtMux")
.addValue("Continuous"s, "datapathMode")
.addValue("Disabled"s, "datapath")
.addValue(181.370575, "rxFreq")
.addValue(196.250259, "txFreq")
.addValue(0, "status")
.addValue(657.400024, "opticalPower")
.addTag(tags::Key::CRU, 3)
.addTag(tags::Key::ID, 3)
);
std::string expected2 = R"(link,CRU=3,id=3 gbtMode="GBT/GBT",loopback="None",gbtMux="TTC:CTP",datapathMode="Continuous",datapath="Disabled",rxFreq=181.371,txFreq=196.25,status=0i,opticalPower=657.4)";
std::string returned2 = coutRedirect.str();
disableRedirect();
removeRandomValues(returned2);
BOOST_CHECK(expected2 == returned2);
}

BOOST_AUTO_TEST_CASE(buffering)
BOOST_AUTO_TEST_CASE(testSettingRun)
{
auto monitoring = Monitoring::Get("stdout://,influxdb-udp://localhost:1234");
monitoring->enableBuffering(10);
for (int i = 0; i < 25; i++) {
monitoring->send({10, "myMetricInt"});
}
monitoring->flushBuffer();
enableRedirect();

monitoring->setRunNumber(1234);
monitoring->send(Metric{4321, "test"});
std::string expected = "test,run=1234 value=4321i";
std::string returned = coutRedirect.str();

disableRedirect();
removeRandomValues(returned);
BOOST_CHECK(expected == returned);
}

BOOST_AUTO_TEST_CASE(testSymbols)
BOOST_AUTO_TEST_CASE(testGlobalTags)
{
BOOST_WARN_MESSAGE(!BOOST_IS_DEFINED(O2_MONITORING_WITH_APPMON), "ApMon Backend disabled");
BOOST_WARN_MESSAGE(BOOST_IS_DEFINED(O2_MONITORING_OS_LINUX), "Linux OS detected");
BOOST_WARN_MESSAGE(BOOST_IS_DEFINED(O2_MONITORING_OS_MAC), "Mac OS detected");
enableRedirect();

monitoring->addGlobalTag("custom_name", "Monitoring");
monitoring->addGlobalTag(tags::Key::Name, tags::Value::Readout);
monitoring->send(Metric{4321, "test"});
std::string expected = "test,custom_name=Monitoring,name=Readout,run=1234 value=4321i";
std::string returned = coutRedirect.str();

disableRedirect();
removeRandomValues(returned);
BOOST_CHECK(expected == returned);
}

} // namespace Test
Expand Down
45 changes: 31 additions & 14 deletions test/testMonitoringFactory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
#include <chrono>
#include <vector>

#define BOOST_TEST_MODULE TestMonitoringVerbosityy
#define BOOST_TEST_MODULE Monitoring Factory
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>

#include "../include/Monitoring/MonitoringFactory.h"

Expand All @@ -27,24 +29,39 @@ namespace Test

using Monitoring = o2::monitoring::MonitoringFactory;

BOOST_AUTO_TEST_CASE(verbosity)
BOOST_AUTO_TEST_CASE(influxdbUdp)
{
std::string ilUrl = "stdout://";
auto il = Monitoring::GetBackend(ilUrl);
BOOST_CHECK_EQUAL(static_cast<std::underlying_type<Verbosity>::type>(il->getVerbosity()), 2);
MonitoringFactory::Get("influxdb-udp://localhost:1000");
}

BOOST_AUTO_TEST_CASE(InfluxDbv2)
{
MonitoringFactory::Get("influxdbv2://localhost:9999?org=cern&bucket=test&token=TOKEN");
}

BOOST_AUTO_TEST_CASE(StdOut)
{
MonitoringFactory::Get("stdout://");
}

std::string influxUrl = "influxdb-udp://127.0.0.1:1234";
auto influx = Monitoring::GetBackend(influxUrl);
BOOST_CHECK_EQUAL(static_cast<std::underlying_type<Verbosity>::type>(influx->getVerbosity()), 1);
BOOST_AUTO_TEST_CASE(Noop)
{
MonitoringFactory::Get("no-op://");
}

std::string influxDebugUrl = "influxdb-udp://127.0.0.1:1234/debug";
auto influxDebug = Monitoring::GetBackend(influxDebugUrl);
BOOST_CHECK_EQUAL(static_cast<std::underlying_type<Verbosity>::type>(influxDebug->getVerbosity()), 2);
BOOST_AUTO_TEST_CASE(InfluxDbUnix)
{
MonitoringFactory::Get("influxdb-unix:///tmp/unix.sock");
}

std::string ilProdUrl = "stdout:///info";
auto ilProd = Monitoring::GetBackend(ilProdUrl);
BOOST_CHECK_EQUAL(static_cast<std::underlying_type<Verbosity>::type>(ilProd->getVerbosity()), 1);
#ifdef O2_MONITORING_WITH_APPMON
BOOST_AUTO_TEST_CASE(ApMon)
{
boost::filesystem::path configPath = boost::filesystem::canonical(".");
auto Monitoring = MonitoringFactory::Get("apmon://" + configPath.string() + "/ApMon.conf");
monitoring->send({10, "myCrazyMetric"});
}
#endif

} // namespace Test
} // namespace monitoring
Expand Down
33 changes: 0 additions & 33 deletions test/testNoop.cxx

This file was deleted.

Loading

0 comments on commit 779be58

Please sign in to comment.