代码拉取完成,页面将自动刷新
// Copyright 2022 iLogtail Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "checkpoint/CheckPointManager.h"
#include "common/FileSystemUtil.h"
#include "common/Flags.h"
#include "unittest/Unittest.h"
DECLARE_FLAG_INT32(checkpoint_find_max_file_count);
namespace logtail {
std::string kTestRootDir;
class CheckpointManagerUnittest : public ::testing::Test {
public:
static void SetUpTestCase() {
kTestRootDir = (bfs::path(GetProcessExecutionDir()) / "CheckpointManagerUnittest").string();
bfs::remove_all(kTestRootDir);
bfs::create_directories(kTestRootDir);
AppConfig::GetInstance()->SetLoongcollectorConfDir(kTestRootDir);
}
static void TearDownTestCase() { bfs::remove_all(kTestRootDir); }
void TestSearchFilePathByDevInodeInDirectory();
};
UNIT_TEST_CASE(CheckpointManagerUnittest, TestSearchFilePathByDevInodeInDirectory);
void CheckpointManagerUnittest::TestSearchFilePathByDevInodeInDirectory() {
const std::string kRotateFileName = "test.log.5";
const std::string kFileName = "test.log";
const std::string kFilePath = (bfs::path(kTestRootDir) / kFileName).string();
const std::string kRotateFilePath = (bfs::path(kTestRootDir) / kRotateFileName).string();
const std::string kTempPath = (bfs::path(kTestRootDir) / ".." / kFileName).string();
std::ofstream(kFilePath) << "";
fsutil::PathStat ps;
EXPECT_TRUE(fsutil::PathStat::stat(kFilePath, ps));
auto devInode = ps.GetDevInode();
// Rotate file in current directory.
bfs::rename(kFilePath, kRotateFilePath);
// Normal search.
{
auto const filePath = SearchFilePathByDevInodeInDirectory(kTestRootDir, 0, devInode, nullptr);
EXPECT_TRUE(filePath);
EXPECT_EQ(filePath.value(), kRotateFilePath);
}
// Exceed limit when search.
{
bfs::rename(kRotateFilePath, kTempPath);
auto bakLimit = INT32_FLAG(checkpoint_find_max_file_count);
INT32_FLAG(checkpoint_find_max_file_count) = 2;
for (size_t idx = 1; idx < 5; ++idx) {
std::ofstream(kFilePath + "." + std::to_string(idx)) << "";
}
std::map<DevInode, SplitedFilePath> cache;
auto const filePath = SearchFilePathByDevInodeInDirectory(kTestRootDir, 0, devInode, &cache);
EXPECT_FALSE(filePath);
EXPECT_EQ(cache.size(), INT32_FLAG(checkpoint_find_max_file_count) + 1);
INT32_FLAG(checkpoint_find_max_file_count) = bakLimit;
bfs::rename(kTempPath, kRotateFilePath);
}
// File is moved to sub-directory.
const auto kSubDir = bfs::path(kTestRootDir) / "sub" / "sub";
bfs::create_directories(kSubDir);
const auto kSubDirFilePath = (kSubDir / kRotateFileName).string();
bfs::rename(kRotateFilePath, kSubDirFilePath);
// Search with depth.
{
auto filePath = SearchFilePathByDevInodeInDirectory(kTestRootDir, 0, devInode, nullptr);
EXPECT_FALSE(filePath);
filePath = SearchFilePathByDevInodeInDirectory(kTestRootDir, 2, devInode, nullptr);
EXPECT_TRUE(filePath);
EXPECT_EQ(filePath.value(), kSubDirFilePath);
}
}
} // namespace logtail
UNIT_TEST_MAIN
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。