github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/alertmanager/alertmanager_ring_test.go (about)

     1  package alertmanager
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/grafana/dskit/ring"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestIsHealthyForAlertmanagerOperations(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	tests := map[string]struct {
    15  		instance *ring.InstanceDesc
    16  		timeout  time.Duration
    17  		expected bool
    18  	}{
    19  		"ACTIVE instance with last keepalive newer than timeout": {
    20  			instance: &ring.InstanceDesc{State: ring.ACTIVE, Timestamp: time.Now().Add(-30 * time.Second).Unix()},
    21  			timeout:  time.Minute,
    22  			expected: true,
    23  		},
    24  		"ACTIVE instance with last keepalive older than timeout": {
    25  			instance: &ring.InstanceDesc{State: ring.ACTIVE, Timestamp: time.Now().Add(-90 * time.Second).Unix()},
    26  			timeout:  time.Minute,
    27  			expected: false,
    28  		},
    29  		"JOINING instance with last keepalive newer than timeout": {
    30  			instance: &ring.InstanceDesc{State: ring.JOINING, Timestamp: time.Now().Add(-30 * time.Second).Unix()},
    31  			timeout:  time.Minute,
    32  			expected: false,
    33  		},
    34  		"LEAVING instance with last keepalive newer than timeout": {
    35  			instance: &ring.InstanceDesc{State: ring.LEAVING, Timestamp: time.Now().Add(-30 * time.Second).Unix()},
    36  			timeout:  time.Minute,
    37  			expected: false,
    38  		},
    39  		"PENDING instance with last keepalive newer than timeout": {
    40  			instance: &ring.InstanceDesc{State: ring.PENDING, Timestamp: time.Now().Add(-30 * time.Second).Unix()},
    41  			timeout:  time.Minute,
    42  			expected: false,
    43  		},
    44  	}
    45  
    46  	for testName, testData := range tests {
    47  		testData := testData
    48  
    49  		t.Run(testName, func(t *testing.T) {
    50  			actual := testData.instance.IsHealthy(RingOp, testData.timeout, time.Now())
    51  			assert.Equal(t, testData.expected, actual)
    52  		})
    53  	}
    54  }