emqx_exhook_api_SUITE.erl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-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_exhook_api_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -define(HOST, "http://127.0.0.1:18083/").
  21. -define(API_VERSION, "v5").
  22. -define(BASE_PATH, "api").
  23. -define(CLUSTER_RPC_SHARD, emqx_cluster_rpc_shard).
  24. -define(CONF_DEFAULT, <<"
  25. exhook {
  26. servers =
  27. [ { name = default,
  28. url = \"http://127.0.0.1:9000\"
  29. }
  30. ]
  31. }
  32. ">>).
  33. all() ->
  34. [t_list, t_get, t_add, t_move_1, t_move_2, t_delete, t_hooks, t_update].
  35. init_per_suite(Config) ->
  36. application:load(emqx_conf),
  37. ok = ekka:start(),
  38. ok = mria_rlog:wait_for_shards([?CLUSTER_RPC_SHARD], infinity),
  39. meck:new(emqx_alarm, [non_strict, passthrough, no_link]),
  40. meck:expect(emqx_alarm, activate, 3, ok),
  41. meck:expect(emqx_alarm, deactivate, 3, ok),
  42. _ = emqx_exhook_demo_svr:start(),
  43. ok = emqx_config:init_load(emqx_exhook_schema, ?CONF_DEFAULT),
  44. emqx_mgmt_api_test_util:init_suite([emqx_exhook]),
  45. [Conf] = emqx:get_config([exhook, servers]),
  46. [{template, Conf} | Config].
  47. end_per_suite(Config) ->
  48. ekka:stop(),
  49. mria:stop(),
  50. mria_mnesia:delete_schema(),
  51. meck:unload(emqx_alarm),
  52. emqx_mgmt_api_test_util:end_suite([emqx_exhook]),
  53. emqx_exhook_demo_svr:stop(),
  54. emqx_exhook_demo_svr:stop(<<"test1">>),
  55. Config.
  56. init_per_testcase(t_add, Config) ->
  57. {ok, _} = emqx_cluster_rpc:start_link(),
  58. _ = emqx_exhook_demo_svr:start(<<"test1">>, 9001),
  59. timer:sleep(200),
  60. Config;
  61. init_per_testcase(_, Config) ->
  62. {ok, _} = emqx_cluster_rpc:start_link(),
  63. timer:sleep(200),
  64. Config.
  65. end_per_testcase(_, Config) ->
  66. case erlang:whereis(node()) of
  67. undefined -> ok;
  68. P ->
  69. erlang:unlink(P),
  70. erlang:exit(P, kill)
  71. end,
  72. Config.
  73. t_list(_) ->
  74. {ok, Data} = request_api(get, api_path(["exhooks"]), "",
  75. auth_header_()),
  76. List = decode_json(Data),
  77. ?assertEqual(1, length(List)),
  78. [Svr] = List,
  79. ?assertMatch(#{name := <<"default">>,
  80. metrics := _,
  81. node_metrics := _,
  82. node_status := _,
  83. hooks := _
  84. }, Svr).
  85. t_get(_) ->
  86. {ok, Data} = request_api(get, api_path(["exhooks", "default"]), "",
  87. auth_header_()),
  88. Svr = decode_json(Data),
  89. ?assertMatch(#{name := <<"default">>,
  90. metrics := _,
  91. node_metrics := _,
  92. node_status := _,
  93. hooks := _
  94. }, Svr).
  95. t_add(Cfg) ->
  96. Template = proplists:get_value(template, Cfg),
  97. Instance = Template#{name => <<"test1">>,
  98. url => "http://127.0.0.1:9001"
  99. },
  100. {ok, Data} = request_api(post, api_path(["exhooks"]), "",
  101. auth_header_(), Instance),
  102. Svr = decode_json(Data),
  103. ?assertMatch(#{name := <<"test1">>,
  104. metrics := _,
  105. node_metrics := _,
  106. node_status := _,
  107. hooks := _}, Svr),
  108. ?assertMatch([<<"default">>, <<"test1">>], emqx_exhook_mgr:running()).
  109. t_move_1(_) ->
  110. Result = request_api(post, api_path(["exhooks", "default", "move"]), "",
  111. auth_header_(),
  112. #{position => bottom, related => <<>>}),
  113. ?assertMatch({ok, <<>>}, Result),
  114. ?assertMatch([<<"test1">>, <<"default">>], emqx_exhook_mgr:running()).
  115. t_move_2(_) ->
  116. Result = request_api(post, api_path(["exhooks", "default", "move"]), "",
  117. auth_header_(),
  118. #{position => before, related => <<"test1">>}),
  119. ?assertMatch({ok, <<>>}, Result),
  120. ?assertMatch([<<"default">>, <<"test1">>], emqx_exhook_mgr:running()).
  121. t_delete(_) ->
  122. Result = request_api(delete, api_path(["exhooks", "test1"]), "",
  123. auth_header_()),
  124. ?assertMatch({ok, <<>>}, Result),
  125. ?assertMatch([<<"default">>], emqx_exhook_mgr:running()).
  126. t_hooks(_Cfg) ->
  127. {ok, Data} = request_api(get, api_path(["exhooks", "default", "hooks"]), "",
  128. auth_header_()),
  129. [Hook1 | _] = decode_json(Data),
  130. ?assertMatch(#{name := _,
  131. params := _,
  132. metrics := _,
  133. node_metrics := _
  134. }, Hook1).
  135. t_update(Cfg) ->
  136. Template = proplists:get_value(template, Cfg),
  137. Instance = Template#{enable => false},
  138. {ok, <<>>} = request_api(put, api_path(["exhooks", "default"]), "",
  139. auth_header_(), Instance),
  140. ?assertMatch([], emqx_exhook_mgr:running()).
  141. decode_json(Data) ->
  142. BinJosn = emqx_json:decode(Data, [return_maps]),
  143. emqx_map_lib:unsafe_atom_key_map(BinJosn).
  144. request_api(Method, Url, Auth) ->
  145. request_api(Method, Url, [], Auth, []).
  146. request_api(Method, Url, QueryParams, Auth) ->
  147. request_api(Method, Url, QueryParams, Auth, []).
  148. request_api(Method, Url, QueryParams, Auth, []) ->
  149. NewUrl = case QueryParams of
  150. "" -> Url;
  151. _ -> Url ++ "?" ++ QueryParams
  152. end,
  153. do_request_api(Method, {NewUrl, [Auth]});
  154. request_api(Method, Url, QueryParams, Auth, Body) ->
  155. NewUrl = case QueryParams of
  156. "" -> Url;
  157. _ -> Url ++ "?" ++ QueryParams
  158. end,
  159. do_request_api(Method, {NewUrl, [Auth], "application/json", emqx_json:encode(Body)}).
  160. do_request_api(Method, Request)->
  161. case httpc:request(Method, Request, [], [{body_format, binary}]) of
  162. {error, socket_closed_remotely} ->
  163. {error, socket_closed_remotely};
  164. {ok, {{"HTTP/1.1", Code, _}, _, Return} }
  165. when Code =:= 200 orelse Code =:= 204 orelse Code =:= 201 ->
  166. {ok, Return};
  167. {ok, {Reason, _, _}} ->
  168. {error, Reason}
  169. end.
  170. auth_header_() ->
  171. AppId = <<"admin">>,
  172. AppSecret = <<"public">>,
  173. auth_header_(binary_to_list(AppId), binary_to_list(AppSecret)).
  174. auth_header_(User, Pass) ->
  175. Encoded = base64:encode_to_string(lists:append([User,":",Pass])),
  176. {"Authorization","Basic " ++ Encoded}.
  177. api_path(Parts)->
  178. ?HOST ++ filename:join([?BASE_PATH, ?API_VERSION] ++ Parts).