File tree

1 file changed

+47
-33
lines changed

1 file changed

+47
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import com.google.cloud.spanner.DatabaseAdminClient;
2525
import com.google.cloud.spanner.DatabaseId;
2626
import com.google.cloud.spanner.ErrorCode;
27+
import com.google.cloud.spanner.InstanceAdminClient;
28+
import com.google.cloud.spanner.InstanceConfigId;
2729
import com.google.cloud.spanner.InstanceId;
30+
import com.google.cloud.spanner.InstanceInfo;
2831
import com.google.cloud.spanner.Spanner;
2932
import com.google.cloud.spanner.SpannerException;
3033
import com.google.cloud.spanner.SpannerExceptionFactory;
@@ -402,40 +405,51 @@ public void testSample() throws Exception {
402405

403406
out = runSample("deletebackup");
404407
assertThat(out).contains("Deleted backup [" + backupId + "]");
405-
408+
}
409+
410+
@Test
411+
public void testEncryptedDatabaseAndBackupSamples() throws Exception {
406412
String projectId = spanner.getOptions().getProjectId();
407-
out = SampleRunner
408-
.runSample(() -> CreateDatabaseWithEncryptionKey.createDatabaseWithEncryptionKey(
409-
dbClient,
410-
projectId,
411-
instanceId,
412-
encryptedDatabaseId,
413-
key));
414-
assertThat(out).contains(String.format(
415-
"Database projects/%s/instances/%s/databases/%s created with encryption key %s",
416-
projectId, instanceId, encryptedDatabaseId, key));
417-
418-
out = SampleRunner.runSampleWithRetry(
419-
() -> CreateBackupWithEncryptionKey.createBackupWithEncryptionKey(dbClient, projectId,
420-
instanceId, encryptedDatabaseId, encryptedBackupId, key),
421-
new ShouldRetryBackupOperation());
422-
assertThat(out).containsMatch(String.format(
423-
"Backup projects/%s/instances/%s/backups/%s of size \\d+ bytes "
424-
+ "was created at (.*) using encryption key %s",
425-
projectId, instanceId, encryptedBackupId, key));
426-
427-
out = SampleRunner.runSampleWithRetry(
428-
() -> RestoreBackupWithEncryptionKey.restoreBackupWithEncryptionKey(dbClient, projectId,
429-
instanceId, encryptedBackupId, encryptedRestoreId, key),
430-
new ShouldRetryBackupOperation());
431-
assertThat(out).contains(String.format(
432-
"Database projects/%s/instances/%s/databases/%s"
433-
+ " restored to projects/%s/instances/%s/databases/%s"
434-
+ " from backup projects/%s/instances/%s/backups/%s"
435-
+ " using encryption key %s",
436-
projectId, instanceId, encryptedDatabaseId,
437-
projectId, instanceId, encryptedRestoreId,
438-
projectId, instanceId, encryptedBackupId, key));
413+
// Create a separate instance for this test to prevent multiple parallel backup operations on
414+
// the same instance that need to wait for each other.
415+
String instanceId = String.format("encrypted-test-%s", UUID.randomUUID());
416+
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
417+
instanceAdminClient
418+
.createInstance(InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId))
419+
.setDisplayName("Encrypted Databases and Backups test instance")
420+
.setInstanceConfigId(InstanceConfigId.of(projectId, "regional-us-central1"))
421+
.setNodeCount(1).build())
422+
.get();
423+
try {
424+
String out = SampleRunner
425+
.runSample(() -> CreateDatabaseWithEncryptionKey.createDatabaseWithEncryptionKey(dbClient,
426+
projectId, instanceId, encryptedDatabaseId, key));
427+
assertThat(out).contains(String.format(
428+
"Database projects/%s/instances/%s/databases/%s created with encryption key %s",
429+
projectId, instanceId, encryptedDatabaseId, key));
430+
431+
out = SampleRunner.runSampleWithRetry(
432+
() -> CreateBackupWithEncryptionKey.createBackupWithEncryptionKey(dbClient, projectId,
433+
instanceId, encryptedDatabaseId, encryptedBackupId, key),
434+
new ShouldRetryBackupOperation());
435+
assertThat(out).containsMatch(String.format(
436+
"Backup projects/%s/instances/%s/backups/%s of size \\d+ bytes "
437+
+ "was created at (.*) using encryption key %s",
438+
projectId, instanceId, encryptedBackupId, key));
439+
440+
out = SampleRunner.runSampleWithRetry(
441+
() -> RestoreBackupWithEncryptionKey.restoreBackupWithEncryptionKey(dbClient, projectId,
442+
instanceId, encryptedBackupId, encryptedRestoreId, key),
443+
new ShouldRetryBackupOperation());
444+
assertThat(out).contains(String.format(
445+
"Database projects/%s/instances/%s/databases/%s"
446+
+ " restored to projects/%s/instances/%s/databases/%s"
447+
+ " from backup projects/%s/instances/%s/backups/%s" + " using encryption key %s",
448+
projectId, instanceId, encryptedDatabaseId, projectId, instanceId, encryptedRestoreId,
449+
projectId, instanceId, encryptedBackupId, key));
450+
} finally {
451+
instanceAdminClient.deleteInstance(instanceId);
452+
}
439453
}
440454

441455
private String runSampleRunnable(Runnable sample) {

0 commit comments

Comments
 (0)