emqx_ft_test_helpers.erl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_ft_test_helpers).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("common_test/include/ct.hrl").
  20. -define(S3_HOST, <<"minio">>).
  21. -define(S3_PORT, 9000).
  22. config(Storage) ->
  23. config(Storage, #{}).
  24. config(Storage, FTOptions0) ->
  25. FTOptions1 = maps:merge(
  26. #{<<"enable">> => true, <<"storage">> => Storage},
  27. FTOptions0
  28. ),
  29. #{<<"file_transfer">> => FTOptions1}.
  30. local_storage(Config) ->
  31. local_storage(Config, #{exporter => local}).
  32. local_storage(Config, Opts) ->
  33. #{
  34. <<"local">> => #{
  35. <<"enable">> => true,
  36. <<"segments">> => #{<<"root">> => root(Config, node(), [segments])},
  37. <<"exporter">> => exporter(Config, Opts)
  38. }
  39. }.
  40. exporter(Config, #{exporter := local}) ->
  41. #{
  42. <<"local">> => #{
  43. <<"enable">> => true,
  44. <<"root">> => root(Config, node(), [exports])
  45. }
  46. };
  47. exporter(_Config, #{exporter := s3, bucket_name := BucketName}) ->
  48. BaseConfig = emqx_s3_test_helpers:base_raw_config(tcp),
  49. #{
  50. <<"s3">> => BaseConfig#{
  51. <<"enable">> => true,
  52. <<"bucket">> => list_to_binary(BucketName),
  53. <<"host">> => ?S3_HOST,
  54. <<"port">> => ?S3_PORT
  55. }
  56. }.
  57. load_config(Config) ->
  58. emqx_common_test_helpers:load_config(emqx_ft_schema, #{<<"file_transfer">> => Config}).
  59. tcp_port(Node) ->
  60. {_, Port} = rpc:call(Node, emqx_config, get, [[listeners, tcp, default, bind]]),
  61. Port.
  62. root(Config, Node, Tail) ->
  63. iolist_to_binary(filename:join([ft_root(Config), Node | Tail])).
  64. ft_root(Config) ->
  65. filename:join([?config(priv_dir, Config), "file_transfer"]).
  66. cleanup_ft_root(Config) ->
  67. file:del_dir_r(emqx_ft_test_helpers:ft_root(Config)).
  68. start_client(ClientId) ->
  69. start_client(ClientId, node()).
  70. start_client(ClientId, Node) ->
  71. Port = tcp_port(Node),
  72. {ok, Client} = emqtt:start_link([{proto_ver, v5}, {clientid, ClientId}, {port, Port}]),
  73. {ok, _} = emqtt:connect(Client),
  74. Client.
  75. upload_file(ClientId, FileId, Name, Data) ->
  76. upload_file(sync, ClientId, FileId, Name, Data).
  77. upload_file(Mode, ClientId, FileId, Name, Data) ->
  78. upload_file(Mode, ClientId, FileId, Name, Data, node()).
  79. upload_file(Mode, ClientId, FileId, Name, Data, Node) ->
  80. C1 = start_client(ClientId, Node),
  81. ReqTopicPrefix = request_topic_prefix(Mode, FileId),
  82. Size = byte_size(Data),
  83. Meta = #{
  84. name => Name,
  85. expire_at => erlang:system_time(_Unit = second) + 3600,
  86. size => Size
  87. },
  88. MetaPayload = emqx_utils_json:encode(emqx_ft:encode_filemeta(Meta)),
  89. MetaTopic = <<ReqTopicPrefix/binary, "/init">>,
  90. {ok, #{reason_code_name := success}} = emqtt:publish(C1, MetaTopic, MetaPayload, 1),
  91. {ok, #{reason_code_name := success}} = emqtt:publish(
  92. C1, <<ReqTopicPrefix/binary, "/0">>, Data, 1
  93. ),
  94. FinTopic = <<ReqTopicPrefix/binary, "/fin/", (integer_to_binary(Size))/binary>>,
  95. FinResult = fin_result(Mode, ClientId, C1, FinTopic),
  96. ok = emqtt:stop(C1),
  97. FinResult.
  98. fin_result(Mode, ClientId, C, FinTopic) ->
  99. {ok, _, _} = emqtt:subscribe(C, response_topic(ClientId), 1),
  100. case emqtt:publish(C, FinTopic, <<>>, 1) of
  101. {ok, #{reason_code_name := success}} ->
  102. maybe_wait_for_assemble(Mode, ClientId, FinTopic);
  103. {ok, #{reason_code_name := Error}} ->
  104. {error, Error}
  105. end.
  106. maybe_wait_for_assemble(sync, _ClientId, _FinTopic) ->
  107. ok;
  108. maybe_wait_for_assemble(async, ClientId, FinTopic) ->
  109. ResponseTopic = response_topic(ClientId),
  110. receive
  111. {publish, #{payload := Payload, topic := ResponseTopic}} ->
  112. case emqx_utils_json:decode(Payload) of
  113. #{<<"topic">> := FinTopic, <<"reason_code">> := 0} ->
  114. ok;
  115. #{<<"topic">> := FinTopic, <<"reason_code">> := Code} ->
  116. {error, emqx_reason_codes:name(Code)};
  117. _ ->
  118. maybe_wait_for_assemble(async, ClientId, FinTopic)
  119. end
  120. end.
  121. response_topic(ClientId) ->
  122. <<"$file-response/", (to_bin(ClientId))/binary>>.
  123. request_topic_prefix(sync, FileId) ->
  124. <<"$file/", (to_bin(FileId))/binary>>;
  125. request_topic_prefix(async, FileId) ->
  126. <<"$file-async/", (to_bin(FileId))/binary>>.
  127. to_bin(Val) ->
  128. iolist_to_binary(Val).
  129. aws_config() ->
  130. emqx_s3_test_helpers:aws_config(tcp, binary_to_list(?S3_HOST), ?S3_PORT).
  131. pem_privkey() ->
  132. <<
  133. "\n"
  134. "-----BEGIN EC PRIVATE KEY-----\n"
  135. "MHQCAQEEICKTbbathzvD8zvgjL7qRHhW4alS0+j0Loo7WeYX9AxaoAcGBSuBBAAK\n"
  136. "oUQDQgAEJBdF7MIdam5T4YF3JkEyaPKdG64TVWCHwr/plC0QzNVJ67efXwxlVGTo\n"
  137. "ju0VBj6tOX1y6C0U+85VOM0UU5xqvw==\n"
  138. "-----END EC PRIVATE KEY-----\n"
  139. >>.
  140. unique_binary_string() ->
  141. emqx_guid:to_hexstr(emqx_guid:gen()).