github.com/argoproj/argo-events@v1.9.1/eventsources/sources/storagegrid/start_test.go (about) 1 /* 2 Copyright 2018 BlackRock, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package storagegrid 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "io" 23 "net/http" 24 "testing" 25 26 "github.com/argoproj/argo-events/eventsources/common/webhook" 27 "github.com/argoproj/argo-events/pkg/apis/events" 28 "github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1" 29 "github.com/ghodss/yaml" 30 "github.com/smartystreets/goconvey/convey" 31 ) 32 33 var ( 34 notification = ` 35 { 36 "Action": "Publish", 37 "Message": { 38 "Records": [ 39 { 40 "eventName": "ObjectCreated:Put", 41 "storageGridEventSource": "sgws:s3", 42 "eventTime": "2019-02-27T21:15:09Z", 43 "eventVersion": "2.0", 44 "requestParameters": { 45 "sourceIPAddress": "1.1.1.1" 46 }, 47 "responseElements": { 48 "x-amz-request-id": "12345678" 49 }, 50 "s3": { 51 "bucket": { 52 "arn": "urn:sgfs:s3:::my_bucket", 53 "name": "my_bucket", 54 "ownerIdentity": { 55 "principalId": "55555555555555555" 56 } 57 }, 58 "configurationId": "Object-Event", 59 "object": { 60 "eTag": "4444444444444444", 61 "key": "hello-world.txt", 62 "sequencer": "AAAAAA", 63 "size": 6 64 }, 65 "s3SchemaVersion": "1.0" 66 }, 67 "userIdentity": { 68 "principalId": "1111111111111111" 69 } 70 } 71 ] 72 }, 73 "TopicArn": "urn:h:sns:us-east::my_topic_1", 74 "Version": "2010-03-31" 75 } 76 ` 77 router = &Router{ 78 route: webhook.GetFakeRoute(), 79 } 80 ) 81 82 func TestRouteActiveHandler(t *testing.T) { 83 convey.Convey("Given a route configuration", t, func() { 84 storageGridEventSource := &v1alpha1.StorageGridEventSource{ 85 Webhook: &v1alpha1.WebhookContext{ 86 Endpoint: "/", 87 URL: "testurl", 88 Port: "8080", 89 }, 90 Events: []string{ 91 "ObjectCreated:Put", 92 }, 93 Filter: &v1alpha1.StorageGridFilter{ 94 Prefix: "hello-", 95 Suffix: ".txt", 96 }, 97 } 98 99 writer := &webhook.FakeHttpWriter{} 100 101 convey.Convey("Inactive route should return error", func() { 102 pbytes, err := yaml.Marshal(storageGridEventSource) 103 convey.So(err, convey.ShouldBeNil) 104 router.HandleRoute(writer, &http.Request{ 105 Body: io.NopCloser(bytes.NewReader(pbytes)), 106 }) 107 convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusBadRequest) 108 }) 109 110 convey.Convey("Active route should return success", func() { 111 router.route.Active = true 112 router.storageGridEventSource = storageGridEventSource 113 dataCh := make(chan []byte) 114 go func() { 115 resp := <-router.route.DataCh 116 dataCh <- resp 117 }() 118 119 router.HandleRoute(writer, &http.Request{ 120 Body: io.NopCloser(bytes.NewReader([]byte(notification))), 121 }) 122 convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusOK) 123 }) 124 }) 125 } 126 127 func TestGenerateUUID(t *testing.T) { 128 convey.Convey("Make sure generated UUIDs are unique", t, func() { 129 u1 := generateUUID() 130 u2 := generateUUID() 131 convey.So(u1.String(), convey.ShouldNotEqual, u2.String()) 132 }) 133 } 134 135 func TestFilterName(t *testing.T) { 136 convey.Convey("Given a storage grid event, test whether the object key passes the filter", t, func() { 137 storageGridEventSource := &v1alpha1.StorageGridEventSource{ 138 Webhook: &v1alpha1.WebhookContext{ 139 Endpoint: "/", 140 URL: "testurl", 141 Port: "8080", 142 }, 143 Events: []string{ 144 "ObjectCreated:Put", 145 }, 146 Filter: &v1alpha1.StorageGridFilter{ 147 Prefix: "hello-", 148 Suffix: ".txt", 149 }, 150 } 151 var gridNotification *events.StorageGridNotification 152 err := json.Unmarshal([]byte(notification), &gridNotification) 153 convey.So(err, convey.ShouldBeNil) 154 convey.So(gridNotification, convey.ShouldNotBeNil) 155 156 ok := filterName(gridNotification, storageGridEventSource) 157 convey.So(ok, convey.ShouldEqual, true) 158 }) 159 }