-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathconfig_test.go
More file actions
828 lines (814 loc) · 24.2 KB
/
Copy pathconfig_test.go
File metadata and controls
828 lines (814 loc) · 24.2 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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package awss3
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/dustin/go-humanize"
"github.com/stretchr/testify/assert"
"github.com/elastic/beats/v7/libbeat/common/match"
"github.com/elastic/beats/v7/libbeat/reader/parser"
"github.com/elastic/beats/v7/libbeat/reader/readfile"
conf "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/mapstr"
)
func TestConfig(t *testing.T) {
const queueURL = "https://example.com"
const s3Bucket = "arn:aws:s3:::aBucket"
const s3AccessPoint = "arn:aws:s3:us-east-2:123456789:accesspoint/test-accesspoint"
const nonAWSS3Bucket = "minio-bucket"
makeConfig := func(quequeURL, s3Bucket string, s3AccessPoint string, nonAWSS3Bucket string) config {
// Have a separate copy of defaults in the test to make it clear when
// anyone changes the defaults.
parserConf := parser.Config{}
require.NoError(t, parserConf.Unpack(conf.MustNewConfigFrom("")))
return config{
QueueURL: quequeURL,
BucketARN: s3Bucket,
AccessPointARN: s3AccessPoint,
NonAWSBucketName: nonAWSS3Bucket,
APITimeout: 120 * time.Second,
VisibilityTimeout: 300 * time.Second,
SQSMaxReceiveCount: 5,
SQSWaitTime: 20 * time.Second,
SQSGraceTime: 20 * time.Second,
BucketListInterval: 120 * time.Second,
BucketListPrefix: "",
LexicographicalLookbackKeys: 100,
PathStyle: false,
NumberOfWorkers: 5,
ReaderConfig: readerConfig{
BufferSize: 16 * humanize.KiByte,
MaxBytes: 10 * humanize.MiByte,
LineTerminator: readfile.AutoLineTerminator,
Parsers: parserConf,
},
}
}
testCases := []struct {
name string
queueURL string
s3Bucket string
s3AccessPoint string
nonAWSS3Bucket string
config mapstr.M
expectedErr string
expectedCfg func(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket string) config
}{
{
name: "input with defaults for queueURL",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": queueURL,
},
expectedErr: "",
expectedCfg: makeConfig,
},
{
name: "input with defaults for s3Bucket",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"bucket_arn": s3Bucket,
"number_of_workers": 5,
},
expectedErr: "",
expectedCfg: func(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket string) config {
c := makeConfig("", s3Bucket, "", "")
c.NumberOfWorkers = 5
return c
},
},
{
name: "input with file_selectors",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": queueURL,
"file_selectors": []mapstr.M{
{
"regex": "/CloudTrail/",
},
},
},
expectedErr: "",
expectedCfg: func(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket string) config {
c := makeConfig(queueURL, "", "", "")
regex := match.MustCompile("/CloudTrail/")
c.FileSelectors = []fileSelectorConfig{
{
Regex: ®ex,
ReaderConfig: c.ReaderConfig,
},
}
return c
},
},
{
name: "non-AWS_endpoint_with_explicit_region",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": queueURL,
"region": "region",
"endpoint": "ep",
},
expectedErr: "",
expectedCfg: func(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket string) config {
c := makeConfig(queueURL, "", "", "")
c.RegionName = "region"
c.AWSConfig.Endpoint = "ep"
return c
},
},
{
name: "explicit_AWS_endpoint_with_explicit_region",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": "https://sqs.us-east-1.amazonaws.com/627959692251/test-s3-logs",
"region": "region",
"endpoint": "amazonaws.com",
},
expectedErr: "",
expectedCfg: func(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket string) config {
c := makeConfig(queueURL, "", "", "")
c.QueueURL = "https://sqs.us-east-1.amazonaws.com/627959692251/test-s3-logs"
c.AWSConfig.Endpoint = "amazonaws.com"
c.RegionName = "region"
return c
},
},
{
name: "inferred_AWS_endpoint_with_explicit_region",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": "https://sqs.us-east-1.amazonaws.com/627959692251/test-s3-logs",
"region": "region",
},
expectedErr: "",
expectedCfg: func(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket string) config {
c := makeConfig(queueURL, "", "", "")
c.QueueURL = "https://sqs.us-east-1.amazonaws.com/627959692251/test-s3-logs"
c.RegionName = "region"
return c
},
},
{
name: "localstack_with_region_name",
queueURL: "http://localhost:4566/000000000000/sample-queue",
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": "http://localhost:4566/000000000000/sample-queue",
"region": "myregion",
},
expectedErr: "",
expectedCfg: func(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket string) config {
c := makeConfig(queueURL, "", "", "")
c.RegionName = "myregion"
return c
},
},
{
name: "error on no queueURL, s3Bucket, s3AccessPoint, and nonAWSS3Bucket",
queueURL: "",
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": "",
"bucket_arn": "",
"access_point_arn": "",
"non_aws_bucket_name": "",
},
expectedErr: "neither queue_url, bucket_arn, access_point_arn, nor non_aws_bucket_name were provided",
expectedCfg: nil,
},
{
name: "error on both queueURL and s3Bucket",
queueURL: queueURL,
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": queueURL,
"bucket_arn": s3Bucket,
},
expectedErr: "queue_url <https://example.com>, bucket_arn <arn:aws:s3:::aBucket>, access_point_arn <>, non_aws_bucket_name <> cannot be set at the same time",
expectedCfg: nil,
},
{
name: "error on both queueURL and NonAWSS3Bucket",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: nonAWSS3Bucket,
config: mapstr.M{
"queue_url": queueURL,
"non_aws_bucket_name": nonAWSS3Bucket,
},
expectedErr: "queue_url <https://example.com>, bucket_arn <>, access_point_arn <>, non_aws_bucket_name <minio-bucket> cannot be set at the same time",
expectedCfg: nil,
},
{
name: "error on both s3Bucket and NonAWSS3Bucket",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: nonAWSS3Bucket,
config: mapstr.M{
"bucket_arn": s3Bucket,
"non_aws_bucket_name": nonAWSS3Bucket,
},
expectedErr: "queue_url <>, bucket_arn <arn:aws:s3:::aBucket>, access_point_arn <>, non_aws_bucket_name <minio-bucket> cannot be set at the same time",
expectedCfg: nil,
},
{
name: "error on queueURL, s3Bucket, and NonAWSS3Bucket",
queueURL: queueURL,
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: nonAWSS3Bucket,
config: mapstr.M{
"queue_url": queueURL,
"bucket_arn": s3Bucket,
"non_aws_bucket_name": nonAWSS3Bucket,
},
expectedErr: "queue_url <https://example.com>, bucket_arn <arn:aws:s3:::aBucket>, access_point_arn <>, non_aws_bucket_name <minio-bucket> cannot be set at the same time",
expectedCfg: nil,
},
{
name: "error on both s3Bucket and s3AccessPoint",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: s3AccessPoint,
nonAWSS3Bucket: nonAWSS3Bucket,
config: mapstr.M{
"bucket_arn": s3Bucket,
"access_point_arn": s3AccessPoint,
},
expectedErr: "queue_url <>, bucket_arn <arn:aws:s3:::aBucket>, access_point_arn <arn:aws:s3:us-east-2:123456789:accesspoint/test-accesspoint>, non_aws_bucket_name <> cannot be set at the same time",
expectedCfg: nil,
},
{
name: "error on api_timeout == 0",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": queueURL,
"api_timeout": "0",
},
expectedErr: "api_timeout <0s> must be greater than the sqs.wait_time <20s",
expectedCfg: nil,
},
{
name: "error on visibility_timeout == 0",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": queueURL,
"visibility_timeout": "0",
},
expectedErr: "visibility_timeout <0s> must be greater than 0 and less than or equal to 12h",
expectedCfg: nil,
},
{
name: "error on visibility_timeout > 12h",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": queueURL,
"visibility_timeout": "12h1ns",
},
expectedErr: "visibility_timeout <12h0m0.000000001s> must be greater than 0 and less than or equal to 12h",
expectedCfg: nil,
},
{
name: "error on bucket_list_interval == 0",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"bucket_arn": s3Bucket,
"bucket_list_interval": "0",
},
expectedErr: "bucket_list_interval <0s> must be greater than 0",
expectedCfg: nil,
},
{
name: "error on number_of_workers == 0",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"bucket_arn": s3Bucket,
"number_of_workers": "0",
},
expectedErr: "number_of_workers <0> must be greater than 0",
expectedCfg: nil,
},
{
name: "error on buffer_size == 0 ",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": queueURL,
"buffer_size": "0",
},
expectedErr: "buffer_size <0> must be greater than 0",
expectedCfg: nil,
},
{
name: "error on max_bytes == 0 ",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": queueURL,
"max_bytes": "0",
},
expectedErr: "max_bytes <0> must be greater than 0",
expectedCfg: nil,
},
{
name: "error on expand_event_list_from_field and content_type != application/json ",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": queueURL,
"expand_event_list_from_field": "Records",
"content_type": "text/plain",
},
expectedErr: "content_type must be `application/json` when expand_event_list_from_field is used",
expectedCfg: nil,
},
{
name: "error on expand_event_list_from_field and content_type != application/json ",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"bucket_arn": s3Bucket,
"expand_event_list_from_field": "Records",
"content_type": "text/plain",
},
expectedErr: "content_type must be `application/json` when expand_event_list_from_field is used",
expectedCfg: nil,
},
{
name: "input for non-AWS S3 Bucket without region specified",
queueURL: "",
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: nonAWSS3Bucket,
config: mapstr.M{
"non_aws_bucket_name": nonAWSS3Bucket,
"number_of_workers": 5,
},
expectedErr: "region must be configured when using non_aws_bucket_name",
expectedCfg: nil,
},
{
name: "input for non-AWS S3 Bucket with region specified",
queueURL: "",
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: nonAWSS3Bucket,
config: mapstr.M{
"non_aws_bucket_name": nonAWSS3Bucket,
"region": "region",
"number_of_workers": 5,
},
expectedErr: "",
expectedCfg: func(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket string) config {
c := makeConfig("", "", "", nonAWSS3Bucket)
c.NumberOfWorkers = 5
c.RegionName = "region"
return c
},
},
{
name: "error on FIPS with non-AWS S3 Bucket",
queueURL: "",
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: nonAWSS3Bucket,
config: mapstr.M{
"non_aws_bucket_name": nonAWSS3Bucket,
"number_of_workers": 5,
"fips_enabled": true,
},
expectedErr: "fips_enabled cannot be used with a non-AWS S3 bucket",
expectedCfg: nil,
},
{
name: "error on path_style with AWS native S3 Bucket",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"bucket_arn": s3Bucket,
"number_of_workers": 5,
"path_style": true,
},
expectedErr: "path_style can only be used when polling non-AWS S3 services",
expectedCfg: nil,
},
{
name: "error on provider with AWS native S3 Bucket",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"bucket_arn": s3Bucket,
"number_of_workers": 5,
"provider": "asdf",
},
expectedErr: "provider can only be overridden when polling non-AWS S3 services",
expectedCfg: nil,
},
{
name: "error on provider with AWS SQS Queue",
queueURL: queueURL,
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"queue_url": queueURL,
"number_of_workers": 5,
"provider": "asdf",
},
expectedErr: "provider can only be overridden when polling non-AWS S3 services",
expectedCfg: nil,
},
{
name: "backup_to_bucket with AWS",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"bucket_arn": s3Bucket,
"backup_to_bucket_arn": "arn:aws:s3:::bBucket",
"backup_to_bucket_prefix": "backup",
"number_of_workers": 5,
},
expectedErr: "",
expectedCfg: func(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket string) config {
c := makeConfig("", s3Bucket, "", "")
c.BackupConfig.BackupToBucketArn = "arn:aws:s3:::bBucket"
c.BackupConfig.BackupToBucketPrefix = "backup"
c.NumberOfWorkers = 5
return c
},
},
{
name: "backup_to_bucket with non-AWS",
queueURL: "",
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: nonAWSS3Bucket,
config: mapstr.M{
"non_aws_bucket_name": nonAWSS3Bucket,
"region": "region",
"non_aws_backup_to_bucket_name": "bBucket",
"backup_to_bucket_prefix": "backup",
"number_of_workers": 5,
},
expectedErr: "",
expectedCfg: func(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket string) config {
c := makeConfig("", "", "", nonAWSS3Bucket)
c.NonAWSBucketName = nonAWSS3Bucket
c.BackupConfig.NonAWSBackupToBucketName = "bBucket"
c.BackupConfig.BackupToBucketPrefix = "backup"
c.NumberOfWorkers = 5
c.RegionName = "region"
return c
},
},
{
name: "error with non-AWS backup and AWS source",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"bucket_arn": s3Bucket,
"non_aws_backup_to_bucket_name": "bBucket",
"number_of_workers": 5,
},
expectedErr: "backup to non-AWS bucket can only be used for non-AWS sources",
expectedCfg: nil,
},
{
name: "error with AWS backup and non-AWS source",
queueURL: "",
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: nonAWSS3Bucket,
config: mapstr.M{
"non_aws_bucket_name": nonAWSS3Bucket,
"backup_to_bucket_arn": "arn:aws:s3:::bBucket",
"number_of_workers": 5,
},
expectedErr: "backup to AWS bucket can only be used for AWS sources",
expectedCfg: nil,
},
{
name: "error with same bucket backup and empty backup prefix",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"bucket_arn": s3Bucket,
"backup_to_bucket_arn": s3Bucket,
"number_of_workers": 5,
},
expectedErr: "backup_to_bucket_prefix is a required property when source and backup bucket are the same",
expectedCfg: nil,
},
{
name: "error with same bucket backup (non-AWS) and empty backup prefix",
queueURL: "",
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: nonAWSS3Bucket,
config: mapstr.M{
"non_aws_bucket_name": nonAWSS3Bucket,
"non_aws_backup_to_bucket_name": nonAWSS3Bucket,
"number_of_workers": 5,
},
expectedErr: "backup_to_bucket_prefix is a required property when source and backup bucket are the same",
expectedCfg: nil,
},
{
name: "error with same bucket backup and backup prefix equal to list prefix",
queueURL: "",
s3Bucket: s3Bucket,
s3AccessPoint: "",
nonAWSS3Bucket: "",
config: mapstr.M{
"bucket_arn": s3Bucket,
"backup_to_bucket_arn": s3Bucket,
"number_of_workers": 5,
"backup_to_bucket_prefix": "processed_",
"bucket_list_prefix": "processed_",
},
expectedErr: "backup_to_bucket_prefix cannot be the same as bucket_list_prefix, this will create an infinite loop",
expectedCfg: nil,
},
{
name: "error with same bucket backup (non-AWS) and backup prefix equal to list prefix",
queueURL: "",
s3Bucket: "",
s3AccessPoint: "",
nonAWSS3Bucket: nonAWSS3Bucket,
config: mapstr.M{
"non_aws_bucket_name": nonAWSS3Bucket,
"non_aws_backup_to_bucket_name": nonAWSS3Bucket,
"number_of_workers": 5,
"backup_to_bucket_prefix": "processed_",
"bucket_list_prefix": "processed_",
},
expectedErr: "backup_to_bucket_prefix cannot be the same as bucket_list_prefix, this will create an infinite loop",
expectedCfg: nil,
},
{
name: "validate ignore_older and start_timestamp configurations",
s3Bucket: s3Bucket,
config: mapstr.M{
"bucket_arn": s3Bucket,
"ignore_older": "24h",
"start_timestamp": "2024-11-20T20:00:00Z",
},
expectedCfg: func(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket string) config {
c := makeConfig(queueURL, s3Bucket, s3AccessPoint, nonAWSS3Bucket)
c.IgnoreOlder = 24 * time.Hour
c.StartTimestamp = "2024-11-20T20:00:00Z"
return c
},
},
{
name: "ignore_older only accepts valid duration - unit valid with ParseDuration",
s3Bucket: s3Bucket,
config: mapstr.M{
"bucket_arn": s3Bucket,
"ignore_older": "24D",
},
expectedErr: "time: unknown unit \"D\" in duration \"24D\" accessing 'ignore_older'",
},
{
name: "start_timestamp accepts a valid timestamp of format - YYYY-MM-DDTHH:MM:SSZ",
s3Bucket: s3Bucket,
config: mapstr.M{
"bucket_arn": s3Bucket,
"start_timestamp": "2024-11-20 20:20:00",
},
expectedErr: "invalid input for start_timestamp: parsing time \"2024-11-20 20:20:00\" as \"2006-01-02T15:04:05Z07:00\": cannot parse \" 20:20:00\" as \"T\" accessing config",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
in := conf.MustNewConfigFrom(tc.config)
c := defaultConfig()
if err := in.Unpack(&c); err != nil {
if tc.expectedErr != "" {
assert.Contains(t, err.Error(), tc.expectedErr)
return
}
t.Fatal(err)
}
if tc.expectedCfg == nil {
t.Fatal("missing expected config in test case")
}
assert.EqualValues(t, tc.expectedCfg(tc.queueURL, tc.s3Bucket, tc.s3AccessPoint, tc.nonAWSS3Bucket), c)
})
}
}
// TestIsValidAccessPointARN tests the isValidAccessPointARN function
func TestIsValidAccessPointARN(t *testing.T) {
testCases := []struct {
name string
arn string
expected bool
}{
{
name: "Valid Access Point ARN",
arn: "arn:aws:s3:us-east-1:123456789:accesspoint/my-access-point",
expected: true,
},
{
name: "Valid Access Point ARN with another region",
arn: "arn:aws:s3:us-west-2:123456789:accesspoint/my-access-point",
expected: true,
},
{
name: "Invalid ARN with missing parts",
arn: "arn:aws:s3:123456789:accesspoint",
expected: false,
},
{
name: "Invalid ARN without accesspoint keyword",
arn: "arn:aws:s3:us-east-1:123456789:bucket/my-bucket",
expected: false,
},
{
name: "Invalid ARN with wrong format",
arn: "arn:aws:s3:us-east-1:123456789:my-access-point",
expected: false,
},
{
name: "Empty ARN",
arn: "",
expected: false,
},
{
name: "ARN with extra parts but valid access point format",
arn: "arn:aws:s3:us-east-1:123456789:accesspoint/my-access-point/extra",
expected: true,
},
{
name: "ARN with empty name",
arn: "arn:aws:s3:us-east-1:123456789:accesspoint/",
expected: false,
},
}
// Run test cases
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := isValidAccessPointARN(tc.arn)
if result != tc.expected {
t.Errorf("expected %v, got %v for ARN: %s", tc.expected, result, tc.arn)
}
})
}
}
func TestBackupPrefixToExclude(t *testing.T) {
tests := []struct {
name string
cfg config
want string
}{
{
name: "no backup configured",
cfg: config{
BucketARN: "arn:aws:s3:::my-bucket",
BucketListPrefix: "",
},
want: "",
},
{
name: "different bucket",
cfg: config{
BucketARN: "arn:aws:s3:::my-bucket",
BucketListPrefix: "",
BackupConfig: backupConfig{
BackupToBucketArn: "arn:aws:s3:::other-bucket",
BackupToBucketPrefix: "processed/",
},
},
want: "",
},
{
name: "same bucket, empty list prefix",
cfg: config{
BucketARN: "arn:aws:s3:::my-bucket",
BucketListPrefix: "",
BackupConfig: backupConfig{
BackupToBucketArn: "arn:aws:s3:::my-bucket",
BackupToBucketPrefix: "processed/",
},
},
want: "processed/",
},
{
name: "same bucket, backup prefix within list prefix scope",
cfg: config{
BucketARN: "arn:aws:s3:::my-bucket",
BucketListPrefix: "logs/",
BackupConfig: backupConfig{
BackupToBucketArn: "arn:aws:s3:::my-bucket",
BackupToBucketPrefix: "logs/processed/",
},
},
want: "logs/processed/",
},
{
name: "same bucket, backup prefix outside list prefix scope",
cfg: config{
BucketARN: "arn:aws:s3:::my-bucket",
BucketListPrefix: "logs/",
BackupConfig: backupConfig{
BackupToBucketArn: "arn:aws:s3:::my-bucket",
BackupToBucketPrefix: "archived/",
},
},
want: "",
},
{
name: "same bucket via access point ARN",
cfg: config{
AccessPointARN: "arn:aws:s3:us-east-1:123456789:accesspoint/ap",
BucketListPrefix: "",
BackupConfig: backupConfig{
BackupToBucketArn: "arn:aws:s3:us-east-1:123456789:accesspoint/ap",
BackupToBucketPrefix: "done/",
},
},
want: "done/",
},
{
name: "same non-AWS bucket",
cfg: config{
NonAWSBucketName: "minio-bucket",
BucketListPrefix: "",
BackupConfig: backupConfig{
NonAWSBackupToBucketName: "minio-bucket",
BackupToBucketPrefix: "backup/",
},
},
want: "backup/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.cfg.backupPrefixToExclude()
assert.Equal(t, tt.want, got, "backupPrefixToExclude() mismatch")
})
}
}