[GCP Observability C++] Add empty config test (#32790)

<!--

If you know who should review your pull request, please assign it to
that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the
appropriate
lang label.

-->
pull/32849/head^2
Yash Tibrewal 2 years ago committed by GitHub
parent 2b83675cc8
commit e49cfd494c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/cpp/ext/gcp/observability_config.cc
  2. 16
      test/cpp/ext/gcp/observability_config_test.cc

@ -55,7 +55,7 @@ absl::StatusOr<std::string> GetGcpObservabilityConfigContents() {
// First, try GRPC_GCP_OBSERVABILITY_CONFIG_FILE
std::string contents_str;
auto path = grpc_core::GetEnv("GRPC_GCP_OBSERVABILITY_CONFIG_FILE");
if (path.has_value()) {
if (path.has_value() && !path.value().empty()) {
grpc_slice contents;
grpc_error_handle error =
grpc_load_file(path->c_str(), /*add_null_terminator=*/true, &contents);
@ -70,7 +70,7 @@ absl::StatusOr<std::string> GetGcpObservabilityConfigContents() {
}
// Next, try GRPC_GCP_OBSERVABILITY_CONFIG env var.
auto env_config = grpc_core::GetEnv("GRPC_GCP_OBSERVABILITY_CONFIG");
if (env_config.has_value()) {
if (env_config.has_value() && !env_config.value().empty()) {
return std::move(*env_config);
}
// No observability config found.

@ -409,6 +409,22 @@ TEST_P(EnvParsingTest, BadJson) {
<< config.status().message();
}
TEST_P(EnvParsingTest, BadJsonEmptyString) {
SetConfig("");
auto config = GcpObservabilityConfig::ReadFromEnv();
if (GetParam().config_source() == EnvParsingTestType::ConfigSource::kFile) {
EXPECT_EQ(config.status().code(), absl::StatusCode::kInvalidArgument);
EXPECT_THAT(config.status().message(),
::testing::HasSubstr("JSON parsing failed"))
<< config.status().message();
} else {
EXPECT_EQ(config.status(),
absl::FailedPreconditionError(
"Environment variables GRPC_GCP_OBSERVABILITY_CONFIG_FILE or "
"GRPC_GCP_OBSERVABILITY_CONFIG not defined"));
}
}
// Make sure that GCP config errors are propagated as expected.
TEST_P(EnvParsingTest, BadGcpConfig) {
SetConfig(R"json({

Loading…
Cancel
Save