forked from hashicorp/consul-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
275 lines (237 loc) · 7.1 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"fmt"
"os"
"path"
"reflect"
"strings"
"testing"
"time"
)
// Test that an empty config does nothing
func TestMerge_emptyConfig(t *testing.T) {
consul := "consul.io:8500"
config := &Config{Consul: consul}
config.Merge(&Config{})
if config.Consul != consul {
t.Fatalf("expected %q to equal %q", config.Consul, consul)
}
}
// Test that simple values are merged
func TestMerge_simpleConfig(t *testing.T) {
config, newConsul := &Config{Consul: "consul.io:8500"}, "packer.io:7300"
config.Merge(&Config{Consul: newConsul})
if config.Consul != newConsul {
t.Fatalf("expected %q to equal %q", config.Consul, newConsul)
}
}
// Test that complex values are merged
func TestMerge_complexConfig(t *testing.T) {
config := &Config{
ConfigTemplates: []*ConfigTemplate{
&ConfigTemplate{
Source: "a",
Destination: "b",
},
&ConfigTemplate{
Source: "c",
Destination: "d",
Command: "e",
},
},
}
otherConfig := &Config{
ConfigTemplates: []*ConfigTemplate{
&ConfigTemplate{
Source: "f",
Destination: "g",
Command: "h",
},
&ConfigTemplate{
Source: "i",
Destination: "j",
},
},
}
config.Merge(otherConfig)
if !reflect.DeepEqual(config.ConfigTemplates, otherConfig.ConfigTemplates) {
t.Fatalf("expected %q to equal %q", config.ConfigTemplates, otherConfig.ConfigTemplates)
}
}
// Test that file read errors are propagated up
func TestParseConfig_readFileError(t *testing.T) {
_, err := ParseConfig(path.Join(os.TempDir(), "config.json"))
if err == nil {
t.Fatal("expected error, but nothing was returned")
}
expectedErr := "no such file or directory"
if !strings.Contains(err.Error(), expectedErr) {
t.Fatalf("expected error %q to contain %q", err.Error(), expectedErr)
}
}
// Test that parser errors are propagated up
func TestParseConfig_parseFileError(t *testing.T) {
configFile := createTempfile([]byte(`
invalid file in here
`), t)
defer deleteTempfile(configFile, t)
_, err := ParseConfig(configFile.Name())
if err == nil {
t.Fatal("expected error, but nothing was returned")
}
expectedErr := "syntax error"
if !strings.Contains(err.Error(), expectedErr) {
t.Fatalf("expected error %q to contain %q", err.Error(), expectedErr)
}
}
// Test that mapstructure errors are propagated up
func TestParseConfig_mapstructureError(t *testing.T) {
configFile := createTempfile([]byte(`
consul = true
`), t)
defer deleteTempfile(configFile, t)
_, err := ParseConfig(configFile.Name())
if err == nil {
t.Fatal("expected error, but nothing was returned")
}
expectedErr := "nconvertible type 'bool'"
if !strings.Contains(err.Error(), expectedErr) {
t.Fatalf("expected error %q to contain %q", err.Error(), expectedErr)
}
}
// Test that the config is parsed correctly
func TestParseConfig_correctValues(t *testing.T) {
configFile := createTempfile([]byte(`
consul = "nyc1.demo.consul.io"
token = "abcd1234"
wait = "5s:10s"
template {
source = "nginx.conf.ctmpl"
destination = "/etc/nginx/nginx.conf"
}
template {
source = "redis.conf.ctmpl"
destination = "/etc/redis/redis.conf"
command = "service redis restart"
}
`), t)
defer deleteTempfile(configFile, t)
config, err := ParseConfig(configFile.Name())
if err != nil {
t.Fatal(err)
}
expected := &Config{
Path: configFile.Name(),
Consul: "nyc1.demo.consul.io",
Token: "abcd1234",
Wait: &Wait{
Min: time.Second * 5,
Max: time.Second * 10,
},
WaitRaw: "5s:10s",
ConfigTemplates: []*ConfigTemplate{
&ConfigTemplate{
Source: "nginx.conf.ctmpl",
Destination: "/etc/nginx/nginx.conf",
},
&ConfigTemplate{
Source: "redis.conf.ctmpl",
Destination: "/etc/redis/redis.conf",
Command: "service redis restart",
},
},
}
if !reflect.DeepEqual(config, expected) {
t.Fatalf("expected \n%#v\n\n, got \n\n%#v", expected, config)
}
}
// Test that ParseWait errors are propagated up
func TestParseConfig_parseWaitError(t *testing.T) {
configFile := createTempfile([]byte(`
wait = "not_valid:duration"
`), t)
defer deleteTempfile(configFile, t)
_, err := ParseConfig(configFile.Name())
if err == nil {
t.Fatal("expected error, but nothing was returned")
}
expectedErr := "invalid duration not_valid"
if !strings.Contains(err.Error(), expectedErr) {
t.Fatalf("expected error %q to contain %q", err.Error(), expectedErr)
}
}
// Test that an error is returned when the empty string is given
func TestParseConfigTemplate_emptyStringArgs(t *testing.T) {
_, err := ParseConfigTemplate("")
if err == nil {
t.Fatal("expected error, but nothing was returned")
}
expectedErr := "cannot specify empty template declaration"
if !strings.Contains(err.Error(), expectedErr) {
t.Fatalf("expected error %q to contain %q", err.Error(), expectedErr)
}
}
// Test that an error is returned when a string with spaces is given
func TestParseConfigTemplate_stringWithSpacesArgs(t *testing.T) {
_, err := ParseConfigTemplate(" ")
if err == nil {
t.Fatal("expected error, but nothing was returned")
}
expectedErr := "cannot specify empty template declaration"
if !strings.Contains(err.Error(), expectedErr) {
t.Fatalf("expected error %q to contain %q", err.Error(), expectedErr)
}
}
// Test that an error is returned when there are too many arguments
func TestParseConfigurationTemplate_tooManyArgs(t *testing.T) {
_, err := ParseConfigTemplate("foo:bar:blitz:baz")
if err == nil {
t.Fatal("expected error, but nothing was returned")
}
expectedErr := "invalid template declaration format"
if !strings.Contains(err.Error(), expectedErr) {
t.Fatalf("expected error %q to contain %q", err.Error(), expectedErr)
}
}
// Test that a source value is correctly used
func TestParseConfigurationTemplate_source(t *testing.T) {
source := "/tmp/config.ctmpl"
template, err := ParseConfigTemplate(source)
if err != nil {
t.Fatal(err)
}
if template.Source != source {
t.Errorf("expected %q to equal %q", template.Source, source)
}
}
// Test that a destination wait value is correctly used
func TestParseConfigurationTemplate_destination(t *testing.T) {
source, destination := "/tmp/config.ctmpl", "/tmp/out"
template, err := ParseConfigTemplate(fmt.Sprintf("%s:%s", source, destination))
if err != nil {
t.Fatal(err)
}
if template.Source != source {
t.Errorf("expected %q to equal %q", template.Source, source)
}
if template.Destination != destination {
t.Errorf("expected %q to equal %q", template.Destination, destination)
}
}
// Test that a command wait value is correctly used
func TestParseConfigurationTemplate_command(t *testing.T) {
source, destination, command := "/tmp/config.ctmpl", "/tmp/out", "reboot"
template, err := ParseConfigTemplate(fmt.Sprintf("%s:%s:%s", source, destination, command))
if err != nil {
t.Fatal(err)
}
if template.Source != source {
t.Errorf("expected %q to equal %q", template.Source, source)
}
if template.Destination != destination {
t.Errorf("expected %q to equal %q", template.Destination, destination)
}
if template.Command != command {
t.Errorf("expected %q to equal %q", template.Command, command)
}
}