|
| 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 | +} |
0 commit comments