@@ -15,6 +15,7 @@ package silence
15
15
16
16
import (
17
17
"bytes"
18
+ "fmt"
18
19
"os"
19
20
"runtime"
20
21
"sort"
@@ -470,32 +471,24 @@ func TestSilenceLimits(t *testing.T) {
470
471
EndsAt : time .Now ().Add (5 * time .Minute ),
471
472
}
472
473
require .NoError (t , s .Set (sil1 ))
473
- require .NotEqual (t , "" , sil1 .Id )
474
474
475
- // Insert sil2 should fail because maximum number of silences
476
- // has been exceeded.
475
+ // Insert sil2 should fail because maximum number of silences has been
476
+ // exceeded.
477
477
sil2 := & pb.Silence {
478
- Matchers : []* pb.Matcher {{Name : "a " , Pattern : "b " }},
478
+ Matchers : []* pb.Matcher {{Name : "c " , Pattern : "d " }},
479
479
StartsAt : time .Now (),
480
480
EndsAt : time .Now ().Add (5 * time .Minute ),
481
481
}
482
482
require .EqualError (t , s .Set (sil2 ), "exceeded maximum number of silences: 1 (limit: 1)" )
483
- require .Equal (t , "" , sil2 .Id )
484
483
485
- // Expire sil1 and run the GC. This should allow sil2 to be
486
- // inserted.
484
+ // Expire sil1 and run the GC. This should allow sil2 to be inserted.
487
485
require .NoError (t , s .Expire (sil1 .Id ))
488
486
n , err := s .GC ()
489
487
require .NoError (t , err )
490
488
require .Equal (t , 1 , n )
491
-
492
489
require .NoError (t , s .Set (sil2 ))
493
- require .NotEqual (t , "" , sil2 .Id )
494
490
495
- // Should be able to update sil2 without hitting the limit.
496
- require .NoError (t , s .Set (sil2 ))
497
-
498
- // Expire sil2.
491
+ // Expire sil2 and run the GC.
499
492
require .NoError (t , s .Expire (sil2 .Id ))
500
493
n , err = s .GC ()
501
494
require .NoError (t , err )
@@ -505,26 +498,55 @@ func TestSilenceLimits(t *testing.T) {
505
498
sil3 := & pb.Silence {
506
499
Matchers : []* pb.Matcher {
507
500
{
508
- Name : strings .Repeat ("a " , 2 << 9 ),
509
- Pattern : strings .Repeat ("b " , 2 << 9 ),
501
+ Name : strings .Repeat ("e " , 2 << 9 ),
502
+ Pattern : strings .Repeat ("f " , 2 << 9 ),
510
503
},
511
504
{
512
- Name : strings .Repeat ("c " , 2 << 9 ),
513
- Pattern : strings .Repeat ("d " , 2 << 9 ),
505
+ Name : strings .Repeat ("g " , 2 << 9 ),
506
+ Pattern : strings .Repeat ("h " , 2 << 9 ),
514
507
},
515
508
},
516
- CreatedBy : strings .Repeat ("e " , 2 << 9 ),
517
- Comment : strings .Repeat ("f " , 2 << 9 ),
509
+ CreatedBy : strings .Repeat ("i " , 2 << 9 ),
510
+ Comment : strings .Repeat ("j " , 2 << 9 ),
518
511
StartsAt : time .Now (),
519
512
EndsAt : time .Now ().Add (5 * time .Minute ),
520
513
}
521
- err = s .Set (sil3 )
522
- require .Error (t , err )
523
- // Do not check the exact size as it can change between consecutive runs
524
- // due to padding.
525
- require .Contains (t , err .Error (), "silence exceeded maximum size" )
526
- // TODO: Once we fix https://github.com/prometheus/alertmanager/issues/3878 this should be require.Equal.
527
- require .NotEqual (t , "" , sil3 .Id )
514
+ require .EqualError (t , s .Set (sil3 ), fmt .Sprintf ("silence exceeded maximum size: %d bytes (limit: 4096 bytes)" , s .toMeshSilence (sil3 ).Size ()))
515
+
516
+ // Should be able to insert sil4.
517
+ sil4 := & pb.Silence {
518
+ Matchers : []* pb.Matcher {{Name : "k" , Pattern : "l" }},
519
+ StartsAt : time .Now (),
520
+ EndsAt : time .Now ().Add (5 * time .Minute ),
521
+ }
522
+ require .NoError (t , s .Set (sil4 ))
523
+
524
+ // Should be able to update sil4 without modifications. It is expected to
525
+ // keep the same ID.
526
+ sil5 := cloneSilence (sil4 )
527
+ require .NoError (t , s .Set (sil5 ))
528
+ require .Equal (t , sil4 .Id , sil5 .Id )
529
+
530
+ // Should be able to update the comment. It is also expected to keep the
531
+ // same ID.
532
+ sil6 := cloneSilence (sil5 )
533
+ sil6 .Comment = "m"
534
+ require .NoError (t , s .Set (sil6 ))
535
+ require .Equal (t , sil5 .Id , sil6 .Id )
536
+
537
+ // Should not be able to update the start and end time as this requires
538
+ // sil6 to be expired and a new silence to be created. However, this would
539
+ // exceed the maximum number of silences, which counts both active and
540
+ // expired silences.
541
+ sil7 := cloneSilence (sil6 )
542
+ sil7 .StartsAt = time .Now ().Add (5 * time .Minute )
543
+ sil7 .EndsAt = time .Now ().Add (10 * time .Minute )
544
+ require .EqualError (t , s .Set (sil7 ), "exceeded maximum number of silences: 1 (limit: 1)" )
545
+
546
+ // sil6 should not be expired because the update failed.
547
+ sil6 , err = s .QueryOne (QIDs (sil6 .Id ))
548
+ require .NoError (t , err )
549
+ require .Equal (t , types .SilenceStateActive , getState (sil6 , s .nowUTC ()))
528
550
}
529
551
530
552
func TestSilenceNoLimits (t * testing.T ) {
0 commit comments