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.

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