/
09-filter-meta_spec.lua
356 lines (294 loc) · 9.18 KB
/
09-filter-meta_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
local helpers = require "spec.helpers"
local utils = require "kong.tools.utils"
local cjson = require "cjson"
local file = helpers.file
local TEST_FILTER_SRC = "spec/fixtures/proxy_wasm_filters/build/response_transformer.wasm"
local function json(body)
return {
headers = { ["Content-Type"] = "application/json" },
body = body,
}
end
local function post_config(client, config)
config._format_version = config._format_version or "3.0"
local res = client:post("/config?flatten_errors=1", json(config))
assert.response(res).has.jsonbody()
return res
end
local function random_name()
return "test-" .. utils.random_string()
end
for _, strategy in helpers.each_strategy({ "postgres", "off" }) do
describe("filter metadata [#" .. strategy .. "]", function()
local filter_path
local admin
local proxy
lazy_setup(function()
helpers.clean_prefix()
if strategy == "postgres" then
helpers.get_db_utils(strategy, {
"routes",
"services",
"filter_chains",
})
end
filter_path = helpers.make_temp_dir()
do
local name = "rt_no_validation"
assert(file.copy(TEST_FILTER_SRC, filter_path .. "/" .. name .. ".wasm"))
end
do
local name = "rt_with_validation"
assert(file.copy(TEST_FILTER_SRC, filter_path .. "/" .. name .. ".wasm"))
assert(file.write(filter_path .. "/" .. name .. ".meta.json", cjson.encode({
config_schema = {
type = "object",
properties = {
add = {
type = "object",
properties = {
headers = {
type = "array",
elements = { type = "string" },
},
},
required = { "headers" },
},
},
required = { "add" },
}
})))
end
assert(helpers.start_kong({
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
plugins = "off",
wasm = true,
wasm_filters_path = filter_path,
nginx_main_worker_processes = 1,
}))
admin = helpers.admin_client()
proxy = helpers.proxy_client()
helpers.clean_logfile()
end)
lazy_teardown(function()
if admin then admin:close() end
if proxy then proxy:close() end
helpers.stop_kong()
if filter_path and os.getenv("KONG_DONT_CLEAN") ~= "1" then
helpers.dir.rmtree(filter_path)
end
end)
describe("config validation -", function()
local create_filter_chain
if strategy == "off" then
create_filter_chain = function(route_host, filter_chain)
return post_config(admin, {
services = {
{ name = random_name(),
url = helpers.mock_upstream_url,
routes = {
{ name = random_name(),
hosts = { route_host },
filter_chains = { filter_chain }
},
},
},
},
})
end
else
create_filter_chain = function(route_host, filter_chain)
local res = admin:post("/services", json {
name = random_name(),
url = helpers.mock_upstream_url,
})
assert.response(res).has.status(201)
local service = assert.response(res).has.jsonbody()
res = admin:post("/routes", json {
name = random_name(),
hosts = { route_host },
service = { id = service.id },
})
assert.response(res).has.status(201)
local route = assert.response(res).has.jsonbody()
res = admin:post("/routes/" .. route.id .. "/filter-chains",
json(filter_chain))
assert.response(res).has.jsonbody()
return res
end
end
it("filters with config schemas are validated", function()
local res = create_filter_chain(random_name(), {
name = random_name(),
filters = {
{
name = "rt_with_validation",
config = {}, -- empty
},
},
})
assert.response(res).has.status(400)
local body = assert.response(res).has.jsonbody()
if strategy == "off" then
assert.is_table(body.flattened_errors)
assert.same(1, #body.flattened_errors)
local err = body.flattened_errors[1]
assert.is_table(err)
assert.same("filter_chain", err.entity_type)
assert.same({
{
field = "filters.1.config",
message = "property add is required",
type = "field"
}
}, err.errors)
else
assert.same({
filters = {
{
config = "property add is required"
}
}
}, body.fields)
end
local host = random_name() .. ".test"
res = create_filter_chain(host, {
name = random_name(),
filters = {
{
name = "rt_with_validation",
config = {
add = {
headers = {
"x-foo:123",
},
},
},
},
},
})
assert.response(res).has.status(201)
assert.eventually(function()
res = proxy:get("/status/200", { headers = { host = host } })
assert.response(res).has.status(200)
assert.response(res).has.header("x-foo")
end).has_no_error()
end)
it("filters without config schemas can only accept a string", function()
local host = random_name() .. ".test"
local res = create_filter_chain(host, {
name = random_name(),
filters = {
{
name = "rt_no_validation",
config = {
add = {
headers = 1234,
},
}
},
},
})
assert.response(res).has.status(400)
local body = assert.response(res).has.jsonbody()
assert.same({
filters = {
{ config = "wrong type: expected one of string, null, got object" }
}
}, body.fields)
end)
it("filters without config schemas are not validated", function()
local host = random_name() .. ".test"
local res = create_filter_chain(host, {
name = random_name(),
filters = {
{
name = "rt_no_validation",
config = cjson.encode({
add = {
headers = 123,
},
}),
},
},
})
assert.response(res).has.status(201)
assert.eventually(function()
res = proxy:get("/status/200", { headers = { host = host } })
assert.response(res).has.no.header("x-foo")
assert.logfile().has.line("failed parsing filter config", true, 0)
end).has_no_error()
end)
end)
end)
describe("filter metadata [#" .. strategy .. "] startup errors -", function()
local filter_path
local filter_name = "test-filter"
local meta_path
local conf
lazy_setup(function()
if strategy == "postgres" then
helpers.get_db_utils(strategy, {
"routes",
"services",
"filter_chains",
})
end
end)
before_each(function()
filter_path = helpers.make_temp_dir()
assert(file.copy(TEST_FILTER_SRC, filter_path .. "/" .. filter_name .. ".wasm"))
meta_path = filter_path .. "/" .. filter_name .. ".meta.json"
conf = {
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
plugins = "off",
wasm = true,
wasm_filters_path = filter_path,
nginx_main_worker_processes = 1,
}
helpers.clean_prefix()
helpers.prepare_prefix()
end)
after_each(function()
helpers.kill_all()
if filter_path and os.getenv("KONG_DONT_CLEAN") ~= "1" then
helpers.dir.rmtree(filter_path)
end
end)
describe("kong start", function()
it("fails when filter.meta.json is not a file", function()
assert(helpers.dir.makepath(meta_path))
local ok, err = helpers.start_kong(conf)
assert.falsy(ok)
assert.matches("Failed to load metadata for one or more filters", err, nil, true)
assert.matches(filter_name, err, nil, true)
assert.matches(meta_path, err, nil, true)
assert.matches("path exists but is not a file", err, nil, true)
end)
it("fails when filter.meta.json is not vaild json", function()
assert(file.write(meta_path, "oops!"))
local ok, err = helpers.start_kong(conf)
assert.falsy(ok)
assert.matches("Failed to load metadata for one or more filters", err, nil, true)
assert.matches(filter_name, err, nil, true)
assert.matches(meta_path, err, nil, true)
assert.matches("JSON decode error", err, nil, true)
end)
it("fails when filter.meta.json is not semantically valid", function()
assert(file.write(meta_path, cjson.encode({
config_schema = {
type = "i am not a valid type",
},
})))
local ok, err = helpers.start_kong(conf)
assert.falsy(ok)
assert.matches("Failed to load metadata for one or more filters", err, nil, true)
assert.matches(filter_name, err, nil, true)
assert.matches(meta_path, err, nil, true)
assert.matches("file contains invalid metadata", err, nil, true)
end)
end)
end)
end -- each strategy