aboutsummaryrefslogtreecommitdiff
path: root/server_config_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2025-08-29 01:37:28 +0700
committerShulhan <ms@kilabit.info>2025-09-26 13:59:58 +0700
commit62161730b5802c4206f0cc169a744b364576bb74 (patch)
tree914389da9748b3b7cff731357deff7026273d22f /server_config_test.go
parentb6530117f0dcc554114bfac8b86d9fb1fde605c4 (diff)
downloadlilin-62161730b5802c4206f0cc169a744b364576bb74.tar.xz
all: implement notification using Mattermost incoming webhook
In the server configuration, one can defined the following section to send the notification using Mattermost, [notif] kind = mattermost webhook_url = # The incoming webhook URL. channel = # The channel where the notification will be placed. down_template = # Message template when service is down. up_template = # Message template when service is up.
Diffstat (limited to 'server_config_test.go')
-rw-r--r--server_config_test.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/server_config_test.go b/server_config_test.go
new file mode 100644
index 0000000..bb30e5c
--- /dev/null
+++ b/server_config_test.go
@@ -0,0 +1,81 @@
+// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info>
+// SPDX-License-Identifier: GPL-3.0-only
+
+package lilin
+
+import (
+ "net/url"
+ "testing"
+
+ "git.sr.ht/~shulhan/pakakeh.go/lib/test"
+)
+
+func TestServerConfig_init(t *testing.T) {
+ type testCase struct {
+ baseDir string
+ exp ServerConfig
+ }
+
+ var webhookURL12000 *url.URL
+ var webhookURL12001 *url.URL
+ var err error
+
+ webhookURL12000, err = url.Parse(`http://127.0.0.1:12000`)
+ if err != nil {
+ t.Fatal(err)
+ }
+ webhookURL12001, err = url.Parse(`http://127.0.0.1:12001`)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var listCase = []testCase{{
+ baseDir: `testdata/server_config/ok/`,
+ exp: ServerConfig{
+ BaseDir: `testdata/server_config/ok/`,
+ configDir: `testdata/server_config/ok/etc/lilin`,
+ configServiceDir: `testdata/server_config/ok/etc/lilin/service.d`,
+ logServiceDir: `testdata/server_config/ok/var/log/lilin/service.d`,
+ Address: `127.0.0.1:12121`,
+ Notifs: []*NotifConfig{{
+ Kind: `mattermost`,
+ WebhookURL: `http://127.0.0.1:12000`,
+ Channel: `chan_1`,
+ UpTemplate: `Service {{.ID}} is up again!`,
+ DownTemplate: `Service {{.ID}} is down: {{.Error}}`,
+ webhookURL: webhookURL12000,
+ }, {
+ Kind: `mattermost`,
+ WebhookURL: `http://127.0.0.1:12001`,
+ Channel: `chan_2`,
+ UpTemplate: `Service {{.ID}} is alive!`,
+ DownTemplate: `Service {{.ID}} is down: {{.Error}}`,
+ webhookURL: webhookURL12001,
+ }},
+ },
+ }}
+
+ for _, tcase := range listCase {
+ var cfg = ServerConfig{
+ BaseDir: tcase.baseDir,
+ }
+
+ var err = cfg.init()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ for x, notifCfg := range cfg.Notifs {
+ if notifCfg.upTmpl == nil {
+ t.Fatalf(`ServerConfig.Notifs #%d: upTmpl is nil`, x)
+ }
+ if notifCfg.downTmpl == nil {
+ t.Fatalf(`ServerConfig.Notifs #%d: downTmpl is nil`, x)
+ }
+ notifCfg.upTmpl = nil
+ notifCfg.downTmpl = nil
+ }
+
+ test.Assert(t, tcase.baseDir, tcase.exp, cfg)
+ }
+}