Browse Source

[1880] Use a helper function instead of repeating code

Mukund Sivaraman 13 years ago
parent
commit
408043d940

+ 2 - 0
src/lib/log/tests/Makefile.am

@@ -67,6 +67,7 @@ run_unittests_CPPFLAGS = $(AM_CPPFLAGS)
 run_unittests_CXXFLAGS = $(AM_CXXFLAGS)
 run_unittests_LDADD    = $(AM_LDADD)
 run_unittests_LDFLAGS  = $(AM_LDFLAGS)
+run_unittests_LDFLAGS  += $(top_builddir)/src/lib/testutils/libtestutils.la
 
 # logging initialization tests.  These are put in separate programs to
 # ensure that the initialization status at the start of each test is known,
@@ -90,6 +91,7 @@ initializer_unittests_2_CPPFLAGS = $(AM_CPPFLAGS)
 initializer_unittests_2_CXXFLAGS = $(AM_CXXFLAGS)
 initializer_unittests_2_LDADD    = $(AM_LDADD)
 initializer_unittests_2_LDFLAGS  = $(AM_LDFLAGS)
+initializer_unittests_2_LDFLAGS  += $(top_builddir)/src/lib/testutils/libtestutils.la
 
 noinst_PROGRAMS += $(TESTS)
 endif

+ 3 - 10
src/lib/log/tests/logger_unittest.cc

@@ -14,11 +14,11 @@
 
 #include <iostream>
 #include <string>
-#include <sys/time.h>
-#include <sys/resource.h>
 
 #include <gtest/gtest.h>
 
+#include <testutils/resource.h>
+
 #include <log/logger.h>
 #include <log/logger_manager.h>
 #include <log/logger_name.h>
@@ -372,14 +372,7 @@ TEST_F(LoggerTest, LoggerNameLength) {
     // Note that we just check that it dies - we don't check what message is
     // output.
     EXPECT_DEATH({
-        /* Set rlimits so that no coredumps are created. As a new
-           process is forked to run this EXPECT_DEATH test, the rlimits
-           of the parent process that runs the other tests should be
-           unaffected. */
-        rlimit core_limit;
-        core_limit.rlim_cur = 0;
-        core_limit.rlim_max = 0;
-        EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+        isc::testutils::dontCreateCoreDumps();
 
         string ok3(Logger::MAX_LOGGER_NAME_SIZE + 1, 'x');
         Logger l3(ok3.c_str());

+ 2 - 10
src/lib/log/tests/message_initializer_2_unittest.cc

@@ -12,10 +12,9 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
-#include <sys/time.h>
-#include <sys/resource.h>
 #include <log/message_initializer.h>
 #include <gtest/gtest.h>
+#include <testutils/resource.h>
 
 using namespace isc::log;
 
@@ -44,14 +43,7 @@ TEST(MessageInitializerTest2, MessageLoadTest) {
 #ifdef EXPECT_DEATH
     // Adding one more should take us over the limit.
     EXPECT_DEATH({
-        /* Set rlimits so that no coredumps are created. As a new
-           process is forked to run this EXPECT_DEATH test, the rlimits
-           of the parent process that runs the other tests should be
-           unaffected. */
-        rlimit core_limit;
-        core_limit.rlim_cur = 0;
-        core_limit.rlim_max = 0;
-        EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+        isc::testutils::dontCreateCoreDumps();
 
         MessageInitializer initializer2(values);
       }, ".*");

+ 1 - 0
src/lib/server_common/tests/Makefile.am

@@ -48,6 +48,7 @@ run_unittests_LDADD += $(top_builddir)/src/lib/util/unittests/libutil_unittests.
 run_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libexceptions.la
 run_unittests_LDADD += $(top_builddir)/src/lib/config/libcfgclient.la
 run_unittests_LDADD += $(top_builddir)/src/lib/config/tests/libfake_session.la
+run_unittests_LDADD += $(top_builddir)/src/lib/testutils/libtestutils.la
 endif
 
 noinst_PROGRAMS = $(TESTS)

+ 3 - 19
src/lib/server_common/tests/portconfig_unittest.cc

@@ -12,12 +12,10 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
-#include <sys/time.h>
-#include <sys/resource.h>
-
 #include <server_common/portconfig.h>
 #include <testutils/socket_request.h>
 #include <testutils/mockups.h>
+#include <testutils/resource.h>
 
 #include <cc/data.h>
 #include <exceptions/exceptions.h>
@@ -316,14 +314,7 @@ TEST_F(InstallListenAddressesDeathTest, inconsistent) {
     // Make sure it actually kills the application (there should be an abort
     // in this case)
     EXPECT_DEATH({
-        /* Set rlimits so that no coredumps are created. As a new
-           process is forked to run this EXPECT_DEATH test, the rlimits
-           of the parent process that runs the other tests should be
-           unaffected. */
-        rlimit core_limit;
-        core_limit.rlim_cur = 0;
-        core_limit.rlim_max = 0;
-        EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+        isc::testutils::dontCreateCoreDumps();
 
         try {
           installListenAddresses(deathAddresses, store_, dnss_);
@@ -342,14 +333,7 @@ TEST_F(InstallListenAddressesDeathTest, cantClose) {
     // Instruct it to fail on close
     sock_requestor_.break_release_ = true;
     EXPECT_DEATH({
-        /* Set rlimits so that no coredumps are created. As a new
-           process is forked to run this EXPECT_DEATH test, the rlimits
-           of the parent process that runs the other tests should be
-           unaffected. */
-        rlimit core_limit;
-        core_limit.rlim_cur = 0;
-        core_limit.rlim_max = 0;
-        EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+        isc::testutils::dontCreateCoreDumps();
 
 	try {
 	  // Setting to empty will close all current sockets.

+ 1 - 0
src/lib/testutils/Makefile.am

@@ -9,6 +9,7 @@ noinst_LTLIBRARIES = libtestutils.la
 
 libtestutils_la_SOURCES = srv_test.h srv_test.cc
 libtestutils_la_SOURCES += dnsmessage_test.h dnsmessage_test.cc
+libtestutils_la_SOURCES += resource.cc resource.h
 libtestutils_la_SOURCES += mockups.h
 libtestutils_la_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
 libtestutils_la_LIBADD = $(top_builddir)/src/lib/asiolink/libasiolink.la

+ 36 - 0
src/lib/testutils/resource.cc

@@ -0,0 +1,36 @@
+// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <sys/time.h>
+#include <sys/resource.h>
+
+#include <gtest/gtest.h>
+
+namespace isc {
+namespace testutils {
+
+void
+dontCreateCoreDumps(void)
+{
+    /* Set rlimits so that no coredumps are created. As a new
+       process is forked to run this EXPECT_DEATH test, the rlimits
+       of the parent process that runs the other tests should be
+       unaffected. */
+    rlimit core_limit = {0, 0};
+
+    EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+}
+
+} // end of namespace testutils
+} // end of namespace isc

+ 35 - 0
src/lib/testutils/resource.h

@@ -0,0 +1,35 @@
+// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#ifndef __ISC_TESTUTILS_RESOURCE_H
+#define __ISC_TESTUTILS_RESOURCE_H
+
+namespace isc {
+namespace testutils {
+
+/// Don't create core dumps.
+///
+/// This function sets the core size to 0, inhibiting the creation of
+/// core dumps. It is meant to be used in testcases where EXPECT_DEATH
+/// is used, where processes abort (and create cores in the process).
+/// As a new process is forked to run EXPECT_DEATH tests, the rlimits of
+/// the parent process that runs the other tests should be unaffected.
+
+void
+dontCreateCoreDumps(void);
+
+} // end of namespace testutils
+} // end of namespace isc
+
+#endif /* __ISC_TESTUTILS_RESOURCE_H */

+ 1 - 0
src/lib/util/tests/Makefile.am

@@ -44,6 +44,7 @@ run_unittests_LDADD += $(top_builddir)/src/lib/util/io/libutil_io.la
 run_unittests_LDADD += \
 	$(top_builddir)/src/lib/util/unittests/libutil_unittests.la
 run_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libexceptions.la
+run_unittests_LDADD += $(top_builddir)/src/lib/testutils/libtestutils.la
 endif
 
 noinst_PROGRAMS = $(TESTS)

+ 2 - 11
src/lib/util/tests/buffer_unittest.cc

@@ -12,10 +12,8 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
-#include <sys/time.h>
-#include <sys/resource.h>
-
 #include <exceptions/exceptions.h>
+#include <testutils/resource.h>
 
 #include <util/buffer.h>
 
@@ -188,14 +186,7 @@ TEST_F(BufferTest, outputBufferReadat) {
 #ifdef EXPECT_DEATH
     // We use assert now, so we check it dies
     EXPECT_DEATH({
-        /* Set rlimits so that no coredumps are created. As a new
-           process is forked to run this EXPECT_DEATH test, the rlimits
-           of the parent process that runs the other tests should be
-           unaffected. */
-        rlimit core_limit;
-        core_limit.rlim_cur = 0;
-        core_limit.rlim_max = 0;
-        EXPECT_EQ(setrlimit(RLIMIT_CORE, &core_limit), 0);
+        isc::testutils::dontCreateCoreDumps();
 
         try {
             obuffer[sizeof(testdata)];