Skip to content

Commit 2799ffa

Browse files
committed
incident/tests: Don't invalidate source_id_seq
1 parent fb4bc02 commit 2799ffa

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

internal/incident/incidents_test.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/icinga/icinga-notifications/internal/event"
1010
"github.com/icinga/icinga-notifications/internal/object"
1111
"github.com/icinga/icinga-notifications/internal/testutils"
12+
"github.com/icinga/icinga-notifications/internal/utils"
13+
"github.com/jmoiron/sqlx"
1214
"github.com/stretchr/testify/assert"
1315
"github.com/stretchr/testify/require"
1416
"go.uber.org/zap/zaptest"
@@ -20,11 +22,16 @@ func TestLoadOpenIncidents(t *testing.T) {
2022
ctx := context.Background()
2123
db := testutils.GetTestDB(ctx, t)
2224

23-
// Insert a dummy sources for our test cases!
24-
source := config.Source{ID: 1, Type: "notifications", Name: "Icinga Notifications", Icinga2InsecureTLS: types.Bool{Bool: false, Valid: true}}
25-
stmt, _ := db.BuildInsertStmt(source)
26-
_, err := db.NamedExecContext(ctx, stmt, source)
27-
require.NoError(t, err, "populating source table should not fail")
25+
// Insert a dummy source for our test cases!
26+
source := config.Source{Type: "notifications", Name: "Icinga Notifications", Icinga2InsecureTLS: types.Bool{Bool: false, Valid: true}}
27+
err := utils.RunInTx(ctx, db, func(tx *sqlx.Tx) error {
28+
id, err := utils.InsertAndFetchId(ctx, tx, utils.BuildInsertStmtWithout(db, source, "id"), source)
29+
require.NoError(t, err, "populating source table should not fail")
30+
31+
source.ID = id
32+
return nil
33+
})
34+
require.NoError(t, err, "utils.RunInTx() should not fail")
2835

2936
// Reduce the default placeholders per statement to a meaningful number, so that we can
3037
// test some parallelism when loading the incidents.
@@ -37,7 +44,7 @@ func TestLoadOpenIncidents(t *testing.T) {
3744

3845
testData := make(map[string]*Incident, 10*db.Options.MaxPlaceholdersPerStatement)
3946
for j := 1; j <= 10*db.Options.MaxPlaceholdersPerStatement; j++ {
40-
i := makeIncident(ctx, db, t, false)
47+
i := makeIncident(ctx, db, t, source.ID, false)
4148
testData[i.ObjectID.String()] = i
4249
}
4350

@@ -69,11 +76,11 @@ func TestLoadOpenIncidents(t *testing.T) {
6976

7077
for j := 1; j <= db.Options.MaxPlaceholdersPerStatement/2; j++ {
7178
// We don't need to cache recovered incidents in memory.
72-
_ = makeIncident(ctx, db, t, true)
79+
_ = makeIncident(ctx, db, t, source.ID, true)
7380

7481
if j%2 == 0 {
7582
// Add some extra new not recovered incidents to fully simulate a daemon reload.
76-
i := makeIncident(ctx, db, t, false)
83+
i := makeIncident(ctx, db, t, source.ID, false)
7784
testData[i.ObjectID.String()] = i
7885
}
7986
}
@@ -132,10 +139,10 @@ func assertIncidents(ctx context.Context, db *database.DB, t *testing.T, testDat
132139
// This will firstly create and synchronise a new object from a freshly generated dummy event with distinct
133140
// tags and name, and ensures that no error is returned, otherwise it will cause the entire test suite to fail.
134141
// Once the object has been successfully synchronised, an incident is created and synced with the database.
135-
func makeIncident(ctx context.Context, db *database.DB, t *testing.T, recovered bool) *Incident {
142+
func makeIncident(ctx context.Context, db *database.DB, t *testing.T, sourceID int64, recovered bool) *Incident {
136143
ev := &event.Event{
137144
Time: time.Time{},
138-
SourceId: 1,
145+
SourceId: sourceID,
139146
Name: testutils.MakeRandomString(t),
140147
Tags: map[string]string{ // Always generate unique object tags not to produce same object ID!
141148
"host": testutils.MakeRandomString(t),

0 commit comments

Comments
 (0)