Skip to content

Commit 27da1c1

Browse files
authored
Integration test for InstanceAdmin::ListInstances. (#371)
This fixes #343.
1 parent 848349a commit 27da1c1

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

bigtable/tests/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,10 @@ target_link_libraries(mutations_integration_test
4040
gmock bigtable_client bigtable_client_testing bigtable_protos
4141
gRPC::grpc protobuf::libprotobuf)
4242
add_dependencies(tests-local mutations_integration_test)
43+
44+
# Integration tests for bigtable::InstanceAdmin.
45+
add_executable(instance_admin_integration_test instance_admin_integration_test.cc)
46+
target_link_libraries(instance_admin_integration_test PRIVATE
47+
gmock bigtable_client bigtable_client_testing bigtable_protos
48+
gRPC::grpc protobuf::libprotobuf)
49+
add_dependencies(tests-local instance_admin_integration_test)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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/instance_admin.h"
16+
#include "bigtable/client/internal/make_unique.h"
17+
#include <gmock/gmock.h>
18+
19+
namespace {
20+
class InstanceTestEnvironment : public ::testing::Environment {
21+
public:
22+
explicit InstanceTestEnvironment(std::string project) {
23+
project_id_ = std::move(project);
24+
}
25+
26+
static std::string const& project_id() { return project_id_; }
27+
28+
private:
29+
static std::string project_id_;
30+
};
31+
32+
std::string InstanceTestEnvironment::project_id_;
33+
34+
class InstanceAdminIntegrationTest : public ::testing::Test {
35+
protected:
36+
void SetUp() override {
37+
auto instance_admin_client = bigtable::CreateDefaultInstanceAdminClient(
38+
InstanceTestEnvironment::project_id(), bigtable::ClientOptions());
39+
instance_admin_ = bigtable::internal::make_unique<bigtable::InstanceAdmin>(
40+
instance_admin_client);
41+
}
42+
43+
protected:
44+
std::unique_ptr<bigtable::InstanceAdmin> instance_admin_;
45+
};
46+
47+
bool UsingCloudBigtableEmulator() {
48+
return std::getenv("BIGTABLE_EMULATOR_HOST") != nullptr;
49+
}
50+
} // namespace
51+
52+
/// @test Verify that InstanceAdmin::ListInstances works as expected.
53+
TEST_F(InstanceAdminIntegrationTest, ListInstancesTest) {
54+
// The emulator does not support instance operations.
55+
if (UsingCloudBigtableEmulator()) {
56+
return;
57+
}
58+
auto instances = instance_admin_->ListInstances();
59+
for (auto const& i : instances) {
60+
auto const npos = std::string::npos;
61+
EXPECT_NE(npos, i.name().find(instance_admin_->project_name()));
62+
}
63+
}
64+
65+
int main(int argc, char* argv[]) {
66+
::testing::InitGoogleTest(&argc, argv);
67+
68+
if (argc != 2) {
69+
std::string const cmd = argv[0];
70+
auto last_slash = std::string(cmd).find_last_of('/');
71+
// Show usage if number of arguments is invalid.
72+
std::cerr << "Usage: " << cmd.substr(last_slash + 1) << " <project_id>"
73+
<< std::endl;
74+
return 1;
75+
}
76+
77+
std::string const project_id = argv[1];
78+
(void)::testing::AddGlobalTestEnvironment(
79+
new InstanceTestEnvironment(project_id));
80+
81+
return RUN_ALL_TESTS();
82+
}

bigtable/tests/integration_tests_utils.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ function run_all_integration_tests() {
5151
instance_id=$1
5252
fi
5353

54+
echo
55+
echo "Running bigtable::InstanceAdmin integration test."
56+
./instance_admin_integration_test "${project_id}"
57+
5458
echo
5559
echo "Running bigtable::TableAdmin integration test."
5660
delete_all_tables "${project_id}" "${instance_id:-}"

0 commit comments

Comments
 (0)