emqx_utils_api_SUITE.erl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-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_utils_api_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include("emqx_utils_api.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. -define(DUMMY, dummy_module).
  22. all() -> emqx_common_test_helpers:all(?MODULE).
  23. init_per_suite(Config) ->
  24. emqx_common_test_helpers:boot_modules(all),
  25. emqx_common_test_helpers:start_apps([]),
  26. Config.
  27. end_per_suite(_Config) ->
  28. emqx_common_test_helpers:stop_apps([]).
  29. init_per_testcase(_Case, Config) ->
  30. meck:new(?DUMMY, [non_strict]),
  31. meck:expect(?DUMMY, expect_not_called, 1, fun(Node) -> throw({blow_this_up, Node}) end),
  32. meck:expect(?DUMMY, expect_success, 1, {ok, success}),
  33. meck:expect(?DUMMY, expect_error, 1, {error, error}),
  34. Config.
  35. end_per_testcase(_Case, _Config) ->
  36. meck:unload(?DUMMY).
  37. t_with_node(_) ->
  38. test_with(fun emqx_utils_api:with_node/2, [<<"all">>]).
  39. t_with_node_or_cluster(_) ->
  40. test_with(fun emqx_utils_api:with_node_or_cluster/2, []),
  41. meck:reset(?DUMMY),
  42. ?assertEqual(
  43. ?OK(success),
  44. emqx_utils_api:with_node_or_cluster(
  45. <<"all">>,
  46. fun ?DUMMY:expect_success/1
  47. )
  48. ),
  49. ?assertMatch([{_, {?DUMMY, expect_success, [all]}, {ok, success}}], meck:history(?DUMMY)).
  50. %% helpers
  51. test_with(TestFun, ExtraBadNodes) ->
  52. % make sure this is an atom
  53. 'unknownnode@unknownnohost',
  54. BadNodes =
  55. [
  56. <<"undefined">>,
  57. <<"this_should_not_be_an_atom">>,
  58. <<"unknownnode@unknownnohost">>
  59. ] ++ ExtraBadNodes,
  60. [ensure_not_found(TestFun(N, fun ?DUMMY:expect_not_called/1)) || N <- BadNodes],
  61. ensure_not_called(?DUMMY, expect_not_called),
  62. ensure_not_existing_atom(<<"this_should_not_be_an_atom">>),
  63. GoodNode = node(),
  64. ?assertEqual(
  65. ?OK(success),
  66. TestFun(GoodNode, fun ?DUMMY:expect_success/1)
  67. ),
  68. ?assertEqual(
  69. ?BAD_REQUEST(error),
  70. TestFun(GoodNode, fun ?DUMMY:expect_error/1)
  71. ),
  72. ok.
  73. ensure_not_found(Result) ->
  74. ?assertMatch({404, _}, Result).
  75. ensure_not_called(Mod, Fun) ->
  76. ?assert(not meck:called(Mod, Fun, '_')).
  77. ensure_not_existing_atom(Bin) ->
  78. try binary_to_existing_atom(Bin) of
  79. _ -> throw(is_atom)
  80. catch
  81. error:badarg ->
  82. ok
  83. end.