Browse Source

[master] Merged trac4062 (fixed DaemonTest.createPIDFileOverwrite in debug)

Francis Dupont 9 years ago
parent
commit
bd35386624
1 changed files with 25 additions and 3 deletions
  1. 25 3
      src/lib/dhcpsrv/tests/daemon_unittest.cc

+ 25 - 3
src/lib/dhcpsrv/tests/daemon_unittest.cc

@@ -57,7 +57,12 @@ namespace {
 class DaemonTest : public ::testing::Test {
 public:
     /// @brief Constructor
-    DaemonTest() {
+    DaemonTest() : env_copy_() {
+        // Take a copy of KEA_PIDFILE_DIR environment variable value
+        char *env_value = getenv("KEA_PIDFILE_DIR");
+        if (env_value) {
+            env_copy_ = std::string(env_value);
+        }
     }
 
     /// @brief Destructor
@@ -67,13 +72,26 @@ public:
     /// the default after each test completes.
     ~DaemonTest() {
         isc::log::setDefaultLoggingOutput();
+        // Restore KEA_PIDFILE_DIR environment variable value
+        if (env_copy_.empty()) {
+            static_cast<void>(unsetenv("KEA_PIDFILE_DIR"));
+        } else {
+            static_cast<void>(setenv("KEA_PIDFILE_DIR", env_copy_.c_str(), 1));
+        }
     }
+
+private:
+    /// @brief copy of KEA_PIDFILE_DIR environment variable value
+    std::string env_copy_;
 };
 
 
 // Very simple test. Checks whether Daemon can be instantiated and its
 // default parameters are sane
 TEST_F(DaemonTest, constructor) {
+    // Disable KEA_PIDFILE_DIR
+    EXPECT_EQ(0, unsetenv("KEA_PIDFILE_DIR"));
+
     EXPECT_NO_THROW(Daemon instance1);
 
     // Check that the verbose mode is not set by default.
@@ -164,7 +182,7 @@ TEST_F(DaemonTest, makePIDFileName) {
 
     // Make sure the name is as we expect
     std::ostringstream stream;
-    stream  << CfgMgr::instance().getDataDir() << "/test.myproc.pid";
+    stream  << instance.getPIDFileDir() << "/test.myproc.pid";
     EXPECT_EQ(stream.str(), name);
 
     // Verify that the default directory can be overridden
@@ -207,8 +225,12 @@ TEST_F(DaemonTest, createPIDFileOverwrite) {
     }
 
     // Back in the parent test, we need to wait for the child to die
+    // As with debugging waitpid() can be interrupted ignore EINTR.
     int stat;
-    int ret = waitpid(pid, &stat, 0);
+    int ret;
+    do {
+        ret = waitpid(pid, &stat, 0);
+    } while ((ret == -1) && (errno == EINTR));
     ASSERT_EQ(ret, pid);
 
     // Ok, so we should now have a PID that we know to be dead.