emqx_acl_cache_SUITE.erl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020 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_acl_cache_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. all() -> emqx_ct:all(?MODULE).
  21. init_per_suite(Config) ->
  22. emqx_ct_helpers:boot_modules(all),
  23. emqx_ct_helpers:start_apps([]),
  24. Config.
  25. end_per_suite(_Config) ->
  26. emqx_ct_helpers:stop_apps([]).
  27. %%--------------------------------------------------------------------
  28. %% Test cases
  29. %%--------------------------------------------------------------------
  30. t_clean_acl_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_acl_cache),
  44. ct:log("acl caches: ~p", [Caches]),
  45. ?assert(length(Caches) > 0),
  46. erlang:send(ClientPid, clean_acl_cache),
  47. ?assertEqual(0, length(gen_server:call(ClientPid, list_acl_cache))),
  48. emqtt:stop(Client).
  49. % optimize??
  50. t_reload_aclfile_and_cleanall(_Config) ->
  51. RasieMsg = fun() -> Self = self(), #{puback => fun(Msg) -> Self ! {puback, Msg} end,
  52. disconnected => fun(_) -> ok end,
  53. publish => fun(_) -> ok end } end,
  54. {ok, Client} = emqtt:start_link([{clientid, <<"emqx_c">>}, {proto_ver, v5},
  55. {msg_handler, RasieMsg()}]),
  56. {ok, _} = emqtt:connect(Client),
  57. {ok, PktId} = emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, qos1),
  58. %% Success publish to broker
  59. receive
  60. {puback, #{packet_id := PktId, reason_code := Rc}} ->
  61. ?assertEqual(16#10, Rc);
  62. _ ->
  63. ?assert(false)
  64. end,
  65. %% Check acl cache list
  66. [ClientPid] = emqx_cm:lookup_channels(<<"emqx_c">>),
  67. ?assert(length(gen_server:call(ClientPid, list_acl_cache)) > 0),
  68. emqtt:stop(Client).
  69. %% @private
  70. testdir(DataPath) ->
  71. Ls = filename:split(DataPath),
  72. filename:join(lists:sublist(Ls, 1, length(Ls) - 1)).
  73. % t_cache_k(_) ->
  74. % error('TODO').
  75. % t_cache_v(_) ->
  76. % error('TODO').
  77. % t_cleanup_acl_cache(_) ->
  78. % error('TODO').
  79. % t_get_oldest_key(_) ->
  80. % error('TODO').
  81. % t_get_newest_key(_) ->
  82. % error('TODO').
  83. % t_get_cache_max_size(_) ->
  84. % error('TODO').
  85. % t_get_cache_size(_) ->
  86. % error('TODO').
  87. % t_dump_acl_cache(_) ->
  88. % error('TODO').
  89. % t_empty_acl_cache(_) ->
  90. % error('TODO').
  91. % t_put_acl_cache(_) ->
  92. % error('TODO').
  93. % t_get_acl_cache(_) ->
  94. % error('TODO').
  95. % t_is_enabled(_) ->
  96. % error('TODO').