|
| 1 | +// Copyright 2025 iLogtail Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "common/JsonUtil.h" |
| 16 | +#include "config/OnetimeConfigManager.h" |
| 17 | +#include "unittest/Unittest.h" |
| 18 | + |
| 19 | +using namespace std; |
| 20 | + |
| 21 | +namespace logtail { |
| 22 | + |
| 23 | +class OnetimeConfigManagerUnittest : public testing::Test { |
| 24 | +public: |
| 25 | + void TestGetOnetimeConfigStatusFromCheckpoint() const; |
| 26 | + void TestUpdateConfig() const; |
| 27 | + void TestCheckpointFile() const; |
| 28 | + |
| 29 | +protected: |
| 30 | + // static void SetUpTestCase() { sManager->mCheckpointRootPath = filesystem::path("./input_static_file"); } |
| 31 | + |
| 32 | + // void SetUp() override { filesystem::create_directories(sManager->mCheckpointRootPath); } |
| 33 | + |
| 34 | + void TearDown() override { |
| 35 | + // sManager->ClearUnusedCheckpoints(); |
| 36 | + // sManager->mInputCheckpointMap.clear(); |
| 37 | + // filesystem::remove_all(sManager->mCheckpointRootPath); |
| 38 | + } |
| 39 | + |
| 40 | +private: |
| 41 | + static OnetimeConfigManager* sManager; |
| 42 | +}; |
| 43 | + |
| 44 | +void OnetimeConfigManagerUnittest::TestGetOnetimeConfigStatusFromCheckpoint() const { |
| 45 | +} |
| 46 | + |
| 47 | +void OnetimeConfigManagerUnittest::TestUpdateConfig() const { |
| 48 | + filesystem::create_directories("test_config"); |
| 49 | + { ofstream fout("test_config/test_config_1.json"); } |
| 50 | + { ofstream fout("test_config/test_config_2.json"); } |
| 51 | + |
| 52 | + // restart |
| 53 | + APSARA_TEST_TRUE(sManager->AddConfig( |
| 54 | + "test_config_1", ConfigType::Collection, filesystem::path("test_config/test_config_1.json"), 1, 1000000000)); |
| 55 | + APSARA_TEST_TRUE(sManager->AddConfig( |
| 56 | + "test_config_2", ConfigType::Collection, filesystem::path("test_config/test_config_2.json"), 2, 1500000000)); |
| 57 | + APSARA_TEST_TRUE(sManager->AddConfig( |
| 58 | + "test_config_3", ConfigType::Collection, filesystem::path("test_config/test_config_3.json"), 3, 4000000000)); |
| 59 | + APSARA_TEST_EQUAL(3U, sManager->mConfigInfoMap.size()); |
| 60 | + { |
| 61 | + const auto& info = sManager->mConfigInfoMap.at("test_config_1"); |
| 62 | + APSARA_TEST_EQUAL(ConfigType::Collection, info.mType); |
| 63 | + APSARA_TEST_EQUAL(filesystem::path("test_config/test_config_1.json"), info.mFilepath); |
| 64 | + APSARA_TEST_EQUAL(1U, info.mHash); |
| 65 | + APSARA_TEST_EQUAL(1000000000U, info.mExpireTime); |
| 66 | + } |
| 67 | + { |
| 68 | + const auto& info = sManager->mConfigInfoMap.at("test_config_2"); |
| 69 | + APSARA_TEST_EQUAL(ConfigType::Collection, info.mType); |
| 70 | + APSARA_TEST_EQUAL(filesystem::path("test_config/test_config_2.json"), info.mFilepath); |
| 71 | + APSARA_TEST_EQUAL(2U, info.mHash); |
| 72 | + APSARA_TEST_EQUAL(1500000000U, info.mExpireTime); |
| 73 | + } |
| 74 | + { |
| 75 | + const auto& info = sManager->mConfigInfoMap.at("test_config_3"); |
| 76 | + APSARA_TEST_EQUAL(ConfigType::Collection, info.mType); |
| 77 | + APSARA_TEST_EQUAL(filesystem::path("test_config/test_config_3.json"), info.mFilepath); |
| 78 | + APSARA_TEST_EQUAL(3U, info.mHash); |
| 79 | + APSARA_TEST_EQUAL(4000000000U, info.mExpireTime); |
| 80 | + } |
| 81 | + |
| 82 | + // update |
| 83 | + APSARA_TEST_TRUE(sManager->RemoveConfig("test_config_1")); |
| 84 | + APSARA_TEST_TRUE(sManager->AddConfig( |
| 85 | + "test_config_1", ConfigType::Collection, filesystem::path("test_config/test_config_1.json"), 1, 1200000000)); |
| 86 | + APSARA_TEST_FALSE(sManager->RemoveConfig("test_config_4")); |
| 87 | + APSARA_TEST_EQUAL(3U, sManager->mConfigInfoMap.size()); |
| 88 | + { |
| 89 | + const auto& info = sManager->mConfigInfoMap.at("test_config_1"); |
| 90 | + APSARA_TEST_EQUAL(ConfigType::Collection, info.mType); |
| 91 | + APSARA_TEST_EQUAL(filesystem::path("test_config/test_config_1.json"), info.mFilepath); |
| 92 | + APSARA_TEST_EQUAL(1U, info.mHash); |
| 93 | + APSARA_TEST_EQUAL(1200000000U, info.mExpireTime); |
| 94 | + } |
| 95 | + |
| 96 | + // delete timeout config |
| 97 | + filesystem::remove("test_config/test_config_1.json"); |
| 98 | + sManager->DeleteTimeoutConfigFiles(); |
| 99 | + APSARA_TEST_EQUAL(1U, sManager->mConfigInfoMap.size()); |
| 100 | + APSARA_TEST_NOT_EQUAL(sManager->mConfigInfoMap.end(), sManager->mConfigInfoMap.find("test_config_3")); |
| 101 | + APSARA_TEST_FALSE(filesystem::exists("test_config/test_config_2.json")); |
| 102 | + |
| 103 | + filesystem::remove_all("test_config"); |
| 104 | +} |
| 105 | + |
| 106 | +void OnetimeConfigManagerUnittest::TestCheckpointFile() const { |
| 107 | +} |
| 108 | + |
| 109 | +OnetimeConfigManager* OnetimeConfigManagerUnittest::sManager = OnetimeConfigManager::GetInstance(); |
| 110 | + |
| 111 | +UNIT_TEST_CASE(OnetimeConfigManagerUnittest, TestGetOnetimeConfigStatusFromCheckpoint) |
| 112 | +UNIT_TEST_CASE(OnetimeConfigManagerUnittest, TestUpdateConfig) |
| 113 | +UNIT_TEST_CASE(OnetimeConfigManagerUnittest, TestCheckpointFile) |
| 114 | + |
| 115 | +} // namespace logtail |
| 116 | + |
| 117 | +UNIT_TEST_MAIN |
0 commit comments