-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathLifecycleConfiguration.spec.js
More file actions
1480 lines (1398 loc) · 53 KB
/
LifecycleConfiguration.spec.js
File metadata and controls
1480 lines (1398 loc) · 53 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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const assert = require('assert');
const { parseString } = require('xml2js');
const { ValidLifecycleRules } = require('../../../lib/models/LifecycleConfiguration');
const LifecycleConfiguration =
require('../../../lib/models/LifecycleConfiguration').default;
const days = {
AbortIncompleteMultipartUpload: 'DaysAfterInitiation',
NoncurrentVersionExpiration: 'NoncurrentDays',
Expiration: 'Days',
};
const mockConfig = {
replicationEndpoints: [
{ site: 'a' },
{ site: 'b' },
{ site: 'aCrrLocation' }
],
locationConstraints: {
'a': { isCRR: false },
'b': { isCRR: false },
'aCrrLocation': { isCRR: true }, // true => 'aCrrLocation' will be filtered out from storageClasses
},
supportedLifecycleRules: ValidLifecycleRules,
};
const MAX_DAYS = 2147483647; // Max 32-bit signed binary integer.
const date = new Date();
date.setUTCHours(0, 0, 0, 0);
/**
* Format of xml request:
<LifecycleConfiguration>
<Rule>
<ID>id1</ID>
<Filter>
<Prefix>logs/</Prefix>
</Filter>
<Status>Enabled</Status>
<Expiration>
<Days>365</Days>
</Expiration>
</Rule>
<Rule>
<ID>DeleteAfterBecomingNonCurrent</ID>
<Filter>
<And>
<Prefix>logs/</Prefix>
<Tag>
<Key>key1</Key>
<Value>value1</Value>
</Tag>
</And>
</Filter>
<Status>Enabled</Status>
<NoncurrentVersionExpiration>
<NoncurrentDays>1</NoncurrentDays>
</NoncurrentVersionExpiration>
</Rule>
</LifecycleConfiguration>
*/
const requiredTags = [
{ tag: 'LifecycleConfiguration', error: 'MalformedXML',
errMessage: 'request xml is undefined or empty' },
{ tag: 'Rule', error: 'MissingRequiredParameter',
errMessage: 'missing required key \'Rules\' in ' +
'LifecycleConfiguration' },
{ tag: 'Status', error: 'MissingRequiredParameter',
errMessage: 'Rule xml does not include Status' },
{ tag: 'Filter', error: 'MalformedXML',
errMessage: 'Rule xml does not include valid Filter or Prefix' },
{ tag: 'Action', error: 'InvalidRequest',
errMessage: 'Rule does not include valid action' }];
const invalidActions = [
{ tag: 'AbortIncompleteMultipartUpload', label: 'no-time',
error: 'MalformedXML',
errMessage: 'AbortIncompleteMultipartUpload action does not ' +
'include DaysAfterInitiation' },
{ tag: 'AbortIncompleteMultipartUpload', label: 'no-tags',
error: 'InvalidRequest', errMessage: 'Tag-based filter cannot be ' +
'used with AbortIncompleteMultipartUpload action' },
{ tag: 'AbortIncompleteMultipartUpload', label: 'invalid-days',
error: 'InvalidArgument',
errMessage: 'DaysAfterInitiation is not a positive integer' },
{ tag: 'Expiration', label: 'no-time', error: 'MalformedXML',
errMessage: 'Expiration action does not include an action time' },
{ tag: 'Expiration', label: 'mult-times', error: 'MalformedXML',
errMessage: 'Expiration action includes more than one time' },
{ tag: 'Expiration', label: 'non-iso', error: 'InvalidArgument',
errMessage: 'Date must be in ISO 8601 format' },
{ tag: 'Expiration', label: 'invalid-days', error: 'InvalidArgument',
errMessage: 'Expiration days is not a positive integer' },
{ tag: 'Expiration', label: 'no-tags', inTag: 'ExpiredObjectDeleteMarker',
error: 'InvalidRequest',
errMessage: 'Tag-based filter cannot be used with ' +
'ExpiredObjectDeleteMarker action' },
{ tag: 'Expiration', label: 'invalid-eodm', error: 'MalformedXML',
errMessage: 'ExpiredObjDeleteMarker is not true or false' },
{ tag: 'NoncurrentVersionExpiration', label: 'no-time',
error: 'MalformedXML',
errMessage: 'NoncurrentVersionExpiration action does not include ' +
'NoncurrentDays' },
{ tag: 'NoncurrentVersionExpiration', label: 'invalid-days',
error: 'InvalidArgument',
errMessage: 'NoncurrentDays is not a positive integer' }];
const invalidFilters = [
{ tag: 'Filter', label: 'also-prefix', error: 'MalformedXML',
errMessage: 'Rule xml should not include both Filter and Prefix' },
{ tag: 'Filter', label: 'and-prefix-tag', error: 'MalformedXML',
errMessage: 'Filter should only include one of And, Prefix, or ' +
'Tag key' },
{ tag: 'And', label: 'only-prefix', error: 'MalformedXML',
errMessage: 'And should include Prefix and Tags or more than one Tag' },
{ tag: 'And', label: 'single-tag', error: 'MalformedXML',
errMessage: 'And should include Prefix and Tags or more than one Tag' },
{ tag: 'Tag', label: 'no-key', error: 'MissingRequiredParameter',
errMessage: 'Tag XML does not contain both Key and Value' },
{ tag: 'Tag', label: 'no-value', error: 'MissingRequiredParameter',
errMessage: 'Tag XML does not contain both Key and Value' },
{ tag: 'Tag', label: 'key-too-long', error: 'InvalidRequest',
errMessage: 'A Tag\'s Key must be a length between 1 and 128' },
{ tag: 'Tag', label: 'value-too-long', error: 'InvalidRequest',
errMessage: 'A Tag\'s Value must be a length between 0 and 256' },
{ tag: 'Tag', label: 'prefix-too-long', error: 'InvalidRequest',
errMessage: 'The maximum size of a prefix is 1024' }];
function generateAction(errorTag, tagObj) {
const xmlObj = {};
if (tagObj) {
let middleTags = '';
if (tagObj.label === 'no-time') {
middleTags = '';
}
if (tagObj.label === 'no-tags') {
middleTags = tagObj.inTag ?
`<${tagObj.inTag}>true</${tagObj.inTag}>` :
`<${days[tagObj.tag]}>1</${days[tagObj.tag]}>`;
xmlObj.filter = '<Filter><Tag><Key>key</Key>' +
'<Value></Value></Tag></Filter>';
}
if (tagObj.label === 'invalid-days') {
middleTags = `<${days[tagObj.tag]}>0</${days[tagObj.tag]}>`;
}
if (tagObj.label === 'mult-times') {
middleTags = `<Days>1</Days><Date>${date}</Date>`;
}
if (tagObj.label === 'non-iso') {
middleTags = '<Date>03-08-2018</Date>';
}
if (tagObj.label === 'invalid-eodm') {
middleTags = '<ExpiredObjectDeleteMarker>foo' +
'</ExpiredObjectDeleteMarker>';
}
xmlObj.actions = `<${tagObj.tag}>${middleTags}</${tagObj.tag}>`;
} else {
xmlObj.actions = '';
}
return xmlObj;
}
function generateFilter(errorTag, tagObj) {
let Filter;
let middleTags = '';
if (tagObj) {
if (tagObj.label === 'and-prefix-tag') {
middleTags = '<And></And><Prefix></Prefix><Tag></Tag>';
}
if (tagObj.label === 'only-prefix') {
middleTags = '<And><Prefix></Prefix></And>';
}
if (tagObj.label === 'empty-prefix') {
middleTags = '<Prefix></Prefix>';
}
if (tagObj.label === 'single-tag') {
middleTags = '<And><Tags><Key>fo</Key><Value></Value></Tags></And>';
}
if (tagObj.label === 'no-key') {
middleTags = '<Tag><Value></Value></Tag>';
}
if (tagObj.label === 'no-value') {
middleTags = '<Tag><Key></Key></Tag>';
}
if (tagObj.label === 'key-too-long') {
const longKey = 'a'.repeat(129);
middleTags = `<Tag><Key>${longKey}</Key><Value></Value></Tag>`;
}
if (tagObj.label === 'value-too-long') {
const longValue = 'b'.repeat(257);
middleTags = `<Tag><Key>a</Key><Value>${longValue}</Value></Tag>`;
}
if (tagObj.label === 'prefix-too-long') {
const longValue = 'a'.repeat(1025);
middleTags = `<Prefix>${longValue}</Prefix>`;
}
if (tagObj.label === 'mult-prefixes') {
middleTags = '<Prefix>foo</Prefix><Prefix>bar</Prefix>' +
`<Prefix>${tagObj.lastPrefix}</Prefix>`;
}
if (tagObj.label === 'mult-tags') {
middleTags = '<And><Tag><Key>color</Key><Value>blue</Value></Tag>' +
'<Tag><Key>shape</Key><Value>circle</Value></Tag></And>';
}
if (tagObj.label === 'not-unique-key-tag') {
middleTags = '<And><Tag><Key>color</Key><Value>blue</Value></Tag>' +
'<Tag><Key>color</Key><Value>red</Value></Tag></And>';
}
Filter = `<Filter>${middleTags}</Filter>`;
if (tagObj.label === 'also-prefix') {
Filter = '<Filter></Filter><Prefix></Prefix>';
}
} else {
Filter = '';
}
return Filter;
}
function generateRule(errorTag, tagObj, ID, Status, Filter, Action) {
let Rule;
if (tagObj && tagObj.rule === 'not-unique-id') {
Rule = `<Rule>${ID + Status + Filter + Action}</Rule>` +
`<Rule>${ID + Status + Filter + Action}</Rule>`;
} else if (tagObj && tagObj.rule === 'too-many-rules') {
for (let i = 0; i <= 1000; i++) {
// eslint-disable-next-line no-param-reassign
ID = `<ID>foo${i}</ID>`;
Rule = `${Rule}<Rule>${ID + Status + Filter + Action}</Rule>`;
}
} else {
Rule = '';
}
return Rule;
}
function generateXml(errorTag, tagObj) {
let ID;
let Status;
let Filter;
let Action;
let Rule;
if (errorTag === 'ID') {
ID = tagObj && tagObj.id ? `<ID>${tagObj.id}</ID>` : '';
} else {
ID = '<ID>foo</ID>';
}
if (errorTag === 'Status') {
Status = tagObj && tagObj.status ?
`<Status>${tagObj.status}</Status>` : '';
} else {
Status = '<Status>Enabled</Status>';
}
if (errorTag === 'Filter') {
Filter = generateFilter(errorTag, tagObj);
} else {
Filter = '<Filter></Filter>';
}
if (errorTag === 'Action') {
const xmlObj = generateAction(errorTag, tagObj);
Action = xmlObj.actions;
Filter = xmlObj.filter ? xmlObj.filter : Filter;
} else {
Action = '<Expiration><Days>1</Days></Expiration>';
}
if (errorTag === 'Rule') {
Rule = generateRule(errorTag, tagObj, ID, Status, Filter, Action);
} else {
Rule = `<Rule>${ID + Status + Filter + Action}</Rule>`;
}
const Lifecycle = errorTag === 'LifecycleConfiguration' ? '' :
`<LifecycleConfiguration>${Rule}` +
'</LifecycleConfiguration>';
return Lifecycle;
}
function generateParsedXml(errorTag, tagObj, cb) {
const xml = generateXml(errorTag, tagObj);
parseString(xml, (err, parsedXml) => {
assert.equal(err, null, 'Error parsing xml');
cb(parsedXml);
});
}
function checkError(parsedXml, error, errMessage, cb) {
const lcConfig = new LifecycleConfiguration(parsedXml, mockConfig)
.getLifecycleConfiguration();
assert.strictEqual(lcConfig.error.is[error], true);
assert.strictEqual(lcConfig.error.description, errMessage);
cb();
}
describe('LifecycleConfiguration class getLifecycleConfiguration', () => {
let tagObj;
beforeEach(() => {
tagObj = {};
mockConfig.supportedLifecycleRules = ValidLifecycleRules;
});
it('should return MalformedXML error if request xml is empty', done => {
const errMessage = 'request xml is undefined or empty';
checkError('', 'MalformedXML', errMessage, done);
});
requiredTags.forEach(t => {
it(`should return ${t.error} error if ${t.tag} tag is missing`,
done => {
generateParsedXml(t.tag, null, parsedXml => {
checkError(parsedXml, t.error, t.errMessage, done);
});
});
});
invalidActions.forEach(a => {
it(`should return ${a.error} for ${a.label} action error`,
done => {
generateParsedXml('Action', a, parsedXml => {
checkError(parsedXml, a.error, a.errMessage, done);
});
});
});
invalidFilters.forEach(filter => {
it(`should return ${filter.error} for ${filter.label} filter error`,
done => {
generateParsedXml('Filter', filter, parsedXml => {
checkError(parsedXml, filter.error, filter.errMessage, done);
});
});
});
it('should return MalformedXML error if invalid status', done => {
tagObj.status = 'foo';
const errMessage = 'Status is not valid';
generateParsedXml('Status', tagObj, parsedXml => {
checkError(parsedXml, 'MalformedXML', errMessage, done);
});
});
it('should return InvalidRequest error if ID not unique', done => {
tagObj.rule = 'not-unique-id';
const errMessage = 'Rule ID must be unique';
generateParsedXml('Rule', tagObj, parsedXml => {
checkError(parsedXml, 'InvalidRequest', errMessage, done);
});
});
it('should return InvalidArgument error if invalid ID', done => {
tagObj.id = 'a'.repeat(256);
const errMessage = 'Rule ID is greater than 255 characters long';
generateParsedXml('ID', tagObj, parsedXml => {
checkError(parsedXml, 'InvalidArgument', errMessage, done);
});
});
it('should return MalformedXML error if over 1000 rules', done => {
tagObj.rule = 'too-many-rules';
const errMessage = 'request xml includes over max limit of 1000 rules';
generateParsedXml('Rule', tagObj, parsedXml => {
checkError(parsedXml, 'MalformedXML', errMessage, done);
});
});
it('should apply all unique Key tags if multiple tags included', done => {
tagObj.label = 'mult-tags';
generateParsedXml('Filter', tagObj, parsedXml => {
const lcConfig = new LifecycleConfiguration(parsedXml, mockConfig)
.getLifecycleConfiguration();
const expected = [{ key: 'color', val: 'blue' },
{ key: 'shape', val: 'circle' }];
assert.deepStrictEqual(expected, lcConfig.rules[0].filter.tags);
done();
});
});
it('should return InvalidRequest is tag key is not unique', done => {
tagObj.label = 'not-unique-key-tag';
const errMessage = 'Tag Keys must be unique';
generateParsedXml('Filter', tagObj, parsedXml => {
checkError(parsedXml, 'InvalidRequest', errMessage, done);
});
});
it('should include prefix in the response even if it is an empty string', done => {
tagObj.label = 'empty-prefix';
const expectedPrefix = '';
generateParsedXml('Filter', tagObj, parsedXml => {
const lcConfig = new LifecycleConfiguration(parsedXml, mockConfig).
getLifecycleConfiguration();
assert.strictEqual(expectedPrefix,
lcConfig.rules[0].filter.rulePrefix);
done();
});
});
it('should return NotImplemented error when rule action is not supported', done => {
mockConfig.supportedLifecycleRules = ['Expiration']; // Only support expiration
const xml = `
<LifecycleConfiguration>
<Rule>
<ID>test-rule</ID>
<Status>Enabled</Status>
<Filter></Filter>
<Transition>
<Days>1</Days>
<StorageClass>a</StorageClass>
</Transition>
</Rule>
</LifecycleConfiguration>`;
parseString(xml, (err, parsedXml) => {
assert.equal(err, null, 'Error parsing xml');
const lcConfig = new LifecycleConfiguration(parsedXml, mockConfig)
.getLifecycleConfiguration();
assert.strictEqual(lcConfig.error.is.NotImplemented, true);
assert.strictEqual(lcConfig.error.description, 'Transition lifecycle action not implemented');
done();
});
});
it('should return NotImplemented error when some rule action are not supported', done => {
mockConfig.supportedLifecycleRules = ['Expiration']; // Only support expiration
const xml = `
<LifecycleConfiguration>
<Rule>
<ID>test-rule</ID>
<Status>Enabled</Status>
<Filter></Filter>
<Expiration>
<Days>2</Days>
</Expiration>
<Transition>
<Days>1</Days>
<StorageClass>a</StorageClass>
</Transition>
</Rule>
</LifecycleConfiguration>`;
parseString(xml, (err, parsedXml) => {
assert.equal(err, null, 'Error parsing xml');
const lcConfig = new LifecycleConfiguration(parsedXml, mockConfig)
.getLifecycleConfiguration();
assert.strictEqual(lcConfig.error.is.NotImplemented, true);
assert.strictEqual(lcConfig.error.description, 'Transition lifecycle action not implemented');
done();
});
});
it('should succeed when all rule actions are supported', done => {
mockConfig.supportedLifecycleRules = ['Expiration', 'Transition'];
const xml = `
<LifecycleConfiguration>
<Rule>
<ID>test-rule</ID>
<Status>Enabled</Status>
<Filter></Filter>
<Expiration>
<Days>2</Days>
</Expiration>
<Transition>
<Days>1</Days>
<StorageClass>a</StorageClass>
</Transition>
</Rule>
</LifecycleConfiguration>`;
parseString(xml, (err, parsedXml) => {
assert.equal(err, null, 'Error parsing xml');
const lcConfig = new LifecycleConfiguration(parsedXml, mockConfig)
.getLifecycleConfiguration();
assert.strictEqual(lcConfig.error, undefined);
done();
});
});
it('should fail if transition destination is marked as a CRR in the location constraints', done => {
mockConfig.supportedLifecycleRules = ['Transition', 'NoncurrentVersionTransition'];
const xml = `
<LifecycleConfiguration>
<Rule>
<ID>test-rule</ID>
<Status>Enabled</Status>
<Filter></Filter>
<Transition>
<Days>1</Days>
<StorageClass>aCrrLocation</StorageClass>
</Transition>
</Rule>
</LifecycleConfiguration>`;
parseString(xml, (err, parsedXml) => {
assert.equal(err, null, 'Error parsing xml');
const lcConfig = new LifecycleConfiguration(parsedXml, mockConfig)
.getLifecycleConfiguration();
assert.strictEqual(lcConfig.error.is.MalformedXML, true);
assert.strictEqual(lcConfig.error.description,
"'StorageClass' must be one of 'a', 'b', but provided: aCrrLocation");
done();
});
});
});
describe('LifecycleConfiguration', () => {
const lifecycleConfiguration = new LifecycleConfiguration({}, mockConfig);
function getParsedXML() {
return {
LifecycleConfiguration: {
Rule: [{
ID: ['test-id'],
Prefix: [''],
Status: ['Enabled'],
Expiration: [{
Days: 1,
}],
}],
},
};
}
describe('::_getRuleFilterDesc', () => {
it('should get Prefix', () => {
const rule = getParsedXML().LifecycleConfiguration.Rule[0];
const ruleFilter = lifecycleConfiguration._getRuleFilterDesc(rule);
assert.strictEqual(ruleFilter, "prefix ''");
});
it('should get Filter.Prefix', () => {
const rule = getParsedXML().LifecycleConfiguration.Rule[0];
delete rule.Prefix;
rule.Filter = [{ Prefix: [''] }];
const ruleFilter = lifecycleConfiguration._getRuleFilterDesc(rule);
assert.strictEqual(ruleFilter, "filter '(prefix=)'");
});
it('should get Filter.Tag', () => {
const rule = getParsedXML().LifecycleConfiguration.Rule[0];
delete rule.Prefix;
rule.Filter = [{ Tag: [{ Key: ['a'], Value: [''] }] }];
const ruleFilter = lifecycleConfiguration._getRuleFilterDesc(rule);
assert.strictEqual(ruleFilter, "filter '(tag: key=a, value=)'");
});
it('should get Filter.And', () => {
const rule = getParsedXML().LifecycleConfiguration.Rule[0];
delete rule.Prefix;
rule.Filter = [{
And: [{
Prefix: [''],
Tag: [{
Key: ['a'],
Value: ['b'],
},
{
Key: ['c'],
Value: ['d'],
}],
}],
}];
const ruleFilter = lifecycleConfiguration._getRuleFilterDesc(rule);
assert.strictEqual(ruleFilter, 'filter ' +
"'(prefix= and tag: key=a, value=b and tag: key=c, value=d)'");
});
it('should get Filter.And without Prefix', () => {
const rule = getParsedXML().LifecycleConfiguration.Rule[0];
delete rule.Prefix;
rule.Filter = [{
And: [{
Tag: [{
Key: ['a'],
Value: ['b'],
},
{
Key: ['c'],
Value: ['d'],
}],
}],
}];
const ruleFilter = lifecycleConfiguration._getRuleFilterDesc(rule);
assert.strictEqual(ruleFilter,
"filter '(tag: key=a, value=b and tag: key=c, value=d)'");
});
it('should get Filter with empty object', () => {
const rule = {
ID: ['test-id'],
Status: ['Enabled'],
Expiration: [{
Days: 1,
}],
};
rule.Filter = [{}];
const ruleFilter = lifecycleConfiguration._getRuleFilterDesc(rule);
assert.strictEqual(ruleFilter, 'filter (all)');
});
it('should get empty Filter', () => {
const rule = {
ID: ['test-id'],
Status: ['Enabled'],
Expiration: [{
Days: 1,
}],
};
rule.Filter = [];
const ruleFilter = lifecycleConfiguration._getRuleFilterDesc(rule);
assert.strictEqual(ruleFilter, 'filter (all)');
});
});
describe('::_checkDays', () => {
it(`should return no error when days value is 0 - ${MAX_DAYS}`, () => {
const error = lifecycleConfiguration._checkDays({
days: 0,
});
assert.strictEqual(error, null);
});
it('should return error when exceeding max value', () => {
const error = lifecycleConfiguration._checkDays({
days: MAX_DAYS + 1,
field: 'a',
ancestor: 'b',
});
const msg = "'a' in b action must not exceed 2147483647";
expect(error.is.MalformedXML).toBeTruthy();
expect(error.description).toEqual(msg);
});
it('should return error when negative value', () => {
const error = lifecycleConfiguration._checkDays({
days: -1,
field: 'a',
ancestor: 'b',
});
const msg = "'a' in b action must be nonnegative";
expect(error.is.InvalidArgument).toBeTruthy();
expect(error.description).toEqual(msg);
});
});
describe('::_checkStorageClasses', () => {
it('should return no error when StorageClass is first one used', () => {
const error = lifecycleConfiguration._checkStorageClasses({
usedStorageClasses: [],
storageClass: 'a',
});
assert.strictEqual(error, null);
});
it('should return no error when StorageClass has not been used', () => {
const error = lifecycleConfiguration._checkStorageClasses({
usedStorageClasses: ['a'],
storageClass: 'b',
});
assert.strictEqual(error, null);
});
it('should return error when unknown StorageClass is given', () => {
const error = lifecycleConfiguration._checkStorageClasses({
storageClass: 'c',
});
const msg = "'StorageClass' must be one of 'a', 'b', but provided: c";
expect(error.is.MalformedXML).toBeTruthy();
expect(error.description).toEqual(msg);
});
it('should return error when StorageClass has been used', () => {
const error = lifecycleConfiguration._checkStorageClasses({
usedStorageClasses: ['a'],
storageClass: 'a',
field: 'a',
ancestor: 'b',
rule: getParsedXML().LifecycleConfiguration.Rule[0],
});
const msg = "'StorageClass' must be different for 'b' actions " +
"in same 'Rule' with prefix ''";
expect(error.is.InvalidRequest).toBeTruthy();
expect(error.description).toEqual(msg);
});
});
describe('::_checkTimeType', () => {
it('should return no error when first time type in rule', () => {
const error = lifecycleConfiguration._checkTimeType({
usedTimeType: null,
currentTimeType: 'Date',
rule: {},
});
assert.strictEqual(error, null);
});
it('should return no error when time type is same as others', () => {
const error = lifecycleConfiguration._checkTimeType({
usedTimeType: 'Date',
currentTimeType: 'Date',
rule: {},
});
assert.strictEqual(error, null);
});
it('should return error when time type differs from others', () => {
const error = lifecycleConfiguration._checkTimeType({
usedTimeType: 'Date',
currentTimeType: 'Days',
rule: getParsedXML().LifecycleConfiguration.Rule[0],
});
const msg = "Found mixed 'Date' and 'Days' based Transition " +
"actions in lifecycle rule for prefix ''";
expect(error.is.InvalidRequest).toBeTruthy();
expect(error.description).toEqual(msg);
});
it('should return error when time type differs across expiration',
() => {
const error = lifecycleConfiguration._checkTimeType({
usedTimeType: 'Date',
currentTimeType: 'Date',
rule: getParsedXML().LifecycleConfiguration.Rule[0],
});
const msg = "Found mixed 'Date' and 'Days' based Expiration and " +
"Transition actions in lifecycle rule for prefix ''";
expect(error.is.InvalidRequest).toBeTruthy();
expect(error.description).toEqual(msg);
});
});
describe('::_checkDate', () => {
it('should return no error with a valid ISO date at midnight', () => {
const date = '2016-01-01T00:00:00';
const error = lifecycleConfiguration._checkDate(date);
assert.strictEqual(error, null);
});
it('should return no error with a valid ISO date at midnight', () => {
const date = '2016-01-01T00:00:00.000Z';
const error = lifecycleConfiguration._checkDate(date);
assert.strictEqual(error, null);
});
it('should return no error with a valid ISO date at midnight with timezone', () => {
const date = '2024-01-08T06:00:00+06:00';
const error = lifecycleConfiguration._checkDate(date);
assert.strictEqual(error, null);
});
it('should return an error with a non-ISO date', () => {
const date = '2016-01-01T00:00:00000Z';
const error = lifecycleConfiguration._checkDate(date);
const msg = 'Date must be in ISO 8601 format';
expect(error.is.InvalidArgument).toBeTruthy();
expect(error.description).toEqual(msg);
});
it('should return an error with a non-ISO date', () => {
const date = 'Fri, 01 Jan 2016 00:00:00 GMT';
const error = lifecycleConfiguration._checkDate(date);
const msg = 'Date must be in ISO 8601 format';
expect(error.is.InvalidArgument).toBeTruthy();
expect(error.description).toEqual(msg);
});
it('should return an error with a non-ISO date', () => {
const date = '2024-01-08T00:00:00+34:00';
const error = lifecycleConfiguration._checkDate(date);
const msg = 'Date must be in ISO 8601 format';
expect(error.is.InvalidArgument).toBeTruthy();
expect(error.description).toEqual(msg);
});
it('should return an error with a date that is not set to midnight', () => {
const date = '2024-01-04T15:22:40Z';
const error = lifecycleConfiguration._checkDate(date);
const msg = '\'Date\' must be at midnight GMT';
expect(error.is.InvalidArgument).toBeTruthy();
expect(error.description).toEqual(msg);
});
it('should return an error with a date that is not set to midnight', () => {
const date = '2024-01-08T00:00:00.123Z';
const error = lifecycleConfiguration._checkDate(date);
const msg = '\'Date\' must be at midnight GMT';
expect(error.is.InvalidArgument).toBeTruthy();
expect(error.description).toEqual(msg);
});
});
describe('::_parseNoncurrentVersionTransition', () => {
function getRule() {
return {
NoncurrentVersionTransition: [
{
NoncurrentDays: ['0'],
StorageClass: ['a'],
},
{
NoncurrentDays: ['1'],
StorageClass: ['b'],
},
],
};
}
it('should return correctly parsed result object', () => {
const rule = getRule();
const result =
lifecycleConfiguration._parseNoncurrentVersionTransition(rule);
assert.deepStrictEqual(result, {
nonCurrentVersionTransition: [
{
noncurrentDays: 0,
storageClass: 'a',
},
{
noncurrentDays: 1,
storageClass: 'b',
},
],
});
});
it('should return parsed result object with error', () => {
const rule = getRule();
rule.NoncurrentVersionTransition[0].NoncurrentDays[0] = '-1';
const result =
lifecycleConfiguration._parseNoncurrentVersionTransition(rule);
const msg = "'NoncurrentDays' in NoncurrentVersionTransition " +
'action must be nonnegative';
expect(result.error.is.InvalidArgument).toBeTruthy();
expect(result.error.description).toEqual(msg);
});
});
describe('::_parseTransition with Days', () => {
function getRule() {
return {
Transition: [
{
Days: ['0'],
StorageClass: ['a'],
},
{
Days: ['1'],
StorageClass: ['b'],
},
],
};
}
it('should return correctly parsed result object', () => {
const rule = getRule();
const result = lifecycleConfiguration._parseTransition(rule);
assert.deepStrictEqual(result, {
transition: [
{
days: 0,
storageClass: 'a',
},
{
days: 1,
storageClass: 'b',
},
],
});
});
it('should return parsed result object with error when days is ' +
'negative', () => {
const rule = getRule();
rule.Transition[0].Days[0] = '-1';
const result = lifecycleConfiguration._parseTransition(rule);
const msg = "'Days' in Transition action must be nonnegative";
expect(result.error.is.InvalidArgument).toBeTruthy();
expect(result.error.description).toEqual(msg);
});
it('should return parsed result object with error when two ' +
'transition days are the same', () => {
const rule = getRule();
rule.Prefix = ['prefix'];
rule.Transition[1].Days[0] = '0';
const result = lifecycleConfiguration._parseTransition(rule);
const msg = "'Days' in the 'Transition' action for StorageClass " +
"'a' for prefix 'prefix' must be at least one day apart from " +
"prefix 'prefix' in the 'Transition' action for StorageClass " +
"'b'";
expect(result.error.is.InvalidArgument).toBeTruthy();
expect(result.error.description).toEqual(msg);
});
});
describe('::_parseTransition with Date', () => {
it('should return parsed result object with error when dates are not ' +
'more than one day apart', () => {
const rule = {
Prefix: ['prefix'],
Transition: [
{
Date: ['2019-01-01T00:00:00.000Z'],
StorageClass: ['a'],
},
{
Date: ['2019-01-01T23:59:59.999Z'],
StorageClass: ['b'],
},
],
};
const result = lifecycleConfiguration._parseTransition(rule);
const msg = "'Date' in the 'Transition' action for StorageClass " +
"'a' for prefix 'prefix' must be at least one day apart from " +
"prefix 'prefix' in the 'Transition' action for StorageClass " +
"'b'";
expect(result.error.is.InvalidArgument).toBeTruthy();
expect(result.error.description).toEqual(msg);
});
});
describe('::_checkTimeGap', () => {
it('should not return error when only one transition', () => {
const params = {
rule: {
Transition: [{
Days: ['0'],
StorageClass: ['a'],
}],
},
days: 0,
storageClass: 'a',
};
const error = lifecycleConfiguration._checkTimeGap(params);
assert.strictEqual(error, null);
});
it('should not return error when transitions have days greater than ' +
'24 hours apart', () => {
const params = {
rule: {
Transition: [{
Days: ['0'],
StorageClass: ['a'],
}, {
Days: ['1'],
StorageClass: ['b'],
}],
},
days: 0,
storageClass: 'a',
};
const error = lifecycleConfiguration._checkTimeGap(params);
assert.strictEqual(error, null);
});
it('should return error when transitions have same day', () => {
const params = {
rule: {
Prefix: 'prefix',
Transition: [{
Days: ['0'],
StorageClass: ['a'],
}, {
Days: ['0'],
StorageClass: ['b'],
}],
},
days: 0,
storageClass: 'a',
};
const error = lifecycleConfiguration._checkTimeGap(params);
expect(error.is.InvalidArgument).toBeTruthy();
});
it('should not return error when transitions have dates greater than ' +
'24 hours apart', () => {
const params = {
rule: {
Transition: [{
Date: ['2019-01-01T00:00:00.000Z'],
StorageClass: ['a'],
}, {
Date: ['2019-01-02T00:00:00.000Z'],
StorageClass: ['b'],
}],
},
date: '2019-01-01T00:00:00.000Z',
storageClass: 'a',
};
const error = lifecycleConfiguration._checkTimeGap(params);
assert.strictEqual(error, null);
});
it('should return error when transitions have dates less than 24 ' +
'hours apart', () => {
const params = {
rule: {
Prefix: 'prefix',
Transition: [{
Date: ['2019-01-01T00:00:00.000Z'],
StorageClass: ['a'],
}, {
Date: ['2019-01-01T23:59:59.999Z'],
StorageClass: ['b'],
}],
},