|
24 | 24 | import com.google.cloud.spanner.DatabaseAdminClient; |
25 | 25 | import com.google.cloud.spanner.DatabaseId; |
26 | 26 | import com.google.cloud.spanner.ErrorCode; |
| 27 | +import com.google.cloud.spanner.InstanceAdminClient; |
| 28 | +import com.google.cloud.spanner.InstanceConfigId; |
27 | 29 | import com.google.cloud.spanner.InstanceId; |
| 30 | +import com.google.cloud.spanner.InstanceInfo; |
28 | 31 | import com.google.cloud.spanner.Spanner; |
29 | 32 | import com.google.cloud.spanner.SpannerException; |
30 | 33 | import com.google.cloud.spanner.SpannerExceptionFactory; |
@@ -402,40 +405,51 @@ public void testSample() throws Exception { |
402 | 405 |
|
403 | 406 | out = runSample("deletebackup"); |
404 | 407 | assertThat(out).contains("Deleted backup [" + backupId + "]"); |
405 | | - |
| 408 | + } |
| 409 | + |
| 410 | + @Test |
| 411 | + public void testEncryptedDatabaseAndBackupSamples() throws Exception { |
406 | 412 | 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 | + } |
439 | 453 | } |
440 | 454 |
|
441 | 455 | private String runSampleRunnable(Runnable sample) { |
|
0 commit comments