]> granicus.if.org Git - icinga2/blobdiff - test/icinga-perfdata.cpp
Merge pull request #6999 from Icinga/bugfix/compiler-warnings
[icinga2] / test / icinga-perfdata.cpp
index bac094e0a8134f4f6e2865599fa3c122d84bfdba..b0fa1ee9878d04bc1589221e7b5670fdc24c4114 100644 (file)
@@ -1,43 +1,43 @@
-/******************************************************************************
- * Icinga 2                                                                   *
- * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)   *
- *                                                                            *
- * This program is free software; you can redistribute it and/or              *
- * modify it under the terms of the GNU General Public License                *
- * as published by the Free Software Foundation; either version 2             *
- * of the License, or (at your option) any later version.                     *
- *                                                                            *
- * This program is distributed in the hope that it will be useful,            *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
- * GNU General Public License for more details.                               *
- *                                                                            *
- * You should have received a copy of the GNU General Public License          *
- * along with this program; if not, write to the Free Software Foundation     *
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
- ******************************************************************************/
-
-#include "icinga/pluginutility.cpp"
-#include <boost/test/unit_test.hpp>
+/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
+
+#include "base/perfdatavalue.hpp"
+#include "icinga/pluginutility.hpp"
+#include <BoostTestTargetConfig.h>
 
 using namespace icinga;
 
 BOOST_AUTO_TEST_SUITE(icinga_perfdata)
 
+BOOST_AUTO_TEST_CASE(empty)
+{
+       Array::Ptr pd = PluginUtility::SplitPerfdata("");
+       BOOST_CHECK(pd->GetLength() == 0);
+}
+
 BOOST_AUTO_TEST_CASE(simple)
 {
-       Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456");
-       BOOST_CHECK(pd->Get("test") == 123456);
+       PerfdataValue::Ptr pdv = PerfdataValue::Parse("test=123456");
+       BOOST_CHECK(pdv->GetLabel() == "test");
+       BOOST_CHECK(pdv->GetValue() == 123456);
 
-       String str = PluginUtility::FormatPerfdata(pd);
+       String str = pdv->Format();
        BOOST_CHECK(str == "test=123456");
 }
 
+BOOST_AUTO_TEST_CASE(quotes)
+{
+       Array::Ptr pd = PluginUtility::SplitPerfdata("'hello world'=123456");
+       BOOST_CHECK(pd->GetLength() == 1);
+       
+       PerfdataValue::Ptr pdv = PerfdataValue::Parse("'hello world'=123456");
+       BOOST_CHECK(pdv->GetLabel() == "hello world");
+       BOOST_CHECK(pdv->GetValue() == 123456);
+}
+
 BOOST_AUTO_TEST_CASE(multiple)
 {
-       Dictionary::Ptr pd = PluginUtility::ParsePerfdata("testA=123456 testB=123456");
-       BOOST_CHECK(pd->Get("testA") == 123456);
-       BOOST_CHECK(pd->Get("testB") == 123456);
+       Array::Ptr pd = PluginUtility::SplitPerfdata("testA=123456 testB=123456");
+       BOOST_CHECK(pd->GetLength() == 2);
 
        String str = PluginUtility::FormatPerfdata(pd);
        BOOST_CHECK(str == "testA=123456 testB=123456");
@@ -45,9 +45,7 @@ BOOST_AUTO_TEST_CASE(multiple)
 
 BOOST_AUTO_TEST_CASE(uom)
 {
-       Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456b");
-
-       PerfdataValue::Ptr pv = pd->Get("test");
+       PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456B");
        BOOST_CHECK(pv);
 
        BOOST_CHECK(pv->GetValue() == 123456);
@@ -58,15 +56,34 @@ BOOST_AUTO_TEST_CASE(uom)
        BOOST_CHECK(pv->GetMin() == Empty);
        BOOST_CHECK(pv->GetMax() == Empty);
 
-       String str = PluginUtility::FormatPerfdata(pd);
-       BOOST_CHECK(str == "test=123456b");
+       String str = pv->Format();
+       BOOST_CHECK(str == "test=123456B");
+
+       pv = PerfdataValue::Parse("test=1000ms;200;500");
+       BOOST_CHECK(pv);
+
+       BOOST_CHECK(pv->GetValue() == 1);
+       BOOST_CHECK(pv->GetUnit() == "seconds");
+       BOOST_CHECK(pv->GetWarn() == 0.2);
+       BOOST_CHECK(pv->GetCrit() == 0.5);
+
+       pv = PerfdataValue::Parse("test=1000ms");
+       BOOST_CHECK(pv);
+
+       BOOST_CHECK(pv->GetValue() == 1);
+       BOOST_CHECK(pv->GetUnit() == "seconds");
+       BOOST_CHECK(pv->GetCrit() == Empty);
+       BOOST_CHECK(pv->GetWarn() == Empty);
+       BOOST_CHECK(pv->GetMin() == Empty);
+       BOOST_CHECK(pv->GetMax() == Empty);
+
+       str = pv->Format();
+       BOOST_CHECK(str == "test=1s");
 }
 
 BOOST_AUTO_TEST_CASE(warncritminmax)
 {
-       Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456b;1000;2000;3000;4000");
-
-       PerfdataValue::Ptr pv = pd->Get("test");
+       PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456B;1000;2000;3000;4000");
        BOOST_CHECK(pv);
 
        BOOST_CHECK(pv->GetValue() == 123456);
@@ -77,14 +94,33 @@ BOOST_AUTO_TEST_CASE(warncritminmax)
        BOOST_CHECK(pv->GetMin() == 3000);
        BOOST_CHECK(pv->GetMax() == 4000);
 
-       String str = PluginUtility::FormatPerfdata(pd);
-       BOOST_CHECK(str == "test=123456b;1000;2000;3000;4000");
+       BOOST_CHECK(pv->Format() == "test=123456B;1000;2000;3000;4000");
+}
+
+BOOST_AUTO_TEST_CASE(ignore_invalid_warn_crit_min_max)
+{
+       PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456;1000:2000;0:3000;3000;4000");
+       BOOST_CHECK(pv);
+       BOOST_CHECK(pv->GetValue() == 123456);
+       BOOST_CHECK(pv->GetWarn() == Empty);
+       BOOST_CHECK(pv->GetCrit() == Empty);
+       BOOST_CHECK(pv->GetMin() == 3000);
+       BOOST_CHECK(pv->GetMax() == 4000);
+
+       BOOST_CHECK(pv->Format() == "test=123456");
 }
 
 BOOST_AUTO_TEST_CASE(invalid)
 {
-       BOOST_CHECK(PluginUtility::ParsePerfdata("test=1,23456") == "test=1,23456");
-       BOOST_CHECK(PluginUtility::ParsePerfdata("test=123456;10%;20%") == "test=123456;10%;20%");
+       BOOST_CHECK_THROW(PerfdataValue::Parse("123456"), boost::exception);
+       BOOST_CHECK_THROW(PerfdataValue::Parse("test=1,23456"), boost::exception);
+}
+
+BOOST_AUTO_TEST_CASE(multi)
+{
+       Array::Ptr pd = PluginUtility::SplitPerfdata("test::a=3 b=4");
+       BOOST_CHECK(pd->Get(0) == "test::a=3");
+       BOOST_CHECK(pd->Get(1) == "test::b=4");
 }
 
 BOOST_AUTO_TEST_SUITE_END()