emqx_authz_cache_SUITE.erl 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_authz_cache_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. all() -> emqx_common_test_helpers:all(?MODULE).
  21. init_per_suite(Config) ->
  22. emqx_common_test_helpers:boot_modules(all),
  23. emqx_common_test_helpers:start_apps([]),
  24. Config.
  25. end_per_suite(_Config) ->
  26. emqx_common_test_helpers:stop_apps([]).
  27. %%--------------------------------------------------------------------
  28. %% Test cases
  29. %%--------------------------------------------------------------------
  30. t_clean_authz_cache(_) ->
  31. {ok, Client} = emqtt:start_link([{clientid, <<"emqx_c">>}]),
  32. {ok, _} = emqtt:connect(Client),
  33. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  34. emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, 0),
  35. ct:sleep(100),
  36. ClientPid = case emqx_cm:lookup_channels(<<"emqx_c">>) of
  37. [Pid] when is_pid(Pid) ->
  38. Pid;
  39. Pids when is_list(Pids) ->
  40. lists:last(Pids);
  41. _ -> {error, not_found}
  42. end,
  43. Caches = gen_server:call(ClientPid, list_authz_cache),
  44. ct:log("authz caches: ~p", [Caches]),
  45. ?assert(length(Caches) > 0),
  46. erlang:send(ClientPid, clean_authz_cache),
  47. ?assertEqual(0, length(gen_server:call(ClientPid, list_authz_cache))),
  48. emqtt:stop(Client).
  49. t_drain_authz_cache(_) ->
  50. {ok, Client} = emqtt:start_link([{clientid, <<"emqx_c">>}]),
  51. {ok, _} = emqtt:connect(Client),
  52. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  53. emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, 0),
  54. ct:sleep(100),
  55. ClientPid = case emqx_cm:lookup_channels(<<"emqx_c">>) of
  56. [Pid] when is_pid(Pid) ->
  57. Pid;
  58. Pids when is_list(Pids) ->
  59. lists:last(Pids);
  60. _ -> {error, not_found}
  61. end,
  62. Caches = gen_server:call(ClientPid, list_authz_cache),
  63. ct:log("authz caches: ~p", [Caches]),
  64. ?assert(length(Caches) > 0),
  65. emqx_authz_cache:drain_cache(),
  66. ?assertEqual(0, length(gen_server:call(ClientPid, list_authz_cache))),
  67. ct:sleep(100),
  68. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  69. ?assert(length(gen_server:call(ClientPid, list_authz_cache)) > 0),
  70. emqtt:stop(Client).