Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_add_file_system.cpp
Go to the documentation of this file.
1#define BOOST_TEST_MODULE add_file_system
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 test_add_file_systems_common(std::vector<int>& capacity, std::vector<int>& available, std::vector<int>& devs) {
50 mock_get_space = [ i = 0, capacity, available ]( const bfs::path& p, boost::system::error_code& ec) mutable -> bfs::space_info {
51 ec = boost::system::errc::make_error_code(errc::success);
52
53 bfs::space_info rc;
54 rc.capacity = capacity[i];
55 rc.available = available[i];
56 i++;
57
58 return rc;
59 };
60
61 mock_get_stat = [ j = 0, devs ]( const char *path, struct stat *buf ) mutable -> int {
62 buf->st_dev = devs[j];
63 j++;
64
65 return 0;
66 };
67
68 set_threshold(80, 75);
69
70 for (size_t k = 0; k < capacity.size(); k++) {
71 add_file_system("/test" + std::to_string(k));
72 }
73 }
74
75 // fixture data and methods
76 std::function<bfs::space_info(const bfs::path& p, boost::system::error_code& ec)> mock_get_space;
77 std::function<int(const char *path, struct stat *buf)> mock_get_stat;
78
80};
81
82BOOST_AUTO_TEST_SUITE(space_handler_tests)
84 {
85 mock_get_stat = []( const char *path, struct stat *buf ) -> int {
86 return 1; // anything other than 0 is an error in stat
87 };
88
89 BOOST_REQUIRE_THROW(add_file_system("/test"), chain::plugin_config_exception);
90 }
91
93 {
94 mock_get_space = []( const bfs::path& p, boost::system::error_code& ec) -> bfs::space_info {
95 ec = boost::system::errc::make_error_code(errc::no_such_file_or_directory);
96 bfs::space_info rc;
97 return rc;
98 };
99
100 mock_get_stat = []( const char *path, struct stat *buf ) -> int {
101 buf->st_dev = 0;
102 return 0;
103 };
104
105 BOOST_REQUIRE_THROW(add_file_system("/test"), chain::plugin_config_exception);
106 }
107
109 {
110 std::vector<int> capacity {1000000, 2000000, 3000000, 4000000};
111 std::vector<int> available {500000, 1500000, 2500000, 3500000};
112 std::vector<int> devs {0, 1, 2, 3};
113
114 // As long as no exceptions, it is considered success.
115 BOOST_REQUIRE_NO_THROW(test_add_file_systems_common(capacity, available, devs));
116 }
117
119 {
120 std::vector<int> capacity {1000000, 2000000, 3000000, 4000000};
121 std::vector<int> available {500000, 1500000, 2500000, 3500000};
122 std::vector<int> devs {0, 0, 0, 0};
123
124 // As long as no exceptions, it is considered success.
125 BOOST_REQUIRE_NO_THROW(test_add_file_systems_common(capacity, available, devs));
126 }
127
129 {
130 std::vector<int> capacity {1000000, 2000000, 3000000, 4000000, 50000};
131 std::vector<int> available {500000, 1500000, 2500000, 3500000, 20000};
132 std::vector<int> devs {0, 2, 2, 0, 3};
133
134 // As long as no exceptions, it is considered success.
135 BOOST_REQUIRE_NO_THROW(test_add_file_systems_common(capacity, available, devs));
136 }
137
138BOOST_AUTO_TEST_SUITE_END()
const mie::Vuint & p
Definition bn.cpp:27
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)
unsigned int uint32_t
Definition stdint.h:126
bfs::space_info get_space(const bfs::path &p, boost::system::error_code &ec) const
mock_space_provider(add_file_system_fixture &fixture)
int get_stat(const char *path, struct stat *buf) const
file_space_handler_t space_handler
void add_file_system(const bfs::path &path_name)
void test_add_file_systems_common(std::vector< int > &capacity, std::vector< int > &available, std::vector< int > &devs)
std::function< int(const char *path, struct stat *buf)> mock_get_stat
boost::asio::io_context ctx
void set_threshold(uint32_t threshold, uint32_t warning_threshold)
std::function< bfs::space_info(const bfs::path &p, boost::system::error_code &ec)> mock_get_space
BOOST_FIXTURE_TEST_CASE(get_stat_failure, add_file_system_fixture)
uint16_t j
uint8_t buf[2048]
yh_rc rc