Skip to content

Commit

Permalink
Added GenericTestbed and ControlInterface interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhalstea authored and divyagayathri-hcl committed Jul 19, 2024
1 parent f480c52 commit 95b34f1
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 1 deletion.
39 changes: 39 additions & 0 deletions thinkit/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package(
cc_library(
name = "thinkit",
deps = [
":control_interface",
":generic_testbed",
":mirror_testbed",
":switch",
Expand All @@ -38,6 +39,7 @@ cc_library(
name = "thinkit_mocks",
testonly = 1,
deps = [
":mock_control_interface",
":mock_generic_testbed",
":mock_mirror_testbed",
":mock_switch",
Expand Down Expand Up @@ -221,18 +223,33 @@ cc_library(
name = "generic_testbed",
hdrs = ["generic_testbed.h"],
deps = [
":control_interface",
":switch",
":test_environment",
"//thinkit/proto:generic_testbed_cc_proto",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
],
)

cc_library(
name = "generic_testbed_fixture",
testonly = 1,
hdrs = ["generic_testbed_fixture.h"],
deps = [
":generic_testbed",
"@com_google_absl//absl/memory",
"@com_google_googletest//:gtest",
],
)

cc_library(
name = "mock_generic_testbed",
testonly = 1,
hdrs = ["mock_generic_testbed.h"],
deps = [
":control_interface",
":generic_testbed",
":switch",
":test_environment",
Expand Down Expand Up @@ -263,6 +280,28 @@ cc_library(
],
)

cc_library(
name = "mock_control_interface",
testonly = 1,
hdrs = ["mock_control_interface.h"],
deps = [
":control_interface",
"@com_github_gnoi//diag:diag_cc_grpc_proto",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status:statusor",
"@com_google_googletest//:gtest",
],
)

cc_test(
name = "mock_control_interface_test",
srcs = ["mock_control_interface_test.cc"],
deps = [
":mock_control_interface",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "packet_generation_finalizer",
hdrs = ["packet_generation_finalizer.h"],
Expand Down
3 changes: 2 additions & 1 deletion thinkit/bazel_test_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ class BazelTestEnvironment : public TestEnvironment {

absl::Status StoreTestArtifact(absl::string_view filename,
absl::string_view contents) override;
using TestEnvironment::StoreTestArtifact; // Inherit protobuf overload.

absl::Status AppendToTestArtifact(absl::string_view filename,
absl::string_view contents) override;

using TestEnvironment::AppendToTestArtifact; // Inherit protobuf overload.

bool MaskKnownFailures() { return mask_known_failures_; };

Expand Down
18 changes: 18 additions & 0 deletions thinkit/generic_testbed.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

#include <string>

#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "thinkit/control_interface.h"
#include "thinkit/proto/generic_testbed.pb.h"
#include "thinkit/switch.h"
#include "thinkit/test_environment.h"

Expand All @@ -37,6 +40,13 @@ enum class RequestType {
kDelete,
};

// InterfaceInfo represents the mode of an interface and the name of the peer
// interface.
struct InterfaceInfo {
third_party::pins_infra::thinkit::InterfaceMode interface_mode;
std::string peer_interface_name; // Empty if not applicable.
};

// The GenericTestbed interface represents a testbed with control interface and
// Ixia interface.
class GenericTestbed {
Expand All @@ -46,9 +56,17 @@ class GenericTestbed {
// Returns the switch (aka system) under test.
virtual Switch& Sut() = 0;

// Returns the control interface responsible for packet injection and various
// management operations.
virtual ControlInterface& Interface() = 0;

// Returns the test environment in which the test is run.
virtual TestEnvironment& Environment() = 0;

// Returns the information for all SUT interfaces.
virtual absl::flat_hash_map<std::string, InterfaceInfo>
GetSutInterfaceInfo() = 0;

// Sends a REST request to the Ixia and returns the response.
virtual absl::StatusOr<HttpResponse> SendRestRequestToIxia(
RequestType type, absl::string_view url, absl::string_view payload) = 0;
Expand Down
101 changes: 101 additions & 0 deletions thinkit/generic_testbed_fixture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) 2024, Google Inc.
//
// 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
//
// http://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 GOOGLE_THINKIT_GENERIC_TESTBED_TEST_FIXTURE_H_
#define GOOGLE_THINKIT_GENERIC_TESTBED_TEST_FIXTURE_H_

#include <memory>

#include "absl/memory/memory.h"
#include "gtest/gtest.h"
#include "thinkit/generic_testbed.h"

namespace thinkit {

// The ThinKit `GenericTestbedInterface` defines an interface every test
// platform should implement. The expectations are such that the GenericTestbed
// should only be accessed after SetUp() is called and before TearDown() is
// called.
class GenericTestbedInterface {
public:
virtual ~GenericTestbedInterface() = default;

virtual void SetUp() = 0;
virtual void TearDown() = 0;

virtual GenericTestbed& GetGenericTestbed() = 0;
};

// The Thinkit `TestParams` defines test parameters to
// `GenericTestbedFixture` class.
struct TestParams {
// Ownership transferred in GenericTestbedFixture class.
GenericTestbedInterface* generic_testbed;
std::string gnmi_config;
absl::optional<std::vector<int>> port_ids;
};

// The ThinKit `GenericTestbedFixture` class acts as a base test fixture for
// platform independent PINS tests. Any platform specific SetUp or TearDown
// requirements are abstracted through the ThinKit GenericTestbedInterface which
// is passed as a parameter.
//
// New PINS tests should extend this fixture, and if needed can extend the
// SetUp() and/or TearDown() methods:
// class MyPinsTest : public thinkit::GenericTestbedFixture {
// void SetUp() override {
// GenericTestbedFixture::SetUp(); // called first.
//
// // custom setup steps ...
// }
//
// void TearDown() override {
// // custom tear down steps ...
//
// GenericTestbedFixture::TearDown(); // called last.
// }
// };
//
// Individual tests should use the new suite name:
// TEST_P(MyPinsTest, MyTestName) {}
class GenericTestbedFixture : public testing::TestWithParam<TestParams> {
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
// will ensure the platform is ready, and in a healthy state.
void SetUp() override { generic_testbed_interface_->SetUp(); }

// A derived class that needs/wants to do its own teardown can override this
// method. However, it should take care to call this base teardown last. Once
// this method is called accessing the platform can result in unexpected
// behaviors.
void TearDown() override { generic_testbed_interface_->TearDown(); }

// Accessor for the Generic testbed. This is only safe to be called after the
// SetUp has completed.
GenericTestbed& GetGenericTestbed() {
return generic_testbed_interface_->GetGenericTestbed();
}

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

private:
// Takes ownership of the GenericTestbedInterface parameter.
std::unique_ptr<GenericTestbedInterface> generic_testbed_interface_ =
absl::WrapUnique<GenericTestbedInterface>(GetParam().generic_testbed);
};

} // namespace thinkit

#endif // GOOGLE_THINKIT_GENERIC_TESTBED_TEST_FIXTURE_H_
44 changes: 44 additions & 0 deletions thinkit/mock_control_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2024, Google Inc.
//
// 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
//
// http://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 GOOGLE_THINKIT_MOCK_CONTROL_INTERFACE_H_
#define GOOGLE_THINKIT_MOCK_CONTROL_INTERFACE_H_

#include "absl/container/flat_hash_set.h"
#include "absl/status/statusor.h"
#include "diag/diag.grpc.pb.h"
#include "gmock/gmock.h"
#include "thinkit/control_interface.h"

namespace thinkit {
class MockControlInterface : public ControlInterface {
public:
MOCK_METHOD(absl::Status, SetAdminLinkState,
(absl::Span<const std::string> sut_ports, LinkState state),
(override));
MOCK_METHOD(absl::Status, Reboot, (RebootType reboot_Type), (override));
MOCK_METHOD(absl::StatusOr<gnoi::diag::StartBERTResponse>, StartBERT,
(const gnoi::diag::StartBERTRequest& request), (override));
MOCK_METHOD(absl::StatusOr<gnoi::diag::StopBERTResponse>, StopBERT,
(const gnoi::diag::StopBERTRequest& request), (override));
MOCK_METHOD(absl::StatusOr<gnoi::diag::GetBERTResultResponse>, GetBERTResult,
(const gnoi::diag::GetBERTResultRequest& request));
MOCK_METHOD(absl::StatusOr<absl::flat_hash_set<std::string>>, GetUpLinks,
(absl::Span<const std::string> sut_ports), (override));
MOCK_METHOD(absl::Status, CheckUp, (), (override));
};

} // namespace thinkit

#endif // GOOGLE_THINKIT_MOCK_CONTROL_INTERFACE_H_
25 changes: 25 additions & 0 deletions thinkit/mock_control_interface_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024, Google Inc.
//
// 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
//
// http://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 "thinkit/mock_control_interface.h"

#include "gtest/gtest.h"

namespace thinkit {
namespace {

TEST(MockControlInterface, TestBuild) { MockControlInterface mock; }

} // namespace
} // namespace thinkit
4 changes: 4 additions & 0 deletions thinkit/mock_generic_testbed.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "gmock/gmock.h"
#include "thinkit/control_interface.h"
#include "thinkit/generic_testbed.h"
#include "thinkit/switch.h"
#include "thinkit/test_environment.h"
Expand All @@ -27,7 +28,10 @@ namespace thinkit {
class MockGenericTestbed : public GenericTestbed {
public:
MOCK_METHOD(Switch&, Sut, (), (override));
MOCK_METHOD(ControlInterface&, Interface, (), (override));
MOCK_METHOD(TestEnvironment&, Environment, (), (override));
MOCK_METHOD((absl::flat_hash_map<std::string, InterfaceInfo>),
GetSutInterfaceInfo, (), (override));
MOCK_METHOD(absl::StatusOr<HttpResponse>, SendRestRequestToIxia,
(RequestType type, absl::string_view url,
absl::string_view payload),
Expand Down
10 changes: 10 additions & 0 deletions thinkit/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ package(
licenses = ["notice"],
)

proto_library(
name = "generic_testbed_proto",
srcs = ["generic_testbed.proto"],
)

cc_proto_library(
name = "generic_testbed_cc_proto",
deps = [":generic_testbed_proto"],
)

proto_library(
name = "metrics_proto",
srcs = ["metrics.proto"],
Expand Down
40 changes: 40 additions & 0 deletions thinkit/proto/generic_testbed.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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
//
// http://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.

syntax = "proto3";

package third_party.pins_infra.thinkit;

option cc_generic_services = false;

// Mode types for the SUT interfaces are connected..
enum InterfaceMode {
UNKNOWN_MODE = 0;
DISCONNECTED = 1;
LOOPBACK = 2;
CONTROL_INTERFACE = 3;
TRAFFIC_GENERATOR = 4;
}

// This message represents the mode of interface(s) and the number of
// which are required.
message InterfaceRequirement {
InterfaceMode interface_mode = 1;
int32 count = 2;
}

// This message represents interface(s) requirement for a test.
message TestRequirements {
repeated InterfaceRequirement interface_requirements = 1;
}

0 comments on commit 95b34f1

Please sign in to comment.