aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/test/log_message/log_message_test.cpp
blob: 02e58feca947f40c36971de86f3280a5ed44a464 (plain) (blame)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <gtest/gtest.h>
#include <vespa/log/log_message.h>
#include <vespa/log/exceptions.h>

using LogLevel = ns_log::Logger::LogLevel;

namespace ns_log {

namespace {

void
assertParseFail(std::string exp_what, std::string log_line)
{
    LogMessage message;
    try {
        message.parse_log_line(log_line);
        EXPECT_TRUE(false) << "Exception not thrown";
    } catch (BadLogLineException &e) {
        EXPECT_EQ(exp_what, e.what());
    }
}

}

class LogMessageTest : public ::testing::Test {
public:
    LogMessageTest() { }
    ~LogMessageTest() { }
};

TEST_F(LogMessageTest, require_that_plain_entry_is_ok)
{
    std::string log_line = "10.5\tlocalhost\t10/20\ttest\ttestrunner\twarning\thello world";
    LogMessage message;
    message.parse_log_line(log_line);
    EXPECT_EQ(INT64_C(10500000000), message.time_nanos());
    EXPECT_EQ("localhost", message.hostname());
    EXPECT_EQ(10, message.process_id());
    EXPECT_EQ(20, message.thread_id());
    EXPECT_EQ("test", message.service());
    EXPECT_EQ("testrunner", message.component());
    EXPECT_EQ(LogLevel::warning, message.level());
    EXPECT_EQ("hello world", message.payload());
}

TEST_F(LogMessageTest, require_that_missing_thread_id_is_ok)
{
    std::string log_line = "10.5\tlocalhost\t10\ttest\ttestrunner\twarning\thello world";
    LogMessage message;
    message.parse_log_line(log_line);
    EXPECT_EQ(10, message.process_id());
    EXPECT_EQ(0, message.thread_id());
}

TEST_F(LogMessageTest, require_that_empty_line_fails)
{
    assertParseFail("Bad 1st tab: ", "");
}

TEST_F(LogMessageTest, require_that_tab_at_start_of_line_fails)
{
    assertParseFail("Bad 1st tab: \t", "\t");
}

TEST_F(LogMessageTest, require_that_no_tab_after_time_fails)
{
    assertParseFail("Bad 1st tab: 10", "10");
}

TEST_F(LogMessageTest, require_that_malformed_time_fails)
{
    assertParseFail("Bad time field: 10x", "10x\t");
}

TEST_F(LogMessageTest, require_that_no_tab_after_hostname_fails)
{
    std::string log_line = "10\tlocalhost";
    assertParseFail(std::string("Bad 2nd tab: ") + log_line, log_line);
}

TEST_F(LogMessageTest, require_that_no_tab_after_pid_fails)
{
    std::string log_line = "10\tlocalhost\t10/20";
    assertParseFail(std::string("Bad 3rd tab: ") + log_line, log_line);
}

TEST_F(LogMessageTest, require_that_malformed_pid_fails)
{
    assertParseFail("Bad pid field: x", "10\tlocalhost\tx\t");
}

TEST_F(LogMessageTest, require_that_malformed_pid_fails_again)
{
    assertParseFail("Bad pid field: 10/", "10\tlocalhost\t10/\t");
}

TEST_F(LogMessageTest, require_that_no_tab_after_service_fails)
{
    std::string log_line = "10\tlocalhost\t10\t";
    assertParseFail(std::string("Bad 4th tab: ") + log_line, log_line);
}

TEST_F(LogMessageTest, require_that_no_tab_after_component_fails)
{
    std::string log_line = "10\tlocalhost\t10\ttest\t";
    assertParseFail(std::string("Bad 5th tab: ") + log_line, log_line);
}

TEST_F(LogMessageTest, require_that_empty_component_fails)
{
    std::string log_line = "10\tlocalhost\t10\ttest\t\t";
    assertParseFail(std::string("Bad 5th tab: ") + log_line, log_line);
}

TEST_F(LogMessageTest, require_that_no_tab_after_level_fails)
{
    std::string log_line = "10\tlocalhost\t10\ttest\ttestrunner\t";
    assertParseFail(std::string("Bad 6th tab: ") + log_line, log_line);
}

TEST_F(LogMessageTest, require_that_empty_level_fails)
{
    std::string log_line = "10\tlocalhost\t10\ttest\ttestrunner\t\t";
    assertParseFail(std::string("Bad 6th tab: ") + log_line, log_line);
}

TEST_F(LogMessageTest, require_that_empty_payload_is_ok)
{
    std::string log_line = "10\tlocalhost\t10\ttest\ttestrunner\twarning\t";
    LogMessage message;
    message.parse_log_line(log_line);
    EXPECT_EQ(std::string(""), message.payload());
}

TEST_F(LogMessageTest, require_that_nonempty_payload_is_ok)
{
    std::string log_line = "10\tlocalhost\t10\ttest\ttestrunner\twarning\thi";
    LogMessage message;
    message.parse_log_line(log_line);
    EXPECT_EQ(std::string("hi"), message.payload());
}

}

int
main(int argc, char* argv[])
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}