chore(optimize): optimize MGMT test cases

This commit is contained in:
Turtle 2021-05-17 13:23:47 +08:00 committed by turtleDeng
parent f21ef50938
commit a7d2f44a5b
3 changed files with 38 additions and 71 deletions

View File

@ -28,7 +28,9 @@ all() ->
emqx_ct:all(?MODULE). emqx_ct:all(?MODULE).
init_per_suite(Cfg) -> init_per_suite(Cfg) ->
emqx_ct_helpers:start_apps([emqx_bridge_mqtt, emqx_rule_engine]), application:load(emqx_modules),
application:load(emqx_bridge_mqtt),
emqx_ct_helpers:start_apps([emqx_rule_engine, emqx_management]),
Cfg. Cfg.
end_per_suite(Cfg) -> end_per_suite(Cfg) ->

View File

@ -34,55 +34,22 @@
-define(BASE_PATH, "api"). -define(BASE_PATH, "api").
all() -> all() ->
[{group, rest_api}]. emqx_ct:all(?MODULE).
groups() ->
[{rest_api,
[sequence],
[ alarms
, apps
, banned
, brokers
, clients
, listeners
, metrics
, nodes
, plugins
, acl_cache
, pubsub
, routes_and_subscriptions
, stats
, data
]
}].
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_ct_helpers:start_apps([emqx_management, emqx_auth_mnesia, emqx_modules]), application:load(emqx_modules),
ekka_mnesia:start(), emqx_ct_helpers:start_apps([emqx_management]),
emqx_mgmt_auth:mnesia(boot),
Config. Config.
end_per_suite(_Config) -> end_per_suite(Config) ->
emqx_ct_helpers:stop_apps([emqx_auth_mnesia, emqx_management, emqx_modules]), emqx_ct_helpers:stop_apps([emqx_management]),
ekka_mnesia:ensure_stopped(). Config.
init_per_testcase(data, Config) ->
ok = emqx_dashboard_admin:mnesia(boot),
application:ensure_all_started(emqx_dashboard),
ok = emqx_rule_registry:mnesia(boot),
application:ensure_all_started(emqx_rule_engine),
Config;
init_per_testcase(_, Config) -> init_per_testcase(_, Config) ->
Config. Config.
end_per_testcase(data, _Config) -> end_per_testcase(_, Config) ->
application:stop(emqx_dahboard), Config.
application:stop(emqx_rule_engine),
ok;
end_per_testcase(_, _Config) ->
ok.
get(Key, ResponseBody) -> get(Key, ResponseBody) ->
maps:get(Key, jiffy:decode(list_to_binary(ResponseBody), [return_maps])). maps:get(Key, jiffy:decode(list_to_binary(ResponseBody), [return_maps])).
@ -101,7 +68,7 @@ is_existing(Name, [_Alarm | More]) ->
is_existing(_Name, []) -> is_existing(_Name, []) ->
false. false.
alarms(_) -> t_alarms(_) ->
emqx_alarm:activate(alarm1), emqx_alarm:activate(alarm1),
emqx_alarm:activate(alarm2), emqx_alarm:activate(alarm2),
@ -134,7 +101,7 @@ alarms(_) ->
?assertNot(lookup_alarm(<<"alarm1">>, maps:get(<<"alarms">>, lists:nth(1, get(<<"data">>, Return5))))), ?assertNot(lookup_alarm(<<"alarm1">>, maps:get(<<"alarms">>, lists:nth(1, get(<<"data">>, Return5))))),
?assertNot(lookup_alarm(<<"alarm2">>, maps:get(<<"alarms">>, lists:nth(1, get(<<"data">>, Return5))))). ?assertNot(lookup_alarm(<<"alarm2">>, maps:get(<<"alarms">>, lists:nth(1, get(<<"data">>, Return5))))).
apps(_) -> t_apps(_) ->
AppId = <<"123456">>, AppId = <<"123456">>,
meck:new(emqx_mgmt_auth, [passthrough, no_history]), meck:new(emqx_mgmt_auth, [passthrough, no_history]),
meck:expect(emqx_mgmt_auth, add_app, 6, fun(_, _, _, _, _, _) -> {error, undefined} end), meck:expect(emqx_mgmt_auth, add_app, 6, fun(_, _, _, _, _, _) -> {error, undefined} end),
@ -172,7 +139,7 @@ apps(_) ->
[App] = get(<<"data">>, Result), [App] = get(<<"data">>, Result),
?assertEqual(<<"admin">>, maps:get(<<"app_id">>, App)). ?assertEqual(<<"admin">>, maps:get(<<"app_id">>, App)).
banned(_) -> t_banned(_) ->
Who = <<"myclient">>, Who = <<"myclient">>,
{ok, _} = request_api(post, api_path(["banned"]), [], {ok, _} = request_api(post, api_path(["banned"]), [],
auth_header_(), #{<<"who">> => Who, auth_header_(), #{<<"who">> => Who,
@ -190,7 +157,7 @@ banned(_) ->
{ok, Result2} = request_api(get, api_path(["banned"]), auth_header_()), {ok, Result2} = request_api(get, api_path(["banned"]), auth_header_()),
?assertEqual([], get(<<"data">>, Result2)). ?assertEqual([], get(<<"data">>, Result2)).
brokers(_) -> t_brokers(_) ->
{ok, _} = request_api(get, api_path(["brokers"]), auth_header_()), {ok, _} = request_api(get, api_path(["brokers"]), auth_header_()),
{ok, _} = request_api(get, api_path(["brokers", atom_to_list(node())]), auth_header_()), {ok, _} = request_api(get, api_path(["brokers", atom_to_list(node())]), auth_header_()),
meck:new(emqx_mgmt, [passthrough, no_history]), meck:new(emqx_mgmt, [passthrough, no_history]),
@ -199,7 +166,7 @@ brokers(_) ->
?assertEqual(<<"undefined">>, get(<<"message">>, Error)), ?assertEqual(<<"undefined">>, get(<<"message">>, Error)),
meck:unload(emqx_mgmt). meck:unload(emqx_mgmt).
clients(_) -> t_clients(_) ->
process_flag(trap_exit, true), process_flag(trap_exit, true),
Username1 = <<"user1">>, Username1 = <<"user1">>,
Username2 = <<"user2">>, Username2 = <<"user2">>,
@ -288,7 +255,7 @@ receive_exit(Count) ->
ct:log("timeout") ct:log("timeout")
end. end.
listeners(_) -> t_listeners(_) ->
{ok, _} = request_api(get, api_path(["listeners"]), auth_header_()), {ok, _} = request_api(get, api_path(["listeners"]), auth_header_()),
{ok, _} = request_api(get, api_path(["nodes", atom_to_list(node()), "listeners"]), auth_header_()), {ok, _} = request_api(get, api_path(["nodes", atom_to_list(node()), "listeners"]), auth_header_()),
meck:new(emqx_mgmt, [passthrough, no_history]), meck:new(emqx_mgmt, [passthrough, no_history]),
@ -299,7 +266,7 @@ listeners(_) ->
maps:get(<<"error">>, maps:get(<<"listeners">>, Error))), maps:get(<<"error">>, maps:get(<<"listeners">>, Error))),
meck:unload(emqx_mgmt). meck:unload(emqx_mgmt).
metrics(_) -> t_metrics(_) ->
{ok, _} = request_api(get, api_path(["metrics"]), auth_header_()), {ok, _} = request_api(get, api_path(["metrics"]), auth_header_()),
{ok, _} = request_api(get, api_path(["nodes", atom_to_list(node()), "metrics"]), auth_header_()), {ok, _} = request_api(get, api_path(["nodes", atom_to_list(node()), "metrics"]), auth_header_()),
meck:new(emqx_mgmt, [passthrough, no_history]), meck:new(emqx_mgmt, [passthrough, no_history]),
@ -307,7 +274,7 @@ metrics(_) ->
{ok, "{\"message\":\"undefined\"}"} = request_api(get, api_path(["nodes", atom_to_list(node()), "metrics"]), auth_header_()), {ok, "{\"message\":\"undefined\"}"} = request_api(get, api_path(["nodes", atom_to_list(node()), "metrics"]), auth_header_()),
meck:unload(emqx_mgmt). meck:unload(emqx_mgmt).
nodes(_) -> t_nodes(_) ->
{ok, _} = request_api(get, api_path(["nodes"]), auth_header_()), {ok, _} = request_api(get, api_path(["nodes"]), auth_header_()),
{ok, _} = request_api(get, api_path(["nodes", atom_to_list(node())]), auth_header_()), {ok, _} = request_api(get, api_path(["nodes", atom_to_list(node())]), auth_header_()),
meck:new(emqx_mgmt, [passthrough, no_history]), meck:new(emqx_mgmt, [passthrough, no_history]),
@ -317,7 +284,8 @@ nodes(_) ->
?assertEqual(<<"undefined">>, maps:get(<<"error">>, Error)), ?assertEqual(<<"undefined">>, maps:get(<<"error">>, Error)),
meck:unload(emqx_mgmt). meck:unload(emqx_mgmt).
plugins(_) -> t_plugins(_) ->
application:ensure_all_started(emqx_auth_mnesia),
{ok, Plugins1} = request_api(get, api_path(["plugins"]), auth_header_()), {ok, Plugins1} = request_api(get, api_path(["plugins"]), auth_header_()),
[Plugins11] = filter(get(<<"data">>, Plugins1), <<"node">>, atom_to_binary(node(), utf8)), [Plugins11] = filter(get(<<"data">>, Plugins1), <<"node">>, atom_to_binary(node(), utf8)),
[Plugin1] = filter(maps:get(<<"plugins">>, Plugins11), <<"name">>, <<"emqx_auth_mnesia">>), [Plugin1] = filter(maps:get(<<"plugins">>, Plugins11), <<"name">>, <<"emqx_auth_mnesia">>),
@ -370,9 +338,10 @@ plugins(_) ->
atom_to_list(emqx_auth_mnesia), atom_to_list(emqx_auth_mnesia),
"unload"]), "unload"]),
auth_header_()), auth_header_()),
?assertEqual(<<"not_started">>, get(<<"message">>, Error2)). ?assertEqual(<<"not_started">>, get(<<"message">>, Error2)),
application:stop(emqx_auth_mnesia).
acl_cache(_) -> t_acl_cache(_) ->
ClientId = <<"client1">>, ClientId = <<"client1">>,
Topic = <<"mytopic">>, Topic = <<"mytopic">>,
{ok, C1} = emqtt:start_link(#{clientid => ClientId}), {ok, C1} = emqtt:start_link(#{clientid => ClientId}),
@ -395,7 +364,7 @@ acl_cache(_) ->
?assertEqual(0, length(Caches3)), ?assertEqual(0, length(Caches3)),
ok = emqtt:disconnect(C1). ok = emqtt:disconnect(C1).
pubsub(_) -> t_pubsub(_) ->
Qos1Received = emqx_metrics:val('messages.qos1.received'), Qos1Received = emqx_metrics:val('messages.qos1.received'),
Qos2Received = emqx_metrics:val('messages.qos2.received'), Qos2Received = emqx_metrics:val('messages.qos2.received'),
Received = emqx_metrics:val('messages.received'), Received = emqx_metrics:val('messages.received'),
@ -514,7 +483,7 @@ loop(Data) ->
?assertEqual(0, maps:get(<<"code">>, H)), ?assertEqual(0, maps:get(<<"code">>, H)),
loop(T). loop(T).
routes_and_subscriptions(_) -> t_routes_and_subscriptions(_) ->
ClientId = <<"myclient">>, ClientId = <<"myclient">>,
Topic = <<"mytopic">>, Topic = <<"mytopic">>,
{ok, NonRoute} = request_api(get, api_path(["routes"]), auth_header_()), {ok, NonRoute} = request_api(get, api_path(["routes"]), auth_header_()),
@ -559,7 +528,7 @@ routes_and_subscriptions(_) ->
ok = emqtt:disconnect(C1). ok = emqtt:disconnect(C1).
stats(_) -> t_stats(_) ->
{ok, _} = request_api(get, api_path(["stats"]), auth_header_()), {ok, _} = request_api(get, api_path(["stats"]), auth_header_()),
{ok, _} = request_api(get, api_path(["nodes", atom_to_list(node()), "stats"]), auth_header_()), {ok, _} = request_api(get, api_path(["nodes", atom_to_list(node()), "stats"]), auth_header_()),
meck:new(emqx_mgmt, [passthrough, no_history]), meck:new(emqx_mgmt, [passthrough, no_history]),
@ -568,7 +537,11 @@ stats(_) ->
?assertEqual(<<"undefined">>, get(<<"message">>, Return)), ?assertEqual(<<"undefined">>, get(<<"message">>, Return)),
meck:unload(emqx_mgmt). meck:unload(emqx_mgmt).
data(_) -> t_data(_) ->
ok = emqx_rule_registry:mnesia(boot),
ok = emqx_dashboard_admin:mnesia(boot),
application:ensure_all_started(emqx_rule_engine),
application:ensure_all_started(emqx_dashboard),
{ok, Data} = request_api(post, api_path(["data","export"]), [], auth_header_(), [#{}]), {ok, Data} = request_api(post, api_path(["data","export"]), [], auth_header_(), [#{}]),
#{<<"filename">> := Filename, <<"node">> := Node} = emqx_ct_http:get_http_data(Data), #{<<"filename">> := Filename, <<"node">> := Node} = emqx_ct_http:get_http_data(Data),
{ok, DataList} = request_api(get, api_path(["data","export"]), auth_header_()), {ok, DataList} = request_api(get, api_path(["data","export"]), auth_header_()),
@ -576,7 +549,8 @@ data(_) ->
?assertMatch({ok, _}, request_api(post, api_path(["data","import"]), [], auth_header_(), #{<<"filename">> => Filename, <<"node">> => Node})), ?assertMatch({ok, _}, request_api(post, api_path(["data","import"]), [], auth_header_(), #{<<"filename">> => Filename, <<"node">> => Node})),
?assertMatch({ok, _}, request_api(post, api_path(["data","import"]), [], auth_header_(), #{<<"filename">> => Filename})), ?assertMatch({ok, _}, request_api(post, api_path(["data","import"]), [], auth_header_(), #{<<"filename">> => Filename})),
application:stop(emqx_rule_engine),
application:stop(emqx_dahboard),
ok. ok.
request_api(Method, Url, Auth) -> request_api(Method, Url, Auth) ->

View File

@ -28,24 +28,15 @@ all() ->
emqx_ct:all(?MODULE). emqx_ct:all(?MODULE).
init_per_suite(Cfg) -> init_per_suite(Cfg) ->
emqx_ct_helpers:start_apps([emqx_web_hook, application:load(emqx_modules),
emqx_bridge_mqtt, application:load(emqx_web_hook),
emqx_rule_engine, emqx_ct_helpers:start_apps([emqx_rule_engine, emqx_management]),
emqx_modules,
emqx_management,
emqx_dashboard]),
ok = ekka_mnesia:start(),
ok = emqx_rule_registry:mnesia(boot), ok = emqx_rule_registry:mnesia(boot),
ok = emqx_rule_engine:load_providers(), ok = emqx_rule_engine:load_providers(),
Cfg. Cfg.
end_per_suite(Cfg) -> end_per_suite(Cfg) ->
emqx_ct_helpers:stop_apps([emqx_dashboard, emqx_ct_helpers:stop_apps([emqx_management, emqx_rule_engine]),
emqx_management,
emqx_modules,
emqx_rule_engine,
emqx_bridge_mqtt,
emqx_web_hook]),
Cfg. Cfg.
get_data_path() -> get_data_path() ->