Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2020 YADRO |
| 3 | |
| 4 | #include "file_storage.hpp" |
| 5 | |
| 6 | #include <fstream> |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | namespace fs = std::filesystem; |
| 11 | |
| 12 | /** |
| 13 | * @class FileStorageTest |
| 14 | * @brief Persistent file storage tests. |
| 15 | */ |
| 16 | class FileStorageTest : public ::testing::Test |
| 17 | { |
| 18 | protected: |
| 19 | void SetUp() override |
| 20 | { |
| 21 | fs::remove_all(logPath); |
| 22 | } |
| 23 | |
| 24 | void TearDown() override |
| 25 | { |
| 26 | fs::remove_all(logPath); |
| 27 | } |
| 28 | |
Patrick Williams | b675272 | 2023-05-10 07:50:26 -0500 | [diff] [blame^] | 29 | const fs::path logPath = fs::temp_directory_path() / |
| 30 | "file_storage_test_out"; |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | TEST_F(FileStorageTest, InvalidPath) |
| 34 | { |
| 35 | ASSERT_THROW(FileStorage("", "", 0), std::invalid_argument); |
| 36 | ASSERT_THROW(FileStorage("relative/path", "", 0), std::invalid_argument); |
| 37 | ASSERT_THROW(FileStorage("/noaccess", "", 0), fs::filesystem_error); |
| 38 | } |
| 39 | |
| 40 | TEST_F(FileStorageTest, Save) |
| 41 | { |
| 42 | const char* data = "test message\n"; |
| 43 | LogBuffer buf(0, 0); |
| 44 | buf.append(data, strlen(data)); |
| 45 | |
| 46 | FileStorage fs(logPath, "", 0); |
| 47 | fs.save(buf); |
| 48 | |
| 49 | const auto itBegin = fs::recursive_directory_iterator(logPath); |
| 50 | const auto itEnd = fs::recursive_directory_iterator{}; |
| 51 | ASSERT_EQ(std::distance(itBegin, itEnd), 1); |
| 52 | |
| 53 | const fs::path file = *fs::directory_iterator(logPath); |
| 54 | EXPECT_NE(fs::file_size(file), 0); |
| 55 | } |
| 56 | |
| 57 | TEST_F(FileStorageTest, Rotation) |
| 58 | { |
| 59 | const size_t limit = 5; |
| 60 | const std::string prefix = "host123"; |
| 61 | |
| 62 | const char* data = "test message\n"; |
| 63 | LogBuffer buf(0, 0); |
| 64 | buf.append(data, strlen(data)); |
| 65 | |
| 66 | FileStorage fs(logPath, prefix, limit); |
| 67 | for (size_t i = 0; i < limit + 3; ++i) |
| 68 | { |
| 69 | fs.save(buf); |
| 70 | } |
| 71 | |
| 72 | // Dir and other files that can not be removed |
| 73 | const fs::path dir = logPath / (prefix + "_11111111_222222.log.gz"); |
| 74 | const fs::path files[] = {logPath / "short", |
| 75 | logPath / (prefix + "_11111111_222222.bad.ext"), |
| 76 | logPath / (prefix + "x_11111111_222222.log.gz")}; |
| 77 | fs::create_directory(dir); |
| 78 | for (const auto& i : files) |
| 79 | { |
| 80 | std::ofstream dummy(i); |
| 81 | } |
| 82 | |
| 83 | const auto itBegin = fs::recursive_directory_iterator(logPath); |
| 84 | const auto itEnd = fs::recursive_directory_iterator{}; |
| 85 | EXPECT_EQ(std::distance(itBegin, itEnd), |
| 86 | limit + 1 /*dir*/ + sizeof(files) / sizeof(files[0])); |
| 87 | EXPECT_TRUE(fs::exists(dir)); |
| 88 | for (const auto& i : files) |
| 89 | { |
| 90 | EXPECT_TRUE(fs::exists(i)); |
| 91 | } |
| 92 | } |