Skip to content

Commit 98fb964

Browse files
authored
test: add new Zone fixture to IT test runner registry (googleapis#2899)
Update KmsFixture and BucketFixtures to use the same region for location of keys and buckets.
1 parent 8be3b54 commit 98fb964

File tree

6 files changed

+197
-10
lines changed

6 files changed

+197
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void testCreateBlobWithDefaultKmsKeyName() {
145145
assertEquals(bucket.getDefaultKmsKeyName(), kms.getKey1().getName());
146146

147147
try {
148-
String blobName = "test-create-with-default-kms-key-name-blob";
148+
String blobName = generator.randomObjectName();
149149
BlobInfo blob = BlobInfo.newBuilder(bucket, blobName).build();
150150
Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT);
151151
assertNotNull(remoteBlob);

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ public String toString() {
8383
}
8484

8585
@SuppressWarnings("SwitchStatementWithTooFewBranches")
86-
static BackendResources of(Backend backend, TestRunScopedInstance<OtelSdkShim> otelSdk) {
86+
static BackendResources of(
87+
Backend backend,
88+
TestRunScopedInstance<OtelSdkShim> otelSdk,
89+
TestRunScopedInstance<Zone.ZoneShim> zone) {
8790
ProtectedBucketNames protectedBucketNames = new ProtectedBucketNames();
8891
TestRunScopedInstance<StorageInstance> storageJson =
8992
TestRunScopedInstance.of(
@@ -181,7 +184,11 @@ static BackendResources of(Backend backend, TestRunScopedInstance<OtelSdkShim> o
181184
String bucketName = String.format("java-storage-grpc-%s", UUID.randomUUID());
182185
protectedBucketNames.add(bucketName);
183186
return new BucketInfoShim(
184-
BucketInfo.of(bucketName), storageJson.get().getStorage(), ctrl.get().getCtrl());
187+
BucketInfo.newBuilder(bucketName)
188+
.setLocation(zone.get().get().getRegion())
189+
.build(),
190+
storageJson.get().getStorage(),
191+
ctrl.get().getCtrl());
185192
});
186193
TestRunScopedInstance<BucketInfoShim> bucketRp =
187194
TestRunScopedInstance.of(
@@ -190,7 +197,10 @@ static BackendResources of(Backend backend, TestRunScopedInstance<OtelSdkShim> o
190197
String bucketName = String.format("java-storage-grpc-rp-%s", UUID.randomUUID());
191198
protectedBucketNames.add(bucketName);
192199
return new BucketInfoShim(
193-
BucketInfo.newBuilder(bucketName).setRequesterPays(true).build(),
200+
BucketInfo.newBuilder(bucketName)
201+
.setLocation(zone.get().get().getRegion())
202+
.setRequesterPays(true)
203+
.build(),
194204
storageJson.get().getStorage(),
195205
ctrl.get().getCtrl());
196206
});
@@ -201,7 +211,10 @@ static BackendResources of(Backend backend, TestRunScopedInstance<OtelSdkShim> o
201211
String bucketName = String.format("java-storage-grpc-v-%s", UUID.randomUUID());
202212
protectedBucketNames.add(bucketName);
203213
return new BucketInfoShim(
204-
BucketInfo.newBuilder(bucketName).setVersioningEnabled(true).build(),
214+
BucketInfo.newBuilder(bucketName)
215+
.setLocation(zone.get().get().getRegion())
216+
.setVersioningEnabled(true)
217+
.build(),
205218
storageJson.get().getStorage(),
206219
ctrl.get().getCtrl());
207220
});
@@ -213,6 +226,7 @@ static BackendResources of(Backend backend, TestRunScopedInstance<OtelSdkShim> o
213226
protectedBucketNames.add(bucketName);
214227
return new BucketInfoShim(
215228
BucketInfo.newBuilder(bucketName)
229+
.setLocation(zone.get().get().getRegion())
216230
.setHierarchicalNamespace(
217231
HierarchicalNamespace.newBuilder().setEnabled(true).build())
218232
.setIamConfiguration(
@@ -241,7 +255,7 @@ static BackendResources of(Backend backend, TestRunScopedInstance<OtelSdkShim> o
241255
TestRunScopedInstance<KmsFixture> kmsFixture =
242256
TestRunScopedInstance.of(
243257
"fixture/KMS/[" + backend.name() + "]",
244-
() -> KmsFixture.of(storageJson.get().getStorage()));
258+
() -> KmsFixture.of(storageJson.get().getStorage(), zone.get().get()));
245259

246260
return new BackendResources(
247261
backend,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,12 @@ public void stop() {
9999
// KMS prevents key and ring deletion, https://cloud.google.com/kms/docs/faq#cannot_delete
100100
}
101101

102-
static KmsFixture of(Storage s) {
102+
static KmsFixture of(Storage s, Zone zone) {
103103
// KMS prevents key and ring deletion, https://cloud.google.com/kms/docs/faq#cannot_delete
104104
// therefore we instead prefer stable names to not blow out the number of keys and rings
105105
// in a project.
106-
return new KmsFixture(s, "us", "gcs_test_kms_key_ring", "gcs_kms_key_one", "gcs_kms_key_two");
106+
return new KmsFixture(
107+
s, zone.getRegion(), "gcs_test_kms_key_ring", "gcs_kms_key_one", "gcs_kms_key_two");
107108
}
108109

109110
private KeyRing resolveKeyRing(KeyManagementServiceClient kms) throws StatusRuntimeException {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.storage.it.runner.registry;
18+
19+
import com.google.api.client.http.GenericUrl;
20+
import com.google.api.client.http.HttpRequest;
21+
import com.google.api.client.http.HttpRequestFactory;
22+
import com.google.api.client.http.HttpResponse;
23+
import com.google.api.client.http.HttpResponseException;
24+
import com.google.api.client.http.javanet.NetHttpTransport;
25+
import com.google.common.base.Suppliers;
26+
import com.google.common.io.CharStreams;
27+
import java.io.IOException;
28+
import java.io.InputStream;
29+
import java.io.InputStreamReader;
30+
import java.io.Reader;
31+
import java.net.UnknownHostException;
32+
import java.util.Optional;
33+
import java.util.function.Supplier;
34+
35+
final class MetadataService {
36+
37+
private static final Supplier<HttpRequestFactory> requestFactory =
38+
Suppliers.memoize(
39+
() ->
40+
new NetHttpTransport.Builder()
41+
.build()
42+
.createRequestFactory(
43+
request -> {
44+
request.setCurlLoggingEnabled(false);
45+
request.getHeaders().set("Metadata-Flavor", "Google");
46+
}));
47+
private static final String baseUri = "http://metadata.google.internal";
48+
49+
public static void main(String[] args) throws IOException {
50+
System.out.println("zone() = " + zone());
51+
}
52+
53+
public static Optional<Zone> zone() throws IOException {
54+
return get("/computeMetadata/v1/instance/zone").map(Zone::parse);
55+
}
56+
57+
public static Optional<String> get(String path) throws IOException {
58+
GenericUrl url = new GenericUrl(baseUri + path);
59+
try {
60+
HttpRequest req = requestFactory.get().buildGetRequest(url);
61+
HttpResponse resp = req.execute();
62+
try (InputStream content = resp.getContent();
63+
Reader r = new InputStreamReader(content)) {
64+
return CharStreams.readLines(r).stream().findFirst();
65+
}
66+
} catch (HttpResponseException | UnknownHostException e) {
67+
return Optional.empty();
68+
}
69+
}
70+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public final class Registry extends RunListener {
7878
private final TestRunScopedInstance<Generator> generator =
7979
TestRunScopedInstance.of("fixture/GENERATOR", Generator::new);
8080

81+
private final TestRunScopedInstance<Zone.ZoneShim> zone =
82+
TestRunScopedInstance.of("fixture/ZONE", Zone.ZoneShim::new);
8183
final TestRunScopedInstance<OtelSdkShim> otelSdk =
8284
TestRunScopedInstance.of(
8385
"fixture/OTEL_SDK",
@@ -86,14 +88,16 @@ public final class Registry extends RunListener {
8688
return new OtelSdkShim(projectId);
8789
});
8890

89-
private final BackendResources prodBackendResources = BackendResources.of(Backend.PROD, otelSdk);
91+
private final BackendResources prodBackendResources =
92+
BackendResources.of(Backend.PROD, otelSdk, zone);
9093
private final BackendResources testBenchBackendResource =
91-
BackendResources.of(Backend.TEST_BENCH, otelSdk);
94+
BackendResources.of(Backend.TEST_BENCH, otelSdk, zone);
9295

9396
private final ImmutableList<RegistryEntry<?>> entries =
9497
new ImmutableList.Builder<RegistryEntry<?>>()
9598
.add(
9699
RegistryEntry.of(0, OpenTelemetry.class, otelSdk),
100+
RegistryEntry.of(1, Zone.class, zone),
97101
RegistryEntry.of(1, TestBench.class, testBench),
98102
RegistryEntry.of(2, Generator.class, generator),
99103
registryEntry(3, Backend.class, CrossRunIntersection::getBackend),
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.storage.it.runner.registry;
18+
19+
import com.google.common.base.MoreObjects;
20+
import java.io.IOException;
21+
import java.util.Objects;
22+
23+
public final class Zone {
24+
25+
private final String region;
26+
private final String zone;
27+
28+
private Zone(String region, String zone) {
29+
this.zone = zone;
30+
this.region = region;
31+
}
32+
33+
public String getRegion() {
34+
return region;
35+
}
36+
37+
public String getZone() {
38+
return zone;
39+
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o) {
44+
return true;
45+
}
46+
if (!(o instanceof Zone)) {
47+
return false;
48+
}
49+
Zone zone1 = (Zone) o;
50+
return Objects.equals(zone, zone1.zone) && Objects.equals(region, zone1.region);
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
return Objects.hash(zone, region);
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return MoreObjects.toStringHelper(this).add("region", region).add("zone", zone).toString();
61+
}
62+
63+
public static Zone parse(String s) {
64+
String z = s;
65+
int idx = z.lastIndexOf('/');
66+
if (idx > -1) {
67+
z = z.substring(idx + 1);
68+
}
69+
String r = "";
70+
int idx2 = z.lastIndexOf('-');
71+
if (idx2 > -1) {
72+
r = z.substring(0, idx2);
73+
}
74+
return new Zone(r, z);
75+
}
76+
77+
static final class ZoneShim implements ManagedLifecycle {
78+
79+
private Zone zone;
80+
81+
@Override
82+
public Zone get() {
83+
return zone;
84+
}
85+
86+
@Override
87+
public void start() {
88+
try {
89+
zone = MetadataService.zone().orElseGet(() -> parse("us-east1-c"));
90+
} catch (IOException e) {
91+
throw new RuntimeException(e);
92+
}
93+
}
94+
95+
@Override
96+
public void stop() {}
97+
}
98+
}

0 commit comments

Comments
 (0)