emqx_authz_cache_SUITE.erl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019-2023 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 =
  37. case emqx_cm:lookup_channels(<<"emqx_c">>) of
  38. Pids when is_list(Pids) ->
  39. lists:last(Pids);
  40. _ ->
  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 =
  56. case emqx_cm:lookup_channels(<<"emqx_c">>) of
  57. [Pid] when is_pid(Pid) ->
  58. Pid;
  59. Pids when is_list(Pids) ->
  60. lists:last(Pids);
  61. _ ->
  62. {error, not_found}
  63. end,
  64. Caches = gen_server:call(ClientPid, list_authz_cache),
  65. ct:log("authz caches: ~p", [Caches]),
  66. ?assert(length(Caches) > 0),
  67. emqx_authz_cache:drain_cache(),
  68. ?assertEqual(0, length(gen_server:call(ClientPid, list_authz_cache))),
  69. ct:sleep(100),
  70. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  71. ?assert(length(gen_server:call(ClientPid, list_authz_cache)) > 0),
  72. emqtt:stop(Client).