|
| 1 | +/* |
| 2 | + * Copyright 2026 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; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.junit.Assert.assertThrows; |
| 21 | +import static org.junit.Assert.fail; |
| 22 | +import static org.junit.Assume.assumeTrue; |
| 23 | + |
| 24 | +import com.google.api.gax.rpc.ApiException; |
| 25 | +import com.google.api.gax.rpc.StatusCode; |
| 26 | +import com.google.cloud.storage.BucketInfo; |
| 27 | +import com.google.cloud.storage.BucketInfo.HierarchicalNamespace; |
| 28 | +import com.google.cloud.storage.BucketInfo.IamConfiguration; |
| 29 | +import com.google.cloud.storage.Storage; |
| 30 | +import com.google.cloud.storage.StorageOptions; |
| 31 | +import com.google.protobuf.Duration; |
| 32 | +import com.google.protobuf.FieldMask; |
| 33 | +import com.google.storage.control.v2.BucketName; |
| 34 | +import com.google.storage.control.v2.RapidCache; |
| 35 | +import com.google.storage.control.v2.StorageControlClient; |
| 36 | +import com.google.storage.control.v2.StorageControlSettings; |
| 37 | +import com.google.storage.control.v2.stub.StorageControlStubSettings; |
| 38 | +import io.grpc.StatusException; |
| 39 | +import io.grpc.StatusRuntimeException; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Locale; |
| 43 | +import java.util.UUID; |
| 44 | +import java.util.concurrent.ExecutionException; |
| 45 | +import java.util.logging.Level; |
| 46 | +import java.util.logging.Logger; |
| 47 | +import org.junit.AfterClass; |
| 48 | +import org.junit.BeforeClass; |
| 49 | +import org.junit.Ignore; |
| 50 | +import org.junit.Test; |
| 51 | + |
| 52 | +/** |
| 53 | + * Integration tests for Storage Control RapidCache CRUD operations. |
| 54 | + * |
| 55 | + * <p>RapidCache requires a regional bucket with Hierarchical Namespace (HNS) enabled. The cache ID |
| 56 | + * is required by the backend to match the zone name where it is provisioned (e.g. europe-west1-c). |
| 57 | + */ |
| 58 | +@Ignore("TODO: Enable tests once RCU is available in prod") |
| 59 | +public class ITRapidCacheTest { |
| 60 | + |
| 61 | + private static final Logger LOGGER = Logger.getLogger(ITRapidCacheTest.class.getName()); |
| 62 | + |
| 63 | + private static StorageControlClient controlClient; |
| 64 | + private static Storage storageClient; |
| 65 | + private static String bucketName; |
| 66 | + |
| 67 | + // Shared Cache Details |
| 68 | + private static String cacheId; |
| 69 | + private static String cacheName; |
| 70 | + private static RapidCache sharedCache; |
| 71 | + |
| 72 | + @BeforeClass |
| 73 | + public static void setUpClass() throws Exception { |
| 74 | + // Initialize standard Storage client for prod (gRPC) |
| 75 | + storageClient = |
| 76 | + StorageOptions.grpc() |
| 77 | + .setAttemptDirectPath(false) |
| 78 | + .setGrpcInterceptorProvider(GrpcPlainRequestLoggingInterceptor.getInterceptorProvider()) |
| 79 | + .setEnableGrpcClientMetrics(false) |
| 80 | + .build() |
| 81 | + .getService(); |
| 82 | + |
| 83 | + // Initialize StorageControl client for prod (gRPC) |
| 84 | + StorageControlSettings controlSettings = |
| 85 | + StorageControlSettings.newBuilder() |
| 86 | + .setTransportChannelProvider( |
| 87 | + StorageControlStubSettings.defaultGrpcTransportProviderBuilder() |
| 88 | + .setInterceptorProvider( |
| 89 | + GrpcPlainRequestLoggingInterceptor.getInterceptorProvider()) |
| 90 | + .build()) |
| 91 | + .build(); |
| 92 | + controlClient = StorageControlClient.create(controlSettings); |
| 93 | + |
| 94 | + // Create HNS enabled regional bucket in europe-west1 |
| 95 | + bucketName = "java-storage-rapid-" + UUID.randomUUID().toString().substring(0, 8); |
| 96 | + BucketInfo bucketInfo = |
| 97 | + BucketInfo.newBuilder(bucketName) |
| 98 | + .setLocation("europe-west1") |
| 99 | + .setHierarchicalNamespace(HierarchicalNamespace.newBuilder().setEnabled(true).build()) |
| 100 | + .setIamConfiguration( |
| 101 | + IamConfiguration.newBuilder().setIsUniformBucketLevelAccessEnabled(true).build()) |
| 102 | + .build(); |
| 103 | + storageClient.create(bucketInfo); |
| 104 | + // Wait for bucket metadata to propagate in GCS Control plane |
| 105 | + Thread.sleep(20000); |
| 106 | + |
| 107 | + // Define shared cache ID (forced to be the zone name by the backend) |
| 108 | + cacheId = "europe-west1-c"; |
| 109 | + cacheName = String.format("projects/_/buckets/%s/rapidCaches/%s", bucketName, cacheId); |
| 110 | + |
| 111 | + RapidCache rapidCache = |
| 112 | + RapidCache.newBuilder() |
| 113 | + .setName(cacheName) |
| 114 | + .setZone(cacheId) |
| 115 | + .setCacheType("rapid-cache-ultra") |
| 116 | + .setTtl(Duration.newBuilder().setSeconds(86400).build()) // 24 hours |
| 117 | + .build(); |
| 118 | + sharedCache = |
| 119 | + controlClient.createRapidCacheAsync(BucketName.format("_", bucketName), rapidCache).get(); |
| 120 | + } |
| 121 | + |
| 122 | + @AfterClass |
| 123 | + public static void tearDownClass() { |
| 124 | + if (controlClient != null && cacheName != null) { |
| 125 | + try { |
| 126 | + controlClient.disableRapidCacheAsync(cacheName).get(); |
| 127 | + } catch (Exception e) { |
| 128 | + if (isUnimplemented(e)) { |
| 129 | + LOGGER.log( |
| 130 | + Level.WARNING, |
| 131 | + "DisableRapidCache is not yet supported over gRPC by the service (UNIMPLEMENTED)." |
| 132 | + + " Skipping cleanup of cache: " |
| 133 | + + cacheName); |
| 134 | + } else { |
| 135 | + LOGGER.log(Level.WARNING, "Failed to clean up rapid cache: " + e.getMessage()); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + if (storageClient != null && bucketName != null) { |
| 140 | + try { |
| 141 | + storageClient.delete(bucketName); |
| 142 | + } catch (Exception e) { |
| 143 | + LOGGER.log( |
| 144 | + Level.WARNING, |
| 145 | + "Failed to clean up bucket (expected if cache is still active): " + e.getMessage()); |
| 146 | + } |
| 147 | + } |
| 148 | + if (controlClient != null) { |
| 149 | + try { |
| 150 | + controlClient.close(); |
| 151 | + } catch (Exception e) { |
| 152 | + LOGGER.log(Level.WARNING, "Failed to close controlClient", e); |
| 153 | + } |
| 154 | + } |
| 155 | + if (storageClient != null) { |
| 156 | + try { |
| 157 | + storageClient.close(); |
| 158 | + } catch (Exception e) { |
| 159 | + LOGGER.log(Level.WARNING, "Failed to close storageClient", e); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + // --- Independent CRUD and Negative Test Cases --- |
| 165 | + |
| 166 | + @Test |
| 167 | + public void createRapidCache() { |
| 168 | + assertThat(sharedCache).isNotNull(); |
| 169 | + assertThat(sharedCache.getName()).isEqualTo(cacheName); |
| 170 | + assertThat(sharedCache.getZone()).isEqualTo(cacheId); |
| 171 | + assertThat(sharedCache.getCacheType()).isEqualTo("rapid-cache-ultra"); |
| 172 | + assertThat(sharedCache.getState().toLowerCase(Locale.ROOT)).isAnyOf("running", "active"); |
| 173 | + assertThat(sharedCache.getTtl().getSeconds()).isEqualTo(86400); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void createDuplicateRapidCache() { |
| 178 | + RapidCache duplicateCache = |
| 179 | + RapidCache.newBuilder() |
| 180 | + .setName(cacheName) |
| 181 | + .setZone(cacheId) |
| 182 | + .setCacheType("rapid-cache-ultra") |
| 183 | + .setTtl(Duration.newBuilder().setSeconds(86400).build()) |
| 184 | + .build(); |
| 185 | + |
| 186 | + ExecutionException duplicateEx = |
| 187 | + assertThrows( |
| 188 | + ExecutionException.class, |
| 189 | + () -> |
| 190 | + controlClient |
| 191 | + .createRapidCacheAsync(BucketName.format("_", bucketName), duplicateCache) |
| 192 | + .get()); |
| 193 | + assertThat(duplicateEx.getCause()).isInstanceOf(ApiException.class); |
| 194 | + ApiException duplicateApiException = (ApiException) duplicateEx.getCause(); |
| 195 | + assertThat(duplicateApiException.getStatusCode().getCode()) |
| 196 | + .isEqualTo(StatusCode.Code.ALREADY_EXISTS); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void getRapidCache() { |
| 201 | + RapidCache retrieved = controlClient.getRapidCache(cacheName); |
| 202 | + assertThat(retrieved).isNotNull(); |
| 203 | + assertThat(retrieved.getName()).isEqualTo(cacheName); |
| 204 | + assertThat(retrieved.getZone()).isEqualTo(cacheId); |
| 205 | + assertThat(retrieved.getCacheType()).isEqualTo("rapid-cache-ultra"); |
| 206 | + assertThat(retrieved.getState().toLowerCase(Locale.ROOT)).isAnyOf("running", "active"); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + public void listRapidCaches() { |
| 211 | + StorageControlClient.ListRapidCachesPagedResponse listResponse = |
| 212 | + controlClient.listRapidCaches(BucketName.format("_", bucketName)); |
| 213 | + List<String> names = new ArrayList<>(); |
| 214 | + for (RapidCache rc : listResponse.iterateAll()) { |
| 215 | + names.add(rc.getName()); |
| 216 | + } |
| 217 | + assertThat(names).contains(cacheName); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void updateRapidCache() throws Exception { |
| 222 | + RapidCache toUpdate = |
| 223 | + RapidCache.newBuilder() |
| 224 | + .setName(cacheName) |
| 225 | + .setZone(cacheId) |
| 226 | + .setCacheType("rapid-cache-ultra") |
| 227 | + .setTtl(Duration.newBuilder().setSeconds(172800).build()) // 48h |
| 228 | + .build(); |
| 229 | + FieldMask updateMask = FieldMask.newBuilder().addPaths("ttl").build(); |
| 230 | + RapidCache updated = controlClient.updateRapidCacheAsync(toUpdate, updateMask).get(); |
| 231 | + assertThat(updated).isNotNull(); |
| 232 | + assertThat(updated.getTtl().getSeconds()).isEqualTo(172800); |
| 233 | + } |
| 234 | + |
| 235 | + @Test |
| 236 | + public void createAndDisableRapidCache() throws Exception { |
| 237 | + String isolatedBucket = "java-storage-rapid-" + UUID.randomUUID().toString().substring(0, 8); |
| 238 | + BucketInfo bucketInfo = |
| 239 | + BucketInfo.newBuilder(isolatedBucket) |
| 240 | + .setLocation("europe-west1") |
| 241 | + .setHierarchicalNamespace(HierarchicalNamespace.newBuilder().setEnabled(true).build()) |
| 242 | + .setIamConfiguration( |
| 243 | + IamConfiguration.newBuilder().setIsUniformBucketLevelAccessEnabled(true).build()) |
| 244 | + .build(); |
| 245 | + storageClient.create(bucketInfo); |
| 246 | + |
| 247 | + try { |
| 248 | + Thread.sleep(20000); |
| 249 | + String isolatedCacheName = |
| 250 | + String.format("projects/_/buckets/%s/rapidCaches/%s", isolatedBucket, cacheId); |
| 251 | + RapidCache rapidCache = |
| 252 | + RapidCache.newBuilder() |
| 253 | + .setName(isolatedCacheName) |
| 254 | + .setZone(cacheId) |
| 255 | + .setCacheType("rapid-cache-ultra") |
| 256 | + .setTtl(Duration.newBuilder().setSeconds(86400).build()) |
| 257 | + .build(); |
| 258 | + |
| 259 | + RapidCache created = |
| 260 | + controlClient |
| 261 | + .createRapidCacheAsync(BucketName.format("_", isolatedBucket), rapidCache) |
| 262 | + .get(); |
| 263 | + assertThat(created).isNotNull(); |
| 264 | + assertThat(created.getName()).isEqualTo(isolatedCacheName); |
| 265 | + |
| 266 | + try { |
| 267 | + RapidCache disabled = controlClient.disableRapidCacheAsync(isolatedCacheName).get(); |
| 268 | + assertThat(disabled).isNotNull(); |
| 269 | + assertThat(disabled.getName()) |
| 270 | + .startsWith(String.format("projects/_/buckets/%s/rapidCaches/", isolatedBucket)); |
| 271 | + assertThat(disabled.getState().toLowerCase(Locale.ROOT)) |
| 272 | + .isAnyOf("disabled", "anywhere-cache-state-unspecified"); |
| 273 | + } catch (Exception e) { |
| 274 | + if (isUnimplemented(e)) { |
| 275 | + LOGGER.warning( |
| 276 | + "DisableRapidCache is not yet supported over gRPC by the service (UNIMPLEMENTED)." |
| 277 | + + " Skipping assertion."); |
| 278 | + assumeTrue( |
| 279 | + "DisableRapidCache is not yet supported over gRPC by the service (UNIMPLEMENTED)", |
| 280 | + false); |
| 281 | + } |
| 282 | + throw e; |
| 283 | + } |
| 284 | + } finally { |
| 285 | + try { |
| 286 | + storageClient.delete(isolatedBucket); |
| 287 | + } catch (Exception e) { |
| 288 | + LOGGER.log( |
| 289 | + Level.WARNING, |
| 290 | + "Failed to clean up isolated bucket (expected if cache is still active): " |
| 291 | + + e.getMessage()); |
| 292 | + } |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + @Test |
| 297 | + public void disableRapidCache_nonExistent() throws Exception { |
| 298 | + String nonExistentCacheName = |
| 299 | + String.format("projects/_/buckets/%s/rapidCaches/non-existent-12345", bucketName); |
| 300 | + try { |
| 301 | + controlClient.disableRapidCacheAsync(nonExistentCacheName).get(); |
| 302 | + fail("Expected ExecutionException"); |
| 303 | + } catch (ExecutionException e) { |
| 304 | + if (isUnimplemented(e)) { |
| 305 | + LOGGER.warning( |
| 306 | + "DisableRapidCache is not yet supported over gRPC by the service (UNIMPLEMENTED)." |
| 307 | + + " Skipping assertion."); |
| 308 | + assumeTrue( |
| 309 | + "DisableRapidCache is not yet supported over gRPC by the service (UNIMPLEMENTED)", |
| 310 | + false); |
| 311 | + } |
| 312 | + assertThat(e.getCause()).isInstanceOf(ApiException.class); |
| 313 | + ApiException apiException = (ApiException) e.getCause(); |
| 314 | + assertThat(apiException.getStatusCode().getCode()).isEqualTo(StatusCode.Code.NOT_FOUND); |
| 315 | + } catch (Exception e) { |
| 316 | + if (isUnimplemented(e)) { |
| 317 | + LOGGER.warning( |
| 318 | + "DisableRapidCache is not yet supported over gRPC by the service (UNIMPLEMENTED)." |
| 319 | + + " Skipping assertion."); |
| 320 | + assumeTrue( |
| 321 | + "DisableRapidCache is not yet supported over gRPC by the service (UNIMPLEMENTED)", |
| 322 | + false); |
| 323 | + } |
| 324 | + throw e; |
| 325 | + } |
| 326 | + } |
| 327 | + |
| 328 | + @Test |
| 329 | + public void createRapidCache_invalidConfig() { |
| 330 | + String invalidCacheId = "invalid-cache-" + UUID.randomUUID().toString().substring(0, 8); |
| 331 | + String invalidCacheName = |
| 332 | + String.format("projects/_/buckets/%s/rapidCaches/%s", bucketName, invalidCacheId); |
| 333 | + |
| 334 | + RapidCache rapidCache = |
| 335 | + RapidCache.newBuilder() |
| 336 | + .setName(invalidCacheName) |
| 337 | + .setZone("invalid-zone") |
| 338 | + .setCacheType("rapid-cache-ultra") |
| 339 | + .build(); |
| 340 | + |
| 341 | + ExecutionException thrown = |
| 342 | + assertThrows( |
| 343 | + ExecutionException.class, |
| 344 | + () -> |
| 345 | + controlClient |
| 346 | + .createRapidCacheAsync(BucketName.format("_", bucketName), rapidCache) |
| 347 | + .get()); |
| 348 | + assertThat(thrown.getCause()).isInstanceOf(ApiException.class); |
| 349 | + ApiException apiException = (ApiException) thrown.getCause(); |
| 350 | + assertThat(apiException.getStatusCode().getCode()).isEqualTo(StatusCode.Code.INVALID_ARGUMENT); |
| 351 | + } |
| 352 | + |
| 353 | + @Test |
| 354 | + public void getRapidCache_nonExistent() { |
| 355 | + String nonExistentCacheName = |
| 356 | + String.format("projects/_/buckets/%s/rapidCaches/non-existent-cache", bucketName); |
| 357 | + |
| 358 | + ApiException thrown = |
| 359 | + assertThrows(ApiException.class, () -> controlClient.getRapidCache(nonExistentCacheName)); |
| 360 | + assertThat(thrown.getStatusCode().getCode()).isEqualTo(StatusCode.Code.NOT_FOUND); |
| 361 | + } |
| 362 | + |
| 363 | + private static boolean isUnimplemented(Throwable t) { |
| 364 | + if (t == null) { |
| 365 | + return false; |
| 366 | + } |
| 367 | + if (t instanceof ApiException) { |
| 368 | + return ((ApiException) t).getStatusCode().getCode() == StatusCode.Code.UNIMPLEMENTED; |
| 369 | + } |
| 370 | + if (t instanceof StatusRuntimeException) { |
| 371 | + return ((StatusRuntimeException) t).getStatus().getCode() |
| 372 | + == io.grpc.Status.Code.UNIMPLEMENTED; |
| 373 | + } |
| 374 | + if (t instanceof StatusException) { |
| 375 | + return ((StatusException) t).getStatus().getCode() == io.grpc.Status.Code.UNIMPLEMENTED; |
| 376 | + } |
| 377 | + if (t.getCause() != null && t.getCause() != t) { |
| 378 | + return isUnimplemented(t.getCause()); |
| 379 | + } |
| 380 | + return false; |
| 381 | + } |
| 382 | +} |
0 commit comments