Debian Codegraph @

config/config_test.go

raw symbols SCIP
1 package config
2
3 import "testing"
4
5 func (s *Server) equal(other *Server) bool {
6 if s.Addr != other.Addr {
7 return false
8 }
9
10 if s.CAPath != other.CAPath {
11 return false
12 }
13
14 if len(s.KeyPaths) != len(other.KeyPaths) {
15 return false
16 }
17
18 if len(s.CertPaths) != len(other.KeyPaths) {
19 return false
20 }
21
22 for i := range s.KeyPaths {
23 if s.KeyPaths[i] != other.KeyPaths[i] {
24 return false
25 }
26 }
27
28 for i := range s.CertPaths {
29 if s.CertPaths[i] != other.CertPaths[i] {
30 return false
31 }
32 }
33
34 if s.Systemd != other.Systemd {
35 return false
36 }
37
38 return true
39 }
40
41 func (ui *UI) equal(other *UI) bool {
42 if ui.Root != other.Root {
43 return false
44 }
45
46 if ui.Static != other.Static {
47 return false
48 }
49
50 return true
51 }
52
53 func (hc *HipChat) equal(other *HipChat) bool {
54 if hc.Host != other.Host || hc.Room != other.Room || hc.APIKey != other.APIKey {
55 return false
56 }
57
58 return true
59 }
60
61 func (m *Metrics) equal(other *Metrics) bool {
62 return m.Host == other.Host && m.Port == other.Port
63 }
64
65 func (d *Delegations) equal(other *Delegations) bool {
66 return d.Persist == other.Persist && d.Policy == other.Policy
67 }
68
69 func (c *Config) equal(other *Config) bool {
70 if !c.Server.equal(other.Server) {
71 return false
72 }
73
74 if !c.UI.equal(other.UI) {
75 return false
76 }
77
78 if !c.HipChat.equal(other.HipChat) {
79 return false
80 }
81
82 if !c.Metrics.equal(other.Metrics) {
83 return false
84 }
85
86 if !c.Delegations.equal(other.Delegations) {
87 return false
88 }
89
90 return true
91 }
92
93 // TestEmptyEqual makes sure two empty configurations are equal.
94 func TestEmptyEqual(t *testing.T) {
95 a := New()
96 b := New()
97
98 if !a.equal(b) {
99 t.Fatal("empty configurations should be equivalent")
100 }
101 }
102
103 // TestLoadFile validates loading a configuration from disk.
104 func TestLoadFile(t *testing.T) {
105 goodConfig := "testdata/config.json"
106 badConfig := "testdata/bad_config.json"
107 expected := New()
108 expected.Server = &Server{
109 Addr: "localhost:8080",
110 KeyPaths: "testdata/server.key",
111 CertPaths: "testdata/server.pem",
112 }
113
114 _, err := Load("testdata/enoent.json")
115 if err == nil {
116 t.Fatal("attempt to load non-existent file should fail")
117 }
118
119 _, err = Load(badConfig)
120 if err == nil {
121 t.Fatal("attempt to load malformed JSON should fail")
122 }
123
124 cfg, err := Load(goodConfig)
125 if err != nil {
126 t.Fatalf("failed to load config: %s", err)
127 }
128
129 if !cfg.equal(expected) {
130 t.Fatal("loaded config is invalid")
131 }
132 }
133
134 // TestValid validates the Validate function.
135 func TestValid(t *testing.T) {
136 config := New()
137
138 if config.Valid() {
139 t.Fatal("empty config shouldn't be valid")
140 }
141
142 // Certs and no keys is an invalid config.
143 config.Server.CertPaths = "testdata/server.pem"
144 if config.Valid() {
145 t.Fatal("config shouldn't be valid")
146 }
147
148 // Keys and no certs is an invalid config.
149 config.Server.CertPaths = ""
150 config.Server.KeyPaths = "testdata/server.key"
151 if config.Valid() {
152 t.Fatal("config shouldn't be valid")
153 }
154
155 // Key pairs but no address information is an invalid config.
156 config.Server.CertPaths = "testdata/server.pem"
157 if config.Valid() {
158 t.Fatal("config shouldn't be valid")
159 }
160
161 config.Server.Addr = "localhost:8080"
162 if !config.Valid() {
163 t.Fatal("config should be valid")
164 }
165
166 config.Server.Addr = ""
167 config.Server.Systemd = true
168 if !config.Valid() {
169 t.Fatal("config should be valid")
170 }
171 }
172
173 func TestHipChatValid(t *testing.T) {
174 hc := &HipChat{}
175 if hc.Valid() {
176 t.Fatal("empty hipchat config shouldn't be valid")
177 }
178
179 hc.APIKey = "test"
180 if hc.Valid() {
181 t.Fatal("invalid hipchat config shouldn't be valid")
182 }
183
184 hc.Room = "test"
185 if hc.Valid() {
186 t.Fatal("invalid hipchat config shouldn't be valid")
187 }
188
189 hc.Host = "test"
190 if !hc.Valid() {
191 t.Fatal("valid hipchat config marked as invalid")
192 }
193 }