|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 | #include "manager_test.hpp" |
3 | 3 |
|
| 4 | +#include <csignal> |
| 5 | + |
4 | 6 | std::filesystem::path ManagerTest::dataSyncCfgDir; |
5 | 7 | std::filesystem::path ManagerTest::tmpDataSyncDataDir; |
6 | 8 | nlohmann::json ManagerTest::commonJsonData; |
@@ -208,3 +210,118 @@ TEST_F(ManagerTest, testDBusDataPersistency) |
208 | 210 |
|
209 | 211 | ctx.run(); |
210 | 212 | } |
| 213 | + |
| 214 | +TEST_F(ManagerTest, testSignalReceiverLogic) |
| 215 | +{ |
| 216 | + using namespace std::literals; |
| 217 | + namespace ed = data_sync::ext_data; |
| 218 | + |
| 219 | + std::unique_ptr<ed::ExternalDataIFaces> extDataIface = |
| 220 | + std::make_unique<ed::MockExternalDataIFaces>(); |
| 221 | + |
| 222 | + ed::MockExternalDataIFaces* mockExtDataIfaces = |
| 223 | + dynamic_cast<ed::MockExternalDataIFaces*>(extDataIface.get()); |
| 224 | + |
| 225 | + ON_CALL(*mockExtDataIfaces, fetchBMCRedundancyMgrProps()) |
| 226 | + // NOLINTNEXTLINE |
| 227 | + .WillByDefault([&mockExtDataIfaces]() -> sdbusplus::async::task<> { |
| 228 | + mockExtDataIfaces->setBMCRole(ed::BMCRole::Active); |
| 229 | + mockExtDataIfaces->setBMCRedundancy(true); |
| 230 | + co_return; |
| 231 | + }); |
| 232 | + |
| 233 | + EXPECT_CALL(*mockExtDataIfaces, fetchSiblingBmcIP()) |
| 234 | + // NOLINTNEXTLINE |
| 235 | + .WillRepeatedly([]() -> sdbusplus::async::task<> { co_return; }); |
| 236 | + |
| 237 | + EXPECT_CALL(*mockExtDataIfaces, fetchRbmcCredentials()) |
| 238 | + // NOLINTNEXTLINE |
| 239 | + .WillRepeatedly([]() -> sdbusplus::async::task<> { co_return; }); |
| 240 | + |
| 241 | + nlohmann::json jsonData = { |
| 242 | + {"Files", |
| 243 | + { |
| 244 | + {{"Path", ManagerTest::tmpDataSyncDataDir.string() + "/srcFile1"}, |
| 245 | + {"DestinationPath", |
| 246 | + ManagerTest::tmpDataSyncDataDir.string() + "/destDir/"}, |
| 247 | + {"Description", "FullSync from Active to Passive bmc"}, |
| 248 | + {"SyncDirection", "Active2Passive"}, |
| 249 | + {"SyncType", "Immediate"}}, |
| 250 | + {{"Path", ManagerTest::tmpDataSyncDataDir.string() + "/srcFile2"}, |
| 251 | + {"DestinationPath", |
| 252 | + ManagerTest::tmpDataSyncDataDir.string() + "/destDir/"}, |
| 253 | + {"Description", "FullSync from Active to Passive bmc"}, |
| 254 | + {"SyncDirection", "Active2Passive"}, |
| 255 | + {"SyncType", "Immediate"}}, |
| 256 | + }}}; |
| 257 | + |
| 258 | + fs::path srcFile1{jsonData["Files"][0]["Path"]}; |
| 259 | + fs::path srcFile2{jsonData["Files"][1]["Path"]}; |
| 260 | + |
| 261 | + fs::path destDir1{jsonData["Files"][0]["DestinationPath"]}; |
| 262 | + fs::path destDir2{jsonData["Files"][1]["DestinationPath"]}; |
| 263 | + |
| 264 | + writeConfig(jsonData); |
| 265 | + std::filesystem::remove("/tmp/data_sync_paths_info.lsv"); |
| 266 | + sdbusplus::async::context ctx; |
| 267 | + |
| 268 | + std::string data1{"Data written on the file1\n"}; |
| 269 | + std::string data2{"Data written on the file2\n"}; |
| 270 | + |
| 271 | + ManagerTest::writeData(srcFile1, data1); |
| 272 | + ManagerTest::writeData(srcFile2, data2); |
| 273 | + |
| 274 | + ASSERT_EQ(ManagerTest::readData(srcFile1), data1); |
| 275 | + ASSERT_EQ(ManagerTest::readData(srcFile2), data2); |
| 276 | + |
| 277 | + data_sync::Manager manager{ctx, std::move(extDataIface), |
| 278 | + ManagerTest::dataSyncCfgDir}; |
| 279 | + |
| 280 | + auto waitingForFullSyncToFinish = |
| 281 | + // NOLINTNEXTLINE |
| 282 | + [&](sdbusplus::async::context& ctx) -> sdbusplus::async::task<void> { |
| 283 | + auto status = manager.getFullSyncStatus(); |
| 284 | + |
| 285 | + while (status != FullSyncStatus::FullSyncCompleted && |
| 286 | + status != FullSyncStatus::FullSyncFailed) |
| 287 | + { |
| 288 | + co_await sdbusplus::async::sleep_for(ctx, |
| 289 | + std::chrono::milliseconds(50)); |
| 290 | + status = manager.getFullSyncStatus(); |
| 291 | + } |
| 292 | + |
| 293 | + EXPECT_EQ(status, FullSyncStatus::FullSyncCompleted) |
| 294 | + << "FullSync status is not Completed!"; |
| 295 | + |
| 296 | + // Raising signal after full sync completes, so that sync events starts |
| 297 | + // and the watchers are being created. |
| 298 | + sdbusplus::async::sleep_for(ctx, 0.1s); |
| 299 | + // Raising a signal to trigger the signal handler logic. |
| 300 | + std::raise(SIGUSR1); |
| 301 | + // waiting to write all the watched paths to a file as per |
| 302 | + // signal handler logic. |
| 303 | + sdbusplus::async::sleep_for(ctx, 0.5s); |
| 304 | + |
| 305 | + ctx.request_stop(); |
| 306 | + |
| 307 | + // Forcing to trigger inotify events so that all running immediate |
| 308 | + // sync tasks will resume and stop since the context is requested to |
| 309 | + // stop in the above. |
| 310 | + ManagerTest::writeData(srcFile1, data1); |
| 311 | + ManagerTest::writeData(srcFile2, data2); |
| 312 | + |
| 313 | + // Reading the file where all the watched paths are written by signal |
| 314 | + // handler logic. |
| 315 | + auto watchersF = |
| 316 | + ManagerTest::readData("/tmp/data_sync_paths_info.json"); |
| 317 | + auto parsed = nlohmann::json::parse(watchersF); |
| 318 | + EXPECT_TRUE(parsed.is_array()); |
| 319 | + // Checking if the srcFile1 and srcFile2 are present in the parsed json |
| 320 | + EXPECT_NE(watchersF.find(srcFile1), std::string::npos); |
| 321 | + EXPECT_NE(watchersF.find(srcFile2), std::string::npos); |
| 322 | + co_return; |
| 323 | + }; |
| 324 | + |
| 325 | + ctx.spawn(waitingForFullSyncToFinish(ctx)); |
| 326 | + ctx.run(); |
| 327 | +} |
0 commit comments