diff --git a/tests/framework/clients/notification.go b/tests/framework/clients/notification.go index ec1e769ba..d7805441f 100644 --- a/tests/framework/clients/notification.go +++ b/tests/framework/clients/notification.go @@ -17,6 +17,9 @@ limitations under the License. package clients import ( + "strings" + "time" + "github.com/rook/rook/tests/framework/installer" "github.com/rook/rook/tests/framework/utils" ) @@ -56,3 +59,17 @@ func (t *NotificationOperation) CheckNotificationCR(notificationName string) boo return true } + +func (t *NotificationOperation) CheckNotificationFromHTTPEndPoint(appLabel, eventName, fileName string) (bool, error) { + // wait for the notification to reach http-server + time.Sleep(5 * time.Second) + selectorName := "--selector=" + appLabel + l, err := t.k8sh.Kubectl("logs", selectorName, "--tail", "5") + if err != nil { + return false, err + } + if strings.Contains(l, eventName) && strings.Contains(l, fileName) { + return true, nil + } + return false, nil +} diff --git a/tests/framework/clients/topic.go b/tests/framework/clients/topic.go index d91cf13f3..bc0e181a3 100644 --- a/tests/framework/clients/topic.go +++ b/tests/framework/clients/topic.go @@ -19,6 +19,7 @@ package clients import ( "github.com/rook/rook/tests/framework/installer" "github.com/rook/rook/tests/framework/utils" + kerrors "k8s.io/apimachinery/pkg/api/errors" ) // TopicOperation is a wrapper for rook topic operations @@ -62,3 +63,51 @@ func (t *TopicOperation) CheckTopic(topicName string) bool { logger.Infof("topic ARN is %q", topicARN) return true } + +func (t *TopicOperation) CreateHTTPServer(serverName, namespace, port string) error { + // TODO: Fix https://github.com/rook/rook/issues/9741, do not use third party image + deployment := ` +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: ` + serverName + ` + namespace: ` + namespace + ` +spec: + replicas: 1 + selector: + matchLabels: + app: ` + serverName + ` + template: + metadata: + labels: + app: ` + serverName + ` + spec: + containers: + - name: ` + serverName + ` + image: quay.io/jthottan/pythonwebserver:latest +--- +apiVersion: v1 +kind: Service +metadata: + name: ` + serverName + ` + namespace: ` + namespace + ` +spec: + type: NodePort + selector: + app: ` + serverName + ` + ports: + - port: ` + port + ` + targetPort: ` + port + ` +` + _, err := t.k8sh.KubectlWithStdin(deployment, []string{"apply", "-f", "-"}...) + if err != nil && !kerrors.IsAlreadyExists(err) { + return err + } + appLabel := "app=" + serverName + err = t.k8sh.WaitForLabeledPodsToRun(appLabel, namespace) + if err != nil { + return err + } + return nil +} diff --git a/tests/framework/installer/ceph_manifests.go b/tests/framework/installer/ceph_manifests.go index 7c224b074..1e81a1fce 100644 --- a/tests/framework/installer/ceph_manifests.go +++ b/tests/framework/installer/ceph_manifests.go @@ -508,7 +508,11 @@ kind: CephBucketNotification metadata: name: ` + notificationName + ` spec: - topic: ` + topicName + topic: ` + topicName + ` + events: + - s3:ObjectCreated:Put + - s3:ObjectRemoved:Delete +` } //GetBucketTopic returns the manifest to create ceph bucket topic @@ -520,7 +524,7 @@ metadata: spec: endpoint: http: - uri: http://` + httpEndpointService + ` + uri: ` + httpEndpointService + ` objectStoreName: ` + storeName + ` objectStoreNamespace: ` + m.settings.Namespace } diff --git a/tests/integration/ceph_bucket_notification_test.go b/tests/integration/ceph_bucket_notification_test.go index 21297f7b2..d0a67a8c8 100644 --- a/tests/integration/ceph_bucket_notification_test.go +++ b/tests/integration/ceph_bucket_notification_test.go @@ -30,6 +30,15 @@ import ( 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") @@ -50,9 +59,15 @@ func testBucketNotifications(s suite.Suite, helper *clients.TestClient, k8sh *ut notificationName := "my-notification" topicName := "my-topic" - httpEndpointService := "my-notification-sink" + 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) @@ -103,6 +118,52 @@ func testBucketNotifications(s suite.Suite, helper *clients.TestClient, k8sh *ut 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, region, true) + } else { + s3client, err = rgw.NewS3Agent(s3AccessKey, s3SecretKey, s3endpoint, region, 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()) @@ -161,6 +222,45 @@ func testBucketNotifications(s suite.Suite, helper *clients.TestClient, k8sh *ut 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, region, true) + } else { + s3client, err = rgw.NewS3Agent(s3AccessKey, s3SecretKey, s3endpoint, region, 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" diff --git a/tests/scripts/pythonwebserver/Dockerfile b/tests/scripts/pythonwebserver/Dockerfile new file mode 100644 index 000000000..801d4f02f --- /dev/null +++ b/tests/scripts/pythonwebserver/Dockerfile @@ -0,0 +1,4 @@ +FROM python:3 +ADD server.py / +EXPOSE 8080 +CMD [ "python", "./server.py"] diff --git a/tests/scripts/pythonwebserver/server.py b/tests/scripts/pythonwebserver/server.py new file mode 100644 index 000000000..d2b1459e3 --- /dev/null +++ b/tests/scripts/pythonwebserver/server.py @@ -0,0 +1,54 @@ +''' +Copyright 2022 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. +''' +#!/usr/bin/env python3 +""" +Very simple HTTP server in python for logging requests +Usage:: + ./server.py [] +""" +from http.server import BaseHTTPRequestHandler, HTTPServer +import logging + +class S(BaseHTTPRequestHandler): + def _set_response(self): + self.send_response(200) + self.send_header('Content-type', 'text/html') + self.end_headers() + + def do_POST(self): + content_length = int(self.headers['Content-Length']) # <--- Gets the size of data + post_data = self.rfile.read(content_length) # <--- Gets the data itself + logging.info("POST request\nBody:\n%s\n", post_data.decode('utf-8')) + +def run(server_class=HTTPServer, handler_class=S, port=8080): + logging.basicConfig(level=logging.INFO) + server_address = ('', port) + httpd = server_class(server_address, handler_class) + logging.info('Starting httpd...\n') + try: + httpd.serve_forever() + except KeyboardInterrupt: + pass + httpd.server_close() + logging.info('Stopping httpd...\n') + +if __name__ == '__main__': + from sys import argv + + if len(argv) == 2: + run(port=int(argv[1])) + else: + run()