Repository: prometheus/alertmanager
File: types/types_test.go (Go)

Code before:
	}

	require.Equal(t, 0, countTotal())

	a1 := model.Alert{
		StartsAt: now.Add(-2 * time.Minute),
		EndsAt:   now.Add(2 * time.Minute),
		Labels:   model.LabelSet{"test": "active"},
	}
	a2 := model.Alert{
		StartsAt: now.Add(-2 * time.Minute),
		EndsAt:   now.Add(2 * time.Minute),
		Labels:   model.LabelSet{"test": "suppressed"},
	}
	a3 := model.Alert{
		StartsAt: now.Add(-2 * time.Minute),
		EndsAt:   now.Add(-1 * time.Minute),
		Labels:   model.LabelSet{"test": "resolved"},
	}

	// Insert an active alert.
	marker.SetActiveOrSilenced(a1.Fingerprint(), 1, nil, nil)
	require.Equal(t, 1, countByState(AlertStateActive))
	require.Equal(t, 1, countTotal())

	// Insert a suppressed alert by silence.
	marker.SetActiveOrSilenced(a2.Fingerprint(), 1, []string{"1"}, nil)
	require.Equal(t, 1, countByState(AlertStateSuppressed))
	require.Equal(t, 2, countTotal())

	// Insert a resolved silenced alert - it'll count as suppressed.
	marker.SetActiveOrSilenced(a3.Fingerprint(), 1, []string{"1"}, nil)
	require.Equal(t, 2, countByState(AlertStateSuppressed))
	require.Equal(t, 3, countTotal())

	// Insert a resolved alert - it'll count as active.
	marker.SetActiveOrSilenced(a3.Fingerprint(), 1, nil, nil)
	require.Equal(t, 2, countByState(AlertStateActive))
	require.Equal(t, 3, countTotal())
}

func TestAlertMerge(t *testing.T) {
	now := time.Now()

	// By convention, alert A is always older than alert B.
	pairs := []struct {
		A, B, Res *Alert
	}{
		{
			// Both alerts have the Timeout flag set.
			// StartsAt is defined by Alert A.

Review comment (suggestion):
Just a suggestion to make this easier to read:

```suggestion
	// Insert a silenced alert.
```

Code after:
	}

	require.Equal(t, 0, countTotal())

	a1 := model.Alert{
		StartsAt: now.Add(-2 * time.Minute),
		EndsAt:   now.Add(2 * time.Minute),
		Labels:   model.LabelSet{"test": "active"},
	}
	a2 := model.Alert{
		StartsAt: now.Add(-2 * time.Minute),
		EndsAt:   now.Add(2 * time.Minute),
		Labels:   model.LabelSet{"test": "suppressed"},
	}
	a3 := model.Alert{
		StartsAt: now.Add(-2 * time.Minute),
		EndsAt:   now.Add(-1 * time.Minute),
		Labels:   model.LabelSet{"test": "resolved"},
	}

	// Insert an active alert.
	marker.SetActiveOrSilenced(a1.Fingerprint(), 1, nil, nil)
	require.Equal(t, 1, countByState(AlertStateActive))
	require.Equal(t, 1, countTotal())

	// Insert a silenced alert.
	marker.SetActiveOrSilenced(a2.Fingerprint(), 1, []string{"1"}, nil)
	require.Equal(t, 1, countByState(AlertStateSuppressed))
	require.Equal(t, 2, countTotal())

	// Insert a resolved silenced alert - it'll count as suppressed.
	marker.SetActiveOrSilenced(a3.Fingerprint(), 1, []string{"1"}, nil)
	require.Equal(t, 2, countByState(AlertStateSuppressed))
	require.Equal(t, 3, countTotal())

	// Remove the silence from a3 - it'll count as active.
	marker.SetActiveOrSilenced(a3.Fingerprint(), 1, nil, nil)
	require.Equal(t, 2, countByState(AlertStateActive))
	require.Equal(t, 3, countTotal())
}

func TestAlertMerge(t *testing.T) {
	now := time.Now()

	// By convention, alert A is always older than alert B.
	pairs := []struct {
		A, B, Res *Alert
	}{
		{
			// Both alerts have the Timeout flag set.
			// StartsAt is defined by Alert A.