fix(backup): accept files outside `data/dir` when importing
Fixes https://github.com/emqx/emqx/issues/7990 Currently, when importing a data backup using `emqx_ctl data import /some/data.json`, it'll only search in the `data/backup` directory and fail if the file is not inside that dir.
This commit is contained in:
parent
b1d642bd6a
commit
67e3e2de96
|
@ -666,7 +666,10 @@ look_up_file(Filename) ->
|
||||||
end,
|
end,
|
||||||
case lists:filter(Filter, backup_files()) of
|
case lists:filter(Filter, backup_files()) of
|
||||||
[] ->
|
[] ->
|
||||||
{error, not_found};
|
case filelib:is_file(Filename) of
|
||||||
|
true -> {ok, Filename};
|
||||||
|
false -> {error, not_found}
|
||||||
|
end;
|
||||||
List ->
|
List ->
|
||||||
{ok, hd(List)}
|
{ok, hd(List)}
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -72,6 +72,13 @@ init_per_testcase(t_plugins_cmd, Config) ->
|
||||||
meck:expect(emqx_plugins, reload, fun(_) -> ok end),
|
meck:expect(emqx_plugins, reload, fun(_) -> ok end),
|
||||||
mock_print(),
|
mock_print(),
|
||||||
Config;
|
Config;
|
||||||
|
init_per_testcase(t_import_outside_backup_dir, Config) ->
|
||||||
|
RandomName = emqx_guid:to_hexstr(emqx_guid:gen()),
|
||||||
|
Filepath = "/tmp/" ++ binary_to_list(RandomName) ++ ".json",
|
||||||
|
FakeData = #{version => "4.4"},
|
||||||
|
ok = file:write_file(Filepath, emqx_json:encode(FakeData)),
|
||||||
|
[ {tmp_file, Filepath}
|
||||||
|
| Config];
|
||||||
init_per_testcase(_Case, Config) ->
|
init_per_testcase(_Case, Config) ->
|
||||||
mock_print(),
|
mock_print(),
|
||||||
Config.
|
Config.
|
||||||
|
@ -79,6 +86,10 @@ init_per_testcase(_Case, Config) ->
|
||||||
end_per_testcase(t_plugins_cmd, _Config) ->
|
end_per_testcase(t_plugins_cmd, _Config) ->
|
||||||
meck:unload(emqx_plugins),
|
meck:unload(emqx_plugins),
|
||||||
unmock_print();
|
unmock_print();
|
||||||
|
end_per_testcase(t_import_outside_backup_dir, Config) ->
|
||||||
|
Filepath = ?config(tmp_file, Config),
|
||||||
|
file:delete(Filepath),
|
||||||
|
ok;
|
||||||
end_per_testcase(_Case, _Config) ->
|
end_per_testcase(_Case, _Config) ->
|
||||||
unmock_print().
|
unmock_print().
|
||||||
|
|
||||||
|
@ -384,6 +395,12 @@ t_backup_file(_)->
|
||||||
{error, not_found} = emqx_mgmt_data_backup:delete_backup_file(BadFilename),
|
{error, not_found} = emqx_mgmt_data_backup:delete_backup_file(BadFilename),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
t_import_outside_backup_dir(Config) ->
|
||||||
|
Filepath = ?config(tmp_file, Config),
|
||||||
|
Env = "{}",
|
||||||
|
?assertEqual(ok, emqx_mgmt_data_backup:import(Filepath, Env)),
|
||||||
|
ok.
|
||||||
|
|
||||||
mock_print() ->
|
mock_print() ->
|
||||||
ok = safe_unmeck(emqx_ctl),
|
ok = safe_unmeck(emqx_ctl),
|
||||||
meck:new(emqx_ctl, [non_strict, passthrough]),
|
meck:new(emqx_ctl, [non_strict, passthrough]),
|
||||||
|
|
Loading…
Reference in New Issue