xref: /openbmc/bmcweb/test/redfish-core/lib/manager_diagnostic_data_test.cpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #include "async_resp.hpp"
4 #include "manager_diagnostic_data.hpp"
5 
6 #include <boost/asio/error.hpp>
7 #include <boost/beast/http/status.hpp>
8 #include <boost/system/linux_error.hpp>
9 #include <nlohmann/json.hpp>
10 
11 #include <cstdint>
12 #include <limits>
13 #include <memory>
14 
15 #include <gtest/gtest.h>
16 
17 namespace redfish
18 {
19 namespace
20 {
21 
22 using json_pointer = nlohmann::json::json_pointer;
23 
testDataGetNoError(boost::system::error_code ec)24 void testDataGetNoError(boost::system::error_code ec)
25 {
26     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
27 
28     setBytesProperty(asyncResp,
29                      json_pointer("/MemoryStatistics/FreeStorageSpace"), ec, 0);
30     EXPECT_TRUE(asyncResp->res.jsonValue.is_null());
31     EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
32 }
33 
TEST(ManagerDiagnosticDataTest,ManagerDataGetServerUnreachable)34 TEST(ManagerDiagnosticDataTest, ManagerDataGetServerUnreachable)
35 {
36     testDataGetNoError(boost::asio::error::basic_errors::host_unreachable);
37 }
38 
TEST(ManagerDiagnosticDataTest,ManagerDataGetPathInvalid)39 TEST(ManagerDiagnosticDataTest, ManagerDataGetPathInvalid)
40 {
41     testDataGetNoError(boost::system::linux_error::bad_request_descriptor);
42 }
43 
verifyError(crow::Response & res)44 void verifyError(crow::Response& res)
45 {
46     EXPECT_EQ(res.result(), boost::beast::http::status::internal_server_error);
47     res.clear();
48 }
49 
TEST(ManagerDiagnosticDataTest,ManagerDataGetFailure)50 TEST(ManagerDiagnosticDataTest, ManagerDataGetFailure)
51 {
52     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
53     boost::system::error_code ec = boost::asio::error::operation_aborted;
54 
55     setBytesProperty(asyncResp,
56                      json_pointer("/MemoryStatistics/FreeStorageSpace"), ec, 0);
57     verifyError(asyncResp->res);
58 }
59 
TEST(ManagerDiagnosticDataTest,ManagerDataGetNullPtr)60 TEST(ManagerDiagnosticDataTest, ManagerDataGetNullPtr)
61 {
62     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
63 
64     setPercentProperty(
65         asyncResp,
66         nlohmann::json::json_pointer("/MemoryStatistics/FreeStorageSpace"), {},
67         std::numeric_limits<double>::quiet_NaN());
68     EXPECT_EQ(asyncResp->res.jsonValue["FreeStorageSpaceKiB"], nullptr);
69 }
70 
TEST(ManagerDiagnosticDataTest,ManagerDataGetSuccess)71 TEST(ManagerDiagnosticDataTest, ManagerDataGetSuccess)
72 {
73     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
74 
75     setBytesProperty(asyncResp, json_pointer("/FreeStorageSpaceKiB"), {},
76                      204800.0);
77     EXPECT_EQ(asyncResp->res.jsonValue["FreeStorageSpaceKiB"].get<int64_t>(),
78               200);
79 }
80 
81 } // namespace
82 } // namespace redfish
83