-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing helper function to take care of OC paths containing values in …
…[1/1] format. Implement portable Bazel test environment.
- Loading branch information
1 parent
67821df
commit 70abdd6
Showing
8 changed files
with
301 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "lib/gnmi/gnmi_helper.h" | ||
|
||
#include "absl/strings/str_split.h" | ||
#include "absl/strings/string_view.h" | ||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include "gutil/proto_matchers.h" | ||
#include "gutil/status_matchers.h" | ||
#include "p4/v1/p4runtime.grpc.pb.h" | ||
#include "proto/gnmi/gnmi.pb.h" | ||
|
||
namespace pins_test { | ||
|
||
namespace { | ||
using gutil::EqualsProto; | ||
} // namespace | ||
|
||
TEST(OCStringToPath, OCStringToPathTestCase1) { | ||
EXPECT_THAT( | ||
ConvertOCStringToPath("interfaces/interface[name=ethernet0]/config/mtu"), | ||
EqualsProto(R"pb( | ||
elem { name: "interfaces" } | ||
elem { | ||
name: "interface" | ||
key { key: "name" value: "ethernet0" } | ||
} | ||
elem { name: "config" } | ||
elem { name: "mtu" } | ||
)pb")); | ||
} | ||
|
||
TEST(OCStringToPath, OCStringToPathTestCase2) { | ||
EXPECT_THAT( | ||
ConvertOCStringToPath("components/component[name=1/1]/state/name"), | ||
EqualsProto(R"pb( | ||
elem { name: "components" } | ||
elem { | ||
name: "component" | ||
key { key: "name" value: "1/1" } | ||
} | ||
elem { name: "state" } | ||
elem { name: "name" } | ||
)pb")); | ||
} | ||
|
||
TEST(OCStringToPath, OCStringToPathTestCase3) { | ||
EXPECT_THAT( | ||
ConvertOCStringToPath( | ||
"interfaces/interface[name=ethernet0]/config/mtu/ic[name=1/1]/value"), | ||
EqualsProto(R"pb( | ||
elem { name: "interfaces" } | ||
elem { | ||
name: "interface" | ||
key { key: "name" value: "ethernet0" } | ||
} | ||
elem { name: "config" } | ||
elem { name: "mtu" } | ||
elem { | ||
name: "ic" | ||
key { key: "name" value: "1/1" } | ||
} | ||
elem { name: "value" } | ||
)pb")); | ||
} | ||
|
||
} // namespace pins_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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/bazel_test_environment.h" | ||
|
||
#include <cstdlib> | ||
#include <filesystem> | ||
#include <fstream> | ||
#include <ios> | ||
#include <ostream> | ||
#include <system_error> // NOLINT | ||
|
||
#include "absl/status/status.h" | ||
#include "absl/strings/str_cat.h" | ||
#include "absl/strings/string_view.h" | ||
#include "gtest/gtest.h" | ||
#include "gutil/status.h" | ||
#include "thinkit/test_environment.h" | ||
|
||
namespace thinkit { | ||
|
||
namespace { | ||
|
||
absl::StatusOr<std::string> ArtifactDirectory() { | ||
// Pick appropriate artifact directory using Bazel environment variables, see | ||
// https://docs.bazel.build/versions/master/test-encyclopedia.html#initial-conditions | ||
char* base_dir = std::getenv("TEST_UNDECLARED_OUTPUTS_DIR"); | ||
if (base_dir == nullptr) { | ||
base_dir = std::getenv("TEST_TMPDIR"); | ||
} | ||
if (base_dir == nullptr) { | ||
return gutil::InternalErrorBuilder() | ||
<< "Environment variables TEST_UNDECLARED_OUTPUTS_DIR and " | ||
"TEST_TMPDIR undefined; is this a Bazel test run?"; | ||
} | ||
std::string dir = base_dir; | ||
if (auto* test_info = testing::UnitTest::GetInstance()->current_test_info(); | ||
test_info != nullptr) { | ||
absl::StrAppend(&dir, "/", test_info->test_case_name(), "/", | ||
test_info->name()); | ||
} | ||
|
||
// Ensure the directory exists. | ||
std::error_code error; | ||
std::filesystem::create_directories(dir, error); | ||
if (error) { | ||
return gutil::InternalErrorBuilder() | ||
<< "failed to create test artifact directory '" << dir | ||
<< "': " << error; | ||
} | ||
return dir; | ||
} | ||
|
||
absl::Status WriteToTestArtifact(absl::string_view filename, | ||
absl::string_view contents, | ||
std::ios_base::openmode mode) { | ||
ASSIGN_OR_RETURN(std::string directory, ArtifactDirectory()); | ||
std::string filepath = absl::StrCat(directory, "/", filename); | ||
std::ofstream file; | ||
file.open(filepath, mode); | ||
if (!file.is_open()) { | ||
return gutil::InternalErrorBuilder() | ||
<< "unable to open test artifact file: '" << filepath << "'"; | ||
} | ||
file << contents; | ||
file.close(); | ||
if (file.good()) return absl::OkStatus(); | ||
return gutil::InternalErrorBuilder() | ||
<< "failed to store test artifact: '" << filepath << "'"; | ||
} | ||
|
||
} // namespace | ||
|
||
absl::Status BazelTestEnvironment::StoreTestArtifact( | ||
absl::string_view filename, absl::string_view contents) { | ||
return WriteToTestArtifact(filename, contents, std::ios_base::trunc); | ||
} | ||
|
||
absl::Status BazelTestEnvironment::AppendToTestArtifact( | ||
absl::string_view filename, absl::string_view contents) { | ||
return WriteToTestArtifact(filename, contents, std::ios_base::app); | ||
} | ||
|
||
} // namespace thinkit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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_BAZEL_TEST_ENVIRONMENT_H_ | ||
#define GOOGLE_THINKIT_BAZEL_TEST_ENVIRONMENT_H_ | ||
|
||
#include <ios> | ||
|
||
#include "absl/strings/string_view.h" | ||
#include "thinkit/test_environment.h" | ||
|
||
namespace thinkit { | ||
|
||
// Simple `thinkit::TestEnvironment` that works well with the Bazel build | ||
// system. | ||
class BazelTestEnvironment : public TestEnvironment { | ||
public: | ||
BazelTestEnvironment() = delete; | ||
BazelTestEnvironment(bool mask_known_failures) | ||
: mask_known_failures_{mask_known_failures} {} | ||
|
||
absl::Status StoreTestArtifact(absl::string_view filename, | ||
absl::string_view contents) override; | ||
|
||
absl::Status AppendToTestArtifact(absl::string_view filename, | ||
absl::string_view contents) override; | ||
|
||
|
||
bool MaskKnownFailures() { return mask_known_failures_; }; | ||
|
||
private: | ||
bool mask_known_failures_; | ||
}; | ||
|
||
} // namespace thinkit | ||
|
||
#endif // GOOGLE_THINKIT_BAZEL_TEST_ENVIRONMENT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "thinkit/bazel_test_environment.h" | ||
|
||
#include "absl/strings/string_view.h" | ||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include "gutil/status_matchers.h" | ||
|
||
namespace thinkit { | ||
namespace { | ||
|
||
constexpr absl::string_view kTestArtifact = "my_test_artifact.txt"; | ||
|
||
class BazelTestEnvironmentTest : public testing::Test { | ||
protected: | ||
std::unique_ptr<TestEnvironment> environment_ = | ||
absl::make_unique<BazelTestEnvironment>(/*mask_known_failures=*/true); | ||
}; | ||
|
||
TEST_F(BazelTestEnvironmentTest, StoreTestArtifact) { | ||
EXPECT_OK(environment_->StoreTestArtifact(kTestArtifact, "Hello, World!\n")); | ||
EXPECT_OK(environment_->StoreTestArtifact(kTestArtifact, "Hello, Test!\n")); | ||
} | ||
|
||
TEST_F(BazelTestEnvironmentTest, AppendToTestArtifact) { | ||
EXPECT_OK( | ||
environment_->AppendToTestArtifact(kTestArtifact, "Hello, World!\n")); | ||
EXPECT_OK( | ||
environment_->AppendToTestArtifact(kTestArtifact, "Hello, Test!\n")); | ||
} | ||
|
||
} // namespace | ||
} // namespace thinkit |