Skip to content

Commit

Permalink
[Thinkit] Add a test to ensure that table entries are successfully cl…
Browse files Browse the repository at this point in the history
…eared.
  • Loading branch information
kishanps authored and divyagayathri-hcl committed Nov 27, 2024
1 parent 421ecac commit 305a235
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
2 changes: 2 additions & 0 deletions tests/forwarding/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ cc_library(
"//sai_p4/instantiations/google:sai_pd_cc_proto",
"//thinkit:mirror_testbed",
"//thinkit:mirror_testbed_fixture",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_google_googletest//:gtest",
],
)
Expand All @@ -113,6 +114,7 @@ cc_library(
"//sai_p4/instantiations/google:sai_p4info_cc",
"//sai_p4/instantiations/google:sai_pd_cc_proto",
"//thinkit:mirror_testbed_fixture",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_proto",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
Expand Down
31 changes: 9 additions & 22 deletions tests/forwarding/p4_blackbox_fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "gtest/gtest.h"
#include "gutil/status_matchers.h"
#include "lib/gnmi/gnmi_helper.h"
#include "p4/config/v1/p4info.pb.h"
#include "p4_pdpi/p4_runtime_session.h"
#include "p4_pdpi/pd.h"
#include "sai_p4/instantiations/google/sai_p4info.h"
Expand Down Expand Up @@ -50,25 +51,14 @@ class P4BlackboxFixture : public thinkit::MirrorTestbedFixture {
ASSERT_OK(pins_test::PushGnmiConfig(GetMirrorTestbed().ControlSwitch(),
sut_gnmi_config));

// Initialize the connection.
ASSERT_OK_AND_ASSIGN(sut_p4rt_session_, pdpi::P4RuntimeSession::Create(
GetMirrorTestbed().Sut()));

ASSERT_OK(pdpi::SetMetadataAndSetForwardingPipelineConfig(sut_p4rt_session_.get(),
p4::v1::SetForwardingPipelineConfigRequest::RECONCILE_AND_COMMIT,
sai::GetP4Info(sai::Instantiation::kMiddleblock)));

// Clear entries here in case the previous test did not (e.g. because it
// crashed).
ASSERT_OK(pdpi::ClearTableEntries(sut_p4rt_session_.get()));
// Check that switch is in a clean state.
ASSERT_OK_AND_ASSIGN(auto read_back_entries,
pdpi::ReadPiTableEntries(sut_p4rt_session_.get()));
ASSERT_EQ(read_back_entries.size(), 0);
// Initialize the connection and clear table entries.
ASSERT_OK_AND_ASSIGN(sut_p4rt_session_,
pdpi::P4RuntimeSession::CreateWithP4InfoAndClearTables(
GetMirrorTestbed().Sut(), p4info_));
}

void TearDown() override {
if (SutP4RuntimeSession() != nullptr && clear_table_entries_on_teardown_) {
if (SutP4RuntimeSession() != nullptr) {
// Clear all table entries to leave the switch in a clean state.
EXPECT_OK(pdpi::ClearTableEntries(SutP4RuntimeSession()));
}
Expand All @@ -81,17 +71,14 @@ class P4BlackboxFixture : public thinkit::MirrorTestbedFixture {
}

const pdpi::IrP4Info& IrP4Info() const { return ir_p4info_; }

protected:
void DisableClearingTableEntriesOnTearDown() {
clear_table_entries_on_teardown_ = false;
}
const p4::config::v1::P4Info& P4Info() const { return p4info_; }

private:
bool clear_table_entries_on_teardown_ = true;
std::unique_ptr<pdpi::P4RuntimeSession> sut_p4rt_session_;
pdpi::IrP4Info ir_p4info_ =
sai::GetIrP4Info(sai::Instantiation::kMiddleblock);
p4::config::v1::P4Info p4info_ =
sai::GetP4Info(sai::Instantiation::kMiddleblock);
};

} // namespace pins
Expand Down
35 changes: 35 additions & 0 deletions tests/forwarding/smoke_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "gutil/proto_matchers.h"
#include "gutil/status_matchers.h"
#include "gutil/testing.h"
#include "p4/config/v1/p4info.pb.h"
#include "p4/v1/p4runtime.pb.h"
#include "p4_pdpi/p4_runtime_session.h"
#include "p4_pdpi/pd.h"
Expand Down Expand Up @@ -263,5 +264,39 @@ TEST_P(SmokeTestFixture, InsertAndReadTableEntries) {
<< "\nActual: " << read_response.DebugString();
}

// Ensures that both CreateWithP4InfoAndClearTables and ClearTableEntries
// properly clear the table entries of a table.
TEST_P(SmokeTestFixture, EnsureClearTables) {
// Sets up initial session.
ASSERT_OK_AND_ASSIGN(auto session,
pdpi::P4RuntimeSession::CreateWithP4InfoAndClearTables(
GetMirrorTestbed().Sut(), P4Info()));
// The table should be clear after setup.
ASSERT_OK(pdpi::CheckNoTableEntries(session.get()));
// Sets up an example table entry.
const sai::TableEntry pd_entry = gutil::ParseProtoOrDie<sai::TableEntry>(
R"pb(
router_interface_table_entry {
match { router_interface_id: "router-interface-1" }
action {
set_port_and_src_mac { port: "1" src_mac: "02:2a:10:00:00:03" }
}
}
)pb");
ASSERT_OK_AND_ASSIGN(p4::v1::TableEntry pi_entry,
pdpi::PartialPdTableEntryToPiTableEntry(IrP4Info(), pd_entry));
ASSERT_OK(pdpi::InstallPiTableEntries(session.get(), IrP4Info(), {pi_entry}));
ASSERT_OK(pdpi::ClearTableEntries(session.get()));
// The table should be clear after clearing.
ASSERT_OK(pdpi::CheckNoTableEntries(session.get()));
ASSERT_OK(pdpi::InstallPiTableEntries(session.get(), IrP4Info(), {pi_entry}));
ASSERT_OK_AND_ASSIGN(auto session2,
pdpi::P4RuntimeSession::CreateWithP4InfoAndClearTables(
GetMirrorTestbed().Sut(), P4Info()));
// The table should be clear for both sessions after setting up a new session.
ASSERT_OK(pdpi::CheckNoTableEntries(session.get()));
ASSERT_OK(pdpi::CheckNoTableEntries(session2.get()));
}

} // namespace
} // namespace pins

0 comments on commit 305a235

Please sign in to comment.