xref: /openbmc/bmcweb/test/redfish-core/lib/chassis_test.cpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #include "app.hpp"
4 #include "async_resp.hpp"
5 #include "chassis.hpp"
6 #include "dbus_utility.hpp"
7 #include "generated/enums/chassis.hpp"
8 #include "http_request.hpp"
9 #include "http_response.hpp"
10 
11 #include <boost/beast/core/string_type.hpp>
12 #include <boost/beast/http/status.hpp>
13 #include <boost/beast/http/verb.hpp>
14 #include <nlohmann/json.hpp>
15 
16 #include <functional>
17 #include <memory>
18 #include <string>
19 #include <system_error>
20 #include <utility>
21 
22 #include <gtest/gtest.h>
23 
24 namespace redfish
25 {
26 namespace
27 {
28 
assertChassisResetActionInfoGet(const std::string & chassisId,crow::Response & res)29 void assertChassisResetActionInfoGet(const std::string& chassisId,
30                                      crow::Response& res)
31 {
32     EXPECT_EQ(res.jsonValue["@odata.type"], "#ActionInfo.v1_1_2.ActionInfo");
33     EXPECT_EQ(res.jsonValue["@odata.id"],
34               "/redfish/v1/Chassis/" + chassisId + "/ResetActionInfo");
35     EXPECT_EQ(res.jsonValue["Name"], "Reset Action Info");
36 
37     EXPECT_EQ(res.jsonValue["Id"], "ResetActionInfo");
38 
39     nlohmann::json::array_t parameters;
40     nlohmann::json::object_t parameter;
41     parameter["Name"] = "ResetType";
42     parameter["Required"] = true;
43     parameter["DataType"] = "String";
44     nlohmann::json::array_t allowed;
45     allowed.emplace_back("PowerCycle");
46     parameter["AllowableValues"] = std::move(allowed);
47     parameters.emplace_back(std::move(parameter));
48 
49     EXPECT_EQ(res.jsonValue["Parameters"], parameters);
50 }
51 
TEST(HandleChassisResetActionInfoGet,StaticAttributesAreExpected)52 TEST(HandleChassisResetActionInfoGet, StaticAttributesAreExpected)
53 {
54     auto response = std::make_shared<bmcweb::AsyncResp>();
55     std::error_code err;
56     crow::Request request{{boost::beast::http::verb::get, "/whatever", 11},
57                           err};
58 
59     std::string fakeChassis = "fakeChassis";
60     response->res.setCompleteRequestHandler(
61         std::bind_front(assertChassisResetActionInfoGet, fakeChassis));
62 
63     crow::App app;
64     handleChassisResetActionInfoGet(app, request, response, fakeChassis);
65 }
66 
TEST(TranslateChassisTypeToRedfish,TranslationsAreExpected)67 TEST(TranslateChassisTypeToRedfish, TranslationsAreExpected)
68 {
69     ASSERT_EQ(
70         chassis::ChassisType::Blade,
71         translateChassisTypeToRedfish(
72             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Blade"));
73     ASSERT_EQ(
74         chassis::ChassisType::Component,
75         translateChassisTypeToRedfish(
76             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Component"));
77     ASSERT_EQ(
78         chassis::ChassisType::Enclosure,
79         translateChassisTypeToRedfish(
80             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Enclosure"));
81     ASSERT_EQ(
82         chassis::ChassisType::Module,
83         translateChassisTypeToRedfish(
84             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Module"));
85     ASSERT_EQ(
86         chassis::ChassisType::RackMount,
87         translateChassisTypeToRedfish(
88             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.RackMount"));
89     ASSERT_EQ(
90         chassis::ChassisType::StandAlone,
91         translateChassisTypeToRedfish(
92             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.StandAlone"));
93     ASSERT_EQ(
94         chassis::ChassisType::StorageEnclosure,
95         translateChassisTypeToRedfish(
96             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.StorageEnclosure"));
97     ASSERT_EQ(
98         chassis::ChassisType::Zone,
99         translateChassisTypeToRedfish(
100             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Zone"));
101     ASSERT_EQ(
102         chassis::ChassisType::Invalid,
103         translateChassisTypeToRedfish(
104             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Unknown"));
105 }
106 
TEST(HandleChassisProperties,TypeFound)107 TEST(HandleChassisProperties, TypeFound)
108 {
109     auto response = std::make_shared<bmcweb::AsyncResp>();
110     auto properties = dbus::utility::DBusPropertiesMap();
111     properties.emplace_back(
112         std::string("Type"),
113         dbus::utility::DbusVariantType(
114             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.RackMount"));
115     handleChassisProperties(response, properties);
116     ASSERT_EQ("RackMount", response->res.jsonValue["ChassisType"]);
117 
118     response = std::make_shared<bmcweb::AsyncResp>();
119     properties.clear();
120     properties.emplace_back(
121         std::string("Type"),
122         dbus::utility::DbusVariantType(
123             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.StandAlone"));
124     handleChassisProperties(response, properties);
125     ASSERT_EQ("StandAlone", response->res.jsonValue["ChassisType"]);
126 }
127 
TEST(HandleChassisProperties,BadTypeFound)128 TEST(HandleChassisProperties, BadTypeFound)
129 {
130     auto response = std::make_shared<bmcweb::AsyncResp>();
131     auto properties = dbus::utility::DBusPropertiesMap();
132     properties.emplace_back(
133         std::string("Type"),
134         dbus::utility::DbusVariantType(
135             "xyz.openbmc_project.Inventory.Item.Chassis.ChassisType.Unknown"));
136     handleChassisProperties(response, properties);
137     // We fall back to RackMount
138     ASSERT_EQ("RackMount", response->res.jsonValue["ChassisType"]);
139 }
140 
TEST(HandleChassisProperties,FailToGetProperty)141 TEST(HandleChassisProperties, FailToGetProperty)
142 {
143     auto response = std::make_shared<bmcweb::AsyncResp>();
144     auto properties = dbus::utility::DBusPropertiesMap();
145     properties.emplace_back(std::string("Type"),
146                             dbus::utility::DbusVariantType(123));
147     handleChassisProperties(response, properties);
148     ASSERT_EQ(boost::beast::http::status::internal_server_error,
149               response->res.result());
150 }
151 
TEST(HandleChassisProperties,TypeNotFound)152 TEST(HandleChassisProperties, TypeNotFound)
153 {
154     auto response = std::make_shared<bmcweb::AsyncResp>();
155     auto properties = dbus::utility::DBusPropertiesMap();
156     handleChassisProperties(response, properties);
157     ASSERT_EQ("RackMount", response->res.jsonValue["ChassisType"]);
158 }
159 
160 } // namespace
161 } // namespace redfish
162