Skip to content

Commit 779be58

Browse files
authored
[OMON-445] Make test more scope oriented (#243)
1 parent b3b7246 commit 779be58

File tree

10 files changed

+179
-202
lines changed

10 files changed

+179
-202
lines changed

CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ endif()
5555
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
5656

5757
find_package(Boost REQUIRED COMPONENTS unit_test_framework program_options system filesystem)
58-
find_package(Git QUIET)
5958
find_package(ApMon MODULE)
6059
find_package(CURL MODULE)
6160
find_package(RdKafka CONFIG)
@@ -216,15 +215,14 @@ set_target_properties(8-DbFiller PROPERTIES OUTPUT_NAME "o2-monitoring-dbfiller"
216215
enable_testing()
217216

218217
set(TEST_SRCS
219-
test/testMonitoring.cxx
220218
test/testMonitoringFactory.cxx
221219
test/testDerived.cxx
222220
test/testMetric.cxx
223221
test/testProcessDetails.cxx
224222
test/testProcessMonitor.cxx
225-
test/testInfluxDb.cxx
226-
test/testNoop.cxx
227223
test/testRegexMatch.cxx
224+
test/testMonitoring.cxx
225+
test/testVerbosity.cxx
228226
)
229227

230228
if(ApMon_FOUND)

test/testApMon.cxx

Lines changed: 0 additions & 40 deletions
This file was deleted.

test/testDerived.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <vector>
1717
#include <iostream>
1818

19-
#define BOOST_TEST_MODULE Test Monitoring DerivedMetrics
19+
#define BOOST_TEST_MODULE Monitoring DerivedMetrics
2020
#define BOOST_TEST_DYN_LINK
2121
#include <boost/test/unit_test.hpp>
2222

test/testInfluxDb.cxx

Lines changed: 0 additions & 75 deletions
This file was deleted.

test/testMonitoring.cxx

Lines changed: 83 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
// granted to it by virtue of its status as an Intergovernmental Organization
99
// or submit itself to any jurisdiction.
1010

11-
#include <chrono>
12-
#include <vector>
13-
14-
#define BOOST_TEST_MODULE Test Monitoring Colletor
11+
#define BOOST_TEST_MODULE Monitoring Interface
1512
#define BOOST_TEST_DYN_LINK
1613
#include <boost/test/unit_test.hpp>
17-
#include <boost/lexical_cast.hpp>
14+
#include "Monitoring/MonitoringFactory.h"
1815

19-
#include "../include/Monitoring/MonitoringFactory.h"
16+
using namespace std::string_literals;
2017

2118
namespace o2
2219
{
@@ -25,39 +22,97 @@ namespace monitoring
2522
namespace Test
2623
{
2724

28-
using Monitoring = o2::monitoring::MonitoringFactory;
25+
auto monitoring = MonitoringFactory::Get("influxdb-stdout://");
26+
std::stringstream coutRedirect;
27+
std::streambuf* coutBuffer;
28+
29+
void enableRedirect()
30+
{
31+
coutBuffer = std::cout.rdbuf(coutRedirect.rdbuf());
32+
}
2933

30-
BOOST_AUTO_TEST_CASE(createMonitoring)
34+
void disableRedirect()
3135
{
32-
auto monitoring = Monitoring::Get("stdout://");
36+
coutRedirect.str(std::string());
37+
std::cout.rdbuf(coutBuffer);
38+
}
3339

34-
int intMetric = 10;
35-
std::string stringMetric("monitoringString");
36-
double doubleMetric = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
40+
void removeRandomValues(std::string& metric)
41+
{
42+
metric.erase(metric.rfind(' ')); // remove timestamp
43+
auto begHost = metric.find(",hostname");
44+
auto endHost = metric.find(",", begHost+1);
45+
metric.erase(begHost, endHost-begHost);
46+
}
3747

38-
monitoring->addGlobalTag("name", "Readout");
39-
monitoring->addGlobalTag(tags::Key::Name, tags::Value::Readout);
48+
BOOST_AUTO_TEST_CASE(parseDataPoints)
49+
{
50+
enableRedirect();
51+
52+
monitoring->send(Metric{"card"}
53+
.addValue(40.217773, "temperature")
54+
.addValue(0, "droppedPackets")
55+
.addValue(0.768170, "ctpClock")
56+
.addValue(240.471130, "localClock")
57+
.addValue(0, "totalPacketsPerSecond")
58+
.addTag(tags::Key::ID, 3)
59+
.addTag(tags::Key::Type, tags::Value::CRU)
60+
);
61+
std::string expected1 = "card,id=3,type=CRU temperature=40.2178,droppedPackets=0i,ctpClock=0.76817,localClock=240.471,totalPacketsPerSecond=0i";
62+
std::string returned1 = coutRedirect.str();
4063

41-
monitoring->send({intMetric, "myCrazyMetricI"});
42-
monitoring->send({stringMetric, "myCrazyMetricS"});
43-
monitoring->send({doubleMetric, "myCrazyMetricD"});
64+
disableRedirect();
65+
removeRandomValues(returned1);
66+
BOOST_CHECK(expected1 == returned1);
67+
68+
enableRedirect();
69+
monitoring->send(Metric{"link"}
70+
.addValue("GBT/GBT"s, "gbtMode")
71+
.addValue("None"s, "loopback")
72+
.addValue("TTC:CTP"s, "gbtMux")
73+
.addValue("Continuous"s, "datapathMode")
74+
.addValue("Disabled"s, "datapath")
75+
.addValue(181.370575, "rxFreq")
76+
.addValue(196.250259, "txFreq")
77+
.addValue(0, "status")
78+
.addValue(657.400024, "opticalPower")
79+
.addTag(tags::Key::CRU, 3)
80+
.addTag(tags::Key::ID, 3)
81+
);
82+
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)";
83+
std::string returned2 = coutRedirect.str();
84+
disableRedirect();
85+
removeRandomValues(returned2);
86+
BOOST_CHECK(expected2 == returned2);
4487
}
4588

46-
BOOST_AUTO_TEST_CASE(buffering)
89+
BOOST_AUTO_TEST_CASE(testSettingRun)
4790
{
48-
auto monitoring = Monitoring::Get("stdout://,influxdb-udp://localhost:1234");
49-
monitoring->enableBuffering(10);
50-
for (int i = 0; i < 25; i++) {
51-
monitoring->send({10, "myMetricInt"});
52-
}
53-
monitoring->flushBuffer();
91+
enableRedirect();
92+
93+
monitoring->setRunNumber(1234);
94+
monitoring->send(Metric{4321, "test"});
95+
std::string expected = "test,run=1234 value=4321i";
96+
std::string returned = coutRedirect.str();
97+
98+
disableRedirect();
99+
removeRandomValues(returned);
100+
BOOST_CHECK(expected == returned);
54101
}
55102

56-
BOOST_AUTO_TEST_CASE(testSymbols)
103+
BOOST_AUTO_TEST_CASE(testGlobalTags)
57104
{
58-
BOOST_WARN_MESSAGE(!BOOST_IS_DEFINED(O2_MONITORING_WITH_APPMON), "ApMon Backend disabled");
59-
BOOST_WARN_MESSAGE(BOOST_IS_DEFINED(O2_MONITORING_OS_LINUX), "Linux OS detected");
60-
BOOST_WARN_MESSAGE(BOOST_IS_DEFINED(O2_MONITORING_OS_MAC), "Mac OS detected");
105+
enableRedirect();
106+
107+
monitoring->addGlobalTag("custom_name", "Monitoring");
108+
monitoring->addGlobalTag(tags::Key::Name, tags::Value::Readout);
109+
monitoring->send(Metric{4321, "test"});
110+
std::string expected = "test,custom_name=Monitoring,name=Readout,run=1234 value=4321i";
111+
std::string returned = coutRedirect.str();
112+
113+
disableRedirect();
114+
removeRandomValues(returned);
115+
BOOST_CHECK(expected == returned);
61116
}
62117

63118
} // namespace Test

test/testMonitoringFactory.cxx

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
#include <chrono>
1212
#include <vector>
1313

14-
#define BOOST_TEST_MODULE TestMonitoringVerbosityy
14+
#define BOOST_TEST_MODULE Monitoring Factory
1515
#define BOOST_TEST_DYN_LINK
1616
#include <boost/test/unit_test.hpp>
1717
#include <boost/lexical_cast.hpp>
18+
#include <boost/filesystem/path.hpp>
19+
#include <boost/filesystem/operations.hpp>
1820

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

@@ -27,24 +29,39 @@ namespace Test
2729

2830
using Monitoring = o2::monitoring::MonitoringFactory;
2931

30-
BOOST_AUTO_TEST_CASE(verbosity)
32+
BOOST_AUTO_TEST_CASE(influxdbUdp)
3133
{
32-
std::string ilUrl = "stdout://";
33-
auto il = Monitoring::GetBackend(ilUrl);
34-
BOOST_CHECK_EQUAL(static_cast<std::underlying_type<Verbosity>::type>(il->getVerbosity()), 2);
34+
MonitoringFactory::Get("influxdb-udp://localhost:1000");
35+
}
36+
37+
BOOST_AUTO_TEST_CASE(InfluxDbv2)
38+
{
39+
MonitoringFactory::Get("influxdbv2://localhost:9999?org=cern&bucket=test&token=TOKEN");
40+
}
41+
42+
BOOST_AUTO_TEST_CASE(StdOut)
43+
{
44+
MonitoringFactory::Get("stdout://");
45+
}
3546

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

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

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

4966
} // namespace Test
5067
} // namespace monitoring

test/testNoop.cxx

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)