]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
test: Don't dump core when using EXPECT_DEATH 14821/head
authorBrad Hubbard <bhubbard@redhat.com>
Thu, 27 Apr 2017 03:40:50 +0000 (13:40 +1000)
committerBrad Hubbard <bhubbard@redhat.com>
Fri, 28 Apr 2017 01:49:38 +0000 (11:49 +1000)
Signed-off-by: Kefu Chai <kchai@redhat.com>
Signed-off-by: Brad Hubbard <bhubbard@redhat.com>
src/include/coredumpctl.h [new file with mode: 0644]
src/log/test.cc
src/test/bufferlist.cc
src/test/ceph_compatset.cc
src/test/common/test_mutex.cc
src/test/objectstore/chain_xattr.cc
src/test/objectstore/store_test.cc
src/test/osd/TestPGLog.cc
src/test/osd/types.cc
src/test/signals.cc
src/test/test_texttable.cc

diff --git a/src/include/coredumpctl.h b/src/include/coredumpctl.h
new file mode 100644 (file)
index 0000000..e442494
--- /dev/null
@@ -0,0 +1,39 @@
+#ifdef HAVE_SYS_PRCTL_H
+#include <iostream>
+#include <sys/prctl.h>
+#include "common/errno.h"
+
+struct PrCtl {
+  int saved_state = -1;
+  int set_dumpable(int new_state) {
+    int r = prctl(PR_SET_DUMPABLE, new_state);
+    if (r) {
+      r = -errno;
+      std::cerr << "warning: unable to " << (new_state ? "set" : "unset")
+                << " dumpable flag: " << cpp_strerror(r)
+                << std::endl;
+    }
+    return r;
+  }
+  PrCtl(int new_state = 0) {
+    int r = prctl(PR_GET_DUMPABLE);
+    if (r == -1) {
+      r = errno;
+      std::cerr << "warning: unable to get dumpable flag: " << cpp_strerror(r)
+                << std::endl;
+    } else if (r != new_state) {
+      if (!set_dumpable(new_state)) {
+        saved_state = r;
+      }
+    }
+  }
+  ~PrCtl() {
+    if (saved_state < 0) {
+      return;
+    }
+    set_dumpable(saved_state);
+  }
+};
+#else
+struct PrCtl {};
+#endif
index 573cc35b5e491466cfe24768e64f881e904eb3e0..e11505af4561158d7c91d11dd65b0deb2bef888e 100644 (file)
@@ -3,6 +3,7 @@
 #include "log/Log.h"
 #include "common/Clock.h"
 #include "common/PrebufferedStreambuf.h"
+#include "include/coredumpctl.h"
 #include "SubsystemMap.h"
 
 using namespace ceph::logging;
@@ -201,7 +202,10 @@ void do_segv()
 
   log.inject_segv();
   Entry *e = new Entry(ceph_clock_now(), pthread_self(), 10, 1);
-  log.submit_entry(e);  // this should segv
+  {
+    PrCtl unset_dumpable;
+    log.submit_entry(e);  // this should segv
+  }
 
   log.flush();
   log.stop();
index eb259544b45b7c280667dfed03a2f3689d017d82..af3999e55a02c94725d9d3147f7757ae67c43674 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "include/buffer.h"
 #include "include/utime.h"
+#include "include/coredumpctl.h"
 #include "include/encoding.h"
 #include "common/environment.h"
 #include "common/Clock.h"
@@ -484,6 +485,7 @@ TEST(BufferPtr, constructors) {
     EXPECT_EQ(original.get_raw(), ptr.get_raw());
     EXPECT_EQ(2, ptr.raw_nref());
     EXPECT_EQ(0, ::memcmp(original.c_str(), ptr.c_str(), len));
+    PrCtl unset_dumpable;
     EXPECT_DEATH(bufferptr(original, 0, original.length() + 1), "");
     EXPECT_DEATH(bufferptr(bufferptr(), 0, 0), "");
   }
@@ -678,12 +680,14 @@ TEST(BufferPtr, accessors) {
   EXPECT_EQ('X', ptr.c_str()[0]);
   {
     bufferptr ptr;
+    PrCtl unset_dumpable;
     EXPECT_DEATH(ptr.c_str(), "");
     EXPECT_DEATH(ptr[0], "");
   }
   EXPECT_EQ('X', const_ptr.c_str()[0]);
   {
     const bufferptr const_ptr;
+    PrCtl unset_dumpable;
     EXPECT_DEATH(const_ptr.c_str(), "");
     EXPECT_DEATH(const_ptr[0], "");
   }
@@ -702,10 +706,14 @@ TEST(BufferPtr, accessors) {
     bufferptr ptr;
     EXPECT_EQ((unsigned)0, ptr.unused_tail_length());
   }
-  EXPECT_DEATH(ptr[len], "");
-  EXPECT_DEATH(const_ptr[len], "");
+  {
+    PrCtl unset_dumpable;
+    EXPECT_DEATH(ptr[len], "");
+    EXPECT_DEATH(const_ptr[len], "");
+  }
   {
     const bufferptr const_ptr;
+    PrCtl unset_dumpable;
     EXPECT_DEATH(const_ptr.raw_c_str(), "");
     EXPECT_DEATH(const_ptr.raw_length(), "");
     EXPECT_DEATH(const_ptr.raw_nref(), "");
@@ -754,6 +762,7 @@ TEST(BufferPtr, is_zero) {
 TEST(BufferPtr, copy_out) {
   {
     const bufferptr ptr;
+    PrCtl unset_dumpable;
     EXPECT_DEATH(ptr.copy_out((unsigned)0, (unsigned)0, NULL), "");
   }
   {
@@ -789,13 +798,17 @@ TEST(BufferPtr, copy_out_bench) {
 TEST(BufferPtr, copy_in) {
   {
     bufferptr ptr;
+    PrCtl unset_dumpable;
     EXPECT_DEATH(ptr.copy_in((unsigned)0, (unsigned)0, NULL), "");
   }
   {
     char in[] = "ABCD";
     bufferptr ptr(2);
-    EXPECT_DEATH(ptr.copy_in((unsigned)0, strlen(in) + 1, NULL), "");
-    EXPECT_DEATH(ptr.copy_in(strlen(in) + 1, (unsigned)0, NULL), "");
+    {
+      PrCtl unset_dumpable;
+      EXPECT_DEATH(ptr.copy_in((unsigned)0, strlen(in) + 1, NULL), "");
+      EXPECT_DEATH(ptr.copy_in(strlen(in) + 1, (unsigned)0, NULL), "");
+    }
     ptr.copy_in((unsigned)0, (unsigned)2, in);
     EXPECT_EQ(in[0], ptr[0]);
     EXPECT_EQ(in[1], ptr[1]);
@@ -823,13 +836,17 @@ TEST(BufferPtr, copy_in_bench) {
 TEST(BufferPtr, append) {
   {
     bufferptr ptr;
+    PrCtl unset_dumpable;
     EXPECT_DEATH(ptr.append('A'), "");
     EXPECT_DEATH(ptr.append("B", (unsigned)1), "");
   }
   {
     bufferptr ptr(2);
-    EXPECT_DEATH(ptr.append('A'), "");
-    EXPECT_DEATH(ptr.append("B", (unsigned)1), "");
+    {
+      PrCtl unset_dumpable;
+      EXPECT_DEATH(ptr.append('A'), "");
+      EXPECT_DEATH(ptr.append("B", (unsigned)1), "");
+    }
     ptr.set_length(0);
     ptr.append('A');
     EXPECT_EQ((unsigned)1, ptr.length());
@@ -864,7 +881,10 @@ TEST(BufferPtr, append_bench) {
 TEST(BufferPtr, zero) {
   char str[] = "XXXX";
   bufferptr ptr(buffer::create_static(strlen(str), str));
-  EXPECT_DEATH(ptr.zero(ptr.length() + 1, 0), "");
+  {
+    PrCtl unset_dumpable;
+    EXPECT_DEATH(ptr.zero(ptr.length() + 1, 0), "");
+  }
   ptr.zero(1, 1);
   EXPECT_EQ('X', ptr[0]);
   EXPECT_EQ('\0', ptr[1]);
@@ -2207,7 +2227,10 @@ TEST(BufferList, append) {
     bufferptr in(back);
     EXPECT_EQ((unsigned)1, bl.get_num_buffers());
     EXPECT_EQ((unsigned)1, bl.length());
-    EXPECT_DEATH(bl.append(in, (unsigned)100, (unsigned)100), "");
+    {
+      PrCtl unset_dumpable;
+      EXPECT_DEATH(bl.append(in, (unsigned)100, (unsigned)100), "");
+    }
     EXPECT_LT((unsigned)0, in.unused_tail_length());
     in.append('B');
     bl.append(in, back.end(), 1);
@@ -2764,7 +2787,10 @@ TEST(BufferList, zero) {
       bufferptr ptr(s[i], strlen(s[i]));
       bl.push_back(ptr);
     }
-    EXPECT_DEATH(bl.zero((unsigned)0, (unsigned)2000), "");
+    {
+      PrCtl unset_dumpable;
+      EXPECT_DEATH(bl.zero((unsigned)0, (unsigned)2000), "");
+    }
     bl.zero((unsigned)2, (unsigned)5);
     EXPECT_EQ(0, ::memcmp("AB\0\0\0\0\0HIKLM", bl.c_str(), 9));
   }
index 5cd7ef2a7c963dd8f753751026da8fe374189bb0..3010b559988653f9feadc10e39ff95092c15ff11 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "include/types.h"
 #include "include/compat.h"
+#include "include/coredumpctl.h"
 
 //#undef assert
 //#define      assert(foo) if (!(foo)) abort();
@@ -37,8 +38,11 @@ TEST(CephCompatSet, AllSet) {
   CompatSet::FeatureSet ro;
   CompatSet::FeatureSet incompat;
 
-  EXPECT_DEATH(compat.insert(CompatSet::Feature(0, "test")), "");
-  EXPECT_DEATH(compat.insert(CompatSet::Feature(64, "test")), "");
+  {
+    PrCtl unset_dumpable;
+    EXPECT_DEATH(compat.insert(CompatSet::Feature(0, "test")), "");
+    EXPECT_DEATH(compat.insert(CompatSet::Feature(64, "test")), "");
+  }
 
   for (int i = 1; i < 64; i++) {
     stringstream cname;
index eea86df204f255f5cc9c4e5a31180dd80bf5be44..b62341e71422120f2b53ead160faee7946372858 100644 (file)
@@ -1,5 +1,5 @@
 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
-// vim: ts=8 sw=2 &smarttab
+// vim: ts=8 sw=2 smarttab
 /*
  * Ceph - scalable distributed file system
  *
@@ -9,6 +9,7 @@
 #include "gtest/gtest.h"
 #include "common/ceph_context.h"
 #include "common/config.h"
+#include "include/coredumpctl.h"
 
 /*
  * Override normal ceph assert.
@@ -62,5 +63,6 @@ TEST(Mutex, RecursiveWithoutLockdep) {
 TEST(Mutex, DeleteLocked) {
   Mutex* m = new Mutex("Recursive3",false);
   m->Lock();
+  PrCtl unset_dumpable;
   EXPECT_DEATH(delete m,".*");
 }
index 9f98ed2934df13e60ac2888d3d319a4ff1ab4e24..a30c57c84030a7172fbb0b9d7f889babc33215b5 100644 (file)
@@ -23,6 +23,7 @@
 #include <signal.h>
 #include "os/filestore/chain_xattr.h"
 #include "include/Context.h"
+#include "include/coredumpctl.h"
 #include "common/errno.h"
 #include "common/ceph_argparse.h"
 #include "global/global_init.h"
@@ -120,6 +121,7 @@ TEST(chain_xattr, get_and_set) {
   {
     int x;
     const string name = user + string(CHAIN_XATTR_MAX_NAME_LEN * 2, '@');
+    PrCtl unset_dumpable;
     ASSERT_DEATH(chain_setxattr(file, name.c_str(), &x, sizeof(x)), "");
     ASSERT_DEATH(chain_fsetxattr(fd, name.c_str(), &x, sizeof(x)), "");
   }
index 627a5337dbdfe33ed1bc7092c40e9649a6edf901..4458f98586e084419b084ccd4644389e39daf17b 100644 (file)
@@ -36,6 +36,7 @@
 #include "common/Cond.h"
 #include "common/errno.h"
 #include "include/stringify.h"
+#include "include/coredumpctl.h"
 
 #include "include/unordered_map.h"
 #include "store_test_fixture.h"
@@ -2825,8 +2826,8 @@ TEST_P(StoreTest, SimpleCloneTest) {
     ObjectStore::Transaction t;
     t.remove_collection(cid);
     cerr << "Invalid rm coll" << std::endl;
+    PrCtl unset_dumpable;
     EXPECT_DEATH(apply_transaction(store, &osr, std::move(t)), ".*Directory not empty.*");
-
   }
   {
     ObjectStore::Transaction t;
@@ -2847,6 +2848,7 @@ TEST_P(StoreTest, SimpleCloneTest) {
     t.remove(cid, hoid);
     t.remove(cid, hoid2);
     t.remove_collection(cid);
+    PrCtl unset_dumpable;
     EXPECT_DEATH(apply_transaction(store, &osr, std::move(t)), ".*Directory not empty.*");
   }
   {
index b052442e4f85a4cf5082f8794aabf816234ea4fa..626c3cef57ccea1fbb469aad4bfed8a330c1ca97 100644 (file)
@@ -24,6 +24,7 @@
 #include "gtest/gtest.h"
 #include "osd/PGLog.h"
 #include "osd/OSDMap.h"
+#include "include/coredumpctl.h"
 
 class PGLogTest : public ::testing::Test, protected PGLog {
 public:
@@ -1211,6 +1212,7 @@ TEST_F(PGLogTest, merge_log) {
     olog.tail = eversion_t(1, 1);
 
     TestHandler h(remove_snap);
+    PrCtl unset_dumpable;
     ASSERT_DEATH(merge_log(oinfo, olog, fromosd, info, &h,
                           dirty_info, dirty_big_info), "");
   }
index 3a03d606d38dcc747086eb51a6a196ff892d58b2..ded980e257630bdb23937c8f3aac6282dbade9a8 100644 (file)
@@ -19,6 +19,7 @@
 #include "osd/osd_types.h"
 #include "osd/OSDMap.h"
 #include "gtest/gtest.h"
+#include "include/coredumpctl.h"
 #include "common/Thread.h"
 #include "include/stringify.h"
 #include "osd/ReplicatedBackend.h"
@@ -882,6 +883,7 @@ TEST(pg_missing_t, add_next_event)
     EXPECT_TRUE(e.object_is_indexed());
     EXPECT_FALSE(e.reqid_is_indexed());
     EXPECT_FALSE(missing.is_missing(oid));
+    PrCtl unset_dumpable;
     EXPECT_DEATH(missing.add_next_event(e), "");
   }
 
@@ -1034,14 +1036,20 @@ TEST(pg_missing_t, got)
     hobject_t oid(object_t("objname"), "key", 123, 456, 0, "");
     pg_missing_t missing;
     // assert if the oid does not exist
-    EXPECT_DEATH(missing.got(oid, eversion_t()), "");
+    {
+      PrCtl unset_dumpable;
+      EXPECT_DEATH(missing.got(oid, eversion_t()), "");
+    }
     EXPECT_FALSE(missing.is_missing(oid));
     epoch_t epoch = 10;
     eversion_t need(epoch,10);
     missing.add(oid, need, eversion_t());
     EXPECT_TRUE(missing.is_missing(oid));
     // assert if that the version to be removed is lower than the version of the object
-    EXPECT_DEATH(missing.got(oid, eversion_t(epoch / 2,20)), "");
+    {
+      PrCtl unset_dumpable;
+      EXPECT_DEATH(missing.got(oid, eversion_t(epoch / 2,20)), "");
+    }
     // remove of a later version removes the object
     missing.got(oid, eversion_t(epoch * 2,20));
     EXPECT_FALSE(missing.is_missing(oid));
@@ -1485,6 +1493,7 @@ TEST(ghobject_t, parse) {
 
 TEST(pool_opts_t, invalid_opt) {
   EXPECT_FALSE(pool_opts_t::is_opt_name("INVALID_OPT"));
+  PrCtl unset_dumpable;
   EXPECT_DEATH(pool_opts_t::get_opt_desc("INVALID_OPT"), "");
 }
 
@@ -1496,7 +1505,10 @@ TEST(pool_opts_t, scrub_min_interval) {
 
   pool_opts_t opts;
   EXPECT_FALSE(opts.is_set(pool_opts_t::SCRUB_MIN_INTERVAL));
-  EXPECT_DEATH(opts.get(pool_opts_t::SCRUB_MIN_INTERVAL), "");
+  {
+    PrCtl unset_dumpable;
+    EXPECT_DEATH(opts.get(pool_opts_t::SCRUB_MIN_INTERVAL), "");
+  }
   double val;
   EXPECT_FALSE(opts.get(pool_opts_t::SCRUB_MIN_INTERVAL, &val));
   opts.set(pool_opts_t::SCRUB_MIN_INTERVAL, static_cast<double>(2015));
@@ -1514,7 +1526,10 @@ TEST(pool_opts_t, scrub_max_interval) {
 
   pool_opts_t opts;
   EXPECT_FALSE(opts.is_set(pool_opts_t::SCRUB_MAX_INTERVAL));
-  EXPECT_DEATH(opts.get(pool_opts_t::SCRUB_MAX_INTERVAL), "");
+  {
+    PrCtl unset_dumpable;
+    EXPECT_DEATH(opts.get(pool_opts_t::SCRUB_MAX_INTERVAL), "");
+  }
   double val;
   EXPECT_FALSE(opts.get(pool_opts_t::SCRUB_MAX_INTERVAL, &val));
   opts.set(pool_opts_t::SCRUB_MAX_INTERVAL, static_cast<double>(2015));
@@ -1532,7 +1547,10 @@ TEST(pool_opts_t, deep_scrub_interval) {
 
   pool_opts_t opts;
   EXPECT_FALSE(opts.is_set(pool_opts_t::DEEP_SCRUB_INTERVAL));
-  EXPECT_DEATH(opts.get(pool_opts_t::DEEP_SCRUB_INTERVAL), "");
+  {
+    PrCtl unset_dumpable;
+    EXPECT_DEATH(opts.get(pool_opts_t::DEEP_SCRUB_INTERVAL), "");
+  }
   double val;
   EXPECT_FALSE(opts.get(pool_opts_t::DEEP_SCRUB_INTERVAL, &val));
   opts.set(pool_opts_t::DEEP_SCRUB_INTERVAL, static_cast<double>(2015));
index afcfbb7d645f8ce9d71c65af6c92d067ed48a700..e4b1b17123473ceab2bed107df2e039855beccd7 100644 (file)
@@ -2,6 +2,7 @@
 #include "common/signal.h"
 #include "global/signal_handler.h"
 #include "common/debug.h"
+#include "include/coredumpctl.h"
 
 #include "gtest/gtest.h"
 
@@ -118,7 +119,10 @@ TEST(SignalHandler, Multiple)
 TEST(SignalHandler, LogInternal)
 {
   g_ceph_context->_log->inject_segv();
-  ASSERT_DEATH(derr << "foo" << dendl, ".*");
+  {
+    PrCtl unset_dumpable;
+    ASSERT_DEATH(derr << "foo" << dendl, ".*");
+  }
   g_ceph_context->_log->reset_segv();
 }
 
index 60b0a1ceb9b464242fbceb7824a6a05da18c3095..0d439d16c9c9b1a369f595d72fffb9afdc9ed5a4 100644 (file)
@@ -15,6 +15,7 @@
 #include "common/TextTable.h"
 #include <iostream>
 #include "gtest/gtest.h"
+#include "include/coredumpctl.h"
 
 TEST(TextTable, Alignment) {
   TextTable t;
@@ -72,5 +73,6 @@ TEST(TextTable, TooManyItems) {
   t.define_column("3", TextTable::LEFT, TextTable::LEFT);
 
   // expect assertion failure on this, which throws FailedAssertion
+  PrCtl unset_dumpable;
   ASSERT_DEATH((t << "1" << "2" << "3" << "4" << TextTable::endrow), "");
 }