upgrade_SUITE.erl 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. %% Copyright (c) 2020, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(upgrade_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [doc/1]).
  18. %% ct.
  19. all() ->
  20. ct_helper:all(?MODULE).
  21. init_per_suite(Config) ->
  22. %% Remove environment variables inherited from Erlang.mk.
  23. os:unsetenv("ERLANG_MK_TMP"),
  24. os:unsetenv("APPS_DIR"),
  25. os:unsetenv("DEPS_DIR"),
  26. os:unsetenv("ERL_LIBS"),
  27. os:unsetenv("CI_ERLANG_MK"),
  28. %% Ensure we are using the C locale for all os:cmd calls.
  29. os:putenv("LC_ALL", "C"),
  30. Config.
  31. end_per_suite(_Config) ->
  32. ok.
  33. %% Find GNU Make.
  34. do_find_make_cmd() ->
  35. case os:getenv("MAKE") of
  36. false ->
  37. case os:find_executable("gmake") of
  38. false -> "make";
  39. Cmd -> Cmd
  40. end;
  41. Cmd ->
  42. Cmd
  43. end.
  44. %% Manipulate the release.
  45. do_copy(Example0) ->
  46. Example = atom_to_list(Example0),
  47. {ok, CWD} = file:get_cwd(),
  48. _ = do_exec_log("cp -R " ++ CWD ++ "/../../examples/" ++ Example ++ " " ++ CWD),
  49. Dir = CWD ++ "/" ++ Example,
  50. _ = do_exec_log("sed -i.bak s/\"include \\.\\.\\/\\.\\.\\/erlang.mk\"/\"include ..\\/..\\/..\\/erlang.mk\"/ " ++ Dir ++ "/Makefile"),
  51. ok.
  52. do_remove(Example0) ->
  53. Example = atom_to_list(Example0),
  54. {ok, CWD} = file:get_cwd(),
  55. _ = do_exec_log("rm -rf " ++ CWD ++ "/" ++ Example),
  56. ok.
  57. do_get_paths(Example0) ->
  58. Example = atom_to_list(Example0),
  59. {ok, CWD} = file:get_cwd(),
  60. Dir = CWD ++ "/" ++ Example,
  61. Rel = Dir ++ "/_rel/" ++ Example ++ "_example/bin/" ++ Example ++ "_example",
  62. Log = Dir ++ "/_rel/" ++ Example ++ "_example/log/erlang.log.1",
  63. {Dir, Rel, Log}.
  64. do_compile_and_start(Example) ->
  65. Make = do_find_make_cmd(),
  66. {Dir, Rel, _} = do_get_paths(Example),
  67. _ = do_exec_log(Make ++ " -C " ++ Dir ++ " distclean"),
  68. %% TERM=dumb disables relx coloring.
  69. _ = do_exec_log(Make ++ " -C " ++ Dir ++ " TERM=dumb"),
  70. %% For some reason the release has ExampleStr.boot
  71. %% while the downgrade expects start.boot?
  72. ExampleStr = atom_to_list(Example),
  73. _ = do_exec_log("cp "
  74. ++ Dir ++ "/_rel/" ++ ExampleStr
  75. ++ "_example/releases/1/" ++ ExampleStr ++ "_example.boot "
  76. ++ Dir ++ "/_rel/" ++ ExampleStr
  77. ++ "_example/releases/1/start.boot"),
  78. _ = do_exec_log(Rel ++ " stop"),
  79. _ = do_exec_log(Rel ++ " start"),
  80. timer:sleep(2000),
  81. _ = do_exec_log(Rel ++ " eval 'application:info()'"),
  82. ok.
  83. do_stop(Example) ->
  84. {Dir, Rel, Log} = do_get_paths(Example),
  85. _ = do_exec_log("sed -i.bak s/\"2\"/\"1\"/ " ++ Dir ++ "/relx.config"),
  86. _ = do_exec_log(Rel ++ " stop"),
  87. ct:log("~s~n", [element(2, file:read_file(Log))]).
  88. %% When we are on a tag (git describe --exact-match succeeds),
  89. %% we use the tag before that as a starting point. Otherwise
  90. %% we use the most recent tag.
  91. do_use_ranch_previous(Example) ->
  92. TagsOutput = do_exec_log("git tag | tr - \\~ | sort -V | tr \\~ -"),
  93. Tags = string:lexemes(TagsOutput, "\n"),
  94. DescribeOutput = do_exec_log("git describe --exact-match"),
  95. {CommitOrTag, Prev} = case DescribeOutput of
  96. "fatal: no tag exactly matches " ++ _ -> {commit, hd(lists:reverse(Tags))};
  97. _ -> {tag, hd(tl(lists:reverse(Tags)))}
  98. end,
  99. do_use_ranch_commit(Example, Prev),
  100. CommitOrTag.
  101. %% Replace the current Ranch commit with the one given as argument.
  102. do_use_ranch_commit(Example, Commit) ->
  103. {Dir, _, _} = do_get_paths(Example),
  104. _ = do_exec_log(
  105. "sed -i.bak s/\"dep_ranch_commit = .*\"/\"dep_ranch_commit = "
  106. ++ Commit ++ "\"/ " ++ Dir ++ "/Makefile"
  107. ),
  108. ok.
  109. %% Remove Ranch and rebuild, this time generating a relup.
  110. do_build_relup(Example, CommitOrTag) ->
  111. Make = do_find_make_cmd(),
  112. {Dir, _, _} = do_get_paths(Example),
  113. _ = do_exec_log("rm -rf " ++ Dir ++ "/deps/ranch/*"),
  114. _ = do_exec_log("sed -i.bak s/\"1\"/\"2\"/ " ++ Dir ++ "/relx.config"),
  115. %% We need Ranch to be fetched first in order to copy the current appup
  116. %% and optionally update its version when we are not on a tag.
  117. _ = do_exec_log("cp -R "
  118. ++ Dir ++ "/../../../Makefile "
  119. ++ Dir ++ "/../../../erlang.mk "
  120. ++ Dir ++ "/../../../src "
  121. ++ Dir ++ "/deps/ranch/"),
  122. _ = do_exec_log(Make ++ " -C " ++ Dir ++ " deps"),
  123. _ = case CommitOrTag of
  124. tag -> ok;
  125. commit ->
  126. %% Force the rebuild of Ranch.
  127. _ = do_exec_log(Make ++ " -C " ++ Dir ++ "/deps/ranch clean"),
  128. %% Update the Ranch version so that the upgrade can be applied.
  129. ProjectVersion = do_exec_log("grep \"PROJECT_VERSION = \" " ++ Dir ++ "/deps/ranch/Makefile"),
  130. ["PROJECT_VERSION = " ++ Vsn0|_] = string:lexemes(ProjectVersion, "\n"),
  131. [A, B|Tail] = string:lexemes(Vsn0, "."),
  132. Vsn = binary_to_list(iolist_to_binary([A, $., B, ".9", lists:join($., Tail)])),
  133. ct:log("Changing Ranch version from ~s to ~s~n", [Vsn0, Vsn]),
  134. _ = do_exec_log(
  135. "sed -i.bak s/\"PROJECT_VERSION = .*\"/\"PROJECT_VERSION = " ++ Vsn ++ "\"/ "
  136. ++ Dir ++ "/deps/ranch/Makefile"
  137. ),
  138. %% The version in the appup must be the same as PROJECT_VERSION.
  139. _ = do_exec_log(
  140. "sed -i.bak s/\"" ++ Vsn0 ++ "\"/\"" ++ Vsn ++ "\"/ "
  141. ++ Dir ++ "/deps/ranch/src/ranch.appup"
  142. )
  143. end,
  144. _ = do_exec_log(Make ++ " -C " ++ Dir ++ " relup"),
  145. ok.
  146. %% Copy the tarball in the correct location and upgrade.
  147. do_upgrade(Example) ->
  148. ExampleStr = atom_to_list(Example),
  149. {Dir, Rel, _} = do_get_paths(Example),
  150. _ = do_exec_log("cp "
  151. ++ Dir ++ "/_rel/" ++ ExampleStr
  152. ++ "_example/" ++ ExampleStr ++ "_example-2.tar.gz "
  153. ++ Dir ++ "/_rel/" ++ ExampleStr
  154. ++ "_example/releases/2/" ++ ExampleStr ++ "_example.tar.gz"),
  155. _ = do_exec_log(Rel ++ " upgrade \"2\""),
  156. _ = do_exec_log(Rel ++ " eval 'application:info()'"),
  157. ok.
  158. do_downgrade(Example) ->
  159. {_, Rel, _} = do_get_paths(Example),
  160. _ = do_exec_log(Rel ++ " downgrade \"1\""),
  161. _ = do_exec_log(Rel ++ " eval 'application:info()'"),
  162. ok.
  163. %% Tests.
  164. upgrade_ranch_one_conn(_) ->
  165. case os:type() of
  166. {win32, nt} ->
  167. {skip, "This test suite is not currently supported on Windows."};
  168. _ ->
  169. do_upgrade_ranch_one_conn()
  170. end.
  171. do_upgrade_ranch_one_conn() ->
  172. Example = tcp_echo,
  173. ExampleStr = atom_to_list(Example),
  174. Port = 5555,
  175. {_, Rel, _} = do_get_paths(Example),
  176. try
  177. %% Copy the example.
  178. do_copy(Example),
  179. %% Build and start the example release using the previous Ranch version.
  180. CommitOrTag = do_use_ranch_previous(Example),
  181. do_compile_and_start(Example),
  182. %% Ensure that the metrics key is not present in the ranch:info output.
  183. "false\n" = do_exec_log(Rel ++ " eval "
  184. "'maps:is_key(metrics, ranch:info(" ++ ExampleStr ++ "))'"),
  185. %% Establish a connection and check that it works.
  186. {ok, S} = gen_tcp:connect("localhost", Port, [{active, false}, binary]),
  187. ok = gen_tcp:send(S, "Hello!"),
  188. {ok, <<"Hello!">>} = gen_tcp:recv(S, 0, 1000),
  189. %% Update Ranch to master then build a release upgrade.
  190. do_build_relup(Example, CommitOrTag),
  191. %% Perform the upgrade, then check that our connection is still up.
  192. do_upgrade(Example),
  193. %% Ensure that the mextrics key is present in the ranch:info output.
  194. "true\n" = do_exec_log(Rel ++ " eval "
  195. "'maps:is_key(metrics, ranch:info(" ++ ExampleStr ++ "))'"),
  196. ok = gen_tcp:send(S, "Hello!"),
  197. {ok, <<"Hello!">>} = gen_tcp:recv(S, 0, 1000),
  198. %% Ensure that no accepts have been counted yet.
  199. "0\n" = do_exec_log(Rel ++ " eval "
  200. "'lists:sum([N || {{conns_sup, _, accept}, N} <- "
  201. "maps:to_list(maps:get(metrics, ranch:info(" ++ ExampleStr ++ ")))])'"),
  202. %% Check that new connections are still accepted.
  203. {ok, S2} = gen_tcp:connect("localhost", Port, [{active, false}, binary]),
  204. %% Ensure that the accept has been counted.
  205. "1\n" = do_exec_log(Rel ++ " eval "
  206. "'lists:sum([N || {{conns_sup, _, accept}, N} <- "
  207. "maps:to_list(maps:get(metrics, ranch:info(" ++ ExampleStr ++ ")))])'"),
  208. %% Ensure that no terminates have been counted yet.
  209. "0\n" = do_exec_log(Rel ++ " eval "
  210. "'lists:sum([N || {{conns_sup, _, terminate}, N} <- "
  211. "maps:to_list(maps:get(metrics, ranch:info(" ++ ExampleStr ++ ")))])'"),
  212. %% Close the socket, ensure that the termination has been counted.
  213. ok = gen_tcp:close(S2),
  214. "1\n" = do_exec_log(Rel ++ " eval "
  215. "'lists:sum([N || {{conns_sup, _, terminate}, N} <- "
  216. "maps:to_list(maps:get(metrics, ranch:info(" ++ ExampleStr ++ ")))])'"),
  217. %% Perform the downgrade, then check that our connection is still up.
  218. do_downgrade(Example),
  219. %% Ensure that the mextrics key is not present any more.
  220. "false\n" = do_exec_log(Rel ++ " eval "
  221. "'maps:is_key(metrics, ranch:info(" ++ ExampleStr ++ "))'"),
  222. ok = gen_tcp:send(S, "Hello!"),
  223. {ok, <<"Hello!">>} = gen_tcp:recv(S, 0, 1000),
  224. %% Check that new connections are still accepted.
  225. {ok, _} = gen_tcp:connect("localhost", Port, [{active, false}, binary]),
  226. ok
  227. after
  228. do_stop(tcp_echo),
  229. do_remove(Example)
  230. end.
  231. %% @todo upgrade_ranch_max_conn
  232. do_exec_log(Cmd) ->
  233. ct:log("Command: ~s~n", [Cmd]),
  234. Out=os:cmd(Cmd),
  235. ct:log("Output:~n~n~s~n", [Out]),
  236. Out.