From b22a1a87ba390d79b8819e92911fd6b0be2eee77 Mon Sep 17 00:00:00 2001 From: zhanghongtong Date: Sat, 9 Nov 2019 21:26:15 +0800 Subject: [PATCH] Add more test case for emqx_plugins --- src/emqx_plugins.erl | 6 +- test/emqx_plugins_SUITE.erl | 110 +++++++++++++++++++++++++++++------- 2 files changed, 95 insertions(+), 21 deletions(-) diff --git a/src/emqx_plugins.erl b/src/emqx_plugins.erl index 4a597fda6..d17053894 100644 --- a/src/emqx_plugins.erl +++ b/src/emqx_plugins.erl @@ -32,6 +32,10 @@ , load_expand_plugin/1 ]). +-ifdef(TEST). +-compile(export_all). +-compile(nowarn_export_all). +-endif. %%-------------------------------------------------------------------- %% APIs %%-------------------------------------------------------------------- @@ -82,7 +86,7 @@ load_expand_plugin(PluginDir) -> init_expand_plugin_config(PluginDir), Ebin = filename:join([PluginDir, "ebin"]), code:add_patha(Ebin), - Modules = filelib:wildcard(filename:join([Ebin ++ "*.beam"])), + Modules = filelib:wildcard(filename:join([Ebin, "*.beam"])), lists:foreach(fun(Mod) -> Module = list_to_atom(filename:basename(Mod, ".beam")), code:load_file(Module) diff --git a/test/emqx_plugins_SUITE.erl b/test/emqx_plugins_SUITE.erl index 402a0887a..29d3d5964 100644 --- a/test/emqx_plugins_SUITE.erl +++ b/test/emqx_plugins_SUITE.erl @@ -19,6 +19,7 @@ -compile(export_all). -compile(nowarn_export_all). +-include("emqx.hrl"). -include_lib("eunit/include/eunit.hrl"). all() -> emqx_ct:all(?MODULE). @@ -30,7 +31,7 @@ init_per_suite(Config) -> DataPath = proplists:get_value(data_dir, Config), AppPath = filename:join([DataPath, "emqx_mini_plugin"]), - Cmd = lists:flatten(io_lib:format("cd ~s && make", [AppPath])), + Cmd = lists:flatten(io_lib:format("cd ~s && make && cp -r etc _build/default/lib/emqx_mini_plugin/", [AppPath])), ct:pal("Executing ~s~n", [Cmd]), ct:pal("~n ~s~n", [os:cmd(Cmd)]), @@ -43,21 +44,6 @@ init_per_suite(Config) -> Config. -% t_load_expand_plugin(_) -> -% error('TODO'). - -% t_list(_) -> -% error('TODO'). - -% t_find_plugin(_) -> -% error('TODO'). - -% t_unload(_) -> -% error('TODO'). - -% t_init(_) -> -% error('TODO'). - set_sepecial_cfg(_) -> ExpandPath = filename:dirname(code:lib_dir(emqx_mini_plugin)), @@ -69,8 +55,92 @@ end_per_suite(_Config) -> emqx_ct_helpers:stop_apps([]). t_load(_) -> - {error, load_app_fail} = emqx_plugins:load_expand_plugin("./not_existed_path/"), + ?assertEqual([], emqx_plugins:load()), + ?assertEqual([], emqx_plugins:unload()), + + ?assertEqual({error, not_found}, emqx_plugins:load(not_existed_plugin)), + ?assertMatch({ok, _}, emqx_plugins:load(emqx_mini_plugin)), + ?assertEqual({error, already_started}, emqx_plugins:load(emqx_mini_plugin)), + ?assertEqual(ok, emqx_plugins:unload(emqx_mini_plugin)), + ?assertEqual({error, not_started}, emqx_plugins:unload(emqx_mini_plugin)), + + application:set_env(emqx, expand_plugins_dir, undefined), + application:set_env(emqx, plugins_loaded_file, undefined), + ?assertEqual(ignore, emqx_plugins:load()), + ?assertEqual(ignore, emqx_plugins:unload()). + + +t_init_config(_) -> + ConfFile = "emqx_mini_plugin.config", + Data = "[{emqx_mini_plugin,[{mininame ,test}]}].", + file:write_file(ConfFile, list_to_binary(Data)), + ?assertEqual(ok, emqx_plugins:init_config(ConfFile)), + file:delete(ConfFile), + ?assertEqual({ok,test}, application:get_env(emqx_mini_plugin, mininame)). + +t_load_expand_plugin(_) -> + ?assertEqual({error, load_app_fail}, emqx_plugins:load_expand_plugin("./not_existed_path/")). + +t_list(_) -> + ?assertMatch([{plugin, _, _, _, _, _, _, _, _} | _ ], emqx_plugins:list()). + +t_find_plugin(_) -> + ?assertMatch({plugin, emqx_mini_plugin, _, _, _, _, _, _, _}, emqx_plugins:find_plugin(emqx_mini_plugin)). + +t_plugin_type(_) -> + ?assertEqual(auth, emqx_plugins:plugin_type(auth)), + ?assertEqual(protocol, emqx_plugins:plugin_type(protocol)), + ?assertEqual(backend, emqx_plugins:plugin_type(backend)), + ?assertEqual(bridge, emqx_plugins:plugin_type(bridge)), + ?assertEqual(feature, emqx_plugins:plugin_type(undefined)). + +t_with_loaded_file(_) -> + ?assertMatch({error, _}, emqx_plugins:with_loaded_file("./not_existed_path/", fun(_) -> ok end)). + +t_plugin_loaded(_) -> + ?assertEqual(ok, emqx_plugins:plugin_loaded(emqx_mini_plugin, false)), + ?assertEqual(ok, emqx_plugins:plugin_loaded(emqx_mini_plugin, true)). + +t_plugin_unloaded(_) -> + ?assertEqual(ok, emqx_plugins:plugin_unloaded(emqx_mini_plugin, false)), + ?assertEqual(ok, emqx_plugins:plugin_unloaded(emqx_mini_plugin, true)). + +t_plugin(_) -> + try + emqx_plugins:plugin(not_existed_plugin, undefined) + catch + _Error:Reason:_Stacktrace -> + ?assertEqual({plugin_not_found,not_existed_plugin}, Reason) + end, + ?assertMatch({plugin, emqx_mini_plugin, _, _, _, _, _, _, _}, emqx_plugins:plugin(emqx_mini_plugin, undefined)). + +t_filter_plugins(_) -> + ?assertEqual([name1, name2], emqx_plugins:filter_plugins([name1, {name2,true}, {name3, false}])). + +t_load_plugin(_) -> + ok = meck:new(application, [unstick, non_strict, passthrough, no_history]), + ok = meck:expect(application, load, fun(already_loaded_app) -> {error, {already_loaded, already_loaded_app}}; + (error_app) -> {error, error}; + (_) -> ok end), + ok = meck:expect(application, ensure_all_started, fun(already_loaded_app) -> {error, {already_loaded_app, already_loaded}}; + (error_app) -> {error, error}; + (App) -> {ok, App} end), + + ?assertMatch({error, _}, emqx_plugins:load_plugin(#plugin{name = already_loaded_app}, true)), + ?assertMatch({ok, _}, emqx_plugins:load_plugin(#plugin{name = normal}, true)), + ?assertMatch({error,_}, emqx_plugins:load_plugin(#plugin{name = error_app}, true)), + + ok = meck:unload(application). + +t_unload_plugin(_) -> + ok = meck:new(application, [unstick, non_strict, passthrough, no_history]), + ok = meck:expect(application, stop, fun(not_started_app) -> {error, {not_started, not_started_app}}; + (error_app) -> {error, error}; + (_) -> ok end), + + ?assertEqual(ok, emqx_plugins:unload_plugin(not_started_app, true)), + ?assertEqual(ok, emqx_plugins:unload_plugin(normal, true)), + ?assertEqual({error,error}, emqx_plugins:unload_plugin(error_app, true)), + + ok = meck:unload(application). - {error, not_started} = emqx_plugins:unload(emqx_mini_plugin), - {ok, _} = emqx_plugins:load(emqx_mini_plugin), - ok = emqx_plugins:unload(emqx_mini_plugin).