forked from yard-turkey/rook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ceph_bucket_notification_test.go
452 lines (403 loc) · 18.9 KB
/
ceph_bucket_notification_test.go
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
Copyright 2021 The Rook Authors. All rights reserved.
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 integration
import (
"context"
"testing"
"time"
"github.com/rook/rook/pkg/daemon/ceph/client"
rgw "github.com/rook/rook/pkg/operator/ceph/object"
"github.com/rook/rook/tests/framework/clients"
"github.com/rook/rook/tests/framework/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
httpServerName = "sample-http-server"
httpServerNameSpace = "default"
httpServerPort = "8080"
httpEndpointService = "http://" + httpServerName + "." + httpServerNameSpace + ":" + httpServerPort
putEvent = "ObjectCreated:Put"
deleteEvent = "ObjectRemoved:Delete"
)
func testBucketNotifications(s suite.Suite, helper *clients.TestClient, k8sh *utils.K8sHelper, namespace, storeName string) {
if utils.IsPlatformOpenShift() {
s.T().Skip("bucket notification tests skipped on openshift")
}
bucketNotificationLabelPrefix := "bucket-notification-"
obcNamespace := "default"
ctx := context.TODO()
clusterInfo := client.AdminTestClusterInfo(namespace)
t := s.T()
context := k8sh.MakeContext()
objectStore, err := k8sh.RookClientset.CephV1().CephObjectStores(namespace).Get(ctx, storeName, metav1.GetOptions{})
assert.Nil(t, err)
rgwcontext, err := rgw.NewMultisiteContext(context, clusterInfo, objectStore)
assert.Nil(t, err)
notificationName := "my-notification"
topicName := "my-topic"
appLabel := "app=" + httpServerName
logger.Infof("Testing Bucket Notifications on %s", storeName)
t.Run("create HTTP Endpoint for receiving notifications", func(t *testing.T) {
err := helper.TopicClient.CreateHTTPServer(httpServerName, httpServerNameSpace, httpServerPort)
assert.Nil(t, err)
})
t.Run("create CephBucketTopic", func(t *testing.T) {
err := helper.TopicClient.CreateTopic(topicName, storeName, httpEndpointService)
assert.Nil(t, err)
created := utils.Retry(12, 2*time.Second, "topic is created", func() bool {
return helper.TopicClient.CheckTopic(topicName)
})
assert.True(t, created)
logger.Info("CephBucketTopic created successfully")
})
t.Run("create CephBucketNotification", func(t *testing.T) {
err := helper.NotificationClient.CreateNotification(notificationName, topicName)
assert.Nil(t, err)
created := utils.Retry(12, 2*time.Second, "notification is created", func() bool {
return helper.NotificationClient.CheckNotificationCR(notificationName)
})
assert.True(t, created)
logger.Info("CephBucketNotification created successfully")
})
t.Run("create ObjectBucketClaim", func(t *testing.T) {
logger.Infof("create OBC %q with storageclass %q and notification %q", obcName, bucketStorageClassName, notificationName)
cobErr := helper.BucketClient.CreateBucketStorageClass(namespace, storeName, bucketStorageClassName, "Delete", storeName)
assert.Nil(t, cobErr)
cobcErr := helper.BucketClient.CreateObcNotification(obcName, bucketStorageClassName, bucketname, notificationName, true)
assert.Nil(t, cobcErr)
created := utils.Retry(12, 2*time.Second, "OBC is created", func() bool {
return helper.BucketClient.CheckOBC(obcName, "bound")
})
assert.True(t, created)
logger.Info("OBC created successfully")
var bkt rgw.ObjectBucket
i := 0
for i = 0; i < 4; i++ {
b, code, err := rgw.GetBucket(rgwcontext, bucketname)
if b != nil && err == nil {
bkt = *b
break
}
logger.Warningf("cannot get bucket %q, retrying... bucket: %v. code: %d, err: %v", bucketname, b, code, err)
logger.Infof("(%d) check bucket exists, sleeping for 5 seconds ...", i)
time.Sleep(5 * time.Second)
}
assert.NotEqual(t, 4, i)
assert.Equal(t, bucketname, bkt.Name)
logger.Info("OBC, Secret and ConfigMap created")
})
t.Run("perform s3 operations and check for notifications", func(t *testing.T) {
var s3client *rgw.S3Agent
s3endpoint, _ := helper.ObjectClient.GetEndPointUrl(namespace, storeName)
s3AccessKey, _ := helper.BucketClient.GetAccessKey(obcName)
s3SecretKey, _ := helper.BucketClient.GetSecretKey(obcName)
if objectStore.Spec.IsTLSEnabled() {
s3client, err = rgw.NewInsecureS3Agent(s3AccessKey, s3SecretKey, s3endpoint, storeName, true)
} else {
s3client, err = rgw.NewS3Agent(s3AccessKey, s3SecretKey, s3endpoint, storeName, true, nil)
}
assert.Nil(t, err)
logger.Infof("endpoint (%s) Accesskey (%s) secret (%s)", s3endpoint, s3AccessKey, s3SecretKey)
t.Run("put object", func(t *testing.T) {
_, err := s3client.PutObjectInBucket(bucketname, ObjBody, ObjectKey1, contentType)
assert.Nil(t, err)
})
t.Run("check for put bucket notification", func(t *testing.T) {
notificationReceived, err := helper.NotificationClient.CheckNotificationFromHTTPEndPoint(appLabel, putEvent, ObjectKey1)
assert.True(t, notificationReceived)
assert.Nil(t, err)
// negative test case to confirm didn't receive any delete event notification
notificationReceived, err = helper.NotificationClient.CheckNotificationFromHTTPEndPoint(appLabel, deleteEvent, ObjectKey1)
assert.False(t, notificationReceived)
assert.Nil(t, err)
})
t.Run("delete objects", func(t *testing.T) {
_, err := s3client.DeleteObjectInBucket(bucketname, ObjectKey1)
assert.Nil(t, err)
})
t.Run("check for delete bucket notification", func(t *testing.T) {
notificationReceived, err := helper.NotificationClient.CheckNotificationFromHTTPEndPoint(appLabel, deleteEvent, ObjectKey1)
assert.True(t, notificationReceived)
assert.Nil(t, err)
// negative test case to confirm didn't receive any put event notification
notificationReceived, err = helper.NotificationClient.CheckNotificationFromHTTPEndPoint(appLabel, putEvent, ObjectKey1)
assert.False(t, notificationReceived)
assert.Nil(t, err)
})
})
t.Run("check CephBucketNotification created for bucket", func(t *testing.T) {
notificationPresent := utils.Retry(12, 2*time.Second, "notification is created for bucket", func() bool {
return helper.BucketClient.CheckBucketNotificationSetonRGW(namespace, storeName, obcName, bucketname, notificationName, helper, objectStore.Spec.IsTLSEnabled())
})
assert.True(t, notificationPresent)
logger.Info("CephBucketNotification created successfully on bucket")
})
t.Run("add non-notification label to OBC", func(t *testing.T) {
obc, err := k8sh.BucketClientset.ObjectbucketV1alpha1().ObjectBucketClaims(obcNamespace).Get(ctx, obcName, metav1.GetOptions{})
assert.Nil(t, err)
obc.Labels["test-label"] = "test-value"
_, err = k8sh.BucketClientset.ObjectbucketV1alpha1().ObjectBucketClaims(obcNamespace).Update(ctx, obc, metav1.UpdateOptions{})
assert.Nil(t, err)
// check whether existing bucket notification unaffected
notificationPresent := utils.Retry(12, 2*time.Second, "notification is created for bucket", func() bool {
// TODO : add api to fetch all the notification from backend to see if it is unaffected
t.Skipped()
return helper.BucketClient.CheckBucketNotificationSetonRGW(namespace, storeName, obcName, bucketname, notificationName, helper, objectStore.Spec.IsTLSEnabled())
})
assert.True(t, notificationPresent)
})
t.Run("remove non-notification label from OBC", func(t *testing.T) {
obc, err := k8sh.BucketClientset.ObjectbucketV1alpha1().ObjectBucketClaims(obcNamespace).Get(ctx, obcName, metav1.GetOptions{})
assert.Nil(t, err)
delete(obc.Labels, "test-label")
_, err = k8sh.BucketClientset.ObjectbucketV1alpha1().ObjectBucketClaims(obcNamespace).Update(ctx, obc, metav1.UpdateOptions{})
assert.Nil(t, err)
// check whether existing bucket notification unaffected
notificationPresent := utils.Retry(12, 2*time.Second, "notification is created for bucket", func() bool {
// TODO : add api to fetch all the notification from backend to see if it is unaffected
t.Skipped()
return helper.BucketClient.CheckBucketNotificationSetonRGW(namespace, storeName, obcName, bucketname, notificationName, helper, objectStore.Spec.IsTLSEnabled())
})
assert.True(t, notificationPresent)
})
t.Run("remove notification label from OBC", func(t *testing.T) {
// TODO: add remove notification label support in OBC
t.Skipped()
obc, err := k8sh.BucketClientset.ObjectbucketV1alpha1().ObjectBucketClaims(obcNamespace).Get(ctx, obcName, metav1.GetOptions{})
assert.Nil(t, err)
delete(obc.Labels, bucketNotificationLabelPrefix+notificationName)
_, err = k8sh.BucketClientset.ObjectbucketV1alpha1().ObjectBucketClaims(obcNamespace).Update(ctx, obc, metav1.UpdateOptions{})
assert.Nil(t, err)
// check whether existing bucket notification uneffected
var notificationPresent bool
for i := 0; i < 4; i++ {
notificationPresent = helper.BucketClient.CheckBucketNotificationSetonRGW(namespace, storeName, obcName, bucketname, notificationName, helper, objectStore.Spec.IsTLSEnabled())
if !notificationPresent {
break
}
time.Sleep(5 * time.Second)
}
assert.False(t, notificationPresent)
})
t.Run("perform s3 operations and confirm notifications are no longer received", func(t *testing.T) {
var s3client *rgw.S3Agent
s3endpoint, _ := helper.ObjectClient.GetEndPointUrl(namespace, storeName)
s3AccessKey, _ := helper.BucketClient.GetAccessKey(obcName)
s3SecretKey, _ := helper.BucketClient.GetSecretKey(obcName)
if objectStore.Spec.IsTLSEnabled() {
s3client, err = rgw.NewInsecureS3Agent(s3AccessKey, s3SecretKey, s3endpoint, storeName, true)
} else {
s3client, err = rgw.NewS3Agent(s3AccessKey, s3SecretKey, s3endpoint, storeName, true, nil)
}
assert.Nil(t, err)
logger.Infof("endpoint (%s) Accesskey (%s) secret (%s)", s3endpoint, s3AccessKey, s3SecretKey)
t.Run("put object", func(t *testing.T) {
_, err := s3client.PutObjectInBucket(bucketname, ObjBody, ObjectKey2, contentType)
assert.Nil(t, err)
})
t.Run("check for put bucket notification", func(t *testing.T) {
notificationReceived, err := helper.NotificationClient.CheckNotificationFromHTTPEndPoint(appLabel, putEvent, ObjectKey2)
assert.False(t, notificationReceived)
assert.Nil(t, err)
})
t.Run("delete objects", func(t *testing.T) {
_, err := s3client.DeleteObjectInBucket(bucketname, ObjectKey1)
assert.Nil(t, err)
})
t.Run("check for delete bucket notification", func(t *testing.T) {
notificationReceived, err := helper.NotificationClient.CheckNotificationFromHTTPEndPoint(appLabel, deleteEvent, ObjectKey2)
assert.False(t, notificationReceived)
assert.Nil(t, err)
})
})
t.Run("add topic, notification to existing OBC", func(t *testing.T) {
newNotificationName := "new-notification"
newTopicName := "new-topic"
t.Run("create CephBucketTopic: new-topic", func(t *testing.T) {
err := helper.TopicClient.CreateTopic(newTopicName, storeName, httpEndpointService)
assert.Nil(t, err)
created := utils.Retry(12, 2*time.Second, "topic is created", func() bool {
return helper.TopicClient.CheckTopic(newTopicName)
})
assert.True(t, created)
})
t.Run("create CephBucketNotification: new-notification", func(t *testing.T) {
err = helper.NotificationClient.CreateNotification(newNotificationName, newTopicName)
assert.Nil(t, err)
created := utils.Retry(12, 2*time.Second, "notification is created", func() bool {
return helper.NotificationClient.CheckNotificationCR(newNotificationName)
})
assert.True(t, created)
})
t.Run("add notification label to OBC", func(t *testing.T) {
obc, err := k8sh.BucketClientset.ObjectbucketV1alpha1().ObjectBucketClaims(obcNamespace).Get(ctx, obcName, metav1.GetOptions{})
assert.Nil(t, err)
obc.Labels[bucketNotificationLabelPrefix+newNotificationName] = newNotificationName
_, err = k8sh.BucketClientset.ObjectbucketV1alpha1().ObjectBucketClaims(obcNamespace).Update(ctx, obc, metav1.UpdateOptions{})
assert.Nil(t, err)
})
t.Run("new-notification should be configured for bucket", func(t *testing.T) {
// check whether bucket notification added
notificationPresent := utils.Retry(12, 2*time.Second, "notification is created for bucket", func() bool {
return helper.BucketClient.CheckBucketNotificationSetonRGW(namespace, storeName, obcName, bucketname, newNotificationName, helper, objectStore.Spec.IsTLSEnabled())
})
assert.True(t, notificationPresent)
})
t.Run("delete CephBucketNotification: new-notification", func(t *testing.T) {
err = helper.NotificationClient.DeleteNotification(newNotificationName, topicName)
assert.Nil(t, err)
t.Run("check notification removed from backend Bucket", func(t *testing.T) {
// TODO: need to add that support
t.Skipped()
})
})
t.Run("delete CephBucketTopic: new-topic", func(t *testing.T) {
err = helper.TopicClient.DeleteTopic(newTopicName, storeName, httpEndpointService)
assert.Nil(t, err)
})
})
t.Run("reverse order of creating notification,topic and adding it to ObjectBucketClaim", func(t *testing.T) {
reverseNotificationName := "reverse-notification"
reverseTopicName := "reverse-topic"
reverseOBCName := "reverse-obc"
reverseBucketName := "reverse-bucket"
i := 0
t.Run("create ObjectBucketClaim: reverse-obc", func(t *testing.T) {
err := helper.BucketClient.CreateObcNotification(reverseOBCName, bucketStorageClassName, reverseBucketName, reverseNotificationName, true)
assert.Nil(t, err)
created := utils.Retry(12, 2*time.Second, "OBC is created", func() bool {
return helper.BucketClient.CheckOBC(reverseOBCName, "bound")
})
assert.True(t, created)
logger.Info("OBC created successfully")
var bkt rgw.ObjectBucket
for i = 0; i < 4; i++ {
b, code, err := rgw.GetBucket(rgwcontext, reverseBucketName)
if b != nil && err == nil {
bkt = *b
break
}
logger.Warningf("cannot get bucket %q, retrying... bucket: %v. code: %d, err: %v", reverseBucketName, b, code, err)
logger.Infof("(%d) check bucket exists, sleeping for 5 seconds ...", i)
time.Sleep(5 * time.Second)
}
assert.NotEqual(t, 4, i)
assert.Equal(t, reverseBucketName, bkt.Name)
})
t.Run("create CephBucketNotification: reverse-notification", func(t *testing.T) {
err = helper.NotificationClient.CreateNotification(reverseNotificationName, reverseTopicName)
assert.Nil(t, err)
created := utils.Retry(12, 2*time.Second, "notification is created", func() bool {
return helper.NotificationClient.CheckNotificationCR(reverseNotificationName)
})
assert.True(t, created)
})
t.Run("the notification should not configured for the backend bucket until topic is created", func(t *testing.T) {
// check whether bucket notification added, should fail since topic is not created
// TODO: make below check valid with help of status field in NotificationCR
t.Skipped()
})
t.Run("create CephBucketTopic: reverse-topic", func(t *testing.T) {
err = helper.TopicClient.CreateTopic(reverseTopicName, storeName, httpEndpointService)
assert.Nil(t, err)
created := utils.Retry(12, 2*time.Second, "topic is created", func() bool {
return helper.TopicClient.CheckTopic(reverseTopicName)
})
assert.True(t, created)
})
t.Run("notification should be configured after creating the topic", func(t *testing.T) {
// check whether bucket notification added, should pass since topic got created
notificationPresent := utils.Retry(12, 2*time.Second, "notification is created for bucket", func() bool {
return helper.BucketClient.CheckBucketNotificationSetonRGW(namespace, storeName, reverseOBCName, reverseBucketName, reverseNotificationName, helper, objectStore.Spec.IsTLSEnabled())
})
assert.True(t, notificationPresent)
})
t.Run("delete CephBucketNotification: reverse-notification", func(t *testing.T) {
err = helper.NotificationClient.DeleteNotification(reverseNotificationName, reverseTopicName)
assert.Nil(t, err)
t.Run("check notification removed from backend Bucket", func(t *testing.T) {
// TODO: need to add that support
t.Skipped()
})
})
t.Run("delete CephBucketTopic: reverse-topic", func(t *testing.T) {
err = helper.TopicClient.DeleteTopic(reverseTopicName, storeName, httpEndpointService)
assert.Nil(t, err)
})
t.Run("delete ObjectBucketClaim: reverse-obc", func(t *testing.T) {
err = helper.BucketClient.DeleteObc(reverseOBCName, bucketStorageClassName, reverseBucketName, maxObject, true)
assert.Nil(t, err)
logger.Info("Checking to see if the obc, secret, and cm have all been deleted")
for i = 0; i < 4 && !helper.BucketClient.CheckOBC(reverseOBCName, "deleted"); i++ {
logger.Infof("(%d) obc deleted check, sleeping for 5 seconds ...", i)
time.Sleep(5 * time.Second)
}
assert.NotEqual(t, 4, i)
logger.Info("ensure OBC bucket was deleted")
var rgwErr int
for i = 0; i < 4; i++ {
_, rgwErr, _ = rgw.GetBucket(rgwcontext, reverseBucketName)
if rgwErr == rgw.RGWErrorNotFound {
break
}
logger.Infof("(%d) check bucket deleted, sleeping for 5 seconds ...", i)
time.Sleep(5 * time.Second)
}
assert.NotEqual(t, 4, i)
assert.Equal(t, rgwErr, rgw.RGWErrorNotFound)
})
})
t.Run("delete ObjectBucketClaim", func(t *testing.T) {
i := 0
dobcErr := helper.BucketClient.DeleteObc(obcName, bucketStorageClassName, bucketname, maxObject, true)
assert.Nil(t, dobcErr)
logger.Info("Checking to see if the obc, secret, and cm have all been deleted")
for i = 0; i < 4 && !helper.BucketClient.CheckOBC(obcName, "deleted"); i++ {
logger.Infof("(%d) obc deleted check, sleeping for 5 seconds ...", i)
time.Sleep(5 * time.Second)
}
assert.NotEqual(t, 4, i)
logger.Info("ensure OBC bucket was deleted")
var rgwErr int
for i = 0; i < 4; i++ {
_, rgwErr, _ = rgw.GetBucket(rgwcontext, bucketname)
if rgwErr == rgw.RGWErrorNotFound {
break
}
logger.Infof("(%d) check bucket deleted, sleeping for 5 seconds ...", i)
time.Sleep(5 * time.Second)
}
assert.NotEqual(t, 4, i)
assert.Equal(t, rgwErr, rgw.RGWErrorNotFound)
dobErr := helper.BucketClient.DeleteBucketStorageClass(namespace, storeName, bucketStorageClassName, "Delete", storeName)
assert.Nil(t, dobErr)
})
t.Run("delete CephBucketNotification", func(t *testing.T) {
err := helper.NotificationClient.DeleteNotification(notificationName, topicName)
assert.Nil(t, err)
t.Run("check notification removed from backend Bucket", func(t *testing.T) {
// TODO: need to add that support
t.Skipped()
})
})
t.Run("delete CephBucketTopic", func(t *testing.T) {
err := helper.TopicClient.DeleteTopic(topicName, storeName, httpEndpointService)
assert.Nil(t, err)
})
t.Run("delete CephObjectStore", func(t *testing.T) {
deleteObjectStore(t, k8sh, namespace, storeName)
assertObjectStoreDeletion(t, k8sh, namespace, storeName)
})
}