forked from adobe/S3Mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BucketV2IT.kt
247 lines (216 loc) · 9.4 KB
/
BucketV2IT.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Copyright 2017-2024 Adobe.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.adobe.testing.s3mock.its
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.assertj.core.api.InstanceOfAssertFactories
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInfo
import software.amazon.awssdk.awscore.exception.AwsErrorDetails
import software.amazon.awssdk.awscore.exception.AwsServiceException
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.model.AbortIncompleteMultipartUpload
import software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration
import software.amazon.awssdk.services.s3.model.CreateBucketRequest
import software.amazon.awssdk.services.s3.model.DeleteBucketLifecycleRequest
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest
import software.amazon.awssdk.services.s3.model.ExpirationStatus
import software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationRequest
import software.amazon.awssdk.services.s3.model.GetBucketLocationRequest
import software.amazon.awssdk.services.s3.model.HeadBucketRequest
import software.amazon.awssdk.services.s3.model.LifecycleExpiration
import software.amazon.awssdk.services.s3.model.LifecycleRule
import software.amazon.awssdk.services.s3.model.LifecycleRuleFilter
import software.amazon.awssdk.services.s3.model.NoSuchBucketException
import software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationRequest
import java.util.concurrent.TimeUnit
/**
* Test the application using the AmazonS3 SDK V2.
*/
internal class BucketV2IT : S3TestBase() {
private val s3ClientV2: S3Client = createS3ClientV2()
@Test
@S3VerifiedSuccess(year = 2024)
fun createAndDeleteBucket(testInfo: TestInfo) {
val bucketName = bucketName(testInfo)
s3ClientV2.createBucket(CreateBucketRequest.builder().bucket(bucketName).build())
val bucketCreated = s3ClientV2.waiter()
.waitUntilBucketExists(HeadBucketRequest.builder().bucket(bucketName).build())
val bucketCreatedResponse = bucketCreated.matched().response().get()
assertThat(bucketCreatedResponse).isNotNull
//does not throw exception if bucket exists.
s3ClientV2.headBucket(HeadBucketRequest.builder().bucket(bucketName).build())
s3ClientV2.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build())
val bucketDeleted = s3ClientV2.waiter()
.waitUntilBucketNotExists(HeadBucketRequest.builder().bucket(bucketName).build())
bucketDeleted.matched().exception().get().also {
assertThat(it).isNotNull
assertThat(it).isInstanceOf(NoSuchBucketException::class.java)
}
}
@Test
@S3VerifiedSuccess(year = 2024)
fun getBucketLocation(testInfo: TestInfo) {
val bucketName = givenBucketV2(testInfo)
val bucketLocation = s3ClientV2.getBucketLocation(GetBucketLocationRequest.builder().bucket(bucketName).build())
assertThat(bucketLocation.locationConstraint().toString()).isEqualTo("eu-west-1")
}
@Test
@S3VerifiedSuccess(year = 2024)
fun duplicateBucketCreation(testInfo: TestInfo) {
val bucketName = bucketName(testInfo)
s3ClientV2.createBucket(CreateBucketRequest.builder().bucket(bucketName).build())
val bucketCreated = s3ClientV2.waiter()
.waitUntilBucketExists(HeadBucketRequest.builder().bucket(bucketName).build())
bucketCreated.matched().response().get().also {
assertThat(it).isNotNull
}
assertThatThrownBy {
s3ClientV2.createBucket(CreateBucketRequest.builder().bucket(bucketName).build())
}
.isInstanceOf(AwsServiceException::class.java)
.hasMessageContaining("Service: S3, Status Code: 409")
.asInstanceOf(InstanceOfAssertFactories.type(AwsServiceException::class.java))
.extracting(AwsServiceException::awsErrorDetails)
.extracting(AwsErrorDetails::errorCode)
.isEqualTo("BucketAlreadyOwnedByYou")
s3ClientV2.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build())
val bucketDeleted = s3ClientV2.waiter()
.waitUntilBucketNotExists(HeadBucketRequest.builder().bucket(bucketName).build())
bucketDeleted.matched().exception().get().also {
assertThat(it).isNotNull
assertThat(it).isInstanceOf(NoSuchBucketException::class.java)
}
}
@Test
@S3VerifiedSuccess(year = 2024)
fun duplicateBucketDeletion(testInfo: TestInfo) {
val bucketName = bucketName(testInfo)
s3ClientV2.createBucket(CreateBucketRequest.builder().bucket(bucketName).build())
val bucketCreated = s3ClientV2.waiter()
.waitUntilBucketExists(HeadBucketRequest.builder().bucket(bucketName).build())
bucketCreated.matched().response().get().also {
assertThat(it).isNotNull
}
s3ClientV2.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build())
val bucketDeleted = s3ClientV2.waiter()
.waitUntilBucketNotExists(HeadBucketRequest.builder().bucket(bucketName).build())
bucketDeleted.matched().exception().get().also {
assertThat(it).isNotNull
assertThat(it).isInstanceOf(NoSuchBucketException::class.java)
}
assertThatThrownBy {
s3ClientV2.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build())
}
.isInstanceOf(AwsServiceException::class.java)
.hasMessageContaining("Service: S3, Status Code: 404")
.asInstanceOf(InstanceOfAssertFactories.type(AwsServiceException::class.java))
.extracting(AwsServiceException::awsErrorDetails)
.extracting(AwsErrorDetails::errorCode)
.isEqualTo("NoSuchBucket")
}
@Test
@S3VerifiedSuccess(year = 2024)
fun getBucketLifecycle_notFound(testInfo: TestInfo) {
val bucketName = bucketName(testInfo)
s3ClientV2.createBucket(CreateBucketRequest.builder().bucket(bucketName).build())
val bucketCreated = s3ClientV2.waiter()
.waitUntilBucketExists(HeadBucketRequest.builder().bucket(bucketName).build())
val bucketCreatedResponse = bucketCreated.matched().response()!!.get()
assertThat(bucketCreatedResponse).isNotNull
assertThatThrownBy {
s3ClientV2.getBucketLifecycleConfiguration(
GetBucketLifecycleConfigurationRequest.builder().bucket(bucketName).build()
)
}
.isInstanceOf(AwsServiceException::class.java)
.hasMessageContaining("Service: S3, Status Code: 404")
.asInstanceOf(InstanceOfAssertFactories.type(AwsServiceException::class.java))
.extracting(AwsServiceException::awsErrorDetails)
.extracting(AwsErrorDetails::errorCode)
.isEqualTo("NoSuchLifecycleConfiguration")
}
@Test
@S3VerifiedSuccess(year = 2024)
fun putGetDeleteBucketLifecycle(testInfo: TestInfo) {
val bucketName = bucketName(testInfo)
s3ClientV2.createBucket(CreateBucketRequest.builder().bucket(bucketName).build())
val bucketCreated = s3ClientV2.waiter()
.waitUntilBucketExists(HeadBucketRequest.builder().bucket(bucketName).build())
bucketCreated.matched().response()!!.get().also {
assertThat(it).isNotNull
}
val configuration = BucketLifecycleConfiguration
.builder()
.rules(
LifecycleRule
.builder()
.id(bucketName)
.abortIncompleteMultipartUpload(
AbortIncompleteMultipartUpload
.builder()
.daysAfterInitiation(2)
.build()
)
.expiration(
LifecycleExpiration
.builder()
.days(2)
.build()
)
.filter(LifecycleRuleFilter.fromPrefix("myprefix/"))
.status(ExpirationStatus.ENABLED)
.build()
)
.build()
s3ClientV2.putBucketLifecycleConfiguration(
PutBucketLifecycleConfigurationRequest
.builder()
.bucket(bucketName)
.lifecycleConfiguration(
configuration
)
.build()
)
s3ClientV2.getBucketLifecycleConfiguration(
GetBucketLifecycleConfigurationRequest
.builder()
.bucket(bucketName)
.build()
).also {
assertThat(it.rules()[0]).isEqualTo(configuration.rules()[0])
}
s3ClientV2.deleteBucketLifecycle(
DeleteBucketLifecycleRequest.builder().bucket(bucketName).build()
).also {
assertThat(it.sdkHttpResponse().statusCode()).isEqualTo(204)
}
// give AWS time to actually delete the lifecycleConfiguration, otherwise the following call
// will not fail as expected...
TimeUnit.SECONDS.sleep(3)
assertThatThrownBy {
s3ClientV2.getBucketLifecycleConfiguration(
GetBucketLifecycleConfigurationRequest.builder().bucket(bucketName).build()
)
}
.isInstanceOf(AwsServiceException::class.java)
.hasMessageContaining("Service: S3, Status Code: 404")
.asInstanceOf(InstanceOfAssertFactories.type(AwsServiceException::class.java))
.extracting(AwsServiceException::awsErrorDetails)
.extracting(AwsErrorDetails::errorCode)
.isEqualTo("NoSuchLifecycleConfiguration")
}
}