emqx_acl_cache_SUITE.erl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019-2021 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. t_drain_acl_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_acl_cache),
  63. ct:log("acl caches: ~p", [Caches]),
  64. ?assert(length(Caches) > 0),
  65. emqx_acl_cache:drain_cache(),
  66. ?assertEqual(0, length(gen_server:call(ClientPid, list_acl_cache))),
  67. ct:sleep(100),
  68. {ok, _, _} = emqtt:subscribe(Client, <<"t2">>, 0),
  69. ?assert(length(gen_server:call(ClientPid, list_acl_cache)) > 0),
  70. emqtt:stop(Client).
  71. % optimize??
  72. t_reload_aclfile_and_cleanall(_Config) ->
  73. RasieMsg = fun() -> Self = self(), #{puback => fun(Msg) -> Self ! {puback, Msg} end,
  74. disconnected => fun(_) -> ok end,
  75. publish => fun(_) -> ok end } end,
  76. {ok, Client} = emqtt:start_link([{clientid, <<"emqx_c">>}, {proto_ver, v5},
  77. {msg_handler, RasieMsg()}]),
  78. {ok, _} = emqtt:connect(Client),
  79. {ok, PktId} = emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, qos1),
  80. %% Success publish to broker
  81. receive
  82. {puback, #{packet_id := PktId, reason_code := Rc}} ->
  83. ?assertEqual(16#10, Rc);
  84. _ ->
  85. ?assert(false)
  86. end,
  87. %% Check acl cache list
  88. [ClientPid] = emqx_cm:lookup_channels(<<"emqx_c">>),
  89. ?assert(length(gen_server:call(ClientPid, list_acl_cache)) > 0),
  90. emqtt:stop(Client).
  91. %% @private
  92. testdir(DataPath) ->
  93. Ls = filename:split(DataPath),
  94. filename:join(lists:sublist(Ls, 1, length(Ls) - 1)).
  95. % t_cache_k(_) ->
  96. % error('TODO').
  97. % t_cache_v(_) ->
  98. % error('TODO').
  99. % t_cleanup_acl_cache(_) ->
  100. % error('TODO').
  101. % t_get_oldest_key(_) ->
  102. % error('TODO').
  103. % t_get_newest_key(_) ->
  104. % error('TODO').
  105. % t_get_cache_max_size(_) ->
  106. % error('TODO').
  107. % t_get_cache_size(_) ->
  108. % error('TODO').
  109. % t_dump_acl_cache(_) ->
  110. % error('TODO').
  111. % t_empty_acl_cache(_) ->
  112. % error('TODO').
  113. % t_put_acl_cache(_) ->
  114. % error('TODO').
  115. % t_get_acl_cache(_) ->
  116. % error('TODO').
  117. % t_is_enabled(_) ->
  118. % error('TODO').