emqx_modules_conf_SUITE.erl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-2024 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_modules_conf_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. %%--------------------------------------------------------------------
  21. %% Setups
  22. %%--------------------------------------------------------------------
  23. all() ->
  24. emqx_common_test_helpers:all(?MODULE).
  25. init_per_suite(Conf) ->
  26. emqx_common_test_helpers:load_config(emqx_modules_schema, <<"gateway {}">>),
  27. emqx_common_test_helpers:start_apps([emqx_conf, emqx_modules]),
  28. Conf.
  29. end_per_suite(_Conf) ->
  30. emqx_common_test_helpers:stop_apps([emqx_modules, emqx_conf]).
  31. init_per_testcase(_CaseName, Conf) ->
  32. Conf.
  33. end_per_testcase(_CaseName, _Conf) ->
  34. [emqx_modules_conf:remove_topic_metrics(T) || T <- emqx_modules_conf:topic_metrics()],
  35. ok.
  36. %%--------------------------------------------------------------------
  37. %% Cases
  38. %%--------------------------------------------------------------------
  39. t_topic_metrics_add_remove(_) ->
  40. ?assertEqual([], emqx_modules_conf:topic_metrics()),
  41. ?assertMatch({ok, _}, emqx_modules_conf:add_topic_metrics(<<"test-topic">>)),
  42. ?assertEqual([<<"test-topic">>], emqx_modules_conf:topic_metrics()),
  43. ?assertEqual(ok, emqx_modules_conf:remove_topic_metrics(<<"test-topic">>)),
  44. ?assertEqual([], emqx_modules_conf:topic_metrics()),
  45. ?assertMatch({error, _}, emqx_modules_conf:remove_topic_metrics(<<"test-topic">>)).
  46. t_topic_metrics_merge_update(_) ->
  47. ?assertEqual([], emqx_modules_conf:topic_metrics()),
  48. ?assertMatch({ok, _}, emqx_modules_conf:add_topic_metrics(<<"test-topic-before-import1">>)),
  49. ?assertMatch({ok, _}, emqx_modules_conf:add_topic_metrics(<<"test-topic-before-import2">>)),
  50. ImportConf = #{
  51. <<"topic_metrics">> =>
  52. [
  53. #{<<"topic">> => <<"imported_topic1">>},
  54. #{<<"topic">> => <<"imported_topic2">>}
  55. ]
  56. },
  57. ?assertMatch({ok, _}, emqx_modules_conf:import_config(ImportConf)),
  58. ExpTopics = [
  59. <<"test-topic-before-import1">>,
  60. <<"test-topic-before-import2">>,
  61. <<"imported_topic1">>,
  62. <<"imported_topic2">>
  63. ],
  64. ?assertEqual(ExpTopics, emqx_modules_conf:topic_metrics()).
  65. t_topic_metrics_update(_) ->
  66. ?assertEqual([], emqx_modules_conf:topic_metrics()),
  67. ?assertMatch({ok, _}, emqx_modules_conf:add_topic_metrics(<<"test-topic-before-update1">>)),
  68. ?assertMatch({ok, _}, emqx_modules_conf:add_topic_metrics(<<"test-topic-before-update2">>)),
  69. UpdConf = [#{<<"topic">> => <<"new_topic1">>}, #{<<"topic">> => <<"new_topic2">>}],
  70. ?assertMatch({ok, _}, emqx_conf:update([topic_metrics], UpdConf, #{override_to => cluster})),
  71. ?assertEqual([<<"new_topic1">>, <<"new_topic2">>], emqx_modules_conf:topic_metrics()).