17
17
package restapi
18
18
19
19
import (
20
+ "context"
20
21
"fmt"
21
22
"testing"
22
23
@@ -38,26 +39,27 @@ func (mc minioClientMock) getBucketNotification(bucketName string) (bucketNotifi
38
39
}
39
40
40
41
//// 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
43
44
44
45
// Define a mock struct of mc S3Client interface implementation
45
46
type s3ClientMock struct {
46
47
}
47
48
48
49
// 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 )
51
52
}
52
53
53
54
// 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 )
56
57
}
57
58
58
59
func TestAddBucketNotification (t * testing.T ) {
59
60
assert := assert .New (t )
60
61
// mock minIO client
62
+ ctx := context .Background ()
61
63
client := s3ClientMock {}
62
64
function := "createBucketEvent()"
63
65
// 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) {
66
68
testPrefix := ""
67
69
testSuffix := ""
68
70
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 {
70
72
return nil
71
73
}
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 {
73
75
t .Errorf ("Failed on %s:, error occurred: %s" , function , err .Error ())
74
76
}
75
77
@@ -82,23 +84,24 @@ func TestAddBucketNotification(t *testing.T) {
82
84
testPrefix = "photos/"
83
85
testSuffix = ".jpg"
84
86
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 {
86
88
return nil
87
89
}
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 {
89
91
t .Errorf ("Failed on %s:, error occurred: %s" , function , err .Error ())
90
92
}
91
93
92
94
// 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 {
94
96
return probe .NewError (errors .New ("error" ))
95
97
}
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 ) {
97
99
assert .Equal ("error" , err .Error ())
98
100
}
99
101
}
100
102
101
103
func TestDeleteBucketNotification (t * testing.T ) {
104
+ ctx := context .Background ()
102
105
assert := assert .New (t )
103
106
// mock minIO client
104
107
client := s3ClientMock {}
@@ -112,18 +115,18 @@ func TestDeleteBucketNotification(t *testing.T) {
112
115
models .NotificationEventTypePut }
113
116
prefix := "/photos"
114
117
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 {
116
119
return nil
117
120
}
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 {
119
122
t .Errorf ("Failed on %s:, error occurred: %s" , function , err .Error ())
120
123
}
121
124
122
125
// 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 {
124
127
return probe .NewError (errors .New ("error" ))
125
128
}
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 ) {
127
130
assert .Equal ("error" , err .Error ())
128
131
}
129
132
0 commit comments