-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudsync_service_test.go
More file actions
549 lines (510 loc) · 16.1 KB
/
cloudsync_service_test.go
File metadata and controls
549 lines (510 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package truenas
import (
"context"
"encoding/json"
"errors"
"testing"
)
func TestCloudSyncService_CreateCredential_V25(t *testing.T) {
callCount := 0
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
callCount++
if callCount == 1 {
if method != "cloudsync.credentials.create" {
t.Errorf("expected method cloudsync.credentials.create, got %s", method)
}
// Verify V25 format: provider is object
p := params.(map[string]any)
provider, ok := p["provider"].(map[string]any)
if !ok {
t.Fatal("expected provider to be map[string]any for V25")
}
if provider["type"] != "S3" {
t.Errorf("expected provider.type S3, got %v", provider["type"])
}
if provider["access_key_id"] != "AKIATEST" {
t.Errorf("expected access_key_id AKIATEST, got %v", provider["access_key_id"])
}
return json.RawMessage(`{"id": 1}`), nil
}
// Re-read via GetCredential
return sampleCredentialV25JSON(), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
cred, err := svc.CreateCredential(context.Background(), CreateCredentialOpts{
Name: "My S3 Cred",
ProviderType: "S3",
Attributes: map[string]string{
"access_key_id": "AKIATEST",
"secret_access_key": "secret123",
"endpoint": "s3.example.com",
"region": "us-east-1",
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cred == nil {
t.Fatal("expected non-nil credential")
}
if cred.ID != 1 {
t.Errorf("expected ID 1, got %d", cred.ID)
}
if cred.Name != "My S3 Cred" {
t.Errorf("expected name 'My S3 Cred', got %q", cred.Name)
}
if cred.ProviderType != "S3" {
t.Errorf("expected provider type S3, got %s", cred.ProviderType)
}
if cred.Attributes["access_key_id"] != "AKIATEST" {
t.Errorf("expected access_key_id AKIATEST, got %s", cred.Attributes["access_key_id"])
}
if cred.Attributes["region"] != "us-east-1" {
t.Errorf("expected region us-east-1, got %s", cred.Attributes["region"])
}
}
func TestCloudSyncService_CreateCredential_V24(t *testing.T) {
callCount := 0
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
callCount++
if callCount == 1 {
if method != "cloudsync.credentials.create" {
t.Errorf("expected method cloudsync.credentials.create, got %s", method)
}
// Verify V24 format: provider is string, attributes separate
p := params.(map[string]any)
provider, ok := p["provider"].(string)
if !ok {
t.Fatal("expected provider to be string for V24")
}
if provider != "S3" {
t.Errorf("expected provider S3, got %s", provider)
}
attrs, ok := p["attributes"].(map[string]any)
if !ok {
t.Fatal("expected attributes to be map[string]any for V24")
}
if attrs["access_key_id"] != "AKIATEST" {
t.Errorf("expected attributes.access_key_id AKIATEST, got %v", attrs["access_key_id"])
}
return json.RawMessage(`{"id": 1}`), nil
}
return sampleCredentialV24JSON(), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 24, Minor: 10})
cred, err := svc.CreateCredential(context.Background(), CreateCredentialOpts{
Name: "My S3 Cred",
ProviderType: "S3",
Attributes: map[string]string{
"access_key_id": "AKIATEST",
"secret_access_key": "secret123",
"endpoint": "s3.example.com",
"region": "us-east-1",
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cred == nil {
t.Fatal("expected non-nil credential")
}
if cred.ID != 1 {
t.Errorf("expected ID 1, got %d", cred.ID)
}
if cred.ProviderType != "S3" {
t.Errorf("expected provider type S3, got %s", cred.ProviderType)
}
}
func TestCloudSyncService_CreateCredential_Error(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return nil, errors.New("connection refused")
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
cred, err := svc.CreateCredential(context.Background(), CreateCredentialOpts{})
if err == nil {
t.Fatal("expected error")
}
if cred != nil {
t.Error("expected nil credential on error")
}
if err.Error() != "connection refused" {
t.Errorf("expected 'connection refused', got %q", err.Error())
}
}
func TestCloudSyncService_CreateCredential_ParseError(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return json.RawMessage(`not json`), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
_, err := svc.CreateCredential(context.Background(), CreateCredentialOpts{})
if err == nil {
t.Fatal("expected parse error")
}
}
func TestCloudSyncService_GetCredential_V25(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
if method != "cloudsync.credentials.query" {
t.Errorf("expected method cloudsync.credentials.query, got %s", method)
}
return sampleCredentialV25JSON(), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
cred, err := svc.GetCredential(context.Background(), 1)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cred == nil {
t.Fatal("expected non-nil credential")
}
if cred.ID != 1 {
t.Errorf("expected ID 1, got %d", cred.ID)
}
if cred.Name != "My S3 Cred" {
t.Errorf("expected name 'My S3 Cred', got %q", cred.Name)
}
if cred.ProviderType != "S3" {
t.Errorf("expected provider type S3, got %s", cred.ProviderType)
}
if cred.Attributes["secret_access_key"] != "secret123" {
t.Errorf("expected secret_access_key secret123, got %s", cred.Attributes["secret_access_key"])
}
if cred.Attributes["endpoint"] != "s3.example.com" {
t.Errorf("expected endpoint s3.example.com, got %s", cred.Attributes["endpoint"])
}
}
func TestCloudSyncService_GetCredential_V24(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return sampleCredentialV24JSON(), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 24, Minor: 10})
cred, err := svc.GetCredential(context.Background(), 1)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cred == nil {
t.Fatal("expected non-nil credential")
}
if cred.ID != 1 {
t.Errorf("expected ID 1, got %d", cred.ID)
}
if cred.ProviderType != "S3" {
t.Errorf("expected provider type S3, got %s", cred.ProviderType)
}
if cred.Attributes["access_key_id"] != "AKIATEST" {
t.Errorf("expected access_key_id AKIATEST, got %s", cred.Attributes["access_key_id"])
}
}
func TestCloudSyncService_GetCredential_NotFound(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return json.RawMessage(`[]`), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
cred, err := svc.GetCredential(context.Background(), 999)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cred != nil {
t.Error("expected nil credential for not found")
}
}
func TestCloudSyncService_GetCredential_Error(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return nil, errors.New("timeout")
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
_, err := svc.GetCredential(context.Background(), 1)
if err == nil {
t.Fatal("expected error")
}
}
func TestCloudSyncService_GetCredential_ParseError(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return json.RawMessage(`not json`), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
_, err := svc.GetCredential(context.Background(), 1)
if err == nil {
t.Fatal("expected parse error")
}
}
func TestCloudSyncService_ListCredentials_V25(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
if method != "cloudsync.credentials.query" {
t.Errorf("expected method cloudsync.credentials.query, got %s", method)
}
if params != nil {
t.Error("expected nil params for ListCredentials")
}
return json.RawMessage(`[
{"id": 1, "name": "S3 Cred", "provider": {"type": "S3", "access_key_id": "key1", "secret_access_key": "sec1"}},
{"id": 2, "name": "B2 Cred", "provider": {"type": "B2", "account": "acct", "key": "b2key"}}
]`), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
creds, err := svc.ListCredentials(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(creds) != 2 {
t.Fatalf("expected 2 credentials, got %d", len(creds))
}
if creds[0].ProviderType != "S3" {
t.Errorf("expected first cred provider type S3, got %s", creds[0].ProviderType)
}
if creds[1].ProviderType != "B2" {
t.Errorf("expected second cred provider type B2, got %s", creds[1].ProviderType)
}
if creds[1].Attributes["account"] != "acct" {
t.Errorf("expected second cred account 'acct', got %s", creds[1].Attributes["account"])
}
}
func TestCloudSyncService_ListCredentials_V24(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return json.RawMessage(`[
{"id": 1, "name": "S3 Cred", "provider": "S3", "attributes": {"access_key_id": "key1"}},
{"id": 2, "name": "B2 Cred", "provider": "B2", "attributes": {"account": "acct"}}
]`), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 24, Minor: 10})
creds, err := svc.ListCredentials(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(creds) != 2 {
t.Fatalf("expected 2 credentials, got %d", len(creds))
}
if creds[0].Attributes["access_key_id"] != "key1" {
t.Errorf("expected first cred access_key_id 'key1', got %s", creds[0].Attributes["access_key_id"])
}
}
func TestCloudSyncService_ListCredentials_Empty(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return json.RawMessage(`[]`), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
creds, err := svc.ListCredentials(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(creds) != 0 {
t.Errorf("expected 0 credentials, got %d", len(creds))
}
}
func TestCloudSyncService_ListCredentials_Error(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return nil, errors.New("network error")
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
_, err := svc.ListCredentials(context.Background())
if err == nil {
t.Fatal("expected error")
}
}
func TestCloudSyncService_ListCredentials_ParseError(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return json.RawMessage(`not json`), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
_, err := svc.ListCredentials(context.Background())
if err == nil {
t.Fatal("expected parse error")
}
}
func TestCloudSyncService_UpdateCredential_V25(t *testing.T) {
callCount := 0
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
callCount++
if callCount == 1 {
if method != "cloudsync.credentials.update" {
t.Errorf("expected method cloudsync.credentials.update, got %s", method)
}
// Verify params shape: []any{id, map}
slice, ok := params.([]any)
if !ok {
t.Fatal("expected []any params for update")
}
if len(slice) != 2 {
t.Fatalf("expected 2 elements, got %d", len(slice))
}
id, ok := slice[0].(int64)
if !ok || id != 1 {
t.Errorf("expected id 1, got %v", slice[0])
}
// Verify V25 format
p := slice[1].(map[string]any)
provider, ok := p["provider"].(map[string]any)
if !ok {
t.Fatal("expected provider to be map for V25")
}
if provider["type"] != "S3" {
t.Errorf("expected provider.type S3, got %v", provider["type"])
}
return json.RawMessage(`{"id": 1}`), nil
}
return sampleCredentialV25JSON(), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
cred, err := svc.UpdateCredential(context.Background(), 1, UpdateCredentialOpts{
Name: "My S3 Cred",
ProviderType: "S3",
Attributes: map[string]string{
"access_key_id": "AKIATEST",
"secret_access_key": "secret123",
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cred == nil {
t.Fatal("expected non-nil credential")
}
if cred.ID != 1 {
t.Errorf("expected ID 1, got %d", cred.ID)
}
}
func TestCloudSyncService_UpdateCredential_V24(t *testing.T) {
callCount := 0
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
callCount++
if callCount == 1 {
// Verify V24 format
slice := params.([]any)
p := slice[1].(map[string]any)
if _, ok := p["provider"].(string); !ok {
t.Fatal("expected provider to be string for V24")
}
if _, ok := p["attributes"].(map[string]any); !ok {
t.Fatal("expected attributes to be map for V24")
}
return json.RawMessage(`{"id": 1}`), nil
}
return sampleCredentialV24JSON(), nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 24, Minor: 10})
cred, err := svc.UpdateCredential(context.Background(), 1, UpdateCredentialOpts{
Name: "My S3 Cred",
ProviderType: "S3",
Attributes: map[string]string{
"access_key_id": "AKIATEST",
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cred == nil {
t.Fatal("expected non-nil credential")
}
}
func TestCloudSyncService_UpdateCredential_Error(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return nil, errors.New("not found")
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
_, err := svc.UpdateCredential(context.Background(), 999, UpdateCredentialOpts{})
if err == nil {
t.Fatal("expected error")
}
}
func TestCloudSyncService_DeleteCredential(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
if method != "cloudsync.credentials.delete" {
t.Errorf("expected method cloudsync.credentials.delete, got %s", method)
}
id, ok := params.(int64)
if !ok || id != 5 {
t.Errorf("expected id 5, got %v", params)
}
return nil, nil
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
err := svc.DeleteCredential(context.Background(), 5)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestCloudSyncService_DeleteCredential_Error(t *testing.T) {
mock := &mockAsyncCaller{
mockCaller: mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return nil, errors.New("permission denied")
},
},
}
svc := NewCloudSyncService(mock, Version{Major: 25, Minor: 4})
err := svc.DeleteCredential(context.Background(), 1)
if err == nil {
t.Fatal("expected error")
}
}