blob: 2107725aca9493c6e23b1843cc6e61167d8a475e [file] [log] [blame]
Matt Spinler367144c2019-09-19 15:33:52 -05001#include "extensions/openpower-pels/registry.hpp"
2
3#include <filesystem>
4#include <fstream>
5#include <nlohmann/json.hpp>
6
7#include <gtest/gtest.h>
8
9using namespace openpower::pels::message;
10namespace fs = std::filesystem;
11
12const auto registryData = R"(
13{
14 "PELs":
15 [
16 {
17 "Name": "xyz.openbmc_project.Power.Fault",
18 "Subsystem": "power_supply",
19 "ActionFlags": ["service_action", "report"],
20
21 "SRC":
22 {
23 "ReasonCode": "0x2030"
24 }
25 },
26
27 {
28 "Name": "xyz.openbmc_project.Power.OverVoltage",
29 "Subsystem": "power_control_hw",
30 "Severity": "unrecoverable",
31 "MfgSeverity": "non_error",
32 "ActionFlags": ["service_action", "report", "call_home"],
33 "MfgActionFlags": ["hidden"],
34
35 "SRC":
36 {
37 "ReasonCode": "0x2333",
38 "Type": "BD",
39 "SymptomIDFields": ["SRCWord5", "SRCWord6", "SRCWord7"],
40 "PowerFault": true,
41 "Words6To9":
42 {
43 "6":
44 {
45 "description": "Failing unit number",
46 "AdditionalDataPropSource": "PS_NUM"
47 },
48
49 "7":
50 {
51 "description": "bad voltage",
52 "AdditionalDataPropSource": "VOLTAGE"
53 }
54 }
55 }
56 }
57 ]
58}
59)";
60
61class RegistryTest : public ::testing::Test
62{
63 protected:
64 static void SetUpTestCase()
65 {
66 char path[] = "/tmp/regtestXXXXXX";
67 regDir = mkdtemp(path);
68 }
69
70 static void TearDownTestCase()
71 {
72 fs::remove_all(regDir);
73 }
74
75 static std::string writeData(const char* data)
76 {
77 fs::path path = regDir / "registry.json";
78 std::ofstream stream{path};
79 stream << data;
80 return path;
81 }
82
83 static fs::path regDir;
84};
85
86fs::path RegistryTest::regDir{};
87
88TEST_F(RegistryTest, TestNoEntry)
89{
90 auto path = RegistryTest::writeData(registryData);
91 Registry registry{path};
92
93 auto entry = registry.lookup("foo");
94 EXPECT_FALSE(entry);
95}
96
97TEST_F(RegistryTest, TestFindEntry)
98{
99 auto path = RegistryTest::writeData(registryData);
100 Registry registry{path};
101
102 auto entry = registry.lookup("xyz.openbmc_project.Power.OverVoltage");
103 ASSERT_TRUE(entry);
104 EXPECT_EQ(entry->name, "xyz.openbmc_project.Power.OverVoltage");
105 EXPECT_EQ(entry->subsystem, 0x62);
106 EXPECT_EQ(*(entry->severity), 0x40);
107 EXPECT_EQ(*(entry->mfgSeverity), 0x00);
108 EXPECT_EQ(entry->actionFlags, 0xA800);
109 EXPECT_EQ(*(entry->mfgActionFlags), 0x4000);
110 EXPECT_FALSE(entry->eventType);
111 EXPECT_FALSE(entry->eventScope);
112
113 // TODO: compare SRC fields
114}
115
116// Check the entry that mostly uses defaults
117TEST_F(RegistryTest, TestFindEntryMinimal)
118{
119 auto path = RegistryTest::writeData(registryData);
120 Registry registry{path};
121
122 auto entry = registry.lookup("xyz.openbmc_project.Power.Fault");
123 ASSERT_TRUE(entry);
124 EXPECT_EQ(entry->name, "xyz.openbmc_project.Power.Fault");
125 EXPECT_EQ(entry->subsystem, 0x61);
126 EXPECT_FALSE(entry->severity);
127 EXPECT_FALSE(entry->mfgSeverity);
128 EXPECT_FALSE(entry->mfgActionFlags);
129 EXPECT_EQ(entry->actionFlags, 0xA000);
130 EXPECT_FALSE(entry->eventType);
131 EXPECT_FALSE(entry->eventScope);
132}
133
134TEST_F(RegistryTest, TestBadJSON)
135{
136 auto path = RegistryTest::writeData("bad {} json");
137
138 Registry registry{path};
139
140 EXPECT_FALSE(registry.lookup("foo"));
141}
142
143// Test the helper functions the use the pel_values data.
144TEST_F(RegistryTest, TestHelperFunctions)
145{
146 using namespace openpower::pels::message::helper;
147 EXPECT_EQ(getSubsystem("input_power_source"), 0xA1);
148 EXPECT_THROW(getSubsystem("foo"), std::runtime_error);
149
150 EXPECT_EQ(getSeverity("symptom_recovered"), 0x71);
151 EXPECT_THROW(getSeverity("foo"), std::runtime_error);
152
153 EXPECT_EQ(getEventType("dump_notification"), 0x08);
154 EXPECT_THROW(getEventType("foo"), std::runtime_error);
155
156 EXPECT_EQ(getEventScope("possibly_multiple_platforms"), 0x04);
157 EXPECT_THROW(getEventScope("foo"), std::runtime_error);
158
159 std::vector<std::string> flags{"service_action", "dont_report",
160 "termination"};
161 EXPECT_EQ(getActionFlags(flags), 0x9100);
162
163 flags.clear();
164 flags.push_back("foo");
165 EXPECT_THROW(getActionFlags(flags), std::runtime_error);
166}