From 7548567992d02c1a32265f11133b39b67117db3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Mon, 16 Jan 2023 12:40:24 +0100 Subject: [PATCH] config: Add parsing test --- config_test.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 config_test.go diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..7d744af --- /dev/null +++ b/config_test.go @@ -0,0 +1,69 @@ +package main + +import ( + "os" + "path/filepath" + "reflect" + "testing" +) + +func TestReadConfig(t *testing.T) { + configContent := ` +# http listen address +http-address :8080 +# Log level (either debug, info, warning, error) +log-level info +# Optionally protect with HTTP basic authentication +user webhookUser +password webhookPass + +labels { + order "severity,instance" + + severity "critical" { + priority 5 + tags "rotating_light" + } + + severity "info" { + priority 1 + } + + instance "example.com" { + tags "computer,example" + } +} + +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 +} +` + + expectedCfg := &config{ + HTTPAddress: ":8080", LogLevel: "info", 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"}}, + "severity:info": {Priority: "1"}, + "instance:example.com": {Tags: []string{"computer", "example"}}}}} + + 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) + } +}