Skip to content

Commit ad37160

Browse files
authored
test(samples): update sample tests to use BucketCleaner to cleanup buckets (#2753)
1 parent 207abd1 commit ad37160

File tree

15 files changed

+81
-75
lines changed

15 files changed

+81
-75
lines changed

samples/install-without-bom/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@
6969
<version>1.133.0</version>
7070
<scope>test</scope>
7171
</dependency>
72+
<dependency>
73+
<groupId>com.google.cloud</groupId>
74+
<artifactId>google-cloud-storage</artifactId>
75+
<version>2.43.1</version>
76+
<classifier>tests</classifier>
77+
<scope>test</scope>
78+
</dependency>
7279
</dependencies>
7380

7481
<!-- compile and run all snippet tests -->

samples/snapshot/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161
<version>1.133.0</version>
6262
<scope>test</scope>
6363
</dependency>
64+
<dependency>
65+
<groupId>com.google.cloud</groupId>
66+
<artifactId>google-cloud-storage</artifactId>
67+
<version>2.43.2-SNAPSHOT</version><!-- {x-version-update:google-cloud-storage:current} -->
68+
<classifier>tests</classifier>
69+
<scope>test</scope>
70+
</dependency>
6471
</dependencies>
6572

6673
<!-- compile and run all snippet tests -->

samples/snippets/pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@
7979
<version>1.133.0</version>
8080
<scope>test</scope>
8181
</dependency>
82-
<!-- [START storage_install_with_bom] -->
82+
<dependency>
83+
<!-- tests jars aren't in the bom, manually include the version here -->
84+
<groupId>com.google.cloud</groupId>
85+
<artifactId>google-cloud-storage</artifactId>
86+
<version>2.43.1</version>
87+
<classifier>tests</classifier>
88+
<scope>test</scope>
89+
</dependency>
90+
<!-- [START storage_install_with_bom] -->
8391
</dependencies>
8492
<!-- [END storage_install_with_bom] -->
8593
</project>

samples/snippets/src/test/java/com/example/storage/ConfigureRetriesTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.storage.BucketInfo;
2424
import com.google.cloud.storage.Storage;
2525
import com.google.cloud.storage.StorageOptions;
26+
import com.google.cloud.storage.it.BucketCleaner;
2627
import com.google.cloud.storage.testing.RemoteStorageHelper;
2728
import com.google.cloud.testing.junit4.StdOutCaptureRule;
2829
import org.junit.After;
@@ -49,11 +50,10 @@ public void setUp() {
4950
}
5051

5152
@After
52-
public void tearDown() {
53-
if (blob != null && blob.exists()) {
54-
blob.delete();
53+
public void tearDown() throws Exception {
54+
try (Storage ignore = storage) {
55+
BucketCleaner.doCleanup(bucketName, storage);
5556
}
56-
storage.delete(bucketName);
5757
}
5858

5959
@Test

samples/snippets/src/test/java/com/example/storage/ITBucketSnippets.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,14 @@
8686
import com.google.cloud.storage.Storage;
8787
import com.google.cloud.storage.Storage.BlobTargetOption;
8888
import com.google.cloud.storage.StorageRoles;
89+
import com.google.cloud.storage.it.BucketCleaner;
8990
import com.google.cloud.storage.testing.RemoteStorageHelper;
9091
import com.google.cloud.testing.junit4.StdOutCaptureRule;
9192
import com.google.common.collect.ImmutableList;
9293
import com.google.common.collect.ImmutableMap;
9394
import java.nio.file.Files;
9495
import java.nio.file.Paths;
9596
import java.util.Map;
96-
import java.util.concurrent.ExecutionException;
97-
import java.util.concurrent.TimeUnit;
98-
import java.util.logging.Level;
9997
import java.util.logging.Logger;
10098
import org.junit.After;
10199
import org.junit.AfterClass;
@@ -150,12 +148,9 @@ public static void beforeClass() {
150148
}
151149

152150
@AfterClass
153-
public static void afterClass() throws ExecutionException, InterruptedException {
154-
if (storage != null) {
155-
boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, BUCKET, 5, TimeUnit.SECONDS);
156-
if (!wasDeleted && log.isLoggable(Level.WARNING)) {
157-
log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET);
158-
}
151+
public static void afterClass() throws Exception {
152+
try (Storage ignore = storage) {
153+
BucketCleaner.doCleanup(BUCKET, storage);
159154
}
160155
}
161156

samples/snippets/src/test/java/com/example/storage/ITObjectSnippets.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
import com.google.cloud.storage.BlobInfo;
6161
import com.google.cloud.storage.BucketInfo;
6262
import com.google.cloud.storage.Storage;
63-
import com.google.cloud.storage.Storage.BlobListOption;
6463
import com.google.cloud.storage.StorageClass;
64+
import com.google.cloud.storage.it.BucketCleaner;
6565
import com.google.cloud.storage.testing.RemoteStorageHelper;
6666
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
6767
import com.google.cloud.testing.junit4.StdOutCaptureRule;
@@ -76,9 +76,6 @@
7676
import java.util.Date;
7777
import java.util.Map;
7878
import java.util.Random;
79-
import java.util.concurrent.ExecutionException;
80-
import java.util.concurrent.TimeUnit;
81-
import java.util.logging.Level;
8279
import java.util.logging.Logger;
8380
import javax.net.ssl.HttpsURLConnection;
8481
import org.junit.AfterClass;
@@ -117,15 +114,9 @@ public static void beforeClass() {
117114
}
118115

119116
@AfterClass
120-
public static void afterClass() throws ExecutionException, InterruptedException {
121-
if (storage != null) {
122-
for (BlobInfo info : storage.list(BUCKET, BlobListOption.versions(true)).getValues()) {
123-
storage.delete(info.getBlobId());
124-
}
125-
boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, BUCKET, 5, TimeUnit.SECONDS);
126-
if (!wasDeleted && log.isLoggable(Level.WARNING)) {
127-
log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET);
128-
}
117+
public static void afterClass() throws Exception {
118+
try (Storage ignore = storage) {
119+
BucketCleaner.doCleanup(BUCKET, storage);
129120
}
130121
}
131122

samples/snippets/src/test/java/com/example/storage/ITStorageSnippets.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@
2121

2222
import com.google.cloud.storage.BucketInfo;
2323
import com.google.cloud.storage.Storage;
24+
import com.google.cloud.storage.it.BucketCleaner;
2425
import com.google.cloud.storage.testing.RemoteStorageHelper;
2526
import com.google.cloud.testing.junit4.StdOutCaptureRule;
2627
import java.io.File;
2728
import java.io.FileInputStream;
2829
import java.nio.file.Files;
2930
import java.util.HashMap;
3031
import java.util.Map;
31-
import java.util.concurrent.ExecutionException;
32-
import java.util.concurrent.TimeUnit;
33-
import java.util.logging.Level;
3432
import java.util.logging.Logger;
3533
import org.apache.http.client.HttpClient;
3634
import org.apache.http.client.methods.HttpPost;
@@ -65,12 +63,9 @@ public static void beforeClass() {
6563
}
6664

6765
@AfterClass
68-
public static void afterClass() throws ExecutionException, InterruptedException {
69-
if (storage != null) {
70-
boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, BUCKET, 1, TimeUnit.MINUTES);
71-
if (!wasDeleted && log.isLoggable(Level.WARNING)) {
72-
log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET);
73-
}
66+
public static void afterClass() throws Exception {
67+
try (Storage ignore = storage) {
68+
BucketCleaner.doCleanup(BUCKET, storage);
7469
}
7570
}
7671

samples/snippets/src/test/java/com/example/storage/QuickstartSampleIT.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
import com.google.cloud.storage.BucketInfo;
2222
import com.google.cloud.storage.Storage;
2323
import com.google.cloud.storage.StorageOptions;
24+
import com.google.cloud.storage.it.BucketCleaner;
2425
import com.google.cloud.testing.junit4.StdOutCaptureRule;
2526
import com.google.storage.control.v2.StorageLayoutName;
2627
import java.util.UUID;
2728
import org.junit.After;
2829
import org.junit.Before;
29-
import org.junit.Ignore;
3030
import org.junit.Rule;
3131
import org.junit.Test;
3232
import org.junit.runner.RunWith;
@@ -41,19 +41,16 @@ public class QuickstartSampleIT {
4141

4242
private String bucketName;
4343

44-
private static void deleteBucket(String bucketName) {
45-
Storage storage = StorageOptions.getDefaultInstance().getService();
46-
storage.delete(bucketName);
47-
}
48-
4944
@Before
5045
public void setUp() {
5146
bucketName = "java-storage-grpc-" + UUID.randomUUID();
5247
}
5348

5449
@After
55-
public void tearDown() {
56-
deleteBucket(bucketName);
50+
public void tearDown() throws Exception {
51+
try (Storage storage = StorageOptions.getDefaultInstance().getService()) {
52+
BucketCleaner.doCleanup(bucketName, storage);
53+
}
5754
}
5855

5956
@Test

samples/snippets/src/test/java/com/example/storage/TestBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.cloud.storage.BucketInfo;
2323
import com.google.cloud.storage.Storage;
2424
import com.google.cloud.storage.StorageOptions;
25+
import com.google.cloud.storage.it.BucketCleaner;
2526
import com.google.cloud.storage.testing.RemoteStorageHelper;
2627
import com.google.cloud.testing.junit4.StdOutCaptureRule;
2728
import org.junit.After;
@@ -49,6 +50,6 @@ public void setUp() {
4950

5051
@After
5152
public void tearDown() {
52-
RemoteStorageHelper.forceDelete(storage, bucketName);
53+
BucketCleaner.doCleanup(bucketName, storage);
5354
}
5455
}

samples/snippets/src/test/java/com/example/storage/control/v2/FoldersTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.cloud.storage.BucketInfo.IamConfiguration;
2626
import com.google.cloud.storage.Storage;
2727
import com.google.cloud.storage.StorageOptions;
28+
import com.google.cloud.storage.it.BucketCleaner;
2829
import com.google.cloud.storage.testing.RemoteStorageHelper;
2930
import com.google.cloud.testing.junit4.StdOutCaptureRule;
3031
import com.google.storage.control.v2.BucketName;
@@ -67,10 +68,9 @@ public void setUp() throws Exception {
6768
@After
6869
public void tearDown() throws Exception {
6970
// Use try-with-resource to handle the dance closing multiple things
70-
//noinspection EmptyTryBlock
7171
try (AutoCloseable ignore1 = storage;
7272
AutoCloseable ignore2 = storageControl) {
73-
// ignore
73+
BucketCleaner.doCleanup(bucket.getName(), storage, storageControl);
7474
}
7575
}
7676

0 commit comments

Comments
 (0)