From 305a235f52db77d9fd086b47dc33a295c5595618 Mon Sep 17 00:00:00 2001 From: kishanps Date: Thu, 20 May 2021 13:51:21 -0700 Subject: [PATCH] [Thinkit] Add a test to ensure that table entries are successfully cleared. --- tests/forwarding/BUILD.bazel | 2 ++ tests/forwarding/p4_blackbox_fixture.h | 31 +++++++---------------- tests/forwarding/smoke_test.cc | 35 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 22 deletions(-) diff --git a/tests/forwarding/BUILD.bazel b/tests/forwarding/BUILD.bazel index 5023d3b8..eded229e 100644 --- a/tests/forwarding/BUILD.bazel +++ b/tests/forwarding/BUILD.bazel @@ -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", ], ) @@ -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", diff --git a/tests/forwarding/p4_blackbox_fixture.h b/tests/forwarding/p4_blackbox_fixture.h index f86f2f92..812550ac 100644 --- a/tests/forwarding/p4_blackbox_fixture.h +++ b/tests/forwarding/p4_blackbox_fixture.h @@ -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" @@ -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())); } @@ -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 sut_p4rt_session_; pdpi::IrP4Info ir_p4info_ = sai::GetIrP4Info(sai::Instantiation::kMiddleblock); + p4::config::v1::P4Info p4info_ = + sai::GetP4Info(sai::Instantiation::kMiddleblock); }; } // namespace pins diff --git a/tests/forwarding/smoke_test.cc b/tests/forwarding/smoke_test.cc index b1b84837..b19cba8b 100644 --- a/tests/forwarding/smoke_test.cc +++ b/tests/forwarding/smoke_test.cc @@ -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" @@ -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( + 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