emqx_plugins_tests.erl 4.4 KB

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