Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to validate StartBertRequest parameters. make smoke_test public for all platforms. adding the interfaces as part of interface name. Fix smoke test dependency. #652

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions tests/forwarding/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,54 @@ cc_library(
"@com_google_absl//absl/time",
],
)

cc_library(
name = "test_data",
testonly = True,
srcs = ["test_data.cc"],
hdrs = ["test_data.h"],
deps = [
"//gutil:testing",
"//sai_p4/instantiations/google:sai_pd_cc_proto",
"@com_google_absl//absl/strings:str_format",
],
)

cc_library(
name = "p4_blackbox_fixture",
testonly = True,
hdrs = ["p4_blackbox_fixture.h"],
deps = [
"//gutil:status_matchers",
"//p4_pdpi:p4_runtime_session",
"//p4_pdpi:pd",
"//sai_p4/instantiations/google:sai_p4info_cc",
"//sai_p4/instantiations/google:sai_pd_cc_proto",
"//thinkit:mirror_testbed",
"//thinkit:mirror_testbed_fixture",
"@com_google_googletest//:gtest",
],
)

cc_library(
name = "smoke_test",
testonly = True,
srcs = ["smoke_test.cc"],
hdrs = ["smoke_test.h"],
visibility = ["//visibility:public"],
deps = [
":p4_blackbox_fixture",
":test_data",
"//gutil:proto_matchers",
"//gutil:status_matchers",
"//gutil:testing",
"//p4_pdpi:p4_runtime_session",
"//p4_pdpi:pd",
"//sai_p4/instantiations/google:sai_p4info_cc",
"//sai_p4/instantiations/google:sai_pd_cc_proto",
"//thinkit:mirror_testbed_fixture",
"@com_google_googletest//:gtest",
],
alwayslink = True,
)

79 changes: 79 additions & 0 deletions tests/forwarding/p4_blackbox_fixture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef PINS_TESTS_FORWARDING_P4_BLACKBOX_FIXTURE_H_
#define PINS_TESTS_FORWARDING_P4_BLACKBOX_FIXTURE_H_

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "gutil/status_matchers.h"
#include "p4_pdpi/p4_runtime_session.h"
#include "p4_pdpi/pd.h"
#include "sai_p4/instantiations/google/sai_p4info.h"
#include "sai_p4/instantiations/google/sai_pd.pb.h"
#include "thinkit/mirror_testbed.h"
#include "thinkit/mirror_testbed_fixture.h"

namespace gpins {

// Fixture for P4 blackbox testing. It performs test specific setup and
// teardown: Creates an initializes a P4RT channel, to get the switch ready to
// accept programming operations. Clears the switch of all table entries before
// every test.
class P4BlackboxFixture : public thinkit::MirrorTestbedFixture {
public:
void SetUp() override {
MirrorTestbedFixture::SetUp();
// Initialize the connection.
ASSERT_OK_AND_ASSIGN(sut_p4rt_session_, pdpi::P4RuntimeSession::Create(
GetMirrorTestbed().Sut()));

//ASSERT_OK(pdpi::SetForwardingPipelineConfig(sut_p4rt_session_.get(),
// sai::GetP4Info(sai::Instantiation::kMiddleblock)));
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);
}

void TearDown() override {
if (SutP4RuntimeSession() != nullptr) {
// Clear all table entries to leave the switch in a clean state.
EXPECT_OK(pdpi::ClearTableEntries(SutP4RuntimeSession()));
}

MirrorTestbedFixture::TearDown();
}

pdpi::P4RuntimeSession* SutP4RuntimeSession() const {
return sut_p4rt_session_.get();
}

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

private:
std::unique_ptr<pdpi::P4RuntimeSession> sut_p4rt_session_;
pdpi::IrP4Info ir_p4info_ = sai::GetIrP4Info(sai::Instantiation::kMiddleblock);
};

} // namespace gpins

#endif // PINS_TESTS_FORWARDING_P4_BLACKBOX_FIXTURE_H_
107 changes: 107 additions & 0 deletions tests/forwarding/smoke_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "tests/forwarding/smoke_test.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "gutil/proto_matchers.h"
#include "gutil/status_matchers.h"
#include "gutil/testing.h"
#include "p4_pdpi/p4_runtime_session.h"
#include "p4_pdpi/pd.h"
#include "sai_p4/instantiations/google/sai_p4info.h"
#include "sai_p4/instantiations/google/sai_pd.pb.h"
#include "tests/forwarding/test_data.h"

namespace gpins {
namespace {

TEST_P(SmokeTestFixture, InsertTableEntry) {
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: "0x000" src_mac: "02:2a:10:00:00:03" }
}
}
)PB");

ASSERT_OK_AND_ASSIGN(const p4::v1::TableEntry pi_entry,
pdpi::PartialPdTableEntryToPiTableEntry(IrP4Info(), pd_entry));
ASSERT_OK(pdpi::InstallPiTableEntry(SutP4RuntimeSession(), pi_entry));
}

TEST_P(SmokeTestFixture, InsertTableEntryWithRandomCharacterId) {
sai::TableEntry pd_entry = gutil::ParseProtoOrDie<sai::TableEntry>(
R"PB(
router_interface_table_entry {
match { router_interface_id: "\x01\x33\x00\xff,\":'}(*{+-" }
action {
set_port_and_src_mac { port: "0x000" src_mac: "02:2a:10:00:00:03" }
}
}
)PB");

ASSERT_OK_AND_ASSIGN(const p4::v1::TableEntry pi_entry,
pdpi::PartialPdTableEntryToPiTableEntry(IrP4Info(), pd_entry));
ASSERT_OK(pdpi::InstallPiTableEntry(SutP4RuntimeSession(), pi_entry));
ASSERT_OK_AND_ASSIGN(auto entries,
pdpi::ReadPiTableEntries(SutP4RuntimeSession()));
ASSERT_EQ(entries.size(), 1);
ASSERT_THAT(entries[0], gutil::EqualsProto(pi_entry));
}

TEST_P(SmokeTestFixture, InsertAndReadTableEntries) {
pdpi::P4RuntimeSession* session = SutP4RuntimeSession();
const pdpi::IrP4Info& ir_p4info = IrP4Info();
std::vector<sai::TableEntry> write_pd_entries =
sai_pd::CreateUpTo255GenericTableEntries(3);

thinkit::TestEnvironment& test_environment = GetMirrorTestbed().Environment();
std::vector<p4::v1::TableEntry> write_pi_entries;
p4::v1::ReadResponse expected_read_response;
write_pi_entries.reserve(write_pd_entries.size());
for (const auto& pd_entry : write_pd_entries) {
ASSERT_OK_AND_ASSIGN(p4::v1::TableEntry pi_entry,
pdpi::PartialPdTableEntryToPiTableEntry(ir_p4info, pd_entry));

ASSERT_OK(test_environment.AppendToTestArtifact(
"pi_entries_written.pb.txt",
absl::StrCat(pi_entry.DebugString(), "\n")));
*expected_read_response.add_entities()->mutable_table_entry() = pi_entry;
write_pi_entries.push_back(std::move(pi_entry));
}

ASSERT_OK(pdpi::InstallPiTableEntries(session, ir_p4info, write_pi_entries));

p4::v1::ReadRequest read_request;
read_request.add_entities()->mutable_table_entry();
ASSERT_OK_AND_ASSIGN(p4::v1::ReadResponse read_response,
pdpi::SetMetadataAndSendPiReadRequest(session, read_request));

for (const auto& entity : read_response.entities()) {
ASSERT_OK(test_environment.AppendToTestArtifact(
"pi_entries_read_back.pb.txt",
absl::StrCat(entity.table_entry().DebugString(), "\n")));
}

// Compare the result in proto format since the fields being compared are
// nested and out of order.
EXPECT_THAT(read_response, gutil::EqualsProto(expected_read_response));
}

} // namespace
} // namespace gpins
27 changes: 27 additions & 0 deletions tests/forwarding/smoke_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef PINS_TESTS_FORWARDING_SMOKE_TEST_H_
#define PINS_TESTS_FORWARDING_SMOKE_TEST_H_

#include "tests/forwarding/p4_blackbox_fixture.h"
#include "thinkit/mirror_testbed_fixture.h"

namespace gpins {

class SmokeTestFixture : public P4BlackboxFixture {};

} // namespace gpins

#endif // PINS_TESTS_FORWARDING_SMOKE_TEST_H_
56 changes: 56 additions & 0 deletions tests/forwarding/test_data.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "tests/forwarding/test_data.h"

#include <net/ethernet.h>

#include "absl/strings/str_format.h"
#include "gutil/testing.h"
#include "sai_p4/instantiations/google/sai_pd.pb.h"

namespace sai_pd {

std::vector<sai::TableEntry> CreateUpTo255GenericTableEntries(
int num_table_entries) {
// Valid IPv4 address is 4 octets of numbers (from 0-255 in decimal) separated
// by periods.
num_table_entries = std::min(num_table_entries, 255);
std::vector<sai::TableEntry> pd_table_entries;
pd_table_entries.reserve(num_table_entries);
for (int i = 0; i < num_table_entries; ++i) {
sai::TableEntry& pd_entry = pd_table_entries.emplace_back();
pd_entry = gutil::ParseProtoOrDie<sai::TableEntry>(
R"PB(
acl_ingress_table_entry {
match {
is_ipv4 { value: "0x1" }
dst_ip { value: "10.206.196.0" mask: "255.255.255.255" }
ip_protocol { value: "0x11" mask: "0xff" }
l4_dst_port { value: "0x0043" mask: "0xffff" }
}
action { trap { qos_queue: "0x1" } }
priority: 2040
}
)PB");
pd_entry.mutable_acl_ingress_table_entry()
->mutable_match()
->mutable_dst_ip()
->set_value(absl::StrFormat("10.206.196.%d", i));
}

return pd_table_entries;
}

} // namespace sai_pd
32 changes: 32 additions & 0 deletions tests/forwarding/test_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef PINS_TESTS_FORWARDING_TEST_DATA_H_
#define PINS_TESTS_FORWARDING_TEST_DATA_H_

#include "sai_p4/instantiations/google/sai_pd.pb.h"

// TODO Move it to the sai_pd library when it is available.
namespace sai_pd {

// Returns a vector of N generic table entries. It is up to the implementation
// to pick the table, match fields, action, etc, but the function guarantees the
// entries are valid. Useful for testing when the exact table entry doesn't
// matter.
std::vector<sai::TableEntry> CreateUpTo255GenericTableEntries(
int num_table_entries);

} // namespace sai_pd

#endif // PINS_TESTS_FORWARDING_TEST_DATA_H_
2 changes: 2 additions & 0 deletions tests/gnoi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ cc_library(
],
deps = [
"//gutil:status_matchers",
"//gutil:testing",
"//thinkit:mirror_testbed_fixture",
"@com_github_gnoi//diag:diag_cc_proto",
"@com_github_google_glog//:glog",
"@com_github_grpc_grpc//:grpc++",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest",
"@com_google_protobuf//:protobuf",
],
Expand Down
Loading
Loading