Skip to content

Commit 1f196ec

Browse files
manish-qlogiccoryan
authored andcommitted
unit tests in MetadataUpdatePolicy for parent, name and table_name x_goog_Request_params (#363)
This fixes #347
1 parent f15198c commit 1f196ec

File tree

6 files changed

+362
-7
lines changed

6 files changed

+362
-7
lines changed

bigtable/client/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,13 @@ add_library(bigtable::client ALIAS bigtable_client)
9898

9999
add_library(bigtable_client_testing
100100
testing/chrono_literals.h
101+
testing/embedded_server_test_fixture.h
102+
testing/embedded_server_test_fixture.cc
101103
testing/internal_table_test_fixture.h
102104
testing/internal_table_test_fixture.cc
103105
testing/mock_data_client.h
106+
testing/inprocess_data_client.h
107+
testing/inprocess_admin_client.h
104108
testing/mock_response_stream.h
105109
testing/table_integration_test.h
106110
testing/table_integration_test.cc

bigtable/client/metadata_update_policy_test.cc

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 Google Inc.
1+
// Copyright 2018 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -13,23 +13,70 @@
1313
// limitations under the License.
1414

1515
#include "bigtable/client/metadata_update_policy.h"
16+
#include "bigtable/client/admin_client.h"
17+
#include "bigtable/client/internal/make_unique.h"
18+
#include "bigtable/client/table.h"
19+
#include "bigtable/client/table_admin.h"
20+
#include "bigtable/client/testing/embedded_server_test_fixture.h"
1621
#include <gtest/gtest.h>
22+
#include <map>
23+
#include <thread>
1724

18-
std::string const kInstanceName = "projects/foo-project/instances/bar-instance";
19-
std::string const kTableId = "baz-table";
20-
std::string const kTableName =
21-
"projects/foo-project/instances/bar-instance/tables/baz-table";
25+
namespace btproto = google::bigtable::v2;
26+
namespace adminproto = google::bigtable::admin::v2;
27+
28+
namespace {
29+
class MetadataUpdatePolicyTest
30+
: public bigtable::testing::EmbeddedServerTestFixture {};
31+
} // anonymous namespace
32+
33+
/// @test A test for setting metadata for admin operations.
34+
TEST_F(MetadataUpdatePolicyTest, RunWithEmbeddedServer) {
35+
grpc::string expected = "parent=" + kInstanceName;
36+
auto gc = bigtable::GcRule::MaxNumVersions(42);
37+
admin_->CreateTable(kTableName, bigtable::TableConfig({{"fam", gc}}, {}));
38+
// Get metadata from embedded server
39+
auto client_metadata = admin_service_.client_metadata();
40+
auto range = client_metadata.equal_range("x-goog-request-params");
41+
ASSERT_EQ(1, std::distance(range.first, range.second));
42+
EXPECT_EQ(expected, range.first->second);
43+
}
44+
45+
/// @test A test for setting metadata when table is not known.
46+
TEST_F(MetadataUpdatePolicyTest, RunWithEmbeddedServerLazyMetadata) {
47+
grpc::string expected = "name=" + kTableName;
48+
admin_->GetTable(kTableId);
49+
// Get metadata from embedded server
50+
auto client_metadata = admin_service_.client_metadata();
51+
auto range = client_metadata.equal_range("x-goog-request-params");
52+
ASSERT_EQ(1, std::distance(range.first, range.second));
53+
EXPECT_EQ(expected, range.first->second);
54+
}
55+
56+
/// @test A test for setting metadata when table is known.
57+
TEST_F(MetadataUpdatePolicyTest, RunWithEmbeddedServerParamTableName) {
58+
grpc::string expected = "table_name=" + kTableName;
59+
auto reader = table_->ReadRows(bigtable::RowSet("row1"), 1,
60+
bigtable::Filter::PassAllFilter());
61+
// lets make the RPC call to send metadata
62+
reader.begin();
63+
// Get metadata from embedded server
64+
auto client_metadata = bigtable_service_.client_metadata();
65+
auto range = client_metadata.equal_range("x-goog-request-params");
66+
ASSERT_EQ(1, std::distance(range.first, range.second));
67+
EXPECT_EQ(expected, range.first->second);
68+
}
2269

2370
/// @test A cloning test for normal construction of metadata .
24-
TEST(MetadataUpdatePolicy, SimpleDefault) {
71+
TEST_F(MetadataUpdatePolicyTest, SimpleDefault) {
2572
auto const x_google_request_params = "parent=" + kInstanceName;
2673
bigtable::MetadataUpdatePolicy created(kInstanceName,
2774
bigtable::MetadataParamTypes::PARENT);
2875
EXPECT_EQ(x_google_request_params, created.x_google_request_params().second);
2976
}
3077

3178
/// @test A test for lazy behaviour of metadata .
32-
TEST(MetadataUpdatePolicy, SimpleLazy) {
79+
TEST_F(MetadataUpdatePolicyTest, SimpleLazy) {
3380
auto const x_google_request_params = "name=" + kTableName;
3481
bigtable::MetadataUpdatePolicy created(
3582
kInstanceName, bigtable::MetadataParamTypes::NAME, kTableId);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2018 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+
// http://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 "bigtable/client/testing/embedded_server_test_fixture.h"
16+
#include "bigtable/client/internal/throw_delegate.h"
17+
#include <thread>
18+
19+
namespace bigtable {
20+
namespace testing {
21+
22+
void EmbeddedServerTestFixture::StartServer() {
23+
int port;
24+
std::string server_address("[::]:0");
25+
builder_.AddListeningPort(server_address, grpc::InsecureServerCredentials(),
26+
&port);
27+
builder_.RegisterService(&bigtable_service_);
28+
builder_.RegisterService(&admin_service_);
29+
server_ = builder_.BuildAndStart();
30+
wait_thread_ = std::thread([this]() { server_->Wait(); });
31+
}
32+
33+
void EmbeddedServerTestFixture::SetUp() {
34+
StartServer();
35+
36+
grpc::ChannelArguments channel_arguments;
37+
static std::string const prefix = "cbt-c++/" + version_string();
38+
channel_arguments.SetUserAgentPrefix(prefix);
39+
40+
std::shared_ptr<grpc::Channel> data_channel =
41+
server_->InProcessChannel(channel_arguments);
42+
data_client_ = std::make_shared<InProcessDataClient>(
43+
std::move(kProjectId), std::move(kInstanceId), std::move(data_channel));
44+
table_ = std::make_shared<bigtable::Table>(data_client_, kTableId);
45+
46+
std::shared_ptr<grpc::Channel> admin_channel =
47+
server_->InProcessChannel(channel_arguments);
48+
admin_client_ = std::make_shared<InProcessAdminClient>(
49+
std::move(kProjectId), std::move(admin_channel));
50+
admin_ = std::make_shared<bigtable::TableAdmin>(admin_client_, kInstanceId);
51+
}
52+
53+
void EmbeddedServerTestFixture::TearDown() {
54+
server_->Shutdown();
55+
wait_thread_.join();
56+
}
57+
58+
} // namespace testing
59+
} // namespace bigtable
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright 2018 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+
// http://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+
#ifndef GOOGLE_CLOUD_CPP_BIGTABLE_CLIENT_TESTING_EMBEDDED_SERVER_TEST_FIXTURE_H_
16+
#define GOOGLE_CLOUD_CPP_BIGTABLE_CLIENT_TESTING_EMBEDDED_SERVER_TEST_FIXTURE_H_
17+
18+
#include "bigtable/client/table.h"
19+
#include "bigtable/client/table_admin.h"
20+
#include "bigtable/client/testing/inprocess_admin_client.h"
21+
#include "bigtable/client/testing/inprocess_data_client.h"
22+
#include "google/bigtable/v2/bigtable.grpc.pb.h"
23+
#include <gtest/gtest.h>
24+
25+
namespace bigtable {
26+
namespace testing {
27+
28+
using ReceivedMetadata = std::multimap<std::string, std::string>;
29+
30+
inline void GetClientMetadata(grpc::ServerContext* context,
31+
ReceivedMetadata& client_metadata) {
32+
for (auto const& kv : context->client_metadata()) {
33+
auto ele = std::make_pair(std::string(kv.first.begin(), kv.first.end()),
34+
std::string(kv.second.begin(), kv.second.end()));
35+
client_metadata.emplace(std::move(ele));
36+
}
37+
}
38+
39+
/**
40+
* Implement the portions of the `google.bigtable.v2.Bigtable` interface
41+
* necessary for the embedded server tests.
42+
*
43+
* This is not a Mock (use `google::bigtable::v2::MockBigtableStub` for that,
44+
* nor is this a Fake implementation (use the Cloud Bigtable Emulator for that),
45+
* this is an implementation of the interface that returns hardcoded values.
46+
* It is suitable for the embedded server tests, but for nothing else.
47+
*/
48+
class BigtableImpl final : public google::bigtable::v2::Bigtable::Service {
49+
public:
50+
BigtableImpl() {}
51+
grpc::Status ReadRows(
52+
grpc::ServerContext* context,
53+
google::bigtable::v2::ReadRowsRequest const* request,
54+
grpc::ServerWriter<google::bigtable::v2::ReadRowsResponse>* writer) {
55+
GetClientMetadata(context, client_metadata_);
56+
return grpc::Status::OK;
57+
}
58+
const ReceivedMetadata& client_metadata() const { return client_metadata_; }
59+
60+
private:
61+
ReceivedMetadata client_metadata_;
62+
};
63+
64+
class TableAdminImpl final
65+
: public google::bigtable::admin::v2::BigtableTableAdmin::Service {
66+
public:
67+
TableAdminImpl() {}
68+
69+
grpc::Status CreateTable(
70+
grpc::ServerContext* context,
71+
google::bigtable::admin::v2::CreateTableRequest const* request,
72+
google::bigtable::admin::v2::Table* response) override {
73+
GetClientMetadata(context, client_metadata_);
74+
return grpc::Status::OK;
75+
}
76+
grpc::Status GetTable(
77+
grpc::ServerContext* context,
78+
google::bigtable::admin::v2::GetTableRequest const* request,
79+
google::bigtable::admin::v2::Table* response) override {
80+
GetClientMetadata(context, client_metadata_);
81+
return grpc::Status::OK;
82+
}
83+
const ReceivedMetadata& client_metadata() const { return client_metadata_; }
84+
85+
private:
86+
ReceivedMetadata client_metadata_;
87+
};
88+
89+
/// Common fixture for integrating embedded server into tests.
90+
class EmbeddedServerTestFixture : public ::testing::Test {
91+
protected:
92+
void StartServer();
93+
void SetUp();
94+
void TearDown();
95+
void ResetChannel();
96+
97+
std::string const kProjectId = "foo-project";
98+
std::string const kInstanceId = "bar-instance";
99+
std::string const kTableId = "baz-table";
100+
101+
// These are hardcoded, and not computed, because we want to test the
102+
// computation.
103+
std::string const kInstanceName =
104+
"projects/foo-project/instances/bar-instance";
105+
std::string const kTableName =
106+
"projects/foo-project/instances/bar-instance/tables/baz-table";
107+
108+
std::string project_id_ = kProjectId;
109+
std::string instance_id_ = kInstanceId;
110+
std::shared_ptr<DataClient> data_client_;
111+
std::shared_ptr<AdminClient> admin_client_;
112+
std::shared_ptr<bigtable::Table> table_;
113+
std::shared_ptr<bigtable::TableAdmin> admin_;
114+
std::thread wait_thread_;
115+
BigtableImpl bigtable_service_;
116+
TableAdminImpl admin_service_;
117+
grpc::ServerBuilder builder_;
118+
std::unique_ptr<grpc::Server> server_;
119+
};
120+
121+
} // namespace testing
122+
} // namespace bigtable
123+
124+
#endif // GOOGLE_CLOUD_CPP_BIGTABLE_CLIENT_TESTING_EMBEDDED_SERVER_TEST_FIXTURE_H_
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2018 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+
// http://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+
#ifndef GOOGLE_CLOUD_CPP_BIGTABLE_CLIENT_TESTING_INPROCESS_ADMIN_CLIENT_H_
16+
#define GOOGLE_CLOUD_CPP_BIGTABLE_CLIENT_TESTING_INPROCESS_ADMIN_CLIENT_H_
17+
18+
#include "bigtable/client/admin_client.h"
19+
#include "bigtable/client/client_options.h"
20+
#include <google/bigtable/admin/v2/bigtable_table_admin.grpc.pb.h>
21+
22+
namespace btadmin = ::google::bigtable::admin::v2;
23+
24+
namespace bigtable {
25+
namespace testing {
26+
27+
/**
28+
* Connects to Cloud Bigtable's administration APIs.
29+
*
30+
* This class is mainly for testing purpose, it enable use of a single embedded
31+
* server
32+
* for multiple test cases. This adminlient uses a pre-defined channel.
33+
*/
34+
class InProcessAdminClient : public bigtable::AdminClient {
35+
public:
36+
InProcessAdminClient(std::string project,
37+
std::shared_ptr<grpc::Channel> channel)
38+
: project_(std::move(project)), channel_(std::move(channel)) {}
39+
40+
using BigtableAdminStubPtr =
41+
std::shared_ptr<btadmin::BigtableTableAdmin::StubInterface>;
42+
43+
std::string const& project() const override { return project_; }
44+
BigtableAdminStubPtr Stub() override {
45+
return btadmin::BigtableTableAdmin::NewStub(channel_);
46+
}
47+
void reset() override {}
48+
void on_completion(grpc::Status const& status) override {}
49+
50+
private:
51+
std::string project_;
52+
std::shared_ptr<grpc::Channel> channel_;
53+
};
54+
55+
} // namespace BIGTABLE_CLIENT_NS
56+
} // namespace bigtable
57+
58+
#endif // GOOGLE_CLOUD_CPP_BIGTABLE_CLIENT_TESTING_INPROCESS_ADMIN_CLIENT_H_

0 commit comments

Comments
 (0)