Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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