]> granicus.if.org Git - icinga2/blob - test/icinga-perfdata.cpp
Merge pull request #6602 from Icinga/fix/improve-tls-handshake-exception-logging
[icinga2] / test / icinga-perfdata.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "base/perfdatavalue.hpp"
21 #include "icinga/pluginutility.hpp"
22 #include <BoostTestTargetConfig.h>
23
24 using namespace icinga;
25
26 BOOST_AUTO_TEST_SUITE(icinga_perfdata)
27
28 BOOST_AUTO_TEST_CASE(empty)
29 {
30         Array::Ptr pd = PluginUtility::SplitPerfdata("");
31         BOOST_CHECK(pd->GetLength() == 0);
32 }
33
34 BOOST_AUTO_TEST_CASE(simple)
35 {
36         PerfdataValue::Ptr pdv = PerfdataValue::Parse("test=123456");
37         BOOST_CHECK(pdv->GetLabel() == "test");
38         BOOST_CHECK(pdv->GetValue() == 123456);
39
40         String str = pdv->Format();
41         BOOST_CHECK(str == "test=123456");
42 }
43
44 BOOST_AUTO_TEST_CASE(quotes)
45 {
46         Array::Ptr pd = PluginUtility::SplitPerfdata("'hello world'=123456");
47         BOOST_CHECK(pd->GetLength() == 1);
48         
49         PerfdataValue::Ptr pdv = PerfdataValue::Parse("'hello world'=123456");
50         BOOST_CHECK(pdv->GetLabel() == "hello world");
51         BOOST_CHECK(pdv->GetValue() == 123456);
52 }
53
54 BOOST_AUTO_TEST_CASE(multiple)
55 {
56         Array::Ptr pd = PluginUtility::SplitPerfdata("testA=123456 testB=123456");
57         BOOST_CHECK(pd->GetLength() == 2);
58
59         String str = PluginUtility::FormatPerfdata(pd);
60         BOOST_CHECK(str == "testA=123456 testB=123456");
61 }
62
63 BOOST_AUTO_TEST_CASE(uom)
64 {
65         PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456B");
66         BOOST_CHECK(pv);
67
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);
75
76         String str = pv->Format();
77         BOOST_CHECK(str == "test=123456B");
78
79         pv = PerfdataValue::Parse("test=1000ms;200;500");
80         BOOST_CHECK(pv);
81
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);
86
87         pv = PerfdataValue::Parse("test=1000ms");
88         BOOST_CHECK(pv);
89
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);
96
97         str = pv->Format();
98         BOOST_CHECK(str == "test=1s");
99 }
100
101 BOOST_AUTO_TEST_CASE(warncritminmax)
102 {
103         PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456B;1000;2000;3000;4000");
104         BOOST_CHECK(pv);
105
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);
113
114         BOOST_CHECK(pv->Format() == "test=123456B;1000;2000;3000;4000");
115 }
116
117 BOOST_AUTO_TEST_CASE(ignore_invalid_warn_crit_min_max)
118 {
119         PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456;1000:2000;0:3000;3000;4000");
120         BOOST_CHECK(pv);
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);
126
127         BOOST_CHECK(pv->Format() == "test=123456");
128 }
129
130 BOOST_AUTO_TEST_CASE(invalid)
131 {
132         BOOST_CHECK_THROW(PerfdataValue::Parse("123456"), boost::exception);
133         BOOST_CHECK_THROW(PerfdataValue::Parse("test=1,23456"), boost::exception);
134 }
135
136 BOOST_AUTO_TEST_CASE(multi)
137 {
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");
141 }
142
143 BOOST_AUTO_TEST_SUITE_END()