Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_threshold.cpp
Go to the documentation of this file.
1#define BOOST_TEST_MODULE threshold
2#include <boost/test/included/unit_test.hpp>
3
5
7
8using namespace sysio;
9using namespace sysio::resource_monitor;
10using namespace boost::system;
11
17
18 int get_stat(const char *path, struct stat *buf) const {
20 }
21
22 bfs::space_info get_space(const bfs::path& p, boost::system::error_code& ec) const {
23 return fixture.mock_get_space(p, ec);
24 }
25
27 };
28
29 boost::asio::io_context ctx;
30
36
37 void add_file_system(const bfs::path& path_name) {
39 }
40
41 void set_threshold(uint32_t threshold, uint32_t warning_threshold) {
42 space_handler.set_threshold(threshold, warning_threshold);
43 }
44
48
49 void set_shutdown_on_exceeded(bool shutdown_on_exceeded) {
50 space_handler.set_shutdown_on_exceeded(shutdown_on_exceeded);
51 }
52
53 bool test_threshold_common(std::map<bfs::path, uintmax_t>& available, std::map<bfs::path, int>& dev, uint32_t warning_threshold=75)
54 {
55 mock_get_space = [available]( const bfs::path& p, boost::system::error_code& ec) mutable -> bfs::space_info {
56 ec = boost::system::errc::make_error_code(errc::success);
57
58 bfs::space_info rc;
59 rc.capacity = 1000000;
60 rc.available = available[p];
61
62 return rc;
63 };
64
65 mock_get_stat = [dev]( const char *path, struct stat *buf ) mutable -> int {
66 bfs::path name = path;
67 buf->st_dev = dev[name];
68
69 return 0;
70 };
71
72 set_threshold(80, warning_threshold);
74
75 for (auto i = 0; i < available.size(); i++) {
76 add_file_system("/test" + std::to_string(i));
77 }
78
79 return is_threshold_exceeded();
80 }
81
82 // fixture data and methods
83 std::function<bfs::space_info(const bfs::path& p, boost::system::error_code& ec)> mock_get_space;
84 std::function<int(const char *path, struct stat *buf)> mock_get_stat;
85
87};
88
89BOOST_AUTO_TEST_SUITE(threshol_tests)
91 {
92 std::map<bfs::path, uintmax_t> availables {{"/test0", 200000}};
93 std::map<bfs::path, int> devs {{"/test0", 0}};
94
95 BOOST_TEST( !test_threshold_common(availables, devs) );
96 }
97
99 {
100 std::map<bfs::path, uintmax_t> availables {{"/test0", 199999}};
101 std::map<bfs::path, int> devs {{"/test0", 0}};
102
103 BOOST_TEST( test_threshold_common(availables, devs) );
104 }
105
106 BOOST_FIXTURE_TEST_CASE(above_threshold_1000_byte, threshold_fixture)
107 {
108 std::map<bfs::path, uintmax_t> availables {{"/test0", 199000}};
109 std::map<bfs::path, int> devs {{"/test0", 0}};
110
111 BOOST_TEST( test_threshold_common(availables, devs) );
112 }
113
115 {
116 std::map<bfs::path, uintmax_t> availables {{"/test0", 249999}};
117 std::map<bfs::path, int> devs {{"/test0", 0}};
118
119 BOOST_TEST( !test_threshold_common(availables, devs) );
120 }
121
123 {
124 std::map<bfs::path, uintmax_t> availables {{"/test0", 250001}};
125 std::map<bfs::path, int> devs {{"/test0", 0}};
126
127 BOOST_TEST( !test_threshold_common(availables, devs) );
128 }
129
131 {
132 std::map<bfs::path, uintmax_t> availables {{"/test0", 200001}};
133 std::map<bfs::path, int> devs {{"/test0", 0}};
134
135 BOOST_TEST( !test_threshold_common(availables, devs) );
136 }
137
139 {
140 std::map<bfs::path, uintmax_t> availables {{"/test0", 200500}};
141 std::map<bfs::path, int> devs {{"/test0", 0}};
142
143 BOOST_TEST( !test_threshold_common(availables, devs) );
144 }
145
146 BOOST_FIXTURE_TEST_CASE(first_file_system_over_threshold, threshold_fixture)
147 {
148 std::map<bfs::path, uintmax_t> availables {{"/test0", 199999},
149 {"/test1", 200500}};
150 std::map<bfs::path, int> devs {{"/test0", 0},
151 {"/test1", 1}};
152
153 BOOST_TEST( test_threshold_common(availables, devs) );
154 }
155
156 BOOST_FIXTURE_TEST_CASE(second_file_system_over_threshold, threshold_fixture)
157 {
158 std::map<bfs::path, uintmax_t> availables {{"/test0", 300000},
159 {"/test1", 100000}};
160 std::map<bfs::path, int> devs {{"/test0", 0},
161 {"/test1", 1}};
162
163 BOOST_TEST( test_threshold_common(availables, devs) );
164 }
165
166 BOOST_FIXTURE_TEST_CASE(no_file_system_over_threshold, threshold_fixture)
167 {
168 std::map<bfs::path, uintmax_t> availables {{"/test0", 300000},
169 {"/test1", 200000}};
170 std::map<bfs::path, int> devs {{"/test0", 0},
171 {"/test1", 1}};
172
173 BOOST_TEST( !test_threshold_common(availables, devs) );
174 }
175
176 BOOST_FIXTURE_TEST_CASE(both_file_systems_over_threshold, threshold_fixture)
177 {
178 std::map<bfs::path, uintmax_t> availables {{"/test0", 150000},
179 {"/test1", 100000}};
180 std::map<bfs::path, int> devs {{"/test0", 0},
181 {"/test1", 1}};
182
183 BOOST_TEST( test_threshold_common(availables, devs) );
184 }
185
186 BOOST_FIXTURE_TEST_CASE(one_of_three_over_threshold, threshold_fixture)
187 {
188 std::map<bfs::path, uintmax_t> availables {{"/test0", 300000},
189 {"/test1", 199999},
190 {"/test2", 250000}};
191 std::map<bfs::path, int> devs {{"/test0", 0},
192 {"/test1", 1},
193 {"/test2", 2}};
194
195 BOOST_TEST( test_threshold_common(availables, devs) );
196 }
197
198 BOOST_FIXTURE_TEST_CASE(one_of_three_over_threshold_dup, threshold_fixture)
199 {
200 std::map<bfs::path, uintmax_t> availables {{"/test0", 100000},
201 {"/test1", 250000},
202 {"/test2", 250000}};
203 std::map<bfs::path, int> devs {{"/test0", 0},
204 {"/test1", 1}, // dup
205 {"/test2", 1}}; // dup
206
207 BOOST_TEST( test_threshold_common(availables, devs) );
208 }
209
210 BOOST_FIXTURE_TEST_CASE(none_of_three_over_threshold, threshold_fixture)
211 {
212 std::map<bfs::path, uintmax_t> availables {{"/test0", 300000},
213 {"/test1", 200000},
214 {"/test2", 250000}};
215 std::map<bfs::path, int> devs {{"/test0", 0},
216 {"/test1", 1},
217 {"/test2", 2}};
218
219 BOOST_TEST( !test_threshold_common(availables, devs) );
220 }
221
222 BOOST_FIXTURE_TEST_CASE(none_of_three_over_threshold_dup, threshold_fixture)
223 {
224 std::map<bfs::path, uintmax_t> availables {{"/test0", 800000},
225 {"/test1", 550000},
226 {"/test2", 550000}};
227 std::map<bfs::path, int> devs {{"/test0", 0},
228 {"/test1", 1}, // dup
229 {"/test2", 1}}; // dup
230
231 BOOST_TEST( !test_threshold_common(availables, devs) );
232 }
233
234 BOOST_FIXTURE_TEST_CASE(warning_threshold_equal_to_threshold, threshold_fixture)
235 {
236 std::map<bfs::path, uintmax_t> availables {{"/test0", 150000}};
237 std::map<bfs::path, int> devs {{"/test0", 0}};
238
239 BOOST_REQUIRE_THROW(test_threshold_common(availables, devs, 80), chain::plugin_config_exception);
240 }
241
242 BOOST_FIXTURE_TEST_CASE(warning_threshold_greater_than_threshold, threshold_fixture)
243 {
244 std::map<bfs::path, uintmax_t> availables {{"/test0", 150000}};
245 std::map<bfs::path, int> devs {{"/test0", 0}};
246
247 BOOST_REQUIRE_THROW( test_threshold_common(availables, devs, 85), chain::plugin_config_exception );
248 }
249
250 BOOST_FIXTURE_TEST_CASE(warning_threshold_less_than_threshold, threshold_fixture)
251 {
252 std::map<bfs::path, uintmax_t> availables {{"/test0", 200000}};
253 std::map<bfs::path, int> devs {{"/test0", 0}};
254
255 BOOST_TEST( !test_threshold_common(availables, devs, 70) );
256 }
257
258 BOOST_FIXTURE_TEST_CASE(get_space_failure_in_middle, threshold_fixture)
259 {
260 mock_get_space = [ i = 0 ]( const bfs::path& p, boost::system::error_code& ec) mutable -> bfs::space_info {
261 if ( i == 3 ) {
262 ec = boost::system::errc::make_error_code(errc::no_such_file_or_directory);
263 } else {
264 ec = boost::system::errc::make_error_code(errc::success);
265 }
266
267 bfs::space_info rc;
268 rc.capacity = 1000000;
269 rc.available = 200500;
270
271 i++;
272
273 return rc;
274 };
275
276 mock_get_stat = []( const char *path, struct stat *buf ) -> int {
277 buf->st_dev = 0;
278 return 0;
279 };
280
281 set_threshold(80, 75);
282 add_file_system("/test");
283
284 auto expected_response = false;
285
286 auto actual_response_0 = is_threshold_exceeded();
287 auto actual_response_1 = is_threshold_exceeded();
288 auto actual_response_2 = is_threshold_exceeded();
289 auto actual_response_3 = is_threshold_exceeded();
290 auto actual_response_4 = is_threshold_exceeded();
291 auto actual_response_5 = is_threshold_exceeded();
292
293 BOOST_TEST(expected_response == actual_response_0);
294 BOOST_TEST(expected_response == actual_response_1);
295 BOOST_TEST(expected_response == actual_response_2);
296 BOOST_TEST(expected_response == actual_response_3);
297 BOOST_TEST(expected_response == actual_response_4);
298 BOOST_TEST(expected_response == actual_response_5);
299 }
300
301BOOST_AUTO_TEST_SUITE_END()
const mie::Vuint & p
Definition bn.cpp:27
std::string name
wraps boost::filesystem::path to provide platform independent path manipulation.
void set_threshold(uint32_t new_threshold, uint32_t new_warning_threshold)
void add_file_system(const bfs::path &path_name)
void set_shutdown_on_exceeded(bool new_shutdown_on_exceeded)
unsigned int uint32_t
Definition stdint.h:126
Immutable except for fc::from_variant.
Definition name.hpp:43
int get_stat(const char *path, struct stat *buf) const
mock_space_provider(threshold_fixture &fixture)
bfs::space_info get_space(const bfs::path &p, boost::system::error_code &ec) const
void set_shutdown_on_exceeded(bool shutdown_on_exceeded)
bool test_threshold_common(std::map< bfs::path, uintmax_t > &available, std::map< bfs::path, int > &dev, uint32_t warning_threshold=75)
void set_threshold(uint32_t threshold, uint32_t warning_threshold)
void add_file_system(const bfs::path &path_name)
std::function< int(const char *path, struct stat *buf)> mock_get_stat
file_space_handler_t space_handler
std::function< bfs::space_info(const bfs::path &p, boost::system::error_code &ec)> mock_get_space
boost::asio::io_context ctx
BOOST_FIXTURE_TEST_CASE(equal_to_threshold, threshold_fixture)
uint8_t buf[2048]
yh_rc rc