From 32d7d8ebeaa642950b4ea58af5580f8ad0822c41 Mon Sep 17 00:00:00 2001 From: Carson Labrado Date: Tue, 10 Jan 2023 17:23:16 +0000 Subject: Aggregation: Fix and forward all responses We only attempt prefix matching when we receive a 200 response. For the retry policy we consider 2XX and 404 to be valid codes. Instead we should forward all responses to the client and let them decide what action they want to take. As part of that we should always attempt to do prefix fixing on the response. Also fixes an oversight where we attempt to do prefix fixing on "OriginOfCondition" properties. That property is only a URI when it is an Action parameter in a SubmitTestEvent request. It is an object when it appears as a response property. Adds test cases for the above fixes. Tested: Tests pass. Queries to top level collections and aggregated URIs still return expected results with added prefixes. Signed-off-by: Carson Labrado Change-Id: Ic76324ceab618160061be5f3c687af20a857fa25 --- .../include/redfish_aggregator_test.cpp | 107 ++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/redfish-core/include/redfish_aggregator_test.cpp b/test/redfish-core/include/redfish_aggregator_test.cpp index 1b3f9244be..665446c137 100644 --- a/test/redfish-core/include/redfish_aggregator_test.cpp +++ b/test/redfish-core/include/redfish_aggregator_test.cpp @@ -1,9 +1,13 @@ #include "redfish_aggregator.hpp" +#include + #include // IWYU pragma: keep namespace redfish { +namespace +{ TEST(IsPropertyUri, SupportedPropertyReturnsTrue) { @@ -11,7 +15,6 @@ TEST(IsPropertyUri, SupportedPropertyReturnsTrue) EXPECT_TRUE(isPropertyUri("@odata.id")); EXPECT_TRUE(isPropertyUri("Image")); EXPECT_TRUE(isPropertyUri("MetricProperty")); - EXPECT_TRUE(isPropertyUri("OriginOfCondition")); EXPECT_TRUE(isPropertyUri("TaskMonitor")); EXPECT_TRUE(isPropertyUri("target")); } @@ -29,6 +32,7 @@ TEST(IsPropertyUri, SpeificallyIgnoredPropertyReturnsFalse) EXPECT_FALSE(isPropertyUri("@odata.context")); EXPECT_FALSE(isPropertyUri("Destination")); EXPECT_FALSE(isPropertyUri("HostName")); + EXPECT_FALSE(isPropertyUri("OriginOfCondition")); } TEST(IsPropertyUri, UnsupportedPropertyReturnsFalse) @@ -136,4 +140,105 @@ TEST(addPrefixToItem, TopLevelCollections) EXPECT_EQ(jsonRequest["@odata.id"], initial); } } + +TEST(addPrefixes, ParseJsonObject) +{ + nlohmann::json parameter; + parameter["Name"] = "/redfish/v1/Chassis/fakeName"; + parameter["@odata.id"] = "/redfish/v1/Chassis/fakeChassis"; + + addPrefixes(parameter, "abcd"); + EXPECT_EQ(parameter["Name"], "/redfish/v1/Chassis/fakeName"); + EXPECT_EQ(parameter["@odata.id"], "/redfish/v1/Chassis/abcd_fakeChassis"); +} + +TEST(addPrefixes, ParseJsonArray) +{ + nlohmann::json array = nlohmann::json::parse(R"( + { + "Conditions": [ + { + "Message": "This is a test", + "@odata.id": "/redfish/v1/Chassis/TestChassis" + }, + { + "Message": "This is also a test", + "@odata.id": "/redfish/v1/Chassis/TestChassis2" + } + ] + } + )", + nullptr, false); + + addPrefixes(array, "5B42"); + EXPECT_EQ(array["Conditions"][0]["@odata.id"], + "/redfish/v1/Chassis/5B42_TestChassis"); + EXPECT_EQ(array["Conditions"][1]["@odata.id"], + "/redfish/v1/Chassis/5B42_TestChassis2"); +} + +TEST(addPrefixes, ParseJsonObjectNestedArray) +{ + nlohmann::json objWithArray = nlohmann::json::parse(R"( + { + "Status": { + "Conditions": [ + { + "Message": "This is a test", + "MessageId": "Test", + "OriginOfCondition": { + "@odata.id": "/redfish/v1/Chassis/TestChassis" + }, + "Severity": "Critical" + } + ], + "Health": "Critical", + "State": "Enabled" + } + } + )", + nullptr, false); + + addPrefixes(objWithArray, "5B42"); + nlohmann::json& array = objWithArray["Status"]["Conditions"]; + EXPECT_EQ(array[0]["OriginOfCondition"]["@odata.id"], + "/redfish/v1/Chassis/5B42_TestChassis"); +} + +// Attempts to perform prefix fixing on a response with response code "result". +// Fixing should always occur +void assertProcessResponse(unsigned result) +{ + nlohmann::json jsonResp; + jsonResp["@odata.id"] = "/redfish/v1/Chassis/TestChassis"; + jsonResp["Name"] = "Test"; + + crow::Response resp; + resp.body() = + jsonResp.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); + resp.addHeader("Content-Type", "application/json"); + resp.result(result); + + auto asyncResp = std::make_shared(); + RedfishAggregator::processResponse("prefix", asyncResp, resp); + + EXPECT_EQ(asyncResp->res.jsonValue["Name"], "Test"); + EXPECT_EQ(asyncResp->res.jsonValue["@odata.id"], + "/redfish/v1/Chassis/prefix_TestChassis"); + EXPECT_EQ(asyncResp->res.resultInt(), result); +} + +TEST(processResponse, validResponseCodes) +{ + assertProcessResponse(100); + assertProcessResponse(200); + assertProcessResponse(204); + assertProcessResponse(300); + assertProcessResponse(404); + assertProcessResponse(405); + assertProcessResponse(500); + assertProcessResponse(507); +} + +} // namespace } // namespace redfish -- cgit v1.2.3