From 2e015c4427a53bc5029540a47efa75e73894957d Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Wed, 17 Jul 2024 13:40:40 +0000 Subject: [PATCH] test(storage): support testing with alternative endpoints --- .../testing/storage_integration_test.cc | 37 +++++++++++++------ .../testing/storage_integration_test.h | 7 ++++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/google/cloud/storage/testing/storage_integration_test.cc b/google/cloud/storage/testing/storage_integration_test.cc index f0fa730515999..24250916a547d 100644 --- a/google/cloud/storage/testing/storage_integration_test.cc +++ b/google/cloud/storage/testing/storage_integration_test.cc @@ -29,21 +29,19 @@ namespace storage { namespace testing { namespace { +using ::google::cloud::internal::GetEnv; + absl::optional EmulatorEndpoint() { - return google::cloud::internal::GetEnv("CLOUD_STORAGE_EMULATOR_ENDPOINT"); + return GetEnv("CLOUD_STORAGE_EMULATOR_ENDPOINT"); } bool UseGrpcForMetadata() { - auto v = - google::cloud::internal::GetEnv("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG") - .value_or(""); + auto v = GetEnv("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG").value_or(""); return absl::StrContains(v, "metadata"); } bool UseGrpcForMedia() { - auto v = - google::cloud::internal::GetEnv("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG") - .value_or(""); + auto v = GetEnv("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG").value_or(""); return absl::StrContains(v, "media"); } @@ -61,12 +59,29 @@ StorageIntegrationTest::~StorageIntegrationTest() { } } +google::cloud::Options StorageIntegrationTest::MakeTestOptions( + google::cloud::Options opts) { + auto fallback = Options{} + .set(TestRetryPolicy()) + .set(TestBackoffPolicy()); + if (auto v = GetEnv("GOOGLE_CLOUD_CPP_STORAGE_TEST_GRPC_ENDPOINT")) { + fallback.set(*v); + } + if (auto v = GetEnv("GOOGLE_CLOUD_CPP_STORAGE_TEST_JSON_ENDPOINT")) { + fallback.set(*v); + } + if (auto v = GetEnv("GOOGLE_CLOUD_CPP_STORAGE_TEST_AUTHORITY")) { + fallback.set(*v); + } + if (auto v = GetEnv("GOOGLE_CLOUD_CPP_STORAGE_TEST_TARGET_API_VERSION")) { + fallback.set(*v); + } + return google::cloud::internal::MergeOptions(std::move(opts), fallback); +} + google::cloud::storage::Client StorageIntegrationTest::MakeIntegrationTestClient(google::cloud::Options opts) { - opts = google::cloud::internal::MergeOptions( - std::move(opts), Options{} - .set(TestRetryPolicy()) - .set(TestBackoffPolicy())); + opts = MakeTestOptions(std::move(opts)); #if GOOGLE_CLOUD_CPP_STORAGE_HAVE_GRPC if (UseGrpcForMedia() || UseGrpcForMetadata()) { return storage_experimental::DefaultGrpcClient(std::move(opts)); diff --git a/google/cloud/storage/testing/storage_integration_test.h b/google/cloud/storage/testing/storage_integration_test.h index ee98ff088095e..8f401cbd8b515 100644 --- a/google/cloud/storage/testing/storage_integration_test.h +++ b/google/cloud/storage/testing/storage_integration_test.h @@ -39,6 +39,13 @@ class StorageIntegrationTest protected: ~StorageIntegrationTest() override; + /** + * Returns the recommended options to run integration tests. + * + * Most tests should use these options or call `MakeIntegrationTestClient()`. + */ + static google::cloud::Options MakeTestOptions(google::cloud::Options opts = {}); + /** * Return a client suitable for most integration tests. *