Skip to content

Commit bfa83df

Browse files
committed
test: add unit test for bypassing abort in ceph_assert
Fixes: https://tracker.ceph.com/issues/70476 Signed-off-by: Radoslaw Zarzynski <[email protected]>
1 parent 100fe1a commit bfa83df

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/test/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,3 +1023,11 @@ add_executable(test_nvmeof_mon_encoding
10231023
target_link_libraries(test_nvmeof_mon_encoding
10241024
mon ceph-common global-static
10251025
)
1026+
1027+
if(NOT WIN32)
1028+
# unittest_ceph_assert
1029+
add_executable(unittest_ceph_assert
1030+
ceph_assert.cc)
1031+
add_ceph_unittest(unittest_ceph_assert)
1032+
target_link_libraries(unittest_ceph_assert ceph-common global)
1033+
endif()

src/test/ceph_assert.cc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2+
// vim: ts=8 sw=2 smarttab
3+
4+
#include <fmt/format.h>
5+
6+
#include "common/ceph_argparse.h"
7+
#include "common/ceph_context.h"
8+
#include "global/global_context.h"
9+
#include "global/global_init.h"
10+
11+
#include "include/ceph_assert.h"
12+
13+
#include "gtest/gtest.h"
14+
15+
static void non_supressed_assert_line16() {
16+
ceph_assert(42 == 41); // LINE16
17+
}
18+
static void supressed_assert_line19() {
19+
ceph_assert(42 == 40); // LINE19
20+
}
21+
static void supressed_assertf_line22() {
22+
ceph_assertf(42 == 39, "FAILED ceph_assertf"); // LINE22
23+
}
24+
static void non_supressed_assertf_line25() {
25+
ceph_assertf(42 == 38, "FAILED ceph_assertf"); // LINE25
26+
}
27+
28+
TEST(CephAssertDeathTest, ceph_assert_supresssions) {
29+
ASSERT_DEATH(non_supressed_assert_line16(), "FAILED ceph_assert");
30+
supressed_assert_line19();
31+
supressed_assertf_line22();
32+
ASSERT_DEATH(non_supressed_assertf_line25(), "FAILED ceph_assertf");
33+
}
34+
35+
int main(int argc, char **argv) {
36+
37+
auto args = argv_to_vec(argc, argv);
38+
auto cct = global_init(
39+
nullptr,
40+
args,
41+
CEPH_ENTITY_TYPE_CLIENT,
42+
CODE_ENVIRONMENT_UTILITY,
43+
CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
44+
g_ceph_context->_conf.set_val(
45+
"ceph_assert_supresssions",
46+
fmt::format(
47+
"{}:{}, {}:{}", __FILE__, /* LINE19 */19, __FILE__, /* LINE22 */22));
48+
common_init_finish(g_ceph_context);
49+
testing::InitGoogleTest(&argc, argv);
50+
return RUN_ALL_TESTS();
51+
}

0 commit comments

Comments
 (0)