-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-prefix_handler_spec.lua
331 lines (308 loc) · 13.3 KB
/
03-prefix_handler_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
local helpers = require "spec.helpers"
local conf_loader = require "kong.conf_loader"
local prefix_handler = require "kong.cmd.utils.prefix_handler"
local exists = helpers.path.exists
local join = helpers.path.join
describe("NGINX conf compiler", function()
describe("gen_default_ssl_cert()", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
prefix = "ssl_tmp",
ssl = true,
ssl_cert = "spec/fixtures/kong_spec.crt",
ssl_cert_key = "spec/fixtures/kong_spec.key",
admin_ssl = true,
admin_ssl_cert = "spec/fixtures/kong_spec.crt",
admin_ssl_cert_key = "spec/fixtures/kong_spec.key",
}))
before_each(function()
helpers.dir.makepath("ssl_tmp")
end)
after_each(function()
pcall(helpers.dir.rmtree, "ssl_tmp")
end)
describe("proxy", function()
it("auto-generates SSL certificate and key", function()
assert(prefix_handler.gen_default_ssl_cert(conf))
assert(exists(conf.ssl_cert_default))
assert(exists(conf.ssl_cert_key_default))
end)
it("does not re-generate if they already exist", function()
assert(prefix_handler.gen_default_ssl_cert(conf))
local cer = helpers.file.read(conf.ssl_cert_default)
local key = helpers.file.read(conf.ssl_cert_key_default)
assert(prefix_handler.gen_default_ssl_cert(conf))
assert.equal(cer, helpers.file.read(conf.ssl_cert_default))
assert.equal(key, helpers.file.read(conf.ssl_cert_key_default))
end)
end)
describe("admin", function()
it("auto-generates SSL certificate and key", function()
assert(prefix_handler.gen_default_ssl_cert(conf, true))
assert(exists(conf.admin_ssl_cert_default))
assert(exists(conf.admin_ssl_cert_key_default))
end)
it("does not re-generate if they already exist", function()
assert(prefix_handler.gen_default_ssl_cert(conf, true))
local cer = helpers.file.read(conf.admin_ssl_cert_default)
local key = helpers.file.read(conf.admin_ssl_cert_key_default)
assert(prefix_handler.gen_default_ssl_cert(conf, true))
assert.equal(cer, helpers.file.read(conf.admin_ssl_cert_default))
assert.equal(key, helpers.file.read(conf.admin_ssl_cert_key_default))
end)
end)
end)
describe("compile_kong_conf()", function()
it("compiles the Kong NGINX conf chunk", function()
local kong_nginx_conf = prefix_handler.compile_kong_conf(helpers.test_conf)
assert.matches("lua_package_path '?/init.lua;./kong/?.lua;;';", kong_nginx_conf, nil, true)
assert.matches("lua_code_cache on;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9000;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:9001;", kong_nginx_conf, nil, true)
assert.matches("server_name kong;", kong_nginx_conf, nil, true)
assert.matches("server_name kong_admin;", kong_nginx_conf, nil, true)
assert.not_matches("lua_ssl_trusted_certificate", kong_nginx_conf, nil, true)
end)
it("compiles with custom conf", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
lua_code_cache = false,
mem_cache_size = "128k",
proxy_listen = "0.0.0.0:80",
admin_listen = "127.0.0.1:8001"
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("lua_code_cache off;", kong_nginx_conf, nil, true)
assert.matches("lua_shared_dict cache 128k;", kong_nginx_conf, nil, true)
assert.matches("listen 0.0.0.0:80;", kong_nginx_conf, nil, true)
assert.matches("listen 127.0.0.1:8001;", kong_nginx_conf, nil, true)
end)
it("disables SSL", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
ssl = false,
admin_ssl = false
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.not_matches("listen %d+%.%d+%.%d+%.%d+:%d+ ssl;", kong_nginx_conf)
assert.not_matches("ssl_certificate", kong_nginx_conf)
assert.not_matches("ssl_certificate_key", kong_nginx_conf)
assert.not_matches("ssl_protocols", kong_nginx_conf)
assert.not_matches("ssl_certificate_by_lua_block", kong_nginx_conf)
end)
it("does not include lua_ssl_trusted_certificate/lua_ssl_verify_depth by default", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
lua_ssl_verify_depth = "2"
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.not_matches("lua_ssl_trusted_certificate", kong_nginx_conf, nil, true)
assert.not_matches("lua_ssl_verify_depth", kong_nginx_conf, nil, true)
end)
it("sets lua_ssl_trusted_certificate/lua_ssl_verify_depth", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
lua_ssl_trusted_certificate = "/path/to/ca.cert",
lua_ssl_verify_depth = "2"
}))
local kong_nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("lua_ssl_trusted_certificate '/path/to/ca.cert';", kong_nginx_conf, nil, true)
assert.matches("lua_ssl_verify_depth 2;", kong_nginx_conf, nil, true)
end)
it("compiles without anonymous reports", function()
local conf = assert(conf_loader(nil, {
anonymous_reports = false,
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.not_matches("error_log syslog:server=.+ error;", nginx_conf)
end)
it("compiles with anonymous reports", function()
local conf = assert(conf_loader(nil, {
anonymous_reports = true,
}))
local nginx_conf = prefix_handler.compile_kong_conf(conf)
assert.matches("error_log syslog:server=.+:61828 error;", nginx_conf)
end)
end)
describe("compile_nginx_conf()", function()
it("compiles a main NGINX conf", function()
local nginx_conf = prefix_handler.compile_nginx_conf(helpers.test_conf)
assert.matches("worker_processes 1;", nginx_conf, nil, true)
assert.matches("daemon on;", nginx_conf, nil, true)
end)
it("compiles with custom conf", function()
local conf = assert(conf_loader(helpers.test_conf_path, {
nginx_daemon = "off"
}))
local nginx_conf = prefix_handler.compile_nginx_conf(conf)
assert.matches("daemon off;", nginx_conf, nil, true)
end)
it("compiles without opinionated nginx optimizations", function()
local conf = assert(conf_loader(nil, {
nginx_optimizations = false,
}))
local nginx_conf = prefix_handler.compile_nginx_conf(conf)
assert.not_matches("worker_rlimit_nofile %d+;", nginx_conf)
assert.not_matches("worker_connections %d+;", nginx_conf)
assert.not_matches("multi_accept on;", nginx_conf)
end)
it("compiles with opinionated nginx optimizations", function()
local conf = assert(conf_loader(nil, {
nginx_optimizations = true,
}))
local nginx_conf = prefix_handler.compile_nginx_conf(conf)
assert.matches("worker_rlimit_nofile %d+;", nginx_conf)
assert.matches("worker_connections %d+;", nginx_conf)
assert.matches("multi_accept on;", nginx_conf)
end)
it("converts dns_resolver to string", function()
local nginx_conf = prefix_handler.compile_nginx_conf({
dns_resolver = { "8.8.8.8", "8.8.4.4" }
}, [[
"resolver ${{DNS_RESOLVER}} ipv6=off;"
]])
assert.matches("resolver 8.8.8.8 8.8.4.4 ipv6=off;", nginx_conf, nil, true)
end)
end)
describe("prepare_prefix()", function()
local tmp_config = conf_loader(helpers.test_conf_path, {
prefix = "servroot_tmp"
})
before_each(function()
pcall(helpers.dir.rmtree, tmp_config.prefix)
helpers.dir.makepath(tmp_config.prefix)
end)
after_each(function()
pcall(helpers.dir.rmtree, tmp_config.prefix)
end)
it("creates inexistent prefix", function()
finally(function()
pcall(helpers.dir.rmtree, "inexistent")
end)
local config = assert(conf_loader(helpers.test_conf_path, {
prefix = "inexistent"
}))
assert(prefix_handler.prepare_prefix(config))
assert.truthy(exists("inexistent"))
end)
it("ensures prefix is a directory", function()
local tmp = os.tmpname()
finally(function()
os.remove(tmp)
end)
local config = assert(conf_loader(helpers.test_conf_path, {
prefix = tmp
}))
local ok, err = prefix_handler.prepare_prefix(config)
assert.equal(tmp.." is not a directory", err)
assert.is_nil(ok)
end)
it("creates pids folder", function()
assert(prefix_handler.prepare_prefix(tmp_config))
assert.truthy(exists(join(tmp_config.prefix, "pids")))
end)
it("creates serf folder", function()
assert(prefix_handler.prepare_prefix(tmp_config))
assert.truthy(exists(join(tmp_config.prefix, "serf")))
end)
it("creates NGINX conf and log files", function()
assert(prefix_handler.prepare_prefix(tmp_config))
assert.truthy(exists(tmp_config.kong_env))
assert.truthy(exists(tmp_config.nginx_kong_conf))
assert.truthy(exists(tmp_config.nginx_err_logs))
assert.truthy(exists(tmp_config.nginx_acc_logs))
assert.truthy(exists(tmp_config.nginx_admin_acc_logs))
end)
it("dumps Kong conf", function()
assert(prefix_handler.prepare_prefix(tmp_config))
local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_env))
assert.same(tmp_config, in_prefix_kong_conf)
end)
it("dump Kong conf (custom conf)", function()
local conf = assert(conf_loader(nil, {
pg_database = "foobar",
prefix = tmp_config.prefix
}))
assert.equal("foobar", conf.pg_database)
assert(prefix_handler.prepare_prefix(conf))
local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_env))
assert.same(conf, in_prefix_kong_conf)
end)
it("writes custom plugins in Kong conf", function()
local conf = assert(conf_loader(nil, {
custom_plugins = { "foo", "bar" },
prefix = tmp_config.prefix
}))
assert(prefix_handler.prepare_prefix(conf))
local in_prefix_kong_conf = assert(conf_loader(tmp_config.kong_env))
assert.True(in_prefix_kong_conf.plugins.foo)
assert.True(in_prefix_kong_conf.plugins.bar)
end)
it("dumps Serf script", function()
assert(prefix_handler.prepare_prefix(tmp_config))
local identifier = helpers.file.read(tmp_config.serf_event)
assert.is_string(identifier)
end)
it("dumps Serf identifier", function()
assert(prefix_handler.prepare_prefix(tmp_config))
local identifier = helpers.file.read(tmp_config.serf_node_id)
assert.is_string(identifier)
end)
it("preserves Serf identifier if already exists", function()
-- prepare twice
assert(prefix_handler.prepare_prefix(tmp_config))
local identifier_1 = helpers.file.read(tmp_config.serf_node_id)
assert(prefix_handler.prepare_prefix(tmp_config))
local identifier_2 = helpers.file.read(tmp_config.serf_node_id)
assert.equal(identifier_1, identifier_2)
end)
describe("ssl", function()
it("does not create SSL dir if disabled", function()
local conf = conf_loader(nil, {
prefix = tmp_config.prefix,
ssl = false,
admin_ssl = false
})
assert(prefix_handler.prepare_prefix(conf))
assert.falsy(exists(join(conf.prefix, "ssl")))
end)
it("does not create SSL dir if using custom cert", function()
local conf = conf_loader(nil, {
prefix = tmp_config.prefix,
ssl = true,
ssl_cert = "spec/fixtures/kong_spec.crt",
ssl_cert_key = "spec/fixtures/kong_spec.key",
admin_ssl = true,
admin_ssl_cert = "spec/fixtures/kong_spec.crt",
admin_ssl_cert_key = "spec/fixtures/kong_spec.key",
})
assert(prefix_handler.prepare_prefix(conf))
assert.falsy(exists(join(conf.prefix, "ssl")))
end)
it("generates default SSL cert", function()
local conf = conf_loader(nil, {
prefix = tmp_config.prefix,
ssl = true,
admin_ssl = true
})
assert(prefix_handler.prepare_prefix(conf))
assert.truthy(exists(join(conf.prefix, "ssl")))
assert.truthy(exists(conf.ssl_cert_default))
assert.truthy(exists(conf.ssl_cert_key_default))
assert.truthy(exists(conf.admin_ssl_cert_default))
assert.truthy(exists(conf.admin_ssl_cert_key_default))
end)
end)
describe("custom template", function()
local templ_fixture = "spec/fixtures/custom_nginx.template"
it("accepts a custom NGINX conf template", function()
assert(prefix_handler.prepare_prefix(tmp_config, templ_fixture))
assert.truthy(exists(tmp_config.nginx_conf))
local contents = helpers.file.read(tmp_config.nginx_conf)
assert.matches("# This is a custom nginx configuration template for Kong specs", contents, nil, true)
assert.matches("daemon on;", contents, nil, true)
assert.matches("listen 0.0.0.0:9000;", contents, nil, true)
end)
it("errors on non-existing file", function()
local ok, err = prefix_handler.prepare_prefix(tmp_config, "spec/fixtures/inexistent.template")
assert.is_nil(ok)
assert.equal("no such file: spec/fixtures/inexistent.template", err)
end)
end)
end)
end)