1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "livestatus/livestatusquery.hpp"
4 #include "base/application.hpp"
5 #include "base/stdiostream.hpp"
6 #include "base/json.hpp"
7 #include <BoostTestTargetConfig.h>
9 using namespace icinga;
11 String LivestatusQueryHelper(const std::vector<String>& lines)
13 LivestatusQuery::Ptr query = new LivestatusQuery(lines, "");
15 std::stringstream stream;
16 StdioStream::Ptr sstream = new StdioStream(&stream, false);
18 query->Execute(sstream);
23 StreamReadContext src;
25 StreamReadStatus srs = sstream->ReadLine(&result, src);
30 if (srs != StatusNewItem)
33 if (result.GetLength() > 0)
34 output += result + "\n";
39 BOOST_TEST_MESSAGE("Query Result: " + output);
44 //____________________________________________________________________________//
46 BOOST_AUTO_TEST_SUITE(livestatus)
48 BOOST_AUTO_TEST_CASE(hosts)
50 BOOST_TEST_MESSAGE( "Querying Livestatus...");
52 std::vector<String> lines;
53 lines.emplace_back("GET hosts");
54 lines.emplace_back("Columns: host_name address check_command");
55 lines.emplace_back("OutputFormat: json");
56 lines.emplace_back("\n");
58 /* use our query helper */
59 String output = LivestatusQueryHelper(lines);
61 Array::Ptr query_result = JsonDecode(output);
63 /* the outer elements */
64 BOOST_CHECK(query_result->GetLength() > 1);
66 Array::Ptr res1 = query_result->Get(0);
67 Array::Ptr res2 = query_result->Get(1);
69 /* results are non-deterministic and not sorted by livestatus */
70 BOOST_CHECK(res1->Contains("test-01") || res2->Contains("test-01"));
71 BOOST_CHECK(res1->Contains("test-02") || res2->Contains("test-02"));
72 BOOST_CHECK(res1->Contains("127.0.0.1") || res2->Contains("127.0.0.1"));
73 BOOST_CHECK(res1->Contains("127.0.0.2") || res2->Contains("127.0.0.2"));
75 BOOST_TEST_MESSAGE("Done with testing livestatus hosts...");
78 BOOST_AUTO_TEST_CASE(services)
80 BOOST_TEST_MESSAGE( "Querying Livestatus...");
82 std::vector<String> lines;
83 lines.emplace_back("GET services");
84 lines.emplace_back("Columns: host_name service_description check_command notes");
85 lines.emplace_back("OutputFormat: json");
86 lines.emplace_back("\n");
88 /* use our query helper */
89 String output = LivestatusQueryHelper(lines);
91 Array::Ptr query_result = JsonDecode(output);
93 /* the outer elements */
94 BOOST_CHECK(query_result->GetLength() > 1);
96 Array::Ptr res1 = query_result->Get(0);
97 Array::Ptr res2 = query_result->Get(1);
99 /* results are non-deterministic and not sorted by livestatus */
100 BOOST_CHECK(res1->Contains("livestatus") || res2->Contains("livestatus")); //service_description
101 BOOST_CHECK(res1->Contains("test livestatus") || res2->Contains("test livestatus")); //notes
103 BOOST_TEST_MESSAGE("Done with testing livestatus services...");
105 //____________________________________________________________________________//
107 BOOST_AUTO_TEST_SUITE_END()