Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / log / test.cc
1 #include <gtest/gtest.h>
2
3 #include "log/Log.h"
4 #include "common/Clock.h"
5 #include "common/PrebufferedStreambuf.h"
6 #include "include/coredumpctl.h"
7 #include "SubsystemMap.h"
8
9 using namespace ceph::logging;
10
11 TEST(Log, Simple)
12 {
13   SubsystemMap subs;
14   subs.add(0, "none", 10, 10);
15   subs.add(1, "foosys", 20, 1);
16   subs.add(2, "bar", 20, 2);
17   subs.add(3, "baz", 10, 3);
18
19   Log log(&subs);
20   log.start();
21  
22   log.set_log_file("/tmp/foo");
23   log.reopen_log_file();
24
25   log.set_stderr_level(5, -1);
26
27
28   for (int i=0; i<100; i++) {
29     int sys = i % 4;
30     int l = 5 + (i%4);
31     if (subs.should_gather(sys, l)) {
32       Entry *e = new Entry(ceph_clock_now(),
33                            pthread_self(),
34                            l,
35                            sys,
36                            "hello world");
37       log.submit_entry(e);
38     }
39   }
40   
41   log.flush();
42
43   log.dump_recent();
44
45   log.stop();
46 }
47
48 int many = 10000;
49
50 TEST(Log, ManyNoGather)
51 {
52   SubsystemMap subs;
53   subs.add(1, "foo", 1, 1);
54   Log log(&subs);
55   log.start();
56   log.set_log_file("/tmp/big");
57   log.reopen_log_file();
58   for (int i=0; i<many; i++) {
59     int l = 10;
60     if (subs.should_gather(1, l))
61       log.submit_entry(new Entry(ceph_clock_now(), pthread_self(), l, 1));
62   }
63   log.flush();
64   log.stop();
65 }
66
67
68 TEST(Log, ManyGatherLog)
69 {
70   SubsystemMap subs;
71   subs.add(1, "foo", 20, 10);
72   Log log(&subs);
73   log.start();
74   log.set_log_file("/tmp/big");
75   log.reopen_log_file();
76   for (int i=0; i<many; i++) {
77     int l = 10;
78     if (subs.should_gather(1, l))
79       log.submit_entry(new Entry(ceph_clock_now(), pthread_self(), l, 1,
80                                  "this is a long string asdf asdf asdf asdf asdf asdf asd fasd fasdf "));
81   }
82   log.flush();
83   log.stop();
84 }
85
86 TEST(Log, ManyGatherLogStringAssign)
87 {
88   SubsystemMap subs;
89   subs.add(1, "foo", 20, 10);
90   Log log(&subs);
91   log.start();
92   log.set_log_file("/tmp/big");
93   log.reopen_log_file();
94   for (int i=0; i<many; i++) {
95     int l = 10;
96     if (subs.should_gather(1, l)) {
97       Entry *e = new Entry(ceph_clock_now(), pthread_self(), l, 1);
98       ostringstream oss;
99       oss << "this i a long stream asdf asdf asdf asdf asdf asdf asdf asdf asdf as fd";
100       e->set_str(oss.str());
101       log.submit_entry(e);
102     }
103   }
104   log.flush();
105   log.stop();
106 }
107 TEST(Log, ManyGatherLogStringAssignWithReserve)
108 {
109   SubsystemMap subs;
110   subs.add(1, "foo", 20, 10);
111   Log log(&subs);
112   log.start();
113   log.set_log_file("/tmp/big");
114   log.reopen_log_file();
115   for (int i=0; i<many; i++) {
116     int l = 10;
117     if (subs.should_gather(1, l)) {
118       Entry *e = new Entry(ceph_clock_now(), pthread_self(), l, 1);
119       ostringstream oss;
120       oss.str().reserve(80);
121       oss << "this i a long stream asdf asdf asdf asdf asdf asdf asdf asdf asdf as fd";
122       e->set_str(oss.str());
123       log.submit_entry(e);
124     }
125   }
126   log.flush();
127   log.stop();
128 }
129
130 TEST(Log, ManyGatherLogPrebuf)
131 {
132   SubsystemMap subs;
133   subs.add(1, "foo", 20, 10);
134   Log log(&subs);
135   log.start();
136   log.set_log_file("/tmp/big");
137   log.reopen_log_file();
138   for (int i=0; i<many; i++) {
139     int l = 10;
140     if (subs.should_gather(1, l)) {
141       Entry *e = new Entry(ceph_clock_now(), pthread_self(), l, 1);
142       PrebufferedStreambuf psb(e->m_static_buf, sizeof(e->m_static_buf));
143       ostream oss(&psb);
144       oss << "this i a long stream asdf asdf asdf asdf asdf asdf asdf asdf asdf as fd";
145       //e->m_str = oss.str();
146       log.submit_entry(e);
147     }
148   }
149   log.flush();
150   log.stop();
151 }
152
153 TEST(Log, ManyGatherLogPrebufOverflow)
154 {
155   SubsystemMap subs;
156   subs.add(1, "foo", 20, 10);
157   Log log(&subs);
158   log.start();
159   log.set_log_file("/tmp/big");
160   log.reopen_log_file();
161   for (int i=0; i<many; i++) {
162     int l = 10;
163     if (subs.should_gather(1, l)) {
164       Entry *e = new Entry(ceph_clock_now(), pthread_self(), l, 1);
165       PrebufferedStreambuf psb(e->m_static_buf, sizeof(e->m_static_buf));
166       ostream oss(&psb);
167       oss << "this i a long stream asdf asdf asdf asdf asdf asdf asdf asdf asdf as fd"
168           << std::string(sizeof(e->m_static_buf) * 2, '-') ;
169       //e->m_str = oss.str();
170       log.submit_entry(e);
171     }
172   }
173   log.flush();
174   log.stop();
175 }
176
177 TEST(Log, ManyGather)
178 {
179   SubsystemMap subs;
180   subs.add(1, "foo", 20, 1);
181   Log log(&subs);
182   log.start();
183   log.set_log_file("/tmp/big");
184   log.reopen_log_file();
185   for (int i=0; i<many; i++) {
186     int l = 10;
187     if (subs.should_gather(1, l))
188       log.submit_entry(new Entry(ceph_clock_now(), pthread_self(), l, 1));
189   }
190   log.flush();
191   log.stop();
192 }
193
194 void do_segv()
195 {
196   SubsystemMap subs;
197   subs.add(1, "foo", 20, 1);
198   Log log(&subs);
199   log.start();
200   log.set_log_file("/tmp/big");
201   log.reopen_log_file();
202
203   log.inject_segv();
204   Entry *e = new Entry(ceph_clock_now(), pthread_self(), 10, 1);
205   {
206     PrCtl unset_dumpable;
207     log.submit_entry(e);  // this should segv
208   }
209
210   log.flush();
211   log.stop();
212 }
213
214 TEST(Log, InternalSegv)
215 {
216   ASSERT_DEATH(do_segv(), ".*");
217 }
218
219 TEST(Log, LargeLog)
220 {
221   SubsystemMap subs;
222   subs.add(1, "foo", 20, 10);
223   Log log(&subs);
224   log.start();
225   log.set_log_file("/tmp/big");
226   log.reopen_log_file();
227   int l = 10;
228   Entry *e = new Entry(ceph_clock_now(), pthread_self(), l, 1);
229
230   std::string msg(10000000, 0);
231   e->set_str(msg);
232   log.submit_entry(e);
233   log.flush();
234   log.stop();
235 }