forked from elastic/logstash-forwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
210 lines (181 loc) · 5.32 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"io/ioutil"
"os"
"path"
"reflect"
"testing"
"time"
)
// -------------------------------------------------------------------
// test support funcs
// -------------------------------------------------------------------
func chkerr(t *testing.T, err error) {
if err != nil {
t.Errorf("Error encountered: %s", err)
}
}
func makeTempDir(t *testing.T) string {
tmpdir, err := ioutil.TempDir("", "logstash-config-test")
chkerr(t, err)
return tmpdir
}
func rmTempDir(tmpdir string) {
_ = os.RemoveAll(tmpdir)
}
// -------------------------------------------------------------------
// Tests
// -------------------------------------------------------------------
func TestDiscoverConfigs(t *testing.T) {
tmpdir := makeTempDir(t)
defer rmTempDir(tmpdir)
tmpfile1 := path.Join(tmpdir, "myfile1")
tmpfile2 := path.Join(tmpdir, "myfile2")
err := ioutil.WriteFile(tmpfile1, make([]byte, 0), 0644)
chkerr(t, err)
err = ioutil.WriteFile(tmpfile2, make([]byte, 0), 0644)
configs, err := DiscoverConfigs(tmpdir)
chkerr(t, err)
expected := []string{tmpfile1, tmpfile2}
if !reflect.DeepEqual(configs, expected) {
t.Fatalf("Expected to find %v, got %v instead", configs, expected)
}
configs, err = DiscoverConfigs(tmpfile1)
expected = []string{tmpfile1}
if !reflect.DeepEqual(configs, expected) {
t.Fatalf("Expected to find %v, got %v instead", configs, expected)
}
}
func TestLoadEmptyConfig(t *testing.T) {
tmpdir := makeTempDir(t)
defer rmTempDir(tmpdir)
configFile := path.Join(tmpdir, "myconfig")
err := ioutil.WriteFile(configFile, []byte(""), 0644)
chkerr(t, err)
config, err := LoadConfig(configFile)
if err != nil {
t.Fatalf("Error loading config file: %s", err)
}
if !reflect.DeepEqual(config, Config{}) {
t.Fatalf("Expected emtpy Config, got \n\n%v\n\n from LoadConfig", config)
}
}
func TestLoadConfigAndStripComments(t *testing.T) {
configJson := `
# A comment at the beginning of the line
{
# A comment after some spaces
"network": {
"servers": [ "localhost:5043" ],
"ssl certificate": "./logstash-forwarder.crt",
"ssl key": "./logstash-forwarder.key",
"ssl ca": "./logstash-forwarder.ca",
"timeout": 20
},
# A comment in the middle of the JSON
"files": [
{
"paths": [
"/var/log/*.log",
"/var/log/messages"
],
"fields": { "type": "syslog" },
"dead time": "6h"
}, {
"paths": [ "/var/log/apache2/access.log" ],
"fields": { "type": "apache" }
}
]
}`
tmpdir := makeTempDir(t)
defer rmTempDir(tmpdir)
configFile := path.Join(tmpdir, "myconfig")
err := ioutil.WriteFile(configFile, []byte(configJson), 0644)
chkerr(t, err)
config, err := LoadConfig(configFile)
if err != nil {
t.Fatalf("Error loading config file: %s", err)
}
defaultDeadTime, _ := time.ParseDuration(defaultConfig.fileDeadtime)
expected := Config{
Network: NetworkConfig{
Servers: []string{"localhost:5043"},
SSLCertificate: "./logstash-forwarder.crt",
SSLKey: "./logstash-forwarder.key",
SSLCA: "./logstash-forwarder.ca",
Timeout: 20,
},
Files: []FileConfig{{
Paths: []string{"/var/log/*.log", "/var/log/messages"},
Fields: map[string]string{"type": "syslog"},
DeadTime: "6h",
deadtime: 21600000000000,
}, {
Paths: []string{"/var/log/apache2/access.log"},
Fields: map[string]string{"type": "apache"},
DeadTime: defaultConfig.fileDeadtime,
deadtime: defaultDeadTime,
}},
}
if !reflect.DeepEqual(config, expected) {
t.Fatalf("Expected\n%v\n\ngot\n\n%v\n\nfrom LoadConfig", expected, config)
}
}
func TestFinalizeConfig(t *testing.T) {
config := Config{}
FinalizeConfig(&config)
if config.Network.Timeout != defaultConfig.netTimeout {
t.Fatalf("Expected FinalizeConfig to default timeout to %d, got %d instead", defaultConfig.netTimeout, config.Network.Timeout)
}
config.Network.Timeout = 40
expected := time.Duration(40) * time.Second
FinalizeConfig(&config)
if config.Network.timeout != expected {
t.Fatalf("Expected FinalizeConfig to set the timeout duration to %v, got %v instead", config.Network.timeout, expected)
}
}
func TestMergeConfig(t *testing.T) {
configA := Config{
Network: NetworkConfig{
Servers: []string{"localhost:5043"},
SSLCertificate: "./logstash-forwarder.crt",
SSLKey: "./logstash-forwarder.key",
},
Files: []FileConfig{{
Paths: []string{"/var/log/messagesA"},
}},
}
configB := Config{
Network: NetworkConfig{
Servers: []string{"otherhost:5043"},
SSLCA: "./logstash-forwarder.crt",
Timeout: 20,
},
Files: []FileConfig{{
Paths: []string{"/var/log/messagesB"},
}},
}
expected := Config{
Network: NetworkConfig{
Servers: []string{"localhost:5043", "otherhost:5043"},
SSLCertificate: "./logstash-forwarder.crt",
SSLKey: "./logstash-forwarder.key",
SSLCA: "./logstash-forwarder.crt",
Timeout: 20,
},
Files: []FileConfig{{
Paths: []string{"/var/log/messagesA"},
}, {
Paths: []string{"/var/log/messagesB"},
}},
}
err := MergeConfig(&configA, configB)
chkerr(t, err)
if !reflect.DeepEqual(configA, expected) {
t.Fatalf("Expected merged config to be %v, got %v instead", expected, configA)
}
err = MergeConfig(&configA, configB)
if err == nil {
t.Fatalf("Expected a double merge attempt to give us an error, it didn't")
}
}