|
| 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/bigtable/internal/bigtable_stub_factory.h" |
| 16 | +#include "google/cloud/bigtable/options.h" |
| 17 | +#include "google/cloud/bigtable/testing/mock_bigtable_stub.h" |
| 18 | +#include "google/cloud/common_options.h" |
| 19 | +#include "google/cloud/credentials.h" |
| 20 | +#include "google/cloud/grpc_options.h" |
| 21 | +#include "google/cloud/internal/api_client_header.h" |
| 22 | +#include "google/cloud/testing_util/scoped_log.h" |
| 23 | +#include "google/cloud/testing_util/status_matchers.h" |
| 24 | +#include "google/cloud/testing_util/validate_metadata.h" |
| 25 | +#include "absl/memory/memory.h" |
| 26 | +#include <gmock/gmock.h> |
| 27 | +#include <chrono> |
| 28 | + |
| 29 | +namespace google { |
| 30 | +namespace cloud { |
| 31 | +namespace bigtable_internal { |
| 32 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 33 | +namespace { |
| 34 | + |
| 35 | +using ::google::cloud::bigtable::testing::MockBigtableStub; |
| 36 | +using ::google::cloud::bigtable::testing::MockReadRowsStream; |
| 37 | +using ::google::cloud::testing_util::ScopedLog; |
| 38 | +using ::google::cloud::testing_util::StatusIs; |
| 39 | +using ::google::cloud::testing_util::ValidateMetadataFixture; |
| 40 | +using ::testing::Contains; |
| 41 | +using ::testing::HasSubstr; |
| 42 | +using ::testing::NotNull; |
| 43 | +using ::testing::Return; |
| 44 | + |
| 45 | +// The point of these tests is to verify that the `CreateBigtableStub` factory |
| 46 | +// function injects the right decorators. We do this by observing the |
| 47 | +// side effects of these decorators. All the tests have nearly identical |
| 48 | +// structure. They create a fully decorated stub, configured to round-robin |
| 49 | +// over kTestChannel mocks. The first mock expects a call, the remaining mocks |
| 50 | +// expect no calls. Some of these side effects can only be verified as part of |
| 51 | +// the first mock. |
| 52 | + |
| 53 | +class BigtableStubFactory : public ::testing::Test { |
| 54 | + protected: |
| 55 | + Status IsContextMDValid(grpc::ClientContext& context, |
| 56 | + std::string const& method) { |
| 57 | + return validate_metadata_fixture_.IsContextMDValid( |
| 58 | + context, method, google::cloud::internal::ApiClientHeader("generator")); |
| 59 | + } |
| 60 | + |
| 61 | + private: |
| 62 | + ValidateMetadataFixture validate_metadata_fixture_; |
| 63 | +}; |
| 64 | + |
| 65 | +auto constexpr kTestChannels = 3; |
| 66 | + |
| 67 | +using MockFactory = ::testing::MockFunction<std::shared_ptr<BigtableStub>( |
| 68 | + std::shared_ptr<grpc::Channel>)>; |
| 69 | +static_assert(std::is_same<decltype(MockFactory{}.AsStdFunction()), |
| 70 | + BaseBigtableStubFactory>::value, |
| 71 | + "Mismatched mock factory type"); |
| 72 | + |
| 73 | +std::shared_ptr<BigtableStub> CreateTestStub( |
| 74 | + CompletionQueue cq, BaseBigtableStubFactory const& factory) { |
| 75 | + auto credentials = google::cloud::MakeAccessTokenCredentials( |
| 76 | + "test-only-invalid", |
| 77 | + std::chrono::system_clock::now() + std::chrono::minutes(5)); |
| 78 | + return CreateDecoratedStubs(std::move(cq), |
| 79 | + google::cloud::Options{} |
| 80 | + .set<GrpcNumChannelsOption>(kTestChannels) |
| 81 | + .set<TracingComponentsOption>({"rpc"}) |
| 82 | + .set<UnifiedCredentialsOption>(credentials), |
| 83 | + factory); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_F(BigtableStubFactory, ReadRows) { |
| 87 | + ::testing::InSequence sequence; |
| 88 | + MockFactory factory; |
| 89 | + EXPECT_CALL(factory, Call) |
| 90 | + .WillOnce([this](std::shared_ptr<grpc::Channel> const&) { |
| 91 | + auto mock = std::make_shared<MockBigtableStub>(); |
| 92 | + EXPECT_CALL(*mock, ReadRows) |
| 93 | + .WillOnce([this](std::unique_ptr<grpc::ClientContext> context, |
| 94 | + google::bigtable::v2::ReadRowsRequest const&) { |
| 95 | + // Verify the Auth decorator is present |
| 96 | + EXPECT_THAT(context->credentials(), NotNull()); |
| 97 | + // Verify the Metadata decorator is present |
| 98 | + EXPECT_STATUS_OK(IsContextMDValid( |
| 99 | + *context, "google.bigtable.v2.Bigtable.ReadRows")); |
| 100 | + auto stream = absl::make_unique<MockReadRowsStream>(); |
| 101 | + EXPECT_CALL(*stream, Read) |
| 102 | + .WillOnce( |
| 103 | + Return(Status(StatusCode::kUnavailable, "nothing here"))); |
| 104 | + return stream; |
| 105 | + }); |
| 106 | + return mock; |
| 107 | + }); |
| 108 | + EXPECT_CALL(factory, Call) |
| 109 | + .Times(kTestChannels - 1) |
| 110 | + .WillRepeatedly([](std::shared_ptr<grpc::Channel> const&) { |
| 111 | + return std::make_shared<MockBigtableStub>(); |
| 112 | + }); |
| 113 | + |
| 114 | + ScopedLog log; |
| 115 | + CompletionQueue cq; |
| 116 | + google::bigtable::v2::ReadRowsRequest req; |
| 117 | + req.set_table_name( |
| 118 | + "projects/the-project/instances/the-instance/tables/the-table"); |
| 119 | + auto stub = CreateTestStub(cq, factory.AsStdFunction()); |
| 120 | + auto stream = stub->ReadRows(absl::make_unique<grpc::ClientContext>(), req); |
| 121 | + auto read = stream->Read(); |
| 122 | + ASSERT_TRUE(absl::holds_alternative<Status>(read)); |
| 123 | + EXPECT_THAT(absl::get<Status>(read), StatusIs(StatusCode::kUnavailable)); |
| 124 | + EXPECT_THAT(log.ExtractLines(), Contains(HasSubstr("ReadRows"))); |
| 125 | +} |
| 126 | + |
| 127 | +TEST_F(BigtableStubFactory, MutateRow) { |
| 128 | + ::testing::InSequence sequence; |
| 129 | + MockFactory factory; |
| 130 | + EXPECT_CALL(factory, Call) |
| 131 | + .WillOnce([this](std::shared_ptr<grpc::Channel> const&) { |
| 132 | + auto mock = std::make_shared<MockBigtableStub>(); |
| 133 | + EXPECT_CALL(*mock, MutateRow) |
| 134 | + .WillOnce([this](grpc::ClientContext& context, |
| 135 | + google::bigtable::v2::MutateRowRequest const&) { |
| 136 | + // Verify the Auth decorator is present |
| 137 | + EXPECT_THAT(context.credentials(), NotNull()); |
| 138 | + // Verify the Metadata decorator is present |
| 139 | + EXPECT_STATUS_OK(IsContextMDValid( |
| 140 | + context, "google.bigtable.v2.Bigtable.MutateRow")); |
| 141 | + return StatusOr<google::bigtable::v2::MutateRowResponse>( |
| 142 | + Status(StatusCode::kUnavailable, "nothing here")); |
| 143 | + }); |
| 144 | + return mock; |
| 145 | + }); |
| 146 | + EXPECT_CALL(factory, Call) |
| 147 | + .Times(kTestChannels - 1) |
| 148 | + .WillRepeatedly([](std::shared_ptr<grpc::Channel> const&) { |
| 149 | + return std::make_shared<MockBigtableStub>(); |
| 150 | + }); |
| 151 | + |
| 152 | + ScopedLog log; |
| 153 | + CompletionQueue cq; |
| 154 | + grpc::ClientContext context; |
| 155 | + google::bigtable::v2::MutateRowRequest req; |
| 156 | + req.set_table_name( |
| 157 | + "projects/the-project/instances/the-instance/tables/the-table"); |
| 158 | + auto stub = CreateTestStub(cq, factory.AsStdFunction()); |
| 159 | + auto response = stub->MutateRow(context, req); |
| 160 | + EXPECT_THAT(response, StatusIs(StatusCode::kUnavailable)); |
| 161 | + EXPECT_THAT(log.ExtractLines(), Contains(HasSubstr("MutateRow"))); |
| 162 | +} |
| 163 | + |
| 164 | +TEST_F(BigtableStubFactory, AsyncMutateRow) { |
| 165 | + ::testing::InSequence sequence; |
| 166 | + MockFactory factory; |
| 167 | + EXPECT_CALL(factory, Call) |
| 168 | + .WillOnce([this](std::shared_ptr<grpc::Channel> const&) { |
| 169 | + auto mock = std::make_shared<MockBigtableStub>(); |
| 170 | + EXPECT_CALL(*mock, AsyncMutateRow) |
| 171 | + .WillOnce([this](CompletionQueue&, |
| 172 | + std::unique_ptr<grpc::ClientContext> context, |
| 173 | + google::bigtable::v2::MutateRowRequest const&) { |
| 174 | + // Verify the Auth decorator is present |
| 175 | + EXPECT_THAT(context->credentials(), NotNull()); |
| 176 | + // Verify the Metadata decorator is present |
| 177 | + EXPECT_STATUS_OK(IsContextMDValid( |
| 178 | + *context, "google.bigtable.v2.Bigtable.MutateRow")); |
| 179 | + return make_ready_future< |
| 180 | + StatusOr<google::bigtable::v2::MutateRowResponse>>( |
| 181 | + Status(StatusCode::kUnavailable, "nothing here")); |
| 182 | + }); |
| 183 | + return mock; |
| 184 | + }); |
| 185 | + EXPECT_CALL(factory, Call) |
| 186 | + .Times(kTestChannels - 1) |
| 187 | + .WillRepeatedly([](std::shared_ptr<grpc::Channel> const&) { |
| 188 | + return std::make_shared<MockBigtableStub>(); |
| 189 | + }); |
| 190 | + |
| 191 | + ScopedLog log; |
| 192 | + CompletionQueue cq; |
| 193 | + google::bigtable::v2::MutateRowRequest req; |
| 194 | + req.set_table_name( |
| 195 | + "projects/the-project/instances/the-instance/tables/the-table"); |
| 196 | + auto stub = CreateTestStub(cq, factory.AsStdFunction()); |
| 197 | + auto response = |
| 198 | + stub->AsyncMutateRow(cq, absl::make_unique<grpc::ClientContext>(), req); |
| 199 | + EXPECT_THAT(response.get(), StatusIs(StatusCode::kUnavailable)); |
| 200 | + EXPECT_THAT(log.ExtractLines(), Contains(HasSubstr("AsyncMutateRow"))); |
| 201 | +} |
| 202 | + |
| 203 | +} // namespace |
| 204 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 205 | +} // namespace bigtable_internal |
| 206 | +} // namespace cloud |
| 207 | +} // namespace google |
0 commit comments