1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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 "livestatus/livestatusquery.hpp"
21 #include "base/application.hpp"
22 #include "base/stdiostream.hpp"
23 #include "base/json.hpp"
24 #include <BoostTestTargetConfig.h>
26 using namespace icinga;
28 String LivestatusQueryHelper(const std::vector<String>& lines)
30 LivestatusQuery::Ptr query = new LivestatusQuery(lines, "");
32 std::stringstream stream;
33 StdioStream::Ptr sstream = new StdioStream(&stream, false);
35 query->Execute(sstream);
40 StreamReadContext src;
42 StreamReadStatus srs = sstream->ReadLine(&result, src);
47 if (srs != StatusNewItem)
50 if (result.GetLength() > 0)
51 output += result + "\n";
56 BOOST_TEST_MESSAGE("Query Result: " + output);
61 //____________________________________________________________________________//
63 BOOST_AUTO_TEST_SUITE(livestatus)
65 BOOST_AUTO_TEST_CASE(hosts)
67 BOOST_TEST_MESSAGE( "Querying Livestatus...");
69 std::vector<String> lines;
70 lines.emplace_back("GET hosts");
71 lines.emplace_back("Columns: host_name address check_command");
72 lines.emplace_back("OutputFormat: json");
73 lines.emplace_back("\n");
75 /* use our query helper */
76 String output = LivestatusQueryHelper(lines);
78 Array::Ptr query_result = JsonDecode(output);
80 /* the outer elements */
81 BOOST_CHECK(query_result->GetLength() > 1);
83 Array::Ptr res1 = query_result->Get(0);
84 Array::Ptr res2 = query_result->Get(1);
86 /* results are non-deterministic and not sorted by livestatus */
87 BOOST_CHECK(res1->Contains("test-01") || res2->Contains("test-01"));
88 BOOST_CHECK(res1->Contains("test-02") || res2->Contains("test-02"));
89 BOOST_CHECK(res1->Contains("127.0.0.1") || res2->Contains("127.0.0.1"));
90 BOOST_CHECK(res1->Contains("127.0.0.2") || res2->Contains("127.0.0.2"));
92 BOOST_TEST_MESSAGE("Done with testing livestatus hosts...");
95 BOOST_AUTO_TEST_CASE(services)
97 BOOST_TEST_MESSAGE( "Querying Livestatus...");
99 std::vector<String> lines;
100 lines.emplace_back("GET services");
101 lines.emplace_back("Columns: host_name service_description check_command notes");
102 lines.emplace_back("OutputFormat: json");
103 lines.emplace_back("\n");
105 /* use our query helper */
106 String output = LivestatusQueryHelper(lines);
108 Array::Ptr query_result = JsonDecode(output);
110 /* the outer elements */
111 BOOST_CHECK(query_result->GetLength() > 1);
113 Array::Ptr res1 = query_result->Get(0);
114 Array::Ptr res2 = query_result->Get(1);
116 /* results are non-deterministic and not sorted by livestatus */
117 BOOST_CHECK(res1->Contains("livestatus") || res2->Contains("livestatus")); //service_description
118 BOOST_CHECK(res1->Contains("test livestatus") || res2->Contains("test livestatus")); //notes
120 BOOST_TEST_MESSAGE("Done with testing livestatus services...");
122 //____________________________________________________________________________//
124 BOOST_AUTO_TEST_SUITE_END()