9
9
"github.com/icinga/icinga-notifications/internal/event"
10
10
"github.com/icinga/icinga-notifications/internal/object"
11
11
"github.com/icinga/icinga-notifications/internal/testutils"
12
+ "github.com/icinga/icinga-notifications/internal/utils"
13
+ "github.com/jmoiron/sqlx"
12
14
"github.com/stretchr/testify/assert"
13
15
"github.com/stretchr/testify/require"
14
16
"go.uber.org/zap/zaptest"
@@ -20,11 +22,16 @@ func TestLoadOpenIncidents(t *testing.T) {
20
22
ctx := context .Background ()
21
23
db := testutils .GetTestDB (ctx , t )
22
24
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" )
28
35
29
36
// Reduce the default placeholders per statement to a meaningful number, so that we can
30
37
// test some parallelism when loading the incidents.
@@ -37,7 +44,7 @@ func TestLoadOpenIncidents(t *testing.T) {
37
44
38
45
testData := make (map [string ]* Incident , 10 * db .Options .MaxPlaceholdersPerStatement )
39
46
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 )
41
48
testData [i .ObjectID .String ()] = i
42
49
}
43
50
@@ -69,11 +76,11 @@ func TestLoadOpenIncidents(t *testing.T) {
69
76
70
77
for j := 1 ; j <= db .Options .MaxPlaceholdersPerStatement / 2 ; j ++ {
71
78
// We don't need to cache recovered incidents in memory.
72
- _ = makeIncident (ctx , db , t , true )
79
+ _ = makeIncident (ctx , db , t , source . ID , true )
73
80
74
81
if j % 2 == 0 {
75
82
// 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 )
77
84
testData [i .ObjectID .String ()] = i
78
85
}
79
86
}
@@ -132,10 +139,10 @@ func assertIncidents(ctx context.Context, db *database.DB, t *testing.T, testDat
132
139
// This will firstly create and synchronise a new object from a freshly generated dummy event with distinct
133
140
// tags and name, and ensures that no error is returned, otherwise it will cause the entire test suite to fail.
134
141
// 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 {
136
143
ev := & event.Event {
137
144
Time : time.Time {},
138
- SourceId : 1 ,
145
+ SourceId : sourceID ,
139
146
Name : testutils .MakeRandomString (t ),
140
147
Tags : map [string ]string { // Always generate unique object tags not to produce same object ID!
141
148
"host" : testutils .MakeRandomString (t ),
0 commit comments