forked from prometheus/blackbox_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
66 lines (58 loc) · 1.41 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"strings"
"testing"
yaml "gopkg.in/yaml.v2"
)
func TestLoadConfig(t *testing.T) {
sc := &SafeConfig{
C: &Config{},
}
err := sc.reloadConfig("testdata/blackbox-good.yml")
if err != nil {
t.Errorf("Error loading config %v: %v", "blackbox.yml", err)
}
}
func TestLoadBadConfigs(t *testing.T) {
sc := &SafeConfig{
C: &Config{},
}
tests := []struct {
ConfigFile string
ExpectedError string
}{
{
ConfigFile: "testdata/blackbox-bad.yml",
ExpectedError: "unknown fields in dns probe: invalid_extra_field",
},
{
ConfigFile: "testdata/invalid-dns-module.yml",
ExpectedError: "Query name must be set for DNS module",
},
}
for i, test := range tests {
err := sc.reloadConfig(test.ConfigFile)
if err.Error() != test.ExpectedError {
t.Errorf("In case %v:\nExpected:\n%v\nGot:\n%v", i, test.ExpectedError, err.Error())
}
}
}
func TestHideConfigSecrets(t *testing.T) {
sc := &SafeConfig{
C: &Config{},
}
err := sc.reloadConfig("testdata/blackbox-good.yml")
if err != nil {
t.Errorf("Error loading config %v: %v", "testdata/blackbox-good.yml", err)
}
// String method must not reveal authentication credentials.
sc.RLock()
c, err := yaml.Marshal(sc.C)
sc.RUnlock()
if err != nil {
t.Errorf("Error marshalling config: %v", err)
}
if strings.Contains(string(c), "mysecret") {
t.Fatal("config's String method reveals authentication credentials.")
}
}