add config file validation #2

Merged
luna merged 7 commits from config-validation into mistress 2022-12-07 18:50:30 +00:00
3 changed files with 42 additions and 33 deletions
Showing only changes of commit 2d2a68b1c3 - Show all commits

View file

@ -1,5 +1,6 @@
lu = require('luaunit')
local rex = require('rex_pcre2')
require('util')
function createNgx()
local ngx = {
@ -54,12 +55,18 @@ function setupFakeRequest(path, options)
end
local ctx = require('ctx')
function setupTest(module_require_path, config)
local config = require('config')
function setupTest(module_require_path, input_config)
resetNgx()
local module = require(module_require_path)
state = module.init(config)
local schema_errors = config.validateSchema(module.config, input_config)
local count = table.pprint(schema_errors)
lu.assertIs(count, 0)
state = module.init(input_config)
ctx.compiled_chain = {
{module, config, state}
{module, input_config, state}
}
return module
end

View file

@ -2,44 +2,21 @@ TestSchemaValidator = {}
local config = require('config')
local function len(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
function pprint(t, ident)
local ident = ident or 0
if type(t) == 'table' then
local count = 0
for k, v in pairs(t) do
print(string.rep('\t', ident) .. k)
pprint(v, ident + 1)
count = count + 1
end
if count == 0 then
print('<empty table>')
end
else
print(string.rep('\t', ident) .. tostring(t))
end
end
function TestSchemaValidator:testBasicFields()
local errors = config.validateSchema({a={type='string'}}, {a='test'})
lu.assertIs(len(errors), 0)
lu.assertIs(table.len(errors), 0)
local errors = config.validateSchema({a={type='number'}}, {a=123})
lu.assertIs(len(errors), 0)
lu.assertIs(table.len(errors), 0)
local errors = config.validateSchema({a={type='string'}}, {a=123})
lu.assertIs(len(errors), 1)
lu.assertIs(table.len(errors), 1)
end
function TestSchemaValidator:testList()
local errors = config.validateSchema({a={type='list', schema={type='number'}}}, {a={1,2,3}})
lu.assertIs(len(errors), 0)
lu.assertIs(table.len(errors), 0)
local errors = config.validateSchema({a={type='list', schema={type='number'}}}, {a={1,2,3,'asd'}})
lu.assertIs(len(errors), 1)
lu.assertIs(table.len(errors), 1)
end
function TestSchemaValidator:testTable()
@ -60,7 +37,7 @@ function TestSchemaValidator:testTable()
{b=2}
}
)
lu.assertIs(len(errors), 0)
lu.assertIs(table.len(errors), 0)
local errors = config.validateSchema(
TEST_SCHEMA,
@ -68,5 +45,5 @@ function TestSchemaValidator:testTable()
{b='sex'}
}
)
lu.assertIs(len(errors), 1)
lu.assertIs(table.len(errors), 1)
end

25
util.lua Normal file
View file

@ -0,0 +1,25 @@
function table.len(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
function table.pprint(t, ident, total_count)
local ident = ident or 0
local total_count = total_count or 0
if type(t) == 'table' then
local count = 0
for k, v in pairs(t) do
print(string.rep('\t', ident) .. k)
count = count + 1
total_count = pprint(v, ident + 1, total_count)
end
if count == 0 then
--print('<empty table>')
end
else
print(string.rep('\t', ident) .. tostring(t))
total_count = total_count + 1
end
return total_count
end