1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "icinga/perfdatavalue.hpp"
21 #include "icinga/pluginutility.hpp"
22 #include <boost/test/unit_test.hpp>
24 using namespace icinga;
26 BOOST_AUTO_TEST_SUITE(icinga_perfdata)
28 BOOST_AUTO_TEST_CASE(empty)
30 Array::Ptr pd = PluginUtility::SplitPerfdata("");
31 BOOST_CHECK(pd->GetLength() == 0);
34 BOOST_AUTO_TEST_CASE(simple)
36 PerfdataValue::Ptr pdv = PerfdataValue::Parse("test=123456");
37 BOOST_CHECK(pdv->GetLabel() == "test");
38 BOOST_CHECK(pdv->GetValue() == 123456);
40 String str = pdv->Format();
41 BOOST_CHECK(str == "test=123456");
44 BOOST_AUTO_TEST_CASE(quotes)
46 Array::Ptr pd = PluginUtility::SplitPerfdata("'hello world'=123456");
47 BOOST_CHECK(pd->GetLength() == 1);
49 PerfdataValue::Ptr pdv = PerfdataValue::Parse("'hello world'=123456");
50 BOOST_CHECK(pdv->GetLabel() == "hello world");
51 BOOST_CHECK(pdv->GetValue() == 123456);
54 BOOST_AUTO_TEST_CASE(multiple)
56 Array::Ptr pd = PluginUtility::SplitPerfdata("testA=123456 testB=123456");
57 BOOST_CHECK(pd->GetLength() == 2);
59 String str = PluginUtility::FormatPerfdata(pd);
60 BOOST_CHECK(str == "testA=123456 testB=123456");
63 BOOST_AUTO_TEST_CASE(uom)
65 PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456B");
68 BOOST_CHECK(pv->GetValue() == 123456);
69 BOOST_CHECK(!pv->GetCounter());
70 BOOST_CHECK(pv->GetUnit() == "bytes");
71 BOOST_CHECK(pv->GetCrit() == Empty);
72 BOOST_CHECK(pv->GetWarn() == Empty);
73 BOOST_CHECK(pv->GetMin() == Empty);
74 BOOST_CHECK(pv->GetMax() == Empty);
76 String str = pv->Format();
77 BOOST_CHECK(str == "test=123456B");
79 pv = PerfdataValue::Parse("test=1000ms;200;500");
82 BOOST_CHECK(pv->GetValue() == 1);
83 BOOST_CHECK(pv->GetUnit() == "seconds");
84 BOOST_CHECK(pv->GetWarn() == 0.2);
85 BOOST_CHECK(pv->GetCrit() == 0.5);
87 pv = PerfdataValue::Parse("test=1000ms");
90 BOOST_CHECK(pv->GetValue() == 1);
91 BOOST_CHECK(pv->GetUnit() == "seconds");
92 BOOST_CHECK(pv->GetCrit() == Empty);
93 BOOST_CHECK(pv->GetWarn() == Empty);
94 BOOST_CHECK(pv->GetMin() == Empty);
95 BOOST_CHECK(pv->GetMax() == Empty);
98 BOOST_CHECK(str == "test=1s");
101 BOOST_AUTO_TEST_CASE(warncritminmax)
103 PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456B;1000;2000;3000;4000");
106 BOOST_CHECK(pv->GetValue() == 123456);
107 BOOST_CHECK(!pv->GetCounter());
108 BOOST_CHECK(pv->GetUnit() == "bytes");
109 BOOST_CHECK(pv->GetWarn() == 1000);
110 BOOST_CHECK(pv->GetCrit() == 2000);
111 BOOST_CHECK(pv->GetMin() == 3000);
112 BOOST_CHECK(pv->GetMax() == 4000);
114 BOOST_CHECK(pv->Format() == "test=123456B;1000;2000;3000;4000");
117 BOOST_AUTO_TEST_CASE(ignore_invalid_warn_crit_min_max)
119 PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456;1000:2000;0:3000;3000;4000");
121 BOOST_CHECK(pv->GetValue() == 123456);
122 BOOST_CHECK(pv->GetWarn() == Empty);
123 BOOST_CHECK(pv->GetCrit() == Empty);
124 BOOST_CHECK(pv->GetMin() == 3000);
125 BOOST_CHECK(pv->GetMax() == 4000);
127 BOOST_CHECK(pv->Format() == "test=123456");
130 BOOST_AUTO_TEST_CASE(invalid)
132 BOOST_CHECK_THROW(PerfdataValue::Parse("123456"), boost::exception);
133 BOOST_CHECK_THROW(PerfdataValue::Parse("test=1,23456"), boost::exception);
136 BOOST_AUTO_TEST_CASE(multi)
138 Array::Ptr pd = PluginUtility::SplitPerfdata("test::a=3 b=4");
139 BOOST_CHECK(pd->Get(0) == "test::a=3");
140 BOOST_CHECK(pd->Get(1) == "test::b=4");
143 BOOST_AUTO_TEST_SUITE_END()