Skip to content

Commit

Permalink
Fixed invalid config parsing in argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
yngvar-antonsson committed Dec 13, 2023
1 parent be208ad commit 8f304b2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ Added
- ``cartridge.cfg`` option ``disable_raft_on_small_clusters`` to disable Raft
failover on clusters with less than 3 instances (default: ``true``).

- ``argparse`` now logs if some sections in config files were ignored.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fixed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- An error with ``cartridge.utils.version_is_at_least`` parsing.

- More clear description of an error in argparse when invalid config file was provided
(`#2169 <https://github.com/tarantool/cartridge/issues/2169>`_).

-------------------------------------------------------------------------------
[2.8.4] - 2023-10-31
-------------------------------------------------------------------------------
Expand Down
35 changes: 33 additions & 2 deletions cartridge/argparse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@
-- In this case, all files in the directory are parsed.
-- To avoid conflicts, the same section mustn't repeat across different files.
--
-- Argparse logs if there were some ignored sections in configuration files.
-- All sections should be key-value pairs as in example, otherwise you'll get an error.
--
-- @module cartridge.argparse

local fio = require('fio')
local yaml = require('yaml')
local json = require('json')
local checks = require('checks')
local errors = require('errors')
local log = require('log')
-- local base_dir = fio.abspath(fio.dirname(arg[0]))

local vars = require('cartridge.vars').new('cartridge.argparse')
Expand Down Expand Up @@ -279,7 +283,13 @@ local function load_dir(dirname)
section_name, dirname, origin[section_name], fio.basename(f)
)
end

if type(content) ~= 'table' then
return nil, ParseConfigError:new(
'invalid content found in file %s in section %s: '..
'expected a table of key-value pairs, got %s',
fio.basename(f), section_name, type(content)
)
end
for argname, argvalue in pairs(content) do
ret[section_name][argname:lower()] = argvalue
end
Expand Down Expand Up @@ -351,13 +361,34 @@ local function parse_file(filename, search_name)
end

local ret = {}
local sections = {}
for section_name, _ in pairs(file_sections) do
sections[section_name] = true
end
for _, section_name in ipairs(section_names) do
local content = file_sections[section_name] or {}

if type(content) ~= 'table' then
return nil, ParseConfigError:new(
'invalid content found in file %s in section %s: '..
'expected a table of key-value pairs, got %s',
filename, section_name, type(content)
)
end
sections[section_name] = false
for argname, argvalue in pairs(content) do
ret[argname:lower()] = argvalue
end
end
local names = {}
for section_name, ignored in pairs(sections) do
if ignored then
table.insert(names, section_name)
end
end
if #names > 0 then
log.warn('Some sections in config file %s were ignored: %s',
filename, table.concat(names, ', '))
end

return ret
end
Expand Down
12 changes: 11 additions & 1 deletion test/unit/argparse_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ g.test_sections = function()
['custom.sub.sub'] = {
x = '@custom.sub.sub',
y_subsub = true,
}
},
['invalid'] = 'so wrong',
})
)

Expand Down Expand Up @@ -142,6 +143,15 @@ g.test_sections = function()
y_sub = 3.14,
y_subsub = true,
})
local function check_err(cmd_args, expected)
local ok, err = pcall(g.run, cmd_args, {'TARANTOOL_CFG=./tarantool.yml'})
t.assert_not(ok)
t.assert_str_contains(err, expected)
end
check_err('--instance-name invalid',
[[ParseConfigError: invalid content found in file ./tarantool.yml in section invalid: ]]..
[[expected a table of key-value pairs, got string]]
)
end

g.test_priority = function()
Expand Down

0 comments on commit 8f304b2

Please sign in to comment.