summaryrefslogtreecommitdiff
path: root/test/redfish-core/include/event_log_test.cpp
blob: c51eee6ec3aadd6b9b6ba750989d7cf9c3ef8bfd (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "event_log.hpp"

#include <nlohmann/json.hpp>

#include <cerrno>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <string>
#include <string_view>
#include <vector>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace redfish::event_log
{
namespace
{

TEST(RedfishEventLog, GetUniqueEntryIDSuccess)
{
    bool success = false;
    std::string entryID;
    std::string example = "2000-01-02T03:04:05";
    success = getUniqueEntryID(example, entryID);

    ASSERT_EQ(success, true);

    // assert the prefix since the specific number can depend on timezone
    ASSERT_TRUE(entryID.starts_with("946"));
}

TEST(RedfishEventLog, GetUniqueEntryIDUnique)
{
    bool success = false;
    std::string entryID1;
    std::string entryID2;
    std::string example = "2000-08-02T03:04:05";

    success = getUniqueEntryID(example, entryID1);
    ASSERT_EQ(success, true);
    success = getUniqueEntryID(example, entryID2);
    ASSERT_EQ(success, true);

    // when calling a second time with the same argument
    // there should be an underscore
    ASSERT_TRUE(entryID2.contains("_"));

    // only one '_' allowed
    ASSERT_THAT(entryID2, testing::MatchesRegex("^[0-9]+_[0-9]+$"));
}

TEST(RedfishEventLog, GetUniqueEntryIDIndex)
{
    std::string entryID1;
    std::string entryID2;
    std::string entryID3;
    std::string example = "2000-08-02T03:04:05";

    getUniqueEntryID(example, entryID1);
    getUniqueEntryID(example, entryID2);
    getUniqueEntryID(example, entryID3);

    const size_t index = entryID2.find('_');

    ASSERT_NE(index, std::string::npos);

    const long n1 = std::stol(entryID2.substr(index + 1));

    // unique index for repeated timestamp is >= 0
    ASSERT_GE(n1, 0);

    const long n2 = std::stol(entryID3.substr(entryID3.find('_') + 1));

    // unique index is monotonic increasing
    ASSERT_TRUE(n2 > n1);
}

TEST(RedfishEventLog, GetEventLogParamsSuccess)
{
    int status = 0;
    std::string logEntry = "32938 3,hello";

    std::string timestamp;
    std::string messageID;
    std::vector<std::string> messageArgs;

    status = getEventLogParams(logEntry, timestamp, messageID, messageArgs);

    ASSERT_EQ(status, 0);

    ASSERT_EQ(timestamp, "32938");
    ASSERT_EQ(messageID, "3");
    ASSERT_EQ(messageArgs.size(), 1);
    ASSERT_EQ(messageArgs[0], "hello");
}

TEST(RedfishEventLog, GetEventLogParamsFailNoTimestamp)
{
    int status = 0;
    std::string logEntry = "3,hello";

    std::string timestamp;
    std::string messageID;
    std::vector<std::string> messageArgs;

    status = getEventLogParams(logEntry, timestamp, messageID, messageArgs);

    ASSERT_EQ(status, -EINVAL);
}

TEST(RedfishEventLog, GetEventLogParamsFailNoComma)
{
    int status = 0;
    std::string logEntry = "malformed";

    std::string timestamp;
    std::string messageID;
    std::vector<std::string> messageArgs;

    status = getEventLogParams(logEntry, timestamp, messageID, messageArgs);

    ASSERT_EQ(status, -EINVAL);
}

TEST(RedfishEventLog, FormatEventLogEntrySuccess)
{
    int status = 0;
    uint64_t eventId = 0;
    std::string logEntryID = "23849423_3";
    std::string messageID = "OpenBMC.0.1.PowerSupplyFanFailed";
    std::vector<std::string_view> messageArgs = {"PSU 1", "FAN 2"};
    std::string timestamp = "my-timestamp";
    std::string customText = "customText";

    nlohmann::json::object_t logEntryJson;
    status = formatEventLogEntry(eventId, logEntryID, messageID, messageArgs,
                                 timestamp, customText, logEntryJson);

    ASSERT_EQ(status, 0);

    ASSERT_TRUE(logEntryJson.contains("EventId"));
    ASSERT_EQ(logEntryJson["EventId"], "0");

    ASSERT_TRUE(logEntryJson.contains("Message"));
    ASSERT_EQ(logEntryJson["Message"], "Power supply PSU 1 fan FAN 2 failed.");

    ASSERT_TRUE(logEntryJson.contains("MessageId"));
    ASSERT_EQ(logEntryJson["MessageId"], "OpenBMC.0.1.PowerSupplyFanFailed");

    ASSERT_TRUE(logEntryJson.contains("MessageArgs"));
    ASSERT_EQ(logEntryJson["MessageArgs"].size(), 2);
    ASSERT_EQ(logEntryJson["MessageArgs"][0], "PSU 1");
    ASSERT_EQ(logEntryJson["MessageArgs"][1], "FAN 2");

    ASSERT_TRUE(logEntryJson.contains("EventTimestamp"));

    // May need to fix this, it should not pass like this.
    ASSERT_EQ(logEntryJson["EventTimestamp"], "my-timestamp");

    ASSERT_TRUE(logEntryJson.contains("Context"));
    ASSERT_EQ(logEntryJson["Context"], "customText");
}

TEST(RedfishEventLog, FormatEventLogEntryFail)
{
    int status = 0;
    uint64_t eventId = 0;
    std::string logEntryID = "malformed";
    std::string messageID;
    std::vector<std::string_view> messageArgs;
    std::string timestamp;
    std::string customText;

    nlohmann::json::object_t logEntryJson;
    status = formatEventLogEntry(eventId, logEntryID, messageID, messageArgs,
                                 timestamp, customText, logEntryJson);

    ASSERT_EQ(status, -1);
}

} // namespace
} // namespace redfish::event_log