Skip to content

Commit

Permalink
[Thinkit] Added SSH client (commands, files) to ThinKit. (sonic-net#340)
Browse files Browse the repository at this point in the history
* [SAI-P4] Update PdEntryBuilder to output non-PD entries for greater utility in hardware tests.

* [gutil] Make `TestArtifactWriter` abstract.

* Initial import of thinkit files.

* Added SSH client (commands, files) to ThinKit.

---------

Co-authored-by: jonathan-dilorenzo <[email protected]>
Co-authored-by: smolkaj <[email protected]>
Co-authored-by: rhalstea <[email protected]>
Co-authored-by: bhagatgit <[email protected]>
  • Loading branch information
5 people authored Jul 17, 2024
1 parent db284fe commit 546298d
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 15 deletions.
3 changes: 2 additions & 1 deletion tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cc_library(
"//gutil:status",
"//gutil:status_matchers",
"//p4_pdpi:p4_runtime_session",
"//thinkit:mirror_testbed",
"//thinkit:ssh_client",
"//thinkit:switch",
"@com_github_gnmi//proto/gnmi:gnmi_cc_proto",
"@com_github_gnmi//proto/gnmi:gnmi_cc_grpc_proto",
Expand All @@ -40,6 +40,7 @@ cc_library(
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest",
],
)
Expand Down
27 changes: 18 additions & 9 deletions tests/thinkit_sanity_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,41 @@

#include <stdint.h>

#include <memory>
#include <string>
#include <utility>

#include "absl/status/statusor.h"
#include "absl/time/time.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "gutil/status.h"
#include "gutil/status_matchers.h"
#include "p4/v1/p4runtime.grpc.pb.h"
#include "p4_pdpi/p4_runtime_session.h"
#include "thinkit/mirror_testbed.h"
#include "thinkit/ssh_client.h"
#include "thinkit/switch.h"

namespace pins_test {
namespace {

void TestP4Sessions(thinkit::MirrorTestbed& testbed) {
using ::testing::Eq;

} // namespace

void TestSSHCommand(thinkit::SSHClient& ssh_client, thinkit::Switch& sut) {
ASSERT_OK_AND_ASSIGN(std::string output,
ssh_client.RunCommand(sut.ChassisName(), "echo foo",
absl::ZeroDuration()));
EXPECT_THAT(output, Eq("foo\n"));
}

void TestP4Session(thinkit::Switch& sut) {
// TODO: Remove kDeviceId once device ID is set through gNMI in
// P4RT app.
static constexpr uint64_t kDeviceId = 183807201;
ASSERT_OK_AND_ASSIGN(auto sut_p4runtime_stub,
testbed.Sut().CreateP4RuntimeStub());
ASSERT_OK_AND_ASSIGN(auto sut_p4runtime_stub, sut.CreateP4RuntimeStub());
EXPECT_OK(
pdpi::P4RuntimeSession::Create(std::move(sut_p4runtime_stub), kDeviceId));
ASSERT_OK_AND_ASSIGN(auto control_switch_p4runtime_stub,
testbed.ControlSwitch().CreateP4RuntimeStub());
EXPECT_OK(pdpi::P4RuntimeSession::Create(
std::move(control_switch_p4runtime_stub), kDeviceId));
}

} // namespace pins_test
11 changes: 7 additions & 4 deletions tests/thinkit_sanity_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
#ifndef GOOGLE_TESTS_THINKIT_SANITY_TESTS_H_
#define GOOGLE_TESTS_THINKIT_SANITY_TESTS_H_

#include "thinkit/mirror_testbed.h"
#include "thinkit/ssh_client.h"
#include "thinkit/switch.h"

namespace pins_test {

// Tests that P4 Sessions can be established with both the SUT and the control
// switch.
void TestP4Sessions(thinkit::MirrorTestbed& testbed);
// Tests that commands can be run on the switch through SSH.
void TestSSHCommand(thinkit::SSHClient& ssh_client, thinkit::Switch& sut);

// Tests that P4 Sessions can be established with the switch.
void TestP4Session(thinkit::Switch& sut);

} // namespace pins_test

Expand Down
90 changes: 89 additions & 1 deletion thinkit/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Google LLC
# 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.
Expand Down Expand Up @@ -45,6 +45,29 @@ cc_library(
],
)

cc_library(
name = "mock_switch",
testonly = 1,
hdrs = ["mock_switch.h"],
deps = [
":switch",
"@com_github_gnmi//proto/gnmi:gnmi_cc_grpc_proto",
"@com_github_p4lang_p4runtime//:p4runtime_cc_grpc",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
],
)

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

cc_library(
name = "mirror_testbed",
hdrs = ["mirror_testbed.h"],
Expand All @@ -54,6 +77,27 @@ cc_library(
],
)

cc_library(
name = "mock_mirror_testbed",
testonly = 1,
hdrs = ["mock_mirror_testbed.h"],
deps = [
":mirror_testbed",
":switch",
":test_environment",
"@com_google_googletest//:gtest",
],
)

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

cc_library(
name = "test_environment",
hdrs = ["test_environment.h"],
Expand All @@ -63,6 +107,27 @@ cc_library(
],
)

cc_library(
name = "mock_test_environment",
testonly = 1,
hdrs = ["mock_test_environment.h"],
deps = [
":test_environment",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
],
)

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

cc_library(
name = "ssh_client",
hdrs = ["ssh_client.h"],
Expand All @@ -73,3 +138,26 @@ cc_library(
"@com_google_absl//absl/time",
],
)

cc_library(
name = "mock_ssh_client",
testonly = 1,
hdrs = ["mock_ssh_client.h"],
deps = [
":ssh_client",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest",
],
)

cc_test(
name = "mock_ssh_client_test",
srcs = ["mock_ssh_client_test.cc"],
deps = [
":mock_ssh_client",
"@com_google_googletest//:gtest_main",
],
)
34 changes: 34 additions & 0 deletions thinkit/mock_mirror_testbed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 THINKIT_MOCK_MIRROR_TESTBED_H_
#define THINKIT_MOCK_MIRROR_TESTBED_H_

#include "gmock/gmock.h"
#include "thinkit/mirror_testbed.h"
#include "thinkit/switch.h"
#include "thinkit/test_environment.h"

namespace thinkit {

class MockMirrorTestbed : public MirrorTestbed {
public:
MOCK_METHOD(Switch&, ControlSwitch, (), (override));
MOCK_METHOD(Switch&, Sut, (), (override));
MOCK_METHOD(TestEnvironment&, Environment, (), (override));
};

} // namespace thinkit

#endif // THINKIT_MOCK_MIRROR_TESTBED_H_
25 changes: 25 additions & 0 deletions thinkit/mock_mirror_testbed_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_mirror_testbed.h"

#include "gtest/gtest.h"

namespace thinkit {
namespace {

TEST(MockMirrorTestbed, BuildTest) { MockMirrorTestbed mock; }

} // namespace
} // namespace thinkit
49 changes: 49 additions & 0 deletions thinkit/mock_ssh_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 THINKIT_MOCK_SSH_CLIENT_H_
#define THINKIT_MOCK_SSH_CLIENT_H_

#include <string>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "gmock/gmock.h"
#include "thinkit/ssh_client.h"

namespace thinkit {

class MockSSHClient : public SSHClient {
public:
MOCK_METHOD(absl::StatusOr<std::string>, RunCommand,
(absl::string_view, absl::string_view, absl::Duration),
(override));
MOCK_METHOD(absl::Status, PutFile,
(absl::string_view, const RemoteFile&, absl::Duration),
(override));
MOCK_METHOD(absl::Status, PutFileContents,
(absl::string_view, const RemoteFile&, absl::Duration),
(override));
MOCK_METHOD(absl::Status, GetFile,
(const RemoteFile&, absl::string_view, absl::Duration),
(override));
MOCK_METHOD(absl::StatusOr<std::string>, GetFileContents,
(const RemoteFile&, absl::Duration), (override));
};

} // namespace thinkit

#endif // THINKIT_MOCK_SSH_CLIENT_H_
25 changes: 25 additions & 0 deletions thinkit/mock_ssh_client_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_ssh_client.h"

#include "gtest/gtest.h"

namespace thinkit {
namespace {

TEST(MockSSHClient, BuildTest) { MockSSHClient mock; }

} // namespace
} // namespace thinkit
41 changes: 41 additions & 0 deletions thinkit/mock_switch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 THINKIT_MOCK_SWITCH_H_
#define THINKIT_MOCK_SWITCH_H_

#include <memory>

#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "gmock/gmock.h"
#include "p4/v1/p4runtime.grpc.pb.h"
#include "proto/gnmi/gnmi.grpc.pb.h"
#include "thinkit/switch.h"

namespace thinkit {

class MockSwitch : public Switch {
public:
MOCK_METHOD(absl::string_view, ChassisName, (), (override));
MOCK_METHOD(uint32_t, DeviceId, (), (override));
MOCK_METHOD(absl::StatusOr<std::unique_ptr<p4::v1::P4Runtime::Stub>>,
CreateP4RuntimeStub, (), (override));
MOCK_METHOD(absl::StatusOr<std::unique_ptr<gnmi::gNMI::Stub>>, CreateGnmiStub,
(), (override));
};

} // namespace thinkit

#endif // THINKIT_MOCK_SWITCH_H_
Loading

0 comments on commit 546298d

Please sign in to comment.