Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / common / test_back_trace.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <boost/algorithm/string.hpp>
5 // std::regex support in libstdc++ 4.8 is incomplete, so let's stick to
6 // boost::regex now.
7 #include <boost/regex.hpp>
8 #include <gtest/gtest.h>
9 #include <sstream>
10 #include <string>
11
12 #include "common/BackTrace.h"
13 #include "common/version.h"
14
15 // a dummy function, so we can check "foo" in the backtrace.
16 // do not mark this function as static or put it into an anonymous namespace,
17 // otherwise it's function name will be removed in the backtrace.
18 std::string foo()
19 {
20   std::ostringstream oss;
21   oss << ceph::BackTrace(1);
22   return oss.str();
23 }
24
25 // a typical backtrace looks like:
26 //
27 // ceph version Development (no_version)
28 // 1: (foo[abi:cxx11]()+0x4a) [0x5562231cf22a]
29 // 2: (BackTrace_Basic_Test::TestBody()+0x28) [0x5562231cf2fc]
30 TEST(BackTrace, Basic) {
31   std::string bt = foo();
32   std::vector<std::string> lines;
33   boost::split(lines, bt, boost::is_any_of("\n"));
34   const unsigned lineno = 1;
35   ASSERT_GT(lines.size(), lineno);
36   ASSERT_EQ(lines[0].find(pretty_version_to_str()), 1U);
37   boost::regex e{"^ 1: "
38 #ifdef __FreeBSD__
39                  "<foo.*>\\s"
40                  "at\\s.*$"};
41 #else
42                  "\\(foo.*\\)\\s"
43                  "\\[0x[[:xdigit:]]+\\]$"};
44 #endif
45   EXPECT_TRUE(boost::regex_match(lines[lineno], e));
46 }