You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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