| #include "storage.hpp" | |
| #include <systemd/sd-bus.h> | |
| #include <cerrno> | |
| #include <cstddef> | |
| #include <cstdint> | |
| #include <filesystem> | |
| #include <memory> | |
| #include <optional> | |
| #include <stdexcept> | |
| #include <string> | |
| #include <string_view> | |
| #include <system_error> // NOLINT | |
| #include <tuple> | |
| #include <utility> | |
| #include <vector> | |
| #include <gmock/gmock.h> | |
| #include <gtest/gtest.h> | |
| #include "absl/functional/any_invocable.h" | |
| #include "boost/asio/error.hpp" // NOLINT | |
| #include "boost/asio/io_context.hpp" // NOLINT | |
| #include "boost/asio/post.hpp" // NOLINT | |
| #include "boost/beast/core/string_type.hpp" // NOLINT | |
| #include "boost/beast/http/field.hpp" // NOLINT | |
| #include "boost/beast/http/message.hpp" // NOLINT | |
| #include "boost/beast/http/status.hpp" // NOLINT | |
| #include "boost/beast/http/string_body.hpp" // NOLINT | |
| #include "boost/system/detail/error_code.hpp" // NOLINT | |
| #include "boost/system/errc.hpp" // NOLINT | |
| #include "boost/system/error_code.hpp" // NOLINT | |
| #include "http_request.hpp" | |
| #include "async_resp.hpp" | |
| #include "dbus_utility.hpp" | |
| #include "storage_utils.hpp" | |
| #include "test/redfish-core/lib/snapshot_fixture.hpp" | |
| #include <nlohmann/json.hpp> | |
| #include "managed_store.hpp" | |
| #include "managed_store_types.hpp" | |
| #include "test/g3/mock_managed_store.hpp" | |
| #include "test/g3/mock_managed_store_test.hpp" | |
| #include "sdbusplus/message.hpp" | |
| #include "sdbusplus/message/native_types.hpp" | |
| #include "sdbusplus/test/sdbus_mock.hpp" | |
| namespace redfish { | |
| namespace { | |
| using ::dbus::utility::DbusVariantType; | |
| using ::managedStore::KeyType; | |
| using ::managedStore::ManagedType; | |
| using ::managedStore:: | |
| SimulateFailedAsyncPostDbusCallThreadSafeWithMsgAndEmptyValueAction; | |
| using ::managedStore::SimulateFailedAsyncSetPropertyDbusCallAction; | |
| using ::managedStore::SimulateSuccessfulAsyncSetPropertyDbusCallAction; | |
| using ::managedStore::ValueType; | |
| using ::testing::_; | |
| using ::testing::An; | |
| class StorageSnapshotFixture : public SnapshotFixture { | |
| protected: | |
| dbus::utility::MapperGetSubTreeResponse subtree = { | |
| {"/drive_0", {{"connection", {}}}}, | |
| {"/drive_1", {}}, | |
| }; | |
| std::string path = "/xyz/openbmc_project/inventory/Board/device_1"; | |
| std::string connection = "xyz.test"; | |
| }; | |
| TEST_F(StorageSnapshotFixture, SetResetTypeOk) { | |
| EXPECT_CALL( | |
| *dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()), | |
| setProperty(_, _, "xyz.openbmc_project.State.Drive", | |
| "RequestedDriveTransition", std::string("PowerCycle"), _)) | |
| .Times(1) | |
| .WillOnce(SimulateSuccessfulAsyncSetPropertyDbusCallAction:: | |
| SimulateSuccessfulAsyncSetPropertyDbusCall()); | |
| setResetType(share_async_resp_, "drive_0", "PowerCycle", subtree); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok); | |
| } | |
| TEST_F(StorageSnapshotFixture, SetResetTypeFailed) { | |
| EXPECT_CALL( | |
| *dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()), | |
| setProperty(_, _, "xyz.openbmc_project.State.Drive", | |
| "RequestedDriveTransition", std::string("PowerCycle"), _)) | |
| .Times(1) | |
| .WillOnce(SimulateFailedAsyncSetPropertyDbusCallAction:: | |
| SimulateFailedAsyncSetPropertyDbusCall()); | |
| setResetType(share_async_resp_, "drive_0", "PowerCycle", subtree); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::internal_server_error); | |
| } | |
| TEST_F(StorageSnapshotFixture, SetResetTypeInvalidDriveId) { | |
| setResetType(share_async_resp_, "drive_2", "PowerCycle", subtree); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::not_found); | |
| } | |
| TEST_F(StorageSnapshotFixture, SetResetTypeInvalidConnection) { | |
| setResetType(share_async_resp_, "drive_1", "PowerCycle", subtree); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::internal_server_error); | |
| } | |
| TEST_F(StorageSnapshotFixture, DriveServiceLabelOnlyResponse) { | |
| auto response = std::make_shared<bmcweb::AsyncResp>(); | |
| nlohmann::json& json = response->res.jsonValue; | |
| /* Mock the Drive object*/ | |
| std::vector<std::string> interfaces = { | |
| storage_utils::driveInterface, | |
| "xyz.openbmc_project.Inventory.Decorator.LocationCode", | |
| }; | |
| // Insert interfaces getObject call | |
| KeyType locationTypeKey(ManagedType::kManagedMapperObject, path, {}); | |
| dbus::utility::MapperGetObject locationTypeValue{{connection, interfaces}}; | |
| std::shared_ptr<ValueType> mockLocationType = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(locationTypeValue)); | |
| ASSERT_TRUE( | |
| dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(locationTypeKey, mockLocationType) | |
| .ok()); | |
| // Insert service label | |
| std::string serviceLabel = "X2"; | |
| KeyType key(ManagedType::kManagedProperty, connection, | |
| sdbusplus::message::object_path(path), | |
| "xyz.openbmc_project.Inventory.Decorator.LocationCode", | |
| "LocationCode"); | |
| std::shared_ptr<ValueType> mockServiceLabel = | |
| managedStore::MockManagedStoreTest::CreateValueType(serviceLabel); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(key, mockServiceLabel) | |
| .ok()); | |
| /* Mock an empty Drive assocation */ | |
| KeyType getAssociatedSubTreeKey(ManagedType::kManagedAssociatedSubtree, | |
| path + "/chassis", | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Board"}); | |
| dbus::utility::MapperGetSubTreeResponse subtree = {}; | |
| std::shared_ptr<ValueType> mockSubtree = | |
| managedStore::MockManagedStoreTest::CreateValueType(std::move(subtree)); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(getAssociatedSubTreeKey, | |
| mockSubtree) | |
| .ok()); | |
| addAllDriveInfo(response, "", connection, path, interfaces, ""); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocation"]["ServiceLabel"], | |
| serviceLabel); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocation"]["LocationType"], nullptr); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocationContext"], | |
| "PlaceHolderAndShouldNotBeUsed"); | |
| EXPECT_EQ(json["PhysicalLocation"]["Oem"]["Google"]["Devpath"], "/phys"); | |
| } | |
| TEST_F(StorageSnapshotFixture, DriveServiceLabelAndTypeResponse) { | |
| auto response = std::make_shared<bmcweb::AsyncResp>(); | |
| nlohmann::json& json = response->res.jsonValue; | |
| /* Mock the Drive object*/ | |
| std::vector<std::string> interfaces = { | |
| storage_utils::driveInterface, | |
| "xyz.openbmc_project.Inventory.Decorator.LocationCode", | |
| "xyz.openbmc_project.Inventory.Connector.Embedded", | |
| }; | |
| // Insert interfaces getObject call | |
| KeyType locationTypeKey(ManagedType::kManagedMapperObject, path, {}); | |
| dbus::utility::MapperGetObject locationTypeValue{{connection, interfaces}}; | |
| std::shared_ptr<ValueType> mockLocationType = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(locationTypeValue)); | |
| ASSERT_TRUE( | |
| dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(locationTypeKey, mockLocationType) | |
| .ok()); | |
| std::string serviceLabel = "X2"; | |
| KeyType key(ManagedType::kManagedProperty, connection, | |
| sdbusplus::message::object_path(path), | |
| "xyz.openbmc_project.Inventory.Decorator.LocationCode", | |
| "LocationCode"); | |
| std::shared_ptr<ValueType> mockServiceLabel = | |
| managedStore::MockManagedStoreTest::CreateValueType(serviceLabel); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(key, mockServiceLabel) | |
| .ok()); | |
| /* Mock an empty Drive assocation */ | |
| KeyType getAssociatedSubTreeKey(ManagedType::kManagedAssociatedSubtree, | |
| path + "/chassis", | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Board"}); | |
| dbus::utility::MapperGetSubTreeResponse subtree = {}; | |
| std::shared_ptr<ValueType> mockSubtree = | |
| managedStore::MockManagedStoreTest::CreateValueType(std::move(subtree)); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(getAssociatedSubTreeKey, | |
| mockSubtree) | |
| .ok()); | |
| addAllDriveInfo(response, "", connection, path, interfaces, ""); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocation"]["ServiceLabel"], "X2"); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocation"]["LocationType"], | |
| "Embedded"); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocationContext"], | |
| "PlaceHolderAndShouldNotBeUsed"); | |
| EXPECT_EQ(json["PhysicalLocation"]["Oem"]["Google"]["Devpath"], | |
| "/phys:device:X2"); | |
| } | |
| /** | |
| * @brief: Mock a parent object that containing the target object | |
| * @param[in] objectPath the current object path | |
| * @param[in] objectType the current object type presented as dbus | |
| interface | |
| * @param[in] parentConnection service name for the parent object | |
| * @param[in] parentPath parent object path; return empty list on empty | |
| string | |
| * @param[in] parentType parent type present as dbus interace | |
| * @param[in] serviceLabel parent service_label/location_code property; | |
| return | |
| * ec on dbus read when empty string | |
| */ | |
| void mockParent(const std::string& objectPath, const std::string& objectType, | |
| const std::string& parentConnection, | |
| const std::string& parentPath, const std::string& parentType, | |
| const std::string& serviceLabel, | |
| const std::string& locationType = "Slot") { | |
| // define the assocation tags | |
| std::string forwardTag = "contained_by"; | |
| std::string backwardTag = "containing"; | |
| if (objectType == storage_utils::driveInterface && | |
| parentType == storage_utils::chassisInterface) { | |
| forwardTag = "chassis"; | |
| backwardTag = "drive"; | |
| } else if (objectType == storage_utils::storageInterface && | |
| parentType == storage_utils::driveInterface) { | |
| forwardTag = "drive"; | |
| backwardTag = "storage"; | |
| } else if (objectType == storage_utils::controllerInterface && | |
| parentType == storage_utils::storageInterface) { | |
| forwardTag = "storage"; | |
| backwardTag = "storage_controller"; | |
| } else if (objectType == storage_utils::volumeInterface && | |
| parentType == storage_utils::storageInterface) { | |
| forwardTag = "contained"; | |
| backwardTag = "containing"; | |
| } | |
| // insert assocation mock | |
| KeyType getAssociatedSubTreeKey( | |
| ManagedType::kManagedAssociatedSubtree, | |
| (std::filesystem::path(objectPath) / forwardTag).string(), | |
| "/xyz/openbmc_project/inventory", 0, {parentType}); | |
| dbus::utility::MapperGetSubTreeResponse subtree = | |
| parentPath.empty() ? dbus::utility::MapperGetSubTreeResponse{} | |
| : dbus::utility::MapperGetSubTreeResponse{ | |
| {parentPath, | |
| {{parentConnection, | |
| {parentType, | |
| {"xyz.openbmc_project.Inventory.Decorator." | |
| "LocationCode"}}}}}, | |
| }; | |
| std::shared_ptr<ValueType> mockSubtree = | |
| managedStore::MockManagedStoreTest::CreateValueType(std::move(subtree)); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(getAssociatedSubTreeKey, | |
| mockSubtree) | |
| .ok()); | |
| // insert service label mock | |
| KeyType parentLocationKey( | |
| ManagedType::kManagedProperty, parentConnection, | |
| sdbusplus::message::object_path(parentPath), | |
| "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode"); | |
| std::shared_ptr<ValueType> mockServiceLabel = | |
| serviceLabel.empty() | |
| ? managedStore::MockManagedStoreTest::CreateErrorValueType( | |
| serviceLabel, | |
| boost::system::error_code(boost::asio::error::not_found)) | |
| : managedStore::MockManagedStoreTest::CreateValueType(serviceLabel); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(parentLocationKey, | |
| mockServiceLabel) | |
| .ok()); | |
| // insert location type mock | |
| KeyType parentLocationTypeKey(ManagedType::kManagedMapperObject, parentPath, | |
| {}); | |
| dbus::utility::MapperGetObject parentLocationTypeValue{ | |
| {parentConnection, | |
| {{"xyz.openbmc_project.Inventory.Decorator.LocationCode"}, | |
| std::string{"xyz.openbmc_project.Inventory.Connector."} + | |
| locationType}}}; | |
| std::shared_ptr<ValueType> mockLocationType = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(parentLocationTypeValue)); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(parentLocationTypeKey, | |
| mockLocationType) | |
| .ok()); | |
| } | |
| TEST_F(StorageSnapshotFixture, DriveLocationContextOnlyResponse) { | |
| auto response = std::make_shared<bmcweb::AsyncResp>(); | |
| nlohmann::json& json = response->res.jsonValue; | |
| std::vector<std::string> interfaces = { | |
| storage_utils::driveInterface, | |
| }; | |
| // Insert interfaces getObject call | |
| KeyType locationTypeKey(ManagedType::kManagedMapperObject, path, {}); | |
| dbus::utility::MapperGetObject locationTypeValue{{connection, interfaces}}; | |
| std::shared_ptr<ValueType> mockLocationType = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(locationTypeValue)); | |
| ASSERT_TRUE( | |
| dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(locationTypeKey, mockLocationType) | |
| .ok()); | |
| // Parent of Drive | |
| mockParent(path, storage_utils::driveInterface, "test.chassis.0", | |
| "/xyz/openbmc_project/inventory/Board/chassis_0", | |
| storage_utils::chassisInterface, "C"); | |
| // Parent of Chassis0 | |
| mockParent("/xyz/openbmc_project/inventory/Board/chassis_0", | |
| storage_utils::chassisInterface, "test.chassis.1", | |
| "/xyz/openbmc_project/inventory/Board/chassis_1", | |
| storage_utils::chassisInterface, "A"); | |
| // Parent of Chassis1 | |
| mockParent("/xyz/openbmc_project/inventory/Board/chassis_1", | |
| storage_utils::chassisInterface, "test.chassis.2", | |
| "/xyz/openbmc_project/inventory/Board/chassis_2", | |
| storage_utils::chassisInterface, "B"); | |
| // Mock empty parents of Chassis2 | |
| mockParent("/xyz/openbmc_project/inventory/Board/chassis_2", | |
| storage_utils::chassisInterface, "", "", | |
| storage_utils::chassisInterface, ""); | |
| addAllDriveInfo(response, "", connection, path, interfaces, ""); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocation"]["ServiceLabel"], nullptr); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocation"]["LocationType"], nullptr); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocationContext"], | |
| "PlaceHolderAndShouldNotBeUsed"); | |
| EXPECT_EQ(json["PhysicalLocation"]["Oem"]["Google"]["Devpath"], | |
| "/phys/B/A/C"); | |
| } | |
| TEST_F(StorageSnapshotFixture, DriveAllLocationResponse) { | |
| auto response = std::make_shared<bmcweb::AsyncResp>(); | |
| nlohmann::json& json = response->res.jsonValue; | |
| std::vector<std::string> interfaces = { | |
| storage_utils::driveInterface, | |
| "xyz.openbmc_project.Inventory.Decorator.LocationCode", | |
| "xyz.openbmc_project.Inventory.Connector.Embedded", | |
| }; | |
| // Insert interfaces getObject call | |
| KeyType locationTypeKey(ManagedType::kManagedMapperObject, path, {}); | |
| dbus::utility::MapperGetObject locationTypeValue{{connection, interfaces}}; | |
| std::shared_ptr<ValueType> mockLocationType = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(locationTypeValue)); | |
| ASSERT_TRUE( | |
| dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(locationTypeKey, mockLocationType) | |
| .ok()); | |
| std::string serviceLabel = "X2"; | |
| KeyType key(ManagedType::kManagedProperty, connection, | |
| sdbusplus::message::object_path(path), | |
| "xyz.openbmc_project.Inventory.Decorator.LocationCode", | |
| "LocationCode"); | |
| std::shared_ptr<ValueType> mockServiceLabel = | |
| managedStore::MockManagedStoreTest::CreateValueType(serviceLabel); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(key, mockServiceLabel) | |
| .ok()); | |
| // Parent of Drive | |
| mockParent(path, storage_utils::driveInterface, "test.chassis.0", | |
| "/xyz/openbmc_project/inventory/Board/chassis_0", | |
| storage_utils::chassisInterface, "C"); | |
| // Parent of Chassis0 | |
| mockParent("/xyz/openbmc_project/inventory/Board/chassis_0", | |
| storage_utils::chassisInterface, "test.chassis.1", | |
| "/xyz/openbmc_project/inventory/Board/chassis_1", | |
| storage_utils::chassisInterface, "A"); | |
| // Parent of Chassis1 | |
| mockParent("/xyz/openbmc_project/inventory/Board/chassis_1", | |
| storage_utils::chassisInterface, "test.chassis.2", | |
| "/xyz/openbmc_project/inventory/Board/chassis_2", | |
| storage_utils::chassisInterface, "B"); | |
| // Mock empty parents of Chassis2 | |
| mockParent("/xyz/openbmc_project/inventory/Board/chassis_2", | |
| storage_utils::chassisInterface, "", "", | |
| storage_utils::chassisInterface, ""); | |
| addAllDriveInfo(response, "", connection, path, interfaces, ""); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocation"]["ServiceLabel"], "X2"); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocation"]["LocationType"], | |
| "Embedded"); | |
| EXPECT_EQ(json["PhysicalLocation"]["PartLocationContext"], | |
| "PlaceHolderAndShouldNotBeUsed"); | |
| EXPECT_EQ(json["PhysicalLocation"]["Oem"]["Google"]["Devpath"], | |
| "/phys/B/A/C:device:X2"); | |
| } | |
| TEST_F(StorageSnapshotFixture, StorageControllerAllLocationResponse) { | |
| auto response = std::make_shared<bmcweb::AsyncResp>(); | |
| nlohmann::json& json = response->res.jsonValue; | |
| /* Mock a physical controller with LocationCode/LocationType */ | |
| std::vector<std::string> interfaces = { | |
| storage_utils::controllerInterface, | |
| "xyz.openbmc_project.Inventory.Decorator.LocationCode", | |
| "xyz.openbmc_project.Inventory.Connector.Embedded", | |
| }; | |
| // Insert interfaces getObject call | |
| KeyType locationTypeKey(ManagedType::kManagedMapperObject, | |
| path + "/storage_controller_0", {}); | |
| dbus::utility::MapperGetObject locationTypeValue{{connection, interfaces}}; | |
| std::shared_ptr<ValueType> mockLocationType = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(locationTypeValue)); | |
| ASSERT_TRUE( | |
| dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(locationTypeKey, mockLocationType) | |
| .ok()); | |
| std::string serviceLabel = "X2"; | |
| KeyType key(ManagedType::kManagedProperty, connection, | |
| sdbusplus::message::object_path(path) / "storage_controller_0", | |
| "xyz.openbmc_project.Inventory.Decorator.LocationCode", | |
| "LocationCode"); | |
| std::shared_ptr<ValueType> mockServiceLabel = | |
| managedStore::MockManagedStoreTest::CreateValueType(serviceLabel); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(key, mockServiceLabel) | |
| .ok()); | |
| // Parent of Storage Controller | |
| mockParent(path + "/storage_controller_0", storage_utils::controllerInterface, | |
| "test.storage.0", path + "/storage_0", | |
| storage_utils::storageInterface, "Storage"); | |
| // Parent of Storage | |
| mockParent(path + "/storage_0", storage_utils::storageInterface, | |
| "test.drive.0", path + "/drive_0", storage_utils::driveInterface, | |
| "Drive"); | |
| // Parent of Drive | |
| mockParent(path + "/drive_0", storage_utils::driveInterface, "test.chassis.0", | |
| path, storage_utils::chassisInterface, "C"); | |
| // Parent of Chassis0 | |
| mockParent(path, storage_utils::chassisInterface, "test.chassis.1", | |
| "/xyz/openbmc_project/inventory/Board/chassis_1", | |
| storage_utils::chassisInterface, "A"); | |
| // Parent of Chassis1 | |
| mockParent("/xyz/openbmc_project/inventory/Board/chassis_1", | |
| storage_utils::chassisInterface, "test.chassis.2", | |
| "/xyz/openbmc_project/inventory/Board/chassis_2", | |
| storage_utils::chassisInterface, "B"); | |
| // Mock empty parents of Chassis2 | |
| mockParent("/xyz/openbmc_project/inventory/Board/chassis_2", | |
| storage_utils::chassisInterface, "", "", | |
| storage_utils::chassisInterface, ""); | |
| storage_utils::tryGetLocation(response, connection, | |
| path + "/storage_controller_0", | |
| storage_utils::controllerInterface, interfaces); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(json["Location"]["PartLocation"]["ServiceLabel"], "X2"); | |
| EXPECT_EQ(json["Location"]["PartLocation"]["LocationType"], "Embedded"); | |
| EXPECT_EQ(json["Location"]["PartLocationContext"], | |
| "PlaceHolderAndShouldNotBeUsed"); | |
| // storage should not be part of the location info | |
| EXPECT_EQ(json["Location"]["Oem"]["Google"]["Devpath"], | |
| "/phys/B/A/C/Drive:device:X2"); | |
| // None-NVMe controller should have no logic tag | |
| EXPECT_EQ(json["Location"]["Oem"]["Google"]["EmbeddedLocationContext"], | |
| nullptr); | |
| json.clear(); | |
| /* Mock a logical NVMe controller w/o LocationType */ | |
| interfaces = {storage_utils::controllerInterface, | |
| "xyz.openbmc_project.Inventory.Decorator.LocationCode", | |
| "xyz.openbmc_project.NVMe.MetricStore"}; | |
| // Insert interfaces getObject call | |
| dbus::utility::MapperGetObject locationTypeValue2{{connection, interfaces}}; | |
| std::shared_ptr<ValueType> mockLocationType2 = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(locationTypeValue2)); | |
| ASSERT_TRUE( | |
| dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(locationTypeKey, mockLocationType2) | |
| .ok()); | |
| storage_utils::tryGetLocation(response, connection, | |
| path + "/storage_controller_0", | |
| storage_utils::controllerInterface, interfaces); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(json["Location"]["PartLocation"]["ServiceLabel"], "X2"); | |
| EXPECT_EQ(json["Location"]["PartLocation"]["LocationType"], nullptr); | |
| EXPECT_EQ(json["Location"]["PartLocationContext"], | |
| "PlaceHolderAndShouldNotBeUsed"); | |
| // storage should not be part of the devpath | |
| EXPECT_EQ(json["Location"]["Oem"]["Google"]["Devpath"], "/phys/B/A/C/Drive"); | |
| // NVMe controller should have logic tag | |
| EXPECT_EQ(json["Location"]["Oem"]["Google"]["EmbeddedLocationContext"], | |
| "controller:storage_controller_0"); | |
| } | |
| void mockStorageObject(std::string storageName, std::string& storagePath) { | |
| storagePath = "/xyz/openbmc_project/inventory/system/board/ParentStorage/" + | |
| storageName; | |
| KeyType key(ManagedType::kManagedSubtree, "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Storage"}); | |
| dbus::utility::MapperGetSubTreeResponse mockSubtreeResponse{ | |
| {storagePath, | |
| {{"xyz.openbmc_project.NVMe", | |
| std::vector<std::string>{ | |
| "xyz.openbmc_project.Nvme.Storage", | |
| "xyz.openbmc_project.Inventory.Item.Storage"}}}}}; | |
| std::shared_ptr<ValueType> subtree = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(mockSubtreeResponse)); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(key, subtree) | |
| .ok()); | |
| KeyType key2(ManagedType::kManagedAssociatedSubtreePaths, | |
| storagePath + "/contained_by", "/xyz/openbmc_project/inventory", | |
| 0, {"xyz.openbmc_project.Inventory.Item.System"}); | |
| std::vector<std::string> system({}); // empty means system | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| key2, managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(system))) | |
| .ok()); | |
| } | |
| void addVolumeToMockStorage(std::string storagePath, std::string volumeName, | |
| std::string& volumePath, | |
| bool includeMetricStore = true) { | |
| std::vector<std::string> volIfaces( | |
| {"xyz.openbmc_project.Inventory.Item.Volume", | |
| "xyz.openbmc_project.Nvme.Volume"}); | |
| if (includeMetricStore) { | |
| volIfaces.push_back("xyz.openbmc_project.NVMe.MetricStore"); | |
| } | |
| volumePath = storagePath + "/volumes/" + volumeName; | |
| KeyType getAssociatedSubTreeKey( | |
| ManagedType::kManagedAssociatedSubtree, storagePath + "/containing", | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Volume"}); | |
| dbus::utility::MapperGetSubTreeResponse mockSubtreeResponse{ | |
| {volumePath, {{"xyz.openbmc_project.NVMe", volIfaces}}}}; | |
| std::shared_ptr<ValueType> subtree = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(mockSubtreeResponse)); | |
| ASSERT_TRUE( | |
| dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(getAssociatedSubTreeKey, subtree) | |
| .ok()); | |
| } | |
| void mockItemVolumeData(std::string volumePath) { | |
| KeyType key2(ManagedType::kManagedPropertyMap, "xyz.openbmc_project.NVMe", | |
| volumePath, "xyz.openbmc_project.Inventory.Item.Volume"); | |
| dbus::utility::DBusPropertiesMap getAllItemVolumeMap{ | |
| {std::make_pair("BlockSize", 4096UL), | |
| std::make_pair("Size", 3221225472000ULL)}}; | |
| std::shared_ptr<ValueType> getAllItemVolume = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(getAllItemVolumeMap)); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(key2, getAllItemVolume) | |
| .ok()); | |
| } | |
| void mockNvmeVolumeData(std::string volumePath) { | |
| KeyType key3(ManagedType::kManagedPropertyMap, "xyz.openbmc_project.NVMe", | |
| volumePath, "xyz.openbmc_project.Nvme.Volume"); | |
| dbus::utility::DBusPropertiesMap getAllVolumeMap{ | |
| {std::make_pair("NamespaceId", 1U), std::make_pair("LBAFormat", 0UL)}}; | |
| std::shared_ptr<ValueType> getAllVolume = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(getAllVolumeMap)); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(key3, getAllVolume) | |
| .ok()); | |
| } | |
| void mockAvoidLocationCheckForVolume(std::string volumePath) { | |
| // adding empty object to avoid location check | |
| KeyType locationKey(ManagedType::kManagedMapperObject, volumePath, {}); | |
| dbus::utility::MapperGetObject locationTypeValue{{}}; | |
| std::shared_ptr<ValueType> mockLocationType = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(locationTypeValue)); | |
| ASSERT_TRUE( | |
| dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(locationKey, mockLocationType) | |
| .ok()); | |
| // create empty storage reverse association to avoid storage check | |
| KeyType getAssociatedSubTreeKey( | |
| ManagedType::kManagedAssociatedSubtree, volumePath + "/contained", | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Storage"}); | |
| dbus::utility::MapperGetSubTreeResponse mockSubtreeResponse{}; | |
| std::shared_ptr<ValueType> subtree = | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(mockSubtreeResponse)); | |
| ASSERT_TRUE( | |
| dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(getAssociatedSubTreeKey, subtree) | |
| .ok()); | |
| } | |
| void mockMetricStore(std::string volumePath, bool failed = false) { | |
| KeyType key(ManagedType::kManagedProperty, "xyz.openbmc_project.NVMe", | |
| volumePath, "xyz.openbmc_project.NVMe.MetricStore", | |
| "MetricCollection"); | |
| if (failed) { | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->evictMockObjectFromManagedStore(key) | |
| .ok()); | |
| } else { | |
| std::shared_ptr<ValueType> metricCollection = | |
| managedStore::MockManagedStoreTest::CreateValueType< | |
| dbus::utility::DbusVariantType>( | |
| std::vector<std::string>({"FirstMetric", "SecondMetric"})); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore(key, metricCollection) | |
| .ok()); | |
| } | |
| } | |
| void checkGenericVolumeMockData(nlohmann::json& json) { | |
| EXPECT_EQ(json["@odata.id"], | |
| "/redfish/v1/Systems/system/Storage/storage_nvme/Volumes/1"); | |
| EXPECT_EQ(json["@odata.type"], "#Volume.v1_9_0.Volume"); | |
| EXPECT_EQ(json["BlockSizeBytes"], 4096); | |
| EXPECT_EQ(json["Capacity"]["Data"]["ProvisionedBytes"], 3221225472000); | |
| EXPECT_EQ(json["Id"], "1"); | |
| EXPECT_EQ(json["NVMeNamespaceProperties"]["LBAFormat"]["LBADataSizeBytes"], | |
| 4096); | |
| EXPECT_EQ(json["NVMeNamespaceProperties"]["LBAFormat"]["LBAFormatType"], | |
| "LBAFormat0"); | |
| EXPECT_EQ(json["NVMeNamespaceProperties"]["NamespaceId"], "0x00000001"); | |
| EXPECT_EQ(json["Name"], "Namespace 1"); | |
| } | |
| TEST_F(StorageSnapshotFixture, StorageVolumeHandlerCheck) { | |
| std::string storageName = "storage_nvme"; | |
| std::string volumeName = "1"; | |
| std::string storagePath; | |
| mockStorageObject(storageName, storagePath); | |
| std::string volumePath; | |
| addVolumeToMockStorage(storagePath, volumeName, volumePath); | |
| mockItemVolumeData(volumePath); | |
| mockNvmeVolumeData(volumePath); | |
| mockMetricStore(volumePath); | |
| // Skip location check due to it's complexity and | |
| // since this test is targeting Metrics | |
| mockAvoidLocationCheckForVolume(volumePath); | |
| storageVolumeHandler(app_, CreateRequest(), share_async_resp_, "system", | |
| storageName, volumeName); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| checkGenericVolumeMockData(json); | |
| EXPECT_EQ(json["Oem"]["Google"]["FirstMetric"]["DataUri"], | |
| "/redfish/v1/Systems/system/Storage/storage_nvme/Volumes/1/Oem/" | |
| "Google/Metrics/FirstMetric"); | |
| EXPECT_EQ(json["Oem"]["Google"]["SecondMetric"]["DataUri"], | |
| "/redfish/v1/Systems/system/Storage/storage_nvme/Volumes/1/Oem/" | |
| "Google/Metrics/SecondMetric"); | |
| } | |
| TEST_F(StorageSnapshotFixture, StorageVolumeHandlerCheckWithoutItemVolume) { | |
| std::string storageName = "storage_nvme"; | |
| std::string volumeName = "1"; | |
| std::string storagePath; | |
| mockStorageObject(storageName, storagePath); | |
| std::string volumePath; | |
| addVolumeToMockStorage(storagePath, volumeName, volumePath); | |
| mockNvmeVolumeData(volumePath); | |
| mockMetricStore(volumePath); | |
| // remove item volume from store | |
| // previous testcases can affect this, hence removing | |
| KeyType itemVolumeKey(ManagedType::kManagedPropertyMap, | |
| "xyz.openbmc_project.NVMe", volumePath, | |
| "xyz.openbmc_project.Inventory.Item.Volume"); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->evictMockObjectFromManagedStore(itemVolumeKey) | |
| .ok()); | |
| // Skip location check due to it's complexity and | |
| // since this test is targeting Metrics | |
| mockAvoidLocationCheckForVolume(volumePath); | |
| storageVolumeHandler(app_, CreateRequest(), share_async_resp_, "system", | |
| storageName, volumeName); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(json["@odata.id"], | |
| "/redfish/v1/Systems/system/Storage/storage_nvme/Volumes/1"); | |
| EXPECT_EQ(json["@odata.type"], "#Volume.v1_9_0.Volume"); | |
| EXPECT_FALSE(json.contains("BlockSizeBytes")); | |
| EXPECT_FALSE(json.contains("Capacity")); | |
| EXPECT_FALSE(json.contains("NVMeNamespaceProperties")); | |
| EXPECT_EQ(json["Id"], "1"); | |
| EXPECT_EQ(json["Name"], std::string("Volume ") + volumeName); | |
| EXPECT_EQ(json["Oem"]["Google"]["FirstMetric"]["DataUri"], | |
| "/redfish/v1/Systems/system/Storage/storage_nvme/Volumes/1/Oem/" | |
| "Google/Metrics/FirstMetric"); | |
| EXPECT_EQ(json["Oem"]["Google"]["SecondMetric"]["DataUri"], | |
| "/redfish/v1/Systems/system/Storage/storage_nvme/Volumes/1/Oem/" | |
| "Google/Metrics/SecondMetric"); | |
| } | |
| TEST_F(StorageSnapshotFixture, StorageVolumeHandlerCheckWithoutMetricStore) { | |
| std::string storageName = "storage_nvme"; | |
| std::string volumeName = "1"; | |
| std::string storagePath; | |
| mockStorageObject(storageName, storagePath); | |
| std::string volumePath; | |
| addVolumeToMockStorage(storagePath, volumeName, volumePath, false); | |
| mockItemVolumeData(volumePath); | |
| mockNvmeVolumeData(volumePath); | |
| // Skip location check due to it's complexity and | |
| // since this test is targeting Metrics | |
| mockAvoidLocationCheckForVolume(volumePath); | |
| storageVolumeHandler(app_, CreateRequest(), share_async_resp_, "system", | |
| storageName, volumeName); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| checkGenericVolumeMockData(json); | |
| EXPECT_FALSE(json["Oem"]["Google"].contains("FirstMetric")); | |
| } | |
| TEST_F(StorageSnapshotFixture, StorageVolumeHandlerCheckWithMetricStoreError) { | |
| std::string storageName = "storage_nvme"; | |
| std::string volumeName = "1"; | |
| std::string storagePath; | |
| mockStorageObject(storageName, storagePath); | |
| std::string volumePath; | |
| addVolumeToMockStorage(storagePath, volumeName, volumePath); | |
| mockItemVolumeData(volumePath); | |
| mockNvmeVolumeData(volumePath); | |
| mockMetricStore(volumePath, true); | |
| // Skip location check due to it's complexity and | |
| // since this test is targeting Metrics | |
| mockAvoidLocationCheckForVolume(volumePath); | |
| storageVolumeHandler(app_, CreateRequest(), share_async_resp_, "system", | |
| storageName, volumeName); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| checkGenericVolumeMockData(json); | |
| EXPECT_FALSE(json["Oem"]["Google"].contains("FirstMetric")); | |
| } | |
| TEST_F(StorageSnapshotFixture, StorageVolumeGetMetricError) { | |
| std::string storageName = "storage_nvme"; | |
| std::string volumeName = "1"; | |
| std::string storagePath; | |
| mockStorageObject(storageName, storagePath); | |
| std::string volumePath; | |
| addVolumeToMockStorage(storagePath, volumeName, volumePath); | |
| mockMetricStore(volumePath); | |
| std::string metricId = "FirstMetric"; | |
| testing::StrictMock<sdbusplus::SdBusMock> sdbus; | |
| EXPECT_CALL(sdbus, | |
| sd_bus_message_new_method_call(testing::_, testing::_, nullptr, | |
| nullptr, nullptr, nullptr)) | |
| .WillRepeatedly(testing::Return(0)); | |
| sd_bus_error err; | |
| err.name = "xyz.openbmc_project.Common.Error.Unavailable"; | |
| EXPECT_CALL(sdbus, sd_bus_message_get_error(testing::_)) | |
| .WillRepeatedly(testing::Return(&err)); | |
| // intentionally failing | |
| // want to test only properties not the metric data since there is no | |
| // existing model to mock fd | |
| sdbusplus::message_t msg = sdbusplus::get_mocked_new(&sdbus).new_method_call( | |
| nullptr, nullptr, nullptr, nullptr); | |
| EXPECT_CALL( | |
| *dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()), | |
| PostDbusCallToIoContextThreadSafe( | |
| _, | |
| An<absl::AnyInvocable<void(const boost::system::error_code&, | |
| const sdbusplus::message_t&, | |
| const sdbusplus::message::unix_fd&)>&&>(), | |
| "xyz.openbmc_project.NVMe", volumePath, | |
| "xyz.openbmc_project.NVMe.MetricStore", "GetMetric", metricId)) | |
| .Times(testing::AtMost(1)) | |
| .WillOnce( | |
| SimulateFailedAsyncPostDbusCallThreadSafeWithMsgAndEmptyValueAction:: | |
| SimulateFailedAsyncPostDbusCallWithMsgAndEmptyValue(msg)); | |
| handleStorageVolumeMetricGet(share_async_resp_, "system", storageName, | |
| volumeName, metricId); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::internal_server_error); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(json["@odata.id"], | |
| "/redfish/v1/Systems/system/Storage/storage_nvme/Volumes/1/Oem/" | |
| "Google/Metrics/FirstMetric"); | |
| EXPECT_EQ(json["Name"], "FirstMetric"); | |
| } | |
| TEST_F(StorageSnapshotFixture, HandleChassisDriveGetInvalidDriveNotFound) { | |
| handleChassisDriveGet(app_, CreateRequest(), share_async_resp_, "platform1", | |
| "invalid_drive"); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::not_found); | |
| } | |
| TEST_F(StorageSnapshotFixture, HandleChassisDriveGetInvalidChassisNotFound) { | |
| handleChassisDriveGet(app_, CreateRequest(), share_async_resp_, | |
| "invalid_chassis", "invalid_drive"); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::not_found); | |
| } | |
| TEST_F(StorageSnapshotFixture, HandleChassisDriveGetEbadr) { | |
| managedStore::KeyType driveAssocKey( | |
| managedStore::ManagedType::kManagedProperty, | |
| "xyz.openbmc_project.ObjectMapper", | |
| sdbusplus::message::object_path( | |
| "/xyz/openbmc_project/inventory/system/board/platform1/drive"), | |
| "xyz.openbmc_project.Association", "endpoints"); | |
| ASSERT_TRUE( | |
| dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| driveAssocKey, CreateErrorValueType( | |
| dbus::utility::DbusVariantType{}, | |
| boost::system::error_code( | |
| EBADR, boost::system::generic_category()))) | |
| .ok()); | |
| handleChassisDriveGet(app_, CreateRequest(), share_async_resp_, "platform1", | |
| "invalid_drive"); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::not_found); | |
| } | |
| TEST_F(StorageSnapshotFixture, HandleChassisDriveGetEnoent) { | |
| managedStore::KeyType driveAssocKey( | |
| managedStore::ManagedType::kManagedProperty, | |
| "xyz.openbmc_project.ObjectMapper", | |
| sdbusplus::message::object_path( | |
| "/xyz/openbmc_project/inventory/system/board/platform1/drive"), | |
| "xyz.openbmc_project.Association", "endpoints"); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| driveAssocKey, | |
| CreateErrorValueType( | |
| dbus::utility::DbusVariantType{}, | |
| boost::system::error_code( | |
| ENOENT, boost::system::generic_category()))) | |
| .ok()); | |
| handleChassisDriveGet(app_, CreateRequest(), share_async_resp_, "platform1", | |
| "invalid_drive"); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::not_found); | |
| } | |
| TEST_F(StorageSnapshotFixture, HandleChassisDriveGetDbusError) { | |
| managedStore::KeyType driveAssocKey( | |
| managedStore::ManagedType::kManagedProperty, | |
| "xyz.openbmc_project.ObjectMapper", | |
| sdbusplus::message::object_path( | |
| "/xyz/openbmc_project/inventory/system/board/platform1/drive"), | |
| "xyz.openbmc_project.Association", "endpoints"); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| driveAssocKey, | |
| CreateErrorValueType(dbus::utility::DbusVariantType{}, | |
| boost::system::errc::make_error_code( | |
| boost::system::errc::io_error))) | |
| .ok()); | |
| handleChassisDriveGet(app_, CreateRequest(), share_async_resp_, "platform1", | |
| "invalid_drive"); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::internal_server_error); | |
| } | |
| TEST_F(StorageSnapshotFixture, BuildDriveFound) { | |
| KeyType storageSubtreeKey(ManagedType::kManagedSubtree, | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Storage"}); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| storageSubtreeKey, | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| dbus::utility::MapperGetSubTreeResponse{})) | |
| .ok()); | |
| std::string driveName = "drive_0"; | |
| dbus::utility::MapperGetSubTreeResponse mockSubtree = { | |
| {"/xyz/openbmc_project/inventory/drive_0", | |
| {{"xyz.openbmc_project.Drive", {}}}}, | |
| }; | |
| buildDrive(share_async_resp_, "platform1", driveName, | |
| boost::system::error_code{}, mockSubtree); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok); | |
| EXPECT_EQ(share_async_resp_->res.jsonValue["@odata.id"], | |
| "/redfish/v1/Chassis/platform1/Drives/drive_0"); | |
| EXPECT_EQ(share_async_resp_->res.jsonValue["Name"], "drive_0"); | |
| EXPECT_EQ(share_async_resp_->res.jsonValue["Id"], "drive_0"); | |
| EXPECT_EQ(share_async_resp_->res.jsonValue["Status"]["State"], "Enabled"); | |
| } | |
| TEST_F(StorageSnapshotFixture, BuildDriveNotFound) { | |
| dbus::utility::MapperGetSubTreeResponse mockSubtree = { | |
| {"/xyz/openbmc_project/inventory/other_drive", | |
| {{"xyz.openbmc_project.Drive", {}}}}, | |
| }; | |
| buildDrive(share_async_resp_, "platform1", "drive_0", | |
| boost::system::error_code{}, mockSubtree); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::not_found); | |
| } | |
| TEST_F(StorageSnapshotFixture, BuildDriveEmptyConnectionNames) { | |
| dbus::utility::MapperGetSubTreeResponse mockSubtree = { | |
| {"/xyz/openbmc_project/inventory/drive_0", {}}, | |
| }; | |
| buildDrive(share_async_resp_, "platform1", "drive_0", | |
| boost::system::error_code{}, mockSubtree); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::not_found); | |
| } | |
| TEST_F(StorageSnapshotFixture, BuildDriveDbusError) { | |
| buildDrive( | |
| share_async_resp_, "platform1", "drive_0", | |
| boost::system::errc::make_error_code(boost::system::errc::io_error), {}); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::internal_server_error); | |
| } | |
| TEST_F(StorageSnapshotFixture, HandleChassisDriveGetValidDrive) { | |
| KeyType storageSubtreeKey(ManagedType::kManagedSubtree, | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Storage"}); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| storageSubtreeKey, | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| dbus::utility::MapperGetSubTreeResponse{})) | |
| .ok()); | |
| managedStore::KeyType driveAssocKey( | |
| managedStore::ManagedType::kManagedProperty, | |
| "xyz.openbmc_project.ObjectMapper", | |
| sdbusplus::message::object_path( | |
| "/xyz/openbmc_project/inventory/system/board/platform1/drive"), | |
| "xyz.openbmc_project.Association", "endpoints"); | |
| dbus::utility::MapperEndPoints driveEndpoints = { | |
| "/xyz/openbmc_project/inventory/system/chassis/drive0"}; | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| driveAssocKey, | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| dbus::utility::DbusVariantType(driveEndpoints))) | |
| .ok()); | |
| managedStore::KeyType driveSubtreeKey( | |
| managedStore::ManagedType::kManagedSubtree, | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Drive"}); | |
| dbus::utility::MapperGetSubTreeResponse mockDriveSubtree = { | |
| {"/xyz/openbmc_project/inventory/system/chassis/drive0", | |
| {{"xyz.openbmc_project.DriveService", {}}}}, | |
| }; | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| driveSubtreeKey, | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(mockDriveSubtree))) | |
| .ok()); | |
| handleChassisDriveGet(app_, CreateRequest(), share_async_resp_, "platform1", | |
| "drive0"); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok); | |
| EXPECT_EQ(share_async_resp_->res.jsonValue["@odata.id"], | |
| "/redfish/v1/Chassis/platform1/Drives/drive0"); | |
| EXPECT_EQ(share_async_resp_->res.jsonValue["Name"], "drive0"); | |
| EXPECT_EQ(share_async_resp_->res.jsonValue["Id"], "drive0"); | |
| } | |
| TEST_F(StorageSnapshotFixture, HandleChassisDriveGetDriveNotInSubtree) { | |
| managedStore::KeyType driveAssocKey( | |
| managedStore::ManagedType::kManagedProperty, | |
| "xyz.openbmc_project.ObjectMapper", | |
| sdbusplus::message::object_path( | |
| "/xyz/openbmc_project/inventory/system/board/platform1/drive"), | |
| "xyz.openbmc_project.Association", "endpoints"); | |
| dbus::utility::MapperEndPoints driveEndpoints = { | |
| "/xyz/openbmc_project/inventory/system/chassis/drive0"}; | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| driveAssocKey, | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| dbus::utility::DbusVariantType(driveEndpoints))) | |
| .ok()); | |
| managedStore::KeyType driveSubtreeKey( | |
| managedStore::ManagedType::kManagedSubtree, | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Drive"}); | |
| dbus::utility::MapperGetSubTreeResponse mockDriveSubtree = {}; | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| driveSubtreeKey, | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| std::move(mockDriveSubtree))) | |
| .ok()); | |
| handleChassisDriveGet(app_, CreateRequest(), share_async_resp_, "platform1", | |
| "drive0"); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::not_found); | |
| } | |
| TEST_F(StorageSnapshotFixture, HandlePostDriveResetActionDbusError) { | |
| managedStore::KeyType driveAssocKey( | |
| managedStore::ManagedType::kManagedProperty, | |
| "xyz.openbmc_project.ObjectMapper", | |
| sdbusplus::message::object_path( | |
| "/xyz/openbmc_project/inventory/system/board/platform1/drive"), | |
| "xyz.openbmc_project.Association", "endpoints"); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| driveAssocKey, | |
| CreateErrorValueType(dbus::utility::DbusVariantType{}, | |
| boost::system::errc::make_error_code( | |
| boost::system::errc::io_error))) | |
| .ok()); | |
| handlePostDriveResetAction(app_, CreateRequest(), share_async_resp_, | |
| "platform1", "drive_0"); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::internal_server_error); | |
| } | |
| TEST_F(StorageSnapshotFixture, HandleGetDriveResetActionInfoDbusError) { | |
| managedStore::KeyType driveAssocKey( | |
| managedStore::ManagedType::kManagedProperty, | |
| "xyz.openbmc_project.ObjectMapper", | |
| sdbusplus::message::object_path( | |
| "/xyz/openbmc_project/inventory/system/board/platform1/drive"), | |
| "xyz.openbmc_project.Association", "endpoints"); | |
| ASSERT_TRUE(dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()) | |
| ->upsertMockObjectIntoManagedStore( | |
| driveAssocKey, | |
| CreateErrorValueType(dbus::utility::DbusVariantType{}, | |
| boost::system::errc::make_error_code( | |
| boost::system::errc::io_error))) | |
| .ok()); | |
| handleGetDriveResetActionInfo(app_, CreateRequest(), share_async_resp_, | |
| "platform1", "drive_0"); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::internal_server_error); | |
| } | |
| managedStore::MockSerializedManagedObjectStore* GetMockStore() { | |
| return dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()); | |
| } | |
| void SetupMockSupportedFormats( | |
| managedStore::MockSerializedManagedObjectStore* mock_store, | |
| const std::vector<std::tuple<size_t, size_t, size_t, std::string>>& | |
| formats, | |
| // NOLINTNEXTLINE(misc-include-cleaner) | |
| std::optional<boost::system::error_code> ec = std::nullopt, | |
| const std::string& storagePath = | |
| "/xyz/openbmc_project/inventory/system/board/test_board/storage0", | |
| const std::string& service = "xyz.openbmc_project.VolumeService") { | |
| auto val = ec ? managedStore::MockManagedStoreTest::CreateErrorValueType( | |
| DbusVariantType(formats), std::move(*ec)) | |
| : managedStore::MockManagedStoreTest::CreateValueType( | |
| DbusVariantType(formats)); | |
| ASSERT_TRUE( | |
| mock_store | |
| ->upsertMockObjectIntoManagedStore( | |
| KeyType(ManagedType::kManagedProperty, service, | |
| sdbusplus::message::object_path(storagePath), | |
| "xyz.openbmc_project.Nvme.Storage", "SupportedFormats"), | |
| std::move(val)) | |
| .ok()); | |
| } | |
| void PopulateTestStorageVolume( | |
| const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | |
| const std::string& systemName = "system1", | |
| const std::string& storageId = "storage0", | |
| const std::string& volumeId = "volume0", | |
| const std::string& service = "xyz.openbmc_project.VolumeService", | |
| const std::string& volumePath = | |
| "/xyz/openbmc_project/inventory/system/board/test_board/storage0/" | |
| "volumes/volume0") { | |
| dbus::utility::MapperServiceMap ifaces = { | |
| {service, | |
| {"xyz.openbmc_project.Inventory.Item.Volume", | |
| "xyz.openbmc_project.Nvme.Volume"}}}; | |
| populateStorageVolume(asyncResp, systemName, storageId, volumeId, service, | |
| volumePath, ifaces); | |
| } | |
| void SetupMockVolumeProperties( | |
| managedStore::MockSerializedManagedObjectStore* mock_store, | |
| const std::string& volumePath = | |
| "/xyz/openbmc_project/inventory/system/board/test_board/storage0/" | |
| "volumes/volume0", | |
| const std::string& service = "xyz.openbmc_project.VolumeService", | |
| bool withNvme = true, uint64_t size = 1024 * 1024 * 1024, | |
| uint64_t blockSize = 4096, uint32_t namespaceId = 1, | |
| uint64_t lbaFormat = 1) { | |
| dbus::utility::DBusPropertiesMap mock_vol_props = { | |
| {"Size", DbusVariantType(size)}, | |
| {"BlockSize", DbusVariantType(blockSize)}}; | |
| ASSERT_TRUE(mock_store | |
| ->upsertMockObjectIntoManagedStore( | |
| KeyType(ManagedType::kManagedPropertyMap, service, | |
| sdbusplus::message::object_path(volumePath), | |
| "xyz.openbmc_project.Inventory.Item.Volume"), | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| mock_vol_props)) | |
| .ok()); | |
| if (withNvme) { | |
| dbus::utility::DBusPropertiesMap mock_nvme_vol_props = { | |
| {"NamespaceId", DbusVariantType(namespaceId)}, | |
| {"LBAFormat", DbusVariantType(lbaFormat)}}; | |
| ASSERT_TRUE(mock_store | |
| ->upsertMockObjectIntoManagedStore( | |
| KeyType(ManagedType::kManagedPropertyMap, service, | |
| sdbusplus::message::object_path(volumePath), | |
| "xyz.openbmc_project.Nvme.Volume"), | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| mock_nvme_vol_props)) | |
| .ok()); | |
| } | |
| } | |
| crow::Request CreateFormatRequest(std::string_view body) { | |
| boost::beast::http::request<boost::beast::http::string_body> beastReq; | |
| beastReq.set(boost::beast::http::field::content_type, "application/json"); | |
| beastReq.body() = body; | |
| std::error_code ec; | |
| crow::Request req(beastReq, ec); | |
| if (ec) { | |
| throw std::runtime_error("Failed to create request"); | |
| } | |
| return req; | |
| } | |
| crow::Request CreateDefaultFormatRequest() { | |
| return CreateFormatRequest(R"({ | |
| "LBAFormat": "LBAFormat1", | |
| "SecureEraseSetting": "CryptoErase", | |
| "DataProtectionType": "None" | |
| })"); | |
| } | |
| void ExpectFormatDbusCall( | |
| managedStore::MockSerializedManagedObjectStore* mock_store, | |
| const std::string& expected_service, const std::string& expected_path, | |
| const std::string& expected_interface) { | |
| EXPECT_CALL( | |
| *mock_store, | |
| PostDbusCallToIoContextThreadSafe( | |
| testing::_, | |
| testing::An< | |
| absl::AnyInvocable<void(const boost::system::error_code&, | |
| const sdbusplus::message_t&)>&&>(), | |
| testing::TypedEq<const std::string&>(expected_service), | |
| testing::TypedEq<const std::string&>(expected_path), | |
| testing::TypedEq<const std::string&>(expected_interface), | |
| testing::TypedEq<const std::string&>("Format"), | |
| testing::TypedEq<uint8_t>(1), | |
| testing::TypedEq<const std::string&>( | |
| "xyz.openbmc_project.NVMe.Format.SecureEraseSetting.CryptoErase"), | |
| testing::TypedEq<const std::string&>( | |
| "xyz.openbmc_project.NVMe.Format.DataProtectionType.None"))) | |
| .WillOnce([mock_store]( | |
| const std::shared_ptr<boost::asio::io_context::strand>&, | |
| absl::AnyInvocable<void(const boost::system::error_code&, | |
| const sdbusplus::message_t&)>&& | |
| callback, | |
| const std::string&, const std::string&, const std::string&, | |
| const std::string&, uint8_t, const std::string&, | |
| const std::string&) { | |
| auto shared_cb = std::make_shared<absl::AnyInvocable<void( | |
| const boost::system::error_code&, const sdbusplus::message_t&)>>( | |
| std::move(callback)); | |
| boost::asio::post(mock_store->GetIoContext(), [shared_cb]() mutable { | |
| sdbusplus::message_t dummy_msg; | |
| (*shared_cb)(boost::system::errc::make_error_code( | |
| boost::system::errc:: | |
| success), // NOLINT(misc-include-cleaner) | |
| dummy_msg); | |
| }); | |
| }); | |
| } | |
| TEST_F(SnapshotFixture, PopulateStorageVolumeExposesSupportedLBAFormats) { | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| SetupMockVolumeProperties(mock_store); | |
| SetupMockSupportedFormats( | |
| mock_store, | |
| {{0, 512, 0, | |
| "xyz.openbmc_project.Nvme.Storage.RelativePerformance.Better"}, | |
| {1, 4096, 0, | |
| "xyz.openbmc_project.Nvme.Storage.RelativePerformance.Best"}}); | |
| PopulateTestStorageVolume(share_async_resp_); | |
| RunIoUntilDone(); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(json["Id"], "volume0"); | |
| EXPECT_EQ(json["NVMeNamespaceProperties"]["SupportedLBAFormats"], | |
| nlohmann::json::parse(R"([ | |
| { | |
| "BlockSize": 512, | |
| "Format": "LBAFormat0", | |
| "MetadataSize": 0, | |
| "RelativePerformance": "Better" | |
| }, | |
| { | |
| "BlockSize": 4096, | |
| "Format": "LBAFormat1", | |
| "MetadataSize": 0, | |
| "RelativePerformance": "Best" | |
| } | |
| ])")); | |
| } | |
| TEST_F(SnapshotFixture, PopulateStorageVolumeSupportedFormatsError) { | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| SetupMockVolumeProperties(mock_store); | |
| SetupMockSupportedFormats( | |
| mock_store, {}, | |
| boost::system::errc::make_error_code( | |
| boost::system::errc::io_error)); // NOLINT(misc-include-cleaner) | |
| PopulateTestStorageVolume(share_async_resp_); | |
| RunIoUntilDone(); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(json["Id"], "volume0"); | |
| ASSERT_TRUE(json.contains("NVMeNamespaceProperties")); | |
| EXPECT_FALSE(json["NVMeNamespaceProperties"].contains("SupportedLBAFormats")); | |
| } | |
| TEST_F(SnapshotFixture, PopulateStorageVolumeRelativePerformanceVariants) { | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| SetupMockVolumeProperties(mock_store); | |
| SetupMockSupportedFormats( | |
| mock_store, | |
| {{0, 512, 0, "xyz.openbmc_project.Nvme.Storage.RelativePerformance.Good"}, | |
| {1, 4096, 0, | |
| "xyz.openbmc_project.Nvme.Storage.RelativePerformance.Unknown"}}); | |
| PopulateTestStorageVolume(share_async_resp_); | |
| RunIoUntilDone(); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(json["NVMeNamespaceProperties"]["SupportedLBAFormats"], | |
| nlohmann::json::parse(R"([ | |
| { | |
| "BlockSize": 512, | |
| "Format": "LBAFormat0", | |
| "MetadataSize": 0, | |
| "RelativePerformance": "Good" | |
| }, | |
| { | |
| "BlockSize": 4096, | |
| "Format": "LBAFormat1", | |
| "MetadataSize": 0, | |
| "RelativePerformance": "Degraded" | |
| } | |
| ])")); | |
| } | |
| void SetupMockStorageSubtree( | |
| managedStore::MockSerializedManagedObjectStore* mock_store, | |
| bool withNvme = true, | |
| const std::string& storagePath = | |
| "/xyz/openbmc_project/inventory/system/board/test_board/storage0", | |
| const std::string& service = "xyz.openbmc_project.StorageService") { | |
| std::vector<std::string> ifaces = { | |
| "xyz.openbmc_project.Inventory.Item.Storage"}; | |
| if (withNvme) { | |
| ifaces.push_back("xyz.openbmc_project.Nvme.Storage"); | |
| } | |
| dbus::utility::MapperGetSubTreeResponse mock_subtree = { | |
| {storagePath, {{service, std::move(ifaces)}}}}; | |
| ASSERT_TRUE( | |
| mock_store | |
| ->upsertMockObjectIntoManagedStore( | |
| KeyType(ManagedType::kManagedSubtree, | |
| sdbusplus::message::object_path( | |
| "/xyz/openbmc_project/inventory"), | |
| 0, | |
| std::vector<std::string>{ | |
| "xyz.openbmc_project.Inventory.Item.Storage"}), | |
| managedStore::MockManagedStoreTest::CreateValueType(mock_subtree)) | |
| .ok()); | |
| } | |
| TEST_F(SnapshotFixture, HandleGetSystemStorageWithNvmeExposesFormatAction) { | |
| auto* mock_store = | |
| dynamic_cast<managedStore::MockSerializedManagedObjectStore*>( | |
| managedStore::GetManagedObjectStore()); | |
| ASSERT_NE(mock_store, nullptr); | |
| SetupMockStorageSubtree(mock_store, /*withNvme=*/true); | |
| crow::Request req = CreateRequest(); | |
| handleGetSystemStorage(app_, req, share_async_resp_, "system1", "storage0"); | |
| RunIoUntilDone(); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(json["Id"], "storage0"); | |
| EXPECT_EQ(json["Actions"]["Oem"]["#GoogleStorage.Format"]["target"], | |
| "/redfish/v1/Systems/system1/Storage/storage0/Actions/Oem/" | |
| "GoogleStorage.Format"); | |
| } | |
| TEST_F(SnapshotFixture, HandleGetSystemStorageWithoutNvmeOmitsFormatAction) { | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| SetupMockStorageSubtree(mock_store, /*withNvme=*/false); | |
| crow::Request req = CreateRequest(); | |
| handleGetSystemStorage(app_, req, share_async_resp_, "system1", "storage0"); | |
| RunIoUntilDone(); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(json["Id"], "storage0"); | |
| EXPECT_FALSE(json.contains("Actions")); | |
| } | |
| TEST_F(SnapshotFixture, HandleGetStorageWithNvmeExposesFormatAction) { | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| SetupMockStorageSubtree(mock_store, /*withNvme=*/true); | |
| crow::Request req = CreateRequest(); | |
| handleGetStorage(app_, req, share_async_resp_, "storage0"); | |
| RunIoUntilDone(); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(json["Id"], "storage0"); | |
| EXPECT_EQ(json["Actions"]["Oem"]["#GoogleStorage.Format"]["target"], | |
| "/redfish/v1/Storage/storage0/Actions/Oem/GoogleStorage.Format"); | |
| } | |
| TEST_F(SnapshotFixture, HandleGetStorageWithoutNvmeOmitsFormatAction) { | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| SetupMockStorageSubtree(mock_store, /*withNvme=*/false); | |
| crow::Request req = CreateRequest(); | |
| handleGetStorage(app_, req, share_async_resp_, "storage0"); | |
| RunIoUntilDone(); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(json["Id"], "storage0"); | |
| EXPECT_FALSE(json.contains("Actions")); | |
| } | |
| TEST_F(SnapshotFixture, HandleFormatActionSuccess) { | |
| share_async_resp_ = std::make_shared<bmcweb::AsyncResp>(); | |
| crow::Request req = CreateDefaultFormatRequest(); | |
| std::optional<FormatParameters> params = | |
| parseFormatParameters(req, share_async_resp_); | |
| ASSERT_TRUE(params.has_value()); | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| ExpectFormatDbusCall( | |
| mock_store, "xyz.openbmc_project.NVMe.Controller", | |
| "/xyz/openbmc_project/inventory/system/board/test_board/controller0", | |
| "xyz.openbmc_project.NVMe.Controller"); | |
| handleFormatAction( | |
| share_async_resp_, | |
| "/xyz/openbmc_project/inventory/system/board/test_board/controller0", | |
| "xyz.openbmc_project.NVMe.Controller", | |
| "xyz.openbmc_project.NVMe.Controller", *params); | |
| RunIoUntilDone(); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok); | |
| EXPECT_TRUE(json.contains("@Message.ExtendedInfo")); | |
| } | |
| TEST_F(SnapshotFixture, HandleFormatActionCryptographicEraseSuccess) { | |
| crow::Request req = CreateFormatRequest(R"({ | |
| "LBAFormat": "LBAFormat0", | |
| "SecureEraseSetting": "CryptographicErase", | |
| "DataProtectionType": "None" | |
| })"); | |
| std::optional<FormatParameters> params = | |
| parseFormatParameters(req, share_async_resp_); | |
| ASSERT_TRUE(params.has_value()); | |
| EXPECT_EQ(params->lbaFormat, 0); | |
| EXPECT_EQ(params->secureEraseSetting, SecureEraseSetting::kCryptoErase); | |
| EXPECT_EQ(toDbusString(params->secureEraseSetting), | |
| "xyz.openbmc_project.NVMe.Format.SecureEraseSetting.CryptoErase"); | |
| EXPECT_EQ(params->dataProtectionType, DataProtectionType::kNone); | |
| EXPECT_EQ(toDbusString(params->dataProtectionType), | |
| "xyz.openbmc_project.NVMe.Format.DataProtectionType.None"); | |
| } | |
| TEST_F(SnapshotFixture, HandleFormatActionLBAFormatOutOfBounds) { | |
| crow::Request req256 = CreateFormatRequest(R"({ | |
| "LBAFormat": "LBAFormat256", | |
| "SecureEraseSetting": "CryptoErase", | |
| "DataProtectionType": "None" | |
| })"); | |
| std::optional<FormatParameters> params256 = | |
| parseFormatParameters(req256, share_async_resp_); | |
| EXPECT_FALSE(params256.has_value()); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::bad_request); | |
| share_async_resp_->res.result(boost::beast::http::status::ok); | |
| crow::Request req16 = CreateFormatRequest(R"({ | |
| "LBAFormat": "LBAFormat16", | |
| "SecureEraseSetting": "CryptoErase", | |
| "DataProtectionType": "None" | |
| })"); | |
| std::optional<FormatParameters> params16 = | |
| parseFormatParameters(req16, share_async_resp_); | |
| EXPECT_FALSE(params16.has_value()); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::bad_request); | |
| } | |
| TEST_F(SnapshotFixture, HandleFormatActionInvalidLBAFormat) { | |
| crow::Request req = CreateFormatRequest(R"({ | |
| "LBAFormat": "InvalidFormat1", | |
| "SecureEraseSetting": "CryptoErase", | |
| "DataProtectionType": "None" | |
| })"); | |
| std::optional<FormatParameters> params = | |
| parseFormatParameters(req, share_async_resp_); | |
| EXPECT_FALSE(params.has_value()); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::bad_request); | |
| } | |
| TEST_F(SnapshotFixture, HandleFormatActionInvalidSecureEraseSetting) { | |
| crow::Request req = CreateFormatRequest(R"({ | |
| "LBAFormat": "LBAFormat1", | |
| "SecureEraseSetting": "InvalidSetting", | |
| "DataProtectionType": "None" | |
| })"); | |
| std::optional<FormatParameters> params = | |
| parseFormatParameters(req, share_async_resp_); | |
| EXPECT_FALSE(params.has_value()); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::bad_request); | |
| } | |
| TEST_F(SnapshotFixture, HandleFormatActionUnsupportedUserDataErase) { | |
| crow::Request req = CreateFormatRequest(R"({ | |
| "LBAFormat": "LBAFormat1", | |
| "SecureEraseSetting": "UserDataErase", | |
| "DataProtectionType": "None" | |
| })"); | |
| std::optional<FormatParameters> params = | |
| parseFormatParameters(req, share_async_resp_); | |
| EXPECT_FALSE(params.has_value()); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::bad_request); | |
| } | |
| TEST_F(SnapshotFixture, HandleFormatActionInvalidDataProtectionType) { | |
| crow::Request req = CreateFormatRequest(R"({ | |
| "LBAFormat": "LBAFormat1", | |
| "SecureEraseSetting": "CryptoErase", | |
| "DataProtectionType": "InvalidType" | |
| })"); | |
| std::optional<FormatParameters> params = | |
| parseFormatParameters(req, share_async_resp_); | |
| EXPECT_FALSE(params.has_value()); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::bad_request); | |
| } | |
| TEST_F(SnapshotFixture, HandleFormatActionMissingParameter) { | |
| crow::Request req = CreateFormatRequest(R"({ | |
| "LBAFormat": "LBAFormat1" | |
| })"); | |
| std::optional<FormatParameters> params = | |
| parseFormatParameters(req, share_async_resp_); | |
| EXPECT_FALSE(params.has_value()); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::bad_request); | |
| EXPECT_FALSE(json.empty()); | |
| } | |
| void SetupMockStorageAndSystem( | |
| managedStore::MockSerializedManagedObjectStore* mock_store, | |
| const std::string& storageId = "storage0", | |
| const std::string& service = "xyz.openbmc_project.StorageService") { | |
| // Mock getSubTree for Storage | |
| managedStore::KeyType storageKey( | |
| managedStore::ManagedType::kManagedSubtree, | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Storage"}); | |
| dbus::utility::MapperGetSubTreeResponse mockStorageSubTree = { | |
| {"/xyz/openbmc_project/inventory/system/storage/" + storageId, | |
| {{service, {"xyz.openbmc_project.Inventory.Item.Storage"}}}}}; | |
| ASSERT_TRUE( | |
| mock_store | |
| ->upsertMockObjectIntoManagedStore( | |
| storageKey, managedStore::MockManagedStoreTest::CreateValueType( | |
| mockStorageSubTree)) | |
| .ok()); | |
| // Mock getAssociatedSubTreePaths for System association | |
| managedStore::KeyType systemKey( | |
| managedStore::ManagedType::kManagedAssociatedSubtreePaths, | |
| "/xyz/openbmc_project/inventory/system/storage/" + storageId + | |
| "/contained_by", | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.System"}); | |
| std::vector<std::string> mockSystems = { | |
| "/xyz/openbmc_project/inventory/system"}; | |
| ASSERT_TRUE( | |
| mock_store | |
| ->upsertMockObjectIntoManagedStore( | |
| systemKey, | |
| managedStore::MockManagedStoreTest::CreateValueType(mockSystems)) | |
| .ok()); | |
| } | |
| TEST_F(SnapshotFixture, HandlePostStorageFormatSuccess) { | |
| crow::Request req = CreateDefaultFormatRequest(); | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| SetupMockStorageAndSystem(mock_store); | |
| ExpectFormatDbusCall(mock_store, "xyz.openbmc_project.StorageService", | |
| "/xyz/openbmc_project/inventory/system/storage/storage0", | |
| "xyz.openbmc_project.Nvme.Storage"); | |
| handlePostStorageFormat(app_, req, share_async_resp_, "system", "storage0"); | |
| RunIoUntilDone(); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok); | |
| EXPECT_TRUE(json.contains("@Message.ExtendedInfo")); | |
| } | |
| TEST_F(SnapshotFixture, HandlePostStorageFormatTopLevelRouteSuccess) { | |
| crow::Request req = CreateDefaultFormatRequest(); | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| // Mock getSubTree for Storage (needed by handler) | |
| managedStore::KeyType storageKey( | |
| managedStore::ManagedType::kManagedSubtree, | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Storage"}); | |
| dbus::utility::MapperGetSubTreeResponse mockStorageSubTree = { | |
| {"/xyz/openbmc_project/inventory/system/storage/storage0", | |
| {{"xyz.openbmc_project.StorageService", | |
| {"xyz.openbmc_project.Inventory.Item.Storage"}}}}}; | |
| ASSERT_TRUE( | |
| mock_store | |
| ->upsertMockObjectIntoManagedStore( | |
| storageKey, managedStore::MockManagedStoreTest::CreateValueType( | |
| mockStorageSubTree)) | |
| .ok()); | |
| // Note: No mock for System association needed because we skip it! | |
| ExpectFormatDbusCall(mock_store, "xyz.openbmc_project.StorageService", | |
| "/xyz/openbmc_project/inventory/system/storage/storage0", | |
| "xyz.openbmc_project.Nvme.Storage"); | |
| // Pass empty string for systemName to simulate top-level route | |
| handlePostStorageFormat(app_, req, share_async_resp_, "", "storage0"); | |
| RunIoUntilDone(); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok); | |
| EXPECT_TRUE(json.contains("@Message.ExtendedInfo")); | |
| } | |
| void SetupMockController( | |
| managedStore::MockSerializedManagedObjectStore* mock_store, | |
| const std::string& storageId = "storage0", | |
| const std::string& controllerId = "controller0", | |
| const std::string& service = "xyz.openbmc_project.ControllerService", | |
| bool hasNvmeControllerIface = true) { | |
| std::vector<std::string> ifaces = { | |
| "xyz.openbmc_project.Inventory.Item.StorageController"}; | |
| if (hasNvmeControllerIface) { | |
| ifaces.push_back("xyz.openbmc_project.Nvme.Controller"); | |
| } | |
| // Mock getAssociatedSubTree for Controllers | |
| managedStore::KeyType controllersKey( | |
| managedStore::ManagedType::kManagedAssociatedSubtree, | |
| "/xyz/openbmc_project/inventory/system/storage/" + storageId + | |
| "/storage_controller", | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.StorageController"}); | |
| dbus::utility::MapperGetSubTreeResponse mockControllersSubTree = { | |
| {"/xyz/openbmc_project/inventory/system/storage/" + storageId + | |
| "/storage_controller/" + controllerId, | |
| {{service, std::move(ifaces)}}}}; | |
| ASSERT_TRUE(mock_store | |
| ->upsertMockObjectIntoManagedStore( | |
| controllersKey, | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| mockControllersSubTree)) | |
| .ok()); | |
| // Mock getAssociatedSubTreePaths for Controller's Storage association | |
| managedStore::KeyType controllerStorageKey( | |
| managedStore::ManagedType::kManagedAssociatedSubtreePaths, | |
| "/xyz/openbmc_project/inventory/system/storage/" + storageId + | |
| "/storage_controller/" + controllerId + "/storage", | |
| "/xyz/openbmc_project/inventory", 0, | |
| {"xyz.openbmc_project.Inventory.Item.Storage"}); | |
| std::vector<std::string> mockControllerStorages = { | |
| "/xyz/openbmc_project/inventory/system/storage/" + storageId}; | |
| ASSERT_TRUE(mock_store | |
| ->upsertMockObjectIntoManagedStore( | |
| controllerStorageKey, | |
| managedStore::MockManagedStoreTest::CreateValueType( | |
| mockControllerStorages)) | |
| .ok()); | |
| } | |
| TEST_F(SnapshotFixture, HandlePostStorageControllerFormatSuccess) { | |
| crow::Request req = CreateDefaultFormatRequest(); | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| SetupMockStorageAndSystem(mock_store); | |
| SetupMockController(mock_store, "storage0", "controller0", | |
| "xyz.openbmc_project.ControllerService"); | |
| ExpectFormatDbusCall(mock_store, "xyz.openbmc_project.ControllerService", | |
| "/xyz/openbmc_project/inventory/system/storage/storage0/" | |
| "storage_controller/controller0", | |
| "xyz.openbmc_project.Nvme.Controller"); | |
| handlePostStorageControllerFormat(app_, req, share_async_resp_, "system", | |
| "storage0", "controller0"); | |
| RunIoUntilDone(); | |
| nlohmann::json& json = share_async_resp_->res.jsonValue; | |
| EXPECT_EQ(share_async_resp_->res.result(), boost::beast::http::status::ok); | |
| EXPECT_TRUE(json.contains("@Message.ExtendedInfo")); | |
| } | |
| TEST_F(SnapshotFixture, HandlePostStorageControllerFormatControllerNotFound) { | |
| crow::Request req = CreateDefaultFormatRequest(); | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| SetupMockStorageAndSystem(mock_store); | |
| SetupMockController(mock_store, "storage0", "controller0", | |
| "xyz.openbmc_project.ControllerService"); | |
| handlePostStorageControllerFormat(app_, req, share_async_resp_, "system", | |
| "storage0", "nonexistent_controller"); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::not_found); | |
| } | |
| TEST_F(SnapshotFixture, | |
| HandlePostStorageControllerFormatNonNvmeActionNotSupported) { | |
| crow::Request req = CreateDefaultFormatRequest(); | |
| auto* mock_store = GetMockStore(); | |
| ASSERT_NE(mock_store, nullptr); | |
| SetupMockStorageAndSystem(mock_store); | |
| SetupMockController(mock_store, "storage0", "controller0", | |
| "xyz.openbmc_project.ControllerService", | |
| /*hasNvmeControllerIface=*/false); | |
| handlePostStorageControllerFormat(app_, req, share_async_resp_, "system", | |
| "storage0", "controller0"); | |
| RunIoUntilDone(); | |
| EXPECT_EQ(share_async_resp_->res.result(), | |
| boost::beast::http::status::bad_request); | |
| } | |
| } // namespace | |
| } // namespace redfish |