Skip to content

Commit

Permalink
Adding domain admin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Janusz Jakubiec authored and Janusz Jakubiec committed Aug 19, 2022
1 parent 3496b04 commit 148938c
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 25 deletions.
84 changes: 80 additions & 4 deletions big_tests/tests/graphql_mnesia_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
-import(domain_helper, [host_type/1]).
-import(mongooseimctl_helper, [rpc_call/3]).
-import(graphql_helper, [execute_command/4, execute_user_command/5, user_to_bin/1,
get_ok_value/2, get_err_code/1, get_err_value/2]).
get_ok_value/2, get_err_code/1, get_err_value/2,
execute_domain_admin_command/4, get_unauthorized/1]).

-record(mnesia_table_test, {key :: integer(), name :: binary()}).
-record(vcard, {us, vcard}).

all() ->
[{group, admin_mnesia_cli},
{group, admin_mnesia_http}].
{group, admin_mnesia_http},
{group, domain_admin_mnesia}].

groups() ->
[{admin_mnesia_http, [sequence], admin_mnesia_tests()},
{admin_mnesia_cli, [sequence], admin_mnesia_tests()}].
{admin_mnesia_cli, [sequence], admin_mnesia_tests()},
{domain_admin_mnesia, [], domain_admin_tests()}].

admin_mnesia_tests() ->
[dump_mnesia_table_test,
Expand All @@ -42,6 +45,17 @@ admin_mnesia_tests() ->
install_fallback_error_test,
set_master_test].

domain_admin_tests() ->
[domain_admin_dump_mnesia_table_test,
domain_admin_dump_mnesia_test,
domain_admin_backup_test,
domain_admin_restore_test,
domain_admin_load_mnesia_test,
domain_admin_change_nodename_test,
domain_admin_install_fallback_test,
domain_admin_set_master_test,
domain_admin_get_info_test].

init_per_suite(Config) ->
application:ensure_all_started(jid),
ok = mnesia:create_schema([node()]),
Expand All @@ -56,7 +70,9 @@ end_per_suite(_C) ->
init_per_group(admin_mnesia_http, Config) ->
graphql_helper:init_admin_handler(Config);
init_per_group(admin_mnesia_cli, Config) ->
graphql_helper:init_admin_cli(Config).
graphql_helper:init_admin_cli(Config);
init_per_group(domain_admin_mnesia, Config) ->
graphql_helper:init_domain_admin_handler(Config).

end_per_group(_, _Config) ->
graphql_helper:clean(),
Expand Down Expand Up @@ -206,6 +222,36 @@ set_master_test(Config) ->
ParsedRes = get_ok_value([data, mnesia, setMaster], set_master(mim(), Config)),
?assertEqual(<<"Master node set">>, ParsedRes).

% Domain admin tests

domain_admin_dump_mnesia_table_test(Config) ->
get_unauthorized(domain_admin_dump_mnesia_table(<<"File">>, <<"mnesia_table_test">>, Config)).

domain_admin_dump_mnesia_test(Config) ->
get_unauthorized(domain_admin_dump_mnesia(<<"File">>, Config)).

domain_admin_backup_test(Config) ->
get_unauthorized(domain_admin_backup_mnesia(<<"Path">>, Config)).

domain_admin_restore_test(Config) ->
get_unauthorized(domain_admin_restore_mnesia(<<"Path">>, Config)).

domain_admin_load_mnesia_test(Config) ->
get_unauthorized(domain_admin_load_mnesia(<<"Path">>, Config)).

domain_admin_change_nodename_test(Config) ->
get_unauthorized(domain_admin_change_nodename(<<"From">>, <<"To">>, <<"file1">>,
<<"file2">>, Config)).

domain_admin_install_fallback_test(Config) ->
get_unauthorized(domain_admin_install_fallback(<<"Path">>, Config)).

domain_admin_set_master_test(Config) ->
get_unauthorized(domain_admin_set_master(mim(), Config)).

domain_admin_get_info_test(Config) ->
get_unauthorized(domain_admin_get_info([<<"all">>], Config)).

%--------------------------------------------------------------------------------------------------
% Helpers
%--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -294,6 +340,36 @@ change_nodename(ChangeFrom, ChangeTo, Source, Target, Config) ->
set_master(Node, Config) ->
execute_command(<<"mnesia">>, <<"setMaster">>, Node, Config).

domain_admin_get_info(Keys, Config) ->
execute_domain_admin_command(<<"mnesia">>, <<"info">>, #{keys => Keys}, Config).

domain_admin_install_fallback(Path, Config) ->
execute_domain_admin_command(<<"mnesia">>, <<"installFallback">>, #{path => Path}, Config).

domain_admin_dump_mnesia(Path, Config) ->
execute_domain_admin_command(<<"mnesia">>, <<"dump">>, #{path => Path}, Config).

domain_admin_backup_mnesia(Path, Config) ->
execute_domain_admin_command(<<"mnesia">>, <<"backup">>, #{path => Path}, Config).

domain_admin_restore_mnesia(Path, Config) ->
execute_domain_admin_command(<<"mnesia">>, <<"restore">>, #{path => Path}, Config).

domain_admin_dump_mnesia_table(Path, Table, Config) ->
Vars = #{path => Path, table => Table},
execute_domain_admin_command(<<"mnesia">>, <<"dumpTable">>, Vars, Config).

domain_admin_load_mnesia(Path, Config) ->
execute_domain_admin_command(<<"mnesia">>, <<"load">>, #{path => Path}, Config).

domain_admin_change_nodename(ChangeFrom, ChangeTo, Source, Target, Config) ->
Vars = #{<<"fromString">> => ChangeFrom, <<"toString">> => ChangeTo,
<<"source">> => Source, <<"target">> => Target},
execute_domain_admin_command(<<"mnesia">>, <<"changeNodename">>, Vars, Config).

domain_admin_set_master(Node, Config) ->
execute_domain_admin_command(<<"mnesia">>, <<"setMaster">>, Node, Config).

mnesia_info_keys() ->
[<<"all">>,
<<"AAAA">>,
Expand Down
45 changes: 27 additions & 18 deletions priv/graphql/schemas/admin/mnesia.gql
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,44 @@
Allow admin to manage mnesia database
"""

type MnesiaAdminQuery {
info(keys: [String!]!): [MnesiaInfo]
type MnesiaAdminQuery @protected{
info(keys: [String!]!): [MnesiaInfo]
@protected(type: GLOBAL)
}

type MnesiaAdminMutation {
setMaster(node: String!): String
changeNodename(fromString: String!, toString: String!,
source: String!, target: String!): String
backup(path: String!): String
restore(path: String!): String
dump(path: String!): String
dumpTable(path: String!, table: String!): String
load(path: String!): String
installFallback(path: String!): String
type MnesiaAdminMutation @protected{
setMaster(node: String!): String
@protected(type: GLOBAL)
changeNodename(fromString: String!, toString: String!,
source: String!, target: String!): String
@protected(type: GLOBAL)
backup(path: String!): String
@protected(type: GLOBAL)
restore(path: String!): String
@protected(type: GLOBAL)
dump(path: String!): String
@protected(type: GLOBAL)
dumpTable(path: String!, table: String!): String
@protected(type: GLOBAL)
load(path: String!): String
@protected(type: GLOBAL)
installFallback(path: String!): String
@protected(type: GLOBAL)
}

union MnesiaInfo = MnesiaStringResponse | MnesiaListResponse | MnesiaIntResponse

type MnesiaStringResponse {
result: String
key: String
result: String
key: String
}

type MnesiaListResponse {
result: [String]
key: String
result: [String]
key: String
}

type MnesiaIntResponse {
result: Int
key: String
result: Int
key: String
}
3 changes: 0 additions & 3 deletions src/graphql/mongoose_graphql_errors.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ err(_Ctx, ErrorTerm) ->
-spec crash(map(), term()) -> err_msg().
crash(_Ctx, Err = #{type := Type}) ->
?LOG_ERROR(Err#{what => graphql_crash}),
io:format("--------------\n\n"),
io:format("~p\n", [Err]),
io:format("--------------\n\n"),
#{message => <<"Unexpected ", Type/binary, " resolver crash">>,
extensions => #{code => resolver_crash}}.

Expand Down

0 comments on commit 148938c

Please sign in to comment.