Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

226 linhas
8.6 KiB

  1. %% Test suite for the rebar pkg index caching and decompression
  2. %% mechanisms.
  3. -module(rebar_pkg_SUITE).
  4. -compile(export_all).
  5. -include_lib("common_test/include/ct.hrl").
  6. -include_lib("eunit/include/eunit.hrl").
  7. -define(bad_etag, "abcdef").
  8. -define(good_etag, "22e1d7387c9085a462340088a2a8ba67").
  9. -define(bad_checksum, <<"D576B442A68C7B92BACDE1EFE9C6E54D8D6C74BDB71D8175B9D3C6EC8C7B62A7">>).
  10. -define(good_checksum, <<"1C6CE379D191FBAB41B7905075E0BF87CBBE23C77CECE775C5A0B786B2244C35">>).
  11. all() -> [good_uncached, good_cached, badindexchk, badpkg,
  12. bad_to_good, good_disconnect, bad_disconnect, pkgs_provider].
  13. init_per_suite(Config) ->
  14. application:start(meck),
  15. Config.
  16. end_per_suite(_Config) ->
  17. application:stop(meck).
  18. init_per_testcase(pkgs_provider, Config) ->
  19. Config;
  20. init_per_testcase(good_uncached=Name, Config0) ->
  21. Config = [{good_cache, false},
  22. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  23. | Config0],
  24. mock_config(Name, Config);
  25. init_per_testcase(good_cached=Name, Config0) ->
  26. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  27. Config1 = [{good_cache, true},
  28. {pkg, Pkg}
  29. | Config0],
  30. Config = mock_config(Name, Config1),
  31. copy_to_cache(Pkg, Config),
  32. Config;
  33. init_per_testcase(badindexchk=Name, Config0) ->
  34. Config = [{good_cache, false},
  35. {pkg, {<<"badindexchk">>, <<"1.0.0">>}}
  36. | Config0],
  37. mock_config(Name, Config);
  38. init_per_testcase(badpkg=Name, Config0) ->
  39. Config = [{good_cache, false},
  40. {pkg, {<<"badpkg">>, <<"1.0.0">>}}
  41. | Config0],
  42. mock_config(Name, Config);
  43. init_per_testcase(bad_to_good=Name, Config0) ->
  44. Config1 = [{good_cache, false},
  45. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  46. | Config0],
  47. Config = mock_config(Name, Config1),
  48. Source = filename:join(?config(data_dir, Config), <<"badpkg-1.0.0.tar">>),
  49. Dest = filename:join(?config(cache_dir, Config), <<"goodpkg-1.0.0.tar">>),
  50. ec_file:copy(Source, Dest),
  51. Config;
  52. init_per_testcase(good_disconnect=Name, Config0) ->
  53. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  54. Config1 = [{good_cache, true},
  55. {pkg, Pkg}
  56. | Config0],
  57. Config = mock_config(Name, Config1),
  58. copy_to_cache(Pkg, Config),
  59. meck:unload(httpc),
  60. meck:new(httpc, [passthrough, unsticky]),
  61. meck:expect(httpc, request, fun(_, _, _, _, _) -> {error, econnrefused} end),
  62. Config;
  63. init_per_testcase(bad_disconnect=Name, Config0) ->
  64. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  65. Config1 = [{good_cache, false},
  66. {pkg, Pkg}
  67. | Config0],
  68. Config = mock_config(Name, Config1),
  69. meck:unload(httpc),
  70. meck:new(httpc, [passthrough, unsticky]),
  71. meck:expect(httpc, request, fun(_, _, _, _, _) -> {error, econnrefused} end),
  72. Config.
  73. end_per_testcase(pkgs_provider, Config) ->
  74. Config;
  75. end_per_testcase(_, Config) ->
  76. unmock_config(Config),
  77. Config.
  78. good_uncached(Config) ->
  79. Tmp = ?config(tmp_dir, Config),
  80. {Pkg,Vsn} = ?config(pkg, Config),
  81. State = ?config(state, Config),
  82. ?assertEqual({ok, true},
  83. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  84. Cache = ?config(cache_dir, Config),
  85. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  86. good_cached(Config) ->
  87. Tmp = ?config(tmp_dir, Config),
  88. {Pkg,Vsn} = ?config(pkg, Config),
  89. State = ?config(state, Config),
  90. Cache = ?config(cache_dir, Config),
  91. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  92. ?assert(filelib:is_regular(CachedFile)),
  93. {ok, Content} = file:read_file(CachedFile),
  94. ?assertEqual({ok, true},
  95. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  96. {ok, Content} = file:read_file(CachedFile).
  97. badindexchk(Config) ->
  98. Tmp = ?config(tmp_dir, Config),
  99. {Pkg,Vsn} = ?config(pkg, Config),
  100. State = ?config(state, Config),
  101. ?assertMatch({bad_registry_checksum, _Path},
  102. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  103. %% The cached file is there for forensic purposes
  104. Cache = ?config(cache_dir, Config),
  105. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  106. badpkg(Config) ->
  107. Tmp = ?config(tmp_dir, Config),
  108. {Pkg,Vsn} = ?config(pkg, Config),
  109. State = ?config(state, Config),
  110. ?assertMatch({bad_download, _Path},
  111. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  112. %% The cached file is there for forensic purposes
  113. Cache = ?config(cache_dir, Config),
  114. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  115. bad_to_good(Config) ->
  116. Tmp = ?config(tmp_dir, Config),
  117. {Pkg,Vsn} = ?config(pkg, Config),
  118. State = ?config(state, Config),
  119. Cache = ?config(cache_dir, Config),
  120. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  121. ?assert(filelib:is_regular(CachedFile)),
  122. {ok, Contents} = file:read_file(CachedFile),
  123. ?assertEqual({ok, true},
  124. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  125. %% Cache has refreshed
  126. ?assert({ok, Contents} =/= file:read_file(CachedFile)).
  127. good_disconnect(Config) ->
  128. Tmp = ?config(tmp_dir, Config),
  129. {Pkg,Vsn} = ?config(pkg, Config),
  130. State = ?config(state, Config),
  131. Cache = ?config(cache_dir, Config),
  132. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  133. ?assert(filelib:is_regular(CachedFile)),
  134. {ok, Content} = file:read_file(CachedFile),
  135. ?assertEqual({ok, true},
  136. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  137. {ok, Content} = file:read_file(CachedFile).
  138. bad_disconnect(Config) ->
  139. Tmp = ?config(tmp_dir, Config),
  140. {Pkg,Vsn} = ?config(pkg, Config),
  141. State = ?config(state, Config),
  142. ?assertEqual({fetch_fail, Pkg, Vsn},
  143. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)).
  144. pkgs_provider(Config) ->
  145. Config1 = rebar_test_utils:init_rebar_state(Config),
  146. rebar_test_utils:run_and_check(
  147. Config1, [], ["pkgs"],
  148. {ok, []}
  149. ).
  150. %%%%%%%%%%%%%%%
  151. %%% Helpers %%%
  152. %%%%%%%%%%%%%%%
  153. mock_config(Name, Config) ->
  154. Priv = ?config(priv_dir, Config),
  155. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  156. TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]),
  157. Tid = ets:new(registry_table, [public]),
  158. ets:insert_new(Tid, [
  159. {<<"badindexchk">>,[[<<"1.0.0">>]]},
  160. {<<"goodpkg">>,[[<<"1.0.0">>]]},
  161. {<<"badpkg">>,[[<<"1.0.0">>]]},
  162. {{<<"badindexchk">>,<<"1.0.0">>}, [[], ?bad_checksum, [<<"rebar3">>]]},
  163. {{<<"goodpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  164. {{<<"badpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]}
  165. ]),
  166. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  167. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  168. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  169. %% The state returns us a fake registry
  170. meck:new(rebar_state, [passthrough]),
  171. meck:expect(rebar_state, get,
  172. fun(_State, rebar_packages_cdn, _Default) ->
  173. "http://test.com/"
  174. end),
  175. meck:new(rebar_dir, [passthrough]),
  176. meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end),
  177. meck:new(rebar_packages, [passthrough]),
  178. meck:expect(rebar_packages, registry_dir, fun(_) -> CacheDir end),
  179. meck:expect(rebar_packages, package_dir, fun(_) -> CacheDir end),
  180. rebar_prv_update:hex_to_index(rebar_state:new()),
  181. %% Cache fetches are mocked -- we assume the server and clients are
  182. %% correctly used.
  183. GoodCache = ?config(good_cache, Config),
  184. {Pkg,Vsn} = ?config(pkg, Config),
  185. PkgFile = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  186. {ok, PkgContents} = file:read_file(filename:join(?config(data_dir, Config), PkgFile)),
  187. meck:new(httpc, [passthrough, unsticky]),
  188. meck:expect(httpc, request,
  189. fun(get, {_Url, _Opts}, _, _, _) when GoodCache ->
  190. {ok, {{Vsn, 304, <<"Not Modified">>}, [{"etag", ?good_etag}], <<>>}};
  191. (get, {_Url, _Opts}, _, _, _) ->
  192. {ok, {{Vsn, 200, <<"OK">>}, [{"etag", ?good_etag}], PkgContents}}
  193. end),
  194. [{cache_root, CacheRoot},
  195. {cache_dir, CacheDir},
  196. {tmp_dir, TmpDir},
  197. {mock_table, Tid} | Config].
  198. unmock_config(Config) ->
  199. meck:unload(),
  200. ets:delete(?config(mock_table, Config)).
  201. copy_to_cache({Pkg,Vsn}, Config) ->
  202. Name = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  203. Source = filename:join(?config(data_dir, Config), Name),
  204. Dest = filename:join(?config(cache_dir, Config), Name),
  205. ec_file:copy(Source, Dest).