Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve startup error when keys_dir is not available #4347

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions apps/aecore/src/aec_keys.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
-export([check_peer_keys/2,
encrypt_key/2,
sign_privkey/0,
start_link/1
start_link/1,
ensure_dir/1
]).
-endif.

Expand Down Expand Up @@ -511,12 +512,19 @@ p_gen_new_peer(Pwd, PubFile, PrivFile) ->
ensure_dir(KeysDir) ->
case filelib:is_dir(KeysDir) of
false ->
KeysDirFile = filename:join(KeysDir, "keyfile"),
ok = filelib:ensure_dir(KeysDirFile);
case ensure_dir_(KeysDir) of
ok -> ok;
Err -> error({could_not_ensure_key_dir, KeysDir, Err})
end;
true ->
ok
end.

%% Note: `filelib:ensure_path/1` was introduced in OTP-25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this comment related 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just an explanation for the silly decision to use ensure_dir when ensure_path is what we want...

ensure_dir_(KeysDir) ->
KeysDirFile = filename:join(KeysDir, "keyfile"),
filelib:ensure_dir(KeysDirFile).

from_local_dir(NewFile) ->
file:read_file(NewFile).

Expand Down
45 changes: 23 additions & 22 deletions apps/aecore/test/aec_keys_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,28 @@ all_test_() ->
end}]
end]}.

start_test_() ->
TF =
fun() ->
?_test(
aec_test_utils:aec_keys_bare_cleanup(
aec_test_utils:aec_keys_bare_setup()))
end,
{setup,
fun() -> ok = application:ensure_started(crypto) end,
fun(_) -> ok = application:stop(crypto) end,
lazy_gen(100, TF)}.

lazy_gen(N, TF) ->
{generator,
fun () ->
if N > 0 ->
[TF()
| lazy_gen(N-1, TF)];
true ->
[]
end
end}.
ensure_dir_test_() ->
{foreach,
fun() -> meck:new(filelib, [passthrough, unstick]) end,
fun(_) -> meck:unload(filelib) end,
[{"OK case 1",
fun() ->
meck:expect(filelib, is_dir, fun(_Dir) -> true end),
meck:expect(filelib, ensure_dir, fun(_Dir) -> ok end),
?assertEqual(ok, aec_keys:ensure_dir("/testdir"))
end},
{"OK case 2",
fun() ->
meck:expect(filelib, is_dir, fun(_Dir) -> false end),
meck:expect(filelib, ensure_dir, fun(_Dir) -> ok end),
?assertEqual(ok, aec_keys:ensure_dir("/testdir"))
end},
{"Bad dir",
fun() ->
meck:expect(filelib, is_dir, fun(_Dir) -> false end),
meck:expect(filelib, ensure_dir, fun(_Dir) -> {error, erofs} end),
?assertError({could_not_ensure_key_dir, "/testdir", _}, aec_keys:ensure_dir("/testdir"))
end}
]}.

-endif.