Skip to content

Commit

Permalink
Make tests less reliant on DebugString implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Yan Avlasov <yavlasov@google.com>
  • Loading branch information
yanavlasov committed Mar 28, 2024
1 parent c0448f7 commit 342fe10
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
20 changes: 10 additions & 10 deletions test/common/config/utility_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

using testing::ContainsRegex;
using testing::Eq;
using testing::HasSubstr;
using testing::Optional;
using testing::Ref;
using testing::Return;
Expand Down Expand Up @@ -156,12 +157,11 @@ TEST(UtilityTest, FactoryForGrpcApiConfigSource) {
{
envoy::config::core::v3::ApiConfigSource api_config_source;
api_config_source.set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);
EXPECT_EQ(
Utility::factoryForGrpcApiConfigSource(async_client_manager, api_config_source, scope,
false)
.status()
.message(),
"API configs must have either a gRPC service or a cluster name defined: api_type: GRPC\n");
EXPECT_THAT(Utility::factoryForGrpcApiConfigSource(async_client_manager, api_config_source,
scope, false)
.status()
.message(),
HasSubstr("API configs must have either a gRPC service or a cluster name defined"));
}

{
Expand Down Expand Up @@ -545,7 +545,7 @@ TEST(UtilityTest, AnyWrongType) {
Utility::translateOpaqueConfig(typed_config, ProtobufMessage::getStrictValidationVisitor(),
out),
EnvoyException,
R"(Unable to unpack as google.protobuf.Timestamp: \[type.googleapis.com/google.protobuf.Duration\] .*)");
R"(Unable to unpack as google.protobuf.Timestamp:.*[\n]*\[type.googleapis.com/google.protobuf.Duration\] .*)");
}

TEST(UtilityTest, TranslateAnyWrongToFactoryConfig) {
Expand All @@ -563,7 +563,7 @@ TEST(UtilityTest, TranslateAnyWrongToFactoryConfig) {
Utility::translateAnyToFactoryConfig(typed_config,
ProtobufMessage::getStrictValidationVisitor(), factory),
EnvoyException,
R"(Unable to unpack as google.protobuf.Timestamp: \[type.googleapis.com/google.protobuf.Duration\] .*)");
R"(Unable to unpack as google.protobuf.Timestamp:.*[\n]*\[type.googleapis.com/google.protobuf.Duration\] .*)");
}

TEST(UtilityTest, TranslateAnyToFactoryConfig) {
Expand Down Expand Up @@ -735,10 +735,10 @@ TEST(CheckApiConfigSourceSubscriptionBackingClusterTest, GrpcClusterTestAcrossTy
api_config_source->set_api_type(envoy::config::core::v3::ApiConfigSource::GRPC);

// GRPC cluster without GRPC services.
EXPECT_EQ(
EXPECT_THAT(
Utility::checkApiConfigSourceSubscriptionBackingCluster(primary_clusters, *api_config_source)
.message(),
"API configs must have either a gRPC service or a cluster name defined: api_type: GRPC\n");
HasSubstr("API configs must have either a gRPC service or a cluster name defined"));

// Non-existent cluster.
api_config_source->add_grpc_services()->mutable_envoy_grpc()->set_cluster_name("foo_cluster");
Expand Down
3 changes: 2 additions & 1 deletion test/common/network/resolver_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ TEST(ResolverTest, NonStandardResolver) {

TEST(ResolverTest, UninitializedAddress) {
envoy::config::core::v3::Address address;
EXPECT_EQ(resolveProtoAddress(address).status().message(), "Address must be set: ");
EXPECT_THAT(resolveProtoAddress(address).status().message(),
testing::HasSubstr("Address must be set: "));
}

TEST(ResolverTest, NoSuchResolver) {
Expand Down
15 changes: 8 additions & 7 deletions test/common/protobuf/utility_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ TEST_F(ProtobufUtilityTest, RepeatedPtrUtilDebugString) {
EXPECT_EQ("[]", RepeatedPtrUtil::debugString(repeated));
repeated.Add()->set_value(10);
EXPECT_THAT(RepeatedPtrUtil::debugString(repeated),
testing::ContainsRegex("\\[value:\\s*10\n\\]"));
testing::ContainsRegex("\\[.*[\n]*value:\\s*10\n\\]"));
repeated.Add()->set_value(20);
EXPECT_THAT(RepeatedPtrUtil::debugString(repeated),
testing::ContainsRegex("\\[value:\\s*10\n, value:\\s*20\n\\]"));
testing::ContainsRegex("\\[.*[\n]*value:\\s*10\n,.*[\n]*value:\\s*20\n\\]"));
}

// Validated exception thrown when downcastAndValidate observes a PGV failures.
Expand Down Expand Up @@ -1455,7 +1455,7 @@ TEST_F(ProtobufUtilityTest, AnyConvertWrongType) {
source_any.PackFrom(source_duration);
EXPECT_THROW_WITH_REGEX(
TestUtility::anyConvert<ProtobufWkt::Timestamp>(source_any), EnvoyException,
R"(Unable to unpack as google.protobuf.Timestamp: \[type.googleapis.com/google.protobuf.Duration\] .*)");
R"(Unable to unpack as google.protobuf.Timestamp:.*[\n]*\[type.googleapis.com/google.protobuf.Duration\] .*)");
}

// Validated exception thrown when anyConvertAndValidate observes a PGV failures.
Expand All @@ -1477,7 +1477,7 @@ TEST_F(ProtobufUtilityTest, UnpackToWrongType) {
ProtobufWkt::Timestamp dst;
EXPECT_THROW_WITH_REGEX(
MessageUtil::unpackToOrThrow(source_any, dst), EnvoyException,
R"(Unable to unpack as google.protobuf.Timestamp: \[type.googleapis.com/google.protobuf.Duration\] .*)");
R"(Unable to unpack as google.protobuf.Timestamp:.*[\n]*\[type.googleapis.com/google.protobuf.Duration\] .*)");
}

// MessageUtility::unpackToOrThrow() with API message works at same version.
Expand Down Expand Up @@ -1523,9 +1523,10 @@ TEST_F(ProtobufUtilityTest, UnpackToNoThrowWrongType) {
ProtobufWkt::Timestamp dst;
auto status = MessageUtil::unpackTo(source_any, dst);
EXPECT_TRUE(absl::IsInternal(status));
EXPECT_THAT(std::string(status.message()),
testing::ContainsRegex("Unable to unpack as google.protobuf.Timestamp: "
"\\[type.googleapis.com/google.protobuf.Duration\\] .*"));
EXPECT_THAT(
std::string(status.message()),
testing::ContainsRegex("Unable to unpack as google.protobuf.Timestamp: "
".*[\n]*\\[type.googleapis.com/google.protobuf.Duration\\] .*"));
}

// MessageUtility::loadFromJson() throws on garbage JSON.
Expand Down
4 changes: 2 additions & 2 deletions test/common/router/config_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6408,9 +6408,9 @@ TEST_F(RouteMatcherTest, WeightedClustersZeroSumOfWeights) {
- name: cluster2
)EOF";

EXPECT_THROW_WITH_MESSAGE(
EXPECT_THROW_WITH_REGEX(
TestConfigImpl(parseRouteConfigurationFromYaml(yaml), factory_context_, true), EnvoyException,
"Field 'weight' is missing in: name: \"cluster1\"\n");
"Field 'weight' is missing in:.*[\n]*name: \"cluster1\"\n");

const std::string yaml2 = R"EOF(
virtual_hosts:
Expand Down

0 comments on commit 342fe10

Please sign in to comment.