ntfy-alertmanager/config/config_test.go
Thorben Günther 25e65db8bd
Add support for self signed ntfy certificates
A fingerprint can be specified in the configuration file. If it matches
the one from the server certificate, it will be accepted.

Closes: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/22
2023-08-25 14:57:54 +02:00

126 lines
2.9 KiB
Go

package config
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
log-format json
alert-mode multi
user webhookUser
password webhookPass
labels {
order "severity,instance"
severity "critical" {
priority 5
tags "rotating_light"
icon "https://foo.com/critical.png"
email-address foo@bar.com
call yes
}
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
certificate-fingerprint 136d2b889c5736d081b4b29c7909276292cfb86a6bd3ad4635cb7017eb996e28082ab8c6794bf62e817941981d53c807b35c245fb18eb6fb66b5ddb4d05c299
user user
password pass
}
alertmanager {
silence-duration 24h
user user
password pass
url https://alertmanager.xenrox.net
}
cache {
type redis
duration 48h
redis-url redis://user:password@localhost:6789/3
}
`
expectedCfg := &Config{
BaseURL: "https://ntfy-alertmanager.xenrox.net",
HTTPAddress: ":8080",
LogLevel: "info",
LogFormat: "json",
AlertMode: Multi,
User: "webhookUser",
Password: "webhookPass",
Ntfy: ntfyConfig{
Topic: "https://ntfy.sh/alertmanager-alerts",
User: "user",
Password: "pass",
CertFingerprint: "136d2b889c5736d081b4b29c7909276292cfb86a6bd3ad4635cb7017eb996e28082ab8c6794bf62e817941981d53c807b35c245fb18eb6fb66b5ddb4d05c299",
},
Labels: labels{Order: []string{"severity", "instance"},
Label: map[string]labelConfig{
"severity:critical": {
Priority: "5",
Tags: []string{"rotating_light"},
Icon: "https://foo.com/critical.png",
EmailAddress: "foo@bar.com",
Call: "yes",
},
"severity:info": {Priority: "1"},
"instance:example.com": {Tags: []string{"computer", "example"}},
},
},
Cache: CacheConfig{
Type: "redis",
Duration: 48 * time.Hour,
CleanupInterval: time.Hour,
RedisURL: "redis://user:password@localhost:6789/3",
},
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)
}
}