More config test coverage for both legacy and current paths

This commit is contained in:
Łukasz Mierzwa
2017-12-03 11:34:12 -08:00
parent e23b00c080
commit 7fa4230a1d
3 changed files with 140 additions and 26 deletions

8
Gopkg.lock generated
View File

@@ -139,6 +139,12 @@
revision = "16398bac157da96aa88f98a2df640c7f32af1da2"
version = "v1.0.1"
[[projects]]
name = "github.com/pmezard/go-difflib"
packages = ["difflib"]
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
version = "v1.0.0"
[[projects]]
name = "github.com/prometheus/client_golang"
packages = ["prometheus","prometheus/promhttp"]
@@ -244,6 +250,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "a6c129dbdbe3ff34769c04d2d246c402f2394c6a4929beeca43db05cc244a502"
inputs-digest = "7c17fa5806664243cc2c060fa70cbc4f9918793787662f2891b041a631724f1e"
solver-name = "gps-cdcl"
solver-version = 1

View File

@@ -3,54 +3,163 @@ package config
import (
"os"
"testing"
"time"
"github.com/cloudflare/unsee/internal/slices"
"github.com/pmezard/go-difflib/difflib"
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
// unset all unsee supported env variables before tests so we start with no
// config from previous test run
func resetEnv() {
unseeEnvVariables := []string{
"ALERTMANAGER_INTERVAL",
"ALERTMANAGER_URI",
"ANNOTATIONS_DEFAULT_HIDDEN",
"ANNOTATIONS_HIDDEN",
"ANNOTATIONS_VISIBLE",
"CONFIG_DIR",
"CONFIG_FILE",
"DEBUG",
"FILTERS_DEFAULT",
"LABELS_COLOR_STATIC",
"LABELS_COLOR_UNIQUE",
"LABELS_KEEP",
"LABELS_STRIP",
"LISTEN_ADDRESS",
"LISTEN_PORT",
"LISTEN_PREFIX",
"LOG_CONFIG",
"LOG_LEVEL",
"RECEIVERS_KEEP",
"RECEIVERS_STRIP",
"SENTRY_PRIVATE",
"SENTRY_PUBLIC",
"HOST",
"PORT",
"SENTRY_DSN",
}
for _, env := range unseeEnvVariables {
os.Unsetenv(env)
}
}
func testReadConfig(t *testing.T) {
if Config.Alertmanager.Interval != time.Second {
t.Errorf("Config.Alertmanager.Interval is invalid, expected 1s, got %v", Config.Alertmanager.Interval)
}
if Config.Debug != true {
t.Errorf("Config.Debug is %v with env DEBUG=true set", Config.Debug)
}
if !slices.StringInSlice(Config.Labels.Color.Static, "a") {
t.Errorf("Config.Colors.Labels.Static is missing value 'a': %v", Config.Labels.Color.Static)
}
if !slices.StringInSlice(Config.Labels.Color.Static, "bb") {
t.Errorf("Config.Colors.Labels.Static is missing value 'bb': %v", Config.Labels.Color.Static)
}
if !slices.StringInSlice(Config.Labels.Color.Static, "ccc") {
t.Errorf("Config.Colors.Labels.Static is missing value 'ccc': %v", Config.Labels.Color.Static)
}
if Config.Listen.Port != 8080 {
t.Errorf("Config.Listen.Port is invalid, expected 8080, got %v", Config.Listen.Port)
}
if len(Config.Labels.Keep) != 0 {
t.Errorf("Config.Labels.Keep is not empty, got %v", Config.Labels.Keep)
expectedConfig := `alertmanager:
interval: 1s
servers:
- name: default
uri: http://localhost
timeout: 40s
annotations:
default:
hidden: true
hidden: []
visible:
- summary
debug: true
filters:
default:
- '@state=active'
- foo=bar
labels:
keep:
- foo
- bar
strip:
- abc
- def
color:
static:
- a
- bb
- ccc
unique:
- f
- gg
listen:
address: 0.0.0.0
port: 80
prefix: /
log:
config: true
level: info
jira:
- regex: DEVOPS-[0-9]+
uri: https://jira.example.com
- regex: FOO-[0-9]+
uri: https://foo.example.com
receivers:
keep: []
strip: []
sentry:
private: secret key
public: public key
`
configDump, err := yaml.Marshal(Config)
if err != nil {
t.Error(err)
}
if string(configDump) != expectedConfig {
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(expectedConfig),
B: difflib.SplitLines(string(configDump)),
FromFile: "Expected",
ToFile: "Current",
Context: 3,
}
text, err := difflib.GetUnifiedDiffString(diff)
if err != nil {
t.Error(err)
}
t.Errorf("Config mismatch:\n%s", text)
}
}
func TestReadConfigLegacy(t *testing.T) {
resetEnv()
log.SetLevel(log.ErrorLevel)
os.Setenv("ALERTMANAGER_TTL", "1s")
os.Setenv("ALERTMANAGER_URIS", "default:http://localhost")
os.Setenv("DEBUG", "true")
os.Setenv("ANNOTATIONS_DEFAULT_HIDDEN", "true")
os.Setenv("ANNOTATIONS_VISIBLE", "summary")
os.Setenv("COLOR_LABELS_STATIC", "a bb ccc")
os.Setenv("COLOR_LABELS_UNIQUE", "f gg")
os.Setenv("DEBUG", "true")
os.Setenv("FILTER_DEFAULT", "@state=active,foo=bar")
os.Setenv("JIRA_REGEX", "DEVOPS-[0-9]+@https://jira.example.com FOO-[0-9]+@https://foo.example.com")
os.Setenv("KEEP_LABELS", "foo bar")
os.Setenv("STRIP_LABELS", "abc def")
os.Setenv("SENTRY_DSN", "secret key")
os.Setenv("SENTRY_PUBLIC_DSN", "public key")
os.Setenv("HOST", "0.0.0.0")
os.Setenv("PORT", "80")
Config.Read()
testReadConfig(t)
}
func TestReadConfig(t *testing.T) {
resetEnv()
log.SetLevel(log.ErrorLevel)
os.Setenv("ALERTMANAGER_INTERVAL", "1s")
os.Setenv("ALERTMANAGER_URIS", "default:http://localhost")
os.Setenv("ALERTMANAGER_URI", "http://localhost")
os.Setenv("ANNOTATIONS_DEFAULT_HIDDEN", "true")
os.Setenv("ANNOTATIONS_VISIBLE", "summary")
os.Setenv("DEBUG", "true")
os.Setenv("FILTERS_DEFAULT", "@state=active foo=bar")
os.Setenv("JIRA_REGEX", "DEVOPS-[0-9]+@https://jira.example.com FOO-[0-9]+@https://foo.example.com")
os.Setenv("LABELS_COLOR_STATIC", "a bb ccc")
os.Setenv("LABELS_COLOR_UNIQUE", "f gg")
os.Setenv("LABELS_KEEP", "foo bar")
os.Setenv("LABELS_STRIP", "abc def")
os.Setenv("LISTEN_ADDRESS", "0.0.0.0")
os.Setenv("LISTEN_PORT", "80")
os.Setenv("SENTRY_PRIVATE", "secret key")
os.Setenv("SENTRY_PUBLIC", "public key")
Config.Read()
testReadConfig(t)
}

View File

@@ -17,7 +17,6 @@ func (config *configSchema) legacyEnvs(v *viper.Viper) {
v.BindEnv("annotations.visible", "ANNOTATIONS_SHOW")
v.BindEnv("labels.color.static", "COLOR_LABELS_STATIC")
v.BindEnv("labels.color.unique", "COLOR_LABELS_UNIQUE")
v.BindEnv("filters.default", "FILTER_DEFAULT")
v.BindEnv("labels.keep", "KEEP_LABELS")
v.BindEnv("labels.strip", "STRIP_LABELS")
v.BindEnv("listen.prefix", "WEB_PREFIX")