emqx_utils_api_SUITE.erl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_lib("emqx_utils/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. Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
  25. [{apps, Apps} | Config].
  26. end_per_suite(Config) ->
  27. emqx_cth_suite:stop(proplists:get_value(apps, Config)).
  28. init_per_testcase(_Case, Config) ->
  29. meck:new(?DUMMY, [non_strict]),
  30. meck:expect(?DUMMY, expect_not_called, 1, fun(Node) -> throw({blow_this_up, Node}) end),
  31. meck:expect(?DUMMY, expect_success, 1, {ok, success}),
  32. meck:expect(?DUMMY, expect_error, 1, {error, error}),
  33. Config.
  34. end_per_testcase(_Case, _Config) ->
  35. meck:unload(?DUMMY).
  36. t_with_node(_) ->
  37. test_with(fun emqx_utils_api:with_node/2, [<<"all">>]).
  38. t_with_node_or_cluster(_) ->
  39. test_with(fun emqx_utils_api:with_node_or_cluster/2, []),
  40. meck:reset(?DUMMY),
  41. ?assertEqual(
  42. ?OK(success),
  43. emqx_utils_api:with_node_or_cluster(
  44. <<"all">>,
  45. fun ?DUMMY:expect_success/1
  46. )
  47. ),
  48. ?assertMatch([{_, {?DUMMY, expect_success, [all]}, {ok, success}}], meck:history(?DUMMY)).
  49. %% helpers
  50. test_with(TestFun, ExtraBadNodes) ->
  51. % make sure this is an atom
  52. 'unknownnode@unknownnohost',
  53. BadNodes =
  54. [
  55. <<"undefined">>,
  56. <<"this_should_not_be_an_atom">>,
  57. <<"unknownnode@unknownnohost">>
  58. ] ++ ExtraBadNodes,
  59. [ensure_not_found(TestFun(N, fun ?DUMMY:expect_not_called/1)) || N <- BadNodes],
  60. ensure_not_called(?DUMMY, expect_not_called),
  61. ensure_not_existing_atom(<<"this_should_not_be_an_atom">>),
  62. GoodNode = node(),
  63. ?assertEqual(
  64. ?OK(success),
  65. TestFun(GoodNode, fun ?DUMMY:expect_success/1)
  66. ),
  67. ?assertEqual(
  68. ?BAD_REQUEST(error),
  69. TestFun(GoodNode, fun ?DUMMY:expect_error/1)
  70. ),
  71. ok.
  72. ensure_not_found(Result) ->
  73. ?assertMatch({404, _}, Result).
  74. ensure_not_called(Mod, Fun) ->
  75. ?assert(not meck:called(Mod, Fun, '_')).
  76. ensure_not_existing_atom(Bin) ->
  77. try binary_to_existing_atom(Bin) of
  78. _ -> throw(is_atom)
  79. catch
  80. error:badarg ->
  81. ok
  82. end.