file_manager_strategy_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #include <list>
17 
18 #include <iostream>
19 #include <fstream>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <chrono>
23 #include <thread>
24 #include <experimental/filesystem>
25 
26 #include <gtest/gtest.h>
27 #include <gmock/gmock.h>
28 #include <aws/logs/model/InputLogEvent.h>
30 
31 using namespace Aws::FileManagement;
32 
34 public:
36 
37  std::string getFileToRead() {
39  }
40 
41  std::string getActiveWriteFile() {
43  }
44 };
45 
46 class FileManagerStrategyTest : public ::testing::Test {
47 public:
48  void SetUp() override
49  {
50  }
51 
52  void TearDown() override
53  {
54  std::experimental::filesystem::path test_folder{"log_tests/"};
55  std::experimental::filesystem::remove_all(test_folder);
56  }
57 
58 protected:
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;
64  FileManagerStrategyOptions options{folder, prefix, extension, max_file_size, storage_limit};
65 };
66 
67 
68 TEST_F(FileManagerStrategyTest, restart_without_token) {
69  const std::string data1 = "test_data_1";
70  const std::string data2 = "test_data_2";
71  {
72  FileManagerStrategy file_manager_strategy(options);
73  EXPECT_NO_THROW(file_manager_strategy.start());
74  file_manager_strategy.write(data1);
75  file_manager_strategy.write(data2);
76  }
77  {
78  FileManagerStrategy file_manager_strategy(options);
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);
85  }
86 }
87 
88 // @todo (rddesmon) preserve file of tokens on restart
89 //TEST_F(FileManagerStrategyTest, restart_with_token) {
90 // const std::string data1 = "test_data_1";
91 // const std::string data2 = "test_data_2";
92 // {
93 // FileManagerStrategy file_manager_strategy(options);
94 // EXPECT_NO_THROW(file_manager_strategy.start());
95 // file_manager_strategy.write(data1);
96 // file_manager_strategy.write(data2);
97 // std::string result1;
98 // DataToken token1 = file_manager_strategy.read(result1);
99 // EXPECT_EQ(data1, result1);
100 // }
101 // {
102 // FileManagerStrategy file_manager_strategy(options);
103 // EXPECT_NO_THROW(file_manager_strategy.start());
104 // std::string result2;
105 // DataToken token2 = file_manager_strategy.read(result2);
106 // EXPECT_EQ(data2, result2);
107 // }
108 //}
109 
110 TEST_F(FileManagerStrategyTest, fail_token_restart_from_last_location) {
111  const std::string data1 = "test_data_1";
112  const std::string data2 = "test_data_2";
113  FileManagerStrategy file_manager_strategy(options);
114  EXPECT_NO_THROW(file_manager_strategy.start());
115  file_manager_strategy.write(data1);
116  file_manager_strategy.write(data2);
117  std::string result1;
118  DataToken token1 = file_manager_strategy.read(result1);
119  EXPECT_EQ(data1, result1);
120  file_manager_strategy.resolve(token1, true);
121  std::string result2, result3, result4;
122  DataToken token2 = file_manager_strategy.read(result2);
123  EXPECT_EQ(data2, result2);
124 
125  file_manager_strategy.resolve(token2, false);
126  // Token was failed, should be re-read.
127  DataToken token3 = file_manager_strategy.read(result3);
128  EXPECT_EQ(data2, result3);
129  file_manager_strategy.resolve(token3, false);
130  // Token was failed, should be re-read.
131  EXPECT_TRUE(file_manager_strategy.isDataAvailable());
132  DataToken token4 = file_manager_strategy.read(result4);
133  EXPECT_EQ(data2, result4);
134  EXPECT_FALSE(file_manager_strategy.isDataAvailable());
135  file_manager_strategy.resolve(token4, true);
136  EXPECT_FALSE(file_manager_strategy.isDataAvailable());
137 }
138 
143  FileManagerStrategy file_manager_strategy(options);
144  EXPECT_NO_THROW(file_manager_strategy.start());
145 }
146 
147 TEST_F(FileManagerStrategyTest, discover_stored_files) {
148  const std::string test_data = "test_data";
149  {
150  FileManagerStrategy file_manager_strategy(options);
151  EXPECT_NO_THROW(file_manager_strategy.start());
152  file_manager_strategy.write(test_data);
153  }
154  {
155  FileManagerStrategy file_manager_strategy(options);
156  EXPECT_NO_THROW(file_manager_strategy.start());
157  EXPECT_TRUE(file_manager_strategy.isDataAvailable());
158  std::string result;
159  DataToken token = file_manager_strategy.read(result);
160  EXPECT_EQ(test_data, result);
161  file_manager_strategy.resolve(token, true);
162  }
163 }
164 
165 TEST_F(FileManagerStrategyTest, get_file_to_read_gets_newest) {
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;
169  {
170  MockFileManagerStrategy file_manager_strategy(options);
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.";
175  }
176  std::string string_25_kb = ss_25_kb.str();
177 
178  for (int i = 0; i < 5; i++) {
179  file_manager_strategy.write(string_25_kb);
180  }
181 
182  long file_count = std::distance(fs::directory_iterator(folder), fs::directory_iterator{});
183  EXPECT_EQ(5, file_count);
184 
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);
189  }
190 
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];
194 
195  EXPECT_EQ(expected_active_write_file, file_manager_strategy.getActiveWriteFile());
196  EXPECT_EQ(expected_file_to_be_read, file_manager_strategy.getFileToRead());
197  }
198 }
199 
200 
201 TEST_F(FileManagerStrategyTest, rotate_large_files) {
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;
205  {
206  FileManagerStrategy file_manager_strategy(options);
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";
211  }
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";
219  }
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);
224  }
225 }
226 
227 TEST_F(FileManagerStrategyTest, resolve_token_deletes_file) {
228  const std::string test_data = "test_data";
229  {
230  FileManagerStrategy file_manager_strategy(options);
231  file_manager_strategy.start();
232  EXPECT_FALSE(file_manager_strategy.isDataAvailable());
233  file_manager_strategy.write(test_data);
234  EXPECT_TRUE(file_manager_strategy.isDataAvailable());
235  std::string result;
236  DataToken token = file_manager_strategy.read(result);
237  file_manager_strategy.resolve(token, true);
238  }
239  {
240  FileManagerStrategy file_manager_strategy(options);
241  file_manager_strategy.start();
242  EXPECT_FALSE(file_manager_strategy.isDataAvailable());
243  }
244 }
245 
246 TEST_F(FileManagerStrategyTest, on_storage_limit_delete_oldest_file) {
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;
252  {
253  FileManagerStrategy file_manager_strategy(options);
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.";
258  }
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);
263 
264  for (int i = 0; i < 5; i++) {
265  file_manager_strategy.write(string_25_kb);
266  }
267 
268  file_count = std::distance(fs::directory_iterator(folder), fs::directory_iterator{});
269  EXPECT_EQ(3, file_count);
270 
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);
275  }
276 
277  std::sort(file_paths.begin(), file_paths.end());
278  const std::string file_to_be_deleted = file_paths[0];
279 
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);
283 
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);
287  }
288  }
289 }
290 
291 class SanitizePathTest : public ::testing::Test {
292 public:
293  void SetUp() override
294  {
295  }
296 
297  void TearDown() override
298  {
299  }
300 
301 };
302 
303 TEST_F(SanitizePathTest, sanitizePath_home_set) {
304  std::string test_path = "~/dir/";
305  char * original_home = getenv("HOME");
306  setenv("HOME", "/home", 1);
307 
308  sanitizePath(test_path);
309 
310  // Cleanup before test
311  if (nullptr != original_home) {
312  setenv("HOME", original_home, 1);
313  } else {
314  unsetenv("HOME");
315  }
316 
317  EXPECT_STREQ(test_path.c_str(), "/home/dir/");
318 }
319 
320 TEST_F(SanitizePathTest, sanitizePath_home_not_set_roshome_set) {
321  std::string test_path = "~/dir/";
322  char * original_home = getenv("HOME");
323  char * original_ros_home = getenv("ROS_HOME");
324  unsetenv("HOME");
325  setenv("ROS_HOME", "/ros_home", 1);
326 
327  sanitizePath(test_path);
328 
329  // Cleanup before test
330  if (nullptr != original_home) {
331  setenv("HOME", original_home, 1);
332  }
333  if (nullptr != original_ros_home) {
334  setenv("ROS_HOME", original_ros_home, 1);
335  } else {
336  unsetenv("ROS_HOME");
337  }
338 
339  EXPECT_STREQ(test_path.c_str(), "/ros_home/dir/");
340 }
341 
342 
343 TEST_F(SanitizePathTest, sanitizePath_home_not_set_roshome_not_set) {
344  std::string test_path = "~/dir/";
345  char * original_home = getenv("HOME");
346  char * original_ros_home = getenv("ROS_HOME");
347  unsetenv("HOME");
348  unsetenv("ROS_HOME");
349 
350 
351  ASSERT_THROW(sanitizePath(test_path), std::runtime_error);
352 
353  // Cleanup before test
354  if (nullptr != original_home) {
355  setenv("HOME", original_home, 1);
356  }
357  if (nullptr != original_ros_home) {
358  setenv("ROS_HOME", original_ros_home, 1);
359  }
360 
361 }
362 
363 TEST_F(SanitizePathTest, sanitizePath_adds_trailing_slash) {
364  std::string test_path = "/test/path";
365  sanitizePath(test_path);
366  EXPECT_STREQ(test_path.c_str(), "/test/path/");
367 }
DataToken read(std::string &data) 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


file_management
Author(s): AWS RoboMaker
autogenerated on Fri May 7 2021 02:18:23