24 #include <experimental/filesystem> 26 #include <gtest/gtest.h> 27 #include <gmock/gmock.h> 28 #include <aws/logs/model/InputLogEvent.h> 54 std::experimental::filesystem::path test_folder{
"log_tests/"};
55 std::experimental::filesystem::remove_all(test_folder);
59 std::string folder =
"log_tests/";
60 std::string prefix =
"test";
61 std::string extension =
".log";
62 uint max_file_size = 1024;
63 uint storage_limit = max_file_size * 10;
69 const std::string data1 =
"test_data_1";
70 const std::string data2 =
"test_data_2";
73 EXPECT_NO_THROW(file_manager_strategy.
start());
74 file_manager_strategy.
write(data1);
75 file_manager_strategy.
write(data2);
79 EXPECT_NO_THROW(file_manager_strategy.
start());
80 std::string result1, result2;
81 file_manager_strategy.
read(result1);
82 file_manager_strategy.
read(result2);
83 EXPECT_EQ(data1, result1);
84 EXPECT_EQ(data2, result2);
111 const std::string data1 =
"test_data_1";
112 const std::string data2 =
"test_data_2";
114 EXPECT_NO_THROW(file_manager_strategy.
start());
115 file_manager_strategy.
write(data1);
116 file_manager_strategy.
write(data2);
119 EXPECT_EQ(data1, result1);
120 file_manager_strategy.
resolve(token1,
true);
121 std::string result2, result3, result4;
123 EXPECT_EQ(data2, result2);
125 file_manager_strategy.
resolve(token2,
false);
128 EXPECT_EQ(data2, result3);
129 file_manager_strategy.
resolve(token3,
false);
133 EXPECT_EQ(data2, result4);
135 file_manager_strategy.
resolve(token4,
true);
144 EXPECT_NO_THROW(file_manager_strategy.
start());
148 const std::string test_data =
"test_data";
151 EXPECT_NO_THROW(file_manager_strategy.
start());
152 file_manager_strategy.
write(test_data);
156 EXPECT_NO_THROW(file_manager_strategy.
start());
160 EXPECT_EQ(test_data, result);
161 file_manager_strategy.
resolve(token,
true);
166 namespace fs = std::experimental::filesystem;
167 const uint max_file_size_in_kb = 25;
168 options.maximum_file_size_in_kb = max_file_size_in_kb;
171 file_manager_strategy.
start();
172 std::ostringstream ss_25_kb;
173 for (
int i = 0; i < 1024; i++) {
174 ss_25_kb <<
"This is 25 bytes of data.";
176 std::string string_25_kb = ss_25_kb.str();
178 for (
int i = 0; i < 5; i++) {
179 file_manager_strategy.
write(string_25_kb);
182 long file_count = std::distance(fs::directory_iterator(folder), fs::directory_iterator{});
183 EXPECT_EQ(5, file_count);
185 std::vector<std::string> file_paths;
186 for (
const auto &entry : fs::directory_iterator(folder)) {
187 const fs::path &path = entry.path();
188 file_paths.push_back(path);
191 std::sort(file_paths.begin(), file_paths.end());
192 const std::string expected_active_write_file = file_paths.end()[-1];
193 const std::string expected_file_to_be_read = file_paths.end()[-2];
196 EXPECT_EQ(expected_file_to_be_read, file_manager_strategy.
getFileToRead());
202 namespace fs = std::experimental::filesystem;
203 const uint max_file_size_in_kb = 10;
204 options.maximum_file_size_in_kb = max_file_size_in_kb;
207 file_manager_strategy.
start();
208 std::ostringstream data1ss;
209 for (
int i = 0; i < 1024; i++) {
210 data1ss <<
"This is some long data that is longer than 10 bytes";
212 std::string data1 = data1ss.str();
213 file_manager_strategy.
write(data1);
214 long file_count = std::distance(fs::directory_iterator(folder), fs::directory_iterator{});
215 EXPECT_EQ(1, file_count);
216 std::ostringstream data2ss;
217 for (
int i = 0; i < 1024; i++) {
218 data2ss <<
"This is some additional data that is also longer than 10 bytes";
220 std::string data2 = data2ss.str();
221 file_manager_strategy.
write(data2);
222 file_count = std::distance(fs::directory_iterator(folder), fs::directory_iterator{});
223 EXPECT_EQ(2, file_count);
228 const std::string test_data =
"test_data";
231 file_manager_strategy.
start();
233 file_manager_strategy.
write(test_data);
237 file_manager_strategy.
resolve(token,
true);
241 file_manager_strategy.
start();
247 namespace fs = std::experimental::filesystem;
248 const uint max_file_size_in_kb = 50;
249 const uint storage_limit = 150;
250 options.maximum_file_size_in_kb = max_file_size_in_kb;
251 options.storage_limit_in_kb = storage_limit;
254 file_manager_strategy.
start();
255 std::ostringstream ss_25_kb;
256 for (
int i = 0; i < 1024; i++) {
257 ss_25_kb <<
"This is 25 bytes of data.";
259 std::string string_25_kb = ss_25_kb.str();
260 file_manager_strategy.
write(string_25_kb);
261 long file_count = std::distance(fs::directory_iterator(folder), fs::directory_iterator{});
262 EXPECT_EQ(1, file_count);
264 for (
int i = 0; i < 5; i++) {
265 file_manager_strategy.
write(string_25_kb);
268 file_count = std::distance(fs::directory_iterator(folder), fs::directory_iterator{});
269 EXPECT_EQ(3, file_count);
271 std::vector<std::string> file_paths;
272 for (
const auto &entry : fs::directory_iterator(folder)) {
273 const fs::path &path = entry.path();
274 file_paths.push_back(path);
277 std::sort(file_paths.begin(), file_paths.end());
278 const std::string file_to_be_deleted = file_paths[0];
280 file_manager_strategy.
write(string_25_kb);
281 file_count = std::distance(fs::directory_iterator(folder), fs::directory_iterator{});
282 EXPECT_EQ(3, file_count);
284 for (
const auto &entry : fs::directory_iterator(folder)) {
285 const std::string file_path = entry.path();
286 EXPECT_TRUE(file_path != file_to_be_deleted);
304 std::string test_path =
"~/dir/";
305 char * original_home = getenv(
"HOME");
306 setenv(
"HOME",
"/home", 1);
311 if (
nullptr != original_home) {
312 setenv(
"HOME", original_home, 1);
317 EXPECT_STREQ(test_path.c_str(),
"/home/dir/");
321 std::string test_path =
"~/dir/";
322 char * original_home = getenv(
"HOME");
323 char * original_ros_home = getenv(
"ROS_HOME");
325 setenv(
"ROS_HOME",
"/ros_home", 1);
330 if (
nullptr != original_home) {
331 setenv(
"HOME", original_home, 1);
333 if (
nullptr != original_ros_home) {
334 setenv(
"ROS_HOME", original_ros_home, 1);
336 unsetenv(
"ROS_HOME");
339 EXPECT_STREQ(test_path.c_str(),
"/ros_home/dir/");
344 std::string test_path =
"~/dir/";
345 char * original_home = getenv(
"HOME");
346 char * original_ros_home = getenv(
"ROS_HOME");
348 unsetenv(
"ROS_HOME");
351 ASSERT_THROW(
sanitizePath(test_path), std::runtime_error);
354 if (
nullptr != original_home) {
355 setenv(
"HOME", original_home, 1);
357 if (
nullptr != original_ros_home) {
358 setenv(
"ROS_HOME", original_ros_home, 1);
364 std::string test_path =
"/test/path";
366 EXPECT_STREQ(test_path.c_str(),
"/test/path/");
std::string getActiveWriteFile()
std::string getFileToRead()
DataToken read(std::string &data) override
std::string getFileToRead()
std::string getActiveWriteFile()
bool isDataAvailable() override
void sanitizePath(std::string &path)
TEST_F(FileManagerStrategyTest, restart_without_token)
MockFileManagerStrategy(const FileManagerStrategyOptions &options)
void write(const std::string &data) override
void resolve(const DataToken &token, bool is_success) override