#include <string>
#include <utility>
#include <vector>
+#include <functional>
+#include <unistd.h>
namespace common_installer {
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();
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,