emqx_plugins_tests.erl 4.6 KB

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