[SmokeTest] Add request mode choice 98/119898/7
authorDamian Pietruchowski <d.pietruchow@samsung.com>
Mon, 20 Mar 2017 10:05:33 +0000 (11:05 +0100)
committerjongmyeong ko <jongmyeong.ko@samsung.com>
Fri, 26 May 2017 04:18:05 +0000 (04:18 +0000)
Smoke tests can be run with normal or global user.
Normal user is created in environment SetUp() and
is deleted in TearDown() method. Normal user name
is "smokeuser".

You can run tests by normal user with flags:
-u or --request_mode=user
You can run tests by global user with flags:
-g or --request_mode=global

Without above flags tests are run with global user
by default

Change-Id: I776e26e27ffe670328293fb01bd0dd35549cfe11
Signed-off-by: Damian Pietruchowski <d.pietruchow@samsung.com>
CMakeLists.txt
packaging/tpk-backend.spec
src/unit_tests/CMakeLists.txt
src/unit_tests/smoke_test.cc
src/unit_tests/smoke_utils.cc
src/unit_tests/smoke_utils.h

index 3c827629c9598313eff89a18e18e94142c6a8f85..06192ca92e4f02aa4fa2f23f2f9be0027794223b 100644 (file)
@@ -49,6 +49,7 @@ PKG_CHECK_MODULES(MANIFEST_PARSER_DEPS REQUIRED manifest-parser)
 PKG_CHECK_MODULES(VCONF_DEPS REQUIRED vconf)
 PKG_CHECK_MODULES(CAPI_SYSTEM_INFO_DEPS REQUIRED capi-system-info)
 PKG_CHECK_MODULES(TTRACE_DEPS REQUIRED ttrace)
+PKG_CHECK_MODULES(GUM_DEPS REQUIRED libgum)
 
 FIND_PACKAGE(Boost REQUIRED COMPONENTS system filesystem regex program_options)
 FIND_PACKAGE(GTest REQUIRED)
index 6a00618ff9e52319bc57e8c831fecf41ad4289e8..0015d6e551a29298ba28372f7340e65f7a1693b0 100644 (file)
@@ -19,6 +19,7 @@ BuildRequires:  pkgconfig(tpk-manifest-handlers)
 BuildRequires:  pkgconfig(vconf)
 BuildRequires:  pkgconfig(capi-system-info)
 BuildRequires:  pkgconfig(ttrace)
+BuildRequires:  pkgconfig(libgum)
 
 %description
 Backend for tizen package files
index f0bc7965cc873dfe58b0edd5f8b74bae5e58e7c2..10b1cae443ab6b58b2aac97176065e5e0e37b840 100644 (file)
@@ -29,6 +29,7 @@ INSTALL(DIRECTORY test_samples/ DESTINATION ${SHAREDIR}/${DESTINATION_DIR}/test_
 APPLY_PKG_CONFIG(${TARGET_SMOKE_TEST} PUBLIC
   Boost
   GTEST
+  GUM_DEPS
 )
 APPLY_PKG_CONFIG(${TARGET_MANIFEST_TEST} PUBLIC
   Boost
index 09bb3002d06f2a3439680906e71f97ce600d36a1..078402840922352b26c882bb757df127d286e12e 100644 (file)
@@ -14,21 +14,33 @@ namespace common_installer {
 
 class SmokeEnvironment : public testing::Environment {
  public:
-  explicit SmokeEnvironment(uid_t uid) : uid_(uid) {
+  explicit SmokeEnvironment(ci::RequestMode mode) : request_mode_(mode) {
   }
   void SetUp() override {
-    backups_ = SetupBackupDirectories(uid_);
+    if (request_mode_ == ci::RequestMode::USER)
+      ASSERT_TRUE(AddTestUser(kNormalUserName));
+    else {
+      kTestUserId = kGlobalUserUid;
+      kTestGroupId = kGlobalUserGid;
+      kTestUserIdStr = std::to_string(kTestUserId);
+    }
+    backups_ = SetupBackupDirectories();
     for (auto& path : backups_)
       BackupPath(path);
   }
   void TearDown() override {
-    UninstallAllSmokeApps(uid_);
+    ASSERT_TRUE(request_mode_ == ci::RequestMode::GLOBAL ||
+                (request_mode_ == ci::RequestMode::USER &&
+                kGlobalUserUid != kTestUserId));
+    UninstallAllSmokeApps(request_mode_);
     for (auto& path : backups_)
       RestorePath(path);
+    if (request_mode_ == ci::RequestMode::USER)
+      ASSERT_TRUE(DeleteTestUser(kNormalUserName));
   }
 
  private:
-  uid_t uid_;
+  ci::RequestMode request_mode_;
   std::vector<bf::path> backups_;
 };
 
@@ -525,8 +537,9 @@ TEST_F(SmokeTest, MountUpdateMode_Rollback) {
 }  // namespace common_installer
 
 int main(int argc,  char** argv) {
+  ci::RequestMode request_mode = ParseRequestMode(argc, argv);
   testing::InitGoogleTest(&argc, argv);
   testing::AddGlobalTestEnvironment(
-      new common_installer::SmokeEnvironment(kGlobalUserUid));
+      new common_installer::SmokeEnvironment(request_mode));
   return RUN_ALL_TESTS();
 }
index de965c12af4578ce087fb520d063fdba5e702f76..c0ebd1655940ab350e922651d8b3e9303cecc403 100644 (file)
@@ -6,11 +6,13 @@
 #include <boost/range/iterator_range.hpp>
 #include <boost/system/error_code.hpp>
 
-
 #include <common/step/configuration/step_fail.h>
 #include <common/utils/subprocess.h>
 #include <common/utils/user_util.h>
 #include <common/request.h>
+#include <gum/gum-user.h>
+#include <gum/gum-user-service.h>
+#include <gum/common/gum-user-types.h>
 
 #include <gtest/gtest.h>
 #include <gtest/gtest-death-test.h>
 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
 const uid_t kGlobalUserGid = tzplatform_getgid(TZ_SYS_GLOBALAPP_USER);
 const uid_t kDefaultUserUid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
-// TODO(s89.jang): Test local case also
-const uid_t kTestUserId = kGlobalUserUid;
-const gid_t kTestGroupId = kGlobalUserGid;
+uid_t kTestUserId = kGlobalUserUid;
+gid_t kTestGroupId = kGlobalUserGid;
+std::string kTestUserIdStr = std::to_string(kTestUserId);
+const char kNormalUserName[] = "smokeuser";
 const char kSystemShareGroupName[] = "system_share";
-const std::string& kTestUserIdStr = std::to_string(kTestUserId);
 const std::string& kDefaultUserIdStr = std::to_string(kDefaultUserUid);
 const char kLegacyExtImageDir[] = "legacy_extimage_dir";
 const char kMigrateTestDBName[] = "app2sd_migrate.db";
@@ -81,6 +83,28 @@ const char* rwDirectories[] = {
   "shared/trusted"
 };
 
+bool AddUser(const char *user_name) {
+  GumUser* user = nullptr;
+  user = gum_user_create_sync(FALSE);
+  if (user == nullptr)
+    LOG(WARNING) << "Failed to create gum user! (user name: "
+                 << user_name << ")";
+  g_object_set(G_OBJECT(user), "username", user_name, "usertype",
+      GUM_USERTYPE_NORMAL, NULL);
+  gboolean rval = FALSE;
+  rval = gum_user_add_sync(user);
+  g_object_unref(user);
+  return rval;
+}
+
+bool DeleteUser(const char *user_name, bool rem_home_dir) {
+  bool rval = FALSE;
+  GumUser* guser = gum_user_get_by_name_sync(user_name, FALSE);
+  if(guser)
+    rval = gum_user_delete_sync(guser, rem_home_dir);
+  return rval;
+}
+
 }
 
 
@@ -92,6 +116,83 @@ bool TouchFile(const bf::path& path) {
   return true;
 }
 
+ci::RequestMode ParseRequestMode(int argc,  char** argv) {
+  bo::options_description desc("Available options");
+  desc.add_options()
+      ("request-mode", bo::value<std::string>(), "set request mode")
+      ("global-request,g", "set request mode to global")
+      ("user-request,u", "set request mode to user");
+
+  bo::variables_map vm;
+  bo::store(bo::command_line_parser(argc, argv).
+      options(desc).allow_unregistered().run(), vm);
+  bo::notify(vm);
+
+  if (vm.count("global-request")) {
+    std::cout << "Request mode was set to global." << std::endl;
+    return ci::RequestMode::GLOBAL;
+  }
+  if (vm.count("user-request")) {
+    std::cout << "Request mode was set to user." << std::endl;
+    return ci::RequestMode::USER;
+  }
+  try {
+    if (vm.count("request-mode")) {
+      if (vm["request-mode"].as<std::string>() == "global") {
+        std::cout << "Request mode was set to global." << std::endl;
+        return ci::RequestMode::GLOBAL;
+      }
+      if (vm["request-mode"].as<std::string>() == "user") {
+        std::cout << "Request mode was set to user." << std::endl;
+        return ci::RequestMode::USER;
+      }
+      std::cout << "Cannot set request mode to "
+                << vm["request-mode"].as<std::string>() << std::endl;
+    }
+  }
+  catch (const boost::bad_any_cast &exception) {
+    LOG(ERROR) << exception.what();
+  }
+  std::cout << "Request mode was set to global." << std::endl;
+  return ci::RequestMode::GLOBAL;
+}
+
+bool AddTestUser(const char *user_name) {
+  std::cout << "Adding test user: " << user_name << std::endl;
+  AddUser(user_name);
+  if (boost::optional<uid_t> uid = ci::GetUidByUserName(user_name)) {
+    kTestUserId = *uid;
+    kTestUserIdStr = std::to_string(kTestUserId);
+    std::cout << "User created properly: uid=" << *uid;
+    if (boost::optional<gid_t> gid = ci::GetGidByUid(*uid)) {
+      kTestGroupId = *gid;
+      std::cout << " gid=" << *gid;
+    }
+    std::cout << std::endl;
+    return true;
+  }
+  LOG(ERROR) << "Adding test user failed";
+  return false;
+}
+
+bool DeleteTestUser(const char *user_name) {
+  std::cout << "Deleting test user: " << user_name << std::endl;
+  uid_t test_uid;
+  if (boost::optional<uid_t> uid = ci::GetUidByUserName(user_name))
+    test_uid = *uid;
+  else
+    // User do not exist
+    return true;
+  DeleteUser(user_name, true);
+  if (boost::optional<uid_t> uid = ci::GetUidByUserName(user_name));
+  else {
+    std::cout << "User deleted properly: user_name=" << user_name
+              << " uid=" << test_uid << std::endl;
+    return true;
+  }
+  LOG(ERROR) << "Deleting test user failed";
+  return false;
+}
 
 void RemoveAllRecoveryFiles() {
   bf::path root_path = ci::GetRootAppPath(false,
@@ -563,11 +664,11 @@ void RestorePath(const bf::path& path) {
   }
 }
 
-std::vector<bf::path> SetupBackupDirectories(uid_t uid) {
+std::vector<bf::path> SetupBackupDirectories() {
   std::vector<bf::path> entries;
   bf::path db_dir = bf::path(tzplatform_getenv(TZ_SYS_DB));
-  if (uid != kGlobalUserUid)
-    db_dir = db_dir / "user" / std::to_string(uid);
+  if (kTestUserId != kGlobalUserUid)
+    db_dir = db_dir / "user" / std::to_string(kTestUserId);
   for (auto e : kDBEntries) {
     bf::path path = db_dir / e;
     entries.emplace_back(path);
@@ -579,7 +680,7 @@ std::vector<bf::path> SetupBackupDirectories(uid_t uid) {
     entries.emplace_back(kPreloadIcons);
   }
 
-  if (uid == kGlobalUserUid) {
+  if (kTestUserId == kGlobalUserUid) {
     entries.emplace_back(kSkelDir);
     entries.emplace_back(kGlobalManifestDir);
     ci::UserList list = ci::GetUserList();
@@ -588,13 +689,13 @@ std::vector<bf::path> SetupBackupDirectories(uid_t uid) {
       entries.emplace_back(apps);
     }
   } else {
-    tzplatform_set_user(uid);
+    tzplatform_set_user(kTestUserId);
     bf::path approot = tzplatform_getenv(TZ_USER_APPROOT);
     tzplatform_reset_user();
     entries.emplace_back(approot);
   }
 
-  bf::path apps_rw = ci::GetRootAppPath(false, uid);
+  bf::path apps_rw = ci::GetRootAppPath(false, kTestUserId);
   entries.emplace_back(apps_rw);
 
   return entries;
@@ -616,12 +717,12 @@ void UninstallAllAppsInDirectory(bf::path dir, bool is_readonly) {
   }
 }
 
-void UninstallAllSmokeApps(uid_t uid) {
-  if (getuid() == 0) {
+void UninstallAllSmokeApps(ci::RequestMode request_mode) {
+  if (getuid() == 0 & request_mode == ci::RequestMode::GLOBAL) {
     bf::path root_path = kPreloadApps;
     UninstallAllAppsInDirectory(root_path, true);
   }
-  bf::path apps_rw = ci::GetRootAppPath(false, uid);
+  bf::path apps_rw = ci::GetRootAppPath(false, kTestUserId);
   UninstallAllAppsInDirectory(apps_rw, false);
 }
 
index 6a29aefd801b5c53365f2c7e7f7a7a28f18df07e..df419718ce0e448858e406f4cfc0bb8fd7f16741 100644 (file)
@@ -6,6 +6,7 @@
 #define UNIT_TESTS_SMOKE_UTILS_H_
 
 #include <boost/filesystem/path.hpp>
+#include <boost/program_options.hpp>
 
 #include <common/pkgmgr_interface.h>
 #include <common/tzip_interface.h>
 namespace bf = boost::filesystem;
 namespace bs = boost::system;
 namespace ci = common_installer;
+namespace bo = boost::program_options;
 
 extern const uid_t kGlobalUserUid;
 extern const uid_t kGlobalUserGid;
 extern const uid_t kDefaultUserUid;
 // TODO(s89.jang): Test local case also
-extern const uid_t kTestUserId;
-extern const gid_t kTestGroupId;
+extern uid_t kTestUserId;
+extern gid_t kTestGroupId;
+extern std::string kTestUserIdStr;
+extern const char kNormalUserName[];
 extern const char kSystemShareGroupName[];
-extern const std::string& kTestUserIdStr;
 extern const std::string& kDefaultUserIdStr;
 extern const char kLegacyExtImageDir[];
 extern const char kMigrateTestDBName[];
@@ -100,6 +103,12 @@ class TestPkgmgrInstaller : public ci::PkgmgrInstallerInterface {
   }
 };
 
+ci::RequestMode ParseRequestMode(int argc,  char** argv);
+
+bool AddTestUser(const char *user_name);
+
+bool DeleteTestUser(const char *user_name);
+
 bool TouchFile(const bf::path& path);
 
 void RemoveAllRecoveryFiles();
@@ -203,10 +212,10 @@ void BackupPath(const bf::path& path);
 
 void RestorePath(const bf::path& path);
 
-std::vector<bf::path> SetupBackupDirectories(uid_t uid);
+std::vector<bf::path> SetupBackupDirectories();
 
 void UninstallAllAppsInDirectory(bf::path dir, bool is_readonly);
 
-void UninstallAllSmokeApps(uid_t uid);
+void UninstallAllSmokeApps(ci::RequestMode request_mode);
 
 #endif  // UNIT_TESTS_SMOKE_UTILS_H_