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.

341 line
15 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. -include("rebar.hrl").
  8. -define(bad_etag, <<"abcdef">>).
  9. -define(good_etag, <<"22e1d7387c9085a462340088a2a8ba67">>).
  10. -define(badpkg_checksum, <<"A14E3718B33F8124E98004433193509EC6660F6CA03302657CAB8785751D77A0">>).
  11. -define(badindex_checksum, <<"7B2CBED315C89F3126B5BF553DD7FF0FB5FE94B064888DD1B095CE8BF4B6A16A">>).
  12. -define(bad_checksum, <<"D576B442A68C7B92BACDE1EFE9C6E54D8D6C74BDB71D8175B9D3C6EC8C7B62A7">>).
  13. -define(good_checksum, <<"12726BDE1F65583A0817A7E8AADCA73F03FD8CB06F01E6CD29117C4A0DA0AFCF">>).
  14. -define(BADPKG_ETAG, <<"BADETAG">>).
  15. all() -> [good_uncached, good_cached, badpkg, badhash_nocache,
  16. badindexchk, badhash_cache, bad_to_good, good_disconnect,
  17. bad_disconnect, pkgs_provider, find_highest_matching].
  18. init_per_suite(Config) ->
  19. application:start(meck),
  20. Config.
  21. end_per_suite(_Config) ->
  22. application:stop(meck).
  23. init_per_testcase(pkgs_provider=Name, Config) ->
  24. %% Need to mock out a registry for this test now because it will try to update it automatically
  25. Priv = ?config(priv_dir, Config),
  26. Tid = ets:new(registry_table, [public]),
  27. ets:insert_new(Tid, []),
  28. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  29. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  30. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  31. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  32. Config;
  33. init_per_testcase(good_uncached=Name, Config0) ->
  34. Config = [{good_cache, false},
  35. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  36. | Config0],
  37. mock_config(Name, Config);
  38. init_per_testcase(good_cached=Name, Config0) ->
  39. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  40. Config1 = [{good_cache, true},
  41. {pkg, Pkg}
  42. | Config0],
  43. Config = mock_config(Name, Config1),
  44. copy_to_cache(Pkg, Config),
  45. Config;
  46. init_per_testcase(badindexchk=Name, Config0) ->
  47. Config = [{good_cache, false},
  48. {pkg, {<<"badindexchk">>, <<"1.0.0">>}}
  49. | Config0],
  50. mock_config(Name, Config);
  51. init_per_testcase(badpkg=Name, Config0) ->
  52. Config = [{good_cache, false},
  53. {pkg, {<<"badpkg">>, <<"1.0.0">>}}
  54. | Config0],
  55. mock_config(Name, Config);
  56. init_per_testcase(badhash_nocache=Name, Config0) ->
  57. Config = [{good_cache, false},
  58. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  59. | Config0],
  60. mock_config(Name, Config);
  61. init_per_testcase(badhash_cache=Name, Config0) ->
  62. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  63. Config1 = [{good_cache, true},
  64. {pkg, Pkg}
  65. | Config0],
  66. Config = mock_config(Name, Config1),
  67. copy_to_cache(Pkg, Config),
  68. Config;
  69. init_per_testcase(bad_to_good=Name, Config0) ->
  70. Config1 = [{good_cache, false},
  71. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  72. | Config0],
  73. Config = mock_config(Name, Config1),
  74. Source = filename:join(?config(data_dir, Config), <<"badpkg-1.0.0.tar">>),
  75. Dest = filename:join(?config(cache_dir, Config), <<"goodpkg-1.0.0.tar">>),
  76. ec_file:copy(Source, Dest),
  77. Config;
  78. init_per_testcase(good_disconnect=Name, Config0) ->
  79. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  80. Config1 = [{good_cache, false},
  81. {pkg, Pkg}
  82. | Config0],
  83. Config = mock_config(Name, Config1),
  84. copy_to_cache(Pkg, Config),
  85. %% meck:unload(httpc),
  86. meck:new(httpc, [passthrough, unsticky]),
  87. meck:expect(httpc, request, fun(_, _, _, _) -> {error, econnrefused} end),
  88. Config;
  89. init_per_testcase(bad_disconnect=Name, Config0) ->
  90. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  91. Config1 = [{good_cache, false},
  92. {pkg, Pkg}
  93. | Config0],
  94. Config = mock_config(Name, Config1),
  95. meck:expect(hex_repo, get_tarball, fun(_, _, _) ->
  96. {error, econnrefused}
  97. end),
  98. Config;
  99. init_per_testcase(Name, Config0) ->
  100. Config = [{good_cache, false},
  101. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  102. | Config0],
  103. mock_config(Name, Config).
  104. end_per_testcase(_, Config) ->
  105. unmock_config(Config),
  106. Config.
  107. good_uncached(Config) ->
  108. Tmp = ?config(tmp_dir, Config),
  109. {Pkg,Vsn} = ?config(pkg, Config),
  110. State = ?config(state, Config),
  111. ?assertEqual(ok,
  112. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum, #{}}, State, #{}, true)),
  113. Cache = ?config(cache_dir, Config),
  114. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  115. good_cached(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, Content} = file:read_file(CachedFile),
  123. ?assertEqual(ok,
  124. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum, #{}}, State, #{}, true)),
  125. {ok, Content} = file:read_file(CachedFile).
  126. badindexchk(Config) ->
  127. Tmp = ?config(tmp_dir, Config),
  128. {Pkg,Vsn} = ?config(pkg, Config),
  129. State = ?config(state, Config),
  130. ?assertMatch({error, {rebar_pkg_resource, {bad_registry_checksum, _, _, _, _}}},
  131. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?bad_checksum, #{}}, State, #{}, true)),
  132. %% The cached file is there for forensic purposes
  133. Cache = ?config(cache_dir, Config),
  134. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  135. badpkg(Config) ->
  136. Tmp = ?config(tmp_dir, Config),
  137. {Pkg,Vsn} = ?config(pkg, Config),
  138. State = ?config(state, Config),
  139. Cache = ?config(cache_dir, Config),
  140. CachePath = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  141. ETagPath = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".etag">>),
  142. rebar_pkg_resource:store_etag_in_cache(ETagPath, ?BADPKG_ETAG),
  143. ?assertMatch({error, {hex_tarball, {tarball, {checksum_mismatch, _, _}}}},
  144. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?badpkg_checksum, #{}}, State, #{}, false)),
  145. %% The cached/etag files are there for forensic purposes
  146. ?assert(filelib:is_regular(ETagPath)),
  147. ?assert(filelib:is_regular(CachePath)).
  148. badhash_nocache(Config) ->
  149. Tmp = ?config(tmp_dir, Config),
  150. {Pkg,Vsn} = ?config(pkg, Config),
  151. State = ?config(state, Config),
  152. ?assertMatch({error, {rebar_pkg_resource, {bad_registry_checksum, _, _, _, _}}},
  153. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?bad_checksum, #{}}, State, #{}, true)),
  154. %% The cached file is there for forensic purposes
  155. Cache = ?config(cache_dir, Config),
  156. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  157. badhash_cache(Config) ->
  158. Tmp = ?config(tmp_dir, Config),
  159. {Pkg,Vsn} = ?config(pkg, Config),
  160. Cache = ?config(cache_dir, Config),
  161. State = ?config(state, Config),
  162. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  163. ?assert(filelib:is_regular(CachedFile)),
  164. {ok, Content} = file:read_file(CachedFile),
  165. ?assertMatch({error, {rebar_pkg_resource, {bad_registry_checksum, _, _, _, _}}},
  166. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?bad_checksum, #{}}, State, #{}, true)),
  167. %% The cached file is there still, unchanged.
  168. ?assert(filelib:is_regular(CachedFile)),
  169. ?assertEqual({ok, Content}, file:read_file(CachedFile)).
  170. bad_to_good(Config) ->
  171. Tmp = ?config(tmp_dir, Config),
  172. {Pkg,Vsn} = ?config(pkg, Config),
  173. State = ?config(state, Config),
  174. Cache = ?config(cache_dir, Config),
  175. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  176. ?assert(filelib:is_regular(CachedFile)),
  177. {ok, Contents} = file:read_file(CachedFile),
  178. ?assertEqual(ok,
  179. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum, #{}}, State, #{}, true)),
  180. %% Cache has refreshed
  181. ?assert({ok, Contents} =/= file:read_file(CachedFile)).
  182. good_disconnect(Config) ->
  183. Tmp = ?config(tmp_dir, Config),
  184. {Pkg,Vsn} = ?config(pkg, Config),
  185. State = ?config(state, Config),
  186. Cache = ?config(cache_dir, Config),
  187. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  188. ETagFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".etag">>),
  189. ?assert(filelib:is_regular(CachedFile)),
  190. {ok, Content} = file:read_file(CachedFile),
  191. rebar_pkg_resource:store_etag_in_cache(ETagFile, ?BADPKG_ETAG),
  192. ?assertEqual(ok,
  193. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum, #{}}, State, #{}, true)),
  194. {ok, Content} = file:read_file(CachedFile).
  195. bad_disconnect(Config) ->
  196. Tmp = ?config(tmp_dir, Config),
  197. {Pkg,Vsn} = ?config(pkg, Config),
  198. State = ?config(state, Config),
  199. ?assertEqual({fetch_fail, Pkg, Vsn},
  200. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum, #{}}, State, #{}, true)).
  201. pkgs_provider(Config) ->
  202. Config1 = rebar_test_utils:init_rebar_state(Config),
  203. rebar_test_utils:run_and_check(
  204. Config1, [], ["pkgs", "relx"],
  205. {ok, []}
  206. ).
  207. find_highest_matching(_Config) ->
  208. State = rebar_state:new(),
  209. {ok, Vsn} = rebar_packages:find_highest_matching_(
  210. <<"goodpkg">>, <<"1.0.0">>, #{name => <<"hexpm">>}, ?PACKAGE_TABLE, State),
  211. ?assertEqual(<<"1.0.1">>, Vsn),
  212. {ok, Vsn1} = rebar_packages:find_highest_matching(
  213. <<"goodpkg">>, <<"1.0">>, #{name => <<"hexpm">>}, ?PACKAGE_TABLE, State),
  214. ?assertEqual(<<"1.1.1">>, Vsn1),
  215. {ok, Vsn2} = rebar_packages:find_highest_matching(
  216. <<"goodpkg">>, <<"2.0">>, #{name => <<"hexpm">>}, ?PACKAGE_TABLE, State),
  217. ?assertEqual(<<"2.0.0">>, Vsn2),
  218. %% regression test. ~> constraints higher than the available packages would result
  219. %% in returning the first package version instead of 'none'.
  220. ?assertEqual(none, rebar_packages:find_highest_matching_(<<"goodpkg">>, <<"~> 5.0">>,
  221. #{name => <<"hexpm">>}, ?PACKAGE_TABLE, State)).
  222. %%%%%%%%%%%%%%%
  223. %%% Helpers %%%
  224. %%%%%%%%%%%%%%%
  225. mock_config(Name, Config) ->
  226. Priv = ?config(priv_dir, Config),
  227. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  228. TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]),
  229. Tid = ets:new(registry_table, [public]),
  230. AllDeps = [
  231. {{<<"badindexchk">>,<<"1.0.0">>}, [[], ?bad_checksum, [<<"rebar3">>]]},
  232. {{<<"goodpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  233. {{<<"goodpkg">>,<<"1.0.1">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  234. {{<<"goodpkg">>,<<"1.1.1">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  235. {{<<"goodpkg">>,<<"2.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  236. {{<<"badpkg">>,<<"1.0.0">>}, [[], ?badpkg_checksum, [<<"rebar3">>]]}
  237. ],
  238. ets:insert_new(Tid, AllDeps),
  239. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  240. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  241. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  242. catch ets:delete(?PACKAGE_TABLE),
  243. rebar_packages:new_package_table(),
  244. lists:foreach(fun({{N, Vsn}, [Deps, Checksum, _]}) ->
  245. case ets:member(?PACKAGE_TABLE, {ec_cnv:to_binary(N), Vsn, <<"hexpm">>}) of
  246. false ->
  247. ets:insert(?PACKAGE_TABLE, #package{key={ec_cnv:to_binary(N), Vsn, <<"hexpm">>},
  248. dependencies=Deps,
  249. retired=false,
  250. checksum=Checksum});
  251. true ->
  252. ok
  253. end
  254. end, AllDeps),
  255. meck:new(hex_repo, [passthrough]),
  256. meck:expect(hex_repo, get_package,
  257. fun(_Config, PkgName) ->
  258. Matches = ets:match_object(Tid, {{PkgName,'_'}, '_'}),
  259. Releases =
  260. [#{checksum => Checksum,
  261. version => Vsn,
  262. dependencies => Deps} ||
  263. {{_, Vsn}, [Deps, Checksum, _]} <- Matches],
  264. {ok, {200, #{}, #{releases => Releases}}}
  265. end),
  266. %% The state returns us a fake registry
  267. meck:new(rebar_state, [passthrough]),
  268. meck:expect(rebar_state, get,
  269. fun(_State, rebar_packages_cdn, _Default) ->
  270. "http://test.com/";
  271. (_, _, Default) ->
  272. Default
  273. end),
  274. meck:expect(rebar_state, resources,
  275. fun(_State) ->
  276. DefaultConfig = hex_core:default_config(),
  277. [rebar_resource_v2:new(pkg, rebar_pkg_resource,
  278. #{repos => [DefaultConfig#{name => <<"hexpm">>}],
  279. base_config => #{}})]
  280. end),
  281. meck:new(rebar_dir, [passthrough]),
  282. meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end),
  283. meck:expect(rebar_packages, registry_dir, fun(_) -> {ok, CacheDir} end),
  284. meck:expect(rebar_packages, package_dir, fun(_, _) -> {ok, CacheDir} end),
  285. meck:new(rebar_prv_update, [passthrough]),
  286. meck:expect(rebar_prv_update, do, fun(State) -> {ok, State} end),
  287. %% Cache fetches are mocked -- we assume the server and clients are
  288. %% correctly used.
  289. GoodCache = ?config(good_cache, Config),
  290. {Pkg,Vsn} = ?config(pkg, Config),
  291. PkgFile = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  292. {ok, PkgContents} = file:read_file(filename:join(?config(data_dir, Config), PkgFile)),
  293. meck:expect(hex_repo, get_tarball, fun(_, _, _) when GoodCache ->
  294. {ok, {304, #{<<"etag">> => ?good_etag}, <<>>}};
  295. (_, _, _) ->
  296. {ok, {200, #{<<"etag">> => ?good_etag}, PkgContents}}
  297. end),
  298. [{cache_root, CacheRoot},
  299. {cache_dir, CacheDir},
  300. {tmp_dir, TmpDir},
  301. {mock_table, Tid} | Config].
  302. unmock_config(Config) ->
  303. meck:unload(),
  304. catch ets:delete(?config(mock_table, Config)).
  305. copy_to_cache({Pkg,Vsn}, Config) ->
  306. Name = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  307. Source = filename:join(?config(data_dir, Config), Name),
  308. Dest = filename:join(?config(cache_dir, Config), Name),
  309. ec_file:copy(Source, Dest).