Skip to content

Commit

Permalink
[Thinkit] Adding thinkit_gnmi_interface_util_tests file. (#731)
Browse files Browse the repository at this point in the history
Co-authored-by: kishanps <[email protected]>
  • Loading branch information
divyagayathri-hcl and kishanps authored Nov 19, 2024
1 parent 15d2de5 commit 9a0bf35
Show file tree
Hide file tree
Showing 2 changed files with 301 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,23 @@ cc_library(
"@com_google_googletest//:gtest",
],
)

cc_test(
name = "thinkit_gnmi_interface_util_tests",
srcs = ["thinkit_gnmi_interface_util_tests.cc"],
deps = [
":thinkit_gnmi_tests",
"//gutil:proto_matchers",
"//gutil:status_matchers",
"//thinkit:mock_ssh_client",
"//thinkit:mock_switch",
"@com_github_gnmi//proto/gnmi:gnmi_cc_proto",
"@com_github_gnmi//proto/gnmi:gnmi_cc_grpc_proto",
"@com_github_nlohmann_json//:nlohmann_json",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
"@com_google_protobuf//:protobuf",
],
)
281 changes: 281 additions & 0 deletions tests/thinkit_gnmi_interface_util_tests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
#include <cstdio>
#include <memory>
#include <string>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "gmock/gmock.h"
#include "google/protobuf/text_format.h"
#include "gtest/gtest.h"
#include "gutil/proto_matchers.h"
#include "gutil/status_matchers.h"
#include "include/nlohmann/json.hpp"
#include "proto/gnmi/gnmi.grpc.pb.h"
#include "proto/gnmi/gnmi.pb.h"
#include "proto/gnmi/gnmi_mock.grpc.pb.h"
#include "tests/thinkit_gnmi_interface_util.h"
#include "tests/thinkit_util.h"
#include "thinkit/mock_ssh_client.h"
#include "thinkit/mock_switch.h"

namespace pins_test {
using gutil::EqualsProto;
using gutil::StatusIs;
using ::nlohmann::json;
using ::testing::_;
using ::testing::ContainerEq;
using ::testing::DoAll;
using ::testing::HasSubstr;
using ::testing::Return;
using ::testing::ReturnRefOfCopy;
using ::testing::SetArgPointee;

class GNMIThinkitInterfaceUtilityTest : public ::testing::Test {
protected:
void SetUp() override {
ON_CALL(mock_switch_, ChassisName())
.WillByDefault(ReturnRefOfCopy(std::string("chassis_1")));
}
thinkit::MockSSHClient mock_ssh_client_;
thinkit::MockSwitch mock_switch_;
gnmi::MockgNMIStub mock_gnmi_stub_;
};

TEST_F(GNMIThinkitInterfaceUtilityTest,
TestGetSupportedBreakoutModesForPortAnySuccess) {
const std::string port = "Ethernet0";
std::vector<std::string> expected_breakout_modes = {
"1x400G", "2x200G", "2x100G", "2x40G", "4x100G"};

const std::string interface_info =
R"pb({ "breakout_modes": "1x400G, 2x200G[100G,40G], 4x100G" }
)pb";
EXPECT_THAT(pins_test::GetSupportedBreakoutModesForPort(interface_info, port),
expected_breakout_modes);
}

TEST_F(GNMIThinkitInterfaceUtilityTest,
TestGetSupportedBreakoutModesForPortChannelizedSuccess) {
const std::string port = "Ethernet0";
std::vector<std::string> expected_breakout_modes = {"2x200G", "2x100G",
"2x40G", "4x100G"};

const std::string interface_info =
R"pb({ "breakout_modes": "1x400G, 2x200G[100G,40G], 4x100G" }
)pb";
EXPECT_THAT(pins_test::GetSupportedBreakoutModesForPort(
interface_info, port, pins_test::BreakoutType::kChannelized),
expected_breakout_modes);
}

TEST_F(GNMIThinkitInterfaceUtilityTest,
TestGetSupportedBreakoutModesForPortBreakoutModesNotFoundFailure) {
const std::string port = "Ethernet0";
const std::string interface_info =
R"pb({}
)pb";
EXPECT_THAT(
pins_test::GetSupportedBreakoutModesForPort(interface_info, port),
StatusIs(absl::StatusCode::kInternal,
HasSubstr(absl::StrCat("Supported breakout modes not found for ",
port, " in platform.json"))));
}

TEST_F(GNMIThinkitInterfaceUtilityTest,
TestGetSupportedBreakoutModesForPortNumBreakoutsIntConversionFailure) {
const std::string port = "Ethernet0";
const std::string interface_info =
R"pb({ "breakout_modes": "Xx400G, 2x200G[100G,40G], 4x100G" }
)pb";
EXPECT_THAT(pins_test::GetSupportedBreakoutModesForPort(
interface_info, port, pins_test::BreakoutType::kChannelized),
StatusIs(absl::StatusCode::kInternal,
HasSubstr("Failed to convert string (X) to integer")));
}

TEST_F(GNMIThinkitInterfaceUtilityTest,
TestGetRandomPortWithSupportedBreakoutModesAnySuccess) {
auto mock_gnmi_stub_ptr = absl::make_unique<gnmi::MockgNMIStub>();
absl::flat_hash_map<std::string, pins_test::RandomPortBreakoutInfo>
expected_port_info;
pins_test::RandomPortBreakoutInfo r;
r.port_name = "Ethernet0";
r.curr_breakout_mode = "1x400G";
r.supported_breakout_mode = "2x200G";
expected_port_info["Ethernet0"] = r;
r.port_name = "Ethernet8";
r.curr_breakout_mode = "2x200G";
r.supported_breakout_mode = "1x400G";
expected_port_info["Ethernet8"] = r;
const std::string platform_json_contents =
R"pb({
"interfaces": {
"Ethernet0": {
"default_brkout_mode": "1x400G",
"breakout_modes": "1x400G, 2x200G[100G,40G]"
},
"Ethernet8": {
"default_brkout_mode": "2x200G",
"breakout_modes": "1x400G, 2x200G[100G,40G]"
},
"Ethernet12": { "breakout_modes": "1x200G[100G,40G]" }
}
}
)pb";
gnmi::GetRequest req;
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(
R"pb(prefix { origin: "openconfig" }
path { elem { name: "interfaces" } }
type: STATE)pb",
&req));
gnmi::GetResponse resp;
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(
R"pb(notification {
timestamp: 1631864194292383538
prefix { origin: "openconfig" }
update {
path { elem { name: "interfaces" } }
val {
json_ietf_val: "{\"openconfig-interfaces:interfaces\":{\"interface\":[{\"name\":\"Ethernet0\",\"state\":{\"oper-status\":\"UP\"}},{\"name\":\"Ethernet8\",\"state\":{\"oper-status\":\"UP\"}}]}}"
}
}
}
)pb",
&resp));
EXPECT_CALL(*mock_gnmi_stub_ptr, Get(_, EqualsProto(req), _))
.WillOnce(DoAll(SetArgPointee<2>(resp), Return(grpc::Status::OK)));
auto random_port_info = pins_test::GetRandomPortWithSupportedBreakoutModes(
*mock_gnmi_stub_ptr, platform_json_contents);
ASSERT_OK(random_port_info.status());
EXPECT_THAT(random_port_info.value().port_name,
expected_port_info[random_port_info.value().port_name].port_name);
EXPECT_THAT(random_port_info.value().curr_breakout_mode,
expected_port_info[random_port_info.value().port_name]
.curr_breakout_mode);
EXPECT_THAT(random_port_info.value().supported_breakout_mode,
expected_port_info[random_port_info.value().port_name]
.supported_breakout_mode);
}

TEST_F(GNMIThinkitInterfaceUtilityTest,
TestGetRandomPortWithSupportedBreakoutModesChannelizedSuccess) {
auto mock_gnmi_stub_ptr = absl::make_unique<gnmi::MockgNMIStub>();
absl::flat_hash_map<std::string, pins_test::RandomPortBreakoutInfo>
expected_port_info;
pins_test::RandomPortBreakoutInfo r;
r.port_name = "Ethernet0";
r.curr_breakout_mode = "1x400G";
r.supported_breakout_mode = "2x200G";
expected_port_info["Ethernet0"] = r;
const std::string platform_json_contents =
R"pb({
"interfaces": {
"Ethernet0": {
"default_brkout_mode": "1x400G",
"breakout_modes": "1x400G, 2x200G[100G,40G]"
},
"Ethernet8": {
"default_brkout_mode": "1x400G",
"breakout_modes": "1x400G"
},
"Ethernet16": {
"default_brkout_mode": "1x400G",
"breakout_modes": "1x400G"
}
}
}
)pb";
gnmi::GetRequest req;
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(
R"pb(prefix { origin: "openconfig" }
path { elem { name: "interfaces" } }
type: STATE)pb",
&req));
gnmi::GetResponse resp;
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(
R"pb(notification {
timestamp: 1631864194292383538
prefix { origin: "openconfig" }
update {
path { elem { name: "interfaces" } }
val {
json_ietf_val: "{\"openconfig-interfaces:interfaces\":{\"interface\":[{\"name\":\"Ethernet0\",\"state\":{\"oper-status\":\"UP\"}},{\"name\":\"Ethernet8\",\"state\":{\"oper-status\":\"UP\"}},{\"name\":\"Ethernet16\",\"state\":{\"oper-status\":\"UP\"}}]}}"
}
}
}
)pb",
&resp));
EXPECT_CALL(*mock_gnmi_stub_ptr, Get(_, EqualsProto(req), _))
.WillOnce(DoAll(SetArgPointee<2>(resp), Return(grpc::Status::OK)));
auto random_port_info = pins_test::GetRandomPortWithSupportedBreakoutModes(
*mock_gnmi_stub_ptr, platform_json_contents,
pins_test::BreakoutType::kChannelized);
ASSERT_OK(random_port_info.status());
EXPECT_THAT(random_port_info.value().port_name,
expected_port_info[random_port_info.value().port_name].port_name);
EXPECT_THAT(random_port_info.value().curr_breakout_mode,
expected_port_info[random_port_info.value().port_name]
.curr_breakout_mode);
EXPECT_THAT(random_port_info.value().supported_breakout_mode,
expected_port_info[random_port_info.value().port_name]
.supported_breakout_mode);
}

TEST_F(GNMIThinkitInterfaceUtilityTest,
TestGetRandomPortWithSupportedBreakoutModesOperStatusMapGetFailure) {
auto mock_gnmi_stub_ptr = absl::make_unique<gnmi::MockgNMIStub>();
gnmi::GetRequest req;
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(
R"pb(prefix { origin: "openconfig" }
path { elem { name: "interfaces" } }
type: STATE)pb",
&req));
EXPECT_CALL(*mock_gnmi_stub_ptr, Get(_, EqualsProto(req), _))
.WillOnce(Return(grpc::Status(grpc::StatusCode::DEADLINE_EXCEEDED, "")));
EXPECT_THAT(
pins_test::GetRandomPortWithSupportedBreakoutModes(*mock_gnmi_stub_ptr,
""),
StatusIs(absl::StatusCode::kDeadlineExceeded,
HasSubstr("Failed to get oper-status map for ports on switch")));
}

TEST_F(GNMIThinkitInterfaceUtilityTest,
TestGetRandomPortWithSupportedBreakoutModesIntConversionFailure) {
auto mock_gnmi_stub_ptr = absl::make_unique<gnmi::MockgNMIStub>();
gnmi::GetRequest req;
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(
R"pb(prefix { origin: "openconfig" }
path { elem { name: "interfaces" } }
type: STATE)pb",
&req));
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(
R"pb(prefix { origin: "openconfig" }
path { elem { name: "interfaces" } }
type: STATE)pb",
&req));
gnmi::GetResponse resp;
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(
R"pb(notification {
timestamp: 1631864194292383538
prefix { origin: "openconfig" }
update {
path { elem { name: "interfaces" } }
val {
json_ietf_val: "{\"openconfig-interfaces:interfaces\":{\"interface\":[{\"name\":\"EthernetX\",\"state\":{\"oper-status\":\"UP\"}}, {\"name\":\"Ethernet8\",\"state\":{\"oper-status\":\"UP\"}}]}}"
}
}
}
)pb",
&resp));
EXPECT_CALL(*mock_gnmi_stub_ptr, Get(_, EqualsProto(req), _))
.WillOnce(DoAll(SetArgPointee<2>(resp), Return(grpc::Status::OK)));
EXPECT_THAT(pins_test::GetRandomPortWithSupportedBreakoutModes(
*mock_gnmi_stub_ptr, ""),
StatusIs(absl::StatusCode::kInternal,
HasSubstr("Failed to convert string (X) to integer")));
}

} // namespace pins_test

0 comments on commit 9a0bf35

Please sign in to comment.