forked from facebookincubator/oomd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogTest.cpp
More file actions
99 lines (82 loc) · 2.75 KB
/
Copy pathLogTest.cpp
File metadata and controls
99 lines (82 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright (C) 2018-present, Facebook, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <fstream>
#include <sstream>
#include "oomd/Log.h"
#include "oomd/shared/OomdContext.h"
using namespace Oomd;
using namespace testing;
class LogTestKmsg : public ::testing::Test {
public:
std::string test_string{"Testing basic logger output!"};
std::string test_prefix{"oomd"};
std::string test_complex_string{
"oomd kill: 10.00 60.00 600.00 Evil 1999 detector:swap,12345MB killer:Evaporate "};
protected:
std::pair<std::unique_ptr<Log>, std::ifstream> get_logger_and_file() {
std::string outfile = "/tmp/logtest.XXXXXX";
int fd = ::mkstemp(&outfile[0]);
auto logger = Log::get_for_unittest(fd, std::cerr);
std::ifstream result_file(outfile);
if (::remove(outfile.c_str()) != 0) {
perror("remove");
}
return std::make_pair(std::move(logger), std::move(result_file));
}
};
/*
Test the plain kmsgLog(buf) interface.
*/
TEST_F(LogTestKmsg, VerifyOutputSimple) {
auto logger_and_file = get_logger_and_file();
auto& logger = logger_and_file.first;
auto& result_file = logger_and_file.second;
logger->kmsgLog(test_string, test_prefix);
/* check output */
std::string compare_string;
getline(result_file, compare_string);
/* verify log contents */
EXPECT_EQ(compare_string, test_prefix.append(": " + test_string));
}
TEST(LogTestAsyncDebug, CoupleLines) {
std::stringstream sstr;
{
auto logger = Log::get_for_unittest(STDERR_FILENO, sstr);
logger->debugLog(std::string("line one\n"));
logger->debugLog(std::string("line2\n"));
}
EXPECT_EQ(sstr.str(), "line one\nline2\n");
}
TEST(LogTestAsyncDebug, LotsOfLines) {
for (int i = 0; i < 10; ++i) {
std::stringstream expected;
std::stringstream sstr;
{
auto logger = Log::get_for_unittest(STDERR_FILENO, sstr);
for (int j = 0; j < 150; ++j) {
logger->debugLog(std::to_string(j) + "_line\n");
expected << j << "_line\n";
}
}
EXPECT_EQ(sstr.str(), expected.str());
}
}