@@ -111,9 +111,9 @@ func TestObjectSync(t *testing.T) {
111111 t .Run ("Host" , func (t * testing.T ) {
112112 t .Parallel ()
113113
114- for hostId , host := range data .Hosts {
114+ for _ , host := range data .Hosts {
115115 host := host
116- hostId := hostId
116+
117117 t .Run ("Verify-" + host .VariantInfoString (), func (t * testing.T ) {
118118 t .Parallel ()
119119
@@ -138,25 +138,15 @@ func TestObjectSync(t *testing.T) {
138138 }
139139 })
140140
141- t .Run ("Verify-SlaLifeCycle-" + fmt .Sprint (hostId ), func (t * testing.T ) {
142- eventually .Assert (t , func (t require.TestingT ) {
143- var count int
144- stmt := `SELECT COUNT(*) FROM "sla_lifecycle" INNER JOIN "host" ON "host"."id"="sla_lifecycle"."host_id" WHERE "service_id" IS NULL AND "host"."name"=?`
145- err := db .Get (& count , db .Rebind (stmt ), host .Name )
146-
147- require .NoError (t , err , "querying host sla lifecycle count should not fail" )
148- require .True (t , count == 1 , "there should be one sla lifecycle entry for host %q" , host .Name )
149- }, 20 * time .Second , 1 * time .Second )
150- })
151141 }
152142 })
153143
154144 t .Run ("Service" , func (t * testing.T ) {
155145 t .Parallel ()
156146
157- for serviceId , service := range data .Services {
147+ for _ , service := range data .Services {
158148 service := service
159- serviceId := serviceId
149+
160150 t .Run ("Verify-" + service .VariantInfoString (), func (t * testing.T ) {
161151 t .Parallel ()
162152
@@ -180,18 +170,47 @@ func TestObjectSync(t *testing.T) {
180170 })
181171 }
182172 })
173+ }
174+ })
183175
184- t .Run ("Verify-SlaLifeCycle-" + fmt .Sprint (serviceId ), func (t * testing.T ) {
185- eventually .Assert (t , func (t require.TestingT ) {
186- var count int
187- stmt := `SELECT COUNT(*) FROM "sla_lifecycle" INNER JOIN "service" ON "service"."id"="sla_lifecycle"."service_id" WHERE "service"."name" = ?`
188- err := db .Get (& count , db .Rebind (stmt ), service .Name )
176+ t .Run ("SlaLifeCycle" , func (t * testing.T ) {
177+ t .Parallel ()
189178
190- require .NoError (t , err , "querying service sla lifecycle should not fail" )
191- require .True (t , count == 1 , "there should be one sla lifecycle entry for service %q" , service .Name )
192- }, 20 * time .Second , 1 * time .Second )
193- })
194- }
179+ slinfo := & SlaLifecycle {CreateTime : types .UnixMilli (time .Now ())}
180+
181+ t .Run ("Hosts" , func (t * testing.T ) {
182+ t .Parallel ()
183+
184+ for hostId , host := range data .Hosts {
185+ host := host
186+ hostId := hostId
187+
188+ t .Run ("Verify-Host-" + fmt .Sprint (hostId ), func (t * testing.T ) {
189+ t .Parallel ()
190+
191+ eventually .Assert (t , func (t require.TestingT ) {
192+ verifySlaLifeCycleRow (t , db , slinfo , host .Name , "" )
193+ }, 20 * time .Second , 1 * time .Second )
194+ })
195+ }
196+ })
197+
198+ t .Run ("Services" , func (t * testing.T ) {
199+ t .Parallel ()
200+
201+ for serviceId , service := range data .Services {
202+ service := service
203+ serviceId := serviceId
204+
205+ t .Run ("Verify-Service-" + fmt .Sprint (serviceId ), func (t * testing.T ) {
206+ t .Parallel ()
207+
208+ eventually .Assert (t , func (t require.TestingT ) {
209+ verifySlaLifeCycleRow (t , db , slinfo , * service .HostName , service .Name )
210+ }, 20 * time .Second , 1 * time .Second )
211+ })
212+ }
213+ })
195214 })
196215
197216 t .Run ("HostGroup" , func (t * testing.T ) {
@@ -384,22 +403,6 @@ func TestObjectSync(t *testing.T) {
384403 require .NoError (t , err , "querying service count should not fail" )
385404 return count == 0
386405 }, 20 * time .Second , 1 * time .Second , "service with name=%q should be removed from database" , service .Name )
387-
388- eventually .Assert (t , func (t require.TestingT ) {
389- var expectedResult []struct {
390- CreateTime types.UnixMilli `db:"create_time"`
391- DeleteTime types.UnixMilli `db:"delete_time"`
392- }
393- stmt := `SELECT "create_time", "delete_time" FROM "sla_lifecycle"
394- INNER JOIN "service" ON "service"."id"="sla_lifecycle"."service_id"
395- WHERE "service"."name" = ?`
396- err := db .Select (& expectedResult , db .Rebind (stmt ), service .Name )
397- require .NoError (t , err , "querying service sla lifecycle should not fail" )
398-
399- require .True (t , len (expectedResult ) == 1 )
400- require .False (t , expectedResult [0 ].CreateTime .Time ().IsZero ())
401- require .False (t , expectedResult [0 ].DeleteTime .Time ().IsZero ())
402- }, 20 * time .Second , 1 * time .Second )
403406 })
404407 }
405408
@@ -457,6 +460,35 @@ func TestObjectSync(t *testing.T) {
457460 })
458461 })
459462
463+ t .Run ("SlaLifeCycle" , func (t * testing.T ) {
464+ t .Parallel ()
465+
466+ for serviceId , service := range makeTestSyncServices (t ) {
467+ service := service
468+ serviceId := serviceId
469+
470+ t .Run ("Verify-Service-" + fmt .Sprint (serviceId ), func (t * testing.T ) {
471+ t .Parallel ()
472+
473+ slinfo := & SlaLifecycle {CreateTime : types .UnixMilli (time .Now ())}
474+ client .CreateObject (t , "services" , * service .HostName + "!" + service .Name , map [string ]interface {}{
475+ "attrs" : makeIcinga2ApiAttributes (service , false ),
476+ })
477+
478+ eventually .Assert (t , func (t require.TestingT ) {
479+ verifySlaLifeCycleRow (t , db , slinfo , * service .HostName , service .Name )
480+ }, 20 * time .Second , 1 * time .Second )
481+
482+ client .DeleteObject (t , "services" , * service .HostName + "!" + service .Name , false )
483+
484+ slinfo .DeleteTime = types .UnixMilli (time .Now ())
485+ eventually .Assert (t , func (t require.TestingT ) {
486+ verifySlaLifeCycleRow (t , db , slinfo , "" , "" )
487+ }, 20 * time .Second , 1 * time .Second )
488+ })
489+ }
490+ })
491+
460492 t .Run ("User" , func (t * testing.T ) {
461493 t .Parallel ()
462494
@@ -1227,6 +1259,65 @@ func verifyIcingaDbRow(t require.TestingT, db *sqlx.DB, obj interface{}) {
12271259 require .False (t , rows .Next (), "SQL query should return only one row: %s" , query )
12281260}
12291261
1262+ func verifySlaLifeCycleRow (t require.TestingT , db * sqlx.DB , slinfo * SlaLifecycle , host string , service string ) {
1263+ query := `SELECT "create_time", "delete_time", "sla_lifecycle"."host_id", "sla_lifecycle"."service_id" FROM "sla_lifecycle"`
1264+ var args []interface {}
1265+ if slinfo .DeleteTime .Time ().IsZero () {
1266+ query += ` INNER JOIN "host" ON "host"."id"="sla_lifecycle"."host_id"`
1267+ where := ` WHERE "host"."name"=?`
1268+
1269+ args = append (args , host )
1270+ if service == "" {
1271+ where += ` AND "service_id" IS NULL`
1272+ } else {
1273+ query += ` INNER JOIN "service" ON "service"."id"="sla_lifecycle"."service_id"`
1274+ where += ` AND "service"."name"=?`
1275+ args = append (args , service )
1276+ }
1277+
1278+ query += where + ` AND "delete_time" = 0`
1279+ } else {
1280+ query += ` WHERE "host_id"=?`
1281+ args = []interface {}{slinfo .HostID }
1282+ if service == "" {
1283+ query += ` AND "service_id" IS NULL`
1284+ } else {
1285+ query += ` AND "service_id"=?`
1286+ args = append (args , slinfo .ServiceID )
1287+ }
1288+ }
1289+
1290+ var resultSet []SlaLifecycle
1291+ err := db .Select (& resultSet , db .Rebind (query ), args ... )
1292+ require .NoError (t , err , "querying sla lifecycle should not fail: Query: %q" , query )
1293+
1294+ require .Len (t , resultSet , 1 , "there should be one sla lifecycle entry" )
1295+
1296+ expected := resultSet [0 ]
1297+ slinfo .HostID = expected .HostID
1298+ slinfo .ServiceID = expected .ServiceID
1299+
1300+ zerotimestamp := time .Unix (0 , 0 )
1301+
1302+ require .NotEqual (t , zerotimestamp , expected .CreateTime .Time ())
1303+ assert .WithinDuration (t , slinfo .CreateTime .Time (), expected .CreateTime .Time (), time .Minute )
1304+
1305+ if slinfo .DeleteTime .Time ().IsZero () {
1306+ require .Equal (t , zerotimestamp , expected .DeleteTime .Time ())
1307+ } else {
1308+ require .NotEqual (t , zerotimestamp , expected .DeleteTime .Time ())
1309+ require .Less (t , expected .DeleteTime .Time (), expected .CreateTime .Time ())
1310+ assert .WithinDuration (t , slinfo .DeleteTime .Time (), expected .DeleteTime .Time (), time .Minute )
1311+ }
1312+ }
1313+
1314+ type SlaLifecycle struct {
1315+ CreateTime types.UnixMilli `db:"create_time"`
1316+ DeleteTime types.UnixMilli `db:"delete_time"`
1317+ HostID types.Binary `db:"host_id"`
1318+ ServiceID types.Binary `db:"service_id"`
1319+ }
1320+
12301321// newString allocates a new *string and initializes it. This helper function exists as
12311322// there seems to be no way to achieve this within a single statement.
12321323func newString (s string ) * string {
0 commit comments