ntfy-alertmanager/config_test.go

123 lines
3.4 KiB
Go
Raw Normal View History

2023-01-16 12:40:24 +01:00
package main
import (
"os"
"path/filepath"
"reflect"
"testing"
"time"
2023-01-16 12:40:24 +01:00
)
func TestReadConfig(t *testing.T) {
configContent := `
# Public facing base URL of the service (e.g. https://ntfy-alertmanager.xenrox.net)
# This setting is required for the "Silence" feature.
base-url https://ntfy-alertmanager.xenrox.net
2023-01-16 12:40:24 +01:00
# http listen address
http-address :8080
# Log level (either debug, info, warning, error)
log-level info
2023-01-20 15:03:46 +01:00
# When multiple alerts are grouped together by Alertmanager, they can either be sent
# each on their own (single mode) or be kept together (multi mode) (either single or multi; default is single)
alert-mode multi
2023-01-16 12:40:24 +01:00
# Optionally protect with HTTP basic authentication
user webhookUser
password webhookPass
labels {
order "severity,instance"
severity "critical" {
priority 5
tags "rotating_light"
icon "https://foo.com/critical.png"
2023-01-16 12:40:24 +01:00
}
severity "info" {
priority 1
}
instance "example.com" {
tags "computer,example"
}
}
resolved {
tags "resolved,partying_face"
icon "https://foo.com/resolved.png"
}
2023-01-16 12:40:24 +01:00
ntfy {
# URL of the ntfy topic - required
topic https://ntfy.sh/alertmanager-alerts
# ntfy access control (https://ntfy.sh/docs/config/#access-control)
user user
password pass
}
alertmanager {
# If set, the ntfy message will contain a "Silence" button, which can be used
# to create a silence via the Alertmanager API. Because of limitations in ntfy,
# the request will be proxied through ntfy-alertmanager. Therefore ntfy-alertmanager
# needs to be exposed to external network requests and base-url has to be set.
silence-duration 24h
2023-02-12 14:15:02 +01:00
# Basic authentication (https://prometheus.io/docs/alerting/latest/https/)
user user
password pass
url https://alertmanager.xenrox.net
}
# When the alert-mode is set to single, ntfy-alertmanager will cache each single alert
# to avoid sending recurrences.
cache {
# How long messages stay in the cache for
duration 48h
# Interval in which the cache is cleaned up
# cleanup-interval 1h
}
2023-01-16 12:40:24 +01:00
`
expectedCfg := &config{
BaseURL: "https://ntfy-alertmanager.xenrox.net",
HTTPAddress: ":8080",
LogLevel: "info",
alertMode: multi,
User: "webhookUser",
Password: "webhookPass",
ntfy: ntfyConfig{Topic: "https://ntfy.sh/alertmanager-alerts", User: "user", Password: "pass"},
2023-01-16 12:40:24 +01:00
labels: labels{Order: []string{"severity", "instance"},
Label: map[string]labelConfig{
"severity:critical": {Priority: "5", Tags: []string{"rotating_light"}, Icon: "https://foo.com/critical.png"},
2023-01-16 12:40:24 +01:00
"severity:info": {Priority: "1"},
"instance:example.com": {Tags: []string{"computer", "example"}},
},
},
cache: cacheConfig{CleanupInterval: time.Hour, Duration: 48 * time.Hour},
2023-02-12 14:15:02 +01:00
am: alertmanagerConfig{
SilenceDuration: time.Hour * 24,
User: "user",
Password: "pass",
URL: "https://alertmanager.xenrox.net",
2023-02-12 14:15:02 +01:00
},
resolved: resolvedConfig{
Tags: []string{"resolved", "partying_face"},
Icon: "https://foo.com/resolved.png",
},
}
2023-01-16 12:40:24 +01:00
configPath := filepath.Join(t.TempDir(), "config")
err := os.WriteFile(configPath, []byte(configContent), 0600)
if err != nil {
t.Errorf("failed to write config file: %v", err)
}
cfg, err := readConfig(configPath)
if err != nil {
t.Errorf("failed to read config file: %v", err)
}
if !reflect.DeepEqual(cfg, expectedCfg) {
t.Errorf("expected: %v, got %v", expectedCfg, cfg)
}
}