-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-conf_loader_spec.lua
374 lines (351 loc) · 13.3 KB
/
02-conf_loader_spec.lua
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
local conf_loader = require "kong.conf_loader"
local helpers = require "spec.helpers"
describe("Configuration loader", function()
it("loads the defaults", function()
local conf = assert(conf_loader())
assert.is_string(conf.lua_package_path)
assert.equal("auto", conf.nginx_worker_processes)
assert.equal("0.0.0.0:8001", conf.admin_listen)
assert.equal("0.0.0.0:8000", conf.proxy_listen)
assert.equal("0.0.0.0:8443", conf.proxy_listen_ssl)
assert.is_nil(conf.ssl_cert) -- check placeholder value
assert.is_nil(conf.ssl_cert_key)
assert.is_nil(getmetatable(conf))
end)
it("loads a given file, with higher precedence", function()
local conf = assert(conf_loader(helpers.test_conf_path))
-- defaults
assert.equal("on", conf.nginx_daemon)
-- overrides
assert.equal("1", conf.nginx_worker_processes)
assert.equal("0.0.0.0:9001", conf.admin_listen)
assert.equal("0.0.0.0:9000", conf.proxy_listen)
assert.equal("0.0.0.0:9443", conf.proxy_listen_ssl)
assert.is_nil(getmetatable(conf))
end)
it("preserves default properties if not in given file", function()
local conf = assert(conf_loader(helpers.test_conf_path))
assert.is_string(conf.lua_package_path) -- still there
end)
it("accepts custom params, with highest precedence", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
admin_listen = "127.0.0.1:9001",
nginx_worker_processes = "auto"
}))
-- defaults
assert.equal("on", conf.nginx_daemon)
-- overrides
assert.equal("auto", conf.nginx_worker_processes)
assert.equal("127.0.0.1:9001", conf.admin_listen)
assert.equal("0.0.0.0:9000", conf.proxy_listen)
assert.equal("0.0.0.0:9443", conf.proxy_listen_ssl)
assert.is_nil(getmetatable(conf))
end)
it("strips extraneous properties (not in defaults)", function()
local conf = assert(conf_loader(nil, {
stub_property = "leave me alone"
}))
assert.is_nil(conf.stub_property)
end)
it("returns a plugins table", function()
local constants = require "kong.constants"
local conf = assert(conf_loader())
assert.is_nil(conf.custom_plugins)
assert.same(constants.PLUGINS_AVAILABLE, conf.plugins)
end)
it("loads custom plugins", function()
local conf = assert(conf_loader(nil, {
custom_plugins = "hello-world,my-plugin"
}))
assert.is_nil(conf.custom_plugins)
assert.True(conf.plugins["hello-world"])
assert.True(conf.plugins["my-plugin"])
end)
it("extracts ports and listen ips from proxy_listen/admin_listen", function()
local conf = assert(conf_loader())
assert.equal("0.0.0.0", conf.admin_ip)
assert.equal(8001, conf.admin_port)
assert.equal("0.0.0.0", conf.proxy_ip)
assert.equal(8000, conf.proxy_port)
assert.equal("0.0.0.0", conf.proxy_ssl_ip)
assert.equal(8443, conf.proxy_ssl_port)
end)
it("attaches prefix paths", function()
local conf = assert(conf_loader())
assert.equal("/usr/local/kong/pids/dnsmasq.pid", conf.dnsmasq_pid)
assert.equal("/usr/local/kong/pids/serf.pid", conf.serf_pid)
assert.equal("/usr/local/kong/logs/serf.log", conf.serf_log)
assert.equal("/usr/local/kong/serf/serf_event.sh", conf.serf_event)
assert.equal("/usr/local/kong/serf/serf.id", conf.serf_node_id)
assert.equal("/usr/local/kong/pids/nginx.pid", conf.nginx_pid)
assert.equal("/usr/local/kong/logs/error.log", conf.nginx_err_logs)
assert.equal("/usr/local/kong/logs/access.log", conf.nginx_acc_logs)
assert.equal("/usr/local/kong/nginx.conf", conf.nginx_conf)
assert.equal("/usr/local/kong/nginx-kong.conf", conf.nginx_kong_conf)
assert.equal("/usr/local/kong/kong.conf", conf.kong_conf)
-- ssl default paths
assert.equal("/usr/local/kong/ssl/kong-default.crt", conf.ssl_cert_default)
assert.equal("/usr/local/kong/ssl/kong-default.key", conf.ssl_cert_key_default)
assert.equal("/usr/local/kong/ssl/kong-default.csr", conf.ssl_cert_csr_default)
end)
it("strips comments ending settings", function()
local conf = assert(conf_loader("spec/fixtures/to-strip.conf"))
assert.equal("cassandra", conf.database)
assert.equal("debug", conf.log_level)
end)
it("overcomes penlight's list_delim option", function()
local conf = assert(conf_loader("spec/fixtures/to-strip.conf"))
assert.False(conf.dnsmasq)
assert.True(conf.plugins.foobar)
assert.True(conf.plugins["hello-world"])
end)
describe("inferences", function()
it("infer booleans (on/off/true/false strings)", function()
local conf = assert(conf_loader())
assert.True(conf.dnsmasq)
assert.equal("on", conf.nginx_daemon)
assert.equal("on", conf.lua_code_cache)
assert.True(conf.anonymous_reports)
assert.False(conf.cassandra_ssl)
assert.False(conf.cassandra_ssl_verify)
assert.False(conf.pg_ssl)
assert.False(conf.pg_ssl_verify)
conf = assert(conf_loader(nil, {
cassandra_ssl = true,
pg_ssl = true
}))
assert.True(conf.cassandra_ssl)
assert.True(conf.pg_ssl)
conf = assert(conf_loader(nil, {
cassandra_ssl = "on",
pg_ssl = "on"
}))
assert.True(conf.cassandra_ssl)
assert.True(conf.pg_ssl)
conf = assert(conf_loader(nil, {
cassandra_ssl = "true",
pg_ssl = "true"
}))
assert.True(conf.cassandra_ssl)
assert.True(conf.pg_ssl)
end)
it("infer arrays (comma-separated strings)", function()
local conf = assert(conf_loader())
assert.same({"127.0.0.1"}, conf.cassandra_contact_points)
assert.same({"dc1:2", "dc2:3"}, conf.cassandra_data_centers)
assert.is_nil(getmetatable(conf.cassandra_contact_points))
assert.is_nil(getmetatable(conf.cassandra_data_centers))
end)
it("infer ngx_boolean", function()
local conf = assert(conf_loader(nil, {
lua_code_cache = true
}))
assert.equal("on", conf.lua_code_cache)
conf = assert(conf_loader(nil, {
lua_code_cache = false
}))
assert.equal("off", conf.lua_code_cache)
conf = assert(conf_loader(nil, {
lua_code_cache = "off"
}))
assert.equal("off", conf.lua_code_cache)
end)
end)
describe("validations", function()
it("enforces properties types", function()
local conf, err = conf_loader(nil, {
lua_package_path = 123
})
assert.equal("lua_package_path is not a string: '123'", err)
assert.is_nil(conf)
end)
it("enforces enums", function()
local conf, err = conf_loader(nil, {
database = "mysql"
})
assert.equal("database has an invalid value: 'mysql' (postgres, cassandra)", err)
assert.is_nil(conf)
conf, err = conf_loader(nil, {
cassandra_consistency = "FOUR"
})
assert.equal("cassandra_consistency has an invalid value: 'FOUR'"
.." (ALL, EACH_QUORUM, QUORUM, LOCAL_QUORUM, ONE, TWO,"
.." THREE, LOCAL_ONE)", err)
assert.is_nil(conf)
end)
it("enforces ipv4:port types", function()
local conf, err = conf_loader(nil, {
cluster_listen = 123
})
assert.equal("cluster_listen must be in the form of IPv4:port", err)
assert.is_nil(conf)
conf, err = conf_loader(nil, {
cluster_listen = "1.1.1.1"
})
assert.equal("cluster_listen must be in the form of IPv4:port", err)
assert.is_nil(conf)
conf, err = conf_loader(nil, {
cluster_listen = "1.1.1.1:3333"
})
assert.is_nil(err)
assert.is_table(conf)
end)
it("enforces listen addresses format", function()
local conf, err = conf_loader(nil, {
admin_listen = "127.0.0.1"
})
assert.is_nil(conf)
assert.equal("admin_listen must be of form 'address:port'", err)
conf, err = conf_loader(nil, {
proxy_listen = "127.0.0.1"
})
assert.is_nil(conf)
assert.equal("proxy_listen must be of form 'address:port'", err)
conf, err = conf_loader(nil, {
proxy_listen_ssl = "127.0.0.1"
})
assert.is_nil(conf)
assert.equal("proxy_listen_ssl must be of form 'address:port'", err)
end)
it("errors when both a resolver and dnsmasq are enabled", function()
local conf, err = conf_loader(nil, {
dnsmasq = true,
dns_resolver = "8.8.8.8:53"
})
assert.equal("must disable dnsmasq when a custom DNS resolver is specified", err)
assert.is_nil(conf)
conf, err = conf_loader(nil, {
dnsmasq = false,
dns_resolver = "8.8.8.8:53"
})
assert.is_nil(err)
assert.is_table(conf)
end)
it("requires a dns_resolver when dnsmasq is disabled", function()
local conf, err = conf_loader(nil, {
dnsmasq = false
})
assert.equal("must specify a custom DNS resolver when dnsmasq is turned off", err)
assert.is_nil(conf)
conf, err = conf_loader(nil, {
dnsmasq = false,
dns_resolver = "8.8.8.8:53"
})
assert.is_nil(err)
assert.is_table(conf)
end)
it("cluster_ttl_on_failure cannot be lower than 60 seconds", function()
local conf, err = conf_loader(nil, {
cluster_ttl_on_failure = 40
})
assert.equal("cluster_ttl_on_failure must be at least 60 seconds", err)
assert.is_nil(conf)
end)
it("does not check SSL cert and key if SSL is off", function()
local conf, err = conf_loader(nil, {
ssl = false,
ssl_cert = "/path/cert.pem"
})
assert.is_nil(err)
assert.is_table(conf)
end)
it("requires both SSL cert and key", function()
local conf, err = conf_loader(nil, {
ssl_cert = "/path/cert.pem"
})
assert.equal("ssl_cert_key must be specified", err)
assert.is_nil(conf)
conf, err = conf_loader(nil, {
ssl_cert_key = "/path/key.pem"
})
assert.equal("ssl_cert must be specified", err)
assert.is_nil(conf)
conf, err = conf_loader(nil, {
ssl_cert = "spec/fixtures/kong_spec.crt",
ssl_cert_key = "spec/fixtures/kong_spec.key"
})
assert.is_nil(err)
assert.is_table(conf)
end)
it("requires SSL cert and key to exist", function()
local conf, _, errors = conf_loader(nil, {
ssl_cert = "/path/cert.pem",
ssl_cert_key = "/path/cert_key.pem"
})
assert.equal(2, #errors)
assert.contains("ssl_cert: no such file at /path/cert.pem", errors)
assert.contains("ssl_cert_key: no such file at /path/cert_key.pem", errors)
assert.is_nil(conf)
conf, _, errors = conf_loader(nil, {
ssl_cert = "spec/fixtures/kong_spec.crt",
ssl_cert_key = "/path/cert_key.pem"
})
assert.equal(1, #errors)
assert.contains("ssl_cert_key: no such file at /path/cert_key.pem", errors)
assert.is_nil(conf)
end)
it("resolves SSL cert/key to absolute path", function()
local conf, err = conf_loader(nil, {
ssl_cert = "spec/fixtures/kong_spec.crt",
ssl_cert_key = "spec/fixtures/kong_spec.key"
})
assert.is_nil(err)
assert.is_table(conf)
assert.True(helpers.path.isabs(conf.ssl_cert))
assert.True(helpers.path.isabs(conf.ssl_cert_key))
end)
it("honors path if provided even if a default file exists", function()
conf_loader.add_default_path("spec/fixtures/to-strip.conf")
finally(function()
package.loaded["kong.conf_loader"] = nil
conf_loader = require "kong.conf_loader"
end)
local conf = assert(conf_loader(helpers.test_conf_path))
assert.equal("postgres", conf.database)
end)
it("requires cassandra_local_datacenter if DCAwareRoundRobin is in use", function()
local conf, err = conf_loader(nil, {
cassandra_lb_policy = "DCAwareRoundRobin"
})
assert.is_nil(conf)
assert.equal("must specify 'cassandra_local_datacenter' when DCAwareRoundRobin policy is in use", err)
end)
end)
describe("errors", function()
it("returns inexistent file", function()
local conf, err = conf_loader "inexistent"
assert.equal("no file at: inexistent", err)
assert.is_nil(conf)
end)
it("returns all errors in ret value #3", function()
local conf, _, errors = conf_loader(nil, {
cassandra_repl_strategy = "foo",
ssl_cert_key = "spec/fixtures/kong_spec.key"
})
assert.equal(2, #errors)
assert.is_nil(conf)
assert.contains("cassandra_repl_strategy has", errors, true)
assert.contains("ssl_cert must be specified", errors)
end)
end)
describe("remove_sensitive()", function()
it("replaces sensitive settings", function()
local conf = assert(conf_loader(nil, {
pg_password = "hide_me",
cassandra_password = "hide_me",
cluster_encrypt_key = "hide_me"
}))
local purged_conf = conf_loader.remove_sensitive(conf)
assert.not_equal("hide_me", purged_conf.pg_password)
assert.not_equal("hide_me", purged_conf.cassandra_password)
assert.not_equal("hide_me", purged_conf.cluster_encrypt_key)
end)
it("does not insert placeholder if no value", function()
local conf = assert(conf_loader())
local purged_conf = conf_loader.remove_sensitive(conf)
assert.is_nil(purged_conf.pg_password)
assert.is_nil(purged_conf.cassandra_password)
assert.is_nil(purged_conf.cluster_encrypt_key)
end)
end)
end)