From: Ilho Kim Date: Fri, 14 Oct 2022 01:15:28 +0000 (+0900) Subject: Add RunFunc to Subprocess for just executing function X-Git-Tag: accepted/tizen/7.0/unified/20230102.132424~13 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fappfw%2Fapp-installers.git;a=commitdiff_plain;h=459674d9783528f22004b28670fd231dc59ecf4a Add RunFunc to Subprocess for just executing function TestRollbackAfterEachStep function use RunFunc to make the smoke test's main process lighter Change-Id: I8ce453645d0581be69847f96b79578492838e07d Signed-off-by: Ilho Kim --- diff --git a/src/common/utils/subprocess.h b/src/common/utils/subprocess.h index 42527ee..79d7e3b 100644 --- a/src/common/utils/subprocess.h +++ b/src/common/utils/subprocess.h @@ -8,6 +8,8 @@ #include #include #include +#include +#include namespace common_installer { @@ -20,6 +22,23 @@ class Subprocess { return RunWithArgs(argv); } + template + bool RunFunc(std::function func, Args&&... args) { + if (started_) { + return false; + } + pid_ = fork(); + if (pid_ == 0) { + exit(func(std::forward(args)...)); + return false; + } else if (pid_ == -1) { + return false; + } else { + started_ = true; + return true; + } + } + bool RunWithArgs( const std::vector& args = std::vector()); int Wait(); diff --git a/test/smoke_tests/common/smoke_utils.cc b/test/smoke_tests/common/smoke_utils.cc index b0004d1..8bf9210 100644 --- a/test/smoke_tests/common/smoke_utils.cc +++ b/test/smoke_tests/common/smoke_utils.cc @@ -595,28 +595,43 @@ bool CheckSharedDataNonExistance(const std::string& pkgid, void BackendInterface::TestRollbackAfterEachStep(int argc, const char* argv[], std::function validator) const { - TestPkgmgrInstaller pkgmgr_installer; - std::shared_ptr query_interface = - CreateQueryInterface(); - auto pkgmgr = - ci::PkgMgrInterface::Create(argc, const_cast(argv), - &pkgmgr_installer, - query_interface); - if (!pkgmgr) { - LOG(ERROR) << "Failed to initialize pkgmgr interface"; - return; - } - AppInstallerPtr backend; - unsigned int insert_idx = 0; - do { - backend = CreateFailExpectedInstaller(pkgmgr, insert_idx); - LOG(DEBUG) << "StepFail is inserted at: " << insert_idx; - ASSERT_EQ(ci::AppInstaller::Result::ERROR, backend->Run()); - if (!validator()) - break; - insert_idx++; - } while (insert_idx < backend->StepCount()); - ASSERT_EQ(insert_idx, backend->StepCount()); + ci::Subprocess backend_helper = CreateSubprocess(); + ASSERT_EQ(backend_helper.RunFunc({[&]() -> int { + TestPkgmgrInstaller pkgmgr_installer; + std::shared_ptr query_interface = + CreateQueryInterface(); + auto pkgmgr = + ci::PkgMgrInterface::Create(argc, const_cast(argv), + &pkgmgr_installer, + query_interface); + if (!pkgmgr) { + LOG(ERROR) << "Failed to initialize pkgmgr interface"; + return 1; + } + AppInstallerPtr backend; + unsigned int insert_idx = 0; + do { + backend = CreateFailExpectedInstaller(pkgmgr, insert_idx); + LOG(DEBUG) << "StepFail is inserted at: " << insert_idx; + ci::AppInstaller::Result ret = backend->Run(); + if (ret != ci::AppInstaller::Result::ERROR) { + LOG(ERROR) << "StepFail not executed"; + return 1; + } + if (!validator()) + break; + insert_idx++; + } while (insert_idx < backend->StepCount()); + if (insert_idx != backend->StepCount()) { + LOG(ERROR) << "Fail to validate"; + return 1; + } + + return 0; + }}), true); + int status = backend_helper.Wait(); + ASSERT_NE(WIFEXITED(status), 0); + ASSERT_EQ(WEXITSTATUS(status), 0); } void BackendInterface::CrashAfterEachStep(std::vector* args,