Add smoke-test
authorSangyoon Jang <jeremy.jang@samsung.com>
Mon, 9 Jul 2018 05:09:13 +0000 (14:09 +0900)
committer장상윤/Tizen Platform Lab(SR)/Engineer/삼성전자 <jeremy.jang@samsung.com>
Mon, 9 Jul 2018 08:06:58 +0000 (17:06 +0900)
Change-Id: If8ae6a1ea84ccb3601b38e7351c17fed327423c0
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
CMakeLists.txt
src/unit_tests/CMakeLists.txt
src/unit_tests/smoke_test.cc [new file with mode: 0644]

index 563b96553cc275635e3d04a356980bd9eb724561..ab64b4092cd6274f1fc812d794edb417cd2972fe 100644 (file)
@@ -37,7 +37,7 @@ PKG_CHECK_MODULES(PKGMGR_DEPS REQUIRED pkgmgr)
 PKG_CHECK_MODULES(PKGMGR_INFO_DEPS REQUIRED pkgmgr-info)
 PKG_CHECK_MODULES(SQLITE_DEPS REQUIRED sqlite3)
 
-FIND_PACKAGE(Boost REQUIRED COMPONENTS program_options)
+FIND_PACKAGE(Boost REQUIRED COMPONENTS filesystem program_options system)
 FIND_PACKAGE(GTest REQUIRED)
 
 ADD_SUBDIRECTORY(src)
index b9fd55e10c6b1c24c987f8d3db297b8abfe15819..519c8ac79bd2668dd241042532037acc7eace534 100644 (file)
@@ -1,10 +1,15 @@
 AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/sql_test/ SQL_TEST_SRCS)
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/ SMOKE_TEST_SRCS)
+
 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../)
 
 SET(TARGET_SQL_TEST "sql-test")
+SET(TARGET_SMOKE_TEST "smoke-test")
 
 ADD_EXECUTABLE(${TARGET_SQL_TEST} ${SQL_TEST_SRCS})
 
+ADD_EXECUTABLE(${TARGET_SMOKE_TEST} ${SMOKE_TEST_SRCS})
+
 APPLY_PKG_CONFIG(${TARGET_SQL_TEST} PUBLIC
   DLOG_DEPS
   GLIB_DEPS
@@ -12,8 +17,16 @@ APPLY_PKG_CONFIG(${TARGET_SQL_TEST} PUBLIC
   GTEST
 )
 
+APPLY_PKG_CONFIG(${TARGET_SMOKE_TEST} PUBLIC
+  Boost
+  DLOG_DEPS
+  GTEST
+)
+
 TARGET_LINK_LIBRARIES(${TARGET_SQL_TEST} PUBLIC ${TARGET_LIB_COMMON} pthread)
 
+TARGET_LINK_LIBRARIES(${TARGET_SMOKE_TEST} PUBLIC ${TARGET_LIB_COMMON} pthread)
+
 ## Add a custom target, test database for sql-test
 SET(TESTDB "sql-test.db")
 SET(TARGET_TESTDB "sql-test-db")
@@ -28,3 +41,4 @@ ADD_DEPENDENCIES(${TARGET_SQL_TEST} ${TARGET_TESTDB})
 
 INSTALL(TARGETS ${TARGET_SQL_TEST} DESTINATION bin/capmgr-ut/)
 INSTALL(FILES ${TESTDB} DESTINATION /opt/share/capmgr-ut/)
+INSTALL(TARGETS ${TARGET_SMOKE_TEST} DESTINATION bin/capmgr-ut/)
diff --git a/src/unit_tests/smoke_test.cc b/src/unit_tests/smoke_test.cc
new file mode 100644 (file)
index 0000000..01f116a
--- /dev/null
@@ -0,0 +1,150 @@
+// Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#include <gtest/gtest.h>
+
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
+#include <boost/system/error_code.hpp>
+
+#include <string>
+
+#include "common/capability.h"
+#include "common/db_manager.h"
+#include "common/remote_device.h"
+#include "common/utils/logging.h"
+
+namespace bf = boost::filesystem;
+namespace bs = boost::system;
+
+namespace {
+
+const char kDBPath[] = "/run/capmgr/capmgr.db";
+const char kJournalPath[] = "/run/capmgr/capmgr.db-journal";
+const ::testing::Environment* env = nullptr;
+
+static boost::filesystem::path GetTrashPath(
+    const boost::filesystem::path& path) {
+  return path.string() + ".trash";
+}
+
+bool BackupPath(const bf::path& path) {
+  bf::path trash_path = GetTrashPath(path);
+  if (bf::exists(trash_path)) {
+    LOG(ERROR) << trash_path << " exists. Please remove "
+               << trash_path << " manually!";
+    return false;
+  }
+  bf::path backup_path = path.string() + ".bck";
+  std::cout << "Backup path: " << path << " to " << backup_path << std::endl;
+  bs::error_code error;
+  bf::remove_all(backup_path, error);
+  if (error)
+    LOG(ERROR) << "Remove failed: " << backup_path
+      << " (" << error.message() << ")";
+  if (bf::exists(path)) {
+    bf::rename(path, backup_path, error);
+    if (error) {
+      LOG(ERROR) << "Failed to setup test environment. Does some previous"
+                 << " test crashed? Path: "
+                 << backup_path << " should not exist.";
+      return false;
+    }
+    assert(!error);
+  }
+  return true;
+}
+
+bool RestorePath(const bf::path& path) {
+  bf::path backup_path = path.string() + ".bck";
+  std::cout << "Restore path: " << path << " from " << backup_path << std::endl;
+  bs::error_code error;
+  bf::remove_all(path, error);
+  if (error) {
+    bf::path trash_path = GetTrashPath(path);
+    LOG(ERROR) << "Remove failed: " << path << " (" << error.message() << ")";
+    std::cout << "Moving " << path << " to " << trash_path << std::endl;
+    bf::rename(path, trash_path, error);
+    if (error)
+      LOG(ERROR) << "Failed to move " << path << " to " << trash_path
+                 << " (" << error.message() << ")";
+    else
+      LOG(ERROR) << trash_path << " should be removed manually!";
+  }
+  if (bf::exists(backup_path)) {
+    bf::rename(backup_path, path, error);
+    if (error) {
+      LOG(ERROR) << "Failed to restore backup path: " << backup_path
+                 << " (" << error.message() << ")";
+      return false;
+    }
+  }
+  return true;
+}
+
+}  // namespace
+
+namespace capmgr {
+
+class SmokeEnvironment : public ::testing::Environment {
+ public:
+  void SetUp() override {
+    // backup original db
+    if (!BackupPath(kDBPath))
+      LOG(ERROR) << "Failed to backup db";
+    if (!BackupPath(kJournalPath))
+      LOG(ERROR) << "Failed to backup db";
+  }
+
+  void TearDown() override {
+    if (!RestorePath(kDBPath))
+      LOG(ERROR) << "Failed to restore db";
+    if (!RestorePath(kJournalPath))
+      LOG(ERROR) << "Failed to restore db";
+  }
+};
+
+class SmokeTest : public ::testing::Test {
+ protected:
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+};
+
+TEST_F(SmokeTest, InsertDeviceTest) {
+  RemoteDevice dev("insertdev", "addr1234", "tm1", "TM1", "5.0", "mobile",
+      "5.0");
+  ASSERT_TRUE(DBManager::InsertDevice(dev));
+}
+
+TEST_F(SmokeTest, DeleteDeviceTest) {
+  RemoteDevice dev("deletedev", "addr1234", "tm1", "TM1", "5.0", "mobile",
+      "5.0");
+  ASSERT_TRUE(DBManager::InsertDevice(dev));
+  ASSERT_TRUE(DBManager::DeleteDevice(dev));
+}
+
+TEST_F(SmokeTest, InsertCapTest) {
+  RemoteDevice dev("insertcapdev", "addr1234", "tm1", "TM1", "5.0", "mobile",
+      "5.0");
+  ASSERT_TRUE(DBManager::InsertDevice(dev));
+  Capability cap("operation", "uri", "mime", "appid", "pkgid");
+  ASSERT_TRUE(DBManager::InsertCapability("insertcapdev", cap));
+}
+
+TEST_F(SmokeTest, DeleteCapTest) {
+  RemoteDevice dev("deletecapdev", "addr1234", "tm1", "TM1", "5.0", "mobile",
+      "5.0");
+  ASSERT_TRUE(DBManager::InsertDevice(dev));
+  Capability cap("operation", "uri", "mime", "appid", "pkgid");
+  ASSERT_TRUE(DBManager::InsertCapability("deletecapdev", cap));
+  ASSERT_TRUE(DBManager::DeleteCapability("deletecapdev", cap));
+}
+
+}  // namespace capmgr
+
+int main(int argc, char* argv[]) {
+  ::testing::InitGoogleTest(&argc, argv);
+  ::env = testing::AddGlobalTestEnvironment(new capmgr::SmokeEnvironment);
+  return RUN_ALL_TESTS();
+}