Add RunFunc to Subprocess for just executing function 66/283266/4
authorIlho Kim <ilho159.kim@samsung.com>
Fri, 14 Oct 2022 01:15:28 +0000 (10:15 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Fri, 21 Oct 2022 07:22:06 +0000 (16:22 +0900)
TestRollbackAfterEachStep function use RunFunc
to make the smoke test's main process lighter

Change-Id: I8ce453645d0581be69847f96b79578492838e07d
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/common/utils/subprocess.h
test/smoke_tests/common/smoke_utils.cc

index 42527ee..79d7e3b 100644 (file)
@@ -8,6 +8,8 @@
 #include <string>
 #include <utility>
 #include <vector>
+#include <functional>
+#include <unistd.h>
 
 namespace common_installer {
 
@@ -20,6 +22,23 @@ class Subprocess {
     return RunWithArgs(argv);
   }
 
+  template <typename... Args>
+  bool RunFunc(std::function<int(Args...)> func, Args&&... args) {
+    if (started_) {
+      return false;
+    }
+    pid_ = fork();
+    if (pid_ == 0) {
+      exit(func(std::forward<Args>(args)...));
+      return false;
+    } else if (pid_ == -1) {
+      return false;
+    } else {
+      started_ = true;
+      return true;
+    }
+  }
+
   bool RunWithArgs(
       const std::vector<std::string>& args = std::vector<std::string>());
   int Wait();
index b0004d1..8bf9210 100644 (file)
@@ -595,28 +595,43 @@ bool CheckSharedDataNonExistance(const std::string& pkgid,
 
 void BackendInterface::TestRollbackAfterEachStep(int argc, const char* argv[],
     std::function<bool()> validator) const {
-  TestPkgmgrInstaller pkgmgr_installer;
-  std::shared_ptr<ci::AppQueryInterface> query_interface =
-      CreateQueryInterface();
-  auto pkgmgr =
-      ci::PkgMgrInterface::Create(argc, const_cast<char**>(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<ci::AppQueryInterface> query_interface =
+        CreateQueryInterface();
+    auto pkgmgr =
+        ci::PkgMgrInterface::Create(argc, const_cast<char**>(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<std::string>* args,