Skip to content

Commit 3a68be4

Browse files
authored
test: update ObjectsFixture to allow discrimination by @BucketFixture (#1876)
Sometimes it's useful to specify that the ObjectsFixture should use the RequesterPays bucket
1 parent cc60a52 commit 3a68be4

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/BackendResources.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private BackendResources(
4747
TestRunScopedInstance<BucketInfoShim> bucket,
4848
TestRunScopedInstance<BucketInfoShim> bucketRequesterPays,
4949
TestRunScopedInstance<ObjectsFixture> objectsFixture,
50+
TestRunScopedInstance<ObjectsFixture> objectsFixtureRequesterPays,
5051
TestRunScopedInstance<KmsFixture> kmsFixture) {
5152
this.backend = backend;
5253
this.protectedBucketNames = protectedBucketNames;
@@ -63,8 +64,14 @@ private BackendResources(
6364
backendIs(backend).and(isRequesterPaysBucket())),
6465
RegistryEntry.of(
6566
7, BucketInfo.class, bucket, backendIs(backend).and(isDefaultBucket())),
66-
RegistryEntry.of(8, ObjectsFixture.class, objectsFixture, backendIs(backend)),
67-
RegistryEntry.of(9, KmsFixture.class, kmsFixture, backendIs(backend)));
67+
RegistryEntry.of(
68+
8, ObjectsFixture.class, objectsFixture, backendIs(backend).and(isDefaultBucket())),
69+
RegistryEntry.of(
70+
9,
71+
ObjectsFixture.class,
72+
objectsFixtureRequesterPays,
73+
backendIs(backend).and(isRequesterPaysBucket())),
74+
RegistryEntry.of(10, KmsFixture.class, kmsFixture, backendIs(backend)));
6875
}
6976

7077
public ImmutableList<RegistryEntry<?>> getRegistryEntries() {
@@ -141,6 +148,11 @@ static BackendResources of(Backend backend) {
141148
TestRunScopedInstance.of(
142149
"OBJECTS_FIXTURE_" + backend.name(),
143150
() -> new ObjectsFixture(storageJson.get().getStorage(), bucket.get().getBucketInfo()));
151+
TestRunScopedInstance<ObjectsFixture> objectsFixtureRequesterPays =
152+
TestRunScopedInstance.of(
153+
"OBJECTS_FIXTURE_REQUESTER_PAYS_" + backend.name(),
154+
() ->
155+
new ObjectsFixture(storageJson.get().getStorage(), bucketRp.get().getBucketInfo()));
144156
TestRunScopedInstance<KmsFixture> kmsFixture =
145157
TestRunScopedInstance.of(
146158
"KMS_FIXTURE_" + backend.name(), () -> KmsFixture.of(storageJson.get().getStorage()));
@@ -153,6 +165,7 @@ static BackendResources of(Backend backend) {
153165
bucket,
154166
bucketRp,
155167
objectsFixture,
168+
objectsFixtureRequesterPays,
156169
kmsFixture);
157170
}
158171
}

google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/ObjectsFixture.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.cloud.storage.BucketInfo;
2323
import com.google.cloud.storage.DataGenerator;
2424
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.Storage.BlobGetOption;
2526
import com.google.cloud.storage.Storage.BlobTargetOption;
2627
import com.google.cloud.storage.Storage.ComposeRequest;
2728
import com.google.cloud.storage.it.ChecksummedTestContent;
@@ -34,6 +35,9 @@ public final class ObjectsFixture implements ManagedLifecycle {
3435
private final Storage s;
3536
private final BucketInfo bucket;
3637

38+
private final BlobTargetOption[] blobTargetOptions;
39+
private final BlobGetOption[] blobGetOptions;
40+
3741
private BlobInfo info1;
3842
private BlobInfo info2;
3943
private BlobInfo info3;
@@ -43,6 +47,21 @@ public final class ObjectsFixture implements ManagedLifecycle {
4347
ObjectsFixture(Storage s, BucketInfo bucket) {
4448
this.s = s;
4549
this.bucket = bucket;
50+
boolean isRequesterPays = Boolean.TRUE.equals(bucket.requesterPays());
51+
String projectId = s.getOptions().getProjectId();
52+
if (isRequesterPays) {
53+
blobTargetOptions =
54+
new BlobTargetOption[] {
55+
BlobTargetOption.doesNotExist(), BlobTargetOption.userProject(projectId)
56+
};
57+
} else {
58+
blobTargetOptions = new BlobTargetOption[] {BlobTargetOption.doesNotExist()};
59+
}
60+
if (isRequesterPays) {
61+
blobGetOptions = new BlobGetOption[] {BlobGetOption.userProject(projectId)};
62+
} else {
63+
blobGetOptions = new BlobGetOption[] {};
64+
}
4665
}
4766

4867
@Override
@@ -83,41 +102,38 @@ public void start() {
83102
BlobInfo info2 = BlobInfo.newBuilder(blobId2).setMetadata(ImmutableMap.of("pow", "2")).build();
84103
BlobInfo info3 = BlobInfo.newBuilder(blobId3).setMetadata(ImmutableMap.of("pow", "3")).build();
85104
BlobInfo info4 = BlobInfo.newBuilder(blobId4).setMetadata(ImmutableMap.of("pow", "4")).build();
86-
s.create(info1, "A".getBytes(StandardCharsets.UTF_8), BlobTargetOption.doesNotExist());
105+
s.create(info1, "A".getBytes(StandardCharsets.UTF_8), blobTargetOptions);
87106

88107
ComposeRequest c2 =
89108
ComposeRequest.newBuilder()
90109
.addSource(blobId1.getName(), blobId1.getName())
91110
.setTarget(info2)
92-
.setTargetOptions(BlobTargetOption.doesNotExist())
111+
.setTargetOptions(blobTargetOptions)
93112
.build();
94113
ComposeRequest c3 =
95114
ComposeRequest.newBuilder()
96115
.addSource(blobId2.getName(), blobId2.getName())
97116
.setTarget(info3)
98-
.setTargetOptions(BlobTargetOption.doesNotExist())
117+
.setTargetOptions(blobTargetOptions)
99118
.build();
100119
ComposeRequest c4 =
101120
ComposeRequest.newBuilder()
102121
.addSource(blobId3.getName(), blobId3.getName())
103122
.setTarget(info4)
104-
.setTargetOptions(BlobTargetOption.doesNotExist())
123+
.setTargetOptions(blobTargetOptions)
105124
.build();
106125
s.compose(c2);
107126
s.compose(c3);
108127
s.compose(c4);
109128

110-
this.info1 = s.get(blobId1).asBlobInfo();
111-
this.info2 = s.get(blobId2).asBlobInfo();
112-
this.info3 = s.get(blobId3).asBlobInfo();
113-
this.info4 = s.get(blobId4).asBlobInfo();
129+
this.info1 = s.get(blobId1, blobGetOptions).asBlobInfo();
130+
this.info2 = s.get(blobId2, blobGetOptions).asBlobInfo();
131+
this.info3 = s.get(blobId3, blobGetOptions).asBlobInfo();
132+
this.info4 = s.get(blobId4, blobGetOptions).asBlobInfo();
114133

115134
byte[] bytes = DataGenerator.base64Characters().genBytes(512 * 1024);
116135
Blob obj512KiB =
117-
s.create(
118-
BlobInfo.newBuilder(bucket, "obj512KiB").build(),
119-
bytes,
120-
BlobTargetOption.doesNotExist());
136+
s.create(BlobInfo.newBuilder(bucket, "obj512KiB").build(), bytes, blobTargetOptions);
121137
this.obj512KiB = new ObjectAndContent(obj512KiB.asBlobInfo(), ChecksummedTestContent.of(bytes));
122138
}
123139

0 commit comments

Comments
 (0)