1313// must be compiled using CGO_ENABLED=0
1414// - ICINGA_TESTING_ICINGADB_SCHEMA_MYSQL: Path to the full Icinga DB schema file for MySQL/MariaDB
1515// - ICINGA_TESTING_ICINGADB_SCHEMA_PGSQL: Path to the full Icinga DB schema file for PostgreSQL
16+ // - ICINGA_TESTING_ICINGA_NOTIFICATIONS_SHARED_DIR: Shared path between the Icinga Notifications container and the
17+ // host to, e.g., share a fifo for the file channel.
18+ // - ICINGA_TESTING_ICINGA_NOTIFICATIONS_SCHEMA_PGSQL: Path to the full Icinga Notifications PostgreSQL schema file
1619package icingatesting
1720
1821import (
1922 "context"
2023 "flag"
2124 "fmt"
25+ "os"
26+ "sync"
27+ "testing"
28+
2229 "github.com/docker/docker/api/types"
2330 "github.com/docker/docker/client"
2431 "github.com/icinga/icinga-testing/internal/services/icinga2"
2532 "github.com/icinga/icinga-testing/internal/services/icingadb"
2633 "github.com/icinga/icinga-testing/internal/services/mysql"
34+ "github.com/icinga/icinga-testing/internal/services/notifications"
2735 "github.com/icinga/icinga-testing/internal/services/postgresql"
2836 "github.com/icinga/icinga-testing/internal/services/redis"
2937 "github.com/icinga/icinga-testing/services"
3038 "github.com/icinga/icinga-testing/utils"
3139 "go.uber.org/zap"
3240 "go.uber.org/zap/zapcore"
3341 "go.uber.org/zap/zaptest"
34- "os"
35- "sync"
36- "testing"
3742)
3843
3944// IT is the core type to start interacting with this module.
@@ -50,18 +55,20 @@ import (
5055// m.Run()
5156// }
5257type IT struct {
53- mutex sync.Mutex
54- deferredCleanup []func ()
55- prefix string
56- dockerClient * client.Client
57- dockerNetworkId string
58- mysql mysql.Creator
59- postgresql postgresql.Creator
60- redis redis.Creator
61- icinga2 icinga2.Creator
62- icingaDb icingadb.Creator
63- logger * zap.Logger
64- loggerDebugCore zapcore.Core
58+ mutex sync.Mutex
59+ deferredCleanup []func ()
60+ prefix string
61+ dockerClient * client.Client
62+ dockerNetworkId string
63+ mysql mysql.Creator
64+ postgresql postgresql.Creator
65+ redis redis.Creator
66+ icinga2 icinga2.Creator
67+ icingaDb icingadb.Creator
68+ icingaNotifications notifications.Creator
69+ icingaNotificationsWebhookReceiver * services.IcingaNotificationsWebhookReceiver
70+ logger * zap.Logger
71+ loggerDebugCore zapcore.Core
6572}
6673
6774var flagDebugLog = flag .String ("icingatesting.debuglog" , "" , "file to write debug log to" )
@@ -272,9 +279,6 @@ func (it *IT) getIcingaDb() icingadb.Creator {
272279}
273280
274281// IcingaDbInstance starts a new Icinga DB instance.
275- //
276- // It expects the ICINGA_TESTING_ICINGADB_BINARY environment variable to be set to the path of a precompiled icingadb
277- // binary which is then started in a new Docker container when this function is called.
278282func (it * IT ) IcingaDbInstance (redis services.RedisServer , rdb services.RelationalDatabase , options ... services.IcingaDbOption ) services.IcingaDb {
279283 return services.IcingaDb {IcingaDbBase : it .getIcingaDb ().CreateIcingaDb (redis , rdb , options ... )}
280284}
@@ -288,6 +292,76 @@ func (it *IT) IcingaDbInstanceT(
288292 return i
289293}
290294
295+ func (it * IT ) getIcingaNotifications () notifications.Creator {
296+ shareDir , ok := os .LookupEnv ("ICINGA_TESTING_ICINGA_NOTIFICATIONS_SHARED_DIR" )
297+ if ! ok {
298+ panic ("environment variable ICINGA_TESTING_ICINGA_NOTIFICATIONS_SHARED_DIR must be set" )
299+ }
300+
301+ it .mutex .Lock ()
302+ defer it .mutex .Unlock ()
303+
304+ if it .icingaNotifications == nil {
305+ it .icingaNotifications = notifications .NewDockerCreator (
306+ it .logger , it .dockerClient , it .prefix + "-icinga-notifications" , it .dockerNetworkId , shareDir )
307+ it .deferCleanup (it .icingaNotifications .Cleanup )
308+ }
309+
310+ return it .icingaNotifications
311+ }
312+
313+ // IcingaNotificationsInstance starts a new Icinga Notifications instance.
314+ func (it * IT ) IcingaNotificationsInstance (
315+ rdb services.RelationalDatabase , options ... services.IcingaNotificationsOption ,
316+ ) services.IcingaNotifications {
317+ return services.IcingaNotifications {
318+ IcingaNotificationsBase : it .getIcingaNotifications ().CreateIcingaNotifications (rdb , options ... ),
319+ }
320+ }
321+
322+ // IcingaNotificationsInstanceT creates a new Icinga Notifications instance and registers its cleanup function with testing.T.
323+ func (it * IT ) IcingaNotificationsInstanceT (
324+ t testing.TB , rdb services.RelationalDatabase , options ... services.IcingaNotificationsOption ,
325+ ) services.IcingaNotifications {
326+ i := it .IcingaNotificationsInstance (rdb , options ... )
327+ t .Cleanup (i .Cleanup )
328+ return i
329+ }
330+
331+ func (it * IT ) getIcingaNotificationsWebhookReceiver () * services.IcingaNotificationsWebhookReceiver {
332+ it .mutex .Lock ()
333+ defer it .mutex .Unlock ()
334+
335+ if it .icingaNotificationsWebhookReceiver == nil {
336+ networkHost , err := utils .DockerNetworkHostAddress (context .Background (), it .dockerClient , it .dockerNetworkId )
337+ if err != nil {
338+ it .logger .Fatal ("cannot get docker host address" , zap .Error (err ))
339+ }
340+ port , err := utils .OpenTcpPort ()
341+ if err != nil {
342+ it .logger .Fatal ("cannot get an open TCP port" , zap .Error (err ))
343+ }
344+
345+ webhookRec , err := services .LaunchIcingaNotificationsWebhookReceiver (fmt .Sprintf ("%s:%d" , networkHost , port ))
346+ if err != nil {
347+ it .logger .Fatal ("cannot launch Icinga Notifications webhook receiver" , zap .Error (err ))
348+ }
349+
350+ it .icingaNotificationsWebhookReceiver = webhookRec
351+ it .deferCleanup (it .icingaNotificationsWebhookReceiver .Cleanup )
352+ }
353+
354+ return it .icingaNotificationsWebhookReceiver
355+ }
356+
357+ // IcingaNotificationsWebhookReceiverInstanceT creates a new Icinga Notifications Webhook Receiver instance and
358+ // registers its cleanup function with testing.T.
359+ func (it * IT ) IcingaNotificationsWebhookReceiverInstanceT (t testing.TB ) * services.IcingaNotificationsWebhookReceiver {
360+ webhookRec := it .getIcingaNotificationsWebhookReceiver ()
361+ t .Cleanup (webhookRec .Cleanup )
362+ return webhookRec
363+ }
364+
291365// Logger returns a *zap.Logger which additionally logs the current test case name.
292366func (it * IT ) Logger (t testing.TB ) * zap.Logger {
293367 cores := []zapcore.Core {zaptest .NewLogger (t , zaptest .WrapOptions (zap .IncreaseLevel (zap .InfoLevel ))).Core ()}
0 commit comments