emqx_mountpoint_SUITE.erl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2018-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_mountpoint_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -import(
  20. emqx_mountpoint,
  21. [
  22. mount/2,
  23. unmount/2,
  24. replvar/2
  25. ]
  26. ).
  27. -include_lib("emqx/include/emqx.hrl").
  28. -include_lib("emqx/include/emqx_mqtt.hrl").
  29. -include_lib("eunit/include/eunit.hrl").
  30. all() -> emqx_common_test_helpers:all(?MODULE).
  31. t_mount(_) ->
  32. Msg = emqx_message:make(<<"clientid">>, <<"topic">>, <<"payload">>),
  33. TopicFilters = [{<<"topic">>, #{qos => 2}}],
  34. ?assertEqual(<<"topic">>, mount(undefined, <<"topic">>)),
  35. ?assertEqual(Msg, mount(undefined, Msg)),
  36. ?assertEqual(TopicFilters, mount(undefined, TopicFilters)),
  37. ?assertEqual(
  38. <<"device/1/topic">>,
  39. mount(<<"device/1/">>, <<"topic">>)
  40. ),
  41. ?assertEqual(
  42. Msg#message{topic = <<"device/1/topic">>},
  43. mount(<<"device/1/">>, Msg)
  44. ),
  45. ?assertEqual(
  46. [{<<"device/1/topic">>, #{qos => 2}}],
  47. mount(<<"device/1/">>, TopicFilters)
  48. ).
  49. t_mount_share(_) ->
  50. T = {TopicFilter, Opts} = emqx_topic:parse(<<"$share/group/topic">>),
  51. TopicFilters = [T],
  52. ?assertEqual(TopicFilter, #share{group = <<"group">>, topic = <<"topic">>}),
  53. ?assertEqual(
  54. TopicFilter,
  55. mount(undefined, TopicFilter)
  56. ),
  57. ?assertEqual(
  58. #share{group = <<"group">>, topic = <<"device/1/topic">>},
  59. mount(<<"device/1/">>, TopicFilter)
  60. ),
  61. ?assertEqual(
  62. [{#share{group = <<"group">>, topic = <<"device/1/topic">>}, Opts}],
  63. mount(<<"device/1/">>, TopicFilters)
  64. ).
  65. t_unmount(_) ->
  66. Msg = emqx_message:make(<<"clientid">>, <<"device/1/topic">>, <<"payload">>),
  67. ?assertEqual(<<"topic">>, unmount(undefined, <<"topic">>)),
  68. ?assertEqual(Msg, unmount(undefined, Msg)),
  69. ?assertEqual(<<"topic">>, unmount(<<"device/1/">>, <<"device/1/topic">>)),
  70. ?assertEqual(Msg#message{topic = <<"topic">>}, unmount(<<"device/1/">>, Msg)),
  71. ?assertEqual(<<"device/1/topic">>, unmount(<<"device/2/">>, <<"device/1/topic">>)),
  72. ?assertEqual(Msg#message{topic = <<"device/1/topic">>}, unmount(<<"device/2/">>, Msg)).
  73. t_unmount_share(_) ->
  74. {TopicFilter, _Opts} = emqx_topic:parse(<<"$share/group/topic">>),
  75. MountedTopicFilter = #share{group = <<"group">>, topic = <<"device/1/topic">>},
  76. ?assertEqual(TopicFilter, #share{group = <<"group">>, topic = <<"topic">>}),
  77. ?assertEqual(
  78. TopicFilter,
  79. unmount(undefined, TopicFilter)
  80. ),
  81. ?assertEqual(
  82. #share{group = <<"group">>, topic = <<"topic">>},
  83. unmount(<<"device/1/">>, MountedTopicFilter)
  84. ).
  85. t_replvar(_) ->
  86. ?assertEqual(undefined, replvar(undefined, #{})),
  87. ?assertEqual(
  88. <<"mount/user/clientid/">>,
  89. replvar(
  90. <<"mount/${username}/${clientid}/">>,
  91. #{
  92. clientid => <<"clientid">>,
  93. username => <<"user">>
  94. }
  95. )
  96. ),
  97. ?assertEqual(
  98. <<"mount/${username}/clientid/">>,
  99. replvar(
  100. <<"mount/${username}/${clientid}/">>,
  101. #{
  102. clientid => <<"clientid">>,
  103. username => undefined
  104. }
  105. )
  106. ),
  107. ?assertEqual(
  108. <<"mount/g1/clientid/">>,
  109. replvar(
  110. <<"mount/${client_attrs.group}/${clientid}/">>,
  111. #{
  112. clientid => <<"clientid">>,
  113. client_attrs => #{<<"group">> => <<"g1">>}
  114. }
  115. )
  116. ),
  117. ?assertEqual(
  118. <<"mount/${client_attrs.group}/clientid/">>,
  119. replvar(
  120. <<"mount/${client_attrs.group}/${clientid}/">>,
  121. #{
  122. clientid => <<"clientid">>
  123. }
  124. )
  125. ),
  126. ?assertEqual(
  127. <<"mount/${not.allowed}/clientid/">>,
  128. replvar(
  129. <<"mount/${not.allowed}/${clientid}/">>,
  130. #{
  131. clientid => <<"clientid">>
  132. }
  133. )
  134. ).