2023-07-12 14:56:48 +02:00
|
|
|
package config
|
2023-01-16 12:40:24 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2023-02-08 14:46:14 +01:00
|
|
|
"time"
|
2023-01-16 12:40:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadConfig(t *testing.T) {
|
|
|
|
configContent := `
|
2023-02-10 17:16:13 +01:00
|
|
|
base-url https://ntfy-alertmanager.xenrox.net
|
2023-01-16 12:40:24 +01:00
|
|
|
http-address :8080
|
|
|
|
log-level info
|
2023-08-13 16:02:02 +02:00
|
|
|
log-format json
|
2023-01-20 15:03:46 +01:00
|
|
|
alert-mode multi
|
2023-01-16 12:40:24 +01:00
|
|
|
user webhookUser
|
|
|
|
password webhookPass
|
|
|
|
|
|
|
|
labels {
|
|
|
|
order "severity,instance"
|
|
|
|
|
|
|
|
severity "critical" {
|
|
|
|
priority 5
|
|
|
|
tags "rotating_light"
|
2023-02-20 13:08:59 +01:00
|
|
|
icon "https://foo.com/critical.png"
|
2023-03-09 17:02:05 +01:00
|
|
|
email-address foo@bar.com
|
2023-07-07 21:37:11 +02:00
|
|
|
call yes
|
2023-01-16 12:40:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
severity "info" {
|
|
|
|
priority 1
|
|
|
|
}
|
|
|
|
|
|
|
|
instance "example.com" {
|
|
|
|
tags "computer,example"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 17:08:58 +01:00
|
|
|
resolved {
|
|
|
|
tags "resolved,partying_face"
|
2023-02-20 13:08:59 +01:00
|
|
|
icon "https://foo.com/resolved.png"
|
2023-02-12 17:08:58 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 12:40:24 +01:00
|
|
|
ntfy {
|
|
|
|
topic https://ntfy.sh/alertmanager-alerts
|
2023-08-25 14:57:54 +02:00
|
|
|
certificate-fingerprint 136d2b889c5736d081b4b29c7909276292cfb86a6bd3ad4635cb7017eb996e28082ab8c6794bf62e817941981d53c807b35c245fb18eb6fb66b5ddb4d05c299
|
2023-01-16 12:40:24 +01:00
|
|
|
user user
|
|
|
|
password pass
|
|
|
|
}
|
2023-02-08 14:46:14 +01:00
|
|
|
|
2023-02-10 17:16:13 +01:00
|
|
|
alertmanager {
|
|
|
|
silence-duration 24h
|
2023-02-12 14:15:02 +01:00
|
|
|
user user
|
|
|
|
password pass
|
2023-02-12 14:58:37 +01:00
|
|
|
url https://alertmanager.xenrox.net
|
2023-02-10 17:16:13 +01:00
|
|
|
}
|
|
|
|
|
2023-02-08 14:46:14 +01:00
|
|
|
cache {
|
2023-03-08 22:05:15 +01:00
|
|
|
type redis
|
2023-02-08 14:46:14 +01:00
|
|
|
duration 48h
|
2023-03-08 22:17:32 +01:00
|
|
|
redis-url redis://user:password@localhost:6789/3
|
2023-02-08 14:46:14 +01:00
|
|
|
}
|
2023-01-16 12:40:24 +01:00
|
|
|
`
|
|
|
|
|
2023-07-12 14:56:48 +02:00
|
|
|
expectedCfg := &Config{
|
2023-02-10 17:16:13 +01:00
|
|
|
BaseURL: "https://ntfy-alertmanager.xenrox.net",
|
|
|
|
HTTPAddress: ":8080",
|
|
|
|
LogLevel: "info",
|
2023-08-13 16:02:02 +02:00
|
|
|
LogFormat: "json",
|
2023-07-12 14:56:48 +02:00
|
|
|
AlertMode: Multi,
|
2023-02-10 17:16:13 +01:00
|
|
|
User: "webhookUser",
|
|
|
|
Password: "webhookPass",
|
2023-08-25 14:57:54 +02:00
|
|
|
Ntfy: ntfyConfig{
|
|
|
|
Topic: "https://ntfy.sh/alertmanager-alerts",
|
|
|
|
User: "user",
|
|
|
|
Password: "pass",
|
|
|
|
CertFingerprint: "136d2b889c5736d081b4b29c7909276292cfb86a6bd3ad4635cb7017eb996e28082ab8c6794bf62e817941981d53c807b35c245fb18eb6fb66b5ddb4d05c299",
|
|
|
|
},
|
2023-07-12 14:56:48 +02:00
|
|
|
Labels: labels{Order: []string{"severity", "instance"},
|
2023-01-16 12:40:24 +01:00
|
|
|
Label: map[string]labelConfig{
|
2023-03-09 17:02:05 +01:00
|
|
|
"severity:critical": {
|
|
|
|
Priority: "5",
|
|
|
|
Tags: []string{"rotating_light"},
|
|
|
|
Icon: "https://foo.com/critical.png",
|
2023-07-12 14:56:48 +02:00
|
|
|
EmailAddress: "foo@bar.com",
|
|
|
|
Call: "yes",
|
2023-03-09 17:02:05 +01:00
|
|
|
},
|
2023-01-16 12:40:24 +01:00
|
|
|
"severity:info": {Priority: "1"},
|
2023-02-08 14:46:14 +01:00
|
|
|
"instance:example.com": {Tags: []string{"computer", "example"}},
|
|
|
|
},
|
|
|
|
},
|
2023-07-12 19:00:25 +02:00
|
|
|
Cache: CacheConfig{
|
|
|
|
Type: "redis",
|
2023-03-08 22:05:15 +01:00
|
|
|
Duration: 48 * time.Hour,
|
2023-03-08 22:17:32 +01:00
|
|
|
CleanupInterval: time.Hour,
|
|
|
|
RedisURL: "redis://user:password@localhost:6789/3",
|
2023-03-08 22:05:15 +01:00
|
|
|
},
|
2023-07-12 14:56:48 +02:00
|
|
|
Am: alertmanagerConfig{
|
2023-02-12 14:15:02 +01:00
|
|
|
SilenceDuration: time.Hour * 24,
|
|
|
|
User: "user",
|
|
|
|
Password: "pass",
|
2023-02-12 14:58:37 +01:00
|
|
|
URL: "https://alertmanager.xenrox.net",
|
2023-02-12 14:15:02 +01:00
|
|
|
},
|
2023-07-12 14:56:48 +02:00
|
|
|
Resolved: resolvedConfig{
|
2023-02-12 17:08:58 +01:00
|
|
|
Tags: []string{"resolved", "partying_face"},
|
2023-02-20 13:08:59 +01:00
|
|
|
Icon: "https://foo.com/resolved.png",
|
2023-02-12 17:08:58 +01:00
|
|
|
},
|
2023-02-08 14:46:14 +01:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-12 14:56:48 +02:00
|
|
|
cfg, err := ReadConfig(configPath)
|
2023-01-16 12:40:24 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|