|
@@ -34,6 +34,8 @@ namespace dhcp {
|
|
|
class DaemonImpl : public Daemon {
|
|
|
public:
|
|
|
static std::string getVersion(bool extended);
|
|
|
+
|
|
|
+ using Daemon::makePIDFileName;
|
|
|
};
|
|
|
|
|
|
std::string DaemonImpl::getVersion(bool extended) {
|
|
@@ -63,6 +65,8 @@ public:
|
|
|
/// the default after each test completes.
|
|
|
~DaemonTest() {
|
|
|
isc::log::setDefaultLoggingOutput();
|
|
|
+ // Since it's static we need to clear it between tests
|
|
|
+ Daemon::setConfigFile("");
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -75,6 +79,172 @@ TEST_F(DaemonTest, constructor) {
|
|
|
// Check that the verbose mode is not set by default.
|
|
|
Daemon instance2;
|
|
|
EXPECT_FALSE(instance2.getVerbose());
|
|
|
+
|
|
|
+ EXPECT_EQ("",instance2.getConfigFile());
|
|
|
+ EXPECT_EQ("",instance2.getProcName());
|
|
|
+ EXPECT_EQ(CfgMgr::instance().getDataDir(),instance2.getPIDFileDir());
|
|
|
+ EXPECT_EQ("",instance2.getPIDFileName());
|
|
|
+}
|
|
|
+
|
|
|
+// Verify config file accessors
|
|
|
+TEST_F(DaemonTest, getSetConfigFile) {
|
|
|
+ Daemon instance;
|
|
|
+
|
|
|
+ EXPECT_NO_THROW(instance.setConfigFile("test.txt"));
|
|
|
+ EXPECT_EQ("test.txt", instance.getConfigFile());
|
|
|
+}
|
|
|
+
|
|
|
+// Verify process name accessors
|
|
|
+TEST_F(DaemonTest, getSetProcName) {
|
|
|
+ Daemon instance;
|
|
|
+
|
|
|
+ EXPECT_NO_THROW(instance.setProcName("myproc"));
|
|
|
+ EXPECT_EQ("myproc", instance.getProcName());
|
|
|
+}
|
|
|
+
|
|
|
+// Verify PID file directory name accessors
|
|
|
+TEST_F(DaemonTest, getSetPIDFileDir) {
|
|
|
+ Daemon instance;
|
|
|
+
|
|
|
+ EXPECT_NO_THROW(instance.setPIDFileDir("/tmp"));
|
|
|
+ EXPECT_EQ("/tmp", instance.getPIDFileDir());
|
|
|
+}
|
|
|
+
|
|
|
+// Verify PID file name accessors.
|
|
|
+TEST_F(DaemonTest, setPIDFileName) {
|
|
|
+ Daemon instance;
|
|
|
+
|
|
|
+ // Verify that PID file name may not be set to empty
|
|
|
+ EXPECT_THROW(instance.setPIDFileName(""), BadValue);
|
|
|
+
|
|
|
+ EXPECT_NO_THROW(instance.setPIDFileName("myproc"));
|
|
|
+ EXPECT_EQ("myproc", instance.getPIDFileName());
|
|
|
+
|
|
|
+ // Verify that setPIDFileName cannot be called twice on the same instance.
|
|
|
+ EXPECT_THROW(instance.setPIDFileName("again"), InvalidOperation);
|
|
|
+}
|
|
|
+
|
|
|
+// Test the getVersion() redefinition
|
|
|
+TEST_F(DaemonTest, getVersion) {
|
|
|
+ EXPECT_THROW(Daemon::getVersion(false), NotImplemented);
|
|
|
+
|
|
|
+ ASSERT_NO_THROW(DaemonImpl::getVersion(false));
|
|
|
+
|
|
|
+ EXPECT_EQ(DaemonImpl::getVersion(false), "BASIC");
|
|
|
+
|
|
|
+ ASSERT_NO_THROW(DaemonImpl::getVersion(true));
|
|
|
+
|
|
|
+ EXPECT_EQ(DaemonImpl::getVersion(true), "EXTENDED");
|
|
|
+}
|
|
|
+
|
|
|
+// Verify makePIDFileName method
|
|
|
+TEST_F(DaemonTest, makePIDFileName) {
|
|
|
+ DaemonImpl instance;
|
|
|
+
|
|
|
+ // Verify that config file cannot be blank
|
|
|
+ instance.setProcName("notblank");
|
|
|
+ EXPECT_THROW(instance.makePIDFileName(), InvalidOperation);
|
|
|
+
|
|
|
+ // Verify that proc name cannot be blank
|
|
|
+ instance.setProcName("");
|
|
|
+ instance.setConfigFile("notblank");
|
|
|
+ EXPECT_THROW(instance.makePIDFileName(), InvalidOperation);
|
|
|
+
|
|
|
+ // Verify that config file must contain a file name
|
|
|
+ instance.setProcName("myproc");
|
|
|
+ instance.setConfigFile(".txt");
|
|
|
+ EXPECT_THROW(instance.makePIDFileName(), BadValue);
|
|
|
+ instance.setConfigFile("/tmp/");
|
|
|
+ EXPECT_THROW(instance.makePIDFileName(), BadValue);
|
|
|
+
|
|
|
+ // Given a valid config file name and proc name we should good to go
|
|
|
+ instance.setConfigFile("/tmp/test.conf");
|
|
|
+ std::string name;
|
|
|
+ EXPECT_NO_THROW(name = instance.makePIDFileName());
|
|
|
+
|
|
|
+ // Make sure the name is as we expect
|
|
|
+ std::ostringstream stream;
|
|
|
+ stream << CfgMgr::instance().getDataDir() << "/test.myproc.pid";
|
|
|
+ EXPECT_EQ(stream.str(), name);
|
|
|
+
|
|
|
+ // Verify that the default directory can be overridden
|
|
|
+ instance.setPIDFileDir("/tmp");
|
|
|
+ EXPECT_NO_THROW(name = instance.makePIDFileName());
|
|
|
+ EXPECT_EQ("/tmp/test.myproc.pid", name);
|
|
|
+}
|
|
|
+
|
|
|
+// Verifies the creation a PID file and that a pre-existing PID file
|
|
|
+// which points to a live PID causes a throw.
|
|
|
+TEST_F(DaemonTest, createPIDFile) {
|
|
|
+ DaemonImpl instance;
|
|
|
+
|
|
|
+ instance.setConfigFile("test.conf");
|
|
|
+ instance.setProcName("daemon_test");
|
|
|
+ instance.setPIDFileDir(TEST_DATA_BUILDDIR);
|
|
|
+
|
|
|
+ EXPECT_NO_THROW(instance.createPIDFile());
|
|
|
+
|
|
|
+ std::ostringstream stream;
|
|
|
+ stream << TEST_DATA_BUILDDIR << "/test.daemon_test.pid";
|
|
|
+ EXPECT_EQ(stream.str(), instance.getPIDFileName());
|
|
|
+
|
|
|
+ // If we try again, we should see our own PID file and fail
|
|
|
+ EXPECT_THROW(instance.createPIDFile(), DaemonPIDExists);
|
|
|
+}
|
|
|
+
|
|
|
+// Verifies that a pre-existing PID file which points to a dead PID
|
|
|
+// is overwritten.
|
|
|
+TEST_F(DaemonTest, createPIDFileOverwrite) {
|
|
|
+ DaemonImpl instance;
|
|
|
+
|
|
|
+ // We're going to use fork to generate a PID we KNOW is dead.
|
|
|
+ int pid = fork();
|
|
|
+ ASSERT_GE(pid, 0);
|
|
|
+
|
|
|
+ if (pid == 0) {
|
|
|
+ // This is the child, die right away. Tragic, no?
|
|
|
+ exit (0);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Back in the parent test, we need to wait for the child to die
|
|
|
+ int stat;
|
|
|
+ int ret = waitpid(pid, &stat, 0);
|
|
|
+ ASSERT_EQ(ret, pid);
|
|
|
+
|
|
|
+ // Ok, so we should now have a PID that we know to be dead.
|
|
|
+ // Let's use it to create a PID file.
|
|
|
+ instance.setConfigFile("test.conf");
|
|
|
+ instance.setProcName("daemon_test");
|
|
|
+ instance.setPIDFileDir(TEST_DATA_BUILDDIR);
|
|
|
+ EXPECT_NO_THROW(instance.createPIDFile(pid));
|
|
|
+
|
|
|
+ // If we try to create the PID file again, this should work.
|
|
|
+ EXPECT_NO_THROW(instance.createPIDFile());
|
|
|
+}
|
|
|
+
|
|
|
+// Verifies that Daemon destruction deletes the PID file
|
|
|
+TEST_F(DaemonTest, PIDFileCleanup) {
|
|
|
+ boost::shared_ptr<DaemonImpl> instance;
|
|
|
+ instance.reset(new DaemonImpl);
|
|
|
+
|
|
|
+ instance->setConfigFile("test.conf");
|
|
|
+ instance->setProcName("daemon_test");
|
|
|
+ instance->setPIDFileDir(TEST_DATA_BUILDDIR);
|
|
|
+ EXPECT_NO_THROW(instance->createPIDFile());
|
|
|
+
|
|
|
+ // If we try again, we should see our own PID file
|
|
|
+ EXPECT_THROW(instance->createPIDFile(), DaemonPIDExists);
|
|
|
+
|
|
|
+ // Save the pid file name
|
|
|
+ std::string pid_file_name = instance->getPIDFileName();
|
|
|
+
|
|
|
+ // Now delete the Daemon instance. This should remove the
|
|
|
+ // PID file.
|
|
|
+ instance.reset();
|
|
|
+
|
|
|
+ struct stat stat_buf;
|
|
|
+ ASSERT_EQ(-1, stat(pid_file_name.c_str(), &stat_buf));
|
|
|
+ EXPECT_EQ(errno, ENOENT);
|
|
|
}
|
|
|
|
|
|
// Checks that configureLogger method is behaving properly.
|
|
@@ -117,18 +287,6 @@ TEST_F(DaemonTest, parsingConsoleOutput) {
|
|
|
EXPECT_EQ("stdout" , storage->getLoggingInfo()[0].destinations_[0].output_);
|
|
|
}
|
|
|
|
|
|
-// Test the getVersion() redefinition
|
|
|
-TEST_F(DaemonTest, getVersion) {
|
|
|
- EXPECT_THROW(Daemon::getVersion(false), NotImplemented);
|
|
|
-
|
|
|
- ASSERT_NO_THROW(DaemonImpl::getVersion(false));
|
|
|
-
|
|
|
- EXPECT_EQ(DaemonImpl::getVersion(false), "BASIC");
|
|
|
-
|
|
|
- ASSERT_NO_THROW(DaemonImpl::getVersion(true));
|
|
|
-
|
|
|
- EXPECT_EQ(DaemonImpl::getVersion(true), "EXTENDED");
|
|
|
-}
|
|
|
|
|
|
|
|
|
// More tests will appear here as we develop Daemon class.
|