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