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.

311 linhas
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. -define(bad_etag, "abcdef").
  8. -define(good_etag, "22e1d7387c9085a462340088a2a8ba67").
  9. -define(bad_checksum, <<"D576B442A68C7B92BACDE1EFE9C6E54D8D6C74BDB71D8175B9D3C6EC8C7B62A7">>).
  10. -define(good_checksum, <<"1C6CE379D191FBAB41B7905075E0BF87CBBE23C77CECE775C5A0B786B2244C35">>).
  11. -define(BADPKG_ETAG, <<"BADETAG">>).
  12. all() -> [good_uncached, good_cached, badindexchk, badpkg,
  13. badhash_nocache, badhash_cache,
  14. bad_to_good, good_disconnect, bad_disconnect, pkgs_provider,
  15. find_highest_matching].
  16. init_per_suite(Config) ->
  17. application:start(meck),
  18. Config.
  19. end_per_suite(_Config) ->
  20. application:stop(meck).
  21. init_per_testcase(pkgs_provider=Name, Config) ->
  22. %% Need to mock out a registry for this test now because it will try to update it automatically
  23. Priv = ?config(priv_dir, Config),
  24. Tid = ets:new(registry_table, [public]),
  25. ets:insert_new(Tid, []),
  26. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  27. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  28. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  29. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  30. meck:new(rebar_packages, [passthrough]),
  31. meck:expect(rebar_packages, registry_dir, fun(_) -> {ok, CacheDir} end),
  32. meck:expect(rebar_packages, package_dir, fun(_) -> {ok, CacheDir} end),
  33. rebar_prv_update:hex_to_index(rebar_state:new()),
  34. Config;
  35. init_per_testcase(good_uncached=Name, Config0) ->
  36. Config = [{good_cache, false},
  37. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  38. | Config0],
  39. mock_config(Name, Config);
  40. init_per_testcase(good_cached=Name, Config0) ->
  41. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  42. Config1 = [{good_cache, true},
  43. {pkg, Pkg}
  44. | Config0],
  45. Config = mock_config(Name, Config1),
  46. copy_to_cache(Pkg, Config),
  47. Config;
  48. init_per_testcase(badindexchk=Name, Config0) ->
  49. Config = [{good_cache, false},
  50. {pkg, {<<"badindexchk">>, <<"1.0.0">>}}
  51. | Config0],
  52. mock_config(Name, Config);
  53. init_per_testcase(badpkg=Name, Config0) ->
  54. Config = [{good_cache, false},
  55. {pkg, {<<"badpkg">>, <<"1.0.0">>}}
  56. | Config0],
  57. mock_config(Name, Config);
  58. init_per_testcase(badhash_nocache=Name, Config0) ->
  59. Config = [{good_cache, false},
  60. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  61. | Config0],
  62. mock_config(Name, Config);
  63. init_per_testcase(badhash_cache=Name, Config0) ->
  64. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  65. Config1 = [{good_cache, true},
  66. {pkg, Pkg}
  67. | Config0],
  68. Config = mock_config(Name, Config1),
  69. copy_to_cache(Pkg, Config),
  70. Config;
  71. init_per_testcase(bad_to_good=Name, Config0) ->
  72. Config1 = [{good_cache, false},
  73. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  74. | Config0],
  75. Config = mock_config(Name, Config1),
  76. Source = filename:join(?config(data_dir, Config), <<"badpkg-1.0.0.tar">>),
  77. Dest = filename:join(?config(cache_dir, Config), <<"goodpkg-1.0.0.tar">>),
  78. ec_file:copy(Source, Dest),
  79. Config;
  80. init_per_testcase(good_disconnect=Name, Config0) ->
  81. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  82. Config1 = [{good_cache, false},
  83. {pkg, Pkg}
  84. | Config0],
  85. Config = mock_config(Name, Config1),
  86. copy_to_cache(Pkg, Config),
  87. meck:unload(httpc),
  88. meck:new(httpc, [passthrough, unsticky]),
  89. meck:expect(httpc, request, fun(_, _, _, _, _) -> {error, econnrefused} end),
  90. Config;
  91. init_per_testcase(bad_disconnect=Name, Config0) ->
  92. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  93. Config1 = [{good_cache, false},
  94. {pkg, Pkg}
  95. | Config0],
  96. Config = mock_config(Name, Config1),
  97. meck:unload(httpc),
  98. meck:new(httpc, [passthrough, unsticky]),
  99. meck:expect(httpc, request, fun(_, _, _, _, _) -> {error, econnrefused} 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)),
  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)),
  127. {ok, Content} = file:read_file(CachedFile).
  128. badindexchk(Config) ->
  129. Tmp = ?config(tmp_dir, Config),
  130. {Pkg,Vsn} = ?config(pkg, Config),
  131. State = ?config(state, Config),
  132. ?assertMatch({bad_registry_checksum, _Path},
  133. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State)),
  134. %% The cached file is there for forensic purposes
  135. Cache = ?config(cache_dir, Config),
  136. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  137. badpkg(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. CachePath = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  143. ETagPath = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".etag">>),
  144. rebar_pkg_resource:store_etag_in_cache(ETagPath, ?BADPKG_ETAG),
  145. ?assertMatch({bad_download, _Path},
  146. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State, false)),
  147. %% The cached/etag files are there for forensic purposes
  148. ?assert(filelib:is_regular(ETagPath)),
  149. ?assert(filelib:is_regular(CachePath)).
  150. badhash_nocache(Config) ->
  151. Tmp = ?config(tmp_dir, Config),
  152. {Pkg,Vsn} = ?config(pkg, Config),
  153. State = ?config(state, Config),
  154. ?assertMatch({unexpected_hash, _Path, ?bad_checksum, ?good_checksum},
  155. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?bad_checksum}, State)),
  156. %% The cached file is there for forensic purposes
  157. Cache = ?config(cache_dir, Config),
  158. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  159. badhash_cache(Config) ->
  160. Tmp = ?config(tmp_dir, Config),
  161. {Pkg,Vsn} = ?config(pkg, Config),
  162. Cache = ?config(cache_dir, Config),
  163. State = ?config(state, Config),
  164. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  165. ?assert(filelib:is_regular(CachedFile)),
  166. {ok, Content} = file:read_file(CachedFile),
  167. ?assertMatch({unexpected_hash, _Path, ?bad_checksum, ?good_checksum},
  168. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?bad_checksum}, State)),
  169. %% The cached file is there still, unchanged.
  170. ?assert(filelib:is_regular(CachedFile)),
  171. ?assertEqual({ok, Content}, file:read_file(CachedFile)).
  172. bad_to_good(Config) ->
  173. Tmp = ?config(tmp_dir, Config),
  174. {Pkg,Vsn} = ?config(pkg, Config),
  175. State = ?config(state, Config),
  176. Cache = ?config(cache_dir, Config),
  177. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  178. ?assert(filelib:is_regular(CachedFile)),
  179. {ok, Contents} = file:read_file(CachedFile),
  180. ?assertEqual({ok, true},
  181. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State)),
  182. %% Cache has refreshed
  183. ?assert({ok, Contents} =/= file:read_file(CachedFile)).
  184. good_disconnect(Config) ->
  185. Tmp = ?config(tmp_dir, Config),
  186. {Pkg,Vsn} = ?config(pkg, Config),
  187. State = ?config(state, Config),
  188. Cache = ?config(cache_dir, Config),
  189. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  190. ETagFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".etag">>),
  191. ?assert(filelib:is_regular(CachedFile)),
  192. {ok, Content} = file:read_file(CachedFile),
  193. rebar_pkg_resource:store_etag_in_cache(ETagFile, ?BADPKG_ETAG),
  194. ?assertEqual({ok, true},
  195. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State)),
  196. {ok, Content} = file:read_file(CachedFile).
  197. bad_disconnect(Config) ->
  198. Tmp = ?config(tmp_dir, Config),
  199. {Pkg,Vsn} = ?config(pkg, Config),
  200. State = ?config(state, Config),
  201. ?assertEqual({fetch_fail, Pkg, Vsn},
  202. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn, ?good_checksum}, State)).
  203. pkgs_provider(Config) ->
  204. Config1 = rebar_test_utils:init_rebar_state(Config),
  205. rebar_test_utils:run_and_check(
  206. Config1, [], ["pkgs"],
  207. {ok, []}
  208. ).
  209. find_highest_matching(_Config) ->
  210. State = rebar_state:new(),
  211. {ok, Vsn} = rebar_packages:find_highest_matching(
  212. <<"test">>, <<"1.0.0">>, <<"goodpkg">>, <<"1.0.0">>, package_index, State),
  213. ?assertEqual(<<"1.0.1">>, Vsn),
  214. {ok, Vsn1} = rebar_packages:find_highest_matching(
  215. <<"test">>, <<"1.0.0">>, <<"goodpkg">>, <<"1.0">>, package_index, State),
  216. ?assertEqual(<<"1.1.1">>, Vsn1),
  217. {ok, Vsn2} = rebar_packages:find_highest_matching(
  218. <<"test">>, <<"1.0.0">>, <<"goodpkg">>, <<"2.0">>, package_index, State),
  219. ?assertEqual(<<"2.0.0">>, Vsn2),
  220. %% regression test. ~> constraints higher than the available packages would result
  221. %% in returning the first package version instead of 'none'.
  222. ?assertEqual(none, rebar_packages:find_highest_matching(<<"test">>, <<"1.0.0">>, <<"goodpkg">>,
  223. <<"~> 5.0">>, package_index, State)).
  224. %%%%%%%%%%%%%%%
  225. %%% Helpers %%%
  226. %%%%%%%%%%%%%%%
  227. mock_config(Name, Config) ->
  228. Priv = ?config(priv_dir, Config),
  229. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  230. TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]),
  231. Tid = ets:new(registry_table, [public]),
  232. ets:insert_new(Tid, [
  233. {<<"badindexchk">>,[[<<"1.0.0">>]]},
  234. {<<"goodpkg">>,[[<<"1.0.0">>, <<"1.0.1">>, <<"1.1.1">>, <<"2.0.0">>]]},
  235. {<<"badpkg">>,[[<<"1.0.0">>]]},
  236. {{<<"badindexchk">>,<<"1.0.0">>}, [[], ?bad_checksum, [<<"rebar3">>]]},
  237. {{<<"goodpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  238. {{<<"goodpkg">>,<<"1.0.1">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  239. {{<<"goodpkg">>,<<"1.1.1">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  240. {{<<"goodpkg">>,<<"2.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  241. {{<<"badpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]}
  242. ]),
  243. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  244. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  245. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  246. %% The state returns us a fake registry
  247. meck:new(rebar_state, [passthrough]),
  248. meck:expect(rebar_state, get,
  249. fun(_State, rebar_packages_cdn, _Default) ->
  250. "http://test.com/"
  251. end),
  252. meck:new(rebar_dir, [passthrough]),
  253. meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end),
  254. meck:new(rebar_packages, [passthrough]),
  255. meck:expect(rebar_packages, registry_dir, fun(_) -> {ok, CacheDir} end),
  256. meck:expect(rebar_packages, package_dir, fun(_) -> {ok, CacheDir} end),
  257. rebar_prv_update:hex_to_index(rebar_state:new()),
  258. meck:new(rebar_prv_update, [passthrough]),
  259. meck:expect(rebar_prv_update, do, fun(State) -> {ok, State} end),
  260. %% Cache fetches are mocked -- we assume the server and clients are
  261. %% correctly used.
  262. GoodCache = ?config(good_cache, Config),
  263. {Pkg,Vsn} = ?config(pkg, Config),
  264. PkgFile = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  265. {ok, PkgContents} = file:read_file(filename:join(?config(data_dir, Config), PkgFile)),
  266. meck:new(httpc, [passthrough, unsticky]),
  267. meck:expect(httpc, request,
  268. fun(get, {_Url, _Opts}, _, _, _) when GoodCache ->
  269. {ok, {{Vsn, 304, <<"Not Modified">>}, [{"etag", ?good_etag}], <<>>}};
  270. (get, {_Url, _Opts}, _, _, _) ->
  271. {ok, {{Vsn, 200, <<"OK">>}, [{"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).