|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/storage/grpc_plugin.h" |
| 16 | +#include "google/cloud/storage/internal/curl_client.h" |
| 17 | +#include "google/cloud/storage/internal/grpc_client.h" |
| 18 | +#include "google/cloud/storage/internal/hybrid_client.h" |
| 19 | +#include "google/cloud/storage/internal/retry_client.h" |
| 20 | +#include "google/cloud/testing_util/scoped_environment.h" |
| 21 | +#include <gmock/gmock.h> |
| 22 | + |
| 23 | +namespace google { |
| 24 | +namespace cloud { |
| 25 | +namespace storage_experimental { |
| 26 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 27 | +namespace { |
| 28 | + |
| 29 | +using ::google::cloud::storage::internal::ClientImplDetails; |
| 30 | +using ::google::cloud::storage::internal::CurlClient; |
| 31 | +using ::google::cloud::storage::internal::GrpcClient; |
| 32 | +using ::google::cloud::storage::internal::HybridClient; |
| 33 | +using ::google::cloud::storage::internal::RetryClient; |
| 34 | +using ::google::cloud::testing_util::ScopedEnvironment; |
| 35 | +using ::testing::IsNull; |
| 36 | +using ::testing::NotNull; |
| 37 | + |
| 38 | +TEST(GrpcPluginTest, MetadataConfigCreatesGrpc) { |
| 39 | + // Explicitly disable the RestClient, which may be enabled by our CI builds. |
| 40 | + auto rest = ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_HAVE_REST_CLIENT", |
| 41 | + absl::nullopt); |
| 42 | + // Explicitly disable logging, which may be enabled by our CI builds. |
| 43 | + auto logging = |
| 44 | + ScopedEnvironment("CLOUD_STORAGE_ENABLE_TRACING", absl::nullopt); |
| 45 | + auto config = |
| 46 | + ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG", absl::nullopt); |
| 47 | + auto client = DefaultGrpcClient(Options{}.set<GrpcPluginOption>("metadata")); |
| 48 | + auto const* const retry = |
| 49 | + dynamic_cast<RetryClient*>(ClientImplDetails::GetRawClient(client).get()); |
| 50 | + ASSERT_THAT(retry, NotNull()); |
| 51 | + auto const* const grpc = |
| 52 | + dynamic_cast<GrpcClient const*>(retry->client().get()); |
| 53 | + ASSERT_THAT(grpc, NotNull()); |
| 54 | +} |
| 55 | + |
| 56 | +TEST(GrpcPluginTest, EnvironmentOverrides) { |
| 57 | + // Explicitly disable the RestClient, which may be enabled by our CI builds. |
| 58 | + auto rest = ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_HAVE_REST_CLIENT", |
| 59 | + absl::nullopt); |
| 60 | + // Explicitly disable logging, which may be enabled by our CI builds. |
| 61 | + auto logging = |
| 62 | + ScopedEnvironment("CLOUD_STORAGE_ENABLE_TRACING", absl::nullopt); |
| 63 | + auto config = |
| 64 | + ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG", "none"); |
| 65 | + auto client = DefaultGrpcClient(Options{}.set<GrpcPluginOption>("metadata")); |
| 66 | + auto const* const retry = |
| 67 | + dynamic_cast<RetryClient*>(ClientImplDetails::GetRawClient(client).get()); |
| 68 | + ASSERT_THAT(retry, NotNull()); |
| 69 | + auto const* const grpc = dynamic_cast<GrpcClient*>(retry->client().get()); |
| 70 | + ASSERT_THAT(grpc, IsNull()); |
| 71 | +} |
| 72 | + |
| 73 | +TEST(GrpcPluginTest, UnsetConfigCreatesCurl) { |
| 74 | + // Explicitly disable the RestClient, which may be enabled by our CI builds. |
| 75 | + auto rest = ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_HAVE_REST_CLIENT", |
| 76 | + absl::nullopt); |
| 77 | + // Explicitly disable logging, which may be enabled by our CI builds. |
| 78 | + auto logging = |
| 79 | + ScopedEnvironment("CLOUD_STORAGE_ENABLE_TRACING", absl::nullopt); |
| 80 | + auto config = |
| 81 | + ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG", absl::nullopt); |
| 82 | + auto client = DefaultGrpcClient(Options{}); |
| 83 | + auto const* const retry = |
| 84 | + dynamic_cast<RetryClient*>(ClientImplDetails::GetRawClient(client).get()); |
| 85 | + ASSERT_THAT(retry, NotNull()); |
| 86 | + auto const* const curl = dynamic_cast<CurlClient*>(retry->client().get()); |
| 87 | + ASSERT_THAT(curl, NotNull()); |
| 88 | +} |
| 89 | + |
| 90 | +TEST(GrpcPluginTest, NoneConfigCreatesCurl) { |
| 91 | + // Explicitly disable the RestClient, which may be enabled by our CI builds. |
| 92 | + auto rest = ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_HAVE_REST_CLIENT", |
| 93 | + absl::nullopt); |
| 94 | + // Explicitly disable logging, which may be enabled by our CI builds. |
| 95 | + auto logging = |
| 96 | + ScopedEnvironment("CLOUD_STORAGE_ENABLE_TRACING", absl::nullopt); |
| 97 | + auto config = |
| 98 | + ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG", absl::nullopt); |
| 99 | + auto client = DefaultGrpcClient(Options{}.set<GrpcPluginOption>("none")); |
| 100 | + auto const* const retry = |
| 101 | + dynamic_cast<RetryClient*>(ClientImplDetails::GetRawClient(client).get()); |
| 102 | + ASSERT_THAT(retry, NotNull()); |
| 103 | + auto const* const curl = dynamic_cast<CurlClient*>(retry->client().get()); |
| 104 | + ASSERT_THAT(curl, NotNull()); |
| 105 | +} |
| 106 | + |
| 107 | +TEST(GrpcPluginTest, MediaConfigCreatesHybrid) { |
| 108 | + // Explicitly disable the RestClient, which may be enabled by our CI builds. |
| 109 | + auto rest = ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_HAVE_REST_CLIENT", |
| 110 | + absl::nullopt); |
| 111 | + // Explicitly disable logging, which may be enabled by our CI builds. |
| 112 | + auto logging = |
| 113 | + ScopedEnvironment("CLOUD_STORAGE_ENABLE_TRACING", absl::nullopt); |
| 114 | + auto config = |
| 115 | + ScopedEnvironment("GOOGLE_CLOUD_CPP_STORAGE_GRPC_CONFIG", absl::nullopt); |
| 116 | + auto client = DefaultGrpcClient(Options{}.set<GrpcPluginOption>("media")); |
| 117 | + auto const* const retry = |
| 118 | + dynamic_cast<RetryClient*>(ClientImplDetails::GetRawClient(client).get()); |
| 119 | + ASSERT_THAT(retry, NotNull()); |
| 120 | + auto const* const hybrid = dynamic_cast<HybridClient*>(retry->client().get()); |
| 121 | + ASSERT_THAT(hybrid, NotNull()); |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace |
| 125 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 126 | +} // namespace storage_experimental |
| 127 | +} // namespace cloud |
| 128 | +} // namespace google |
0 commit comments