upgrade_SUITE.erl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_get_paths(Example0) ->
  46. Example = atom_to_list(Example0),
  47. {ok, CWD} = file:get_cwd(),
  48. Dir = CWD ++ "/../../examples/" ++ Example,
  49. Rel = Dir ++ "/_rel/" ++ Example ++ "_example/bin/" ++ Example ++ "_example",
  50. Log = Dir ++ "/_rel/" ++ Example ++ "_example/log/erlang.log.1",
  51. {Dir, Rel, Log}.
  52. do_compile_and_start(Example) ->
  53. Make = do_find_make_cmd(),
  54. {Dir, Rel, _} = do_get_paths(Example),
  55. ct:log("~s~n", [os:cmd(Make ++ " -C " ++ Dir ++ " distclean")]),
  56. %% TERM=dumb disables relx coloring.
  57. ct:log("~s~n", [os:cmd(Make ++ " -C " ++ Dir ++ " TERM=dumb")]),
  58. %% For some reason the release has ExampleStr.boot
  59. %% while the downgrade expects start.boot?
  60. ExampleStr = atom_to_list(Example),
  61. ct:log("~s~n", [os:cmd("cp "
  62. ++ Dir ++ "/_rel/" ++ ExampleStr
  63. ++ "_example/releases/1/" ++ ExampleStr ++ "_example.boot "
  64. ++ Dir ++ "/_rel/" ++ ExampleStr
  65. ++ "_example/releases/1/start.boot")]),
  66. ct:log("~s~n", [os:cmd(Rel ++ " stop")]),
  67. ct:log("~s~n", [os:cmd(Rel ++ " start")]),
  68. timer:sleep(2000),
  69. ct:log("~s~n", [os:cmd(Rel ++ " eval 'application:info()'")]).
  70. do_stop(Example) ->
  71. {Dir, Rel, Log} = do_get_paths(Example),
  72. ct:log("~s~n", [os:cmd("sed -i.bak s/\"2\"/\"1\"/ " ++ Dir ++ "/relx.config")]),
  73. ct:log("~s~n", [os:cmd(Rel ++ " stop")]),
  74. ct:log("~s~n", [element(2, file:read_file(Log))]).
  75. %% When we are on a tag (git describe --exact-match succeeds),
  76. %% we use the tag before that as a starting point. Otherwise
  77. %% we use the most recent tag.
  78. do_use_ranch_previous(Example) ->
  79. TagsOutput = os:cmd("git tag | tr - \\~ | sort -V | tr \\~ -"),
  80. ct:log("~s~n", [TagsOutput]),
  81. Tags = string:lexemes(TagsOutput, "\n"),
  82. DescribeOutput = os:cmd("git describe --exact-match"),
  83. ct:log("~s~n", [DescribeOutput]),
  84. {CommitOrTag, Prev} = case DescribeOutput of
  85. "fatal: no tag exactly matches " ++ _ -> {commit, hd(lists:reverse(Tags))};
  86. _ -> {tag, hd(tl(lists:reverse(Tags)))}
  87. end,
  88. do_use_ranch_commit(Example, Prev),
  89. CommitOrTag.
  90. %% Replace the current Ranch commit with the one given as argument.
  91. do_use_ranch_commit(Example, Commit) ->
  92. {Dir, _, _} = do_get_paths(Example),
  93. ct:log("~s~n", [os:cmd(
  94. "sed -i.bak s/\"dep_ranch_commit = .*\"/\"dep_ranch_commit = "
  95. ++ Commit ++ "\"/ " ++ Dir ++ "/Makefile"
  96. )]).
  97. %% Remove Ranch and rebuild, this time generating a relup.
  98. do_build_relup(Example, CommitOrTag) ->
  99. Make = do_find_make_cmd(),
  100. {Dir, _, _} = do_get_paths(Example),
  101. ct:log("~s~n", [os:cmd("rm -rf " ++ Dir ++ "/deps/ranch")]),
  102. ct:log("~s~n", [os:cmd("sed -i.bak s/\"1\"/\"2\"/ " ++ Dir ++ "/relx.config")]),
  103. %% We need Ranch to be fetched first in order to copy the current appup
  104. %% and optionally update its version when we are not on a tag.
  105. ct:log("~s~n", [os:cmd(Make ++ " -C " ++ Dir ++ " deps")]),
  106. ct:log("~s~n", [os:cmd("cp " ++ Dir ++ "/../../src/ranch.appup "
  107. ++ Dir ++ "/deps/ranch/src/")]),
  108. case CommitOrTag of
  109. tag -> ok;
  110. commit ->
  111. %% Force the rebuild of Ranch.
  112. ct:log("~s~n", [os:cmd(Make ++ " -C " ++ Dir ++ "/deps/ranch clean")]),
  113. %% Update the Ranch version so that the upgrade can be applied.
  114. ProjectVersion = os:cmd("grep \"PROJECT_VERSION = \" " ++ Dir ++ "/deps/ranch/Makefile"),
  115. ct:log(ProjectVersion),
  116. ["PROJECT_VERSION = " ++ Vsn0|_] = string:lexemes(ProjectVersion, "\n"),
  117. [A, B|Tail] = string:lexemes(Vsn0, "."),
  118. Vsn = binary_to_list(iolist_to_binary([A, $., B, ".9", lists:join($., Tail)])),
  119. ct:log("Changing Ranch version from ~s to ~s~n", [Vsn0, Vsn]),
  120. ct:log("~s~n", [os:cmd(
  121. "sed -i.bak s/\"PROJECT_VERSION = .*\"/\"PROJECT_VERSION = " ++ Vsn ++ "\"/ "
  122. ++ Dir ++ "/deps/ranch/Makefile"
  123. )]),
  124. %% The version in the appup must be the same as PROJECT_VERSION.
  125. ct:log("~s~n", [os:cmd(
  126. "sed -i.bak s/\"" ++ Vsn0 ++ "\"/\"" ++ Vsn ++ "\"/ "
  127. ++ Dir ++ "/deps/ranch/src/ranch.appup"
  128. )])
  129. end,
  130. ct:log("~s~n", [os:cmd(Make ++ " -C " ++ Dir ++ " relup")]).
  131. %% Copy the tarball in the correct location and upgrade.
  132. do_upgrade(Example) ->
  133. ExampleStr = atom_to_list(Example),
  134. {Dir, Rel, _} = do_get_paths(Example),
  135. ct:log("~s~n", [os:cmd("cp "
  136. ++ Dir ++ "/_rel/" ++ ExampleStr
  137. ++ "_example/" ++ ExampleStr ++ "_example-2.tar.gz "
  138. ++ Dir ++ "/_rel/" ++ ExampleStr
  139. ++ "_example/releases/2/" ++ ExampleStr ++ "_example.tar.gz")]),
  140. ct:log("~s~n", [os:cmd(Rel ++ " upgrade \"2\"")]),
  141. ct:log("~s~n", [os:cmd(Rel ++ " eval 'application:info()'")]).
  142. do_downgrade(Example) ->
  143. {_, Rel, _} = do_get_paths(Example),
  144. ct:log("~s~n", [os:cmd(Rel ++ " downgrade \"1\"")]),
  145. ct:log("~s~n", [os:cmd(Rel ++ " eval 'application:info()'")]).
  146. %% Tests.
  147. upgrade_ranch_one_conn(_) ->
  148. case os:type() of
  149. {win32, nt} ->
  150. {skip, "This test suite is not currently supported on Windows."};
  151. _ ->
  152. do_upgrade_ranch_one_conn()
  153. end.
  154. do_upgrade_ranch_one_conn() ->
  155. Example = tcp_echo,
  156. Port = 5555,
  157. try
  158. %% Build and start the example release using the previous Ranch version.
  159. CommitOrTag = do_use_ranch_previous(Example),
  160. do_compile_and_start(Example),
  161. %% Establish a connection and check that it works.
  162. {ok, S} = gen_tcp:connect("localhost", Port, [{active, false}, binary]),
  163. ok = gen_tcp:send(S, "Hello!"),
  164. {ok, <<"Hello!">>} = gen_tcp:recv(S, 0, 1000),
  165. %% Update Ranch to master then build a release upgrade.
  166. do_use_ranch_commit(Example, "master"),
  167. do_build_relup(Example, CommitOrTag),
  168. %% Perform the upgrade, then check that our connection is still up.
  169. do_upgrade(Example),
  170. ok = gen_tcp:send(S, "Hello!"),
  171. {ok, <<"Hello!">>} = gen_tcp:recv(S, 0, 1000),
  172. %% Check that new connections are still accepted.
  173. {ok, _} = gen_tcp:connect("localhost", Port, [{active, false}, binary]),
  174. %% Perform the downgrade, then check that our connection is still up.
  175. do_downgrade(Example),
  176. ok = gen_tcp:send(S, "Hello!"),
  177. {ok, <<"Hello!">>} = gen_tcp:recv(S, 0, 1000),
  178. %% Check that new connections are still accepted.
  179. {ok, _} = gen_tcp:connect("localhost", Port, [{active, false}, binary]),
  180. ok
  181. after
  182. do_stop(tcp_echo)
  183. end.
  184. %% @todo upgrade_ranch_max_conn