-
Notifications
You must be signed in to change notification settings - Fork 2
/
config_parser_test.go
93 lines (70 loc) · 2.67 KB
/
config_parser_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"os"
"os/exec"
"path"
"testing"
)
func openTestFile(t *testing.T, filename string) *os.File {
testDir := getTestDir(t)
sampleFile, err := os.Open(path.Join(testDir, filename))
assert(t, err == nil, fmt.Sprintf("error opening sample file: %s. this is likely a problem in the test itself", err))
return sampleFile
}
func TestGetDefaultConfig(t *testing.T) {
config := getDefaultConfig()
assert(t, config.GlobalConfig.Port == 9883, "default port should be 9883, got %d", config.GlobalConfig.Port)
}
func TestReadConfigFile(t *testing.T) {
sampleFile := openTestFile(t, "sample-config.yaml")
config, err := readConfigFile(sampleFile)
ok(t, err)
assert(t, len(config) > 0, "sample config file should not be empty")
}
func TestParseConfig(t *testing.T) {
sampleFile := openTestFile(t, "sample-config.yaml")
configFile, err := readConfigFile(sampleFile)
ok(t, err)
config, err := parseConfig(configFile)
ok(t, err)
assert(t, config.ScrapeConfig.MetricConfig.Name == "wikipedia_articles_total", "metric name should be 'wikipedia_articles_total', got: %s", config.ScrapeConfig.MetricConfig.Name)
}
func TestParseConfig_invalidParameters(t *testing.T) {
sampleFile := openTestFile(t, "sample-config-invalid-parameters.yaml")
configFile, err := readConfigFile(sampleFile)
ok(t, err)
_, err = parseConfig(configFile)
errorContains(t, err, "unmarshal errors")
}
func TestParseConfig_invalidYaml(t *testing.T) {
sampleFile := openTestFile(t, "sample-config-invalid-syntax.notyaml")
configFile, err := readConfigFile(sampleFile)
ok(t, err)
_, err = parseConfig(configFile)
errorContains(t, err, "error parsing supplied YAML")
}
func TestGetConfig(t *testing.T) {
sampleFile := openTestFile(t, "sample-config.yaml")
config := getConfig(sampleFile)
assert(t, config.GlobalConfig.Port == 9883, "getConfig should contain the sample-config.yaml file configurations")
}
func TestGetConfig_invalidConfig(t *testing.T) {
// getConfig may call os.Exit(1), so we have to get around that
// see: https://talks.golang.org/2014/testing.slide#23
// unfortunately since this is a subprocess we won't be able to get the full test coverage metric
if os.Getenv("BE_CRASHER") == "1" {
sampleFile := openTestFile(t, "sample-config-invalid-syntax.notyaml")
getConfig(sampleFile)
}
cmd := exec.Command(os.Args[0], "-test.run=^TestGetConfig_invalidConfig$")
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok {
assert(t, e.ExitCode() == 1, "process should exit with code 1, was: %d", e.ExitCode())
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}