Skip to content

Commit 3414515

Browse files
authored
test: migrate tests which previously had hard coded object names to use new Generator#randomObjectName() (#1827)
* test: use Generator#randomObjectName() for all object names in ITSignedUrlTest * test: use Generator#randomObjectName() for all object names in ITObjectTest * test: use Generator#randomObjectName() for all object names in ITBucketTest * test: use Generator#randomObjectName() for all object names in ITDownloadBlobWithoutAuth * test: use Generator#randomObjectName() for all object names in ITDownloadToTest * test: use Generator#randomObjectName() for all object names in ITAccessTest * test: use Generator#randomObjectName() for all object names in ITKmsTest * test: use Generator#randomObjectName() for all object names in ITWriteChannelConnectionPoolTest Log a warning message if a test has defined a junit timeout. Defining a junit timeout runs the test on another thread which we don't control, and don't control the migration of. * test: use Generator#randomObjectName() for all object names in ITBatchTest Rewrite testBatchRequestManyOperations to be more straightforward. Rather than generating 100 different operations which are then bucketed, define the 5 high level operations we're verifying.
1 parent 9511b17 commit 3414515

File tree

10 files changed

+195
-155
lines changed

10 files changed

+195
-155
lines changed

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITAccessTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ public void testRetentionPolicyNoLock() throws Exception {
603603
assertNotNull(remoteBucket2.getRetentionEffectiveTime());
604604
assertThat(remoteBucket2.retentionPolicyIsLocked()).isAnyOf(null, false);
605605

606-
String blobName = "test-create-with-retention-policy-hold";
606+
String blobName = generator.randomObjectName();
607607
BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, blobName).build();
608608
Blob remoteBlob = storage.create(blobInfo);
609609
assertNotNull(remoteBlob.getRetentionExpirationTime());

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBatchTest.java

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616

1717
package com.google.cloud.storage.it;
1818

19+
import static com.google.cloud.storage.TestUtils.assertAll;
1920
import static com.google.common.truth.Truth.assertThat;
2021
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.assertNotNull;
23-
import static org.junit.Assert.assertNull;
2423
import static org.junit.Assert.assertTrue;
2524
import static org.junit.Assert.fail;
2625

@@ -29,6 +28,7 @@
2928
import com.google.cloud.storage.BlobInfo;
3029
import com.google.cloud.storage.BucketInfo;
3130
import com.google.cloud.storage.Storage;
31+
import com.google.cloud.storage.Storage.BlobTargetOption;
3232
import com.google.cloud.storage.StorageBatch;
3333
import com.google.cloud.storage.StorageBatchResult;
3434
import com.google.cloud.storage.StorageException;
@@ -38,23 +38,23 @@
3838
import com.google.cloud.storage.it.runner.annotations.Inject;
3939
import com.google.cloud.storage.it.runner.annotations.SingleBackend;
4040
import com.google.cloud.storage.it.runner.annotations.StorageFixture;
41-
import com.google.common.collect.Lists;
42-
import java.util.List;
41+
import com.google.cloud.storage.it.runner.registry.Generator;
42+
import com.google.common.collect.ImmutableMap;
4343
import org.junit.Before;
4444
import org.junit.Test;
4545
import org.junit.runner.RunWith;
4646

4747
@RunWith(StorageITRunner.class)
4848
@SingleBackend(Backend.PROD)
4949
public class ITBatchTest {
50-
private static final int MAX_BATCH_SIZE = 100;
5150
private static final String CONTENT_TYPE = "text/plain";
5251

5352
@Inject
5453
@StorageFixture(Transport.HTTP)
5554
public Storage storage;
5655

5756
@Inject public BucketInfo bucket;
57+
@Inject public Generator generator;
5858

5959
private String bucketName;
6060

@@ -108,67 +108,56 @@ public void testBatchRequest() {
108108
}
109109

110110
@Test
111-
public void testBatchRequestManyOperations() {
112-
List<StorageBatchResult<Boolean>> deleteResults =
113-
Lists.newArrayListWithCapacity(MAX_BATCH_SIZE);
114-
List<StorageBatchResult<Blob>> getResults = Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
115-
List<StorageBatchResult<Blob>> updateResults =
116-
Lists.newArrayListWithCapacity(MAX_BATCH_SIZE / 2);
111+
public void testBatchRequestManyOperations() throws Exception {
112+
// define some object ids for use in the batch operations
113+
BlobId id1 = BlobId.of(bucketName, generator.randomObjectName());
114+
BlobId id2 = BlobId.of(bucketName, generator.randomObjectName());
115+
BlobId id3 = BlobId.of(bucketName, generator.randomObjectName());
116+
BlobId id4 = BlobId.of(bucketName, generator.randomObjectName());
117+
BlobId id5 = BlobId.of(bucketName, generator.randomObjectName());
118+
119+
ImmutableMap<String, String> ka = ImmutableMap.of("k", "a");
120+
ImmutableMap<String, String> kB = ImmutableMap.of("k", "B");
121+
122+
// Create objects which exist before the batch operations
123+
BlobInfo info1 = BlobInfo.newBuilder(id1).setMetadata(ka).build();
124+
BlobInfo info2 = BlobInfo.newBuilder(id2).setMetadata(ka).build();
125+
BlobInfo info3 = BlobInfo.newBuilder(id3).setMetadata(ka).build();
126+
Blob obj1 = storage.create(info1, BlobTargetOption.doesNotExist());
127+
Blob obj2 = storage.create(info2, BlobTargetOption.doesNotExist());
128+
Blob obj3 = storage.create(info3, BlobTargetOption.doesNotExist());
129+
130+
// Define our batch operations
117131
StorageBatch batch = storage.batch();
118-
for (int i = 0; i < MAX_BATCH_SIZE; i++) {
119-
BlobId blobId = BlobId.of(bucketName, "test-batch-request-many-operations-blob-" + i);
120-
deleteResults.add(batch.delete(blobId));
121-
}
122-
for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
123-
BlobId blobId = BlobId.of(bucketName, "test-batch-request-many-operations-blob-" + i);
124-
getResults.add(batch.get(blobId));
125-
}
126-
for (int i = 0; i < MAX_BATCH_SIZE / 2; i++) {
127-
BlobInfo blob =
128-
BlobInfo.newBuilder(BlobId.of(bucketName, "test-batch-request-many-operations-blob-" + i))
129-
.build();
130-
updateResults.add(batch.update(blob));
131-
}
132-
133-
String sourceBlobName1 = "test-batch-request-many-operations-source-blob-1";
134-
String sourceBlobName2 = "test-batch-request-many-operations-source-blob-2";
135-
BlobInfo sourceBlob1 = BlobInfo.newBuilder(bucketName, sourceBlobName1).build();
136-
BlobInfo sourceBlob2 = BlobInfo.newBuilder(bucketName, sourceBlobName2).build();
137-
assertNotNull(storage.create(sourceBlob1));
138-
assertNotNull(storage.create(sourceBlob2));
139-
BlobInfo updatedBlob2 = sourceBlob2.toBuilder().setContentType(CONTENT_TYPE).build();
140132

141-
StorageBatchResult<Blob> getResult = batch.get(bucketName, sourceBlobName1);
142-
StorageBatchResult<Blob> updateResult = batch.update(updatedBlob2);
133+
StorageBatchResult<Blob> get1Success = batch.get(id1);
134+
StorageBatchResult<Blob> update2Success =
135+
batch.update(
136+
obj2.toBuilder().setMetadata(kB).build(), BlobTargetOption.metagenerationMatch());
137+
StorageBatchResult<Boolean> delete3Success = batch.delete(id3);
138+
StorageBatchResult<Blob> get4Error = batch.get(id4);
139+
StorageBatchResult<Boolean> delete5Error = batch.delete(id5);
143140

141+
// submit the batch
144142
batch.submit();
145143

146-
// Check deletes
147-
for (StorageBatchResult<Boolean> failedDeleteResult : deleteResults) {
148-
assertFalse(failedDeleteResult.get());
149-
}
150-
151-
// Check gets
152-
for (StorageBatchResult<Blob> failedGetResult : getResults) {
153-
assertNull(failedGetResult.get());
154-
}
155-
Blob remoteBlob1 = getResult.get();
156-
assertEquals(sourceBlob1.getBucket(), remoteBlob1.getBucket());
157-
assertEquals(sourceBlob1.getName(), remoteBlob1.getName());
158-
159-
// Check updates
160-
for (StorageBatchResult<Blob> failedUpdateResult : updateResults) {
161-
try {
162-
failedUpdateResult.get();
163-
fail("Expected StorageException");
164-
} catch (StorageException ex) {
165-
// expected
166-
}
167-
}
168-
Blob remoteUpdatedBlob2 = updateResult.get();
169-
assertEquals(sourceBlob2.getBucket(), remoteUpdatedBlob2.getBucket());
170-
assertEquals(sourceBlob2.getName(), remoteUpdatedBlob2.getName());
171-
assertEquals(updatedBlob2.getContentType(), remoteUpdatedBlob2.getContentType());
144+
// verify our expected results
145+
assertAll(
146+
() -> {
147+
Blob blob = get1Success.get();
148+
assertThat(blob.getBucket()).isEqualTo(bucketName);
149+
assertThat(blob.getName()).isEqualTo(id1.getName());
150+
assertThat(blob.getMetadata()).isEqualTo(ka);
151+
},
152+
() -> {
153+
Blob blob = update2Success.get();
154+
assertThat(blob.getBucket()).isEqualTo(bucketName);
155+
assertThat(blob.getName()).isEqualTo(id2.getName());
156+
assertThat(blob.getMetadata()).isEqualTo(kB);
157+
},
158+
() -> assertThat(delete3Success.get()).isTrue(),
159+
() -> assertThat(get4Error.get()).isNull(),
160+
() -> assertThat(delete5Error.get()).isFalse());
172161
}
173162

174163
@Test

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBucketTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public void testEnableDisableBucketDefaultEventBasedHold() {
378378
storage.get(
379379
bucketName, Storage.BucketGetOption.fields(BucketField.DEFAULT_EVENT_BASED_HOLD));
380380
assertTrue(remoteBucket.getDefaultEventBasedHold());
381-
String blobName = "test-create-with-event-based-hold";
381+
String blobName = generator.randomObjectName();
382382
BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, blobName).build();
383383
Blob remoteBlob = storage.create(blobInfo);
384384
assertTrue(remoteBlob.getEventBasedHold());

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITDownloadBlobWithoutAuth.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.cloud.storage.it.runner.annotations.Backend;
3232
import com.google.cloud.storage.it.runner.annotations.CrossRun;
3333
import com.google.cloud.storage.it.runner.annotations.Inject;
34+
import com.google.cloud.storage.it.runner.registry.Generator;
3435
import java.util.Iterator;
3536
import org.junit.Test;
3637
import org.junit.runner.RunWith;
@@ -45,6 +46,7 @@ public class ITDownloadBlobWithoutAuth {
4546
@Inject public Storage storage;
4647

4748
@Inject public BucketInfo bucket;
49+
@Inject public Generator generator;
4850

4951
@Test
5052
public void testDownloadPublicBlobWithoutAuthentication() {
@@ -75,7 +77,7 @@ public void testDownloadPublicBlobWithoutAuthentication() {
7577
// try to download blobs from a bucket that requires authentication
7678
// authenticated client will succeed
7779
// unauthenticated client will receive an exception
78-
String sourceBlobName = "source-blob-name";
80+
String sourceBlobName = generator.randomObjectName();
7981
BlobInfo sourceBlob = BlobInfo.newBuilder(bucketName, sourceBlobName).build();
8082
assertThat(storage.create(sourceBlob)).isNotNull();
8183
assertThat(storage.readAllBytes(bucketName, sourceBlobName)).isNotNull();

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITDownloadToTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.cloud.storage.it.runner.annotations.Backend;
3030
import com.google.cloud.storage.it.runner.annotations.CrossRun;
3131
import com.google.cloud.storage.it.runner.annotations.Inject;
32+
import com.google.cloud.storage.it.runner.registry.Generator;
3233
import java.io.File;
3334
import java.io.IOException;
3435
import java.nio.file.Files;
@@ -49,19 +50,22 @@ public final class ITDownloadToTest {
4950

5051
@Inject public Storage storage;
5152
@Inject public BucketInfo bucket;
53+
@Inject public Generator generator;
54+
55+
private BlobId blobId;
5256

5357
@Before
5458
public void before() {
55-
BlobId blobId = BlobId.of(bucket.getName(), "zipped_blob");
59+
String objectString = generator.randomObjectName();
60+
blobId = BlobId.of(bucket.getName(), objectString);
5661
BlobInfo blobInfo =
5762
BlobInfo.newBuilder(blobId).setContentEncoding("gzip").setContentType("text/plain").build();
5863
storage.create(blobInfo, helloWorldGzipBytes);
5964
}
6065

6166
@Test
6267
public void downloadTo_returnRawInputStream_yes() throws IOException {
63-
BlobId blobId = BlobId.of(bucket.getName(), "zipped_blob");
64-
Path helloWorldTxtGz = File.createTempFile("helloWorld", ".txt.gz").toPath();
68+
Path helloWorldTxtGz = File.createTempFile(blobId.getName(), ".txt.gz").toPath();
6569
storage.downloadTo(
6670
blobId, helloWorldTxtGz, Storage.BlobSourceOption.shouldReturnRawInputStream(true));
6771

@@ -74,8 +78,7 @@ public void downloadTo_returnRawInputStream_yes() throws IOException {
7478

7579
@Test
7680
public void downloadTo_returnRawInputStream_no() throws IOException {
77-
BlobId blobId = BlobId.of(bucket.getName(), "zipped_blob");
78-
Path helloWorldTxt = File.createTempFile("helloWorld", ".txt").toPath();
81+
Path helloWorldTxt = File.createTempFile(blobId.getName(), ".txt").toPath();
7982
storage.downloadTo(
8083
blobId, helloWorldTxt, Storage.BlobSourceOption.shouldReturnRawInputStream(false));
8184
byte[] actualTxtBytes = Files.readAllBytes(helloWorldTxt);

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITKmsTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void testUpdateBucketDefaultKmsKeyName() {
113113

114114
@Test
115115
public void testCreateBlobWithKmsKeyName() {
116-
String blobName = "test-create-with-kms-key-name-blob";
116+
String blobName = generator.randomObjectName();
117117
String bucketName = bucket.getName();
118118
BlobInfo blob = BlobInfo.newBuilder(bucketName, blobName).build();
119119
Blob remoteBlob =
@@ -130,7 +130,7 @@ public void testCreateBlobWithKmsKeyName() {
130130

131131
@Test(expected = StorageException.class)
132132
public void testCreateBlobWithKmsKeyNameAndCustomerSuppliedKeyFails() {
133-
String blobName = "test-create-with-kms-key-name-blob";
133+
String blobName = generator.randomObjectName();
134134
BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).build();
135135
storage.create(
136136
blob,
@@ -168,7 +168,7 @@ public void testCreateBlobWithDefaultKmsKeyName() {
168168

169169
@Test
170170
public void testGetBlobKmsKeyNameField() {
171-
String blobName = "test-get-selected-kms-key-name-field-blob";
171+
String blobName = generator.randomObjectName();
172172
BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).setContentType(CONTENT_TYPE).build();
173173
assertNotNull(
174174
storage.create(blob, Storage.BlobTargetOption.kmsKeyName(kms.getKey1().getName())));
@@ -181,7 +181,7 @@ public void testGetBlobKmsKeyNameField() {
181181

182182
@Test
183183
public void testRotateFromCustomerEncryptionToKmsKey() {
184-
String sourceBlobName = "test-copy-blob-encryption-key-source";
184+
String sourceBlobName = generator.randomObjectName();
185185
BlobId source = BlobId.of(bucket.getName(), sourceBlobName);
186186
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
187187
Blob remoteBlob =
@@ -190,7 +190,7 @@ public void testRotateFromCustomerEncryptionToKmsKey() {
190190
BLOB_BYTE_CONTENT,
191191
Storage.BlobTargetOption.encryptionKey(KEY));
192192
assertNotNull(remoteBlob);
193-
String targetBlobName = "test-copy-blob-kms-key-target";
193+
String targetBlobName = generator.randomObjectName();
194194
BlobInfo target =
195195
BlobInfo.newBuilder(bucket, targetBlobName)
196196
.setContentType(CONTENT_TYPE)
@@ -216,7 +216,7 @@ public void testRotateFromCustomerEncryptionToKmsKey() {
216216

217217
@Test(expected = StorageException.class)
218218
public void testRotateFromCustomerEncryptionToKmsKeyWithCustomerEncryption() {
219-
String sourceBlobName = "test-copy-blob-encryption-key-source";
219+
String sourceBlobName = generator.randomObjectName();
220220
BlobId source = BlobId.of(bucket.getName(), sourceBlobName);
221221
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
222222
Blob remoteBlob =
@@ -225,7 +225,7 @@ public void testRotateFromCustomerEncryptionToKmsKeyWithCustomerEncryption() {
225225
BLOB_BYTE_CONTENT,
226226
Storage.BlobTargetOption.encryptionKey(KEY));
227227
assertNotNull(remoteBlob);
228-
String targetBlobName = "test-copy-blob-kms-key-target";
228+
String targetBlobName = generator.randomObjectName();
229229
BlobInfo target =
230230
BlobInfo.newBuilder(bucket, targetBlobName)
231231
.setContentType(CONTENT_TYPE)
@@ -246,7 +246,7 @@ public void testRotateFromCustomerEncryptionToKmsKeyWithCustomerEncryption() {
246246
@Test
247247
public void testWriterWithKmsKeyName() throws IOException {
248248
// Write an empty object with a kmsKeyName.
249-
String blobName = "test-empty-blob";
249+
String blobName = generator.randomObjectName();
250250
BlobInfo blobInfo = BlobInfo.newBuilder(bucket, blobName).build();
251251
Blob blob =
252252
storage.create(blobInfo, Storage.BlobTargetOption.kmsKeyName(kms.getKey1().getName()));

0 commit comments

Comments
 (0)