emqx_plugins_SUITE.erl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_plugins_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("emqx/include/emqx.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. all() -> emqx_ct:all(?MODULE).
  22. init_per_suite(Config) ->
  23. %% Compile extra plugin code
  24. DataPath = proplists:get_value(data_dir, Config),
  25. AppPath = filename:join([DataPath, "emqx_mini_plugin"]),
  26. Cmd = lists:flatten(io_lib:format("cd ~s && make && cp -r etc _build/default/lib/emqx_mini_plugin/", [AppPath])),
  27. ct:pal("Executing ~s~n", [Cmd]),
  28. ct:pal("~n ~s~n", [os:cmd(Cmd)]),
  29. code:add_path(filename:join([AppPath, "_build", "default", "lib", "emqx_mini_plugin", "ebin"])),
  30. put(loaded_file, filename:join([DataPath, "loaded_plugins"])),
  31. emqx_ct_helpers:boot_modules([]),
  32. emqx_ct_helpers:start_apps([], fun set_sepecial_cfg/1),
  33. Config.
  34. set_sepecial_cfg(_) ->
  35. ExpandPath = filename:dirname(code:lib_dir(emqx_mini_plugin)),
  36. application:set_env(emqx, plugins_loaded_file, get(loaded_file)),
  37. application:set_env(emqx, expand_plugins_dir, ExpandPath),
  38. ok.
  39. end_per_suite(_Config) ->
  40. emqx_ct_helpers:stop_apps([]).
  41. t_load(_) ->
  42. ?assertEqual(ok, emqx_plugins:load()),
  43. ?assertEqual(ok, emqx_plugins:unload()),
  44. ?assertEqual({error, not_found}, emqx_plugins:load(not_existed_plugin)),
  45. ?assertEqual({error, parse_config_file_failed}, emqx_plugins:load(emqx_mini_plugin)),
  46. ?assertEqual({error, not_started}, emqx_plugins:unload(emqx_mini_plugin)),
  47. application:set_env(emqx, expand_plugins_dir, undefined),
  48. application:set_env(emqx, plugins_loaded_file, undefined),
  49. ?assertEqual(ignore, emqx_plugins:load()),
  50. ?assertEqual(ignore, emqx_plugins:unload()).
  51. t_init_config(_) ->
  52. ConfFile = "emqx_mini_plugin.config",
  53. Data = "[{emqx_mini_plugin,[{mininame ,test}]}].",
  54. file:write_file(ConfFile, list_to_binary(Data)),
  55. ?assertEqual(ok, emqx_plugins:init_config(ConfFile)),
  56. file:delete(ConfFile),
  57. ?assertEqual({ok,test}, application:get_env(emqx_mini_plugin, mininame)).
  58. t_load_expand_plugin(_) ->
  59. ?assertEqual({error, load_app_fail}, emqx_plugins:load_expand_plugin("./not_existed_path/")).
  60. t_list(_) ->
  61. ?assertMatch([{plugin, _, _, _, _, _, _, _} | _ ], emqx_plugins:list()).
  62. t_find_plugin(_) ->
  63. ?assertMatch({plugin, emqx_mini_plugin, _, _, _, _, _, _}, emqx_plugins:find_plugin(emqx_mini_plugin)).
  64. t_plugin_type(_) ->
  65. ?assertEqual(auth, emqx_plugins:plugin_type(auth)),
  66. ?assertEqual(protocol, emqx_plugins:plugin_type(protocol)),
  67. ?assertEqual(backend, emqx_plugins:plugin_type(backend)),
  68. ?assertEqual(bridge, emqx_plugins:plugin_type(bridge)),
  69. ?assertEqual(feature, emqx_plugins:plugin_type(undefined)).
  70. t_with_loaded_file(_) ->
  71. ?assertMatch({error, _}, emqx_plugins:with_loaded_file("./not_existed_path/", fun(_) -> ok end)).
  72. t_plugin_loaded(_) ->
  73. ?assertEqual(ok, emqx_plugins:plugin_loaded(emqx_mini_plugin, false)),
  74. ?assertEqual(ok, emqx_plugins:plugin_loaded(emqx_mini_plugin, true)).
  75. t_plugin_unloaded(_) ->
  76. ?assertEqual(ok, emqx_plugins:plugin_unloaded(emqx_mini_plugin, false)),
  77. ?assertEqual(ok, emqx_plugins:plugin_unloaded(emqx_mini_plugin, true)).
  78. t_plugin(_) ->
  79. try
  80. emqx_plugins:plugin(not_existed_plugin, undefined)
  81. catch
  82. _Error:Reason:_Stacktrace ->
  83. ?assertEqual({plugin_not_found,not_existed_plugin}, Reason)
  84. end,
  85. ?assertMatch({plugin, emqx_mini_plugin, _, _, _, _, _, _}, emqx_plugins:plugin(emqx_mini_plugin, undefined)).
  86. t_filter_plugins(_) ->
  87. ?assertEqual([name1, name2], emqx_plugins:filter_plugins([name1, {name2,true}, {name3, false}])).
  88. t_load_plugin(_) ->
  89. ok = meck:new(application, [unstick, non_strict, passthrough, no_history]),
  90. ok = meck:expect(application, load, fun(already_loaded_app) -> {error, {already_loaded, already_loaded_app}};
  91. (error_app) -> {error, error};
  92. (_) -> ok end),
  93. ok = meck:expect(application, ensure_all_started, fun(already_loaded_app) -> {error, {already_loaded_app, already_loaded}};
  94. (error_app) -> {error, error};
  95. (App) -> {ok, App} end),
  96. ok = meck:new(emqx_plugins, [unstick, non_strict, passthrough, no_history]),
  97. ok = meck:expect(emqx_plugins, generate_configs, fun(_) -> ok end),
  98. ok = meck:expect(emqx_plugins, apply_configs, fun(_) -> ok end),
  99. ?assertMatch({error, _}, emqx_plugins:load_plugin(already_loaded_app, true)),
  100. ?assertMatch(ok, emqx_plugins:load_plugin(normal, true)),
  101. ?assertMatch({error,_}, emqx_plugins:load_plugin(error_app, true)),
  102. ok = meck:unload(emqx_plugins),
  103. ok = meck:unload(application).
  104. t_unload_plugin(_) ->
  105. ok = meck:new(application, [unstick, non_strict, passthrough, no_history]),
  106. ok = meck:expect(application, stop, fun(not_started_app) -> {error, {not_started, not_started_app}};
  107. (error_app) -> {error, error};
  108. (_) -> ok end),
  109. ?assertEqual(ok, emqx_plugins:unload_plugin(not_started_app, true)),
  110. ?assertEqual(ok, emqx_plugins:unload_plugin(normal, true)),
  111. ?assertEqual({error,error}, emqx_plugins:unload_plugin(error_app, true)),
  112. ok = meck:unload(application).