Run CrashAfterEachStep with subprocess 94/286394/4
authorilho kim <ilho159.kim@samsung.com>
Thu, 5 Jan 2023 06:45:45 +0000 (15:45 +0900)
committerilho kim <ilho159.kim@samsung.com>
Tue, 10 Jan 2023 02:26:19 +0000 (11:26 +0900)
CrashAfterEachStep function use RunFunc
to make the smoke test's main process lighter

Change-Id: Ib11a5a016a2ec4c2d5df3ba3dfb66f28c4111b22
Signed-off-by: ilho kim <ilho159.kim@samsung.com>
test/smoke_tests/common/smoke_utils.cc

index 3b265df..ba7fe5d 100644 (file)
@@ -618,14 +618,14 @@ void BackendInterface::TestRollbackAfterEachStep(int argc, const char* argv[],
         LOG(ERROR) << "StepFail not executed";
         return 1;
       }
-      if (!validator())
+      if (!validator()) {
+        LOG(ERROR) << "Fail to validate. index of StepFail : " << insert_idx;
         break;
+      }
       insert_idx++;
     } while (insert_idx < backend->StepCount());
-    if (insert_idx != backend->StepCount()) {
-      LOG(ERROR) << "Fail to validate";
+    if (insert_idx != backend->StepCount())
       return 1;
-    }
 
     return 0;
   }});
@@ -637,52 +637,75 @@ void BackendInterface::TestRollbackAfterEachStep(int argc, const char* argv[],
 
 void BackendInterface::CrashAfterEachStep(std::vector<std::string>* args,
     std::function<bool(int iter)> validator, PackageType type) const {
-  std::unique_ptr<const char*[]> argv(new const char*[args->size()]);
-  for (size_t i = 0; i < args->size(); ++i) {
-    argv[i] = args->at(i).c_str();
-  }
-  TestPkgmgrInstaller pkgmgr_installer;
-  auto query_interface = CreateQueryInterface();
-  auto pkgmgr =
-      ci::PkgMgrInterface::Create(args->size(), const_cast<char**>(argv.get()),
-                                  &pkgmgr_installer,
-                                  query_interface);
-  if (!pkgmgr) {
-    LOG(ERROR) << "Failed to initialize pkgmgr interface";
-    return;
-  }
-  auto backend = CreateFailExpectedInstaller(pkgmgr, 0);
-  ASSERT_EQ(backend->Run(), ci::AppInstaller::Result::ERROR);
-  int stepCount = backend->StepCount();
-
-  args->push_back("-idx");
-  args->push_back(std::to_string(stepCount));
-  int i;
-  std::string prefix = (type == PackageType::TPK) ? "tpk" : "wgt";
-  for (i = 0; i < stepCount; i++) {
-    ci::Subprocess backend_crash(
-        "/usr/bin/" + prefix + "-installer-ut/smoke-test-helper");
-    args->back() = std::to_string(i);
-    backend_crash.Run(*args);
-    ASSERT_NE(backend_crash.Wait(), 0);
-    if (!validator(i))
-      break;
-  }
-  ASSERT_EQ(stepCount, i);
-
-  args->push_back("-type_clean");
-  for (i = stepCount - 1; i >= 2; i--) {
-    ci::Subprocess backend_crash(
-        "/usr/bin/" + prefix + "-installer-ut/smoke-test-helper");
-    auto it = args->end();
-    it -= 2;
-    *it = std::to_string(i);
-    backend_crash.Run(*args);
-    ASSERT_NE(backend_crash.Wait(), 0);
-    if (!validator(i))
-      break;
-  }
-  ASSERT_EQ(i , 1);
+  ci::Subprocess backend_helper = CreateSubprocess();
+  bool result = backend_helper.RunFunc({[&]() {
+    std::unique_ptr<const char*[]> argv(new const char*[args->size()]);
+    for (size_t i = 0; i < args->size(); ++i) {
+      argv[i] = args->at(i).c_str();
+    }
+    TestPkgmgrInstaller pkgmgr_installer;
+    auto query_interface = CreateQueryInterface();
+    auto pkgmgr = ci::PkgMgrInterface::Create(args->size(),
+        const_cast<char**>(argv.get()), &pkgmgr_installer, query_interface);
+    if (!pkgmgr) {
+      LOG(ERROR) << "Failed to initialize pkgmgr interface";
+      return 1;
+    }
+    auto backend = CreateFailExpectedInstaller(pkgmgr, 0);
+    if (backend->Run() != ci::AppInstaller::Result::ERROR) {
+      LOG(ERROR) << "StepFail not executed";
+      return 1;
+    }
+    int stepCount = backend->StepCount();
+
+    args->push_back("-idx");
+    args->push_back(std::to_string(stepCount));
+    int insert_idx;
+    std::string prefix = (type == PackageType::TPK) ? "tpk" : "wgt";
+    for (insert_idx = 0; insert_idx < stepCount; insert_idx++) {
+      ci::Subprocess backend_crash(
+          "/usr/bin/" + prefix + "-installer-ut/smoke-test-helper");
+      args->back() = std::to_string(insert_idx);
+      backend_crash.Run(*args);
+      if (backend_crash.Wait() == 0) {
+        LOG(ERROR) << "Subprocess exit without crash";
+        return 1;
+      }
+      if (!validator(insert_idx)) {
+        LOG(ERROR) << "Fail to validate. index of StepCrash : " << insert_idx;
+        break;
+      }
+    }
+    if (stepCount != insert_idx)
+      return 1;
+
+    args->push_back("-type_clean");
+    for (insert_idx = stepCount - 1; insert_idx >= 2; insert_idx--) {
+      ci::Subprocess backend_crash(
+          "/usr/bin/" + prefix + "-installer-ut/smoke-test-helper");
+      auto it = args->end();
+      it -= 2;
+      *it = std::to_string(insert_idx);
+      backend_crash.Run(*args);
+      if (backend_crash.Wait() == 0) {
+        LOG(ERROR) << "Subprocess exit without crash";
+        return 1;
+      }
+      if (!validator(insert_idx)) {
+        LOG(ERROR) << "Fail to validate. index of StepCrash : " << insert_idx;
+        break;
+      }
+    }
+    if (insert_idx != 1)
+      return 1;
+
+    return 0;
+  }});
+
+  ASSERT_EQ(result, true);
+  int status = backend_helper.Wait();
+  ASSERT_NE(WIFEXITED(status), 0);
+  ASSERT_EQ(WEXITSTATUS(status), 0);
 }
 
 BackendInterface::CommandResult BackendInterface::RunInstallersWithPkgmgr(