Refactor recovery test tool 72/218972/4
authorIlho Kim <ilho159.kim@samsung.com>
Fri, 29 Nov 2019 06:59:33 +0000 (15:59 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Tue, 17 Dec 2019 01:31:11 +0000 (10:31 +0900)
- Use enum class, not typedef
- Use boost program options for parsing argument

Change-Id: I1d129ab935e65b3eda701135e732e45eba55180f
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/unit_tests/recovery_test.cc

index b8647beb1fcc5eaff3029d5230232dd5e57afc10..2ad87544f349cabc13a1e2bc24528e5db6145dc7 100644 (file)
@@ -2,36 +2,44 @@
 // Use of this source code is governed by an apache-2.0 license that can be
 // found in the LICENSE file.
 
+#include <boost/exception/diagnostic_information.hpp>
+#include <boost/program_options.hpp>
+
 #include <common/utils/subprocess.h>
 #include <unit_tests/common/smoke_utils.h>
 
 #include <gtest/gtest.h>
 
-#include <getopt.h>
-
+#include <map>
 #include <string>
 #include <vector>
 
 #include "unit_tests/smoke_utils.h"
 
 namespace bf = boost::filesystem;
+namespace bpo = boost::program_options;
 namespace ci = common_installer;
 
-typedef enum {
-  UNKNOWN_REQ = -1,
-  INSTALL_REQ = 0,
-  UPDATE_REQ = 1,
-  DELTA_REQ = 2,
-  MOUNT_INSTALL_REQ = 3,
-  MOUNT_UPDATE_REQ = 4,
-} REQ_TYPE;
-
-const char* install_req_filter[] = {
-  "--gtest_filter=SmokeTest.RecoveryMode_Tpk_Installation",
-  "--gtest_filter=SmokeTest.RecoveryMode_Tpk_Update",
-  "--gtest_filter=SmokeTest.RecoveryMode_ForDelta",
-  "--gtest_filter=SmokeTest.RecoveryMode_ForMountInstall",
-  "--gtest_filter=SmokeTest.RecoveryMode_ForMountUpdate"
+enum class ReqType : int {
+  UNKNOWN_REQ,
+  INSTALL_REQ,
+  UPDATE_REQ,
+  DELTA_REQ,
+  MOUNT_INSTALL_REQ,
+  MOUNT_UPDATE_REQ,
+};
+
+std::map<ReqType, std::string> install_req_filter = {
+  { ReqType::INSTALL_REQ,
+      "--gtest_filter=SmokeTest.RecoveryMode_Tpk_Installation" },
+  { ReqType::UPDATE_REQ,
+      "--gtest_filter=SmokeTest.RecoveryMode_Tpk_Update" },
+  { ReqType::DELTA_REQ,
+      "--gtest_filter=SmokeTest.RecoveryMode_ForDelta" },
+  { ReqType::MOUNT_INSTALL_REQ,
+      "--gtest_filter=SmokeTest.RecoveryMode_ForMountInstall" },
+  { ReqType::MOUNT_UPDATE_REQ,
+      "--gtest_filter=SmokeTest.RecoveryMode_ForMountUpdate" }
 };
 
 int delay = 1000000;
@@ -289,107 +297,78 @@ TEST_F(SmokeTest, RecoveryMode_ForMountUpdate) {
 
 const int kBufSize = 1024;
 
-const char *short_options = "iudmn:l:t:r:b";
-const struct option long_options[] = {
-  {"install", 0, NULL, 'i'},
-  {"update", 0, NULL, 'u'},
-  {"delta", 0, NULL, 'd'},
-  {"mount-install", 0, NULL, 'm'},
-  {"mount-update", 0, NULL, 'n'},
-  {"delay", 1, NULL, 'l'},
-  {"interval", 1, NULL, 't'},
-  {"repeat", 1, NULL, 'r'},
-  {"no-backup", 0, NULL, 'b'},
-  {0, 0, 0, 0}    /* sentinel */
-};
-
-static void __print_usage() {
-  printf("\n");
-  printf("--install             "
-      "recovery test for forced termination during package installing\n");
-  printf("--update              "
-      "recovery test for forced termination during package updating\n");
-  printf("--delta               "
-      "recovery test for forced termination during package delta installing\n");
-  printf("--mount-install       "
-      "recovery test for forced termination during package mount-installing\n");
-  printf("--mount-update        "
-      "recovery test for forced termination during package mount-updating\n");
-  printf("--delay    <u_time>   "
-      "fixed delay for forced termination\n");
-  printf("--interval <u_time>   "
-      "use with repeat option. as it repeat the interval is added to delay\n");
-  printf("--repeat   <count>    "
-      "option for performing tests repeatedly\n");
-  printf("--no-backup           "
-      "do a test without backup\n");
-}
-
 int main(int argc,  char** argv) {
   bool no_backup = false;
-  int c = -1;
-  int opt_idx = 0;
   int repeat_count = 10;
-  int req_type = -1;
+  ReqType req_type = ReqType::UNKNOWN_REQ;
   bool repeat = false;
 
-  if (argc == 1) {
-    __print_usage();
-    return 0;
-  }
+  bpo::options_description options("Allowed options");
+  bpo::variables_map opt_map;
+  try {
+    options.add_options()
+        ("help", "display this help message")
+        ("install",
+            "recovery test for forced termination "
+            "during package installing")
+        ("update",
+            "recovery test for forced termination "
+            "during package updating")
+        ("delta",
+            "recovery test for forced termination "
+            "during package delta installing")
+        ("mount-install",
+            "recovery test for forced termination "
+            "during package mount-installing")
+        ("mount-update",
+            "recovery test for forced termination "
+            "during package mount-updating")
+        ("delay", bpo::value<int>(), "fixed delay for forced termination")
+        ("interval", bpo::value<int>(),
+            "use with repeat option. "
+            "as it repeat the interval is added to delay")
+        ("repeat", bpo::value<int>(), "option for performing tests repeatedly")
+        ("no-backup", "Do test without backup");
+    bpo::store(bpo::parse_command_line(argc, argv, options), opt_map);
+    if (opt_map.count("help")) {
+      std::cerr << options << std::endl;
+      return -1;
+    }
 
-  while (1) {
-    c = getopt_long(argc, argv, short_options, long_options, &opt_idx);
-    if (c == -1)
-      break;  /* Parse end */
-    switch (c) {
-    case 'i':  /* install */
-      req_type = INSTALL_REQ;
-      break;
-
-    case 'u':  /* update */
-      req_type = UPDATE_REQ;
-      break;
-
-    case 'd':  /* delta */
-      req_type = DELTA_REQ;
-      break;
-
-    case 'm':  /* mount install */
-      req_type = MOUNT_INSTALL_REQ;
-      break;
-
-    case 'n':  /* mount update */
-      req_type = MOUNT_UPDATE_REQ;
-      break;
-
-    case 'l':
-      if (optarg)
-        delay = atoi(optarg);
-      break;
-
-    case 't':
-      if (optarg)
-        interval = atoi(optarg);
-      break;
-
-    case 'r':  /* repeat */
-      repeat = true;
-      if (optarg)
-        repeat_count = atoi(optarg);
-      break;
+    if (opt_map.count("install"))
+      req_type = ReqType::INSTALL_REQ;
+    else if (opt_map.count("update"))
+      req_type = ReqType::UPDATE_REQ;
+    else if (opt_map.count("delta"))
+      req_type = ReqType::DELTA_REQ;
+    else if (opt_map.count("mount-install"))
+      req_type = ReqType::MOUNT_INSTALL_REQ;
+    else if (opt_map.count("mount-update"))
+      req_type = ReqType::MOUNT_UPDATE_REQ;
 
-    case 'b':  /* skip backup */
-        no_backup = true;
-      break;
+    if (opt_map.count("delay"))
+      delay = opt_map["delay"].as<int>();
 
-    default:
-      break;
+    if (opt_map.count("interval"))
+      interval = opt_map["interval"].as<int>();
+
+    if (opt_map.count("repeat")) {
+      repeat = true;
+      repeat_count = opt_map["repeat"].as<int>();
     }
+
+    if (opt_map.count("no-backup"))
+      no_backup = true;
+    bpo::notify(opt_map);
+  } catch (...) {
+    std::cerr << "Exception occurred: "
+              << boost::current_exception_diagnostic_information()
+              << std::endl;
+    return -1;
   }
 
-  if (req_type == -1) {
-    __print_usage();
+  if (req_type == ReqType::UNKNOWN_REQ) {
+    std::cerr << options << std::endl;
     return 0;
   }
 
@@ -399,7 +378,7 @@ int main(int argc,  char** argv) {
 
     gtest_argv.push_back(nullptr);
     snprintf(buf[gtest_argv.size()], kBufSize,
-        "%s", install_req_filter[req_type]);
+        "%s", install_req_filter[req_type].c_str());
     gtest_argv.push_back(buf[gtest_argv.size()]);
     if (repeat) {
       snprintf(buf[gtest_argv.size()], kBufSize,