Skip to content

Commit

Permalink
Define GetL2ToTunnelV4EntryTest (#50)
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Foster <justffoulkes@gmail.com>
  • Loading branch information
ffoulkes authored Feb 26, 2025
1 parent 621b9bc commit a9c4a14
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ovs-p4rt/sidecar/ovsp4rt_config_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define OVSP4RT_CONFIG_INT_H_

#include <absl/status/status.h>
#include <absl/status/statusor.h>

#include "client/ovsp4rt_client_interface.h"
#include "ovsp4rt/ovs-p4rt.h"
Expand Down Expand Up @@ -36,6 +37,18 @@ extern absl::Status ConfigSrcIpMacMapTableEntry(
ClientInterface& client, const struct ip_mac_map_info& ip_info,
const ::p4::config::v1::P4Info& p4info, bool insert_entry);

extern absl::Status ConfigVlanPopTableEntry(
ClientInterface& client, const uint16_t vlan_id,
const ::p4::config::v1::P4Info& p4info, bool insert_entry);

extern absl::Status ConfigVlanPushTableEntry(
ClientInterface& client, const uint16_t vlan_id,
const ::p4::config::v1::P4Info& p4info, bool insert_entry);

extern absl::StatusOr<::p4::v1::ReadResponse> GetL2ToTunnelV4TableEntry(
ClientInterface& client, const struct mac_learning_info& learn_info,
const ::p4::config::v1::P4Info& p4info);

#endif // ES2K_TARGET

} // namespace ovsp4rt
Expand Down
2 changes: 2 additions & 0 deletions ovs-p4rt/sidecar/ovsp4rt_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef OVSP4RT_PRIVATE_H_
#define OVSP4RT_PRIVATE_H_

#include <absl/status/status.h>

#include "logging/ovsp4rt_diag_detail.h"
#include "ovsp4rt/ovs-p4rt.h"
#include "p4/config/v1/p4info.pb.h"
Expand Down
3 changes: 3 additions & 0 deletions ovs-p4rt/sidecar/tests/es2k/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ endmacro()
#-----------------------------------------------------------------------
# Basic tests
#-----------------------------------------------------------------------
# ::ovsp4rt::BasicTest subclasses
define_es2k_config_test(config_vlan_pop_table_entry_test)
define_es2k_config_test(config_vlan_push_table_entry_test)
define_es2k_config_test(get_l2_to_tunnel_v4_entry_test)

# ::testing::Test subclasses
define_es2k_config_test(es2k_update_src_port_test)
define_es2k_config_test(es2k_update_tunnel_info_test)

Expand Down
94 changes: 94 additions & 0 deletions ovs-p4rt/sidecar/tests/es2k/get_l2_to_tunnel_v4_entry_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2025 Derek Foster
// SPDX-License-Identifier: Apache-2.0

// Unit test for GetL2ToTunnelV4TableEntry().

// Core functionality is handled by PrepareL2ToTunnelV4(),
// which is tested separately. This is a test of the non-core
// functionality.

#include <absl/status/status.h>
#include <absl/status/statusor.h>
#include <arpa/inet.h>

// clang-format off
#include <gtest/gtest.h>
#include <gmock/gmock.h>
// clang-format on

#include "basic_test.h"
#include "client/ovsp4rt_test_client_mock.h"
#include "ovsp4rt/ovs-p4rt.h"
#include "ovsp4rt_config_int.h"

using ::testing::InvokeWithoutArgs;
using ::testing::Return;

namespace ovsp4rt {

class GetL2ToTunnelV4EntryTest : public BasicTest {
public:
GetL2ToTunnelV4EntryTest() {}
virtual ~GetL2ToTunnelV4EntryTest() = default;

static void InitFdbInfo(struct mac_learning_info& fdb_info) {
constexpr uint8_t MAC_ADDR[] = {0xde, 0xad, 0xbe, 0xef, 0x00, 0xe};
memcpy(fdb_info.mac_addr, MAC_ADDR, sizeof(fdb_info.mac_addr));
}

static void InitTunnelInfo(struct mac_learning_info& fdb_info) {
constexpr char IPV4_DST_ADDR[] = "192.168.17.5";
constexpr int IPV4_PREFIX_LEN = 24;

EXPECT_EQ(inet_pton(AF_INET, IPV4_DST_ADDR,
&fdb_info.tnl_info.remote_ip.ip.v4addr.s_addr),
1)
<< "Error converting " << IPV4_DST_ADDR;
fdb_info.tnl_info.remote_ip.family = AF_INET;
fdb_info.tnl_info.remote_ip.prefix_len = IPV4_PREFIX_LEN;
}

static absl::StatusOr<::p4::v1::ReadResponse> GoodReadResponse() {
::p4::v1::ReadResponse response;
return response;
}
};

TEST_F(GetL2ToTunnelV4EntryTest, getL2ToTunnelV4EntryFailure) {
constexpr char REQUEST_FAILED[] = "sendReadRequest failed";
struct mac_learning_info learn_info = {0};
InitFdbInfo(learn_info);
InitTunnelInfo(learn_info);

::p4::config::v1::P4Info p4info;
InitP4Info(&p4info);

TestClientMock client;
EXPECT_CALL(client, sendReadRequest)
.WillOnce(Return(absl::NotFoundError(REQUEST_FAILED)));

auto response = GetL2ToTunnelV4TableEntry(client, learn_info, p4info);
auto status = response.status();

ASSERT_FALSE(status.ok());
ASSERT_TRUE(IsNotFound(status) && status.message() == REQUEST_FAILED);
}

TEST_F(GetL2ToTunnelV4EntryTest, getL2ToTunnelV4EntrySuccess) {
struct mac_learning_info learn_info = {0};
InitFdbInfo(learn_info);
InitTunnelInfo(learn_info);

::p4::config::v1::P4Info p4info;
InitP4Info(&p4info);

TestClientMock client;
EXPECT_CALL(client, sendReadRequest)
.WillOnce(InvokeWithoutArgs(GoodReadResponse));

auto response = GetL2ToTunnelV4TableEntry(client, learn_info, p4info);

ASSERT_TRUE(response.ok()) << response.status();
}

} // namespace ovsp4rt

0 comments on commit a9c4a14

Please sign in to comment.