emqx_plugins_tests.erl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. ensure_configured_test() ->
  19. try test_ensure_configured()
  20. after emqx_plugins:put_configured([])
  21. end.
  22. test_ensure_configured() ->
  23. ok = emqx_plugins:put_configured([]),
  24. P1 =#{name_vsn => "p-1", enable => true},
  25. P2 =#{name_vsn => "p-2", enable => true},
  26. P3 =#{name_vsn => "p-3", enable => false},
  27. emqx_plugins:ensure_configured(P1, front),
  28. emqx_plugins:ensure_configured(P2, {before, <<"p-1">>}),
  29. emqx_plugins:ensure_configured(P3, {before, <<"p-1">>}),
  30. ?assertEqual([P2, P3, P1], emqx_plugins:configured()),
  31. ?assertThrow(#{error := "position_anchor_plugin_not_configured"},
  32. emqx_plugins:ensure_configured(P3, {before, <<"unknown-x">>})).
  33. read_plugin_test() ->
  34. with_rand_install_dir(
  35. fun(_Dir) ->
  36. NameVsn = "bar-5",
  37. InfoFile = emqx_plugins:info_file(NameVsn),
  38. FakeInfo = "name=bar, rel_vsn=\"5\", rel_apps=[justname_no_vsn],"
  39. "description=\"desc bar\"",
  40. try
  41. ok = write_file(InfoFile, FakeInfo),
  42. ?assertMatch({error, #{error := "bad_rel_apps"}},
  43. emqx_plugins:read_plugin(NameVsn, #{}))
  44. after
  45. emqx_plugins:purge(NameVsn)
  46. end
  47. end).
  48. with_rand_install_dir(F) ->
  49. N = rand:uniform(10000000),
  50. TmpDir = integer_to_list(N),
  51. OriginalInstallDir = emqx_plugins:install_dir(),
  52. ok = filelib:ensure_dir(filename:join([TmpDir, "foo"])),
  53. ok = emqx_plugins:put_config(install_dir, TmpDir),
  54. try
  55. F(TmpDir)
  56. after
  57. file:del_dir_r(TmpDir),
  58. ok = emqx_plugins:put_config(install_dir, OriginalInstallDir)
  59. end.
  60. write_file(Path, Content) ->
  61. ok = filelib:ensure_dir(Path),
  62. file:write_file(Path, Content).
  63. %% delete package should mostly work and return ok
  64. %% but it may fail in case the path is a directory
  65. %% or if the file is read-only
  66. delete_package_test() ->
  67. with_rand_install_dir(
  68. fun(_Dir) ->
  69. File = emqx_plugins:pkg_file("a-1"),
  70. ok = write_file(File, "a"),
  71. ok = emqx_plugins:delete_package("a-1"),
  72. %% delete again should be ok
  73. ok = emqx_plugins:delete_package("a-1"),
  74. Dir = File,
  75. ok = filelib:ensure_dir(filename:join([Dir, "foo"])),
  76. ?assertMatch({error, _}, emqx_plugins:delete_package("a-1"))
  77. end).
  78. %% purge plugin's install dir should mostly work and return ok
  79. %% but it may fail in case the dir is read-only
  80. purge_test() ->
  81. with_rand_install_dir(
  82. fun(_Dir) ->
  83. File = emqx_plugins:info_file("a-1"),
  84. Dir = emqx_plugins:dir("a-1"),
  85. ok = filelib:ensure_dir(File),
  86. ?assertMatch({ok, _}, file:read_file_info(Dir)),
  87. ?assertEqual(ok, emqx_plugins:purge("a-1")),
  88. %% assert the dir is gone
  89. ?assertMatch({error, enoent}, file:read_file_info(Dir)),
  90. %% write a file for the dir path
  91. ok = file:write_file(Dir, "a"),
  92. ?assertEqual(ok, emqx_plugins:purge("a-1"))
  93. end).