Skip to content

Commit 837f9a7

Browse files
gauravpurohit06gcf-owl-bot[bot]rajatbhatta
authored
test: Deleting stale encrypted instances (#1986)
test: Deleting encrypted instances that were created to run the tests but not deleted due to system interrupts or timeout Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Rajat Bhatta <93644539+rajatbhatta@users.noreply.github.com>
1 parent e75d6e7 commit 837f9a7

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

samples/snippets/src/test/java/com/example/spanner/SpannerSampleIT.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import com.google.cloud.spanner.DatabaseAdminClient;
2727
import com.google.cloud.spanner.DatabaseId;
2828
import com.google.cloud.spanner.ErrorCode;
29+
import com.google.cloud.spanner.Instance;
2930
import com.google.cloud.spanner.InstanceAdminClient;
3031
import com.google.cloud.spanner.InstanceConfigId;
3132
import com.google.cloud.spanner.InstanceId;
3233
import com.google.cloud.spanner.InstanceInfo;
34+
import com.google.cloud.spanner.Options;
3335
import com.google.cloud.spanner.Spanner;
3436
import com.google.cloud.spanner.SpannerException;
3537
import com.google.cloud.spanner.SpannerExceptionFactory;
@@ -68,6 +70,8 @@ public class SpannerSampleIT {
6870
private static final String encryptedDatabaseId = formatForTest(baseDbId);
6971
private static final String encryptedBackupId = formatForTest(baseDbId);
7072
private static final String encryptedRestoreId = formatForTest(baseDbId);
73+
private static final long STALE_INSTANCE_THRESHOLD_SECS =
74+
TimeUnit.SECONDS.convert(24L, TimeUnit.HOURS);
7175
static Spanner spanner;
7276
static DatabaseId dbId;
7377
static DatabaseAdminClient dbClient;
@@ -93,10 +97,41 @@ public static void setUp() throws Exception {
9397
dbId = DatabaseId.of(options.getProjectId(), instanceId, databaseId);
9498
// Delete stale test databases that have been created earlier by this test, but not deleted.
9599
deleteStaleTestDatabases(instanceId, baseDbId);
96-
key = String.format("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
97-
options.getProjectId(), keyLocation, keyRing, keyName);
100+
key =
101+
String.format(
102+
"projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s",
103+
options.getProjectId(), keyLocation, keyRing, keyName);
104+
105+
/*
106+
* Delete stale instances that have been created earlier by this test but not deleted.
107+
* Backups needed to be deleted from the instance first, as the instance can only be
108+
* deleted once all backups have been deleted.
109+
* */
110+
deleteStaleEncryptedTestInstances();
98111
}
99-
112+
113+
/**
114+
* Deleting all the test instances with name starting with 'encrypted-test-' and were created
115+
* before 24 hours.
116+
*
117+
* @throws InterruptedException If Thread.sleep() interrupted
118+
*/
119+
private static void deleteStaleEncryptedTestInstances() throws InterruptedException {
120+
Timestamp now = Timestamp.now();
121+
122+
for (Instance instance :
123+
spanner
124+
.getInstanceAdminClient()
125+
.listInstances(Options.filter("name:encrypted-test-"))
126+
.iterateAll()) {
127+
if ((now.getSeconds() - instance.getCreateTime().getSeconds())
128+
> STALE_INSTANCE_THRESHOLD_SECS) {
129+
deleteAllBackups(instanceId);
130+
instance.delete();
131+
}
132+
}
133+
}
134+
100135
static void deleteStaleTestDatabases(String instanceId, String baseDbId) {
101136
Timestamp now = Timestamp.now();
102137
Pattern samplePattern = getTestDbIdPattern(baseDbId);
@@ -410,7 +445,7 @@ public void testSample() throws Exception {
410445
out = runSample("deletebackup");
411446
assertThat(out).contains("Deleted backup [" + backupId + "]");
412447
}
413-
448+
414449
@Test
415450
public void testEncryptedDatabaseAndBackupSamples() throws Exception {
416451
String projectId = spanner.getOptions().getProjectId();
@@ -459,7 +494,7 @@ public void testEncryptedDatabaseAndBackupSamples() throws Exception {
459494
}
460495
}
461496

462-
private void deleteAllBackups(String instanceId) throws InterruptedException {
497+
private static void deleteAllBackups(String instanceId) throws InterruptedException {
463498
for (Backup backup : dbClient.listBackups(instanceId).iterateAll()) {
464499
int attempts = 0;
465500
while (attempts < 30) {
@@ -513,7 +548,7 @@ public void testCreateInstanceSample() {
513548
private static int countOccurrences(String input, String search) {
514549
return input.split(search).length - 1;
515550
}
516-
551+
517552
private static String toComparableId(String baseId, String existingId) {
518553
String zeroUuid = "00000000-0000-0000-0000-0000-00000000";
519554
int shouldBeLength = (baseId + "-" + zeroUuid).length();
@@ -526,7 +561,7 @@ private static Pattern getTestDbIdPattern(String baseDbId) {
526561
baseDbId + "-[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{8}",
527562
Pattern.CASE_INSENSITIVE);
528563
}
529-
564+
530565
static String formatForTest(String name) {
531566
return name + "-" + UUID.randomUUID().toString().substring(0, DBID_LENGTH);
532567
}

0 commit comments

Comments
 (0)