-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_test.go
115 lines (99 loc) · 3.06 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package netbox2dns
import (
"testing"
)
func TestFindConfig(t *testing.T) {
dirs := []string{"./testdata/config1", "./testdata/config2", "./testdata/config3"}
want1 := "./testdata/config1/conf.yaml"
got, err := findConfig("conf", dirs, configExtensions)
if err != nil {
t.Fatalf("findConfig() returned an error: %v", err)
}
if got != want1 {
t.Errorf("findConfig(): got %q, want %q", got, want1)
}
want2 := "./testdata/config2/conf.json"
got, err = findConfig("conf", dirs, []string{"json"})
if err != nil {
t.Fatalf("findConfig() returned an error: %v", err)
}
if got != want2 {
t.Errorf("findConfig(): got %q, want %q", got, want2)
}
want3 := "./testdata/config3/conf.cue"
got, err = findConfig("conf", dirs, []string{"cue"})
if err != nil {
t.Fatalf("findConfig() returned an error: %v", err)
}
if got != want3 {
t.Errorf("findConfig(): got %q, want %q", got, want3)
}
want4 := "./testdata/config2/conf.json"
got, err = findConfig("conf", dirs, []string{"json", "cue"})
if err != nil {
t.Fatalf("findConfig() returned an error: %v", err)
}
if got != want4 {
t.Errorf("findConfig(): got %q, want %q", got, want4)
}
}
func TestParseYaml(t *testing.T) {
cfg, err := ParseConfig("testdata/config4/conf.yaml")
valueTest(t, cfg, err)
}
func TestParseJSON(t *testing.T) {
cfg, err := ParseConfig("testdata/config4/conf.json")
valueTest(t, cfg, err)
}
func TestParseCUE(t *testing.T) {
cfg, err := ParseConfig("testdata/config4/conf.json")
valueTest(t, cfg, err)
}
func valueTest(t *testing.T, cfg *Config, err error) {
if err != nil {
t.Fatalf("Unable to parse config: %v", err)
}
if cfg.Netbox.Host != "netbox.example.com" {
t.Errorf("cfg.Netbox.Host wrong; got %q want %q", cfg.Netbox.Host, "netbox.example.com")
}
if cfg.Netbox.Token != "changeme" {
t.Errorf("cfg.Netbox.Host wrong; got %q want %q", cfg.Netbox.Token, "changeme")
}
if cfg.Defaults.Project != "random-string" {
t.Errorf("cfg.Defaults.Project wrong; got %q want %q", cfg.Defaults.Project, "random-string")
}
if len(cfg.Zones) != 4 {
t.Errorf("len(cfg.Zones) wrong; got %d want 4", len(cfg.Zones))
}
if len(cfg.ZoneMap) != 4 {
t.Errorf("len(cfg.ZoneMap) wrong; got %d want 4", len(cfg.Zones))
}
z := cfg.ZoneMap["0.0.0.0.ip6.arpa"]
if z == nil {
t.Fatalf("Failed to find zone for 0.0.0.0.ip6.arpa")
}
if z.Name != "0.0.0.0.ip6.arpa" {
t.Errorf("z.Name wrong; got %q want %q", z.Name, "0.0.0.0.ip6.arpa")
}
if z.ZoneType != "clouddns" {
t.Errorf("z.ZoneType wrong; got %q want %q", z.ZoneType, "clouddns")
}
if z.ZoneName != "reverse-v6-0000" {
t.Errorf("z.ZoneName wrong; got %q want %q", z.ZoneName, "reverse-6-0000")
}
if z.TTL != 300 {
t.Errorf("z.TTL wrong; got %d want 300", z.TTL)
}
if z.Project != "random-string" {
t.Errorf("z.Project wrong; got %q want %q", z.Project, "random-string")
}
if z.DeleteEntries != true {
t.Errorf("z.DeleteEntries wrong; want true")
}
}
func TestValidateYaml(t *testing.T) {
_, err := ParseConfig("testdata/config5/conf1.yaml")
if err == nil {
t.Errorf("Should have failed validation, but succeeded.")
}
}