Skip to content

Commit 0af6693

Browse files
authored
test: fix bucket cleanup logic (#1813)
Some tests have not been properly cleaning up buckets and leaking resources outside the suite. Cleanup of existing leaked buckets will be handled separately.
1 parent 92c9644 commit 0af6693

File tree

4 files changed

+70
-74
lines changed

4 files changed

+70
-74
lines changed

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITAccessTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,8 @@ public void testBucketWithBucketPolicyOnlyEnabled() throws Exception {
611611
// 4. Expect failure when attempting to list default ACLs for BucketPolicyOnly bucket
612612

613613
// TODO: temp bucket
614+
String randBucketName = generator.randomBucketName();
614615
try {
615-
String randBucketName = generator.randomBucketName();
616616
storage.create(
617617
Bucket.newBuilder(randBucketName)
618618
.setIamConfiguration(
@@ -640,6 +640,7 @@ public void testBucketWithBucketPolicyOnlyEnabled() throws Exception {
640640
// Expected: Listing legacy ACLs should fail on a BPO enabled bucket
641641
}
642642
} finally {
643+
BucketCleaner.doCleanup(randBucketName, storage);
643644
}
644645
}
645646

@@ -653,8 +654,8 @@ public void testBucketWithUniformBucketLevelAccessEnabled() throws Exception {
653654
// 4. Expect failure when attempting to list default ACLs for UniformBucketLevelAccess bucket
654655

655656
// TODO: temp bucket
657+
String randBucketName = generator.randomBucketName();
656658
try {
657-
String randBucketName = generator.randomBucketName();
658659
storage.create(
659660
Bucket.newBuilder(randBucketName)
660661
.setIamConfiguration(
@@ -681,6 +682,7 @@ public void testBucketWithUniformBucketLevelAccessEnabled() throws Exception {
681682
// Expected: Listing legacy ACLs should fail on a BPO enabled bucket
682683
}
683684
} finally {
685+
BucketCleaner.doCleanup(randBucketName, storage);
684686
}
685687
}
686688

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBucketLifecycleTest.java

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,23 @@
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.assertNotNull;
23-
import static org.junit.Assert.assertTrue;
2423

25-
import com.google.cloud.storage.Bucket;
2624
import com.google.cloud.storage.BucketInfo;
2725
import com.google.cloud.storage.BucketInfo.LifecycleRule;
2826
import com.google.cloud.storage.BucketInfo.LifecycleRule.AbortIncompleteMPUAction;
2927
import com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleAction;
3028
import com.google.cloud.storage.BucketInfo.LifecycleRule.LifecycleCondition;
29+
import com.google.cloud.storage.BucketInfo.LifecycleRule.SetStorageClassLifecycleAction;
3130
import com.google.cloud.storage.Storage;
32-
import com.google.cloud.storage.Storage.BucketField;
3331
import com.google.cloud.storage.StorageClass;
3432
import com.google.cloud.storage.TransportCompatibility.Transport;
3533
import com.google.cloud.storage.it.runner.StorageITRunner;
3634
import com.google.cloud.storage.it.runner.annotations.Backend;
3735
import com.google.cloud.storage.it.runner.annotations.CrossRun;
3836
import com.google.cloud.storage.it.runner.annotations.Inject;
3937
import com.google.cloud.storage.it.runner.registry.Generator;
40-
import com.google.cloud.storage.testing.RemoteStorageHelper;
4138
import com.google.common.collect.ImmutableList;
4239
import java.time.OffsetDateTime;
43-
import java.util.concurrent.ExecutionException;
44-
import java.util.concurrent.TimeUnit;
4540
import org.junit.Test;
4641
import org.junit.runner.RunWith;
4742

@@ -70,9 +65,9 @@ public class ITBucketLifecycleTest {
7065
@Inject public Generator generator;
7166

7267
@Test
73-
public void testGetBucketLifecycleRules() {
68+
public void testGetBucketLifecycleRules() throws Exception {
7469
String lifecycleTestBucketName = generator.randomBucketName();
75-
storage.create(
70+
BucketInfo bucketInfo =
7671
BucketInfo.newBuilder(lifecycleTestBucketName)
7772
.setLocation("us")
7873
.setLifecycleRules(
@@ -90,16 +85,13 @@ public void testGetBucketLifecycleRules() {
9085
.setCustomTimeBeforeOffsetDateTime(OffsetDateTime.now())
9186
.setDaysSinceCustomTime(30)
9287
.build())))
93-
.build());
94-
Bucket remoteBucket =
95-
storage.get(lifecycleTestBucketName, Storage.BucketGetOption.fields(BucketField.LIFECYCLE));
96-
LifecycleRule lifecycleRule = remoteBucket.getLifecycleRules().get(0);
97-
try {
98-
assertTrue(
99-
lifecycleRule
100-
.getAction()
101-
.getActionType()
102-
.equals(LifecycleRule.SetStorageClassLifecycleAction.TYPE));
88+
.build();
89+
try (TemporaryBucket tempB =
90+
TemporaryBucket.newBuilder().setBucketInfo(bucketInfo).setStorage(storage).build()) {
91+
BucketInfo remoteBucket = tempB.getBucket();
92+
LifecycleRule lifecycleRule = remoteBucket.getLifecycleRules().get(0);
93+
assertThat(lifecycleRule.getAction().getActionType())
94+
.isEqualTo(SetStorageClassLifecycleAction.TYPE);
10395
assertEquals(3, lifecycleRule.getCondition().getNumberOfNewerVersions().intValue());
10496
assertNotNull(lifecycleRule.getCondition().getCreatedBeforeOffsetDateTime());
10597
assertFalse(lifecycleRule.getCondition().getIsLive());
@@ -109,51 +101,46 @@ public void testGetBucketLifecycleRules() {
109101
assertNotNull(lifecycleRule.getCondition().getNoncurrentTimeBeforeOffsetDateTime());
110102
assertEquals(30, lifecycleRule.getCondition().getDaysSinceCustomTime().intValue());
111103
assertNotNull(lifecycleRule.getCondition().getCustomTimeBeforeOffsetDateTime());
112-
} finally {
113-
storage.delete(lifecycleTestBucketName);
114104
}
115105
}
116106

117107
@Test
118-
public void testGetBucketAbortMPULifecycle() {
108+
public void testGetBucketAbortMPULifecycle() throws Exception {
119109
String lifecycleTestBucketName = generator.randomBucketName();
120-
storage.create(
110+
BucketInfo bucketInfo =
121111
BucketInfo.newBuilder(lifecycleTestBucketName)
122112
.setLocation("us")
123113
.setLifecycleRules(
124114
ImmutableList.of(
125115
new LifecycleRule(
126116
LifecycleAction.newAbortIncompleteMPUploadAction(),
127117
LifecycleCondition.newBuilder().setAge(1).build())))
128-
.build());
129-
Bucket remoteBucket =
130-
storage.get(lifecycleTestBucketName, Storage.BucketGetOption.fields(BucketField.LIFECYCLE));
131-
LifecycleRule lifecycleRule = remoteBucket.getLifecycleRules().get(0);
132-
try {
118+
.build();
119+
try (TemporaryBucket tempB =
120+
TemporaryBucket.newBuilder().setBucketInfo(bucketInfo).setStorage(storage).build()) {
121+
BucketInfo remoteBucket = tempB.getBucket();
122+
LifecycleRule lifecycleRule = remoteBucket.getLifecycleRules().get(0);
133123
assertEquals(AbortIncompleteMPUAction.TYPE, lifecycleRule.getAction().getActionType());
134124
assertEquals(1, lifecycleRule.getCondition().getAge().intValue());
135-
} finally {
136-
storage.delete(lifecycleTestBucketName);
137125
}
138126
}
139127

140128
@Test
141-
public void testDeleteLifecycleRules() throws ExecutionException, InterruptedException {
129+
public void testDeleteLifecycleRules() throws Exception {
142130
String bucketName = generator.randomBucketName();
143-
Bucket bucket =
144-
storage.create(
145-
BucketInfo.newBuilder(bucketName)
146-
.setLocation("us")
147-
.setLifecycleRules(LIFECYCLE_RULES)
148-
.build());
149-
assertThat(bucket.getLifecycleRules()).isNotNull();
150-
assertThat(bucket.getLifecycleRules()).hasSize(2);
151-
try {
152-
Bucket updatedBucket = bucket.toBuilder().deleteLifecycleRules().build();
131+
BucketInfo bucketInfo =
132+
BucketInfo.newBuilder(bucketName)
133+
.setLocation("us")
134+
.setLifecycleRules(LIFECYCLE_RULES)
135+
.build();
136+
try (TemporaryBucket tempB =
137+
TemporaryBucket.newBuilder().setBucketInfo(bucketInfo).setStorage(storage).build()) {
138+
BucketInfo bucket = tempB.getBucket();
139+
assertThat(bucket.getLifecycleRules()).isNotNull();
140+
assertThat(bucket.getLifecycleRules()).hasSize(2);
141+
BucketInfo updatedBucket = bucket.toBuilder().deleteLifecycleRules().build();
153142
storage.update(updatedBucket);
154143
assertThat(updatedBucket.getLifecycleRules()).hasSize(0);
155-
} finally {
156-
RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
157144
}
158145
}
159146
}

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITBucketTest.java

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.google.cloud.storage.Storage;
3939
import com.google.cloud.storage.Storage.BlobField;
4040
import com.google.cloud.storage.Storage.BucketField;
41+
import com.google.cloud.storage.Storage.BucketTargetOption;
4142
import com.google.cloud.storage.TransportCompatibility.Transport;
4243
import com.google.cloud.storage.it.runner.StorageITRunner;
4344
import com.google.cloud.storage.it.runner.annotations.Backend;
@@ -130,38 +131,45 @@ public void testGetBucketAllSelectedFields() {
130131
@Test
131132
// Cannot turn on for GRPC until b/246634709 is resolved, verified locally.
132133
@CrossRun.Exclude(transports = Transport.GRPC)
133-
public void testBucketLocationType() {
134+
public void testBucketLocationType() throws Exception {
134135
String bucketName = generator.randomBucketName();
135-
Bucket bucket = storage.create(BucketInfo.newBuilder(bucketName).setLocation("us").build());
136+
BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName).setLocation("us").build();
137+
try (TemporaryBucket tempB =
138+
TemporaryBucket.newBuilder().setBucketInfo(bucketInfo).setStorage(storage).build()) {
139+
BucketInfo bucket = tempB.getBucket();
136140

137-
assertEquals("multi-region", bucket.getLocationType());
141+
assertEquals("multi-region", bucket.getLocationType());
142+
}
138143
}
139144

140145
@Test
141146
// Cannot turn on for GRPC until creation bug b/246634709 is resolved, verified locally.
142147
@CrossRun.Exclude(transports = Transport.GRPC)
143-
public void testBucketCustomPlacmentConfigDualRegion() {
148+
public void testBucketCustomPlacmentConfigDualRegion() throws Exception {
144149
String bucketName = generator.randomBucketName();
145150
List<String> locations = new ArrayList<>();
146151
locations.add("US-EAST1");
147152
locations.add("US-WEST1");
148153
CustomPlacementConfig customPlacementConfig =
149154
CustomPlacementConfig.newBuilder().setDataLocations(locations).build();
150-
Bucket bucket =
151-
storage.create(
152-
BucketInfo.newBuilder(bucketName)
153-
.setCustomPlacementConfig(customPlacementConfig)
154-
.setLocation("us")
155-
.build());
156-
assertTrue(bucket.getCustomPlacementConfig().getDataLocations().contains("US-EAST1"));
157-
assertTrue(bucket.getCustomPlacementConfig().getDataLocations().contains("US-WEST1"));
158-
assertTrue(bucket.getLocation().equalsIgnoreCase("us"));
155+
BucketInfo bucketInfo =
156+
BucketInfo.newBuilder(bucketName)
157+
.setCustomPlacementConfig(customPlacementConfig)
158+
.setLocation("us")
159+
.build();
160+
try (TemporaryBucket tempB =
161+
TemporaryBucket.newBuilder().setBucketInfo(bucketInfo).setStorage(storage).build()) {
162+
BucketInfo bucket = tempB.getBucket();
163+
assertTrue(bucket.getCustomPlacementConfig().getDataLocations().contains("US-EAST1"));
164+
assertTrue(bucket.getCustomPlacementConfig().getDataLocations().contains("US-WEST1"));
165+
assertTrue(bucket.getLocation().equalsIgnoreCase("us"));
166+
}
159167
}
160168

161169
@Test
162170
// Cannot turn on until GRPC Update logic bug is fixed b/247133805
163171
@CrossRun.Exclude(transports = Transport.GRPC)
164-
public void testBucketLogging() {
172+
public void testBucketLogging() throws Exception {
165173
String logsBucketName = generator.randomBucketName();
166174
String loggingBucketName = generator.randomBucketName();
167175

@@ -176,29 +184,28 @@ public void testBucketLogging() {
176184
.build())
177185
.build();
178186

179-
Bucket logsBucket = null;
180-
Bucket loggingBucket = null;
181-
try {
182-
logsBucket = storage.create(logsBucketInfo);
187+
try (TemporaryBucket tempLogsB =
188+
TemporaryBucket.newBuilder().setBucketInfo(logsBucketInfo).setStorage(storage).build();
189+
TemporaryBucket tempLoggingB =
190+
TemporaryBucket.newBuilder()
191+
.setBucketInfo(loggingBucketInfo)
192+
.setStorage(storage)
193+
.build(); ) {
194+
BucketInfo logsBucket = tempLogsB.getBucket();
195+
BucketInfo loggingBucket = tempLoggingB.getBucket();
183196
assertNotNull(logsBucket);
184197

185198
Policy policy = storage.getIamPolicy(logsBucketName);
186199
assertNotNull(policy);
187-
loggingBucket = storage.create(loggingBucketInfo);
188200
assertEquals(logsBucketName, loggingBucket.getLogging().getLogBucket());
189201
assertEquals("test-logs", loggingBucket.getLogging().getLogObjectPrefix());
190202

191203
// Disable bucket logging.
192-
Bucket updatedBucket = loggingBucket.toBuilder().setLogging(null).build().update();
204+
Bucket updatedBucket =
205+
storage.update(
206+
loggingBucket.toBuilder().setLogging(null).build(),
207+
BucketTargetOption.metagenerationMatch());
193208
assertNull(updatedBucket.getLogging());
194-
195-
} finally {
196-
if (logsBucket != null) {
197-
BucketCleaner.doCleanup(logsBucketName, storage);
198-
}
199-
if (loggingBucket != null) {
200-
BucketCleaner.doCleanup(loggingBucketName, storage);
201-
}
202209
}
203210
}
204211

@@ -267,7 +274,7 @@ public void testRpoConfig() {
267274

268275
assertEquals("DEFAULT", storage.get(rpoBucket).getRpo().toString());
269276
} finally {
270-
storage.delete(rpoBucket);
277+
BucketCleaner.doCleanup(rpoBucket, storage);
271278
}
272279
}
273280

google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/StorageInstance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class StorageInstance implements ManagedLifecycle {
4040
}
4141

4242
Storage getStorage() {
43-
return proxy;
43+
return storage;
4444
}
4545

4646
@Override

0 commit comments

Comments
 (0)