1717package restapi
1818
1919import (
20+ "context"
2021 "fmt"
2122 "testing"
2223
@@ -38,26 +39,27 @@ func (mc minioClientMock) getBucketNotification(bucketName string) (bucketNotifi
3839}
3940
4041//// Mock mc S3Client functions ////
41- var mcAddNotificationConfigMock func (arn string , events []string , prefix , suffix string , ignoreExisting bool ) * probe.Error
42- var mcRemoveNotificationConfigMock func (arn string , event string , prefix string , suffix string ) * probe.Error
42+ var mcAddNotificationConfigMock func (ctx context. Context , arn string , events []string , prefix , suffix string , ignoreExisting bool ) * probe.Error
43+ var mcRemoveNotificationConfigMock func (ctx context. Context , arn string , event string , prefix string , suffix string ) * probe.Error
4344
4445// Define a mock struct of mc S3Client interface implementation
4546type s3ClientMock struct {
4647}
4748
4849// implements mc.S3Client.AddNotificationConfigMock()
49- func (c s3ClientMock ) addNotificationConfig (arn string , events []string , prefix , suffix string , ignoreExisting bool ) * probe.Error {
50- return mcAddNotificationConfigMock (arn , events , prefix , suffix , ignoreExisting )
50+ func (c s3ClientMock ) addNotificationConfig (ctx context. Context , arn string , events []string , prefix , suffix string , ignoreExisting bool ) * probe.Error {
51+ return mcAddNotificationConfigMock (ctx , arn , events , prefix , suffix , ignoreExisting )
5152}
5253
5354// implements mc.S3Client.DeleteBucketEventNotification()
54- func (c s3ClientMock ) removeNotificationConfig (arn string , event string , prefix string , suffix string ) * probe.Error {
55- return mcRemoveNotificationConfigMock (arn , event , prefix , suffix )
55+ func (c s3ClientMock ) removeNotificationConfig (ctx context. Context , arn string , event string , prefix string , suffix string ) * probe.Error {
56+ return mcRemoveNotificationConfigMock (ctx , arn , event , prefix , suffix )
5657}
5758
5859func TestAddBucketNotification (t * testing.T ) {
5960 assert := assert .New (t )
6061 // mock minIO client
62+ ctx := context .Background ()
6163 client := s3ClientMock {}
6264 function := "createBucketEvent()"
6365 // Test-1: createBucketEvent() set an event with empty parameters and events, should set default values with no error
@@ -66,10 +68,10 @@ func TestAddBucketNotification(t *testing.T) {
6668 testPrefix := ""
6769 testSuffix := ""
6870 testIgnoreExisting := false
69- mcAddNotificationConfigMock = func (arn string , events []string , prefix , suffix string , ignoreExisting bool ) * probe.Error {
71+ mcAddNotificationConfigMock = func (ctx context. Context , arn string , events []string , prefix , suffix string , ignoreExisting bool ) * probe.Error {
7072 return nil
7173 }
72- if err := createBucketEvent (client , testArn , testNotificationEvents , testPrefix , testSuffix , testIgnoreExisting ); err != nil {
74+ if err := createBucketEvent (ctx , client , testArn , testNotificationEvents , testPrefix , testSuffix , testIgnoreExisting ); err != nil {
7375 t .Errorf ("Failed on %s:, error occurred: %s" , function , err .Error ())
7476 }
7577
@@ -82,23 +84,24 @@ func TestAddBucketNotification(t *testing.T) {
8284 testPrefix = "photos/"
8385 testSuffix = ".jpg"
8486 testIgnoreExisting = true
85- mcAddNotificationConfigMock = func (arn string , events []string , prefix , suffix string , ignoreExisting bool ) * probe.Error {
87+ mcAddNotificationConfigMock = func (ctx context. Context , arn string , events []string , prefix , suffix string , ignoreExisting bool ) * probe.Error {
8688 return nil
8789 }
88- if err := createBucketEvent (client , testArn , testNotificationEvents , testPrefix , testSuffix , testIgnoreExisting ); err != nil {
90+ if err := createBucketEvent (ctx , client , testArn , testNotificationEvents , testPrefix , testSuffix , testIgnoreExisting ); err != nil {
8991 t .Errorf ("Failed on %s:, error occurred: %s" , function , err .Error ())
9092 }
9193
9294 // Test-3 createBucketEvent() S3Client.AddNotificationConfig returns an error and is handled correctly
93- mcAddNotificationConfigMock = func (arn string , events []string , prefix , suffix string , ignoreExisting bool ) * probe.Error {
95+ mcAddNotificationConfigMock = func (ctx context. Context , arn string , events []string , prefix , suffix string , ignoreExisting bool ) * probe.Error {
9496 return probe .NewError (errors .New ("error" ))
9597 }
96- if err := createBucketEvent (client , testArn , testNotificationEvents , testPrefix , testSuffix , testIgnoreExisting ); assert .Error (err ) {
98+ if err := createBucketEvent (ctx , client , testArn , testNotificationEvents , testPrefix , testSuffix , testIgnoreExisting ); assert .Error (err ) {
9799 assert .Equal ("error" , err .Error ())
98100 }
99101}
100102
101103func TestDeleteBucketNotification (t * testing.T ) {
104+ ctx := context .Background ()
102105 assert := assert .New (t )
103106 // mock minIO client
104107 client := s3ClientMock {}
@@ -112,18 +115,18 @@ func TestDeleteBucketNotification(t *testing.T) {
112115 models .NotificationEventTypePut }
113116 prefix := "/photos"
114117 suffix := ".jpg"
115- mcRemoveNotificationConfigMock = func (arn string , event string , prefix string , suffix string ) * probe.Error {
118+ mcRemoveNotificationConfigMock = func (ctx context. Context , arn string , event string , prefix string , suffix string ) * probe.Error {
116119 return nil
117120 }
118- if err := deleteBucketEventNotification (client , testArn , events , swag .String (prefix ), swag .String (suffix )); err != nil {
121+ if err := deleteBucketEventNotification (ctx , client , testArn , events , swag .String (prefix ), swag .String (suffix )); err != nil {
119122 t .Errorf ("Failed on %s:, error occurred: %s" , function , err .Error ())
120123 }
121124
122125 // Test-2 deleteBucketEventNotification() S3Client.DeleteBucketEventNotification returns an error and is handled correctly
123- mcRemoveNotificationConfigMock = func (arn string , event string , prefix string , suffix string ) * probe.Error {
126+ mcRemoveNotificationConfigMock = func (ctx context. Context , arn string , event string , prefix string , suffix string ) * probe.Error {
124127 return probe .NewError (errors .New ("error" ))
125128 }
126- if err := deleteBucketEventNotification (client , testArn , events , swag .String (prefix ), swag .String (suffix )); assert .Error (err ) {
129+ if err := deleteBucketEventNotification (ctx , client , testArn , events , swag .String (prefix ), swag .String (suffix )); assert .Error (err ) {
127130 assert .Equal ("error" , err .Error ())
128131 }
129132
0 commit comments