@@ -17,21 +17,67 @@ package provider
17
17
18
18
import (
19
19
"context"
20
+ "fmt"
20
21
"reflect"
21
22
"testing"
22
23
23
24
"github.com/agiledragon/gomonkey/v2"
24
25
coreV1 "k8s.io/api/core/v1"
26
+ metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
+ "k8s.io/client-go/kubernetes/fake"
25
28
cosispec "sigs.k8s.io/container-object-storage-interface-spec"
26
29
27
30
"github.com/huawei/cosi-driver/pkg/s3/agent"
28
31
"github.com/huawei/cosi-driver/pkg/s3/policy"
29
32
"github.com/huawei/cosi-driver/pkg/user"
30
33
"github.com/huawei/cosi-driver/pkg/user/api"
31
34
"github.com/huawei/cosi-driver/pkg/user/clientset/poe"
35
+ "github.com/huawei/cosi-driver/pkg/utils/keylock"
32
36
)
33
37
34
- func Test_registerUser_NewUser_Success (t * testing.T ) {
38
+ func Test_ProvisionerServer_DriverGrantBucketAccess_Success (t * testing.T ) {
39
+ // arrange
40
+ ctx := context .TODO ()
41
+ req := & cosispec.DriverGrantBucketAccessRequest {}
42
+ s := & provisionerServer {
43
+ K8sClient : fake .NewSimpleClientset (),
44
+ keyLock : keylock .NewKeyLock (keyLockSize ),
45
+ }
46
+ bacSecret := & coreV1.Secret {}
47
+ bcResource := & resourceIdInfo {}
48
+ bcSecret := & coreV1.Secret {}
49
+ userData := & userInfo {}
50
+
51
+ _ , _ = s .K8sClient .CoreV1 ().Secrets (bacSecret .Namespace ).Create (ctx , bacSecret , metaV1.CreateOptions {})
52
+
53
+ wantResponse := & cosispec.DriverGrantBucketAccessResponse {
54
+ AccountId : assembleResourceId (bacSecret .Namespace , bacSecret .Name , req .Name ),
55
+ Credentials : buildCredentials (bcSecret , userData ),
56
+ }
57
+
58
+ // mock
59
+ patches := gomonkey .ApplyFuncReturn (checkDriverGrantBucketAccessRequest , nil ).
60
+ ApplyFuncReturn (fetchDataFromResourceId , bcResource , bcSecret , nil ).
61
+ ApplyFuncReturn (checkBucketExistence , nil ).
62
+ ApplyFuncReturn (registerUser , userData , nil ).
63
+ ApplyFuncReturn (setBucketPolicy , nil )
64
+
65
+ // act
66
+ gotResponse , gotErr := s .DriverGrantBucketAccess (ctx , req )
67
+
68
+ // assert
69
+ if ! reflect .DeepEqual (gotResponse , wantResponse ) || gotErr != nil {
70
+ t .Errorf ("Test_ProvisionerServer_DriverGrantBucketAccess_Success failed, " +
71
+ "wantResponse= [%v], gotResponse= [%v], wantErr= nil, gotErr= [%v]" , wantResponse , gotResponse , gotErr )
72
+ }
73
+
74
+ // cleanup
75
+ t .Cleanup (func () {
76
+ patches .Reset ()
77
+ })
78
+ }
79
+
80
+ func Test_RegisterUser_NewUser_Success (t * testing.T ) {
35
81
// arrange
36
82
ctx := context .TODO ()
37
83
accountSecret := & coreV1.Secret {}
@@ -67,7 +113,7 @@ func Test_registerUser_NewUser_Success(t *testing.T) {
67
113
68
114
// assert
69
115
if reflect .DeepEqual (gotUserData , wantUserData ) || gotErr != nil {
70
- t .Errorf ("Test_registerUser_NewUser_Success failed, got= [%v], want= [%v], " +
116
+ t .Errorf ("Test_RegisterUser_NewUser_Success failed, got= [%v], want= [%v], " +
71
117
"gotErr= [%v], wantErr= nil" , gotUserData , wantUserData , gotErr )
72
118
}
73
119
@@ -77,7 +123,7 @@ func Test_registerUser_NewUser_Success(t *testing.T) {
77
123
})
78
124
}
79
125
80
- func Test_setBucketPolicy_NewPolicy_Success (t * testing.T ) {
126
+ func Test_SetBucketPolicy_NewPolicy_Success (t * testing.T ) {
81
127
// arrange
82
128
userName := "user-demo"
83
129
userArn := "arn-id"
@@ -108,7 +154,252 @@ func Test_setBucketPolicy_NewPolicy_Success(t *testing.T) {
108
154
109
155
// assert
110
156
if gotErr != nil {
111
- t .Errorf ("Test_setBucketPolicy_NewPolicy_Success failed, gotErr= [%v], wantErr= nil" , gotErr )
157
+ t .Errorf ("Test_SetBucketPolicy_NewPolicy_Success failed, gotErr= [%v], wantErr= nil" , gotErr )
158
+ }
159
+
160
+ //cleanup
161
+ t .Cleanup (func () {
162
+ mock .Reset ()
163
+ })
164
+ }
165
+
166
+ func Test_CheckDriverGrantBucketAccessRequest_EmptyBucketId (t * testing.T ) {
167
+ // arrange
168
+ req := & cosispec.DriverGrantBucketAccessRequest {}
169
+ req .BucketId = ""
170
+ wantErr := fmt .Errorf ("empty bucket id" )
171
+
172
+ // act
173
+ gotErr := checkDriverGrantBucketAccessRequest (req )
174
+
175
+ // assert
176
+ if gotErr == nil || gotErr .Error () != wantErr .Error () {
177
+ t .Errorf ("Test_CheckDriverGrantBucketAccessRequest_EmptyBucketId failed, " +
178
+ "gotErr= [%s], wantErr= [%s]" , gotErr , wantErr )
179
+ }
180
+ }
181
+
182
+ func Test_CheckDriverGrantBucketAccessRequest_EmptyUserName (t * testing.T ) {
183
+ // arrange
184
+ req := & cosispec.DriverGrantBucketAccessRequest {}
185
+ req .BucketId = "bucketId"
186
+ req .Name = ""
187
+ wantErr := fmt .Errorf ("empty user name" )
188
+
189
+ // act
190
+ gotErr := checkDriverGrantBucketAccessRequest (req )
191
+
192
+ // assert
193
+ if gotErr == nil || gotErr .Error () != wantErr .Error () {
194
+ t .Errorf ("Test_CheckDriverGrantBucketAccessRequest_EmptyUserName failed, " +
195
+ "gotErr= [%v], wantErr= [%v]" , gotErr , wantErr )
196
+ }
197
+ }
198
+
199
+ func Test_CheckDriverGrantBucketAccessRequest_IAMAuthenticationType (t * testing.T ) {
200
+ // arrange
201
+ req := & cosispec.DriverGrantBucketAccessRequest {}
202
+ req .BucketId = "bucketId"
203
+ req .Name = "userName"
204
+ req .AuthenticationType = cosispec .AuthenticationType_IAM
205
+
206
+ wantErr := fmt .Errorf ("IAM authentication type not implemented" )
207
+
208
+ // act
209
+ gotErr := checkDriverGrantBucketAccessRequest (req )
210
+
211
+ // assert
212
+ if gotErr == nil || gotErr .Error () != wantErr .Error () {
213
+ t .Errorf ("Test_CheckDriverGrantBucketAccessRequest_IAMAuthenticationType failed, " +
214
+ "gotErr= [%v], wantErr= [%v]" , gotErr , wantErr )
215
+ }
216
+ }
217
+
218
+ func Test_CheckDriverGrantBucketAccessRequest_UnknownAuthenticationType (t * testing.T ) {
219
+ // arrange
220
+ req := & cosispec.DriverGrantBucketAccessRequest {}
221
+ req .BucketId = "bucketId"
222
+ req .Name = "userName"
223
+ req .AuthenticationType = cosispec .AuthenticationType_UnknownAuthenticationType
224
+
225
+ wantErr := fmt .Errorf ("unknown authentication type" )
226
+
227
+ // act
228
+ gotErr := checkDriverGrantBucketAccessRequest (req )
229
+
230
+ // assert
231
+ if gotErr == nil || gotErr .Error () != wantErr .Error () {
232
+ t .Errorf ("Test_CheckDriverGrantBucketAccessRequest_UnknownAuthenticationType failed, " +
233
+ "gotErr= [%v], wantErr= [%v]" , gotErr , wantErr )
234
+ }
235
+ }
236
+
237
+ func Test_CheckDriverGrantBucketAccessRequest_MissingAccountSecretName (t * testing.T ) {
238
+ // arrange
239
+ req := & cosispec.DriverGrantBucketAccessRequest {}
240
+ req .BucketId = "bucketId"
241
+ req .Name = "userName"
242
+ req .AuthenticationType = cosispec .AuthenticationType_Key
243
+ req .Parameters = make (map [string ]string )
244
+
245
+ wantErr := fmt .Errorf ("account secret name value is empty" )
246
+
247
+ // act
248
+ gotErr := checkDriverGrantBucketAccessRequest (req )
249
+
250
+ // assert
251
+ if gotErr == nil || gotErr .Error () != wantErr .Error () {
252
+ t .Errorf ("Test_CheckDriverGrantBucketAccessRequest_MissingAccountSecretName failed, " +
253
+ "gotErr= [%v], wantErr= [%v]" , gotErr , wantErr )
254
+ }
255
+ }
256
+
257
+ func Test_CheckDriverGrantBucketAccessRequest_MissingAccountSecretNamespace (t * testing.T ) {
258
+ // arrange
259
+ req := & cosispec.DriverGrantBucketAccessRequest {}
260
+ req .BucketId = "bucketId"
261
+ req .Name = "userName"
262
+ req .AuthenticationType = cosispec .AuthenticationType_Key
263
+ req .Parameters = map [string ]string {
264
+ accountSecretName : "accountSecret" ,
265
+ }
266
+
267
+ wantErr := fmt .Errorf ("account secret namespace value is empty" )
268
+
269
+ // act
270
+ gotErr := checkDriverGrantBucketAccessRequest (req )
271
+
272
+ // assert
273
+ if gotErr == nil || gotErr .Error () != wantErr .Error () {
274
+ t .Errorf ("Test_CheckDriverGrantBucketAccessRequest_MissingAccountSecretNamespace failed, " +
275
+ "gotErr= [%v], wantErr= [%v]" , gotErr , wantErr )
276
+ }
277
+ }
278
+
279
+ func Test_CheckDriverGrantBucketAccessRequest_InvalidBucketPolicyModel (t * testing.T ) {
280
+ // arrange
281
+ req := & cosispec.DriverGrantBucketAccessRequest {}
282
+ req .BucketId = "bucketId"
283
+ req .Name = "userName"
284
+ req .AuthenticationType = cosispec .AuthenticationType_Key
285
+ req .Parameters = map [string ]string {
286
+ accountSecretName : "accountSecret" ,
287
+ accountSecretNamespace : "accountSecretNamespace" ,
288
+ bucketPolicyModel : "invalidModel" ,
289
+ }
290
+
291
+ wantErr := fmt .Errorf ("invalid bucketPolicy model [invalidModel]" )
292
+
293
+ // act
294
+ gotErr := checkDriverGrantBucketAccessRequest (req )
295
+
296
+ // assert
297
+ if gotErr == nil || gotErr .Error () != wantErr .Error () {
298
+ t .Errorf ("Test_CheckDriverGrantBucketAccessRequest_InvalidBucketPolicyModel failed, " +
299
+ "gotErr= [%v], wantErr= [%v]" , gotErr , wantErr )
300
+ }
301
+ }
302
+
303
+ func Test_CheckDriverGrantBucketAccessRequest_NormalCase (t * testing.T ) {
304
+ // arrange
305
+ req := & cosispec.DriverGrantBucketAccessRequest {}
306
+ req .BucketId = "bucketId"
307
+ req .Name = "userName"
308
+ req .AuthenticationType = cosispec .AuthenticationType_Key
309
+ req .Parameters = map [string ]string {
310
+ accountSecretName : "accountSecret" ,
311
+ accountSecretNamespace : "accountSecretNamespace" ,
312
+ bucketPolicyModel : bucketPolicyModelRW ,
313
+ }
314
+
315
+ // act
316
+ gotErr := checkDriverGrantBucketAccessRequest (req )
317
+
318
+ // assert
319
+ if gotErr != nil {
320
+ t .Errorf ("Test_CheckDriverGrantBucketAccessRequest_NormalCase failed, gotErr= [%v], wantErr= [nil]" , gotErr )
321
+ }
322
+ }
323
+
324
+ func Test_BuildCredentials_Success (t * testing.T ) {
325
+ // arrange
326
+ bcAccountSecret := & coreV1.Secret {
327
+ Data : map [string ][]byte {
328
+ "endpoint" : []byte ("https://example.com" ),
329
+ },
330
+ }
331
+ userData := & userInfo {
332
+ accessKeyId : "accessKeyId" ,
333
+ accessSecretKey : "accessSecretKey" ,
334
+ }
335
+
336
+ // act
337
+ gotCredDetails := buildCredentials (bcAccountSecret , userData )
338
+
339
+ // assert
340
+ wantCred := cosispec.CredentialDetails {
341
+ Secrets : map [string ]string {
342
+ accessAk : userData .accessKeyId ,
343
+ accessSk : userData .accessSecretKey ,
344
+ endpoint : string (bcAccountSecret .Data [endpoint ]),
345
+ },
346
+ }
347
+ if ! reflect .DeepEqual (wantCred .Secrets , gotCredDetails [s3Protocol ].Secrets ) {
348
+ t .Errorf ("Test_BuildCredentials_Success failed, gotCredSecret= [%v], wantCredSecret= [%v]" ,
349
+ gotCredDetails [s3Protocol ].Secrets , wantCred .Secrets )
350
+ }
351
+ }
352
+
353
+ func Test_CheckBucketExistence_Success (t * testing.T ) {
354
+ // arrange
355
+ ctx := context .TODO ()
356
+ c := & agent.S3Agent {}
357
+ bucketName := "bucket-demo"
358
+ secret := & coreV1.Secret {}
359
+
360
+ // mock
361
+ mock := gomonkey .
362
+ ApplyFunc (agent .NewS3Agent , func (agent.Config ) (* agent.S3Agent , error ) {
363
+ return c , nil
364
+ }).ApplyMethod (reflect .TypeOf (c ), "CheckBucketExist" ,
365
+ func (_ * agent.S3Agent , ctx context.Context , bucketName string ) error {
366
+ return nil
367
+ })
368
+
369
+ // act
370
+ gotErr := checkBucketExistence (ctx , secret , bucketName )
371
+
372
+ // assert
373
+ if gotErr != nil {
374
+ t .Errorf ("Test_CheckBucketExistence_Success failed, wantErr= nil, gotErr= [%v]" , gotErr )
375
+ }
376
+
377
+ // cleanup
378
+ t .Cleanup (func () {
379
+ mock .Reset ()
380
+ })
381
+ }
382
+
383
+ func Test_CheckBucketExistence_NewAgent_Failed (t * testing.T ) {
384
+ // arrange
385
+ ctx := context .TODO ()
386
+ bucketName := "bucket-demo"
387
+ secret := & coreV1.Secret {}
388
+ angentErr := fmt .Errorf ("s3 new agent error" )
389
+ wantErr := fmt .Errorf ("new s3 agent failed, error is [%v]" , angentErr )
390
+
391
+ // mock
392
+ mock := gomonkey .
393
+ ApplyFunc (agent .NewS3Agent , func (agent.Config ) (* agent.S3Agent , error ) {
394
+ return nil , angentErr
395
+ })
396
+
397
+ // act
398
+ gotErr := checkBucketExistence (ctx , secret , bucketName )
399
+
400
+ // assert
401
+ if gotErr .Error () != wantErr .Error () {
402
+ t .Errorf ("Test_CheckBucketExistence_NewAgent_Failed failed, wangErr= [%v], gotErr= [%v]" , wantErr , gotErr )
112
403
}
113
404
114
405
//cleanup
0 commit comments