From 322ee84dffea8266f6c06c50d874721d10d6305f Mon Sep 17 00:00:00 2001 From: Jakub Pyszczak Date: Tue, 12 Jan 2021 11:26:05 +0100 Subject: [PATCH] [EGD-5108] Fix UT module service Convert module service tests to run natively on linux. --- .../service-desktop/ServiceDesktop.cpp | 3 +- .../deviceInfo/DeviceInfoEndpoint.cpp | 3 ++ .../endpoints/update/UpdateMuditaOS.cpp | 51 ++++++++++--------- .../endpoints/update/UpdateMuditaOS.hpp | 5 +- .../service-desktop/tests/unittest.cpp | 23 +++------ 5 files changed, 42 insertions(+), 43 deletions(-) diff --git a/module-services/service-desktop/ServiceDesktop.cpp b/module-services/service-desktop/ServiceDesktop.cpp index 8d6dd71b2bf2e9c103138227b667d9937b0b2ce5..c21ec3aee8e94ba430fbdf4809ff6c0eff3b670f 100644 --- a/module-services/service-desktop/ServiceDesktop.cpp +++ b/module-services/service-desktop/ServiceDesktop.cpp @@ -109,7 +109,8 @@ sys::ReturnCodes ServiceDesktop::InitHandler() updateOsMsg->updateStats.updateFile.c_str(), updateOsMsg->updateStats.uuid); - if (updateOS->setUpdateFile(updateOsMsg->updateStats.updateFile) == updateos::UpdateError::NoError) + if (updateOS->setUpdateFile(purefs::dir::getUpdatesOSPath(), updateOsMsg->updateStats.updateFile) == + updateos::UpdateError::NoError) updateOS->runUpdate(); } return std::make_shared(); diff --git a/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp b/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp index 59b80ebe941b14346dd2088ca51b4711f3719af8..dd89a6b9f9617a2644974f0024c97093ab137831 100644 --- a/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp +++ b/module-services/service-desktop/endpoints/deviceInfo/DeviceInfoEndpoint.cpp @@ -29,6 +29,9 @@ auto DeviceInfoEndpoint::handle(Context &context) -> void } auto DeviceInfoEndpoint::getDeviceInfo(Context &context) -> bool { + if (ownerServicePtr == nullptr) { + return false; + } json11::Json updateHistory = static_cast(ownerServicePtr)->updateOS->getUpdateHistory(); struct statvfs vfstat; if (statvfs(purefs::dir::getRootDiskPath().c_str(), &vfstat) < 0) { diff --git a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp index ced922c960f87ff4ff915f05756d6f40012dfb77..fb507137b472f3516c17037cf62466a0aa3e5d20 100644 --- a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp +++ b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp @@ -54,10 +54,11 @@ UpdateMuditaOS::UpdateMuditaOS(ServiceDesktop *ownerService) : owner(ownerServic bootConfig.load(); } -updateos::UpdateError UpdateMuditaOS::setUpdateFile(fs::path updateFileToUse) +updateos::UpdateError UpdateMuditaOS::setUpdateFile(const std::filesystem::path &updatesOSPath, + fs::path updateFileToUse) { - updateFile = purefs::dir::getUpdatesOSPath() / updateFileToUse; - if (std::filesystem::exists(updateFile.c_str())) { + updateFile = updatesOSPath / updateFileToUse; + if (std::filesystem::exists(updateFile)) { versionInformation = UpdateMuditaOS::getVersionInfoFromFile(updateFile); if (mtar_open(&updateTar, updateFile.c_str(), "r") == MTAR_ESUCCESS) { totalBytes = utils::filesystem::filelength(updateTar.stream); @@ -86,7 +87,8 @@ updateos::UpdateError UpdateMuditaOS::runUpdate() updateRunStatus.fromVersion = bootConfig.to_json(); storeRunStatusInDB(); - updateos::UpdateError err = prepareTempDirForUpdate(); + updateos::UpdateError err = + prepareTempDirForUpdate(purefs::dir::getTemporaryPath(), purefs::dir::getUpdatesOSPath()); if (err != updateos::UpdateError::NoError) { return informError(err, "runUpdate can't prepare temp directory for update"); } @@ -490,49 +492,47 @@ const fs::path UpdateMuditaOS::getUpdateTmpChild(const fs::path &childPath) return updateTempDirectory / childPath; } -updateos::UpdateError UpdateMuditaOS::prepareTempDirForUpdate() +updateos::UpdateError UpdateMuditaOS::prepareTempDirForUpdate(const std::filesystem::path &temporaryPath, + const std::filesystem::path &updatesOSPath) { status = updateos::UpdateState::CreatingDirectories; - updateTempDirectory = purefs::dir::getTemporaryPath() / utils::filesystem::generateRandomId(updateos::prefix_len); + updateTempDirectory = temporaryPath / utils::filesystem::generateRandomId(updateos::prefix_len); informDebug("Temp dir for update %s", updateTempDirectory.c_str()); - const auto updatesOSPath = purefs::dir::getUpdatesOSPath(); - if (!std::filesystem::is_directory(updatesOSPath.c_str())) { - if (!std::filesystem::create_directory(updatesOSPath.c_str())) { + if (!std::filesystem::is_directory(updatesOSPath)) { + if (!std::filesystem::create_directory(updatesOSPath)) { return informError( updateos::UpdateError::CantCreateUpdatesDir, "%s can't create it", updatesOSPath.c_str()); } } - if (!std::filesystem::is_directory(purefs::dir::getUpdatesOSPath().c_str())) { - if (!std::filesystem::create_directory(purefs::dir::getUpdatesOSPath().c_str())) { - return informError(updateos::UpdateError::CantCreateUpdatesDir, - "%s can't create it %s", - purefs::dir::getUpdatesOSPath().c_str()); + if (!std::filesystem::is_directory(updatesOSPath)) { + if (!std::filesystem::create_directory(updatesOSPath)) { + return informError( + updateos::UpdateError::CantCreateUpdatesDir, "%s can't create it %s", updatesOSPath.c_str()); } else { - informDebug("prepareTempDirForUpdate %s created", purefs::dir::getUpdatesOSPath().c_str()); + informDebug("prepareTempDirForUpdate %s created", updatesOSPath.c_str()); } } else { - informDebug("prepareTempDirForUpdate %s exists", purefs::dir::getUpdatesOSPath().c_str()); + informDebug("prepareTempDirForUpdate %s exists", updatesOSPath.c_str()); } - if (!std::filesystem::is_directory(purefs::dir::getTemporaryPath().c_str())) { - informDebug("prepareTempDirForUpdate %s is not a directory", purefs::dir::getTemporaryPath().c_str()); - if (!std::filesystem::create_directory(purefs::dir::getTemporaryPath().c_str())) { - return informError(updateos::UpdateError::CantCreateTempDir, - "%s can't create it %s", - purefs::dir::getTemporaryPath().c_str()); + if (!std::filesystem::is_directory(temporaryPath)) { + informDebug("prepareTempDirForUpdate %s is not a directory", temporaryPath.c_str()); + if (!std::filesystem::create_directory(temporaryPath.c_str())) { + return informError( + updateos::UpdateError::CantCreateTempDir, "%s can't create it %s", temporaryPath.c_str()); } else { - informDebug("prepareTempDirForUpdate %s created", purefs::dir::getTemporaryPath().c_str()); + informDebug("prepareTempDirForUpdate %s created", temporaryPath.c_str()); } } else { - informDebug("prepareTempDirForUpdate %s exists", purefs::dir::getTemporaryPath().c_str()); + informDebug("prepareTempDirForUpdate %s exists", temporaryPath.c_str()); } if (std::filesystem::is_directory(updateTempDirectory.c_str())) { @@ -746,6 +746,9 @@ void UpdateMuditaOS::informUpdate(const updateos::UpdateState statusCode, const auto msgToSend = std::make_shared(updateos::UpdateMessageType::UpdateInform); messageText = std::string(readBuf.get()); msgToSend->updateStats = (updateos::UpdateStats)(*this); + if (owner == nullptr) { + return; + } sys::Bus::SendUnicast(msgToSend, app::name_desktop, owner); parserFSM::Context responseContext; diff --git a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.hpp b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.hpp index 892c3a5d85174ba9b1e78f73d35b70094afdb2c4..0eb21e5b95cccd78887b2363889cd2dbb99749b8 100644 --- a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.hpp +++ b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.hpp @@ -141,14 +141,15 @@ class UpdateMuditaOS : public updateos::UpdateStats UpdateMuditaOS(ServiceDesktop *ownerService); updateos::UpdateError runUpdate(); - updateos::UpdateError prepareTempDirForUpdate(); + updateos::UpdateError prepareTempDirForUpdate(const std::filesystem::path &temporaryPath, + const std::filesystem::path &updatesOSPath); updateos::UpdateError unpackUpdate(); updateos::UpdateError verifyChecksums(); updateos::UpdateError verifyVersion(); updateos::UpdateError updateBootloader(); updateos::UpdateError prepareRoot(); updateos::UpdateError updateBootJSON(); - updateos::UpdateError setUpdateFile(fs::path updateFileToUse); + updateos::UpdateError setUpdateFile(const std::filesystem::path &updatesOSPath, fs::path updateFileToUse); updateos::UpdateError cleanupAfterUpdate(); updateos::UpdateError updateUserData(); diff --git a/module-services/service-desktop/tests/unittest.cpp b/module-services/service-desktop/tests/unittest.cpp index b7769f47251b25ad2a7ed9a4658724cc45baba10..de620d6b46a6e97e7bfb58ee3f90183227b47e03 100644 --- a/module-services/service-desktop/tests/unittest.cpp +++ b/module-services/service-desktop/tests/unittest.cpp @@ -30,10 +30,11 @@ TEST_CASE("System Update Tests") { UpdateMuditaOS updateOS(nullptr); - updateos::UpdateError err = updateOS.prepareTempDirForUpdate(); + updateos::UpdateError err = updateOS.prepareTempDirForUpdate(std::filesystem::path{"user"} / "tmp", + std::filesystem::path{"sys"} / "updates"); REQUIRE(err == updateos::UpdateError::NoError); - updateOS.setUpdateFile("muditaos-unittest.tar"); + updateOS.setUpdateFile(std::filesystem::path{"sys"} / "updates", "muditaos-unittest.tar"); err = updateOS.unpackUpdate(); REQUIRE(err == updateos::UpdateError::NoError); @@ -42,16 +43,6 @@ TEST_CASE("System Update Tests") REQUIRE(err == updateos::UpdateError::NoError); } -TEST_CASE("Factory Reset Test") -{ - - std::string sysdir = purefs::dir::getRootDiskPath(); - sysdir += "/factory-test/sys"; - std::string factorydir = sysdir + "/factory"; - REQUIRE(FactoryReset::DeleteDirContent(sysdir) == true); - REQUIRE(FactoryReset::CopyDirContent(factorydir, sysdir) == true); -} - using namespace parserFSM; TEST_CASE("Parser Test") @@ -238,7 +229,7 @@ TEST_CASE("Context class test") { SECTION("Correct message") { - auto testMessage = R"({"endpoint":6, "method":1, "uuid":12345, "body":{"test":"test"}})"; + auto testMessage = R"({"endpoint":7, "method":1, "uuid":12345, "body":{"test":"test"}})"; std::string err; auto msgJson = json11::Json::parse(testMessage, err); REQUIRE(err.empty()); @@ -249,11 +240,11 @@ TEST_CASE("Context class test") REQUIRE(context.getUuid() == 12345); REQUIRE(context.getEndpoint() == EndpointType::contacts); REQUIRE(context.createSimpleResponse() == - R"(#000000061{"body": null, "endpoint": 6, "status": 200, "uuid": "12345"})"); + R"(#000000061{"body": null, "endpoint": 7, "status": 200, "uuid": "12345"})"); context.setResponseBody(context.getBody()); REQUIRE(context.createSimpleResponse() == - R"(#000000073{"body": {"test": "test"}, "endpoint": 6, "status": 200, "uuid": "12345"})"); + R"(#000000073{"body": {"test": "test"}, "endpoint": 7, "status": 200, "uuid": "12345"})"); } SECTION("Invalid message") { @@ -274,7 +265,7 @@ TEST_CASE("Endpoint Factory test") { SECTION("Proper endpoint") { - auto testMessage = R"({"endpoint":6, "method":1, "uuid":12345, "body":{"test":"test"}})"; + auto testMessage = R"({"endpoint":7, "method":1, "uuid":12345, "body":{"test":"test"}})"; std::string err; auto msgJson = json11::Json::parse(testMessage, err); REQUIRE(err.empty());