]> granicus.if.org Git - icinga2/blob - test/livestatus.cpp
icinga.com: Update *.{h,c}pp
[icinga2] / test / livestatus.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
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 "livestatus/livestatusquery.hpp"
21 #include "base/application.hpp"
22 #include "base/stdiostream.hpp"
23 #include "base/json.hpp"
24 #include <BoostTestTargetConfig.h>
25
26 using namespace icinga;
27
28 String LivestatusQueryHelper(const std::vector<String>& lines)
29 {
30         LivestatusQuery::Ptr query = new LivestatusQuery(lines, "");
31
32         std::stringstream stream;
33         StdioStream::Ptr sstream = new StdioStream(&stream, false);
34
35         query->Execute(sstream);
36
37         String output;
38         String result;
39
40         StreamReadContext src;
41         for (;;) {
42                 StreamReadStatus srs = sstream->ReadLine(&result, src);
43
44                 if (srs == StatusEof)
45                         break;
46
47                 if (srs != StatusNewItem)
48                         continue;
49
50                 if (result.GetLength() > 0)
51                         output += result + "\n";
52                 else
53                         break;
54         }
55
56         BOOST_TEST_MESSAGE("Query Result: " + output);
57
58         return output;
59 }
60
61 //____________________________________________________________________________//
62
63 BOOST_AUTO_TEST_SUITE(livestatus)
64
65 BOOST_AUTO_TEST_CASE(hosts)
66 {
67         BOOST_TEST_MESSAGE( "Querying Livestatus...");
68
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");
74
75         /* use our query helper */
76         String output = LivestatusQueryHelper(lines);
77
78         Array::Ptr query_result = JsonDecode(output);
79
80         /* the outer elements */
81         BOOST_CHECK(query_result->GetLength() > 1);
82
83         Array::Ptr res1 = query_result->Get(0);
84         Array::Ptr res2 = query_result->Get(1);
85
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"));
91
92         BOOST_TEST_MESSAGE("Done with testing livestatus hosts...");
93 }
94
95 BOOST_AUTO_TEST_CASE(services)
96 {
97         BOOST_TEST_MESSAGE( "Querying Livestatus...");
98
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");
104
105         /* use our query helper */
106         String output = LivestatusQueryHelper(lines);
107
108         Array::Ptr query_result = JsonDecode(output);
109
110         /* the outer elements */
111         BOOST_CHECK(query_result->GetLength() > 1);
112
113         Array::Ptr res1 = query_result->Get(0);
114         Array::Ptr res2 = query_result->Get(1);
115
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
119
120         BOOST_TEST_MESSAGE("Done with testing livestatus services...");
121 }
122 //____________________________________________________________________________//
123
124 BOOST_AUTO_TEST_SUITE_END()