ntfy-alertmanager/config_test.go
2023-02-20 13:39:57 +01:00

103 lines
2.2 KiB
Go

package main
import (
"os"
"path/filepath"
"reflect"
"testing"
"time"
)
func TestReadConfig(t *testing.T) {
configContent := `
base-url https://ntfy-alertmanager.xenrox.net
http-address :8080
log-level info
alert-mode multi
user webhookUser
password webhookPass
labels {
order "severity,instance"
severity "critical" {
priority 5
tags "rotating_light"
icon "https://foo.com/critical.png"
}
severity "info" {
priority 1
}
instance "example.com" {
tags "computer,example"
}
}
resolved {
tags "resolved,partying_face"
icon "https://foo.com/resolved.png"
}
ntfy {
topic https://ntfy.sh/alertmanager-alerts
user user
password pass
}
alertmanager {
silence-duration 24h
user user
password pass
url https://alertmanager.xenrox.net
}
cache {
duration 48h
}
`
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"},
labels: labels{Order: []string{"severity", "instance"},
Label: map[string]labelConfig{
"severity:critical": {Priority: "5", Tags: []string{"rotating_light"}, Icon: "https://foo.com/critical.png"},
"severity:info": {Priority: "1"},
"instance:example.com": {Tags: []string{"computer", "example"}},
},
},
cache: cacheConfig{CleanupInterval: time.Hour, Duration: 48 * time.Hour},
am: alertmanagerConfig{
SilenceDuration: time.Hour * 24,
User: "user",
Password: "pass",
URL: "https://alertmanager.xenrox.net",
},
resolved: resolvedConfig{
Tags: []string{"resolved", "partying_face"},
Icon: "https://foo.com/resolved.png",
},
}
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)
}
}