File: config_test.go

package info (click to toggle)
golang-github-cloudflare-redoctober 0.0~git20161017.0.78e9720-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 636 kB
  • sloc: sh: 65; makefile: 7
file content (193 lines) | stat: -rw-r--r-- 3,688 bytes parent folder | download | duplicates (3)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package config

import "testing"

func (s *Server) equal(other *Server) bool {
	if s.Addr != other.Addr {
		return false
	}

	if s.CAPath != other.CAPath {
		return false
	}

	if len(s.KeyPaths) != len(other.KeyPaths) {
		return false
	}

	if len(s.CertPaths) != len(other.KeyPaths) {
		return false
	}

	for i := range s.KeyPaths {
		if s.KeyPaths[i] != other.KeyPaths[i] {
			return false
		}
	}

	for i := range s.CertPaths {
		if s.CertPaths[i] != other.CertPaths[i] {
			return false
		}
	}

	if s.Systemd != other.Systemd {
		return false
	}

	return true
}

func (ui *UI) equal(other *UI) bool {
	if ui.Root != other.Root {
		return false
	}

	if ui.Static != other.Static {
		return false
	}

	return true
}

func (hc *HipChat) equal(other *HipChat) bool {
	if hc.Host != other.Host || hc.Room != other.Room || hc.APIKey != other.APIKey {
		return false
	}

	return true
}

func (m *Metrics) equal(other *Metrics) bool {
	return m.Host == other.Host && m.Port == other.Port
}

func (d *Delegations) equal(other *Delegations) bool {
	return d.Persist == other.Persist && d.Policy == other.Policy
}

func (c *Config) equal(other *Config) bool {
	if !c.Server.equal(other.Server) {
		return false
	}

	if !c.UI.equal(other.UI) {
		return false
	}

	if !c.HipChat.equal(other.HipChat) {
		return false
	}

	if !c.Metrics.equal(other.Metrics) {
		return false
	}

	if !c.Delegations.equal(other.Delegations) {
		return false
	}

	return true
}

// TestEmptyEqual makes sure two empty configurations are equal.
func TestEmptyEqual(t *testing.T) {
	a := New()
	b := New()

	if !a.equal(b) {
		t.Fatal("empty configurations should be equivalent")
	}
}

// TestLoadFile validates loading a configuration from disk.
func TestLoadFile(t *testing.T) {
	goodConfig := "testdata/config.json"
	badConfig := "testdata/bad_config.json"
	expected := New()
	expected.Server = &Server{
		Addr:      "localhost:8080",
		KeyPaths:  "testdata/server.key",
		CertPaths: "testdata/server.pem",
	}

	_, err := Load("testdata/enoent.json")
	if err == nil {
		t.Fatal("attempt to load non-existent file should fail")
	}

	_, err = Load(badConfig)
	if err == nil {
		t.Fatal("attempt to load malformed JSON should fail")
	}

	cfg, err := Load(goodConfig)
	if err != nil {
		t.Fatalf("failed to load config: %s", err)
	}

	if !cfg.equal(expected) {
		t.Fatal("loaded config is invalid")
	}
}

// TestValid validates the Validate function.
func TestValid(t *testing.T) {
	config := New()

	if config.Valid() {
		t.Fatal("empty config shouldn't be valid")
	}

	// Certs and no keys is an invalid config.
	config.Server.CertPaths = "testdata/server.pem"
	if config.Valid() {
		t.Fatal("config shouldn't be valid")
	}

	// Keys and no certs is an invalid config.
	config.Server.CertPaths = ""
	config.Server.KeyPaths = "testdata/server.key"
	if config.Valid() {
		t.Fatal("config shouldn't be valid")
	}

	// Key pairs but no address information is an invalid config.
	config.Server.CertPaths = "testdata/server.pem"
	if config.Valid() {
		t.Fatal("config shouldn't be valid")
	}

	config.Server.Addr = "localhost:8080"
	if !config.Valid() {
		t.Fatal("config should be valid")
	}

	config.Server.Addr = ""
	config.Server.Systemd = true
	if !config.Valid() {
		t.Fatal("config should be valid")
	}
}

func TestHipChatValid(t *testing.T) {
	hc := &HipChat{}
	if hc.Valid() {
		t.Fatal("empty hipchat config shouldn't be valid")
	}

	hc.APIKey = "test"
	if hc.Valid() {
		t.Fatal("invalid hipchat config shouldn't be valid")
	}

	hc.Room = "test"
	if hc.Valid() {
		t.Fatal("invalid hipchat config shouldn't be valid")
	}

	hc.Host = "test"
	if !hc.Valid() {
		t.Fatal("valid hipchat config marked as invalid")
	}
}