Fix smoke utils to test unified installer 25/228325/9
authorSangyoon Jang <jeremy.jang@samsung.com>
Thu, 19 Mar 2020 08:47:38 +0000 (17:47 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Mon, 20 Apr 2020 03:32:36 +0000 (12:32 +0900)
Change-Id: I172bc7b450f590cce906ccfb0cda7f6409513e38
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/unit_tests/common/smoke_utils.cc
src/unit_tests/common/smoke_utils.h

index 57b65fe..ecfeafc 100644 (file)
@@ -666,6 +666,80 @@ void BackendInterface::CrashAfterEachStep(std::vector<std::string>* args,
   ASSERT_EQ(i , 1);
 }
 
+BackendInterface::CommandResult BackendInterface::RunInstallersWithPkgmgr(
+    ci::PkgMgrPtr pkgmgr) const {
+  std::list<AppInstallerPtr> installers;
+  for (int i = 0; i < pkgmgr->GetRequestInfoCount(); i++) {
+    AppInstallerPtr installer;
+    if (mode_ == RequestResult::FAIL && i == pkgmgr->GetRequestInfoCount() - 1)
+      installer = factory_->CreateFailExpectedInstaller(i, pkgmgr);
+    else
+      installer = factory_->CreateInstaller(i, pkgmgr);
+    if (!installer)
+      LOG(ERROR) << "Failed to create installer";
+    else
+      installers.emplace_back(std::move(installer));
+  }
+
+  // FIXME: I think we should not implement this logic here...
+  CommandResult result = CommandResult::OK;
+  std::list<AppInstallerPtr>::iterator it(installers.begin());
+  for (; it != installers.end(); ++it) {
+    result = (*it)->Process();
+    if (result != CommandResult::OK)
+      break;
+  }
+  if (it != installers.end() && result == CommandResult::ERROR) {
+    do {
+      CommandResult ret = (*it)->Undo();
+      if (ret != CommandResult::OK && ret != CommandResult::ERROR)
+        result = CommandResult::UNDO_ERROR;
+    } while (it-- != installers.begin());
+  } else {
+    --it;
+    do {
+      if ((*it)->Clean() != CommandResult::OK)
+        result = CommandResult::CLEANUP_ERROR;
+    } while (it-- != installers.begin());
+  }
+  return result;
+}
+
+BackendInterface::CommandResult BackendInterface::CallBackendWithRunner(
+    int argc, const char* argv[]) const {
+  TestPkgmgrInstaller pkgmgr_installer;
+  auto pkgmgr = ci::PkgMgrInterface::Create(argc, const_cast<char**>(argv),
+      &pkgmgr_installer);
+  if (!pkgmgr) {
+    LOG(ERROR) << "Failed to initialize pkgmgr interface";
+    return BackendInterface::CommandResult::UNKNOWN;
+  }
+  return RunInstallersWithPkgmgr(pkgmgr);
+}
+
+BackendInterface::CommandResult BackendInterface::Install(
+    const std::vector<bf::path>& paths) const {
+  std::vector<const char*> argv;
+  argv.emplace_back("");
+  argv.emplace_back("-i");
+  for (const auto& p : paths)
+    argv.emplace_back(p.string().c_str());
+  return CallBackendWithRunner(argv.size(), argv.data());
+}
+
+BackendInterface::CommandResult BackendInterface::InstallSuccess(
+    const std::vector<bf::path>& paths) const {
+  RequestResult tmp_mode = mode_;
+  RequestResult &original_mode = const_cast<RequestResult&>(mode_);
+  original_mode = RequestResult::NORMAL;
+  if (Install(paths) != BackendInterface::CommandResult::OK) {
+    LOG(ERROR) << "Failed to install application. Cannot update";
+    return BackendInterface::CommandResult::UNKNOWN;
+  }
+  original_mode = tmp_mode;
+  return BackendInterface::CommandResult::OK;
+}
+
 BackendInterface::CommandResult BackendInterface::RunInstallerWithPkgrmgr(
     ci::PkgMgrPtr pkgmgr) const {
   std::unique_ptr<ci::AppInstaller> installer;
index d02f459..ca6107d 100644 (file)
@@ -224,12 +224,26 @@ bool CheckSharedDataNonExistance(const std::string& pkgid,
 
 bool TouchFile(const boost::filesystem::path& path);
 
+class SmokeInstallerFactory {
+ public:
+  using AppInstallerPtr = std::unique_ptr<common_installer::AppInstaller>;
+  virtual AppInstallerPtr CreateInstaller(
+      int idx, common_installer::PkgMgrPtr pkgmgr) const = 0;
+  virtual AppInstallerPtr CreateFailExpectedInstaller(
+      int idx, common_installer::PkgMgrPtr pkgmgr, int fail_at = -1) const = 0;
+};
+
 class BackendInterface {
  public:
   using CommandResult = common_installer::AppInstaller::Result;
+  using SmokeInstallerFactoryPtr = std::unique_ptr<SmokeInstallerFactory>;
   explicit BackendInterface(std::string uid,
       RequestResult mode = RequestResult::NORMAL)
       : uid_str_(uid), mode_(mode) {}
+  explicit BackendInterface(SmokeInstallerFactoryPtr factory,
+      std::string uid,
+      RequestResult mode = RequestResult::NORMAL)
+      : factory_(std::move(factory)), uid_str_(uid), mode_(mode) {}
   virtual ~BackendInterface() {}
 
   void TestRollbackAfterEachStep(int argc, const char* argv[],
@@ -237,6 +251,8 @@ class BackendInterface {
   void CrashAfterEachStep(std::vector<std::string>* args,
                           std::function<bool(int iter)> validator,
                           PackageType type) const;
+  CommandResult Install(const std::vector<bf::path>& paths) const;
+  CommandResult InstallSuccess(const std::vector<bf::path>& paths) const;
   CommandResult Install(const boost::filesystem::path& path) const;
   CommandResult InstallPreload(const boost::filesystem::path& path) const;
   CommandResult InstallWithStorage(const boost::filesystem::path& path,
@@ -266,9 +282,11 @@ class BackendInterface {
 
  protected:
   CommandResult CallBackend(int argc, const char* argv[]) const;
+  CommandResult CallBackendWithRunner(int argc, const char* argv[]) const;
   using AppQueryInterfacePtr =
       std::unique_ptr<common_installer::AppQueryInterface>;
   using AppInstallerPtr = std::unique_ptr<common_installer::AppInstaller>;
+  SmokeInstallerFactoryPtr factory_;
   std::string uid_str_;
   RequestResult mode_;
 
@@ -280,6 +298,8 @@ class BackendInterface {
       common_installer::PkgMgrPtr pkgmgr) const = 0;
   virtual AppInstallerPtr CreateFailExpectedInstaller(
       common_installer::PkgMgrPtr pkgmgr, int fail_at = -1) const = 0;
+  CommandResult RunInstallersWithPkgmgr(
+      common_installer::PkgMgrPtr pkgmgr) const;
 };
 
 class SmokeTestHelperRunner {