emqx_telemetry_api_SUITE.erl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2022 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. %% http://www.apache.org/licenses/LICENSE-2.0
  8. %%
  9. %% Unless required by applicable law or agreed to in writing, software
  10. %% distributed under the License is distributed on an "AS IS" BASIS,
  11. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. %% See the License for the specific language governing permissions and
  13. %% limitations under the License.
  14. %%--------------------------------------------------------------------
  15. -module(emqx_telemetry_api_SUITE).
  16. -compile(nowarn_export_all).
  17. -compile(export_all).
  18. -import(emqx_dashboard_api_test_helpers, [request/2, request/3, uri/1]).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -include_lib("common_test/include/ct.hrl").
  21. -define(BASE_CONF, #{}).
  22. all() ->
  23. emqx_common_test_helpers:all(?MODULE).
  24. init_per_suite(Config) ->
  25. ok = emqx_common_test_helpers:load_config(emqx_modules_schema, ?BASE_CONF, #{
  26. raw_with_default => true
  27. }),
  28. ok = emqx_common_test_helpers:start_apps(
  29. [emqx_conf, emqx_authn, emqx_authz, emqx_modules, emqx_dashboard],
  30. fun set_special_configs/1
  31. ),
  32. Config.
  33. end_per_suite(_Config) ->
  34. {ok, _} = emqx:update_config(
  35. [authorization],
  36. #{
  37. <<"no_match">> => <<"allow">>,
  38. <<"cache">> => #{<<"enable">> => <<"true">>},
  39. <<"sources">> => []
  40. }
  41. ),
  42. emqx_common_test_helpers:stop_apps([
  43. emqx_dashboard, emqx_conf, emqx_authn, emqx_authz, emqx_modules
  44. ]),
  45. ok.
  46. init_per_testcase(t_status_non_official, Config) ->
  47. meck:new(emqx_telemetry, [non_strict, passthrough]),
  48. meck:expect(emqx_telemetry, official_version, 1, false),
  49. Config;
  50. init_per_testcase(t_status, Config) ->
  51. meck:new(emqx_telemetry, [non_strict, passthrough]),
  52. meck:expect(emqx_telemetry, enable, fun() -> ok end),
  53. Config;
  54. init_per_testcase(_TestCase, Config) ->
  55. Config.
  56. end_per_testcase(t_status_non_official, _Config) ->
  57. meck:unload(emqx_telemetry);
  58. end_per_testcase(t_status, _Config) ->
  59. meck:unload(emqx_telemetry);
  60. end_per_testcase(_TestCase, _Config) ->
  61. ok.
  62. set_special_configs(emqx_dashboard) ->
  63. emqx_dashboard_api_test_helpers:set_default_config();
  64. set_special_configs(emqx_authz) ->
  65. {ok, _} = emqx:update_config([authorization, cache, enable], false),
  66. {ok, _} = emqx:update_config([authorization, no_match], deny),
  67. {ok, _} = emqx:update_config([authorization, sources], []),
  68. ok;
  69. set_special_configs(_App) ->
  70. ok.
  71. %%------------------------------------------------------------------------------
  72. %% Tests
  73. %%------------------------------------------------------------------------------
  74. t_status(_) ->
  75. ?assertMatch(
  76. {ok, 200, _},
  77. request(
  78. put,
  79. uri(["telemetry", "status"]),
  80. #{<<"enable">> => false}
  81. )
  82. ),
  83. {ok, 200, Result0} =
  84. request(get, uri(["telemetry", "status"])),
  85. ?assertEqual(
  86. #{<<"enable">> => false},
  87. jsx:decode(Result0)
  88. ),
  89. ?assertMatch(
  90. {ok, 400, _},
  91. request(
  92. put,
  93. uri(["telemetry", "status"]),
  94. #{<<"enable">> => false}
  95. )
  96. ),
  97. ?assertMatch(
  98. {ok, 200, _},
  99. request(
  100. put,
  101. uri(["telemetry", "status"]),
  102. #{<<"enable">> => true}
  103. )
  104. ),
  105. {ok, 200, Result1} =
  106. request(get, uri(["telemetry", "status"])),
  107. ?assertEqual(
  108. #{<<"enable">> => true},
  109. jsx:decode(Result1)
  110. ),
  111. ?assertMatch(
  112. {ok, 400, _},
  113. request(
  114. put,
  115. uri(["telemetry", "status"]),
  116. #{<<"enable">> => true}
  117. )
  118. ).
  119. t_status_non_official(_) ->
  120. ?assertMatch(
  121. {ok, 200, _},
  122. request(
  123. put,
  124. uri(["telemetry", "status"]),
  125. #{<<"enable">> => false}
  126. )
  127. ).
  128. t_data(_) ->
  129. {ok, 200, Result} =
  130. request(get, uri(["telemetry", "data"])),
  131. ?assertMatch(
  132. #{
  133. <<"active_plugins">> := _,
  134. <<"advanced_mqtt_features">> := _,
  135. <<"build_info">> := _,
  136. <<"emqx_version">> := _,
  137. <<"license">> := _,
  138. <<"messages_received">> := _,
  139. <<"mqtt_runtime_insights">> := _,
  140. <<"nodes_uuid">> := _,
  141. <<"os_name">> := _,
  142. <<"otp_version">> := _,
  143. <<"uuid">> := _,
  144. <<"vm_specs">> := _
  145. },
  146. jsx:decode(Result)
  147. ).