Skip to content

Commit 0b27844

Browse files
committed
refactor to improve unit test coverage
1 parent 276caa7 commit 0b27844

3 files changed

Lines changed: 366 additions & 15 deletions

File tree

google/cloud/bigtable/internal/directpath_prober.cc

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,19 @@ IpPreference ExtractIpPreference(std::string_view peer_address) {
4949
return IpPreference::kNone;
5050
}
5151

52+
bool IsAltsNegotiated(grpc::AuthContext const& auth_ctx) {
53+
if (!auth_ctx.IsPeerAuthenticated()) return false;
54+
std::vector<grpc::string_ref> const sec_types =
55+
auth_ctx.FindPropertyValues("transport_security_type");
56+
return std::any_of(sec_types.begin(), sec_types.end(),
57+
[](grpc::string_ref const& st) { return st == "alts"; });
58+
}
59+
5260
bool IsAltsNegotiated(grpc::ClientContext const& context) {
5361
std::shared_ptr<grpc::AuthContext const> const auth_ctx =
5462
context.auth_context();
5563
if (auth_ctx == nullptr) return false;
56-
if (!auth_ctx->IsPeerAuthenticated()) return false;
57-
std::vector<grpc::string_ref> const sec_types =
58-
auth_ctx->FindPropertyValues("transport_security_type");
59-
return std::any_of(sec_types.begin(), sec_types.end(),
60-
[](grpc::string_ref const& st) { return st == "alts"; });
64+
return IsAltsNegotiated(*auth_ctx);
6165
}
6266

6367
bool IsPeerAuthenticated(grpc::ClientContext const& context) {
@@ -115,12 +119,6 @@ StatusOr<DirectPathProbeResult> DirectPathProber::Probe(
115119
GCP_ERROR_INFO());
116120
}
117121

118-
std::chrono::milliseconds timeout =
119-
options.get<bigtable::experimental::DirectPathProbeTimeoutOption>();
120-
if (timeout <= std::chrono::milliseconds::zero()) {
121-
timeout = std::chrono::milliseconds(2000);
122-
}
123-
124122
auto constexpr kDirectPathEndpoint = "google-c2p:///bigtable.googleapis.com";
125123
auto constexpr kAuthority = "bigtable.googleapis.com";
126124

@@ -144,6 +142,31 @@ StatusOr<DirectPathProbeResult> DirectPathProber::Probe(
144142
std::shared_ptr<BigtableStub> stub =
145143
CreateDecoratedStubs(auth, cq, probe_options, stub_factory);
146144

145+
return Probe(stub, instance_resource, probe_options);
146+
}
147+
148+
StatusOr<DirectPathProbeResult> DirectPathProber::Probe(
149+
std::shared_ptr<BigtableStub> stub,
150+
bigtable::InstanceResource const& instance_resource,
151+
Options const& options) {
152+
return Probe(std::move(stub), instance_resource, options, "", nullptr);
153+
}
154+
155+
StatusOr<DirectPathProbeResult> DirectPathProber::Probe(
156+
std::shared_ptr<BigtableStub> stub,
157+
bigtable::InstanceResource const& instance_resource, Options const& options,
158+
std::string const& peer_address,
159+
std::shared_ptr<grpc::AuthContext const> auth_context) {
160+
if (!stub) {
161+
return internal::InternalError("Stub cannot be null", GCP_ERROR_INFO());
162+
}
163+
164+
std::chrono::milliseconds timeout =
165+
options.get<bigtable::experimental::DirectPathProbeTimeoutOption>();
166+
if (timeout <= std::chrono::milliseconds::zero()) {
167+
timeout = std::chrono::milliseconds(2000);
168+
}
169+
147170
grpc::ClientContext client_context;
148171
client_context.set_deadline(std::chrono::system_clock::now() + timeout);
149172

@@ -154,13 +177,18 @@ StatusOr<DirectPathProbeResult> DirectPathProber::Probe(
154177
}
155178
OperationContext op_ctx;
156179
StatusOr<google::bigtable::v2::PingAndWarmResponse> response =
157-
stub->PingAndWarm(client_context, probe_options, request, op_ctx);
180+
stub->PingAndWarm(client_context, options, request, op_ctx);
158181
if (!response.ok()) return response.status();
159182

160183
DirectPathProbeResult result;
161-
result.peer_address = client_context.peer();
162-
bool const is_alts = IsAltsNegotiated(client_context);
163-
bool const is_auth = IsPeerAuthenticated(client_context);
184+
result.peer_address =
185+
peer_address.empty() ? client_context.peer() : peer_address;
186+
bool const is_alts = auth_context != nullptr
187+
? IsAltsNegotiated(*auth_context)
188+
: IsAltsNegotiated(client_context);
189+
bool const is_auth = auth_context != nullptr
190+
? auth_context->IsPeerAuthenticated()
191+
: IsPeerAuthenticated(client_context);
164192
bool const is_dp_ip = IsDirectPathIp(result.peer_address);
165193
result.success = is_alts || (is_auth && is_dp_ip);
166194
result.ip_preference = result.success

google/cloud/bigtable/internal/directpath_prober.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_DIRECTPATH_PROBER_H
1717

1818
#include "google/cloud/bigtable/instance_resource.h"
19+
#include "google/cloud/bigtable/internal/bigtable_stub.h"
1920
#include "google/cloud/bigtable/options.h"
2021
#include "google/cloud/bigtable/version.h"
2122
#include "google/cloud/completion_queue.h"
2223
#include "google/cloud/internal/unified_grpc_credentials.h"
2324
#include "google/cloud/options.h"
2425
#include "google/cloud/status_or.h"
26+
#include <grpcpp/grpcpp.h>
2527
#include <memory>
2628
#include <string>
2729

@@ -50,6 +52,19 @@ class DirectPathProber {
5052
std::shared_ptr<internal::GrpcAuthenticationStrategy> const& auth,
5153
bigtable::InstanceResource const& instance_resource,
5254
Options const& options, CompletionQueue const& cq);
55+
56+
/// For testing only.
57+
static StatusOr<DirectPathProbeResult> Probe(
58+
std::shared_ptr<BigtableStub> stub,
59+
bigtable::InstanceResource const& instance_resource,
60+
Options const& options);
61+
62+
/// For testing only.
63+
static StatusOr<DirectPathProbeResult> Probe(
64+
std::shared_ptr<BigtableStub> stub,
65+
bigtable::InstanceResource const& instance_resource,
66+
Options const& options, std::string const& peer_address,
67+
std::shared_ptr<grpc::AuthContext const> auth_context);
5368
};
5469

5570
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END

0 commit comments

Comments
 (0)