]> granicus.if.org Git - icinga2/blob - test/icinga-perfdata.cpp
Use Utility::GetFQDN for the NodeName variable.
[icinga2] / test / icinga-perfdata.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
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 "icinga/perfdatavalue.hpp"
21 #include "icinga/pluginutility.hpp"
22 #include <boost/test/unit_test.hpp>
23
24 using namespace icinga;
25
26 BOOST_AUTO_TEST_SUITE(icinga_perfdata)
27
28 BOOST_AUTO_TEST_CASE(empty)
29 {
30         Dictionary::Ptr pd = PluginUtility::ParsePerfdata("");
31         BOOST_CHECK(pd->GetLength() == 0);
32 }
33
34 BOOST_AUTO_TEST_CASE(simple)
35 {
36         Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456");
37         BOOST_CHECK(pd->Get("test") == 123456);
38
39         String str = PluginUtility::FormatPerfdata(pd);
40         BOOST_CHECK(str == "test=123456");
41 }
42
43 BOOST_AUTO_TEST_CASE(quotes)
44 {
45         Dictionary::Ptr pd = PluginUtility::ParsePerfdata("'hello world'=123456");
46         BOOST_CHECK(pd->Get("hello world") == 123456);
47 }
48
49 BOOST_AUTO_TEST_CASE(multiple)
50 {
51         Dictionary::Ptr pd = PluginUtility::ParsePerfdata("testA=123456 testB=123456");
52         BOOST_CHECK(pd->Get("testA") == 123456);
53         BOOST_CHECK(pd->Get("testB") == 123456);
54
55         String str = PluginUtility::FormatPerfdata(pd);
56         BOOST_CHECK(str == "testA=123456 testB=123456");
57 }
58
59 BOOST_AUTO_TEST_CASE(uom)
60 {
61         Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456B");
62
63         PerfdataValue::Ptr pv = pd->Get("test");
64         BOOST_CHECK(pv);
65
66         BOOST_CHECK(pv->GetValue() == 123456);
67         BOOST_CHECK(!pv->GetCounter());
68         BOOST_CHECK(pv->GetUnit() == "bytes");
69         BOOST_CHECK(pv->GetCrit() == Empty);
70         BOOST_CHECK(pv->GetWarn() == Empty);
71         BOOST_CHECK(pv->GetMin() == Empty);
72         BOOST_CHECK(pv->GetMax() == Empty);
73
74         String str = PluginUtility::FormatPerfdata(pd);
75         BOOST_CHECK(str == "test=123456B");
76
77         pd = PluginUtility::ParsePerfdata("test=1000ms;200;500");
78         BOOST_CHECK(pd);
79
80         pv = pd->Get("test");
81         BOOST_CHECK(pv);
82
83         BOOST_CHECK(pv->GetValue() == 1);
84         BOOST_CHECK(pv->GetUnit() == "seconds");
85         BOOST_CHECK(pv->GetWarn() == 0.2);
86         BOOST_CHECK(pv->GetCrit() == 0.5);
87
88         pd = PluginUtility::ParsePerfdata("test=1000ms");
89         BOOST_CHECK(pd);
90
91         pv = pd->Get("test");
92         BOOST_CHECK(pv);
93
94         BOOST_CHECK(pv->GetValue() == 1);
95         BOOST_CHECK(pv->GetUnit() == "seconds");
96         BOOST_CHECK(pv->GetCrit() == Empty);
97         BOOST_CHECK(pv->GetWarn() == Empty);
98         BOOST_CHECK(pv->GetMin() == Empty);
99         BOOST_CHECK(pv->GetMax() == Empty);
100
101         str = PluginUtility::FormatPerfdata(pd);
102         BOOST_CHECK(str == "test=1s");
103 }
104
105 BOOST_AUTO_TEST_CASE(warncritminmax)
106 {
107         Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456B;1000;2000;3000;4000");
108
109         PerfdataValue::Ptr pv = pd->Get("test");
110         BOOST_CHECK(pv);
111
112         BOOST_CHECK(pv->GetValue() == 123456);
113         BOOST_CHECK(!pv->GetCounter());
114         BOOST_CHECK(pv->GetUnit() == "bytes");
115         BOOST_CHECK(pv->GetWarn() == 1000);
116         BOOST_CHECK(pv->GetCrit() == 2000);
117         BOOST_CHECK(pv->GetMin() == 3000);
118         BOOST_CHECK(pv->GetMax() == 4000);
119
120         String str = PluginUtility::FormatPerfdata(pd);
121         BOOST_CHECK(str == "test=123456B;1000;2000;3000;4000");
122 }
123
124 BOOST_AUTO_TEST_CASE(invalid)
125 {
126         BOOST_CHECK(PluginUtility::ParsePerfdata("test=1,23456") == "test=1,23456");
127         BOOST_CHECK(PluginUtility::ParsePerfdata("test=123456;10%;20%") == "test=123456;10%;20%");
128 }
129
130 BOOST_AUTO_TEST_CASE(multi)
131 {
132         Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test::a=3 b=4");
133         BOOST_CHECK(pd->Get("test::a") == 3);
134         BOOST_CHECK(pd->Get("test::b") == 4);
135 }
136
137 BOOST_AUTO_TEST_SUITE_END()