summaryrefslogtreecommitdiff
path: root/test/redfish-core/lib/network_adapter_test.cpp
blob: 4f9b3a169713c68e4262c720c126d984fe5041d4 (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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#include "async_resp.hpp"
#include "dbus_utility.hpp"
#include "http_response.hpp"
#include "network_adapter.hpp"

#include <boost/asio/error.hpp>
#include <boost/beast/http/status.hpp>
#include <boost/system/errc.hpp>
#include <boost/system/linux_error.hpp>
#include <nlohmann/json.hpp>

#include <memory>
#include <string>
#include <string_view>

#include <gtest/gtest.h>

namespace redfish
{
namespace
{

constexpr std::string_view adapterPath =
    "/xyz/openbmc_project/inventory/system/chassis0/adapter0";

TEST(AfterGetNetworkAdapterLocation, EbadrOmitsLocation)
{
    auto response = std::make_shared<bmcweb::AsyncResp>();
    boost::system::error_code ec =
        boost::system::linux_error::bad_request_descriptor;

    afterGetNetworkAdapterLocation(response, std::string(adapterPath), ec, {});

    EXPECT_EQ(response->res.result(), boost::beast::http::status::ok);
    EXPECT_TRUE(response->res.jsonValue.empty());
}

TEST(AfterGetNetworkAdapterLocation, IoErrorOmitsLocation)
{
    auto response = std::make_shared<bmcweb::AsyncResp>();
    boost::system::error_code ec =
        boost::system::errc::make_error_code(boost::system::errc::io_error);

    afterGetNetworkAdapterLocation(response, std::string(adapterPath), ec, {});

    EXPECT_EQ(response->res.result(), boost::beast::http::status::ok);
    EXPECT_TRUE(response->res.jsonValue.empty());
}

TEST(AfterGetNetworkAdapterLocation, ErrorSetsInternalError)
{
    auto response = std::make_shared<bmcweb::AsyncResp>();
    boost::system::error_code ec = boost::asio::error::invalid_argument;

    afterGetNetworkAdapterLocation(response, std::string(adapterPath), ec, {});

    EXPECT_EQ(response->res.result(),
              boost::beast::http::status::internal_server_error);
}

TEST(AfterGetNetworkAdapterLocation, EmptyObjectOmitsLocation)
{
    auto response = std::make_shared<bmcweb::AsyncResp>();
    boost::system::error_code ec;

    afterGetNetworkAdapterLocation(response, std::string(adapterPath), ec, {});

    EXPECT_EQ(response->res.result(), boost::beast::http::status::ok);
    EXPECT_TRUE(response->res.jsonValue.empty());
}

TEST(AfterGetNetworkAdapterLocation, EmbeddedSetsLocationType)
{
    auto response = std::make_shared<bmcweb::AsyncResp>();
    boost::system::error_code ec;
    dbus::utility::MapperGetObject object = {
        {"xyz.openbmc_project.Service0",
         {"xyz.openbmc_project.Inventory.Connector.Embedded"}}};

    afterGetNetworkAdapterLocation(response, std::string(adapterPath), ec,
                                   object);

    EXPECT_EQ(response->res.result(), boost::beast::http::status::ok);
    EXPECT_EQ(
        response->res.jsonValue["Location"]["PartLocation"]["LocationType"],
        "Embedded");
}

TEST(AfterGetNetworkAdapterLocation, SlotSetsLocationType)
{
    auto response = std::make_shared<bmcweb::AsyncResp>();
    boost::system::error_code ec;
    dbus::utility::MapperGetObject object = {
        {"xyz.openbmc_project.Service0",
         {"xyz.openbmc_project.Inventory.Connector.Slot"}}};

    afterGetNetworkAdapterLocation(response, std::string(adapterPath), ec,
                                   object);

    EXPECT_EQ(response->res.result(), boost::beast::http::status::ok);
    EXPECT_EQ(
        response->res.jsonValue["Location"]["PartLocation"]["LocationType"],
        "Slot");
}

TEST(AfterGetNetworkAdapterLocation, UnknownInterfaceOmitsLocation)
{
    auto response = std::make_shared<bmcweb::AsyncResp>();
    boost::system::error_code ec;
    dbus::utility::MapperGetObject object = {
        {"xyz.openbmc_project.Service0",
         {"xyz.openbmc_project.Inventory.Item"}}};

    afterGetNetworkAdapterLocation(response, std::string(adapterPath), ec,
                                   object);

    EXPECT_EQ(response->res.result(), boost::beast::http::status::ok);
    EXPECT_TRUE(response->res.jsonValue.empty());
}

} // namespace
} // namespace redfish