@@ -581,23 +581,34 @@ func TestMultitenantAlertmanager_deleteUnusedRemoteUserState(t *testing.T) {
581
581
582
582
alertStore := prepareInMemoryAlertStore ()
583
583
ringStore := consul .NewInMemoryClient (ring .GetCodec ())
584
- reg := prometheus .NewPedanticRegistry ()
585
- cfg := mockAlertmanagerConfig (t )
586
584
587
- cfg .ShardingRing .ReplicationFactor = 1
588
- cfg .ShardingRing .InstanceID = "instance"
589
- cfg .ShardingRing .InstanceAddr = "127.0.0.1"
590
- cfg .ShardingEnabled = true
585
+ createInstance := func (i int ) * MultitenantAlertmanager {
586
+ reg := prometheus .NewPedanticRegistry ()
587
+ cfg := mockAlertmanagerConfig (t )
591
588
592
- // Increase state write interval so that state gets written sooner, making test faster.
593
- cfg .Persister .Interval = 500 * time .Millisecond
589
+ cfg .ShardingRing .ReplicationFactor = 1
590
+ cfg .ShardingRing .InstanceID = fmt .Sprintf ("instance-%d" , i )
591
+ cfg .ShardingRing .InstanceAddr = fmt .Sprintf ("127.0.0.1-%d" , i )
592
+ cfg .ShardingEnabled = true
594
593
595
- am , err := createMultitenantAlertmanager (cfg , nil , nil , alertStore , ringStore , nil , log .NewNopLogger (), reg )
596
- require .NoError (t , err )
597
- t .Cleanup (func () {
598
- require .NoError (t , services .StopAndAwaitTerminated (ctx , am ))
599
- })
600
- require .NoError (t , services .StartAndAwaitRunning (ctx , am ))
594
+ // Increase state write interval so that state gets written sooner, making test faster.
595
+ cfg .Persister .Interval = 500 * time .Millisecond
596
+
597
+ am , err := createMultitenantAlertmanager (cfg , nil , nil , alertStore , ringStore , nil , log .NewLogfmtLogger (os .Stdout ), reg )
598
+ require .NoError (t , err )
599
+ t .Cleanup (func () {
600
+ require .NoError (t , services .StopAndAwaitTerminated (ctx , am ))
601
+ })
602
+ require .NoError (t , services .StartAndAwaitRunning (ctx , am ))
603
+
604
+ return am
605
+ }
606
+
607
+ // Create two instances. With replication factor of 1, this means that only one
608
+ // of the instances will own the user. This tests that an instance does not delete
609
+ // state for users that are configured, but are owned by other instances.
610
+ am1 := createInstance (1 )
611
+ am2 := createInstance (2 )
601
612
602
613
// Configure the users and wait for the state persister to write some state for both.
603
614
{
@@ -612,7 +623,9 @@ func TestMultitenantAlertmanager_deleteUnusedRemoteUserState(t *testing.T) {
612
623
Templates : []* alertspb.TemplateDesc {},
613
624
}))
614
625
615
- err = am .loadAndSyncConfigs (context .Background (), reasonPeriodic )
626
+ err := am1 .loadAndSyncConfigs (context .Background (), reasonPeriodic )
627
+ require .NoError (t , err )
628
+ err = am2 .loadAndSyncConfigs (context .Background (), reasonPeriodic )
616
629
require .NoError (t , err )
617
630
618
631
require .Eventually (t , func () bool {
@@ -624,7 +637,9 @@ func TestMultitenantAlertmanager_deleteUnusedRemoteUserState(t *testing.T) {
624
637
625
638
// Perform another sync to trigger cleanup; this should have no effect.
626
639
{
627
- err = am .loadAndSyncConfigs (context .Background (), reasonPeriodic )
640
+ err := am1 .loadAndSyncConfigs (context .Background (), reasonPeriodic )
641
+ require .NoError (t , err )
642
+ err = am2 .loadAndSyncConfigs (context .Background (), reasonPeriodic )
628
643
require .NoError (t , err )
629
644
630
645
_ , err = alertStore .GetFullState (context .Background (), user1 )
@@ -637,7 +652,9 @@ func TestMultitenantAlertmanager_deleteUnusedRemoteUserState(t *testing.T) {
637
652
{
638
653
require .NoError (t , alertStore .DeleteAlertConfig (ctx , user1 ))
639
654
640
- err = am .loadAndSyncConfigs (context .Background (), reasonPeriodic )
655
+ err := am1 .loadAndSyncConfigs (context .Background (), reasonPeriodic )
656
+ require .NoError (t , err )
657
+ err = am2 .loadAndSyncConfigs (context .Background (), reasonPeriodic )
641
658
require .NoError (t , err )
642
659
643
660
_ , err = alertStore .GetFullState (context .Background (), user1 )
0 commit comments