From 546298dc6ddae6ed6df01ce24452bd25613f64ca Mon Sep 17 00:00:00 2001 From: divyagayathri-hcl <159437886+divyagayathri-hcl@users.noreply.github.com> Date: Wed, 17 Jul 2024 06:24:14 -0700 Subject: [PATCH] [Thinkit] Added SSH client (commands, files) to ThinKit. (#340) * [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 Co-authored-by: smolkaj Co-authored-by: rhalstea Co-authored-by: bhagatgit <115375369+bhagatgit@users.noreply.github.com> --- tests/BUILD.bazel | 3 +- tests/thinkit_sanity_tests.cc | 27 +++++--- tests/thinkit_sanity_tests.h | 11 ++-- thinkit/BUILD.bazel | 90 ++++++++++++++++++++++++++- thinkit/mock_mirror_testbed.h | 34 ++++++++++ thinkit/mock_mirror_testbed_test.cc | 25 ++++++++ thinkit/mock_ssh_client.h | 49 +++++++++++++++ thinkit/mock_ssh_client_test.cc | 25 ++++++++ thinkit/mock_switch.h | 41 ++++++++++++ thinkit/mock_switch_test.cc | 25 ++++++++ thinkit/mock_test_environment.h | 37 +++++++++++ thinkit/mock_test_environment_test.cc | 25 ++++++++ 12 files changed, 377 insertions(+), 15 deletions(-) create mode 100644 thinkit/mock_mirror_testbed.h create mode 100644 thinkit/mock_mirror_testbed_test.cc create mode 100644 thinkit/mock_ssh_client.h create mode 100644 thinkit/mock_ssh_client_test.cc create mode 100644 thinkit/mock_switch.h create mode 100644 thinkit/mock_switch_test.cc create mode 100644 thinkit/mock_test_environment.h create mode 100644 thinkit/mock_test_environment_test.cc diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 9d240c9a..7604ae21 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -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", @@ -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", ], ) diff --git a/tests/thinkit_sanity_tests.cc b/tests/thinkit_sanity_tests.cc index 86a9ad56..227eb55a 100644 --- a/tests/thinkit_sanity_tests.cc +++ b/tests/thinkit_sanity_tests.cc @@ -16,32 +16,41 @@ #include -#include +#include #include #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 diff --git a/tests/thinkit_sanity_tests.h b/tests/thinkit_sanity_tests.h index 4f8848b5..f31b3f77 100644 --- a/tests/thinkit_sanity_tests.h +++ b/tests/thinkit_sanity_tests.h @@ -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 diff --git a/thinkit/BUILD.bazel b/thinkit/BUILD.bazel index d78d0be2..9a74cd9b 100644 --- a/thinkit/BUILD.bazel +++ b/thinkit/BUILD.bazel @@ -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. @@ -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"], @@ -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"], @@ -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"], @@ -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", + ], +) diff --git a/thinkit/mock_mirror_testbed.h b/thinkit/mock_mirror_testbed.h new file mode 100644 index 00000000..799ff21e --- /dev/null +++ b/thinkit/mock_mirror_testbed.h @@ -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_ diff --git a/thinkit/mock_mirror_testbed_test.cc b/thinkit/mock_mirror_testbed_test.cc new file mode 100644 index 00000000..4505313c --- /dev/null +++ b/thinkit/mock_mirror_testbed_test.cc @@ -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 diff --git a/thinkit/mock_ssh_client.h b/thinkit/mock_ssh_client.h new file mode 100644 index 00000000..d0ee7b28 --- /dev/null +++ b/thinkit/mock_ssh_client.h @@ -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 + +#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, 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, GetFileContents, + (const RemoteFile&, absl::Duration), (override)); +}; + +} // namespace thinkit + +#endif // THINKIT_MOCK_SSH_CLIENT_H_ diff --git a/thinkit/mock_ssh_client_test.cc b/thinkit/mock_ssh_client_test.cc new file mode 100644 index 00000000..2e568b83 --- /dev/null +++ b/thinkit/mock_ssh_client_test.cc @@ -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 diff --git a/thinkit/mock_switch.h b/thinkit/mock_switch.h new file mode 100644 index 00000000..6bc4a868 --- /dev/null +++ b/thinkit/mock_switch.h @@ -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 + +#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>, + CreateP4RuntimeStub, (), (override)); + MOCK_METHOD(absl::StatusOr>, CreateGnmiStub, + (), (override)); +}; + +} // namespace thinkit + +#endif // THINKIT_MOCK_SWITCH_H_ diff --git a/thinkit/mock_switch_test.cc b/thinkit/mock_switch_test.cc new file mode 100644 index 00000000..83189d07 --- /dev/null +++ b/thinkit/mock_switch_test.cc @@ -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_switch.h" + +#include "gtest/gtest.h" + +namespace thinkit { +namespace { + +TEST(MockSwitch, TestBuild) { MockSwitch mock; } + +} // namespace +} // namespace thinkit diff --git a/thinkit/mock_test_environment.h b/thinkit/mock_test_environment.h new file mode 100644 index 00000000..f88f9de2 --- /dev/null +++ b/thinkit/mock_test_environment.h @@ -0,0 +1,37 @@ +// 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_TEST_ENVIRONMENT_H_ +#define THINKIT_MOCK_TEST_ENVIRONMENT_H_ + +#include "absl/status/status.h" +#include "absl/strings/string_view.h" +#include "gmock/gmock.h" +#include "thinkit/test_environment.h" + +namespace thinkit { + +class MockTestEnvironment : public TestEnvironment { + public: + MOCK_METHOD(absl::Status, StoreTestArtifact, + (absl::string_view filename, absl::string_view contents), + (override)); + MOCK_METHOD(absl::Status, AppendToTestArtifact, + (absl::string_view filename, absl::string_view contents), + (override)); +}; + +} // namespace thinkit + +#endif // THINKIT_MOCK_TEST_ENVIRONMENT_H_ diff --git a/thinkit/mock_test_environment_test.cc b/thinkit/mock_test_environment_test.cc new file mode 100644 index 00000000..8c05da1b --- /dev/null +++ b/thinkit/mock_test_environment_test.cc @@ -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_test_environment.h" + +#include "gtest/gtest.h" + +namespace thinkit { +namespace { + +TEST(MockTestEnvironment, TestBuild) { MockTestEnvironment mock; } + +} // namespace +} // namespace thinkit