emqx_plugins_tests.erl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019-2022 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_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. -compile(nowarn_export_all).
  19. -compile(export_all).
  20. ensure_configured_test_todo() ->
  21. meck_emqx(),
  22. try test_ensure_configured()
  23. after emqx_plugins:put_configured([])
  24. end,
  25. meck:unload(emqx).
  26. test_ensure_configured() ->
  27. ok = emqx_plugins:put_configured([]),
  28. P1 =#{name_vsn => "p-1", enable => true},
  29. P2 =#{name_vsn => "p-2", enable => true},
  30. P3 =#{name_vsn => "p-3", enable => false},
  31. emqx_plugins:ensure_configured(P1, front),
  32. emqx_plugins:ensure_configured(P2, {before, <<"p-1">>}),
  33. emqx_plugins:ensure_configured(P3, {before, <<"p-1">>}),
  34. ?assertEqual([P2, P3, P1], emqx_plugins:configured()),
  35. ?assertThrow(#{error := "position_anchor_plugin_not_configured"},
  36. emqx_plugins:ensure_configured(P3, {before, <<"unknown-x">>})).
  37. read_plugin_test() ->
  38. meck_emqx(),
  39. with_rand_install_dir(
  40. fun(_Dir) ->
  41. NameVsn = "bar-5",
  42. InfoFile = emqx_plugins:info_file(NameVsn),
  43. FakeInfo = "name=bar, rel_vsn=\"5\", rel_apps=[justname_no_vsn],"
  44. "description=\"desc bar\"",
  45. try
  46. ok = write_file(InfoFile, FakeInfo),
  47. ?assertMatch({error, #{error := "bad_rel_apps"}},
  48. emqx_plugins:read_plugin(NameVsn, #{}))
  49. after
  50. emqx_plugins:purge(NameVsn)
  51. end
  52. end),
  53. meck:unload(emqx).
  54. with_rand_install_dir(F) ->
  55. N = rand:uniform(10000000),
  56. TmpDir = integer_to_list(N),
  57. OriginalInstallDir = emqx_plugins:install_dir(),
  58. ok = filelib:ensure_dir(filename:join([TmpDir, "foo"])),
  59. ok = emqx_plugins:put_config(install_dir, TmpDir),
  60. try
  61. F(TmpDir)
  62. after
  63. file:del_dir_r(TmpDir),
  64. ok = emqx_plugins:put_config(install_dir, OriginalInstallDir)
  65. end.
  66. write_file(Path, Content) ->
  67. ok = filelib:ensure_dir(Path),
  68. file:write_file(Path, Content).
  69. %% delete package should mostly work and return ok
  70. %% but it may fail in case the path is a directory
  71. %% or if the file is read-only
  72. delete_package_test() ->
  73. meck_emqx(),
  74. with_rand_install_dir(
  75. fun(_Dir) ->
  76. File = emqx_plugins:pkg_file("a-1"),
  77. ok = write_file(File, "a"),
  78. ok = emqx_plugins:delete_package("a-1"),
  79. %% delete again should be ok
  80. ok = emqx_plugins:delete_package("a-1"),
  81. Dir = File,
  82. ok = filelib:ensure_dir(filename:join([Dir, "foo"])),
  83. ?assertMatch({error, _}, emqx_plugins:delete_package("a-1"))
  84. end),
  85. meck:unload(emqx).
  86. %% purge plugin's install dir should mostly work and return ok
  87. %% but it may fail in case the dir is read-only
  88. purge_test() ->
  89. meck_emqx(),
  90. with_rand_install_dir(
  91. fun(_Dir) ->
  92. File = emqx_plugins:info_file("a-1"),
  93. Dir = emqx_plugins:dir("a-1"),
  94. ok = filelib:ensure_dir(File),
  95. ?assertMatch({ok, _}, file:read_file_info(Dir)),
  96. ?assertEqual(ok, emqx_plugins:purge("a-1")),
  97. %% assert the dir is gone
  98. ?assertMatch({error, enoent}, file:read_file_info(Dir)),
  99. %% write a file for the dir path
  100. ok = file:write_file(Dir, "a"),
  101. ?assertEqual(ok, emqx_plugins:purge("a-1"))
  102. end),
  103. meck:unload(emqx).
  104. meck_emqx() ->
  105. meck:new(emqx, [unstick, passthrough]),
  106. meck:expect(emqx, update_config,
  107. fun(Path, Values, _Opts) ->
  108. emqx_config:put(Path, Values)
  109. end),
  110. %meck:expect(emqx, get_config,
  111. % fun(KeyPath, Default) ->
  112. % Map = emqx:get_raw_config(KeyPath, Default),
  113. % Map1 = emqx_map_lib:safe_atom_key_map(Map),
  114. % case Map1 of
  115. % #{states := Plugins} ->
  116. % Map1#{states => [emqx_map_lib:safe_atom_key_map(P) ||P <- Plugins]};
  117. % _ -> Map1
  118. % end
  119. % end),
  120. ok.