added variable for threshold for switching from "all congested" --> "connection failed"
[pingcheck] / test / test_hoststatus.cpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
19 */
20
21 #define BOOST_TEST_MAIN
22 #define BOOST_TEST_DYN_LINK
23
24 #include <streambuf>
25
26 #include <boost/asio.hpp>
27 #include <boost/test/unit_test.hpp>
28
29 #include "mock_linkstatus.h"
30
31 #include "host/hoststatus.h"
32 #include "host/pingstatus.h"
33
34 BOOST_AUTO_TEST_SUITE( TestHostStatus )
35
36 BOOST_AUTO_TEST_CASE( fail_percentage_10 )
37 {
38     int ping_fail_percentage_limit = 10;
39     int ping_congestion_percentage_limit = 75;
40     int congest_caused_by_fail_percentage_limit = 90;
41     int ping_congestion_duration_thresh = 5;
42     int n_parallel_pings = 1;
43     int resolved_ip_count = 10;
44
45     LinkStatusItem link_status( new LinkStatus );
46     HostStatus host_status( "localhost",
47                             ping_fail_percentage_limit,
48                             ping_congestion_percentage_limit,
49                             congest_caused_by_fail_percentage_limit,
50                             ping_congestion_duration_thresh,
51                             n_parallel_pings,
52                             link_status );
53     host_status.set_resolved_ip_count( resolved_ip_count );
54
55     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
56     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
57
58     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
59     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
60
61     host_status.update_ping_statistics( PingStatus_FailureOther, 1);
62     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
63
64     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
65     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
66
67     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
68     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
69
70     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
71     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
72
73     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
74     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
75
76     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
77     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
78
79     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
80     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
81
82     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
83     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
84 }
85
86 BOOST_AUTO_TEST_CASE( fail_percentage_50 )
87 {
88     int ping_fail_percentage_limit = 50;
89     int ping_congestion_percentage_limit = 75;
90     int congest_caused_by_fail_percentage_limit = 90;
91     int ping_congestion_duration_thresh = 5;
92     int n_parallel_pings = 1;
93     int resolved_ip_count = 10;
94
95     LinkStatusItem link_status( new LinkStatus );
96     HostStatus host_status( "localhost",
97                             ping_fail_percentage_limit,
98                             ping_congestion_percentage_limit,
99                             congest_caused_by_fail_percentage_limit,
100                             ping_congestion_duration_thresh,
101                             n_parallel_pings,
102                             link_status );
103     host_status.set_resolved_ip_count( resolved_ip_count );
104
105     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
106     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
107
108     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
109     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
110
111     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
112     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
113
114     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
115     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
116
117     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
118     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
119
120     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
121     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
122
123     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
124     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
125
126     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
127     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
128
129     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
130     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
131
132     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
133     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
134 }
135
136 BOOST_AUTO_TEST_CASE( fail_percentage_80 )
137 {
138     int ping_fail_percentage_limit = 80;
139     int ping_congestion_percentage_limit = 75;
140     int congest_caused_by_fail_percentage_limit = 90;
141     int ping_congestion_duration_thresh = 5;
142     int n_parallel_pings = 1;
143     int resolved_ip_count = 10;
144
145     LinkStatusItem link_status( new LinkStatus );
146     HostStatus host_status( "localhost",
147                             ping_fail_percentage_limit,
148                             ping_congestion_percentage_limit,
149                             congest_caused_by_fail_percentage_limit,
150                             ping_congestion_duration_thresh,
151                             n_parallel_pings,
152                             link_status );
153     host_status.set_resolved_ip_count( resolved_ip_count );
154
155     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
156     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
157
158     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
159     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
160
161     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
162     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
163
164     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
165     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
166
167     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
168     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
169
170     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
171     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
172
173     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
174     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
175
176     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
177     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false );
178
179     host_status.update_ping_statistics( PingStatus_FailureOther, 1 );
180     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
181
182     host_status.update_ping_statistics( PingStatus_SuccessReply, 1 );
183     BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true );
184 }
185
186 BOOST_AUTO_TEST_SUITE_END()