Skip to content

Commit

Permalink
test(storage): support testing with alternative endpoints (#14502)
Browse files Browse the repository at this point in the history
  • Loading branch information
coryan authored Jul 20, 2024
1 parent 20af944 commit 4c40596
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
38 changes: 26 additions & 12 deletions google/cloud/storage/testing/storage_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ namespace storage {
namespace testing {
namespace {

using ::google::cloud::internal::GetEnv;

absl::optional<std::string> 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");
}

Expand All @@ -61,12 +59,28 @@ StorageIntegrationTest::~StorageIntegrationTest() {
}
}

Options StorageIntegrationTest::MakeTestOptions(Options opts) {
auto fallback = Options{}
.set<RetryPolicyOption>(TestRetryPolicy())
.set<BackoffPolicyOption>(TestBackoffPolicy());
if (auto v = GetEnv("GOOGLE_CLOUD_CPP_STORAGE_TEST_GRPC_ENDPOINT")) {
fallback.set<EndpointOption>(*v);
}
if (auto v = GetEnv("GOOGLE_CLOUD_CPP_STORAGE_TEST_JSON_ENDPOINT")) {
fallback.set<RestEndpointOption>(*v);
}
if (auto v = GetEnv("GOOGLE_CLOUD_CPP_STORAGE_TEST_AUTHORITY")) {
fallback.set<AuthorityOption>(*v);
}
if (auto v = GetEnv("GOOGLE_CLOUD_CPP_STORAGE_TEST_TARGET_API_VERSION")) {
fallback.set<google::cloud::storage::internal::TargetApiVersionOption>(*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<RetryPolicyOption>(TestRetryPolicy())
.set<BackoffPolicyOption>(TestBackoffPolicy()));
StorageIntegrationTest::MakeIntegrationTestClient(Options opts) {
opts = MakeTestOptions(std::move(opts));
#if GOOGLE_CLOUD_CPP_STORAGE_HAVE_GRPC
if (UseGrpcForMedia() || UseGrpcForMetadata()) {
return storage_experimental::DefaultGrpcClient(std::move(opts));
Expand Down
9 changes: 8 additions & 1 deletion google/cloud/storage/testing/storage_integration_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 Options MakeTestOptions(Options opts = {});

/**
* Return a client suitable for most integration tests.
*
Expand All @@ -47,7 +54,7 @@ class StorageIntegrationTest
* configured.
*/
static google::cloud::storage::Client MakeIntegrationTestClient(
google::cloud::Options opts = {});
Options opts = {});

/**
* Return a client with retry policies suitable for CreateBucket() class.
Expand Down

0 comments on commit 4c40596

Please sign in to comment.