diff --git a/apps/emqx_management/src/emqx_mgmt_data_backup.erl b/apps/emqx_management/src/emqx_mgmt_data_backup.erl index f5eaaa7ab..5a83528fd 100644 --- a/apps/emqx_management/src/emqx_mgmt_data_backup.erl +++ b/apps/emqx_management/src/emqx_mgmt_data_backup.erl @@ -666,7 +666,10 @@ look_up_file(Filename) -> end, 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 -> {ok, hd(List)} end. diff --git a/apps/emqx_management/test/emqx_mgmt_SUITE.erl b/apps/emqx_management/test/emqx_mgmt_SUITE.erl index 45afbd860..48a771d7a 100644 --- a/apps/emqx_management/test/emqx_mgmt_SUITE.erl +++ b/apps/emqx_management/test/emqx_mgmt_SUITE.erl @@ -72,6 +72,13 @@ init_per_testcase(t_plugins_cmd, Config) -> meck:expect(emqx_plugins, reload, fun(_) -> ok end), mock_print(), 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) -> mock_print(), Config. @@ -79,6 +86,10 @@ init_per_testcase(_Case, Config) -> end_per_testcase(t_plugins_cmd, _Config) -> meck:unload(emqx_plugins), 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) -> unmock_print(). @@ -384,6 +395,12 @@ t_backup_file(_)-> {error, not_found} = emqx_mgmt_data_backup:delete_backup_file(BadFilename), 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() -> ok = safe_unmeck(emqx_ctl), meck:new(emqx_ctl, [non_strict, passthrough]),