emqx_resource_schema_tests.erl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2023-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_resource_schema_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. -include_lib("common_test/include/ct.hrl").
  19. %%===========================================================================
  20. %% Test cases
  21. %%===========================================================================
  22. health_check_interval_validator_test_() ->
  23. [
  24. ?_assertMatch(
  25. #{<<"resource_opts">> := #{<<"health_check_interval">> := 150_000}},
  26. parse_and_check_webhook_bridge(webhook_bridge_health_check_hocon(<<"150s">>))
  27. ),
  28. ?_assertMatch(
  29. #{<<"resource_opts">> := #{<<"health_check_interval">> := 3_600_000}},
  30. parse_and_check_webhook_bridge(webhook_bridge_health_check_hocon(<<"3600000ms">>))
  31. ),
  32. ?_assertThrow(
  33. {_, [
  34. #{
  35. kind := validation_error,
  36. reason := <<"Health Check Interval (3600001ms) is out of range", _/binary>>,
  37. value := 3600001
  38. }
  39. ]},
  40. parse_and_check_webhook_bridge(webhook_bridge_health_check_hocon(<<"3600001ms">>))
  41. ),
  42. {"bad parse: negative number",
  43. ?_assertThrow(
  44. {_, [
  45. #{
  46. kind := validation_error,
  47. reason := <<"Health Check Interval (-10ms) is out of range", _/binary>>,
  48. value := "-10ms"
  49. }
  50. ]},
  51. parse_and_check_webhook_bridge(webhook_bridge_health_check_hocon(<<"-10ms">>))
  52. )},
  53. {"bad parse: underscores",
  54. ?_assertThrow(
  55. {_, [
  56. #{
  57. kind := validation_error,
  58. reason :=
  59. <<"Health Check Interval (3_600_000ms) is out of range", _/binary>>,
  60. value := "3_600_000ms"
  61. }
  62. ]},
  63. parse_and_check_webhook_bridge(webhook_bridge_health_check_hocon(<<"3_600_000ms">>))
  64. )},
  65. ?_assertThrow(
  66. #{exception := #{message := "timeout value too large" ++ _}},
  67. parse_and_check_webhook_bridge(
  68. webhook_bridge_health_check_hocon(<<"150000000000000s">>)
  69. )
  70. )
  71. ].
  72. worker_pool_size_test_() ->
  73. BaseConf = parse(webhook_bridge_health_check_hocon(<<"15s">>)),
  74. Check = fun(WorkerPoolSize) ->
  75. Conf = emqx_utils_maps:deep_put(
  76. [
  77. <<"bridges">>,
  78. <<"webhook">>,
  79. <<"simple">>,
  80. <<"resource_opts">>,
  81. <<"worker_pool_size">>
  82. ],
  83. BaseConf,
  84. WorkerPoolSize
  85. ),
  86. #{<<"bridges">> := #{<<"webhook">> := #{<<"simple">> := CheckedConf}}} = check(Conf),
  87. #{<<"resource_opts">> := #{<<"worker_pool_size">> := WPS}} = CheckedConf,
  88. WPS
  89. end,
  90. AssertThrow = fun(WorkerPoolSize) ->
  91. ?assertThrow(
  92. {_, [
  93. #{
  94. kind := validation_error,
  95. reason := #{expected := _},
  96. value := WorkerPoolSize
  97. }
  98. ]},
  99. Check(WorkerPoolSize)
  100. )
  101. end,
  102. [
  103. ?_assertEqual(1, Check(1)),
  104. ?_assertEqual(100, Check(100)),
  105. ?_assertEqual(1024, Check(1024)),
  106. ?_test(AssertThrow(0)),
  107. ?_test(AssertThrow(1025))
  108. ].
  109. %%===========================================================================
  110. %% Helper functions
  111. %%===========================================================================
  112. parse_and_check_webhook_bridge(Hocon) ->
  113. #{<<"bridges">> := #{<<"webhook">> := #{<<"simple">> := Conf}}} = check(parse(Hocon)),
  114. Conf.
  115. parse(Hocon) ->
  116. {ok, Conf} = hocon:binary(Hocon),
  117. Conf.
  118. check(Conf) when is_map(Conf) ->
  119. hocon_tconf:check_plain(emqx_bridge_schema, Conf).
  120. %%===========================================================================
  121. %% Data section
  122. %%===========================================================================
  123. %% erlfmt-ignore
  124. webhook_bridge_health_check_hocon(HealthCheckInterval) ->
  125. io_lib:format(
  126. "
  127. bridges.webhook.simple {
  128. url = \"http://localhost:4000\"
  129. body = \"body\"
  130. resource_opts {
  131. health_check_interval = \"~s\"
  132. }
  133. }
  134. ",
  135. [HealthCheckInterval]).