1 // Copyright 2011 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/logging/operations.hpp"
38 #include <atf-c++.hpp>
40 #include "utils/datetime.hpp"
41 #include "utils/format/macros.hpp"
42 #include "utils/fs/operations.hpp"
43 #include "utils/fs/path.hpp"
45 namespace datetime
= utils::datetime
;
46 namespace fs
= utils::fs
;
47 namespace logging
= utils::logging
;
50 ATF_TEST_CASE_WITHOUT_HEAD(generate_log_name__before_log
);
51 ATF_TEST_CASE_BODY(generate_log_name__before_log
)
53 datetime::set_mock_now(2011, 2, 21, 18, 10, 0, 0);
54 ATF_REQUIRE_EQ(fs::path("/some/dir/foobar.20110221-181000.log"),
55 logging::generate_log_name(fs::path("/some/dir"), "foobar"));
57 datetime::set_mock_now(2011, 2, 21, 18, 10, 1, 987654);
58 logging::log(logging::level_info
, "file", 123, "A message");
60 datetime::set_mock_now(2011, 2, 21, 18, 10, 2, 123);
61 ATF_REQUIRE_EQ(fs::path("/some/dir/foobar.20110221-181000.log"),
62 logging::generate_log_name(fs::path("/some/dir"), "foobar"));
66 ATF_TEST_CASE_WITHOUT_HEAD(generate_log_name__after_log
);
67 ATF_TEST_CASE_BODY(generate_log_name__after_log
)
69 datetime::set_mock_now(2011, 2, 21, 18, 15, 0, 0);
70 logging::log(logging::level_info
, "file", 123, "A message");
71 datetime::set_mock_now(2011, 2, 21, 18, 15, 1, 987654);
72 logging::log(logging::level_info
, "file", 123, "A message");
74 datetime::set_mock_now(2011, 2, 21, 18, 15, 2, 123);
75 ATF_REQUIRE_EQ(fs::path("/some/dir/foobar.20110221-181500.log"),
76 logging::generate_log_name(fs::path("/some/dir"), "foobar"));
78 datetime::set_mock_now(2011, 2, 21, 18, 15, 3, 1);
79 logging::log(logging::level_info
, "file", 123, "A message");
81 datetime::set_mock_now(2011, 2, 21, 18, 15, 4, 91);
82 ATF_REQUIRE_EQ(fs::path("/some/dir/foobar.20110221-181500.log"),
83 logging::generate_log_name(fs::path("/some/dir"), "foobar"));
87 ATF_TEST_CASE_WITHOUT_HEAD(log
);
88 ATF_TEST_CASE_BODY(log
)
90 logging::set_inmemory();
92 datetime::set_mock_now(2011, 2, 21, 18, 10, 0, 0);
93 logging::log(logging::level_debug
, "f1", 1, "Debug message");
95 datetime::set_mock_now(2011, 2, 21, 18, 10, 1, 987654);
96 logging::log(logging::level_error
, "f2", 2, "Error message");
98 logging::set_persistency("debug", fs::path("test.log"));
100 datetime::set_mock_now(2011, 2, 21, 18, 10, 2, 123);
101 logging::log(logging::level_info
, "f3", 3, "Info message");
103 datetime::set_mock_now(2011, 2, 21, 18, 10, 3, 456);
104 logging::log(logging::level_warning
, "f4", 4, "Warning message");
106 std::ifstream
input("test.log");
109 const pid_t pid
= ::getpid();
112 ATF_REQUIRE(std::getline(input
, line
).good());
114 (F("20110221-181000 D %s f1:1: Debug message") % pid
).str(), line
);
115 ATF_REQUIRE(std::getline(input
, line
).good());
117 (F("20110221-181001 E %s f2:2: Error message") % pid
).str(), line
);
118 ATF_REQUIRE(std::getline(input
, line
).good());
120 (F("20110221-181002 I %s f3:3: Info message") % pid
).str(), line
);
121 ATF_REQUIRE(std::getline(input
, line
).good());
123 (F("20110221-181003 W %s f4:4: Warning message") % pid
).str(), line
);
127 ATF_TEST_CASE_WITHOUT_HEAD(set_persistency__no_backlog
);
128 ATF_TEST_CASE_BODY(set_persistency__no_backlog
)
130 logging::set_persistency("debug", fs::path("test.log"));
132 datetime::set_mock_now(2011, 2, 21, 18, 20, 0, 654321);
133 logging::log(logging::level_debug
, "file", 123, "Debug message");
135 std::ifstream
input("test.log");
138 const pid_t pid
= ::getpid();
141 ATF_REQUIRE(std::getline(input
, line
).good());
143 (F("20110221-182000 D %s file:123: Debug message") % pid
).str(), line
);
147 /// Creates a log for testing purposes, buffering messages on start.
149 /// \param level The level of the desired log.
150 /// \param path The output file.
152 create_log(const std::string
& level
, const std::string
& path
)
154 logging::set_inmemory();
156 datetime::set_mock_now(2011, 3, 19, 11, 40, 0, 100);
157 logging::log(logging::level_debug
, "file1", 11, "Debug 1");
159 datetime::set_mock_now(2011, 3, 19, 11, 40, 1, 200);
160 logging::log(logging::level_error
, "file2", 22, "Error 1");
162 datetime::set_mock_now(2011, 3, 19, 11, 40, 2, 300);
163 logging::log(logging::level_info
, "file3", 33, "Info 1");
165 datetime::set_mock_now(2011, 3, 19, 11, 40, 3, 400);
166 logging::log(logging::level_warning
, "file4", 44, "Warning 1");
168 logging::set_persistency(level
, fs::path(path
));
170 datetime::set_mock_now(2011, 3, 19, 11, 40, 4, 500);
171 logging::log(logging::level_debug
, "file1", 11, "Debug 2");
173 datetime::set_mock_now(2011, 3, 19, 11, 40, 5, 600);
174 logging::log(logging::level_error
, "file2", 22, "Error 2");
176 datetime::set_mock_now(2011, 3, 19, 11, 40, 6, 700);
177 logging::log(logging::level_info
, "file3", 33, "Info 2");
179 datetime::set_mock_now(2011, 3, 19, 11, 40, 7, 800);
180 logging::log(logging::level_warning
, "file4", 44, "Warning 2");
184 ATF_TEST_CASE_WITHOUT_HEAD(set_persistency__some_backlog__debug
);
185 ATF_TEST_CASE_BODY(set_persistency__some_backlog__debug
)
187 create_log("debug", "test.log");
189 std::ifstream
input("test.log");
192 const pid_t pid
= ::getpid();
195 ATF_REQUIRE(std::getline(input
, line
).good());
197 (F("20110319-114000 D %s file1:11: Debug 1") % pid
).str(), line
);
198 ATF_REQUIRE(std::getline(input
, line
).good());
200 (F("20110319-114001 E %s file2:22: Error 1") % pid
).str(), line
);
201 ATF_REQUIRE(std::getline(input
, line
).good());
203 (F("20110319-114002 I %s file3:33: Info 1") % pid
).str(), line
);
204 ATF_REQUIRE(std::getline(input
, line
).good());
206 (F("20110319-114003 W %s file4:44: Warning 1") % pid
).str(), line
);
207 ATF_REQUIRE(std::getline(input
, line
).good());
209 (F("20110319-114004 D %s file1:11: Debug 2") % pid
).str(), line
);
210 ATF_REQUIRE(std::getline(input
, line
).good());
212 (F("20110319-114005 E %s file2:22: Error 2") % pid
).str(), line
);
213 ATF_REQUIRE(std::getline(input
, line
).good());
215 (F("20110319-114006 I %s file3:33: Info 2") % pid
).str(), line
);
216 ATF_REQUIRE(std::getline(input
, line
).good());
218 (F("20110319-114007 W %s file4:44: Warning 2") % pid
).str(), line
);
222 ATF_TEST_CASE_WITHOUT_HEAD(set_persistency__some_backlog__error
);
223 ATF_TEST_CASE_BODY(set_persistency__some_backlog__error
)
225 create_log("error", "test.log");
227 std::ifstream
input("test.log");
230 const pid_t pid
= ::getpid();
233 ATF_REQUIRE(std::getline(input
, line
).good());
235 (F("20110319-114001 E %s file2:22: Error 1") % pid
).str(), line
);
236 ATF_REQUIRE(std::getline(input
, line
).good());
238 (F("20110319-114005 E %s file2:22: Error 2") % pid
).str(), line
);
242 ATF_TEST_CASE_WITHOUT_HEAD(set_persistency__some_backlog__info
);
243 ATF_TEST_CASE_BODY(set_persistency__some_backlog__info
)
245 create_log("info", "test.log");
247 std::ifstream
input("test.log");
250 const pid_t pid
= ::getpid();
253 ATF_REQUIRE(std::getline(input
, line
).good());
255 (F("20110319-114001 E %s file2:22: Error 1") % pid
).str(), line
);
256 ATF_REQUIRE(std::getline(input
, line
).good());
258 (F("20110319-114002 I %s file3:33: Info 1") % pid
).str(), line
);
259 ATF_REQUIRE(std::getline(input
, line
).good());
261 (F("20110319-114003 W %s file4:44: Warning 1") % pid
).str(), line
);
262 ATF_REQUIRE(std::getline(input
, line
).good());
264 (F("20110319-114005 E %s file2:22: Error 2") % pid
).str(), line
);
265 ATF_REQUIRE(std::getline(input
, line
).good());
267 (F("20110319-114006 I %s file3:33: Info 2") % pid
).str(), line
);
268 ATF_REQUIRE(std::getline(input
, line
).good());
270 (F("20110319-114007 W %s file4:44: Warning 2") % pid
).str(), line
);
274 ATF_TEST_CASE_WITHOUT_HEAD(set_persistency__some_backlog__warning
);
275 ATF_TEST_CASE_BODY(set_persistency__some_backlog__warning
)
277 create_log("warning", "test.log");
279 std::ifstream
input("test.log");
282 const pid_t pid
= ::getpid();
285 ATF_REQUIRE(std::getline(input
, line
).good());
287 (F("20110319-114001 E %s file2:22: Error 1") % pid
).str(), line
);
288 ATF_REQUIRE(std::getline(input
, line
).good());
290 (F("20110319-114003 W %s file4:44: Warning 1") % pid
).str(), line
);
291 ATF_REQUIRE(std::getline(input
, line
).good());
293 (F("20110319-114005 E %s file2:22: Error 2") % pid
).str(), line
);
294 ATF_REQUIRE(std::getline(input
, line
).good());
296 (F("20110319-114007 W %s file4:44: Warning 2") % pid
).str(), line
);
300 ATF_TEST_CASE(set_persistency__fail
);
301 ATF_TEST_CASE_HEAD(set_persistency__fail
)
303 set_md_var("require.user", "unprivileged");
305 ATF_TEST_CASE_BODY(set_persistency__fail
)
307 ATF_REQUIRE_THROW_RE(std::range_error
, "'foobar'",
308 logging::set_persistency("foobar", fs::path("log")));
310 fs::mkdir(fs::path("dir"), 0644);
311 ATF_REQUIRE_THROW_RE(std::runtime_error
, "dir/fail.log",
312 logging::set_persistency("debug",
313 fs::path("dir/fail.log")));
317 ATF_INIT_TEST_CASES(tcs
)
319 ATF_ADD_TEST_CASE(tcs
, generate_log_name__before_log
);
320 ATF_ADD_TEST_CASE(tcs
, generate_log_name__after_log
);
322 ATF_ADD_TEST_CASE(tcs
, log
);
324 ATF_ADD_TEST_CASE(tcs
, set_persistency__no_backlog
);
325 ATF_ADD_TEST_CASE(tcs
, set_persistency__some_backlog__debug
);
326 ATF_ADD_TEST_CASE(tcs
, set_persistency__some_backlog__error
);
327 ATF_ADD_TEST_CASE(tcs
, set_persistency__some_backlog__info
);
328 ATF_ADD_TEST_CASE(tcs
, set_persistency__some_backlog__warning
);
329 ATF_ADD_TEST_CASE(tcs
, set_persistency__fail
);