Skip to content

Commit

Permalink
Merge branch 'main' into branch142
Browse files Browse the repository at this point in the history
  • Loading branch information
bhagatgit authored Jul 24, 2024
2 parents 99b8b87 + 1950665 commit b8c6f9d
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 7 deletions.
12 changes: 12 additions & 0 deletions thinkit/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ cc_library(
"@com_github_gnmi//proto/gnmi:gnmi_cc_grpc_proto",
"@com_github_gnoi//cert:cert_cc_grpc_proto",
"@com_github_gnoi//diag:diag_cc_grpc_proto",
"@com_github_gnoi//factory_reset:factory_reset_cc_grpc_proto",
"@com_github_gnoi//os:os_cc_grpc_proto",
"@com_github_gnoi//system:system_cc_grpc_proto",
"@com_github_grpc_grpc//:grpc++",
"@com_github_p4lang_p4runtime//:p4runtime_cc_grpc",
Expand All @@ -66,6 +68,8 @@ cc_library(
"@com_github_gnmi//proto/gnmi:gnmi_cc_grpc_proto",
"@com_github_gnoi//cert:cert_cc_grpc_proto",
"@com_github_gnoi//diag:diag_cc_grpc_proto",
"@com_github_gnoi//factory_reset:factory_reset_cc_grpc_proto",
"@com_github_gnoi//os:os_cc_grpc_proto",
"@com_github_gnoi//system:system_cc_grpc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_grpc",
"@com_google_absl//absl/status:statusor",
Expand Down Expand Up @@ -98,7 +102,15 @@ cc_library(
hdrs = ["mirror_testbed_fixture.h"],
deps = [
":mirror_testbed",
"//gutil:status",
"//p4_pdpi:ir",
"//p4_pdpi:ir_cc_proto",
"//sai_p4/instantiations/google:sai_p4info_cc",
"@com_github_p4lang_p4runtime//:p4info_cc_proto",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
],
)
Expand Down
5 changes: 3 additions & 2 deletions thinkit/generic_testbed_fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class GenericTestbedInterface {

// The Thinkit `TestParams` defines test parameters to
// `GenericTestbedFixture` class.
struct TestParams {
struct GenericTestbedFixtureParams {
// Ownership transferred in GenericTestbedFixture class.
GenericTestbedInterface* generic_testbed;
std::string gnmi_config;
Expand Down Expand Up @@ -74,7 +74,8 @@ struct TestParams {
//
// Individual tests should use the new suite name:
// TEST_P(MyPinsTest, MyTestName) {}
class GenericTestbedFixture : public testing::TestWithParam<TestParams> {
class GenericTestbedFixture
: public testing::TestWithParam<GenericTestbedFixtureParams> {
protected:
// A derived class that needs/wants to do its own setup can override this
// method. However, it should take care to call this base setup first. That
Expand Down
50 changes: 46 additions & 4 deletions thinkit/mirror_testbed_fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@
#define GOOGLE_THINKIT_MIRROR_TESTBED_TEST_FIXTURE_H_

#include <memory>
#include <optional>

#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "gtest/gtest.h"
#include "gutil/status.h"
#include "p4/config/v1/p4info.pb.h"
#include "p4_pdpi/ir.h"
#include "p4_pdpi/ir.pb.h"
#include "sai_p4/instantiations/google/sai_p4info.h"
#include "thinkit/mirror_testbed.h"

namespace thinkit {
Expand All @@ -34,13 +43,22 @@ class MirrorTestbedInterface {
virtual void TearDown() = 0;

virtual MirrorTestbed& GetMirrorTestbed() = 0;

// TODO: Move to TestEnvironment.
virtual absl::Status SaveSwitchLogs(absl::string_view save_prefix) = 0;
};

// The Thinkit `TestParams` defines test parameters to
// The Thinkit `MirrorTestbedFixtureParams` defines test parameters to
// `MirrorTestbedFixture` class.
struct TestParams {
struct MirrorTestbedFixtureParams {
// Ownership of the MirrorTestbedInterface will be transferred to the
// MirrorTestbedFixture class.
MirrorTestbedInterface* mirror_testbed;

// To enable testing of different platforms we pass the gNMI config and P4Info
// as arguments to the MirrorTestbedFixture.
std::string gnmi_config;
p4::config::v1::P4Info p4_info;
};

// The ThinKit `MirrorTestbedFixture` class acts as a base test fixture for
Expand Down Expand Up @@ -70,7 +88,8 @@ struct TestParams {
// Individual tests should use the new suite name to take advantage of the
// custom setup/teardown:
// TEST_P(MyPinsTest, MyTestName) {}
class MirrorTestbedFixture : public testing::TestWithParam<TestParams> {
class MirrorTestbedFixture
: public testing::TestWithParam<MirrorTestbedFixtureParams> {
protected:
// A derived class that needs/wants to do its own setup can override this
// method. However, it should take care to call this base setup first. That
Expand All @@ -89,7 +108,30 @@ class MirrorTestbedFixture : public testing::TestWithParam<TestParams> {
return mirror_testbed_interface_->GetMirrorTestbed();
}

std::string GetGnmiConfig() { return GetParam().gnmi_config; }
// TODO: This should be moved to the TestEnvironment.
absl::Status SaveSwitchLogs(absl::string_view save_prefix) {
return mirror_testbed_interface_->SaveSwitchLogs(save_prefix);
}

const std::string& GetGnmiConfig() const { return GetParam().gnmi_config; }

const p4::config::v1::P4Info& GetP4Info() const { return GetParam().p4_info; }

const pdpi::IrP4Info& GetIrP4Info() const {
// WARNING: the pdpi::IrP4Info will only be created once, and therefore it
// will be created against the current P4Info in this test fixture. It is
// unlikely that the P4Info will change because we do not open up the
// P4Info to the derived test fixtures. However, we also do not
// guarantee that the P4Info cannot be changed.
static const pdpi::IrP4Info* const kIrP4Info = [] {
absl::StatusOr<pdpi::IrP4Info> ir_p4_info =
pdpi::CreateIrP4Info(GetParam().p4_info);
CHECK(ir_p4_info.ok()) // Crash OK: Tests would be hard to debug without.
<< "Failed to translate the P4Info parameter into an IrP4Info.";
return new pdpi::IrP4Info(std::move(ir_p4_info.value()));
}();
return *kIrP4Info;
}

private:
// Takes ownership of the MirrorTestbedInterface parameter.
Expand Down
3 changes: 3 additions & 0 deletions thinkit/mock_ssh_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class MockSSHClient : public SSHClient {
MOCK_METHOD(absl::Status, PutFileContents,
(absl::string_view, const RemotePath&, absl::Duration),
(override));
MOCK_METHOD(absl::Status, PutDirectory,
(absl::string_view, const RemotePath&, absl::Duration),
(override));
MOCK_METHOD(absl::Status, GetFile,
(const RemotePath&, absl::string_view, absl::Duration),
(override));
Expand Down
9 changes: 8 additions & 1 deletion thinkit/mock_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include "absl/strings/string_view.h"
#include "cert/cert.grpc.pb.h"
#include "diag/diag.grpc.pb.h"
#include "factory_reset/factory_reset.grpc.pb.h"
#include "gmock/gmock.h"
#include "os/os.grpc.pb.h"
#include "p4/v1/p4runtime.grpc.pb.h"
#include "proto/gnmi/gnmi.grpc.pb.h"
#include "system/system.grpc.pb.h"
Expand All @@ -37,15 +39,20 @@ class MockSwitch : public Switch {
CreateP4RuntimeStub, (), (override));
MOCK_METHOD(absl::StatusOr<std::unique_ptr<gnmi::gNMI::Stub>>, CreateGnmiStub,
(), (override));
MOCK_METHOD(
absl::StatusOr<
std::unique_ptr<gnoi::factory_reset::FactoryReset::Stub>>,
CreateGnoiFactoryResetStub, (), (override));
MOCK_METHOD(absl::StatusOr<std::unique_ptr<gnoi::system::System::Stub>>,

CreateGnoiSystemStub, (), (override));
MOCK_METHOD(absl::StatusOr<std::unique_ptr<gnoi::diag::Diag::Stub>>,
CreateGnoiDiagStub, (), (override));
MOCK_METHOD(
absl::StatusOr<
std::unique_ptr<gnoi::certificate::CertificateManagement::Stub>>,
CreateGnoiCertificateStub, (), (override));
MOCK_METHOD(absl::StatusOr<std::unique_ptr<gnoi::os::OS::Stub>>,
CreateGnoiOsStub, (), (override));
};

} // namespace thinkit
Expand Down
5 changes: 5 additions & 0 deletions thinkit/ssh_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class SSHClient {
const RemotePath& destination,
absl::Duration timeout) = 0;

// Copies a local directory's files to a remote diretory.
virtual absl::Status PutDirectory(absl::string_view source,
const RemotePath& destination,
absl::Duration timeout) = 0;

// Copies a remote file's contents to a local file, creating a new
// file if needed and replacing any existing contents that was there.
virtual absl::Status GetFile(const RemotePath& source,
Expand Down
11 changes: 11 additions & 0 deletions thinkit/switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "absl/strings/string_view.h"
#include "cert/cert.grpc.pb.h"
#include "diag/diag.grpc.pb.h"
#include "factory_reset/factory_reset.grpc.pb.h"
#include "os/os.grpc.pb.h"
#include "p4/v1/p4runtime.grpc.pb.h"
#include "proto/gnmi/gnmi.grpc.pb.h"
#include "system/system.grpc.pb.h"
Expand Down Expand Up @@ -49,6 +51,11 @@ class Switch {
virtual absl::StatusOr<std::unique_ptr<gnmi::gNMI::Stub>>
CreateGnmiStub() = 0;

// Creates and returns a stub to the gNOI Factory Reset service.
virtual absl::StatusOr<
std::unique_ptr<gnoi::factory_reset::FactoryReset::Stub>>
CreateGnoiFactoryResetStub() = 0;

// Creates and returns a stub to the gNOI System service.
virtual absl::StatusOr<std::unique_ptr<gnoi::system::System::Stub>>
CreateGnoiSystemStub() = 0;
Expand All @@ -61,6 +68,10 @@ class Switch {
virtual absl::StatusOr<
std::unique_ptr<gnoi::certificate::CertificateManagement::Stub>>
CreateGnoiCertificateStub() = 0;

// Creates and returns a stub to the gNOI OS service.
virtual absl::StatusOr<std::unique_ptr<gnoi::os::OS::Stub>>
CreateGnoiOsStub() = 0;
};

} // namespace thinkit
Expand Down

0 comments on commit b8c6f9d

Please sign in to comment.