Skip to content

Commit fb33e9b

Browse files
authored
refactor(storage): common code for client unit tests (#6256)
Not a lot of code saving, but will simplify the next change where I want to deprecate the `storage::Client()` constructor from a `std::shared_ptr<RawClient>`.
1 parent a6fa126 commit fb33e9b

17 files changed

+548
-537
lines changed

google/cloud/storage/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ if (BUILD_TESTING)
354354
add_library(
355355
storage_client_testing # cmake-format: sort
356356
testing/canonical_errors.h
357+
testing/client_unit_test.cc
358+
testing/client_unit_test.h
357359
testing/constants.h
358360
testing/mock_client.h
359361
testing/mock_fake_clock.cc

google/cloud/storage/bucket_test.cc

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "google/cloud/storage/oauth2/google_credentials.h"
1818
#include "google/cloud/storage/retry_policy.h"
1919
#include "google/cloud/storage/testing/canonical_errors.h"
20+
#include "google/cloud/storage/testing/client_unit_test.h"
2021
#include "google/cloud/storage/testing/mock_client.h"
2122
#include "google/cloud/storage/testing/retry_tests.h"
2223
#include "google/cloud/testing_util/status_matchers.h"
@@ -45,28 +46,7 @@ using ::testing::ReturnRef;
4546
*
4647
* https://cloud.google.com/storage/docs/json_api/v1/buckets
4748
*/
48-
class BucketTest : public ::testing::Test {
49-
protected:
50-
void SetUp() override {
51-
mock_ = std::make_shared<testing::MockClient>();
52-
EXPECT_CALL(*mock_, client_options())
53-
.WillRepeatedly(ReturnRef(client_options_));
54-
client_.reset(new Client{
55-
std::shared_ptr<internal::RawClient>(mock_),
56-
LimitedErrorCountRetryPolicy(2),
57-
ExponentialBackoffPolicy(std::chrono::milliseconds(1),
58-
std::chrono::milliseconds(1), 2.0)});
59-
}
60-
void TearDown() override {
61-
client_.reset();
62-
mock_.reset();
63-
}
64-
65-
std::shared_ptr<testing::MockClient> mock_;
66-
std::unique_ptr<Client> client_;
67-
ClientOptions client_options_ =
68-
ClientOptions(oauth2::CreateAnonymousCredentials());
69-
};
49+
class BucketTest : public ::google::cloud::storage::testing::ClientUnitTest {};
7050

7151
TEST_F(BucketTest, CreateBucket) {
7252
std::string text = R"""({
@@ -97,7 +77,8 @@ TEST_F(BucketTest, CreateBucket) {
9777
EXPECT_EQ("test-project-name", r.project_id());
9878
return make_status_or(expected);
9979
});
100-
auto actual = client_->CreateBucket(
80+
auto client = ClientForMock();
81+
auto actual = client.CreateBucket(
10182
"test-bucket-name",
10283
BucketMetadata().set_location("US").set_storage_class("STANDARD"));
10384
ASSERT_STATUS_OK(actual);
@@ -117,8 +98,9 @@ TEST_F(BucketTest, CreateBucketTooManyFailures) {
11798
}
11899

119100
TEST_F(BucketTest, CreateBucketPermanentFailure) {
101+
auto client = ClientForMock();
120102
testing::PermanentFailureStatusTest<BucketMetadata>(
121-
*client_, EXPECT_CALL(*mock_, CreateBucket(_)),
103+
client, EXPECT_CALL(*mock_, CreateBucket(_)),
122104
[](Client& client) {
123105
return client
124106
.CreateBucketForProject("test-bucket-name", "test-project-name",
@@ -151,7 +133,8 @@ TEST_F(BucketTest, GetBucketMetadata) {
151133
EXPECT_EQ("foo-bar-baz", r.bucket_name());
152134
return make_status_or(expected);
153135
});
154-
auto actual = client_->GetBucketMetadata("foo-bar-baz");
136+
auto client = ClientForMock();
137+
auto actual = client.GetBucketMetadata("foo-bar-baz");
155138
ASSERT_STATUS_OK(actual);
156139
EXPECT_EQ(expected, *actual);
157140
}
@@ -166,8 +149,9 @@ TEST_F(BucketTest, GetMetadataTooManyFailures) {
166149
}
167150

168151
TEST_F(BucketTest, GetMetadataPermanentFailure) {
152+
auto client = ClientForMock();
169153
testing::PermanentFailureStatusTest<BucketMetadata>(
170-
*client_, EXPECT_CALL(*mock_, GetBucketMetadata(_)),
154+
client, EXPECT_CALL(*mock_, GetBucketMetadata(_)),
171155
[](Client& client) {
172156
return client.GetBucketMetadata("test-bucket-name").status();
173157
},
@@ -181,7 +165,8 @@ TEST_F(BucketTest, DeleteBucket) {
181165
EXPECT_EQ("foo-bar-baz", r.bucket_name());
182166
return make_status_or(internal::EmptyResponse{});
183167
});
184-
auto status = client_->DeleteBucket("foo-bar-baz");
168+
auto client = ClientForMock();
169+
auto status = client.DeleteBucket("foo-bar-baz");
185170
ASSERT_STATUS_OK(status);
186171
}
187172

@@ -197,8 +182,9 @@ TEST_F(BucketTest, DeleteBucketTooManyFailures) {
197182
}
198183

199184
TEST_F(BucketTest, DeleteBucketPermanentFailure) {
185+
auto client = ClientForMock();
200186
testing::PermanentFailureStatusTest<internal::EmptyResponse>(
201-
*client_, EXPECT_CALL(*mock_, DeleteBucket(_)),
187+
client, EXPECT_CALL(*mock_, DeleteBucket(_)),
202188
[](Client& client) { return client.DeleteBucket("test-bucket-name"); },
203189
"DeleteBucket");
204190
}
@@ -228,7 +214,8 @@ TEST_F(BucketTest, UpdateBucket) {
228214
EXPECT_EQ("STANDARD", r.metadata().storage_class());
229215
return make_status_or(expected);
230216
});
231-
auto actual = client_->UpdateBucket(
217+
auto client = ClientForMock();
218+
auto actual = client.UpdateBucket(
232219
"test-bucket-name",
233220
BucketMetadata().set_location("US").set_storage_class("STANDARD"));
234221
ASSERT_STATUS_OK(actual);
@@ -252,8 +239,9 @@ TEST_F(BucketTest, UpdateBucketTooManyFailures) {
252239
}
253240

254241
TEST_F(BucketTest, UpdateBucketPermanentFailure) {
242+
auto client = ClientForMock();
255243
testing::PermanentFailureStatusTest<BucketMetadata>(
256-
*client_, EXPECT_CALL(*mock_, UpdateBucket(_)),
244+
client, EXPECT_CALL(*mock_, UpdateBucket(_)),
257245
[](Client& client) {
258246
return client.UpdateBucket("test-bucket-name", BucketMetadata())
259247
.status();
@@ -284,7 +272,8 @@ TEST_F(BucketTest, PatchBucket) {
284272
EXPECT_THAT(r.payload(), HasSubstr("STANDARD"));
285273
return make_status_or(expected);
286274
});
287-
auto actual = client_->PatchBucket(
275+
auto client = ClientForMock();
276+
auto actual = client.PatchBucket(
288277
"test-bucket-name",
289278
BucketMetadataPatchBuilder().SetStorageClass("STANDARD"));
290279
ASSERT_STATUS_OK(actual);
@@ -309,8 +298,9 @@ TEST_F(BucketTest, PatchBucketTooManyFailures) {
309298
}
310299

311300
TEST_F(BucketTest, PatchBucketPermanentFailure) {
301+
auto client = ClientForMock();
312302
testing::PermanentFailureStatusTest<BucketMetadata>(
313-
*client_, EXPECT_CALL(*mock_, PatchBucket(_)),
303+
client, EXPECT_CALL(*mock_, PatchBucket(_)),
314304
[](Client& client) {
315305
return client
316306
.PatchBucket("test-bucket-name", BucketMetadataPatchBuilder())
@@ -330,7 +320,8 @@ TEST_F(BucketTest, GetBucketIamPolicy) {
330320
EXPECT_EQ("test-bucket-name", r.bucket_name());
331321
return make_status_or(expected);
332322
});
333-
auto actual = client_->GetBucketIamPolicy("test-bucket-name");
323+
auto client = ClientForMock();
324+
auto actual = client.GetBucketIamPolicy("test-bucket-name");
334325
ASSERT_STATUS_OK(actual);
335326
EXPECT_EQ(expected, *actual);
336327
}
@@ -345,8 +336,9 @@ TEST_F(BucketTest, GetBucketIamPolicyTooManyFailures) {
345336
}
346337

347338
TEST_F(BucketTest, GetBucketIamPolicyPermanentFailure) {
339+
auto client = ClientForMock();
348340
testing::PermanentFailureStatusTest<IamPolicy>(
349-
*client_, EXPECT_CALL(*mock_, GetBucketIamPolicy(_)),
341+
client, EXPECT_CALL(*mock_, GetBucketIamPolicy(_)),
350342
[](Client& client) {
351343
return client.GetBucketIamPolicy("test-bucket-name").status();
352344
},
@@ -365,7 +357,8 @@ TEST_F(BucketTest, SetBucketIamPolicy) {
365357
EXPECT_THAT(r.json_payload(), HasSubstr("test-user"));
366358
return make_status_or(expected);
367359
});
368-
auto actual = client_->SetBucketIamPolicy("test-bucket-name", expected);
360+
auto client = ClientForMock();
361+
auto actual = client.SetBucketIamPolicy("test-bucket-name", expected);
369362
ASSERT_STATUS_OK(actual);
370363
EXPECT_EQ(expected, *actual);
371364
}
@@ -387,8 +380,9 @@ TEST_F(BucketTest, SetBucketIamPolicyTooManyFailures) {
387380
}
388381

389382
TEST_F(BucketTest, SetBucketIamPolicyPermanentFailure) {
383+
auto client = ClientForMock();
390384
testing::PermanentFailureStatusTest<IamPolicy>(
391-
*client_, EXPECT_CALL(*mock_, SetBucketIamPolicy(_)),
385+
client, EXPECT_CALL(*mock_, SetBucketIamPolicy(_)),
392386
[](Client& client) {
393387
return client.SetBucketIamPolicy("test-bucket-name", IamPolicy{})
394388
.status();
@@ -409,8 +403,9 @@ TEST_F(BucketTest, TestBucketIamPermissions) {
409403
EXPECT_THAT(r.permissions(), ElementsAre("storage.buckets.delete"));
410404
return make_status_or(expected);
411405
});
412-
auto actual = client_->TestBucketIamPermissions("test-bucket-name",
413-
{"storage.buckets.delete"});
406+
auto client = ClientForMock();
407+
auto actual = client.TestBucketIamPermissions("test-bucket-name",
408+
{"storage.buckets.delete"});
414409
ASSERT_STATUS_OK(actual);
415410
EXPECT_THAT(*actual, ElementsAreArray(expected.permissions));
416411
}
@@ -426,9 +421,10 @@ TEST_F(BucketTest, TestBucketIamPermissionsTooManyFailures) {
426421
}
427422

428423
TEST_F(BucketTest, TestBucketIamPermissionsPermanentFailure) {
424+
auto client = ClientForMock();
429425
testing::PermanentFailureStatusTest<
430426
internal::TestBucketIamPermissionsResponse>(
431-
*client_, EXPECT_CALL(*mock_, TestBucketIamPermissions(_)),
427+
client, EXPECT_CALL(*mock_, TestBucketIamPermissions(_)),
432428
[](Client& client) {
433429
return client.TestBucketIamPermissions("test-bucket-name", {}).status();
434430
},
@@ -459,7 +455,8 @@ TEST_F(BucketTest, LockBucketRetentionPolicy) {
459455
EXPECT_EQ(42, r.metageneration());
460456
return make_status_or(expected);
461457
});
462-
auto metadata = client_->LockBucketRetentionPolicy("test-bucket-name", 42U);
458+
auto client = ClientForMock();
459+
auto metadata = client.LockBucketRetentionPolicy("test-bucket-name", 42U);
463460
ASSERT_STATUS_OK(metadata);
464461
EXPECT_EQ(expected, *metadata);
465462
}
@@ -475,8 +472,9 @@ TEST_F(BucketTest, LockBucketRetentionPolicyTooManyFailures) {
475472
}
476473

477474
TEST_F(BucketTest, LockBucketRetentionPolicyPermanentFailure) {
475+
auto client = ClientForMock();
478476
testing::PermanentFailureStatusTest<BucketMetadata>(
479-
*client_, EXPECT_CALL(*mock_, LockBucketRetentionPolicy(_)),
477+
client, EXPECT_CALL(*mock_, LockBucketRetentionPolicy(_)),
480478
[](Client& client) {
481479
return client.LockBucketRetentionPolicy("test-bucket-name", 1U)
482480
.status();

0 commit comments

Comments
 (0)