Add tests

This commit is contained in:
Dimitar Dimitrov 2022-09-09 22:30:03 +02:00
parent 6b33c90efe
commit 0fc0832427
No known key found for this signature in database
GPG key ID: 4541B04E6C90EBC3
4 changed files with 74 additions and 1 deletions

View file

@ -0,0 +1,5 @@
groups:
- name: test
rules:
- alert: test
expr: sum by (job)(rate(http_requests_total[5m]))

View file

@ -0,0 +1,5 @@
groups:
- name: test
rules:
- alert: test_2
expr: sum by (job)(rate(http_requests_total[5m]))

View file

@ -962,7 +962,7 @@ type ManagerOptions struct {
GroupLoader GroupLoader
DefaultEvaluationDelay func() time.Duration
// AlwaysRestoreAlertState forces all new groups added via LoadGroups to restore their state.
// AlwaysRestoreAlertState forces all new or changed groups in calls to Update to restore.
// Useful when you know you will be adding alerting rules after the manager has already started.
AlwaysRestoreAlertState bool

View file

@ -809,6 +809,69 @@ func TestUpdate(t *testing.T) {
reloadAndValidate(rgs, t, tmpFile, ruleManager, expected, ogs)
}
func TestUpdate_AlwaysRestore(t *testing.T) {
st := teststorage.New(t)
defer st.Close()
ruleManager := NewManager(&ManagerOptions{
Appendable: st,
Queryable: st,
Context: context.Background(),
Logger: log.NewNopLogger(),
AlwaysRestoreAlertState: true,
})
ruleManager.start()
defer ruleManager.Stop()
err := ruleManager.Update(10*time.Second, []string{"fixtures/rules_alerts.yaml"}, nil, "", nil)
require.NoError(t, err)
for _, g := range ruleManager.groups {
require.True(t, g.shouldRestore)
g.shouldRestore = false // set to false to check if Update will set it to true again
}
// Use different file, so groups haven't changed, therefore, we expect state restoration
err = ruleManager.Update(10*time.Second, []string{"fixtures/rules_alerts2.yaml"}, nil, "", nil)
for _, g := range ruleManager.groups {
require.True(t, g.shouldRestore)
}
require.NoError(t, err)
}
func TestUpdate_AlwaysRestoreDoesntAffectUnchangedGroups(t *testing.T) {
files := []string{"fixtures/rules_alerts.yaml"}
st := teststorage.New(t)
defer st.Close()
ruleManager := NewManager(&ManagerOptions{
Appendable: st,
Queryable: st,
Context: context.Background(),
Logger: log.NewNopLogger(),
AlwaysRestoreAlertState: true,
})
ruleManager.start()
defer ruleManager.Stop()
err := ruleManager.Update(10*time.Second, files, nil, "", nil)
require.NoError(t, err)
for _, g := range ruleManager.groups {
require.True(t, g.shouldRestore)
g.shouldRestore = false // set to false to check if Update will set it to true again
}
// Use the same file, so groups haven't changed, therefore, we don't expect state restoration
err = ruleManager.Update(10*time.Second, files, nil, "", nil)
for _, g := range ruleManager.groups {
require.False(t, g.shouldRestore)
}
require.NoError(t, err)
}
func TestUpdateSetsSourceTenants(t *testing.T) {
st := teststorage.New(t)
defer st.Close()