]> granicus.if.org Git - icinga2/blob - test/livestatus.cpp
Merge pull request #6999 from Icinga/bugfix/compiler-warnings
[icinga2] / test / livestatus.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "livestatus/livestatusquery.hpp"
4 #include "base/application.hpp"
5 #include "base/stdiostream.hpp"
6 #include "base/json.hpp"
7 #include <BoostTestTargetConfig.h>
8
9 using namespace icinga;
10
11 String LivestatusQueryHelper(const std::vector<String>& lines)
12 {
13         LivestatusQuery::Ptr query = new LivestatusQuery(lines, "");
14
15         std::stringstream stream;
16         StdioStream::Ptr sstream = new StdioStream(&stream, false);
17
18         query->Execute(sstream);
19
20         String output;
21         String result;
22
23         StreamReadContext src;
24         for (;;) {
25                 StreamReadStatus srs = sstream->ReadLine(&result, src);
26
27                 if (srs == StatusEof)
28                         break;
29
30                 if (srs != StatusNewItem)
31                         continue;
32
33                 if (result.GetLength() > 0)
34                         output += result + "\n";
35                 else
36                         break;
37         }
38
39         BOOST_TEST_MESSAGE("Query Result: " + output);
40
41         return output;
42 }
43
44 //____________________________________________________________________________//
45
46 BOOST_AUTO_TEST_SUITE(livestatus)
47
48 BOOST_AUTO_TEST_CASE(hosts)
49 {
50         BOOST_TEST_MESSAGE( "Querying Livestatus...");
51
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");
57
58         /* use our query helper */
59         String output = LivestatusQueryHelper(lines);
60
61         Array::Ptr query_result = JsonDecode(output);
62
63         /* the outer elements */
64         BOOST_CHECK(query_result->GetLength() > 1);
65
66         Array::Ptr res1 = query_result->Get(0);
67         Array::Ptr res2 = query_result->Get(1);
68
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"));
74
75         BOOST_TEST_MESSAGE("Done with testing livestatus hosts...");
76 }
77
78 BOOST_AUTO_TEST_CASE(services)
79 {
80         BOOST_TEST_MESSAGE( "Querying Livestatus...");
81
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");
87
88         /* use our query helper */
89         String output = LivestatusQueryHelper(lines);
90
91         Array::Ptr query_result = JsonDecode(output);
92
93         /* the outer elements */
94         BOOST_CHECK(query_result->GetLength() > 1);
95
96         Array::Ptr res1 = query_result->Get(0);
97         Array::Ptr res2 = query_result->Get(1);
98
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
102
103         BOOST_TEST_MESSAGE("Done with testing livestatus services...");
104 }
105 //____________________________________________________________________________//
106
107 BOOST_AUTO_TEST_SUITE_END()