From 8d01ae34c018c34a1002187705385775e70a4113 Mon Sep 17 00:00:00 2001 From: Scott Hart Date: Thu, 27 Jun 2024 13:00:30 -0400 Subject: [PATCH] impl: add integration tests for LRO Start and Await methods (#14377) --- .../compute_integration_test.cc | 19 ++++++++-- .../instance_admin_integration_test.cc | 38 +++++++++++++++++++ .../instance_admin_rest_integration_test.cc | 36 ++++++++++++++++++ 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/google/cloud/compute/integration_tests/compute_integration_test.cc b/google/cloud/compute/integration_tests/compute_integration_test.cc index f64310f92c315..66de96302e8f0 100644 --- a/google/cloud/compute/integration_tests/compute_integration_test.cc +++ b/google/cloud/compute/integration_tests/compute_integration_test.cc @@ -89,8 +89,21 @@ TEST_F(ComputeIntegrationTest, CreateDisks) { disk.set_name(CreateRandomName("int-test-disk-")); disk.set_size_gb("10"); (*disk.mutable_labels())["test"] = "test"; - auto result = client.InsertDisk(project_id_, zone_, disk).get(); - ASSERT_THAT(result, testing_util::IsOk()); + auto start_result = client.InsertDisk(ExperimentalTag{}, NoAwaitTag{}, + project_id_, zone_, disk); + ASSERT_THAT(start_result, IsOk()); + + // Exercise serialization and deserialization to mimic the use case where the + // returned operation is stored elsewhere, the process ends, and then another + // process is started that uses the serialized string to resume waiting on the + // LRO to finish. + std::string operation_string; + EXPECT_TRUE(start_result->SerializeToString(&operation_string)); + google::cloud::cpp::compute::v1::Operation operation; + EXPECT_TRUE(operation.ParseFromString(operation_string)); + + auto await_result = client.InsertDisk(ExperimentalTag{}, operation).get(); + ASSERT_THAT(await_result, IsOk()); auto get_disk = client.GetDisk(project_id_, zone_, disk.name()); ASSERT_THAT(get_disk, IsOk()); @@ -151,7 +164,7 @@ TEST_F(ComputeIntegrationTest, VerifyUpdateSendsUpdateMaskParameter) { disk.set_size_gb("10"); (*disk.mutable_labels())["test"] = "test"; auto result = client.InsertDisk(project_id_, zone_, disk).get(); - ASSERT_THAT(result, testing_util::IsOk()); + ASSERT_THAT(result, IsOk()); google::cloud::cpp::compute::v1::Disk disk_update = disk; disk_update.mutable_labels()->clear(); diff --git a/google/cloud/spanner/admin/integration_tests/instance_admin_integration_test.cc b/google/cloud/spanner/admin/integration_tests/instance_admin_integration_test.cc index 491a9bf3c8af5..4bff744900a0f 100644 --- a/google/cloud/spanner/admin/integration_tests/instance_admin_integration_test.cc +++ b/google/cloud/spanner/admin/integration_tests/instance_admin_integration_test.cc @@ -214,6 +214,44 @@ TEST_F(InstanceAdminClientTest, InstanceCRUDOperations) { EXPECT_STATUS_OK(client_.DeleteInstance(in.FullName())); } +TEST_F(InstanceAdminClientTest, CreateInstanceStartAwait) { + if (!Emulator() && !RunSlowInstanceTests()) { + GTEST_SKIP() << "skipping slow instance tests; set " + << "GOOGLE_CLOUD_CPP_SPANNER_SLOW_INTEGRATION_TESTS=instance" + << " to override"; + } + + Instance in(ProjectId(), spanner_testing::RandomInstanceName(generator_)); + + auto config_name = spanner_testing::PickInstanceConfig( + in.project(), generator_, + [](google::spanner::admin::instance::v1::InstanceConfig const& config) { + return absl::StrContains(config.name(), "/regional-us-west"); + }); + ASSERT_FALSE(config_name.empty()) << "could not get an instance config"; + + auto operation = + client_.CreateInstance(ExperimentalTag{}, NoAwaitTag{}, + CreateInstanceRequestBuilder(in, config_name) + .SetDisplayName("test-display-name") + .SetNodeCount(1) + .SetLabels({{"label-key", "label-value"}}) + .Build()); + ASSERT_STATUS_OK(operation); + + // Verify that an error is returned if there is a mismatch between the rpc + // that returned the operation and the rpc in which is it used. + auto instance_config = + client_.CreateInstanceConfig(ExperimentalTag{}, *operation).get(); + EXPECT_THAT(instance_config, StatusIs(StatusCode::kInvalidArgument)); + + auto instance = client_.CreateInstance(ExperimentalTag{}, *operation).get(); + ASSERT_STATUS_OK(instance); + EXPECT_EQ(instance->name(), in.FullName()); + EXPECT_EQ(instance->display_name(), "test-display-name"); + EXPECT_STATUS_OK(client_.DeleteInstance(in.FullName())); +} + TEST_F(InstanceAdminClientTest, InstanceConfig) { auto project_id = ProjectId(); ASSERT_FALSE(project_id.empty()); diff --git a/google/cloud/spanner/admin/integration_tests/instance_admin_rest_integration_test.cc b/google/cloud/spanner/admin/integration_tests/instance_admin_rest_integration_test.cc index 032f22878b321..9ebb841232f67 100644 --- a/google/cloud/spanner/admin/integration_tests/instance_admin_rest_integration_test.cc +++ b/google/cloud/spanner/admin/integration_tests/instance_admin_rest_integration_test.cc @@ -44,6 +44,7 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN namespace { using ::google::cloud::testing_util::StatusIs; +using ::testing::Eq; std::string const& ProjectId() { static std::string project_id = @@ -257,6 +258,41 @@ TEST_F(InstanceAdminClientRestTest, InstanceCRUDOperations) { EXPECT_STATUS_OK(client_.DeleteInstance(in.FullName())); } +TEST_F(InstanceAdminClientRestTest, CreateInstanceStartAwait) { + if (!Emulator() && !RunSlowInstanceTests()) { + GTEST_SKIP() << "skipping slow instance tests; set " + << "GOOGLE_CLOUD_CPP_SPANNER_SLOW_INTEGRATION_TESTS=instance" + << " to override"; + } + + Instance in(ProjectId(), spanner_testing::RandomInstanceName(generator_)); + + auto config_name = spanner_testing::PickInstanceConfig( + in.project(), generator_, + [](google::spanner::admin::instance::v1::InstanceConfig const& config) { + return absl::StrContains(config.name(), "/regional-us-west"); + }); + ASSERT_FALSE(config_name.empty()) << "could not get an instance config"; + + auto operation = + client_.CreateInstance(ExperimentalTag{}, NoAwaitTag{}, + CreateInstanceRequestBuilder(in, config_name) + .SetDisplayName("test-display-name") + .SetNodeCount(1) + .SetLabels({{"label-key", "label-value"}}) + .Build()); + ASSERT_STATUS_OK(operation); + auto instance_config = + client_.CreateInstanceConfig(ExperimentalTag{}, *operation).get(); + EXPECT_THAT(instance_config, StatusIs(StatusCode::kInvalidArgument)); + + auto instance = client_.CreateInstance(ExperimentalTag{}, *operation).get(); + ASSERT_STATUS_OK(instance); + EXPECT_EQ(instance->name(), in.FullName()); + EXPECT_EQ(instance->display_name(), "test-display-name"); + EXPECT_STATUS_OK(client_.DeleteInstance(in.FullName())); +} + } // namespace GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END } // namespace spanner