File size: 1,706 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package smoke

import (
	"encoding/json"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/target/goalert/test/smoke/harness"
)

// TestAlertCleanup verifies that old alerts are purged from the DB
// when `Maintenance.AlertCleanupDays` is set.
func TestAlertCleanup(t *testing.T) {
	t.Parallel()

	sql := `
	insert into escalation_policies (id, name) 
	values
		({{uuid "eid"}}, 'esc policy');
	insert into services (id, escalation_policy_id, name) 
	values
		({{uuid "sid"}}, {{uuid "eid"}}, 'service');

	insert into alerts (id, service_id, summary, status, created_at) 
	values
		(1, {{uuid "sid"}}, 'testing', 'closed', now()),
		(2, {{uuid "sid"}}, 'testing', 'closed', now() - '2 days'::interval);
`
	h := harness.NewHarness(t, sql, "site24x7-integration")
	defer h.Close()

	h.Trigger()

	var data struct {
		A *struct{ ID string }
		B *struct{ ID string }
	}
	res := h.GraphQLQuery2("{a:alert(id: 1){id} b:alert(id: 2){id}}")
	assert.Empty(t, res.Errors, "errors")
	err := json.Unmarshal(res.Data, &data)
	assert.NoError(t, err)
	assert.Equal(t, "1", data.A.ID)
	assert.Equal(t, "2", data.B.ID)

	cfg := h.Config()
	cfg.Maintenance.AlertCleanupDays = 1
	h.RestartGoAlertWithConfig(cfg)

	assert.EventuallyWithT(t, func(t *assert.CollectT) {
		res = h.GraphQLQuery2("{a:alert(id: 1){id}}")
		assert.Empty(t, res.Errors, "errors")
		err = json.Unmarshal(res.Data, &data)
		assert.NoError(t, err)
		assert.Equal(t, "1", data.A.ID)

		res = h.GraphQLQuery2("{a:alert(id: 2){id}}")
		assert.Empty(t, res.Errors, "errors")
		err = json.Unmarshal(res.Data, &data)
		assert.NoError(t, err)
		// #2 should have been cleaned up
		assert.Nil(t, data.A)
	}, 15*time.Second, time.Second)
}