Skip to content

Commit 12b09f6

Browse files
authored
test: Update KMS ITs to use GAPICs instead of gRPC stub (#11781)
Discovered in googleapis/sdk-platform-java#3942, it looks like KMS ITs are using gRPC generated stubs instead of the GAPIC stubs. While this IT may not actually have that much use to simply test the GET/ CREATE/ LIST functionality of a service, we do use KMS in sdk-platform-java as a downstream test case.
1 parent fc9dbb8 commit 12b09f6

File tree

1 file changed

+42
-58
lines changed
  • java-kms/google-cloud-kms/src/test/java/com/google/cloud/kms/it

1 file changed

+42
-58
lines changed

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

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,81 +16,65 @@
1616

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

19-
import com.google.auth.oauth2.GoogleCredentials;
20-
import com.google.cloud.ServiceOptions;
19+
import static org.junit.Assert.assertEquals;
20+
2121
import com.google.cloud.kms.v1.CreateKeyRingRequest;
2222
import com.google.cloud.kms.v1.GetKeyRingRequest;
23-
import com.google.cloud.kms.v1.KeyManagementServiceGrpc;
23+
import com.google.cloud.kms.v1.KeyManagementServiceClient;
2424
import com.google.cloud.kms.v1.KeyRing;
2525
import com.google.cloud.kms.v1.KeyRingName;
2626
import com.google.cloud.kms.v1.LocationName;
27-
import io.grpc.ManagedChannel;
28-
import io.grpc.ManagedChannelBuilder;
29-
import io.grpc.Metadata;
30-
import io.grpc.Status;
31-
import io.grpc.StatusRuntimeException;
32-
import io.grpc.auth.MoreCallCredentials;
33-
import io.grpc.stub.MetadataUtils;
3427
import java.io.IOException;
35-
import org.junit.Assert;
28+
import java.util.concurrent.TimeUnit;
29+
import org.junit.After;
3630
import org.junit.Before;
3731
import org.junit.Test;
3832

3933
public class ITKmsTest {
34+
35+
private KeyManagementServiceClient kmsClient;
36+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4037
private static final String KMS_KEY_RING_LOCATION = "us";
41-
private static final String KMS_KEY_RING_NAME = "gcs_test_kms_key_ring";
42-
private static Metadata requestParamsHeader = new Metadata();
43-
private static Metadata.Key<String> requestParamsKey =
44-
Metadata.Key.of("x-goog-request-params", Metadata.ASCII_STRING_MARSHALLER);
45-
private static KeyManagementServiceGrpc.KeyManagementServiceBlockingStub kmsStub;
38+
private static final String KMS_KEY_RING_ID = "gcs_test_kms_key_ring";
39+
private static final String KMS_KEY_RING_NAME =
40+
KeyRingName.of(PROJECT_ID, KMS_KEY_RING_LOCATION, KMS_KEY_RING_ID).toString();
4641

4742
@Before
4843
public void setUp() throws IOException {
49-
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
50-
ManagedChannel kmsChannel =
51-
ManagedChannelBuilder.forTarget("cloudkms.googleapis.com:443").build();
52-
kmsStub =
53-
KeyManagementServiceGrpc.newBlockingStub(kmsChannel)
54-
.withCallCredentials(MoreCallCredentials.from(credentials));
44+
kmsClient = KeyManagementServiceClient.create();
45+
46+
String parent = LocationName.of(PROJECT_ID, KMS_KEY_RING_LOCATION).toString();
47+
KeyManagementServiceClient.ListKeyRingsPagedResponse listKeyRingsPagedResponse =
48+
kmsClient.listKeyRings(parent);
49+
boolean foundTestKeyRing = false;
50+
for (KeyRing keyRing : listKeyRingsPagedResponse.iterateAll()) {
51+
// keyRing.getName() returns the fully qualified name and not the ID
52+
if (KMS_KEY_RING_NAME.equals(keyRing.getName())) {
53+
foundTestKeyRing = true;
54+
}
55+
}
56+
// Only create the test key ring if it doesn't exist
57+
if (!foundTestKeyRing) {
58+
kmsClient.createKeyRing(
59+
CreateKeyRingRequest.newBuilder()
60+
.setParent(parent)
61+
.setKeyRingId(KMS_KEY_RING_ID)
62+
.setKeyRing(KeyRing.newBuilder().build())
63+
.build());
64+
}
5565
}
5666

57-
@Test
58-
public void ensureKmsKeyRingExists() {
59-
String projectId = ServiceOptions.getDefaultProjectId();
60-
KeyRing keyRing = getKeyRing(kmsStub, projectId);
61-
Assert.assertNotNull(keyRing);
67+
@After
68+
public void cleanUp() throws InterruptedException {
69+
kmsClient.close();
70+
kmsClient.awaitTermination(10, TimeUnit.SECONDS);
6271
}
6372

64-
private static KeyRing getKeyRing(
65-
KeyManagementServiceGrpc.KeyManagementServiceBlockingStub kmsStub, String projectId)
66-
throws StatusRuntimeException {
67-
String kmsKeyRingResourcePath =
68-
KeyRingName.of(projectId, ITKmsTest.KMS_KEY_RING_LOCATION, ITKmsTest.KMS_KEY_RING_NAME)
69-
.toString();
70-
try {
71-
GetKeyRingRequest getKeyRingRequest =
72-
GetKeyRingRequest.newBuilder().setName(kmsKeyRingResourcePath).build();
73-
requestParamsHeader.put(requestParamsKey, "name=" + kmsKeyRingResourcePath);
74-
KeyManagementServiceGrpc.KeyManagementServiceBlockingStub stubForGetKeyRing =
75-
kmsStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(requestParamsHeader));
76-
return stubForGetKeyRing.getKeyRing(getKeyRingRequest);
77-
} catch (StatusRuntimeException ex) {
78-
if (ex.getStatus().getCode() == Status.Code.NOT_FOUND) {
79-
String keyRingParent =
80-
LocationName.of(projectId, ITKmsTest.KMS_KEY_RING_LOCATION).toString();
81-
CreateKeyRingRequest createKeyRingRequest =
82-
CreateKeyRingRequest.newBuilder()
83-
.setParent(keyRingParent)
84-
.setKeyRingId(ITKmsTest.KMS_KEY_RING_NAME)
85-
.build();
86-
requestParamsHeader.put(requestParamsKey, "parent=" + keyRingParent);
87-
KeyManagementServiceGrpc.KeyManagementServiceBlockingStub stubForCreateKeyRing =
88-
kmsStub.withInterceptors(
89-
MetadataUtils.newAttachHeadersInterceptor(requestParamsHeader));
90-
return stubForCreateKeyRing.createKeyRing(createKeyRingRequest);
91-
} else {
92-
throw ex;
93-
}
94-
}
73+
@Test
74+
public void getKeyRing() {
75+
GetKeyRingRequest getKeyRingRequest =
76+
GetKeyRingRequest.newBuilder().setName(KMS_KEY_RING_NAME).build();
77+
KeyRing keyRing = kmsClient.getKeyRing(getKeyRingRequest);
78+
assertEquals(KMS_KEY_RING_NAME, keyRing.getName());
9579
}
9680
}

0 commit comments

Comments
 (0)